Search in sources :

Example 1 with Texture

use of com.ardor3d.image.Texture in project energy3d by concord-consortium.

the class TriangleMeshLib method createMeshes.

private static List<Mesh> createMeshes(final ArrayList<GroupData> groups) {
    final List<Mesh> results = new ArrayList<Mesh>();
    for (final GroupData group : groups) {
        final Mesh mesh = new Mesh();
        mesh.setUserData(group.key);
        mesh.setModelBound(new BoundingBox());
        results.add(mesh);
        final FloatBuffer vertexBuffer = BufferUtils.createVector3Buffer(group.vertices.size());
        mesh.getMeshData().setVertexBuffer(vertexBuffer);
        for (final ReadOnlyVector3 v : group.vertices) {
            vertexBuffer.put(v.getXf()).put(v.getYf()).put(v.getZf());
        }
        final FloatBuffer normalBuffer = BufferUtils.createFloatBuffer(vertexBuffer.limit());
        mesh.getMeshData().setNormalBuffer(normalBuffer);
        for (final ReadOnlyVector3 v : group.normals) {
            normalBuffer.put(v.getXf()).put(v.getYf()).put(v.getZf());
        }
        if (!group.textures.isEmpty()) {
            final FloatBuffer textureBuffer = BufferUtils.createVector2Buffer(group.textures.size());
            mesh.getMeshData().setTextureBuffer(textureBuffer, 0);
            for (final ReadOnlyVector2 v : group.textures) {
                textureBuffer.put(v.getXf()).put(v.getYf());
            }
            if (group.textureImage != null) {
                final Texture texture = TextureManager.loadFromImage(group.textureImage, Texture.MinificationFilter.Trilinear, TextureStoreFormat.GuessNoCompressedFormat);
                final TextureState ts = new TextureState();
                ts.setTexture(texture);
                mesh.setRenderState(ts);
            }
        }
        mesh.updateModelBound();
    }
    return results;
}
Also used : ReadOnlyVector2(com.ardor3d.math.type.ReadOnlyVector2) ReadOnlyVector3(com.ardor3d.math.type.ReadOnlyVector3) TextureState(com.ardor3d.renderer.state.TextureState) BoundingBox(com.ardor3d.bounding.BoundingBox) ArrayList(java.util.ArrayList) Mesh(com.ardor3d.scenegraph.Mesh) FloatBuffer(java.nio.FloatBuffer) Texture(com.ardor3d.image.Texture) GroupData(org.concord.energy3d.util.MeshLib.GroupData)

Example 2 with Texture

use of com.ardor3d.image.Texture in project energy3d by concord-consortium.

the class HousePart method updateTextureAndColor.

protected void updateTextureAndColor(final Mesh mesh, final ReadOnlyColorRGBA defaultColor, final TextureMode textureMode) {
    if (this instanceof Tree) {
        // special treatment because the same mesh of a tree has two textures (shed or not)
        final TextureState ts = new TextureState();
        final Texture texture = getTexture(getTextureFileName(), textureMode == TextureMode.Simple, defaultColor, lockEdit);
        ts.setTexture(texture);
        mesh.setRenderState(ts);
    } else {
        if (SceneManager.getInstance().getSolarHeatMap()) {
            if (isDrawable()) {
                if (this instanceof Foundation || this instanceof Wall || this instanceof Roof || this instanceof Floor) {
                    SolarRadiation.getInstance().initMeshTextureData(mesh, mesh, this instanceof Roof ? (ReadOnlyVector3) mesh.getParent().getUserData() : getNormal());
                }
            }
        } else if (textureMode == TextureMode.None || getTextureFileName() == null) {
            mesh.clearRenderState(StateType.Texture);
            mesh.setDefaultColor(defaultColor);
        } else {
            final TextureState ts = new TextureState();
            final Texture texture = getTexture(getTextureFileName(), textureMode == TextureMode.Simple, defaultColor, false);
            ts.setTexture(texture);
            mesh.setRenderState(ts);
            mesh.setDefaultColor(ColorRGBA.WHITE);
        }
    }
}
Also used : ReadOnlyVector3(com.ardor3d.math.type.ReadOnlyVector3) TextureState(com.ardor3d.renderer.state.TextureState) Texture(com.ardor3d.image.Texture)

