Search in sources :

Example 11 with Texture2D

use of com.jme3.texture.Texture2D in project jmonkeyengine by jMonkeyEngine.

the class TriangulatedTexture method getResultTexture.

/**
     * This method returns the flat texture. It is calculated if required or if
     * it was not created before. Images that are identical are discarded to
     * reduce the texture size.
     * 
     * @param rebuild
     *            a variable that forces texture recomputation (even if it was
     *            computed vefore)
     * @return flat result texture (all images merged into one)
     */
public Texture2D getResultTexture(boolean rebuild) {
    if (resultTexture == null || rebuild) {
        // sorting the parts by their height (from highest to the lowest)
        List<TriangleTextureElement> list = new ArrayList<TriangleTextureElement>(faceTextures);
        Collections.sort(list, new Comparator<TriangleTextureElement>() {

            public int compare(TriangleTextureElement o1, TriangleTextureElement o2) {
                return o2.image.getHeight() - o1.image.getHeight();
            }
        });
        // arraging the images on the resulting image (calculating the result image width and height)
        Set<Integer> duplicatedFaceIndexes = new HashSet<Integer>();
        int resultImageHeight = list.get(0).image.getHeight();
        int resultImageWidth = 0;
        int currentXPos = 0, currentYPos = 0;
        Map<TriangleTextureElement, Integer[]> imageLayoutData = new HashMap<TriangleTextureElement, Integer[]>(list.size());
        while (list.size() > 0) {
            TriangleTextureElement currentElement = list.remove(0);
            if (currentXPos + currentElement.image.getWidth() > maxTextureSize) {
                currentXPos = 0;
                currentYPos = resultImageHeight;
                resultImageHeight += currentElement.image.getHeight();
            }
            Integer[] currentPositions = new Integer[] { currentXPos, currentYPos };
            imageLayoutData.put(currentElement, currentPositions);
            if (keepIdenticalTextures) {
                // removing identical images
                for (int i = 0; i < list.size(); ++i) {
                    if (currentElement.image.equals(list.get(i).image)) {
                        duplicatedFaceIndexes.add(list.get(i).faceIndex);
                        imageLayoutData.put(list.remove(i--), currentPositions);
                    }
                }
            }
            currentXPos += currentElement.image.getWidth();
            resultImageWidth = Math.max(resultImageWidth, currentXPos);
        // currentYPos += currentElement.image.getHeight();
        // TODO: implement that to compact the result image
        // try to add smaller images below the current one
        // int remainingHeight = resultImageHeight -
        // currentElement.image.getHeight();
        // while(remainingHeight > 0) {
        // for(int i=list.size() - 1;i>=0;--i) {
        //
        // }
        // }
        }
        // computing the result UV coordinates
        resultUVS = new ArrayList<Vector2f>(imageLayoutData.size() * 3);
        for (int i = 0; i < imageLayoutData.size() * 3; ++i) {
            resultUVS.add(null);
        }
        Vector2f[] uvs = new Vector2f[3];
        for (Entry<TriangleTextureElement, Integer[]> entry : imageLayoutData.entrySet()) {
            Integer[] position = entry.getValue();
            entry.getKey().computeFinalUVCoordinates(resultImageWidth, resultImageHeight, position[0], position[1], uvs);
            resultUVS.set(entry.getKey().faceIndex * 3, uvs[0]);
            resultUVS.set(entry.getKey().faceIndex * 3 + 1, uvs[1]);
            resultUVS.set(entry.getKey().faceIndex * 3 + 2, uvs[2]);
        }
        Image resultImage = new Image(format, resultImageWidth, resultImageHeight, BufferUtils.createByteBuffer(resultImageWidth * resultImageHeight * (format.getBitsPerPixel() >> 3)), ColorSpace.Linear);
        resultTexture = new Texture2D(resultImage);
        for (Entry<TriangleTextureElement, Integer[]> entry : imageLayoutData.entrySet()) {
            if (!duplicatedFaceIndexes.contains(entry.getKey().faceIndex)) {
                this.draw(resultImage, entry.getKey().image, entry.getValue()[0], entry.getValue()[1]);
            }
        }
        // setting additional data
        resultTexture.setWrap(WrapAxis.S, this.getWrap(WrapAxis.S));
        resultTexture.setWrap(WrapAxis.T, this.getWrap(WrapAxis.T));
        resultTexture.setMagFilter(this.getMagFilter());
        resultTexture.setMinFilter(this.getMinFilter());
    }
    return resultTexture;
}
Also used : Texture2D(com.jme3.texture.Texture2D) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Image(com.jme3.texture.Image) BufferedImage(java.awt.image.BufferedImage) Vector2f(com.jme3.math.Vector2f) HashSet(java.util.HashSet)

