com.jme3.scene.VertexBuffer.Type Java Examples

The following examples show how to use com.jme3.scene.VertexBuffer.Type. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TangentBinormalGenerator.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static void generate(Spatial scene, boolean splitMirrored) {
    if (scene instanceof Node) {
        Node node = (Node) scene;
        for (Spatial child : node.getChildren()) {
            generate(child, splitMirrored);
        }
    } else {
        Geometry geom = (Geometry) scene;
        Mesh mesh = geom.getMesh();
        
        // Check to ensure mesh has texcoords and normals before generating
        if (mesh.getBuffer(Type.TexCoord) != null 
         && mesh.getBuffer(Type.Normal) != null){
            generate(geom.getMesh(),true, splitMirrored);
        }
    }
}
 
Example #2
Source File: MeshLoader.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void pushTexCoord(Attributes attribs) throws SAXException {
    if (texCoordIndex >= 8) {
        return; // More than 8 not supported by ogre.
    }
    Type type = TEXCOORD_TYPES[texCoordIndex];

    VertexBuffer tcvb = mesh.getBuffer(type);
    FloatBuffer buf = (FloatBuffer) tcvb.getData();

    buf.put(parseFloat(attribs.getValue("u")));
    if (tcvb.getNumComponents() >= 2) {
        buf.put(parseFloat(attribs.getValue("v")));
        if (tcvb.getNumComponents() >= 3) {
            buf.put(parseFloat(attribs.getValue("w")));
            if (tcvb.getNumComponents() == 4) {
                buf.put(parseFloat(attribs.getValue("x")));
            }
        }
    }

    texCoordIndex++;
}
 
Example #3
Source File: UpdatedTerrainPatch.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void updateAll() {
    updatedPatch.setLod(newLod);
    updatedPatch.setLodRight(rightLod);
    updatedPatch.setLodTop(topLod);
    updatedPatch.setLodLeft(leftLod);
    updatedPatch.setLodBottom(bottomLod);
    if (newIndexBuffer != null && isReIndexNeeded()) {
        updatedPatch.setPreviousLod(previousLod);
        updatedPatch.getMesh().clearBuffer(Type.Index);
        if (newIndexBuffer instanceof IntBuffer)
            updatedPatch.getMesh().setBuffer(Type.Index, 3, (IntBuffer)newIndexBuffer);
        else if (newIndexBuffer instanceof ShortBuffer)
            updatedPatch.getMesh().setBuffer(Type.Index, 3, (ShortBuffer)newIndexBuffer);
        else
            updatedPatch.getMesh().setBuffer(Type.Index, 3, (ByteBuffer)newIndexBuffer);
    }
}
 
Example #4
Source File: WireFrustum.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public WireFrustum(Vector3f[] points){
    if (points != null)
        setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(points));

    setBuffer(Type.Index, 2,
            new short[]{
                 0, 1,
                 1, 2,
                 2, 3,
                 3, 0,

                 4, 5,
                 5, 6,
                 6, 7,
                 7, 4,

                 0, 4,
                 1, 5,
                 2, 6,
                 3, 7,
            }
    );
    setMode(Mode.Lines);
}
 
Example #5
Source File: TangentGenerator.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
/**
 * Generate tangents using a standard algorithm.
 *
 * @param spatial       the spatial.
 * @param splitMirrored the split mirrored.
 */
public static void useStandardGenerator(@NotNull Spatial spatial, boolean splitMirrored) {
    try {

        NodeUtils.visitGeometry(spatial, geometry -> {

            var mesh = geometry.getMesh();
            var texCoord = mesh.getBuffer(Type.TexCoord);

            if (texCoord != null) {
                TangentBinormalGenerator.generate(geometry, splitMirrored);
            }
        });

    } catch (Exception e) {
        EditorUtil.handleException(LOGGER, null, e);
    }
}
 
