Package com.ardor3d.scenegraph

Examples of com.ardor3d.scenegraph.Mesh


       
       
       
    }
    public void generateFoliage(DetailLevel level, VoxelBlock block) {
        Mesh mesh = new Mesh();
        MeshData meshData = mesh.getMeshData();
        int numOfLeafs = foliagePositions.size();
              
        Vector3[] points = new Vector3[8];
        for (int i = 0; i<8; i++)
            points[i] = new Vector3();
View Full Code Here


        block.getFoliageNode().attachChild(mesh);
    }
   
   
    public void generateNonVoxelBranches(DetailLevel level, VoxelBlock block) {
        Mesh mesh = new Mesh();
        MeshData meshData = mesh.getMeshData();
       
        int numOfSegments = 6;
       
        Vector3 point1 = new Vector3();
        Vector3 point2 = new Vector3();
View Full Code Here

    public void addModelToScene() {
       
        OctreeRoot octree = block.getOctree();

        Mesh mesh = new Mesh();
        MeshData meshData = mesh.getMeshData();
        int numOfQuads = quads.size();
        int numOfVoxels = voxelCounter;

        FloatBuffer vertexBuffer = BufferUtils.createVector3Buffer(numOfVoxels);
        IntBuffer indexBuffer = BufferUtils.createIntBuffer(numOfQuads * 6);
        FloatBuffer normalBuffer = BufferUtils.createVector3Buffer(numOfVoxels);
        // FloatBuffer texCoordBuffer = BufferUtils.createVector2Buffer(numOfVoxels);
        FloatBuffer colorBuffer  = BufferUtils.createColorBuffer(numOfVoxels);

        ArrayList<Integer> verticesWithGrass = new ArrayList<Integer>();
       
       
        int blockSize = VoxelWorld.BLOCK_SIZE;
       
        for (int x = 0; x < blockSize + 1; x++) {
            for (int y = 0; y < blockSize + 1; y++) {
                for (int z = 0; z < blockSize + 1; z++) {
                    Voxel voxel = voxels[x][y][z];
                    if (voxel != null) {
                        voxel.summarize();
                       
                        octree.insertVertex(x, y, z, voxel.index);
                       
                        int i3 = voxel.index * 3;
                       
                        vertexBuffer.put(i3+0, voxel.vertexPosition.getXf());
                        vertexBuffer.put(i3+1, voxel.vertexPosition.getYf());
                        vertexBuffer.put(i3+2, voxel.vertexPosition.getZf());

                        Vector3 normal = computeVoxelNormal(x,y,z,voxel.vertexPosition);
                        normalBuffer.put(i3+0, normal.getXf());
                        normalBuffer.put(i3+1, normal.getYf());
                        normalBuffer.put(i3+2, normal.getZf());  
                       
                        int i4 = voxel.index * 4;
                        colorBuffer.put(i4+0, voxel.materials.getXf());
                        colorBuffer.put(i4+1, voxel.materials.getYf());
                        colorBuffer.put(i4+2, voxel.materials.getZf());
                        colorBuffer.put(i4+3, voxel.materials.getWf());
                       
                        ///GRASS 
                        if (voxel.vertexPosition.getY() > 33 && voxel.vertexPosition.getYf() < 210 && normal.getY() > 0.7)
                            verticesWithGrass.add(voxel.index);
                    }
                }
            }
        }

        for (int q = 0; q < quads.size(); q++) {
            Quad quad = quads.get(q);

            indexBuffer.put(quad.voxel1.index);
            indexBuffer.put(quad.voxel2.index);
            indexBuffer.put(quad.voxel3.index);
            indexBuffer.put(quad.voxel1.index);
            indexBuffer.put(quad.voxel3.index);
            indexBuffer.put(quad.voxel4.index);
        }

        indexBuffer.rewind();
        //colorBuffer.flip();

        meshData.setVertexBuffer(vertexBuffer);
        meshData.setIndexBuffer(indexBuffer);
        meshData.setNormalBuffer(normalBuffer);
        meshData.setColorBuffer(colorBuffer);


        // meshData.setColorBuffer(colorBuffer);

        block.numOfTriangles = quads.size()*2;
        block.numOfVertices = voxelCounter;

        Vector3 center = basePosition;
        double halfSize = pool.getLevel().getScale() * VoxelWorld.BLOCK_SIZE / 2.0;
        center.addLocal(halfSize, halfSize, halfSize);
        mesh.setModelBound(new BoundingBox(center, halfSize, halfSize, halfSize));

        block.getVoxelNode().attachChild(mesh);
       
        //////////GRASS///////////////////
        if (pool.getLevel().getLevelID() == 0) {
            int numOfGrasses = verticesWithGrass.size();

            Mesh grassMesh = new Mesh();
            MeshData grassMeshData = grassMesh.getMeshData();

            FloatBuffer grassVertexBuffer = BufferUtils.createVector3Buffer(numOfGrasses * 4);
            IntBuffer grassIndexBuffer = BufferUtils.createIntBuffer(numOfGrasses * 6);
            FloatBuffer grassNormalBuffer = BufferUtils.createVector3Buffer(numOfGrasses * 4);
            FloatBuffer grassTexCoordBuffer = BufferUtils.createVector2Buffer(numOfGrasses * 4);
            //FloatBuffer grassBasePositionBuffer = BufferUtils.createVector2Buffer(numOfGrasses * 4);
            for (int i=0, size = verticesWithGrass.size(); i<size; i++) {

                int i3 = verticesWithGrass.get(i) * 3;
                int i4 = i * 4;

                float topY = 2.5f + (float) Math.random();
                float bottomY = -0.1f;
                float halfEidth = 1.5f + (float) Math.random();

                //double alpha = Math.random()*Math.PI;
                //float offsetX = (FastMath.sin(alpha) * halfEidth);
                //float offsetZ = (float)(FastMath.cos(alpha) * halfEidth);
                float offsetX = (float)Math.random()-0.5f;
                float offsetZ = (float)Math.random()-0.5f;
                grassVertexBuffer.put(vertexBuffer.get(i3 +0) + offsetX);
                grassVertexBuffer.put(vertexBuffer.get(i3 +1) + bottomY);
                grassVertexBuffer.put(vertexBuffer.get(i3 +2) + offsetZ);

                grassVertexBuffer.put(vertexBuffer.get(i3 +0) + offsetX);
                grassVertexBuffer.put(vertexBuffer.get(i3 +1) + topY);
                grassVertexBuffer.put(vertexBuffer.get(i3 +2) + offsetZ);

                grassVertexBuffer.put(vertexBuffer.get(i3 +0) + offsetX);
                grassVertexBuffer.put(vertexBuffer.get(i3 +1) + topY);
                grassVertexBuffer.put(vertexBuffer.get(i3 +2) + offsetZ);

                grassVertexBuffer.put(vertexBuffer.get(i3 +0) + offsetX);
                grassVertexBuffer.put(vertexBuffer.get(i3 +1) + bottomY);
                grassVertexBuffer.put(vertexBuffer.get(i3 +2) + offsetZ);
               
                /*grassVertexBuffer.put(vertexBuffer.get(i3 +0) + offsetX);
                grassVertexBuffer.put(vertexBuffer.get(i3 +1) + bottomY);
                grassVertexBuffer.put(vertexBuffer.get(i3 +2) + offsetZ);

                grassVertexBuffer.put(vertexBuffer.get(i3 +0) + offsetX);
                grassVertexBuffer.put(vertexBuffer.get(i3 +1) + topY);
                grassVertexBuffer.put(vertexBuffer.get(i3 +2) + offsetZ);

                grassVertexBuffer.put(vertexBuffer.get(i3 +0) - offsetX);
                grassVertexBuffer.put(vertexBuffer.get(i3 +1) + topY);
                grassVertexBuffer.put(vertexBuffer.get(i3 +2) - offsetZ);

                grassVertexBuffer.put(vertexBuffer.get(i3 +0) - offsetX);
                grassVertexBuffer.put(vertexBuffer.get(i3 +1) + bottomY);
                grassVertexBuffer.put(vertexBuffer.get(i3 +2) - offsetZ);*/

                float nx = normalBuffer.get(i3 +0);
                float ny = normalBuffer.get(i3 +1);
                float nz = normalBuffer.get(i3 +2);

                grassNormalBuffer.put(nx);
                grassNormalBuffer.put(ny);
                grassNormalBuffer.put(nz)
                grassNormalBuffer.put(nx);
                grassNormalBuffer.put(ny);
                grassNormalBuffer.put(nz)
                grassNormalBuffer.put(nx);
                grassNormalBuffer.put(ny);
                grassNormalBuffer.put(nz)
                grassNormalBuffer.put(nx);
                grassNormalBuffer.put(ny);
                grassNormalBuffer.put(nz)


                grassTexCoordBuffer.put(0);
                grassTexCoordBuffer.put(0);
                grassTexCoordBuffer.put(0);
                grassTexCoordBuffer.put(1);
                grassTexCoordBuffer.put(1);
                grassTexCoordBuffer.put(1);
                grassTexCoordBuffer.put(1);
                grassTexCoordBuffer.put(0);

                grassIndexBuffer.put(i4);
                grassIndexBuffer.put(i4+1);
                grassIndexBuffer.put(i4+2);
                grassIndexBuffer.put(i4);
                grassIndexBuffer.put(i4+2);
                grassIndexBuffer.put(i4+3);          
            }

            //grassIndexBuffer.rewind();
            //colorBuffer.flip();

            grassMeshData.setVertexBuffer(grassVertexBuffer);
            grassMeshData.setIndexBuffer(grassIndexBuffer);
            grassMeshData.setNormalBuffer(grassNormalBuffer);
            grassMeshData.setTextureBuffer(grassTexCoordBuffer,0);

            grassMesh.setModelBound(new BoundingBox(center, halfSize, halfSize, halfSize));

            block.getGrassNode().attachChild(grassMesh);           
        }
       
 
View Full Code Here

        GradientBackdrop._mesh.render(renderer);
    }

    private static Mesh createMesh() {
        final Mesh mesh = new Mesh();
        mesh.getMeshData().setVertexBuffer(BufferUtils.createVector3Buffer(4));
        mesh.getMeshData().setColorBuffer(BufferUtils.createColorBuffer(4));
        mesh.getMeshData().setIndexMode(IndexMode.TriangleFan);

        final BlendState blend = new BlendState();
        blend.setBlendEnabled(true);
        blend.setSourceFunction(SourceFunction.SourceAlpha);
        blend.setDestinationFunction(DestinationFunction.OneMinusSourceAlpha);
        mesh.setRenderState(blend);
        mesh.updateWorldRenderStates(false);

        return mesh;
    }
View Full Code Here

        recurse.set(false);
        if (source instanceof Node) {
            recurse.set(true);
            return clone((Node) source);
        } else if (source instanceof Mesh) {
            final Mesh result = clone((Mesh) source);
            result.setMeshData(((Mesh) source).getMeshData());
            result.updateModelBound();
            return result;
        }
        return null;
    }
