Search in sources :

Example 36 with Texture2D

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

the class TextureHelper method loadImageAsTexture.

/**
     * This class returns a texture read from the file or from packed blender
     * data.
     * 
     * @param imageStructure
     *            image structure filled with data
     * @param imaflag
     *            the image flag
     * @param blenderContext
     *            the blender context
     * @return the texture that can be used by JME engine
     * @throws BlenderFileException
     *             this exception is thrown when the blend file structure is
     *             somehow invalid or corrupted
     */
public Texture loadImageAsTexture(Structure imageStructure, int imaflag, BlenderContext blenderContext) throws BlenderFileException {
    LOGGER.log(Level.FINE, "Fetching texture with OMA = {0}", imageStructure.getOldMemoryAddress());
    Texture result = null;
    Image im = (Image) blenderContext.getLoadedFeature(imageStructure.getOldMemoryAddress(), LoadedDataType.FEATURE);
    // if (im == null) {  HACK force reaload always, as constructor in else case is destroying the TextureKeys!
    if ("ID".equals(imageStructure.getType())) {
        LOGGER.fine("Loading texture from external blend file.");
        result = (Texture) this.loadLibrary(imageStructure);
    } else {
        String texturePath = imageStructure.getFieldValue("name").toString();
        Pointer pPackedFile = (Pointer) imageStructure.getFieldValue("packedfile");
        if (pPackedFile.isNull()) {
            LOGGER.log(Level.FINE, "Reading texture from file: {0}", texturePath);
            result = this.loadImageFromFile(texturePath, imaflag, blenderContext);
        } else {
            LOGGER.fine("Packed texture. Reading directly from the blend file!");
            Structure packedFile = pPackedFile.fetchData().get(0);
            Pointer pData = (Pointer) packedFile.getFieldValue("data");
            FileBlockHeader dataFileBlock = blenderContext.getFileBlock(pData.getOldMemoryAddress());
            blenderContext.getInputStream().setPosition(dataFileBlock.getBlockPosition());
            // Should the texture be flipped? It works for sinbad ..
            result = new Texture2D(new ImageLoader().loadImage(blenderContext.getInputStream(), dataFileBlock.getBlockPosition(), true));
        }
    }
    if (result != null) {
        // render result is not being loaded
        blenderContext.addLoadedFeatures(imageStructure.getOldMemoryAddress(), LoadedDataType.STRUCTURE, imageStructure);
        blenderContext.addLoadedFeatures(imageStructure.getOldMemoryAddress(), LoadedDataType.FEATURE, result.getImage());
        result.setName(imageStructure.getName());
    }
    return result;
}
Also used : Texture2D(com.jme3.texture.Texture2D) FileBlockHeader(com.jme3.scene.plugins.blender.file.FileBlockHeader) Pointer(com.jme3.scene.plugins.blender.file.Pointer) Image(com.jme3.texture.Image) Structure(com.jme3.scene.plugins.blender.file.Structure) Texture(com.jme3.texture.Texture)

Example 37 with Texture2D

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

the class EnvMapUtils method getCubeMapCrossDebugViewWithMipMaps.

public static Node getCubeMapCrossDebugViewWithMipMaps(TextureCubeMap cubeMap, AssetManager assetManager) {
    Node n = new Node("CubeMapDebug" + cubeMap.getName());
    int size = cubeMap.getImage().getWidth();
    int nbMips = cubeMap.getImage().getMipMapSizes().length;
    Picture[] pics = new Picture[6 * nbMips];
    // 128f / (float) size;
    float ratio = 1f;
    int offset = 0;
    int guiOffset = 0;
    for (int mipLevel = 0; mipLevel < nbMips; mipLevel++) {
        size = Math.max(1, cubeMap.getImage().getWidth() >> mipLevel);
        int dataSize = cubeMap.getImage().getMipMapSizes()[mipLevel];
        byte[] dataArray = new byte[dataSize];
        for (int i = 0; i < 6; i++) {
            ByteBuffer bb = cubeMap.getImage().getData(i);
            bb.rewind();
            bb.position(offset);
            bb.get(dataArray, 0, dataSize);
            ByteBuffer data = BufferUtils.createByteBuffer(dataArray);
            pics[i] = new Picture("bla");
            Texture2D tex = new Texture2D(new Image(cubeMap.getImage().getFormat(), size, size, data, cubeMap.getImage().getColorSpace()));
            pics[i].setTexture(assetManager, tex, true);
            pics[i].setWidth(size);
            pics[i].setHeight(size);
            n.attachChild(pics[i]);
        }
        pics[0].setLocalTranslation(guiOffset + size, guiOffset + size * 2, 1);
        pics[0].setLocalRotation(new Quaternion().fromAngleAxis(PI, Vector3f.UNIT_Z));
        pics[1].setLocalTranslation(guiOffset + size * 3, guiOffset + size * 2, 1);
        pics[1].setLocalRotation(new Quaternion().fromAngleAxis(PI, Vector3f.UNIT_Z));
        pics[2].setLocalTranslation(guiOffset + size * 2, guiOffset + size * 3, 1);
        pics[2].setLocalRotation(new Quaternion().fromAngleAxis(PI, Vector3f.UNIT_Z));
        pics[3].setLocalTranslation(guiOffset + size * 2, guiOffset + size, 1);
        pics[3].setLocalRotation(new Quaternion().fromAngleAxis(PI, Vector3f.UNIT_Z));
        pics[4].setLocalTranslation(guiOffset + size * 2, guiOffset + size * 2, 1);
        pics[4].setLocalRotation(new Quaternion().fromAngleAxis(PI, Vector3f.UNIT_Z));
        pics[5].setLocalTranslation(guiOffset + size * 4, guiOffset + size * 2, 1);
        pics[5].setLocalRotation(new Quaternion().fromAngleAxis(PI, Vector3f.UNIT_Z));
        guiOffset += size * 2 + 1;
        offset += dataSize;
    }
    Quad q = new Quad(cubeMap.getImage().getWidth() * 4 + nbMips, guiOffset + size);
    Geometry g = new Geometry("bg", q);
    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat.setColor("Color", ColorRGBA.Black);
    g.setMaterial(mat);
    g.setLocalTranslation(0, 0, 0);
    n.attachChild(g);
    n.setLocalScale(ratio);
    return n;
}
Also used : Quad(com.jme3.scene.shape.Quad) Texture2D(com.jme3.texture.Texture2D) Quaternion(com.jme3.math.Quaternion) Node(com.jme3.scene.Node) Material(com.jme3.material.Material) Image(com.jme3.texture.Image) ByteBuffer(java.nio.ByteBuffer) Geometry(com.jme3.scene.Geometry) Picture(com.jme3.ui.Picture)