Example #6
Source File: GeometryBatchFactory.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static void printMesh(Mesh mesh) {
    for (int bufType = 0; bufType < Type.values().length; bufType++) {
        VertexBuffer outBuf = mesh.getBuffer(Type.values()[bufType]);
        if (outBuf == null) {
            continue;
        }

        System.out.println(outBuf.getBufferType() + ": ");
        for (int vert = 0; vert < outBuf.getNumElements(); vert++) {
            String str = "[";
            for (int comp = 0; comp < outBuf.getNumComponents(); comp++) {
                Object val = outBuf.getElementComponent(vert, comp);
                outBuf.setElementComponent(vert, comp, val);
                val = outBuf.getElementComponent(vert, comp);
                str += val;
                if (comp != outBuf.getNumComponents() - 1) {
                    str += ", ";
                }
            }
            str += "]";
            System.out.println(str);
        }
        System.out.println("------");
    }
}
 
Example #7
Source File: RagdollUtils.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Test whether the indexed bone has at least one vertex in the specified
 * meshes with a weight greater than the specified threshold.
 *
 * @param boneIndex the index of the bone (&ge;0)
 * @param targets the meshes to search (not null, no null elements)
 * @param weightThreshold the threshold (&ge;0, &le;1)
 * @return true if at least 1 vertex found, otherwise false
 */
public static boolean hasVertices(int boneIndex, Mesh[] targets,
        float weightThreshold) {
    for (Mesh mesh : targets) {
        VertexBuffer biBuf = mesh.getBuffer(VertexBuffer.Type.BoneIndex);
        Buffer boneIndices = biBuf.getDataReadOnly();
        FloatBuffer boneWeight
                = (FloatBuffer) mesh.getBuffer(Type.BoneWeight).getData();

        boneIndices.rewind();
        boneWeight.rewind();

        int vertexComponents = mesh.getVertexCount() * 3;
        for (int i = 0; i < vertexComponents; i += 3) {
            int start = i / 3 * 4;
            for (int k = start; k < start + 4; k++) {
                if (readIndex(boneIndices, k) == boneIndex
                        && boneWeight.get(k) >= weightThreshold) {
                    return true;
                }
            }
        }
    }

    return false;
}
 
Example #8
Source File: FbxSkin.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void generateSkinning() {
	for(FbxMesh fbxMesh : toSkin) {
		if(fbxMesh.geometries == null)
			continue;
		Mesh firstMesh = fbxMesh.geometries.get(0).getMesh();
		int maxWeightsPerVert = generateBoneData(firstMesh, fbxMesh);
		for(int i = 0; i < fbxMesh.geometries.size(); ++i) {
			Mesh mesh = fbxMesh.geometries.get(i).getMesh();
			if(mesh != firstMesh) {
				mesh.setBuffer(firstMesh.getBuffer(VertexBuffer.Type.BoneWeight));
				mesh.setBuffer(firstMesh.getBuffer(VertexBuffer.Type.BoneIndex));
				mesh.setBuffer(firstMesh.getBuffer(VertexBuffer.Type.HWBoneWeight));
				mesh.setBuffer(firstMesh.getBuffer(VertexBuffer.Type.HWBoneIndex));
			}
			mesh.setMaxNumWeights(maxWeightsPerVert);
			mesh.generateBindPose(true);
		}
	}
}
 
Example #9
Source File: Line.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void updateGeometry(Vector3f start, Vector3f end) {
    this.start = start;
    this.end = end;
    setBuffer(Type.Position, 3, new float[]{start.x,    start.y,    start.z,
                                            end.x,      end.y,      end.z,});


    setBuffer(Type.TexCoord, 2, new float[]{0, 0,
                                            1, 1});

    setBuffer(Type.Normal, 3, new float[]{0, 0, 1,
                                          0, 0, 1});

    setBuffer(Type.Index, 3, new short[]{0, 1});

    updateBound();
}
 
