Search in sources :

Example 6 with TextureState

use of com.ardor3d.renderer.state.TextureState in project energy3d by concord-consortium.

the class TriangleMeshLib method findGroups.

private static ArrayList<GroupData> findGroups(final Mesh mesh, final boolean redoNormal) {
    final FloatBuffer vertexBuffer = mesh.getMeshData().getVertexBuffer();
    vertexBuffer.rewind();
    FloatBuffer normalBuffer = mesh.getMeshData().getNormalBuffer();
    if (normalBuffer == null) {
        normalBuffer = BufferUtils.createFloatBuffer(vertexBuffer.limit());
    } else {
        normalBuffer.rewind();
    }
    final TextureState textureState = (TextureState) mesh.getLocalRenderState(StateType.Texture);
    FloatBuffer textureBuffer = mesh.getMeshData().getTextureBuffer(0);
    if (textureBuffer != null) {
        textureBuffer.rewind();
    } else {
        textureBuffer = BufferUtils.createFloatBuffer(vertexBuffer.limit() / 3 * 2);
    }
    final Vector3 v1 = new Vector3();
    final Vector3 v2 = new Vector3();
    final Vector3 normal = new Vector3();
    final ArrayList<GroupData> groups = new ArrayList<GroupData>();
    for (int i = 0; i < vertexBuffer.limit() / 9; i++) {
        final Vector3 p1 = new Vector3(vertexBuffer.get(), vertexBuffer.get(), vertexBuffer.get());
        final Vector3 p2 = new Vector3(vertexBuffer.get(), vertexBuffer.get(), vertexBuffer.get());
        final Vector3 p3 = new Vector3(vertexBuffer.get(), vertexBuffer.get(), vertexBuffer.get());
        p2.subtract(p1, v1);
        p3.subtract(p1, v2);
        v1.cross(v2, normal);
        // as the vertex buffer can be relative to the node, apply the world transform to get the absolute normal
        mesh.getWorldTransform().applyForwardVector(normal);
        normal.normalizeLocal();
        Vector3 firstNormal = null;
        if (!redoNormal) {
            firstNormal = new Vector3(normalBuffer.get(), normalBuffer.get(), normalBuffer.get());
            if (Double.isNaN(firstNormal.length())) {
                continue;
            }
        }
        final GroupData group = new GroupData();
        group.key.set(normal);
        groups.add(group);
        if (textureState != null && textureState.getTexture() != null) {
            group.textureImage = textureState.getTexture().getImage();
        }
        group.vertices.add(p1);
        group.vertices.add(p2);
        group.vertices.add(p3);
        if (redoNormal) {
            group.normals.add(group.key.clone());
            group.normals.add(group.key.clone());
            group.normals.add(group.key.clone());
        } else {
            group.normals.add(firstNormal);
            group.normals.add(new Vector3(normalBuffer.get(), normalBuffer.get(), normalBuffer.get()));
            group.normals.add(new Vector3(normalBuffer.get(), normalBuffer.get(), normalBuffer.get()));
        }
        // texture is 2D, vertex is 3D
        group.textures.add(new Vector2(textureBuffer.get(), textureBuffer.get()));
        group.textures.add(new Vector2(textureBuffer.get(), textureBuffer.get()));
        group.textures.add(new Vector2(textureBuffer.get(), textureBuffer.get()));
    }
    MeshLib.combineGroups(groups);
    return groups;
}
Also used : TextureState(com.ardor3d.renderer.state.TextureState) ReadOnlyVector2(com.ardor3d.math.type.ReadOnlyVector2) Vector2(com.ardor3d.math.Vector2) ArrayList(java.util.ArrayList) FloatBuffer(java.nio.FloatBuffer) ReadOnlyVector3(com.ardor3d.math.type.ReadOnlyVector3) Vector3(com.ardor3d.math.Vector3) GroupData(org.concord.energy3d.util.MeshLib.GroupData)

Example 7 with TextureState

use of com.ardor3d.renderer.state.TextureState 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 8 with TextureState

use of com.ardor3d.renderer.state.TextureState in project energy3d by concord-consortium.

the class Foundation method updateTextureAndColor.

@Override
public void updateTextureAndColor() {
    for (final Mesh mesh : sideMesh) {
        mesh.setDefaultColor(Scene.getInstance().getTextureMode() == TextureMode.Full ? ColorRGBA.GRAY : (getColor() == null ? Scene.getInstance().getFoundationColor() : getColor()));
        updateTextureAndColor(mesh, getColor() == null ? Scene.getInstance().getFoundationColor() : getColor());
    }
    updateTextureAndColor(mesh, getColor() == null ? Scene.getInstance().getFoundationColor() : getColor());
    if (!SceneManager.getInstance().getSolarHeatMap() && importedNodes != null) {
        final int n = importedNodes.size();
        if (n > 0) {
            Node ni;
            for (int i = 0; i < n; i++) {
                ni = importedNodes.get(i);
                if (root.getChildren().contains(ni)) {
                    for (final Spatial s : ni.getChildren()) {
                        if (s instanceof Mesh) {
                            if (s instanceof Line) {
                                continue;
                            }
                            final Mesh m = (Mesh) s;
                            final UserData ud = (UserData) m.getUserData();
                            final TextureState ts = (TextureState) m.getLocalRenderState(StateType.Texture);
                            if (ts == null || ts.getTexture() == null) {
                                m.clearRenderState(StateType.Texture);
                            // m.setDefaultColor(nis.getDefaultColor());
                            } else {
                                if (ud.getTextureBuffer() == null || Util.isZero(ud.getTextureBuffer())) {
                                    m.clearRenderState(StateType.Texture);
                                // m.setDefaultColor(nis.getDefaultColor());
                                } else {
                                    m.getMeshData().setTextureBuffer(ud.getTextureBuffer(), 0);
                                    m.setRenderState(ud.getRenderState());
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : Line(com.ardor3d.scenegraph.Line) TextureState(com.ardor3d.renderer.state.TextureState) Spatial(com.ardor3d.scenegraph.Spatial) Node(com.ardor3d.scenegraph.Node) Mesh(com.ardor3d.scenegraph.Mesh) CullHint(com.ardor3d.scenegraph.hint.CullHint)

Example 9 with TextureState

use of com.ardor3d.renderer.state.TextureState 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)

Aggregations

TextureState (com.ardor3d.renderer.state.TextureState)9 ReadOnlyVector3 (com.ardor3d.math.type.ReadOnlyVector3)3 Mesh (com.ardor3d.scenegraph.Mesh)3 Texture (com.ardor3d.image.Texture)2 Texture2D (com.ardor3d.image.Texture2D)2 ReadOnlyVector2 (com.ardor3d.math.type.ReadOnlyVector2)2 CullHint (com.ardor3d.scenegraph.hint.CullHint)2 FloatBuffer (java.nio.FloatBuffer)2 ArrayList (java.util.ArrayList)2 GroupData (org.concord.energy3d.util.MeshLib.GroupData)2 BoundingBox (com.ardor3d.bounding.BoundingBox)1 Image (com.ardor3d.image.Image)1 ColorRGBA (com.ardor3d.math.ColorRGBA)1 Matrix3 (com.ardor3d.math.Matrix3)1 Vector2 (com.ardor3d.math.Vector2)1 Vector3 (com.ardor3d.math.Vector3)1 ReadOnlyColorRGBA (com.ardor3d.math.type.ReadOnlyColorRGBA)1 Line (com.ardor3d.scenegraph.Line)1 Node (com.ardor3d.scenegraph.Node)1 Spatial (com.ardor3d.scenegraph.Spatial)1