Java Code Examples for com.jme3.scene.VertexBuffer#setupData()

The following examples show how to use com.jme3.scene.VertexBuffer#setupData() . 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: SkeletonPoints.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Creates a points with bone lengths data. If the data is supplied then the points will show both head and tail of each bone.
 * @param skeleton
 *            the skeleton that will be shown
 * @param boneLengths
 *            a map between the bone's index and the bone's length
 */
public SkeletonPoints(Skeleton skeleton, Map<Integer, Float> boneLengths) {
    this.skeleton = skeleton;
    this.setMode(Mode.Points);
    int pointsCount = skeleton.getBoneCount();

    if (boneLengths != null) {
        this.boneLengths = boneLengths;
        pointsCount *= 2;
    }

    VertexBuffer pb = new VertexBuffer(Type.Position);
    FloatBuffer fpb = BufferUtils.createFloatBuffer(pointsCount * 3);
    pb.setupData(Usage.Stream, 3, Format.Float, fpb);
    this.setBuffer(pb);

    this.updateCounts();

}
 
Example 2
Source File: SkeletonInterBoneWire.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Creates buffers for points. Each line has POINT_AMOUNT of points.
 * @param skeleton
 *            the skeleton that will be showed
 * @param boneLengths
 *            the lengths of the bones
 */
public SkeletonInterBoneWire(Skeleton skeleton, Map<Integer, Float> boneLengths) {
    this.skeleton = skeleton;

    for (Bone bone : skeleton.getRoots()) {
        this.countConnections(bone);
    }

    this.setMode(Mode.Points);
    this.boneLengths = boneLengths;

    VertexBuffer pb = new VertexBuffer(Type.Position);
    FloatBuffer fpb = BufferUtils.createFloatBuffer(POINT_AMOUNT * connectionsAmount * 3);
    pb.setupData(Usage.Stream, 3, Format.Float, fpb);
    this.setBuffer(pb);

    this.updateCounts();
}
 
Example 3
Source File: SkeletonPoints.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public SkeletonPoints(Skeleton skeleton){
    this.skeleton = skeleton;

    setMode(Mode.Points);

    VertexBuffer pb = new VertexBuffer(Type.Position);
    FloatBuffer fpb = BufferUtils.createFloatBuffer(skeleton.getBoneCount() * 3);
    pb.setupData(Usage.Stream, 3, Format.Float, fpb);
    setBuffer(pb);

    setPointSize(7);

    updateCounts();
}
 
Example 4
Source File: FloatToFixed.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static VertexBuffer convertToFloat(VertexBuffer vb){
    if (vb.getFormat() == Format.Float)
        return vb;

    IntBuffer ib = (IntBuffer) vb.getData();
    FloatBuffer fb = BufferUtils.createFloatBuffer(ib.capacity());
    convertToFloat(ib, fb);

    VertexBuffer newVb = new VertexBuffer(vb.getBufferType());
    newVb.setupData(vb.getUsage(),
                    vb.getNumComponents(),
                    Format.Float,
                    fb);
    return newVb;
}
 
Example 5
Source File: FloatToFixed.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static VertexBuffer convertToFixed(VertexBuffer vb){
    if (vb.getFormat() == Format.Int)
        return vb;

    FloatBuffer fb = (FloatBuffer) vb.getData();
    IntBuffer ib = BufferUtils.createIntBuffer(fb.capacity());
    convertToFixed(fb, ib);

    VertexBuffer newVb = new VertexBuffer(vb.getBufferType());
    newVb.setupData(vb.getUsage(),
                    vb.getNumComponents(),
                    Format.Int,
                    ib);
    return newVb;
}
 