Example #10
Source File: EmitterMeshFaceShape.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void setMeshes(List<Mesh> meshes) {
    this.vertices = new ArrayList<List<Vector3f>>(meshes.size());
    this.normals = new ArrayList<List<Vector3f>>(meshes.size());
    for (Mesh mesh : meshes) {
        Vector3f[] vertexTable = BufferUtils.getVector3Array(mesh.getFloatBuffer(Type.Position));
        int[] indices = new int[3];
        List<Vector3f> vertices = new ArrayList<Vector3f>(mesh.getTriangleCount() * 3);
        List<Vector3f> normals = new ArrayList<Vector3f>(mesh.getTriangleCount());
        for (int i = 0; i < mesh.getTriangleCount(); ++i) {
            mesh.getTriangle(i, indices);
            vertices.add(vertexTable[indices[0]]);
            vertices.add(vertexTable[indices[1]]);
            vertices.add(vertexTable[indices[2]]);
            normals.add(FastMath.computeNormal(vertexTable[indices[0]], vertexTable[indices[1]], vertexTable[indices[2]]));
        }
        this.vertices.add(vertices);
        this.normals.add(normals);
    }
}
 
Example #11
Source File: BIHTree.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public BIHTree(Mesh mesh, int maxTrisPerNode) {
    this.mesh = mesh;
    this.maxTrisPerNode = maxTrisPerNode;

    if (maxTrisPerNode < 1 || mesh == null) {
        throw new IllegalArgumentException();
    }

    bihSwapTmp = new float[9];

    FloatBuffer vb = (FloatBuffer) mesh.getBuffer(Type.Position).getData();
    IndexBuffer ib = mesh.getIndexBuffer();
    if (ib == null) {
        ib = new VirtualIndexBuffer(mesh.getVertexCount(), mesh.getMode());
    } else if (mesh.getMode() != Mode.Triangles) {
        ib = new WrappedIndexBuffer(mesh);
    }

    numTris = ib.size() / 3;
    initTriList(vb, ib);
}
 
Example #12
Source File: SkeletonPoints.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * The method updates the geometry according to the positions of the bones.
 */
public void updateGeometry() {
    VertexBuffer vb = this.getBuffer(Type.Position);
    FloatBuffer posBuf = this.getFloatBuffer(Type.Position);
    posBuf.clear();
    for (int i = 0; i < skeleton.getBoneCount(); ++i) {
        Bone bone = skeleton.getBone(i);
        Vector3f head = bone.getModelSpacePosition();

        posBuf.put(head.getX()).put(head.getY()).put(head.getZ());
        if (boneLengths != null) {
            Vector3f tail = head.add(bone.getModelSpaceRotation().mult(Vector3f.UNIT_Y.mult(boneLengths.get(i))));
            posBuf.put(tail.getX()).put(tail.getY()).put(tail.getZ());
        }
    }
    posBuf.flip();
    vb.updateData(posBuf);

    this.updateBound();
}
 
Example #13
Source File: TangentGenerator.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
/**
 * Generate tangents using a Mikktspace algorithm.
 *
 * @param spatial the spatial.
 */
public static void useMikktspaceGenerator(@NotNull Spatial spatial) {
    try {

        NodeUtils.visitGeometry(spatial, geometry -> {

            var mesh = geometry.getMesh();
            var texCoord = mesh.getBuffer(Type.TexCoord);

            if (texCoord != null) {
                MikktspaceTangentGenerator.generate(geometry);
            }
        });

    } catch (Exception e) {
        EditorUtil.handleException(LOGGER, null, e);
    }
}
 
Example #14
Source File: BoundingSphereDebug.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * sets the indices for rendering the sphere.
 */
private void setIndexData() {

    // allocate connectivity
    int nbSegments = (radialSamples) * 3;

    ShortBuffer idxBuf = BufferUtils.createShortBuffer(2 * nbSegments);
    setBuffer(Type.Index, 2, idxBuf);

    int idx = 0;
    int segDone = 0;
    while (segDone < nbSegments) {
        idxBuf.put((short) idx);
        idxBuf.put((short) (idx + 1));
        idx++;
        segDone++;
        if (segDone == radialSamples || segDone == radialSamples * 2) {
            idx++;
        }

    }

}
 
Example #15
Source File: Line.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Alter the start and end.
 *
 * @param start the desired mesh location of the start (not null,
 * unaffected)
 * @param end the desired mesh location of the end (not null, unaffected)
 */