View Full Code Here

        }
        return null;
    }

    protected Mesh clone(final Mesh original) {
        Mesh copy = null;
        try {
            copy = original.getClass().newInstance();
        } catch (final InstantiationException e) {
            logger.log(Level.SEVERE, "Could not access final constructor of class "
                    + original.getClass().getCanonicalName(), e);
            throw new RuntimeException(e);
        } catch (final IllegalAccessException e) {
            logger.log(Level.SEVERE, "Could not access final constructor of class "
                    + original.getClass().getCanonicalName(), e);
            throw new RuntimeException(e);
        }
        copy.setName(original.getName() + "_copy");
        copy.getSceneHints().set(original.getSceneHints());
        copy.setTransform(original.getTransform());
        copy.setDefaultColor(original.getDefaultColor());

        for (final StateType type : StateType.values()) {
            final RenderState state = original.getLocalRenderState(type);
            if (state != null) {
                copy.setRenderState(state);
            }
        }
        return copy;
    }
View Full Code Here

            _renderer = renderer;
        }

        public void visit(final Spatial spatial) {
            if (spatial instanceof Mesh) {
                final Mesh mesh = (Mesh) spatial;
                final TextureState state = (TextureState) mesh.getWorldRenderState(StateType.Texture);
                if (state != null) {
                    _renderer.applyState(state.getType(), state);
                }
            }
        }