Example 12 with Texture2D

use of com.jme3.texture.Texture2D in project jmonkeyengine by jMonkeyEngine.

the class TestPostFiltersCompositing method simpleInitApp.

public void simpleInitApp() {
    this.flyCam.setMoveSpeed(10);
    cam.setLocation(new Vector3f(0.028406568f, 2.015769f, 7.386517f));
    cam.setRotation(new Quaternion(-1.0729783E-5f, 0.9999721f, -0.0073241726f, -0.0014647911f));
    makeScene();
    //Creating the main view port post processor
    FilterPostProcessor fpp = new FilterPostProcessor(assetManager);
    fpp.addFilter(new ColorOverlayFilter(ColorRGBA.Blue));
    viewPort.addProcessor(fpp);
    //creating a frame buffer for the mainviewport
    FrameBuffer mainVPFrameBuffer = new FrameBuffer(cam.getWidth(), cam.getHeight(), 1);
    Texture2D mainVPTexture = new Texture2D(cam.getWidth(), cam.getHeight(), Image.Format.RGBA8);
    mainVPFrameBuffer.addColorTexture(mainVPTexture);
    mainVPFrameBuffer.setDepthBuffer(Image.Format.Depth);
    viewPort.setOutputFrameBuffer(mainVPFrameBuffer);
    //creating the post processor for the gui viewport
    final FilterPostProcessor guifpp = new FilterPostProcessor(assetManager);
    guifpp.setFrameBufferFormat(Image.Format.RGBA8);
    guifpp.addFilter(new ColorOverlayFilter(ColorRGBA.Red));
    //this will compose the main viewport texture with the guiviewport back buffer.
    //Note that you can switch the order of the filters so that guiviewport filters are applied or not to the main viewport texture
    guifpp.addFilter(new ComposeFilter(mainVPTexture));
    guiViewPort.addProcessor(guifpp);
    //compositing is done by mixing texture depending on the alpha channel, 
    //it's important that the guiviewport clear color alpha value is set to 0
    guiViewPort.setBackgroundColor(ColorRGBA.BlackNoAlpha);
    guiViewPort.setClearColor(true);
}
Also used : Texture2D(com.jme3.texture.Texture2D) ColorOverlayFilter(com.jme3.post.filters.ColorOverlayFilter) ComposeFilter(com.jme3.post.filters.ComposeFilter) Quaternion(com.jme3.math.Quaternion) Vector3f(com.jme3.math.Vector3f) FilterPostProcessor(com.jme3.post.FilterPostProcessor) FrameBuffer(com.jme3.texture.FrameBuffer)

Example 13 with Texture2D

use of com.jme3.texture.Texture2D in project jmonkeyengine by jMonkeyEngine.

the class VRMouseManager method setImage.

/**
     * Set the image to use as mouse cursor. The given string describe an asset that the underlying application asset manager has to load.
     * @param texture the image to use as mouse cursor.
     */
public void setImage(String texture) {
    if (environment != null) {
        if (environment.getApplication() != null) {
            if (environment.isInVR() == false) {
                Texture tex = environment.getApplication().getAssetManager().loadTexture(texture);
                mouseImage.setTexture(environment.getApplication().getAssetManager(), (Texture2D) tex, true);
                ySize = tex.getImage().getHeight();
                mouseImage.setHeight(ySize);
                mouseImage.setWidth(tex.getImage().getWidth());
                mouseImage.getMaterial().getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
                mouseImage.getMaterial().getAdditionalRenderState().setDepthWrite(false);
            } else {
                Texture tex = environment.getApplication().getAssetManager().loadTexture(texture);
                mouseImage.setTexture(environment.getApplication().getAssetManager(), (Texture2D) tex, true);
                ySize = tex.getImage().getHeight();
                mouseImage.setHeight(ySize);
                mouseImage.setWidth(tex.getImage().getWidth());
                mouseImage.getMaterial().getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
                mouseImage.getMaterial().getAdditionalRenderState().setDepthWrite(false);
            }
        } else {
            throw new IllegalStateException("This VR environment is not attached to any application.");
        }
    } else {
        throw new IllegalStateException("This VR view manager is not attached to any VR environment.");
    }
}
Also used : Texture(com.jme3.texture.Texture)

Example 14 with Texture2D

use of com.jme3.texture.Texture2D in project jmonkeyengine by jMonkeyEngine.

the class VRViewManagerOSVR method setupFinalFullTexture.