public void updatePoints(Vector3f start, Vector3f end) {
    this.start.set(start);
    this.end.set(end);

    VertexBuffer posBuf = getBuffer(Type.Position);
    
    FloatBuffer fb = (FloatBuffer) posBuf.getData();
    fb.rewind();
    fb.put(start.x).put(start.y).put(start.z);
    fb.put(end.x).put(end.y).put(end.z);
    
    posBuf.updateData(fb);
    
    updateBound();
}
 
Example #16
Source File: RenderDeviceJme.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void renderImage(RenderImage image, int x, int y, int width, int height,
                       Color color, float imageScale){

        RenderImageJme jmeImage = (RenderImageJme) image;

        niftyMat.getAdditionalRenderState().setBlendMode(convertBlend());
        niftyMat.setColor("Color", ColorRGBA.White);
        niftyMat.setTexture("Texture", jmeImage.getTexture());
        niftyMat.setBoolean("UseTex", true);
        setColor(color);

        quad.clearBuffer(Type.TexCoord);
        quad.setBuffer(quadDefaultTC);

        float x0 = x + 0.5f * width  * (1f - imageScale);
        float y0 = y + 0.5f * height * (1f - imageScale);

        tempMat.loadIdentity();
        tempMat.setTranslation(x0, getHeight() - y0, 0);
        tempMat.setScale(width * imageScale, height * imageScale, 0);

        rm.setWorldMatrix(tempMat);
        niftyMat.render(quadGeom, rm);
//        
//        System.out.println("renderImage");
    }
 
Example #17
Source File: WireFrustum.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void update(Vector3f[] points){
    VertexBuffer vb = getBuffer(Type.Position);
    if (vb == null){
        setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(points));
        return;
    }

    FloatBuffer b = BufferUtils.createFloatBuffer(points);
    FloatBuffer a = (FloatBuffer) vb.getData();
    b.rewind();
    a.rewind();
    a.put(b);
    a.rewind();

    vb.updateData(a);
    
    updateBound();
}
 
Example #18
Source File: WireFrustum.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static void initGeom(Mesh m, Vector3f[] points) {
    if (points != null)
        m.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(points));

    m.setBuffer(Type.Index, 2,
            new short[]{
                    0, 1,
                    1, 2,
                    2, 3,
                    3, 0,

                    4, 5,
                    5, 6,
                    6, 7,
                    7, 4,

                    0, 4,
                    1, 5,
                    2, 6,
                    3, 7,
            }
    );
    m.getBuffer(Type.Index).setUsage(Usage.Static);
    m.setMode(Mode.Lines);
}
 
Example #19
Source File: GeometryBatchFactory.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static void printMesh(Mesh mesh) {
    for (int bufType = 0; bufType < Type.values().length; bufType++) {
        VertexBuffer outBuf = mesh.getBuffer(Type.values()[bufType]);
        if (outBuf == null) {
            continue;
        }

        System.out.println(outBuf.getBufferType() + ": ");
        for (int vert = 0; vert < outBuf.getNumElements(); vert++) {
            String str = "[";
            for (int comp = 0; comp < outBuf.getNumComponents(); comp++) {
                Object val = outBuf.getElementComponent(vert, comp);
                outBuf.setElementComponent(vert, comp, val);
                val = outBuf.getElementComponent(vert, comp);
                str += val;
                if (comp != outBuf.getNumComponents() - 1) {
                    str += ", ";
                }
            }
            str += "]";
            System.out.println(str);
        }
        System.out.println("------");
    }
}
 
Example #20
Source File: WireBox.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public WireBox(float xExt, float yExt, float zExt){
    updatePositions(xExt,yExt,zExt);
    setBuffer(Type.Index, 2,
            new short[]{
                 0, 1,
                 1, 2,
                 2, 3,
                 3, 0,

                 4, 5,
                 5, 6,
                 6, 7,
                 7, 4,

                 0, 4,
                 1, 5,
                 2, 6,
                 3, 7,
            }
    );
    setMode(Mode.Lines);

    updateCounts();
}
 