Example 6
Source File: PMDLoaderGLSLSkinning2.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
void createSkinCommonVertData() {
        SkinMeshData smd = meshConverter.getSkinMeshData();
        skinvb = new VertexBuffer(VertexBuffer.Type.Position);
        FloatBuffer skinvfb = smd.skinvfb;
        skinvb.setupData(VertexBuffer.Usage.Dynamic, 3, VertexBuffer.Format.Float, skinvfb);

        skinvb2 = new VertexBuffer(VertexBuffer.Type.Position);
        FloatBuffer skinvfb2 = smd.skinvfb2;
        skinvb2.setupData(VertexBuffer.Usage.Dynamic, 3, VertexBuffer.Format.Float, skinvfb2);
        
        skinnb = new VertexBuffer(VertexBuffer.Type.Normal);
        FloatBuffer skinnfb = smd.skinnfb;
        skinnb.setupData(VertexBuffer.Usage.Static, 3, VertexBuffer.Format.Float, skinnfb);

        skintb = new VertexBuffer(VertexBuffer.Type.TexCoord);
        FloatBuffer skintfb = smd.skintfb;
        skintb.setupData(VertexBuffer.Usage.Static, 2, VertexBuffer.Format.Float, skintfb);

        skinbib = new VertexBuffer(VertexBuffer.Type.BoneIndex);
        ShortBuffer skinbisb = smd.skinbisb;
        skinbib.setupData(VertexBuffer.Usage.Static, 2, VertexBuffer.Format.UnsignedShort, skinbisb);

        skinwb = new VertexBuffer(VertexBuffer.Type.BoneWeight);
        FloatBuffer wfb = smd.wfb;
        skinwb.setupData(VertexBuffer.Usage.Static, 2, VertexBuffer.Format.Float, wfb);

        skinvfb.position(0);
        skinvfb2.position(0);
        skinvfb2.put(skinvfb);
        skinnfb.position(0);
//        skinnfb2.position(0);
//        skinnfb2.put(skinnfb);
        skinIndexArray = smd.skinIndexArray;
    }
 
Example 7
Source File: PMDLoaderGLSLSkinning2.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
PMDSkinMesh createSkinMesh(PMDMaterial pmdMaterial) {
        boolean textureFlag = true;
        if (pmdMaterial.getTextureFileName().length() == 0) {
            textureFlag = false;
        }
        PMDSkinMesh mesh = new PMDSkinMesh();
        mesh.setMode(Mesh.Mode.Triangles);
        mesh.setBuffer(skinvb);
        mesh.setSkinvb2(skinvb2);
        mesh.setBuffer(skinnb);
//        mesh.setSkinnb2(skinnb2);
        if (textureFlag) {
            mesh.setBuffer(skintb);
        }
        mesh.setBuffer(skinbib);
        mesh.setBuffer(skinwb);
        VertexBuffer ib = new VertexBuffer(VertexBuffer.Type.Index);
        ShortBuffer isb = meshConverter.getSkinMeshData().indexShortBufferMap.get(pmdMaterial);
        ib.setupData(VertexBuffer.Usage.Static, 1, VertexBuffer.Format.UnsignedShort, isb);
        mesh.setBuffer(ib);
        mesh.setBoneIndexArray(skinIndexArray);
        ShortBuffer boneIndexBuffer = BufferUtils.createShortBuffer(skinIndexArray.length);
        for(int i=0;i<skinIndexArray.length;i++) {
            boneIndexBuffer.put((short)skinIndexArray[i]);
        }
        mesh.setBoneIndexBuffer(boneIndexBuffer);
        mesh.setBoneMatrixArray(new Matrix4f[skinIndexArray.length]);
        for (int i = 0; i < mesh.getBoneMatrixArray().length; i++) {
            mesh.getBoneMatrixArray()[i] = new Matrix4f();
            mesh.getBoneMatrixArray()[i].loadIdentity();
        }
        FloatBuffer boneMatrixBuffer = BufferUtils.createFloatBuffer(skinIndexArray.length * 16);
        mesh.setBoneMatrixBuffer(boneMatrixBuffer);
        return mesh;
    }
 