Example 38 with Texture2D

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

the class EnvironmentCamera method initialize.

@Override
protected void initialize(Application app) {
    this.backGroundColor = app.getViewPort().getBackgroundColor();
    final Camera[] cameras = new Camera[6];
    Texture2D[] textures = new Texture2D[6];
    viewports = new ViewPort[6];
    framebuffers = new FrameBuffer[6];
    buffers = new ByteBuffer[6];
    images = new Image[6];
    for (int i = 0; i < 6; i++) {
        cameras[i] = createOffCamera(size, position, axisX[i], axisY[i], axisZ[i]);
        viewports[i] = createOffViewPort("EnvView" + i, cameras[i]);
        framebuffers[i] = createOffScreenFrameBuffer(size, viewports[i]);
        textures[i] = new Texture2D(size, size, imageFormat);
        framebuffers[i].setColorTexture(textures[i]);
    }
}
Also used : Texture2D(com.jme3.texture.Texture2D) Camera(com.jme3.renderer.Camera)

Example 39 with Texture2D

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

the class AbstractShadowRenderer method setEdgeFilteringMode.

/**
     * Sets the filtering mode for shadow edges. See {@link EdgeFilteringMode}
     * for more info.
     *
     * @param filterMode the desired filter mode (not null)
     */
public final void setEdgeFilteringMode(EdgeFilteringMode filterMode) {
    if (filterMode == null) {
        throw new NullPointerException();
    }
    this.edgeFilteringMode = filterMode;
    postshadowMat.setInt("FilterMode", filterMode.getMaterialParamValue());
    postshadowMat.setFloat("PCFEdge", edgesThickness);
    if (shadowCompareMode == CompareMode.Hardware) {
        for (Texture2D shadowMap : shadowMaps) {
            if (filterMode == EdgeFilteringMode.Bilinear) {
                shadowMap.setMagFilter(MagFilter.Bilinear);
                shadowMap.setMinFilter(MinFilter.BilinearNoMipMaps);
            } else {
                shadowMap.setMagFilter(MagFilter.Nearest);
                shadowMap.setMinFilter(MinFilter.NearestNoMipMaps);
            }
        }
    }
}
Also used : Texture2D(com.jme3.texture.Texture2D)

Example 40 with Texture2D

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

the class PssmShadowRenderer method setCompareMode.

/**
     * sets the shadow compare mode see {@link CompareMode} for more info
     *
     * @param compareMode
     */
public final void setCompareMode(CompareMode compareMode) {
    if (compareMode == null) {
        throw new NullPointerException();
    }
    if (this.compareMode == compareMode) {
        return;
    }
    this.compareMode = compareMode;
    for (Texture2D shadowMap : shadowMaps) {
        if (compareMode == CompareMode.Hardware) {
            shadowMap.setShadowCompareMode(ShadowCompareMode.LessOrEqual);
            if (filterMode == FilterMode.Bilinear) {
                shadowMap.setMagFilter(MagFilter.Bilinear);
                shadowMap.setMinFilter(MinFilter.BilinearNoMipMaps);
            } else {
                shadowMap.setMagFilter(MagFilter.Nearest);
                shadowMap.setMinFilter(MinFilter.NearestNoMipMaps);
            }
        } else {
            shadowMap.setShadowCompareMode(ShadowCompareMode.Off);
            shadowMap.setMagFilter(MagFilter.Nearest);
            shadowMap.setMinFilter(MinFilter.NearestNoMipMaps);
        }
    }
    postshadowMat.setBoolean("HardwareShadows", compareMode == CompareMode.Hardware);
    applyHWShadows = true;
}
Also used : Texture2D(com.jme3.texture.Texture2D)

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