Example #21
Source File: GLRenderer.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void renderMeshVertexArray(Mesh mesh, int lod, int count, VertexBuffer instanceData) {
        if (mesh.getId() == -1) {
            updateVertexArray(mesh, instanceData);
        } else {
            // TODO: Check if it was updated
        }

        if (context.boundVertexArray != mesh.getId()) {
            gl3.glBindVertexArray(mesh.getId());
            context.boundVertexArray = mesh.getId();
        }

//        IntMap<VertexBuffer> buffers = mesh.getBuffers();
        VertexBuffer indices;
        if (mesh.getNumLodLevels() > 0) {
            indices = mesh.getLodLevel(lod);
        } else {
            indices = mesh.getBuffer(Type.Index);
        }
        if (indices != null) {
            drawTriangleList(indices, mesh, count);
        } else {
            drawTriangleArray(mesh.getMode(), count, mesh.getVertexCount());
        }
        clearVertexAttribs();
    }
 
Example #22
Source File: SkeletonControl.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
void resetToBind() {
    for (Mesh mesh : targets) {
        if (mesh.isAnimated()) {
            Buffer bwBuff = mesh.getBuffer(Type.BoneWeight).getData();
            Buffer biBuff = mesh.getBuffer(Type.BoneIndex).getData();
            if (!biBuff.hasArray() || !bwBuff.hasArray()) {
                mesh.prepareForAnim(true); // prepare for software animation
            }
            VertexBuffer bindPos = mesh.getBuffer(Type.BindPosePosition);
            VertexBuffer bindNorm = mesh.getBuffer(Type.BindPoseNormal);
            VertexBuffer pos = mesh.getBuffer(Type.Position);
            VertexBuffer norm = mesh.getBuffer(Type.Normal);
            FloatBuffer pb = (FloatBuffer) pos.getData();
            FloatBuffer nb = (FloatBuffer) norm.getData();
            FloatBuffer bpb = (FloatBuffer) bindPos.getData();
            FloatBuffer bnb = (FloatBuffer) bindNorm.getData();
            pb.clear();
            nb.clear();
            bpb.clear();
            bnb.clear();

            //reseting bind tangents if there is a bind tangent buffer
            VertexBuffer bindTangents = mesh.getBuffer(Type.BindPoseTangent);
            if (bindTangents != null) {
                VertexBuffer tangents = mesh.getBuffer(Type.Tangent);
                FloatBuffer tb = (FloatBuffer) tangents.getData();
                FloatBuffer btb = (FloatBuffer) bindTangents.getData();
                tb.clear();
                btb.clear();
                tb.put(btb).clear();
            }


            pb.put(bpb).clear();
            nb.put(bnb).clear();
        }
    }
}
 
Example #23
Source File: ModelConverter.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void optimize(Mesh mesh, boolean toFixed){
        // update any data that need updating
        mesh.updateBound();
        mesh.updateCounts();

        // set all buffers into STATIC_DRAW mode
        mesh.setStatic();

        if (mesh.getBuffer(Type.Index) != null){
            // compress index buffer from UShort to UByte (if possible)
            FloatToFixed.compressIndexBuffer(mesh);

            // generate triangle strips stitched with degenerate tris
            generateStrips(mesh, false, false, 16, 0);
        }

        IntMap<VertexBuffer> bufs = mesh.getBuffers();
        for (Entry<VertexBuffer> entry : bufs){
            VertexBuffer vb = entry.getValue();
            if (vb == null || vb.getBufferType() == Type.Index)
                continue;

             if (vb.getFormat() == Format.Float){
                if (vb.getBufferType() == Type.Color){
                    // convert the color buffer to UByte
                    vb = FloatToFixed.convertToUByte(vb);
                    vb.setNormalized(true);
                }else if (toFixed){
                    // convert normals, positions, and texcoords
                    // to fixed-point (16.16)
                    vb = FloatToFixed.convertToFixed(vb);
//                    vb = FloatToFixed.convertToFloat(vb);
                }
                mesh.clearBuffer(vb.getBufferType());
                mesh.setBuffer(vb);
            }
        }
        mesh.setInterleaved();
    }
 
