Search in sources :

Example 1 with GroupData

use of org.concord.energy3d.util.MeshLib.GroupData 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 2 with GroupData

use of org.concord.energy3d.util.MeshLib.GroupData 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)

Aggregations

ReadOnlyVector2 (com.ardor3d.math.type.ReadOnlyVector2)2 ReadOnlyVector3 (com.ardor3d.math.type.ReadOnlyVector3)2 TextureState (com.ardor3d.renderer.state.TextureState)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 Texture (com.ardor3d.image.Texture)1 Vector2 (com.ardor3d.math.Vector2)1 Vector3 (com.ardor3d.math.Vector3)1 Mesh (com.ardor3d.scenegraph.Mesh)1