private void setupFinalFullTexture(Camera cam) {
    if (environment != null) {
        if (environment.getApplication() != null) {
            // create offscreen framebuffer
            FrameBuffer out = new FrameBuffer(cam.getWidth(), cam.getHeight(), 1);
            //offBuffer.setSrgb(true);
            //setup framebuffer's texture
            dualEyeTex = new Texture2D(cam.getWidth(), cam.getHeight(), Image.Format.RGBA8);
            dualEyeTex.setMinFilter(Texture.MinFilter.BilinearNoMipMaps);
            dualEyeTex.setMagFilter(Texture.MagFilter.Bilinear);
            logger.config("Dual eye texture " + dualEyeTex.getName() + " (" + dualEyeTex.getImage().getId() + ")");
            logger.config("               Type: " + dualEyeTex.getType());
            logger.config("               Size: " + dualEyeTex.getImage().getWidth() + "x" + dualEyeTex.getImage().getHeight());
            logger.config("        Image depth: " + dualEyeTex.getImage().getDepth());
            logger.config("       Image format: " + dualEyeTex.getImage().getFormat());
            logger.config("  Image color space: " + dualEyeTex.getImage().getColorSpace());
            //setup framebuffer to use texture
            out.setDepthBuffer(Image.Format.Depth);
            out.setColorTexture(dualEyeTex);
            ViewPort viewPort = environment.getApplication().getViewPort();
            viewPort.setClearFlags(true, true, true);
            viewPort.setBackgroundColor(ColorRGBA.Black);
            viewPort.setOutputFrameBuffer(out);
        } else {
            throw new IllegalStateException("This VR environment is not attached to any application.");
        }
    } else {
        throw new IllegalStateException("This VR view manager is not attached to any VR environment.");
    }
}
Also used : Texture2D(com.jme3.texture.Texture2D) ViewPort(com.jme3.renderer.ViewPort) FrameBuffer(com.jme3.texture.FrameBuffer)

Example 15 with Texture2D

use of com.jme3.texture.Texture2D in project jmonkeyengine by jMonkeyEngine.

the class VRViewManagerOSVR method setupViewBuffers.

private ViewPort setupViewBuffers(Camera cam, String viewName) {
    if (environment != null) {
        if (environment.getApplication() != null) {
            // create offscreen framebuffer
            FrameBuffer offBufferLeft = new FrameBuffer(cam.getWidth(), cam.getHeight(), 1);
            //offBufferLeft.setSrgb(true);
            //setup framebuffer's texture
            Texture2D offTex = new Texture2D(cam.getWidth(), cam.getHeight(), Image.Format.RGBA8);
            offTex.setMinFilter(Texture.MinFilter.BilinearNoMipMaps);
            offTex.setMagFilter(Texture.MagFilter.Bilinear);
            //setup framebuffer to use texture
            offBufferLeft.setDepthBuffer(Image.Format.Depth);
            offBufferLeft.setColorTexture(offTex);
            ViewPort viewPort = environment.getApplication().getRenderManager().createPreView(viewName, cam);
            viewPort.setClearFlags(true, true, true);
            viewPort.setBackgroundColor(ColorRGBA.Black);
            Iterator<Spatial> spatialIter = environment.getApplication().getViewPort().getScenes().iterator();
            while (spatialIter.hasNext()) {
                viewPort.attachScene(spatialIter.next());
            }
            //set viewport to render to offscreen framebuffer
            viewPort.setOutputFrameBuffer(offBufferLeft);
            return viewPort;
        } else {
            throw new IllegalStateException("This VR environment is not attached to any application.");
        }
    } else {
        throw new IllegalStateException("This VR view manager is not attached to any VR environment.");
    }
}
Also used : Texture2D(com.jme3.texture.Texture2D) Spatial(com.jme3.scene.Spatial) ViewPort(com.jme3.renderer.ViewPort) FrameBuffer(com.jme3.texture.FrameBuffer)

Aggregations

Texture2D (com.jme3.texture.Texture2D)53 Material (com.jme3.material.Material)19 FrameBuffer (com.jme3.texture.FrameBuffer)18 Picture (com.jme3.ui.Picture)12 Geometry (com.jme3.scene.Geometry)11 Image (com.jme3.texture.Image)10 ViewPort (com.jme3.renderer.ViewPort)9 Texture (com.jme3.texture.Texture)9 Camera (com.jme3.renderer.Camera)8 Node (com.jme3.scene.Node)7 Spatial (com.jme3.scene.Spatial)7 TextureKey (com.jme3.asset.TextureKey)6 ByteBuffer (java.nio.ByteBuffer)6 Quaternion (com.jme3.math.Quaternion)5 Vector3f (com.jme3.math.Vector3f)5 Test (org.junit.Test)4 DirectionalLight (com.jme3.light.DirectionalLight)3 FilterPostProcessor (com.jme3.post.FilterPostProcessor)3 Box (com.jme3.scene.shape.Box)3 BufferedImage (java.awt.image.BufferedImage)3