Example #24
Source File: TerrainPatch.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void setInBuffer(Mesh mesh, int index, Vector3f normal, Vector3f tangent, Vector3f binormal) {
    VertexBuffer NB = mesh.getBuffer(Type.Normal);
    VertexBuffer TB = mesh.getBuffer(Type.Tangent);
    VertexBuffer BB = mesh.getBuffer(Type.Binormal);
    BufferUtils.setInBuffer(normal, (FloatBuffer)NB.getData(), index);
    BufferUtils.setInBuffer(tangent, (FloatBuffer)TB.getData(), index);
    BufferUtils.setInBuffer(binormal, (FloatBuffer)BB.getData(), index);
    NB.setUpdateNeeded();
    TB.setUpdateNeeded();
    BB.setUpdateNeeded();
}
 
Example #25
Source File: FloatToFixed.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void compressIndexBuffer(Mesh mesh){
    int vertCount = mesh.getVertexCount();
    VertexBuffer vb = mesh.getBuffer(Type.Index);
    Format targetFmt;
    if (vb.getFormat() == Format.UnsignedInt && vertCount <= 0xffff){
        if (vertCount <= 256)
            targetFmt = Format.UnsignedByte;
        else
            targetFmt = Format.UnsignedShort;
    }else if (vb.getFormat() == Format.UnsignedShort && vertCount <= 0xff){
        targetFmt = Format.UnsignedByte;
    }else{
        return;
    }

    IndexBuffer src = mesh.getIndexBuffer();
    Buffer newBuf = VertexBuffer.createBuffer(targetFmt, vb.getNumComponents(), src.size());

    VertexBuffer newVb = new VertexBuffer(Type.Index);
    newVb.setupData(vb.getUsage(), vb.getNumComponents(), targetFmt, newBuf);
    mesh.clearBuffer(Type.Index);
    mesh.setBuffer(newVb);

    IndexBuffer dst = mesh.getIndexBuffer();
    for (int i = 0; i < src.size(); i++){
        dst.put(i, src.get(i));
    }
}
 
Example #26
Source File: LODGeomap.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Mesh createMesh(Vector3f scale, Vector2f tcScale, Vector2f tcOffset, float offsetAmount, int totalSize, boolean center, int lod, boolean rightLod, boolean topLod, boolean leftLod, boolean bottomLod) {
    FloatBuffer pb = writeVertexArray(null, scale, center);
    FloatBuffer texb = writeTexCoordArray(null, tcOffset, tcScale, offsetAmount, totalSize);
    FloatBuffer nb = writeNormalArray(null, scale);
    IndexBuffer ib = writeIndexArrayLodDiff(lod, rightLod, topLod, leftLod, bottomLod, totalSize);
    FloatBuffer bb = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 3);
    FloatBuffer tanb = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 3);
    writeTangentArray(nb, tanb, bb, texb, scale);
    Mesh m = new Mesh();
    m.setMode(Mode.TriangleStrip);
    m.setBuffer(Type.Position, 3, pb);
    m.setBuffer(Type.Normal, 3, nb);
    m.setBuffer(Type.Tangent, 3, tanb);
    m.setBuffer(Type.Binormal, 3, bb);
    m.setBuffer(Type.TexCoord, 2, texb);
    switch (ib.getFormat()) {
        case UnsignedInt:
            m.setBuffer(Type.Index, 3, (IntBuffer) ib.getBuffer());
            break;
        case UnsignedShort:
            m.setBuffer(Type.Index, 3, (ShortBuffer) ib.getBuffer());
            break;
        case UnsignedByte:
            m.setBuffer(Type.Index, 3, (ByteBuffer) ib.getBuffer());
            break;
    }
    m.setStatic();
    m.updateBound();
    return m;
}
 