View Full Code Here

            cam.setPlaneState(state);
            return;
        }
        cam.setPlaneState(state);
        if (element instanceof Mesh && element.getSceneHints().getCullHint() != CullHint.Always) {
            final Mesh mesh = (Mesh) element;

            double rSize = size;
            if (rSize == -1) {
                final BoundingVolume vol = element.getWorldBound();
                if (vol != null) {
                    measureBox.setCenter(vol.getCenter());
                    measureBox.setXExtent(0);
                    measureBox.setYExtent(0);
                    measureBox.setZExtent(0);
                    measureBox.mergeLocal(vol);
                    rSize = AUTO_NORMAL_RATIO
                            * ((measureBox.getXExtent() + measureBox.getYExtent() + measureBox.getZExtent()) / 3);
                } else {
                    rSize = 1.0;
                }

                if (Double.isInfinite(rSize) || Double.isNaN(rSize)) {
                    rSize = 1.0;
                }
            }

            final FloatBuffer norms = mesh.getMeshData().getNormalBuffer();
            final FloatBuffer verts = mesh.getMeshData().getVertexBuffer();
            if (norms != null && verts != null && norms.limit() == verts.limit()) {
                FloatBuffer lineVerts = normalLines.getMeshData().getVertexBuffer();
                if (lineVerts.capacity() < (3 * (2 * mesh.getMeshData().getVertexCount()))) {
                    normalLines.getMeshData().setVertexBuffer(null);
                    lineVerts = BufferUtils.createVector3Buffer(mesh.getMeshData().getVertexCount() * 2);
                    normalLines.getMeshData().setVertexBuffer(lineVerts);
                } else {
                    lineVerts.clear();
                    lineVerts.limit(3 * 2 * mesh.getMeshData().getVertexCount());
                    normalLines.getMeshData().setVertexBuffer(lineVerts);
                }

                FloatBuffer lineColors = normalLines.getMeshData().getColorBuffer();
                if (lineColors.capacity() < (4 * (2 * mesh.getMeshData().getVertexCount()))) {
                    normalLines.getMeshData().setColorBuffer(null);
                    lineColors = BufferUtils.createColorBuffer(mesh.getMeshData().getVertexCount() * 2);
                    normalLines.getMeshData().setColorBuffer(lineColors);
                } else {
                    lineColors.clear();
                }

                IndexBufferData<?> lineInds = normalLines.getMeshData().getIndices();
                if (lineInds == null || lineInds.getBufferCapacity() < (normalLines.getMeshData().getVertexCount())) {
                    normalLines.getMeshData().setIndices(null);
                    lineInds = BufferUtils.createIndexBufferData(mesh.getMeshData().getVertexCount() * 2, normalLines
                            .getMeshData().getVertexCount() - 1);
                    normalLines.getMeshData().setIndices(lineInds);
                } else {
                    lineInds.getBuffer().clear();
                    lineInds.getBuffer().limit(normalLines.getMeshData().getVertexCount());
                }

                verts.rewind();
                norms.rewind();
                lineVerts.rewind();
                lineInds.getBuffer().rewind();

                for (int x = 0; x < mesh.getMeshData().getVertexCount(); x++) {
                    _normalVect.set(verts.get(), verts.get(), verts.get());
                    mesh.getWorldTransform().applyForward(_normalVect);
                    lineVerts.put(_normalVect.getXf());
                    lineVerts.put(_normalVect.getYf());
                    lineVerts.put(_normalVect.getZf());

                    lineColors.put(NORMAL_COLOR_BASE.getRed());
                    lineColors.put(NORMAL_COLOR_BASE.getGreen());
                    lineColors.put(NORMAL_COLOR_BASE.getBlue());
                    lineColors.put(NORMAL_COLOR_BASE.getAlpha());

                    lineInds.put(x * 2);

                    _normalVect2.set(norms.get(), norms.get(), norms.get());
                    mesh.getWorldTransform().applyForwardVector(_normalVect2).normalizeLocal().multiplyLocal(rSize);
                    _normalVect.addLocal(_normalVect2);
                    lineVerts.put(_normalVect.getXf());
                    lineVerts.put(_normalVect.getYf());
                    lineVerts.put(_normalVect.getZf());

View Full Code Here

            cam.setPlaneState(state);
            return;
        }
        cam.setPlaneState(state);
        if (element instanceof Mesh && element.getSceneHints().getCullHint() != CullHint.Always) {
            final Mesh mesh = (Mesh) element;

            double rSize = size;
            if (rSize == -1) {
                final BoundingVolume vol = element.getWorldBound();
                if (vol != null) {
                    measureBox.setCenter(vol.getCenter());
                    measureBox.setXExtent(0);
                    measureBox.setYExtent(0);
                    measureBox.setZExtent(0);
                    measureBox.mergeLocal(vol);
                    rSize = AUTO_NORMAL_RATIO
                            * ((measureBox.getXExtent() + measureBox.getYExtent() + measureBox.getZExtent()) / 3f);
                } else {
                    rSize = 1.0;
                }
            }

            final FloatBuffer norms = mesh.getMeshData().getTangentBuffer();
            final FloatBuffer verts = mesh.getMeshData().getVertexBuffer();
            if (norms != null && verts != null && norms.limit() == verts.limit()) {
                FloatBuffer lineVerts = normalLines.getMeshData().getVertexBuffer();
                if (lineVerts.capacity() < (3 * (2 * mesh.getMeshData().getVertexCount()))) {
                    normalLines.getMeshData().setVertexBuffer(null);
                    lineVerts = BufferUtils.createVector3Buffer(mesh.getMeshData().getVertexCount() * 2);
                    normalLines.getMeshData().setVertexBuffer(lineVerts);
                } else {
                    lineVerts.clear();
                    lineVerts.limit(3 * 2 * mesh.getMeshData().getVertexCount());
                    normalLines.getMeshData().setVertexBuffer(lineVerts);
                }

                FloatBuffer lineColors = normalLines.getMeshData().getColorBuffer();
                if (lineColors.capacity() < (4 * (2 * mesh.getMeshData().getVertexCount()))) {
                    normalLines.getMeshData().setColorBuffer(null);
                    lineColors = BufferUtils.createColorBuffer(mesh.getMeshData().getVertexCount() * 2);
                    normalLines.getMeshData().setColorBuffer(lineColors);
                } else {
                    lineColors.clear();
                }

                IndexBufferData<?> lineInds = normalLines.getMeshData().getIndices();
                if (lineInds == null || lineInds.getBufferCapacity() < (normalLines.getMeshData().getVertexCount())) {
                    normalLines.getMeshData().setIndices(null);
                    lineInds = BufferUtils.createIndexBufferData(mesh.getMeshData().getVertexCount() * 2, normalLines
                            .getMeshData().getVertexCount() - 1);
                    normalLines.getMeshData().setIndices(lineInds);
                } else {
                    lineInds.getBuffer().clear();
                    lineInds.getBuffer().limit(normalLines.getMeshData().getVertexCount());
                }

                verts.rewind();
                norms.rewind();
                lineVerts.rewind();
                lineInds.getBuffer().rewind();

                for (int x = 0; x < mesh.getMeshData().getVertexCount(); x++) {
                    _normalVect.set(verts.get(), verts.get(), verts.get());
                    _normalVect.multiplyLocal(mesh.getWorldScale());
                    lineVerts.put(_normalVect.getXf());
                    lineVerts.put(_normalVect.getYf());
                    lineVerts.put(_normalVect.getZf());

                    lineColors.put(TANGENT_COLOR_BASE.getRed());
                    lineColors.put(TANGENT_COLOR_BASE.getGreen());
                    lineColors.put(TANGENT_COLOR_BASE.getBlue());
                    lineColors.put(TANGENT_COLOR_BASE.getAlpha());

                    lineInds.put(x * 2);

                    _normalVect.addLocal(norms.get() * rSize, norms.get() * rSize, norms.get() * rSize);
                    lineVerts.put(_normalVect.getXf());
                    lineVerts.put(_normalVect.getYf());
                    lineVerts.put(_normalVect.getZf());

                    lineColors.put(TANGENT_COLOR_TIP.getRed());
                    lineColors.put(TANGENT_COLOR_TIP.getGreen());
                    lineColors.put(TANGENT_COLOR_TIP.getBlue());
                    lineColors.put(TANGENT_COLOR_TIP.getAlpha());

                    lineInds.put((x * 2) + 1);
                }

                normalLines.setWorldTranslation(mesh.getWorldTranslation());
                normalLines.setWorldRotation(mesh.getWorldRotation());
                normalLines.onDraw(r);
            }

        }

View Full Code Here

        private FloatBufferData colors;
        private FloatBufferData normals;
        private List<FloatBufferData> texCoordsList;

        public Mesh getMesh() {
            final Mesh mesh = new Mesh("combined");
            mesh.setMeshData(data);
            return mesh;
        }
View Full Code Here

TOP

Related Classes of com.ardor3d.scenegraph.Mesh

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.