Example 8
Source File: RenderDeviceJme.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public RenderDeviceJme(NiftyJmeDisplay display) {
    this.display = display;

    quadColor = new VertexBuffer(Type.Color);
    quadColor.setNormalized(true);
    ByteBuffer bb = BufferUtils.createByteBuffer(4 * 4);
    quadColor.setupData(Usage.Stream, 4, Format.UnsignedByte, bb);
    quad.setBuffer(quadColor);

    quadModTC.setUsage(Usage.Stream);

    // Load the 3 material types separately to avoid
    // reloading the shader when the defines change.

    // Material with a single color (no texture or vertex color)
    colorMaterial = new Material(display.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");

    // Material with a texture and a color (no vertex color)
    textureColorMaterial = new Material(display.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");

    // Material with vertex color, used for gradients (no texture)
    vertexColorMaterial = new Material(display.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
    vertexColorMaterial.setBoolean("VertexColor", true);

    // Shared render state for all materials
    renderState.setDepthTest(false);
    renderState.setDepthWrite(false);
}
 
Example 9
Source File: PMDSkinMesh.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
    public synchronized PMDSkinMesh clone() {
        PMDSkinMesh newMesh = (PMDSkinMesh)super.clone();
//        newMesh.boneMatrixArray = new Matrix4f[boneMatrixArray.length];
        newMesh.skinvb2 = new VertexBuffer(VertexBuffer.Type.Position);
        FloatBuffer skinvfb2 = BufferUtils.clone((FloatBuffer)this.skinvb2.getData());
        newMesh.skinvb2.setupData(VertexBuffer.Usage.Dynamic, 3, VertexBuffer.Format.Float, skinvfb2);
        
//        newMesh.skinnb2 = new VertexBuffer(VertexBuffer.Type.Normal);
//        FloatBuffer skinnfb2 = BufferUtils.clone((FloatBuffer)this.skinnb2.getData());
//        newMesh.skinnb2.setupData(VertexBuffer.Usage.Static, 3, VertexBuffer.Format.Float, skinnfb2);
        
        VertexBuffer skinvb1 = new VertexBuffer(VertexBuffer.Type.Position);
//        FloatBuffer skinvfb1 = BufferUtils.clone((FloatBuffer)this.skinvb2.getData());
        FloatBuffer skinvfb1 = BufferUtils.clone((FloatBuffer)this.getBuffer(VertexBuffer.Type.Position).getData());
        skinvb1.setupData(VertexBuffer.Usage.Dynamic, 3, VertexBuffer.Format.Float, skinvfb1);
        newMesh.clearBuffer(VertexBuffer.Type.Position);
        newMesh.setBuffer(skinvb1);
        
//        VertexBuffer skinnb1 = new VertexBuffer(VertexBuffer.Type.Normal);
//        FloatBuffer skinnfb1 = BufferUtils.clone((FloatBuffer)this.skinnb2.getData());
//        FloatBuffer skinnfb1 = BufferUtils.clone((FloatBuffer)this.getBuffer(VertexBuffer.Type.Normal).getData());
//        skinnb1.setupData(VertexBuffer.Usage.Stream, 3, VertexBuffer.Format.Float, skinnfb1);
//        newMesh.clearBuffer(VertexBuffer.Type.Normal);
//        newMesh.setBuffer(skinnb1);
        FloatBuffer newBoneMatrixBuffer = BufferUtils.createFloatBuffer(boneMatrixBuffer.capacity());
        boneMatrixBuffer.position(0);
        newBoneMatrixBuffer.put(boneMatrixBuffer);
        newBoneMatrixBuffer.position(0);
        newMesh.setBoneMatrixBuffer(newBoneMatrixBuffer);
        return newMesh;
    }
 
Example 10
Source File: FloatToFixed.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static VertexBuffer convertToUByte(VertexBuffer vb){
    FloatBuffer fb = (FloatBuffer) vb.getData();
    ByteBuffer bb = BufferUtils.createByteBuffer(fb.capacity());
    convertToUByte(fb, bb);

    VertexBuffer newVb = new VertexBuffer(vb.getBufferType());
    newVb.setupData(vb.getUsage(),
                    vb.getNumComponents(),
                    Format.UnsignedByte,
                    bb);
    newVb.setNormalized(true);
    return newVb;
}
 
Example 11
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 12
Source File: PMDLoaderGLSLSkinning2.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
PMDMesh createMesh_old(MeshData md) {
        boolean textureFlag = true;
        if (md.getMaterial().getTextureFileName().length() == 0) {
            textureFlag = false;
        }
        PMDMesh mesh = new PMDMesh();
        mesh.setMode(Mesh.Mode.Triangles);
        VertexBuffer vb = new VertexBuffer(VertexBuffer.Type.Position);
        VertexBuffer nb = new VertexBuffer(VertexBuffer.Type.Normal);

        VertexBuffer tb = new VertexBuffer(VertexBuffer.Type.TexCoord);

        VertexBuffer wb = new VertexBuffer(VertexBuffer.Type.BoneWeight);
        VertexBuffer ib = new VertexBuffer(VertexBuffer.Type.Index);
        VertexBuffer bib = new VertexBuffer(VertexBuffer.Type.BoneIndex);
        PMDVertex v = new PMDVertex();
//        System.out.println("isb.capacity() = " + isb.capacity());
//        System.out.println("isb.capacity() = " + md.getIndexList().size());
        vb.setupData(VertexBuffer.Usage.Static, 3, VertexBuffer.Format.Float, md.vfb);
        nb.setupData(VertexBuffer.Usage.Static, 3, VertexBuffer.Format.Float, md.nfb);

//        bvb.setupData(VertexBuffer.Usage.CpuOnly, 3, VertexBuffer.Format.Float, bvfb);
//        bnb.setupData(VertexBuffer.Usage.CpuOnly, 3, VertexBuffer.Format.Float, bnfb);
        if (textureFlag) {
            tb.setupData(VertexBuffer.Usage.Static, 2, VertexBuffer.Format.Float, md.tfb);
        }
        wb.setupData(VertexBuffer.Usage.Static, 2, VertexBuffer.Format.Float, md.wfb);
        ib.setupData(VertexBuffer.Usage.Static, 1, VertexBuffer.Format.UnsignedShort, md.isb);
        bib.setupData(VertexBuffer.Usage.Static, 2, VertexBuffer.Format.UnsignedShort, md.bisb);
        mesh.setBuffer(vb);
        mesh.setBuffer(nb);
        
        mesh.setVbBackup(vb);
        mesh.setNbBackup(nb);

//        mesh.setBuffer(bvb);
//        mesh.setBuffer(bnb);
        if (textureFlag) {
            mesh.setBuffer(tb);
        }
        mesh.setBuffer(wb);
        mesh.setBuffer(ib);
        mesh.setBuffer(bib);
        int[] indexArray = md.indexArray;
        mesh.setBoneIndexArray(indexArray);
        mesh.setBoneIndexBuffer(md.indexBuffer);
        FloatBuffer boneMatrixBuffer = BufferUtils.createFloatBuffer(16 * indexArray.length);
        mesh.setBoneMatrixArray(new Matrix4f[indexArray.length]);
        mesh.setBoneMatrixBuffer(boneMatrixBuffer);
        for (int i = 0; i < mesh.getBoneMatrixArray().length; i++) {
            mesh.getBoneMatrixArray()[i] = new Matrix4f();
            mesh.getBoneMatrixArray()[i].loadIdentity();
            mesh.getBoneMatrixArray()[i].fillFloatBuffer(boneMatrixBuffer, true);
        }
        boneMatrixBuffer.position(0);
        return mesh;
    }
 
Example 13
Source File: ParticlePointMesh.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void initParticleData(ParticleEmitter emitter, int numParticles) {
    setMode(Mode.Points);

    this.emitter = emitter;

    // set positions
    FloatBuffer pb = BufferUtils.createVector3Buffer(numParticles);
    VertexBuffer pvb = new VertexBuffer(VertexBuffer.Type.Position);
    pvb.setupData(Usage.Stream, 3, Format.Float, pb);
     
    //if the buffer is already set only update the data
    VertexBuffer buf = getBuffer(VertexBuffer.Type.Position);
    if (buf != null) {
        buf.updateData(pb);
    } else {
        setBuffer(pvb);
    }

    // set colors
    ByteBuffer cb = BufferUtils.createByteBuffer(numParticles * 4);
    VertexBuffer cvb = new VertexBuffer(VertexBuffer.Type.Color);
    cvb.setupData(Usage.Stream, 4, Format.UnsignedByte, cb);
    cvb.setNormalized(true);
    
    buf = getBuffer(VertexBuffer.Type.Color);
    if (buf != null) {
        buf.updateData(cb);
    } else {
        setBuffer(cvb);
    }

    // set sizes
    FloatBuffer sb = BufferUtils.createFloatBuffer(numParticles);
    VertexBuffer svb = new VertexBuffer(VertexBuffer.Type.Size);
    svb.setupData(Usage.Stream, 1, Format.Float, sb);
            
    buf = getBuffer(VertexBuffer.Type.Size);
    if (buf != null) {
        buf.updateData(sb);
    } else {
        setBuffer(svb);
    }

    // set UV-scale
    FloatBuffer tb = BufferUtils.createFloatBuffer(numParticles*4);
    VertexBuffer tvb = new VertexBuffer(VertexBuffer.Type.TexCoord);
    tvb.setupData(Usage.Stream, 4, Format.Float, tb);
    
    buf = getBuffer(VertexBuffer.Type.TexCoord);
    if (buf != null) {
        buf.updateData(tb);
    } else {
        setBuffer(tvb);
    }
}
 
Example 14
Source File: TestCustomAnim.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void simpleInitApp() {

    AmbientLight al = new AmbientLight();
    rootNode.addLight(al);

    DirectionalLight dl = new DirectionalLight();
    dl.setDirection(Vector3f.UNIT_XYZ.negate());
    rootNode.addLight(dl);

    Box box = new Box(1, 1, 1);

    // Setup bone weight buffer
    FloatBuffer weights = FloatBuffer.allocate( box.getVertexCount() * 4 );
    VertexBuffer weightsBuf = new VertexBuffer(Type.BoneWeight);
    weightsBuf.setupData(Usage.CpuOnly, 4, Format.Float, weights);
    box.setBuffer(weightsBuf);

    // Setup bone index buffer
    ByteBuffer indices = ByteBuffer.allocate( box.getVertexCount() * 4 );
    VertexBuffer indicesBuf = new VertexBuffer(Type.BoneIndex);
    indicesBuf.setupData(Usage.CpuOnly, 4, Format.UnsignedByte, indices);
    box.setBuffer(indicesBuf);

    // Create bind pose buffers
    box.generateBindPose(true);

    // Create skeleton
    bone = new Bone("root");
    bone.setBindTransforms(Vector3f.ZERO, Quaternion.IDENTITY, Vector3f.UNIT_XYZ);
    bone.setUserControl(true);
    skeleton = new Skeleton(new Bone[]{ bone });

    // Assign all verticies to bone 0 with weight 1
    for (int i = 0; i < box.getVertexCount() * 4; i += 4){
        // assign vertex to bone index 0
        indices.array()[i+0] = 0;
        indices.array()[i+1] = 0;
        indices.array()[i+2] = 0;
        indices.array()[i+3] = 0;

        // set weight to 1 only for first entry
        weights.array()[i+0] = 1;
        weights.array()[i+1] = 0;
        weights.array()[i+2] = 0;
        weights.array()[i+3] = 0;
    }

    // Maximum number of weights per bone is 1
    box.setMaxNumWeights(1);

    // Create model
    Geometry geom = new Geometry("box", box);
    geom.setMaterial(assetManager.loadMaterial("Textures/Terrain/BrickWall/BrickWall.j3m"));
    Node model = new Node("model");
    model.attachChild(geom);

    // Create skeleton control
    SkeletonControl skeletonControl = new SkeletonControl(skeleton);
    model.addControl(skeletonControl);

    rootNode.attachChild(model);
}
 
Example 15
Source File: ParticleTriMesh.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void initParticleData(ParticleEmitter emitter, int numParticles) {
    setMode(Mode.Triangles);

    this.emitter = emitter;

    particlesCopy = new Particle[numParticles];

    // set positions
    FloatBuffer pb = BufferUtils.createVector3Buffer(numParticles * 4);
    VertexBuffer pvb = new VertexBuffer(VertexBuffer.Type.Position);
    pvb.setupData(Usage.Stream, 3, Format.Float, pb);
    
    //if the buffer is already set only update the data
    VertexBuffer buf = getBuffer(VertexBuffer.Type.Position);
    if (buf != null) {
        buf.updateData(pb);
    } else {
        setBuffer(pvb);
    }
    
    // set colors
    ByteBuffer cb = BufferUtils.createByteBuffer(numParticles * 4 * 4);
    VertexBuffer cvb = new VertexBuffer(VertexBuffer.Type.Color);
    cvb.setupData(Usage.Stream, 4, Format.UnsignedByte, cb);
    cvb.setNormalized(true);
    
    buf = getBuffer(VertexBuffer.Type.Color);
    if (buf != null) {
        buf.updateData(cb);
    } else {
        setBuffer(cvb);
    }

    // set texcoords
    VertexBuffer tvb = new VertexBuffer(VertexBuffer.Type.TexCoord);
    FloatBuffer tb = BufferUtils.createVector2Buffer(numParticles * 4);
    
    uniqueTexCoords = false;
    for (int i = 0; i < numParticles; i++){
        tb.put(0f).put(1f);
        tb.put(1f).put(1f);
        tb.put(0f).put(0f);
        tb.put(1f).put(0f);
    }
    tb.flip();
    tvb.setupData(Usage.Static, 2, Format.Float, tb);
    
    buf = getBuffer(VertexBuffer.Type.TexCoord);
    if (buf != null) {
        buf.updateData(tb);
    } else {
        setBuffer(tvb);
    }

    // set indices
    ShortBuffer ib = BufferUtils.createShortBuffer(numParticles * 6);
    for (int i = 0; i < numParticles; i++){
        int startIdx = (i * 4);

        // triangle 1
        ib.put((short)(startIdx + 1))
          .put((short)(startIdx + 0))
          .put((short)(startIdx + 2));

        // triangle 2
        ib.put((short)(startIdx + 1))
          .put((short)(startIdx + 2))
          .put((short)(startIdx + 3));
    }
    ib.flip();
    
    VertexBuffer ivb = new VertexBuffer(VertexBuffer.Type.Index);
    ivb.setupData(Usage.Static, 3, Format.UnsignedShort, ib);
    
    buf = getBuffer(VertexBuffer.Type.Index);
    if (buf != null) {
        buf.updateData(ib);
    } else {
        setBuffer(ivb);
    }
    
}
 
Example 16
Source File: SilentTangentBinormalGenerator.java    From OpenRTS with MIT License 4 votes vote down vote up
public static void generate(Mesh mesh, boolean approxTangents, boolean splitMirrored) {
	int[] index = new int[3];
	Vector3f[] v = new Vector3f[3];
	Vector2f[] t = new Vector2f[3];
	for (int i = 0; i < 3; i++) {
		v[i] = new Vector3f();
		t[i] = new Vector2f();
	}

	if (mesh.getBuffer(Type.Normal) == null) {
		throw new IllegalArgumentException("The given mesh has no normal data!");
	}

	List<VertexData> vertices;
	switch (mesh.getMode()) {
		case Triangles:
			vertices = processTriangles(mesh, index, v, t, splitMirrored);
			if (splitMirrored) {
				splitVertices(mesh, vertices, splitMirrored);
			}
			break;
		case TriangleStrip:
			vertices = processTriangleStrip(mesh, index, v, t);
			break;
		case TriangleFan:
			vertices = processTriangleFan(mesh, index, v, t);
			break;
		default:
			throw new UnsupportedOperationException(mesh.getMode() + " is not supported.");
	}

	processTriangleData(mesh, vertices, approxTangents, splitMirrored);

	// if the mesh has a bind pose, we need to generate the bind pose for the tangent buffer
	if (mesh.getBuffer(Type.BindPosePosition) != null) {

		VertexBuffer tangents = mesh.getBuffer(Type.Tangent);
		if (tangents != null) {
			VertexBuffer bindTangents = new VertexBuffer(Type.BindPoseTangent);
			bindTangents.setupData(Usage.CpuOnly, 4, Format.Float, BufferUtils.clone(tangents.getData()));

			if (mesh.getBuffer(Type.BindPoseTangent) != null) {
				mesh.clearBuffer(Type.BindPoseTangent);
			}
			mesh.setBuffer(bindTangents);
			tangents.setUsage(Usage.Stream);
		}
	}
}
 
Example 17
Source File: LodGenerator.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private VertexBuffer makeLod(Mesh mesh) {
    VertexBuffer indexBuffer = mesh.getBuffer(VertexBuffer.Type.Index);
    
    boolean isShortBuffer = indexBuffer.getFormat() == VertexBuffer.Format.UnsignedShort;
    // Create buffers.
    VertexBuffer lodBuffer = new VertexBuffer(VertexBuffer.Type.Index);
    int bufsize = indexCount == 0 ? 3 : indexCount;
    
    if (isShortBuffer) {
        lodBuffer.setupData(VertexBuffer.Usage.Static, 3, VertexBuffer.Format.UnsignedShort, BufferUtils.createShortBuffer(bufsize));
    } else {
        lodBuffer.setupData(VertexBuffer.Usage.Static, 3, VertexBuffer.Format.UnsignedInt, BufferUtils.createIntBuffer(bufsize));
    }
    
    
    
    lodBuffer.getData().rewind();
    //Check if we should fill it with a "dummy" triangle.
    if (indexCount == 0) {
        if (isShortBuffer) {
            for (int m = 0; m < 3; m++) {
                ((ShortBuffer) lodBuffer.getData()).put((short) 0);
            }
        } else {
            for (int m = 0; m < 3; m++) {
                ((IntBuffer) lodBuffer.getData()).put(0);
            }
        }
    }

    // Fill buffers.       
    Buffer buf = lodBuffer.getData();
    buf.rewind();
    for (Triangle triangle : triangleList) {
        if (!triangle.isRemoved) {
        //    assert (indexCount != 0);
            if (isShortBuffer) {
                for (int m = 0; m < 3; m++) {
                    ((ShortBuffer) buf).put((short) triangle.vertexId[m]);
                    
                }
            } else {
                for (int m = 0; m < 3; m++) {
                    ((IntBuffer) buf).put(triangle.vertexId[m]);
                }
                
            }
        }
    }
    buf.clear();
    lodBuffer.updateData(buf);
    return lodBuffer;
}
 
Example 18
Source File: ParticleTriMesh.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
    public void initParticleData(ParticleEmitter emitter, int numParticles) {
        setMode(Mode.Triangles);

        this.emitter = emitter;

//        particlesCopy = new Particle[numParticles];

        // set positions
        FloatBuffer pb = BufferUtils.createVector3Buffer(numParticles * 4);
        // if the buffer is already set only update the data
        VertexBuffer buf = getBuffer(VertexBuffer.Type.Position);
        if (buf != null) {
            buf.updateData(pb);
        } else {
            VertexBuffer pvb = new VertexBuffer(VertexBuffer.Type.Position);
            pvb.setupData(Usage.Stream, 3, Format.Float, pb);
            setBuffer(pvb);
        }
        
        // set colors
        ByteBuffer cb = BufferUtils.createByteBuffer(numParticles * 4 * 4);
        buf = getBuffer(VertexBuffer.Type.Color);
        if (buf != null) {
            buf.updateData(cb);
        } else {
            VertexBuffer cvb = new VertexBuffer(VertexBuffer.Type.Color);
            cvb.setupData(Usage.Stream, 4, Format.UnsignedByte, cb);
            cvb.setNormalized(true);
            setBuffer(cvb);
        }

        // set texcoords
        FloatBuffer tb = BufferUtils.createVector2Buffer(numParticles * 4);
        uniqueTexCoords = false;
        for (int i = 0; i < numParticles; i++){
            tb.put(0f).put(1f);
            tb.put(1f).put(1f);
            tb.put(0f).put(0f);
            tb.put(1f).put(0f);
        }
        tb.flip();
        
        buf = getBuffer(VertexBuffer.Type.TexCoord);
        if (buf != null) {
            buf.updateData(tb);
        } else {
            VertexBuffer tvb = new VertexBuffer(VertexBuffer.Type.TexCoord);
            tvb.setupData(Usage.Static, 2, Format.Float, tb);
            setBuffer(tvb);
        }

        // set indices
        ShortBuffer ib = BufferUtils.createShortBuffer(numParticles * 6);
        for (int i = 0; i < numParticles; i++){
            int startIdx = (i * 4);

            // triangle 1
            ib.put((short)(startIdx + 1))
              .put((short)(startIdx + 0))
              .put((short)(startIdx + 2));

            // triangle 2
            ib.put((short)(startIdx + 1))
              .put((short)(startIdx + 2))
              .put((short)(startIdx + 3));
        }
        ib.flip();

        buf = getBuffer(VertexBuffer.Type.Index);
        if (buf != null) {
            buf.updateData(ib);
        } else {
            VertexBuffer ivb = new VertexBuffer(VertexBuffer.Type.Index);
            ivb.setupData(Usage.Static, 3, Format.UnsignedShort, ib);
            setBuffer(ivb);
        }
        
        updateCounts();
    }
 
Example 19
Source File: ParticlePointMesh.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void initParticleData(ParticleEmitter emitter, int numParticles) {
    setMode(Mode.Points);

    this.emitter = emitter;

    // set positions
    FloatBuffer pb = BufferUtils.createVector3Buffer(numParticles);
    
    //if the buffer is already set only update the data
    VertexBuffer buf = getBuffer(VertexBuffer.Type.Position);
    if (buf != null) {
        buf.updateData(pb);
    } else {
        VertexBuffer pvb = new VertexBuffer(VertexBuffer.Type.Position);
        pvb.setupData(Usage.Stream, 3, Format.Float, pb);
        setBuffer(pvb);
    }

    // set colors
    ByteBuffer cb = BufferUtils.createByteBuffer(numParticles * 4);
    
    buf = getBuffer(VertexBuffer.Type.Color);
    if (buf != null) {
        buf.updateData(cb);
    } else {
        VertexBuffer cvb = new VertexBuffer(VertexBuffer.Type.Color);
        cvb.setupData(Usage.Stream, 4, Format.UnsignedByte, cb);
        cvb.setNormalized(true);
        setBuffer(cvb);
    }

    // set sizes
    FloatBuffer sb = BufferUtils.createFloatBuffer(numParticles);
    
    buf = getBuffer(VertexBuffer.Type.Size);
    if (buf != null) {
        buf.updateData(sb);
    } else {
        VertexBuffer svb = new VertexBuffer(VertexBuffer.Type.Size);
        svb.setupData(Usage.Stream, 1, Format.Float, sb);
        setBuffer(svb);
    }

    // set UV-scale
    FloatBuffer tb = BufferUtils.createFloatBuffer(numParticles*4);
    
    buf = getBuffer(VertexBuffer.Type.TexCoord);
    if (buf != null) {
        buf.updateData(tb);
    } else {
        VertexBuffer tvb = new VertexBuffer(VertexBuffer.Type.TexCoord);
        tvb.setupData(Usage.Stream, 4, Format.Float, tb);
        setBuffer(tvb);
    }
    
    updateCounts();
}
 
Example 20
Source File: TestCustomAnim.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void simpleInitApp() {

	AmbientLight al = new AmbientLight();
	rootNode.addLight(al);

	DirectionalLight dl = new DirectionalLight();
	dl.setDirection(Vector3f.UNIT_XYZ.negate());
	rootNode.addLight(dl);

	Box box = new Box(1, 1, 1);

	VertexBuffer weightsHW = new VertexBuffer(Type.HWBoneWeight);
	VertexBuffer indicesHW = new VertexBuffer(Type.HWBoneIndex);
	indicesHW.setUsage(Usage.CpuOnly);
	weightsHW.setUsage(Usage.CpuOnly);
	box.setBuffer(weightsHW);
	box.setBuffer(indicesHW);

	// Setup bone weight buffer
	FloatBuffer weights = FloatBuffer.allocate(box.getVertexCount() * 4);
	VertexBuffer weightsBuf = new VertexBuffer(Type.BoneWeight);
	weightsBuf.setupData(Usage.CpuOnly, 4, Format.Float, weights);
	box.setBuffer(weightsBuf);

	// Setup bone index buffer
	ByteBuffer indices = ByteBuffer.allocate(box.getVertexCount() * 4);
	VertexBuffer indicesBuf = new VertexBuffer(Type.BoneIndex);
	indicesBuf.setupData(Usage.CpuOnly, 4, Format.UnsignedByte, indices);
	box.setBuffer(indicesBuf);

	// Create bind pose buffers
	box.generateBindPose();

	// Create skeleton
	bone = new Joint("root");
	bone.setLocalTransform(new Transform(Vector3f.ZERO, Quaternion.IDENTITY, Vector3f.UNIT_XYZ));
	armature = new Armature(new Joint[] { bone });

	// Assign all verticies to bone 0 with weight 1
	for (int i = 0; i < box.getVertexCount() * 4; i += 4) {
		// assign vertex to bone index 0
		indices.array()[i + 0] = 0;
		indices.array()[i + 1] = 0;
		indices.array()[i + 2] = 0;
		indices.array()[i + 3] = 0;

		// set weight to 1 only for first entry
		weights.array()[i + 0] = 1;
		weights.array()[i + 1] = 0;
		weights.array()[i + 2] = 0;
		weights.array()[i + 3] = 0;
	}

	// Maximum number of weights per bone is 1
	box.setMaxNumWeights(1);

	// Create model
	Geometry geom = new Geometry("box", box);
	geom.setMaterial(assetManager.loadMaterial("Textures/Terrain/BrickWall/BrickWall.j3m"));
	Node model = new Node("model");
	model.attachChild(geom);

	// Create skeleton control
	SkinningControl skinningControl = new SkinningControl(armature);
	model.addControl(skinningControl);

	rootNode.attachChild(model);
}