Example #27
Source File: MeshLoader.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void startLodFaceList(String submeshindex, String numfaces) {
    int index = Integer.parseInt(submeshindex);
    mesh = geoms.get(index).getMesh();
    int faceCount = Integer.parseInt(numfaces);

    VertexBuffer originalIndexBuffer = mesh.getBuffer(Type.Index);
    vb = new VertexBuffer(VertexBuffer.Type.Index);
    if (originalIndexBuffer.getFormat() == Format.UnsignedInt) {
        // LOD buffer should also be integer
        ib = BufferUtils.createIntBuffer(faceCount * 3);
        sb = null;
        vb.setupData(Usage.Static, 3, Format.UnsignedInt, ib);
    } else {
        sb = BufferUtils.createShortBuffer(faceCount * 3);
        ib = null;
        vb.setupData(Usage.Static, 3, Format.UnsignedShort, sb);
    }

    List<VertexBuffer> levels = lodLevels.get(index);
    if (levels == null) {
        // Create the LOD levels list
        levels = new ArrayList<VertexBuffer>();

        // Add the first LOD level (always the original index buffer)
        levels.add(originalIndexBuffer);
        lodLevels.put(index, levels);
    }
    levels.add(vb);
}
 
Example #28
Source File: GeoMap.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Mesh createMesh(Vector3f scale, Vector2f tcScale, boolean center){
    FloatBuffer pb = writeVertexArray(null, scale, center);
    FloatBuffer tb = writeTexCoordArray(null, Vector2f.ZERO, tcScale);
    FloatBuffer nb = writeNormalArray(null, scale);
    IntBuffer ib = writeIndexArray(null);
    Mesh m = new Mesh();
    m.setBuffer(Type.Position, 3, pb);
    m.setBuffer(Type.Normal, 3, nb);
    m.setBuffer(Type.TexCoord, 2, tb);
    m.setBuffer(Type.Index, 3, ib);
    m.setStatic();
    m.updateBound();
    return m;
}
 
Example #29
Source File: TextureAtlas.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Applies the texture coordinates to the given output mesh
 * if the DiffuseMap or ColorMap of the input geometry exist in the atlas.
 * @param geom The geometry to change the texture coordinate buffer on.
 * @param offset Target buffer offset.
 * @param outMesh The mesh to set the coords in (can be same as input).
 * @return true if texture has been found and coords have been changed, false otherwise.
 */
public boolean applyCoords(Geometry geom, int offset, Mesh outMesh) {
    Mesh inMesh = geom.getMesh();
    geom.computeWorldMatrix();

    VertexBuffer inBuf = inMesh.getBuffer(Type.TexCoord);
    VertexBuffer outBuf = outMesh.getBuffer(Type.TexCoord);

    if (inBuf == null || outBuf == null) {
        throw new IllegalStateException("Geometry mesh has no texture coordinate buffer.");
    }

    Texture tex = getMaterialTexture(geom, "DiffuseMap");
    if (tex == null) {
        tex = getMaterialTexture(geom, "ColorMap");

    }
    if (tex != null) {
        TextureAtlasTile tile = getAtlasTile(tex);
        if (tile != null) {
            FloatBuffer inPos = (FloatBuffer) inBuf.getData();
            FloatBuffer outPos = (FloatBuffer) outBuf.getData();
            tile.transformTextureCoords(inPos, offset, outPos);
            return true;
        } else {
            return false;
        }
    } else {
        throw new IllegalStateException("Geometry has no proper texture.");
    }
}
 
Example #30
Source File: SkeletonControl.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Update the mesh according to the given transformation matrices
 *
 * @param mesh then mesh
 * @param offsetMatrices the transformation matrices to apply
 */
private void softwareSkinUpdate(Mesh mesh, Matrix4f[] offsetMatrices) {

    VertexBuffer tb = mesh.getBuffer(Type.Tangent);
    if (tb == null) {
        //if there are no tangents use the classic skinning
        applySkinning(mesh, offsetMatrices);
    } else {
        //if there are tangents use the skinning with tangents
        applySkinningTangents(mesh, offsetMatrices, tb);
    }


}