Example 3 with Texture

use of com.ardor3d.image.Texture in project energy3d by concord-consortium.

the class HousePart method getTexture.

private Texture getTexture(final String filename, final boolean isTransparent, final ReadOnlyColorRGBA defaultColor, final boolean grayout) {
    Texture texture = TextureManager.load(filename, Texture.MinificationFilter.Trilinear, TextureStoreFormat.GuessNoCompressedFormat, true);
    if (isTransparent) {
        final Color color = new Color(defaultColor.getRed(), defaultColor.getGreen(), defaultColor.getBlue());
        final Image image = texture.getImage();
        final ByteBuffer data = image.getData(0);
        byte alpha;
        int i;
        for (int y = 0; y < image.getHeight(); y++) {
            for (int x = 0; x < image.getWidth(); x++) {
                i = (y * image.getWidth() + x) * 4;
                alpha = data.get(i + 3);
                if (alpha == 0) {
                    // when it is transparent, put the default color of the part
                    data.put(i, (byte) color.getRed());
                    data.put(i + 1, (byte) color.getGreen());
                    data.put(i + 2, (byte) color.getBlue());
                }
            }
        }
        texture.setImage(image);
    }
    if (grayout) {
        final Texture grayoutTexture = cachedGrayTextures.get(filename + ":grayout");
        if (grayoutTexture != null) {
            return grayoutTexture;
        }
        final Image image = texture.getImage();
        // make a copy
        final Image grayImage = new Image();
        grayImage.setDataFormat(image.getDataFormat());
        grayImage.setDataType(image.getDataType());
        grayImage.setWidth(image.getWidth());
        grayImage.setHeight(image.getHeight());
        grayImage.setMipMapByteSizes(image.getMipMapByteSizes());
        final ByteBuffer data = image.getData(0);
        final ByteBuffer grayData = ByteBuffer.allocate(data.capacity());
        byte alpha, red, green, blue, gray;
        int i;
        for (int y = 0; y < image.getHeight(); y++) {
            for (int x = 0; x < image.getWidth(); x++) {
                i = (y * image.getWidth() + x) * 4;
                red = data.get(i);
                green = data.get(i + 1);
                blue = data.get(i + 2);
                alpha = data.get(i + 3);
                gray = (byte) Math.min(red, green);
                gray = (byte) Math.min(blue, gray);
                grayData.put(i, gray);
                grayData.put(i + 1, gray);
                grayData.put(i + 2, gray);
                grayData.put(i + 3, alpha);
            }
        }
        grayImage.addData(grayData);
        texture = TextureManager.loadFromImage(grayImage, Texture.MinificationFilter.Trilinear, TextureStoreFormat.GuessNoCompressedFormat);
        cachedGrayTextures.put(filename + ":grayout", texture);
    }
    return texture;
}
Also used : Color(java.awt.Color) Image(com.ardor3d.image.Image) Texture(com.ardor3d.image.Texture) ByteBuffer(java.nio.ByteBuffer) CullHint(com.ardor3d.scenegraph.hint.CullHint)

Aggregations

Texture (com.ardor3d.image.Texture)3 ReadOnlyVector3 (com.ardor3d.math.type.ReadOnlyVector3)2 TextureState (com.ardor3d.renderer.state.TextureState)2 BoundingBox (com.ardor3d.bounding.BoundingBox)1 Image (com.ardor3d.image.Image)1 ReadOnlyVector2 (com.ardor3d.math.type.ReadOnlyVector2)1 Mesh (com.ardor3d.scenegraph.Mesh)1 CullHint (com.ardor3d.scenegraph.hint.CullHint)1 Color (java.awt.Color)1 ByteBuffer (java.nio.ByteBuffer)1 FloatBuffer (java.nio.FloatBuffer)1 ArrayList (java.util.ArrayList)1 GroupData (org.concord.energy3d.util.MeshLib.GroupData)1