Java Code Examples for java.nio.FloatBuffer#rewind()

The following examples show how to use java.nio.FloatBuffer#rewind() . 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: Shader.java    From Ultraino with MIT License 6 votes vote down vote up
void bindUniforms(GL2 gl, Scene scene,Renderer renderer, Simulation s, MeshEntity me, 
        Matrix4f projectionViewModel, Matrix4f viewModel, Matrix4f model,
        FloatBuffer fb) {
    
    fb.rewind();
    gl.glUniformMatrix4fv(mvpMatrixHandle, 1, false, projectionViewModel.fillFloatBuffer(fb, true));
    gl.glUniformMatrix4fv(mvMatrixHandle, 1, false, viewModel.fillFloatBuffer(fb, true));
    gl.glUniformMatrix4fv(mMatrixHandle, 1, false, model.fillFloatBuffer(fb, true));
    
    Vector3f lightPos = scene.getLight().getTransform().getTranslation();
    Vector3f eyePos = scene.getCamera().getTransform().getTranslation();
    
    gl.glUniform4f(lightPosHandle, lightPos.x, lightPos.y, lightPos.z, 1);
    gl.glUniform4f(eyePosHandle, eyePos.x, eyePos.y, eyePos.z, 1);

    float r = Color.red(me.getColor()) / 255.0f;
    float g = Color.green(me.getColor()) / 255.0f;
    float b = Color.blue(me.getColor()) / 255.0f;
    float a = Color.alpha(me.getColor()) / 255.0f;
    gl.glUniform4f(colorHandle, r, g, b, a);

    gl.glUniform1f(ambient, me.getMaterial().getAmbient());
    gl.glUniform1f(diffuse, me.getMaterial().getDiffuse());
    gl.glUniform1f(specular, me.getMaterial().getSpecular());
    gl.glUniform1f(shininess, me.getMaterial().getShininess());
}
 
Example 2
Source File: WireFrustum.java    From MikuMikuStudio with BSD 2-Clause "Simplified" 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 3
Source File: DMesh.java    From Lemur with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected void morph( VertexBuffer sourcePos, VertexBuffer sourceNorms,
                      VertexBuffer targetPos, VertexBuffer targetNorms ) {
    FloatBuffer sp = (FloatBuffer)sourcePos.getData();
    sp.rewind();

    FloatBuffer sn = (FloatBuffer)sourceNorms.getData();
    sn.rewind();

    FloatBuffer tp = (FloatBuffer)targetPos.getData();
    tp.rewind();

    FloatBuffer tn = (FloatBuffer)targetNorms.getData();
    tn.rewind();

    morph(sp, sn, tp, tn);

    sp.rewind();
    sn.rewind();

    tp.rewind();
    targetPos.updateData(tp);
    tn.rewind();
    targetNorms.updateData(tn);
}
 
Example 4
Source File: HullCollisionShape.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
protected float[] getPoints(Mesh mesh) {
    FloatBuffer vertices = mesh.getFloatBuffer(Type.Position);
    vertices.rewind();
    int components = mesh.getVertexCount() * 3;
    float[] pointsArray = new float[components];
    for (int i = 0; i < components; i += 3) {
        pointsArray[i] = vertices.get();
        pointsArray[i + 1] = vertices.get();
        pointsArray[i + 2] = vertices.get();
    }
    return pointsArray;
}
 
Example 5
Source File: BasicProfiler.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void updateMesh() {
    FloatBuffer pb = (FloatBuffer)mesh.getBuffer(Type.Position).getData();
    pb.rewind();
    float scale = 1 / 1000000f; // scaled to ms as pixels
    for( int i = 0; i < size; i++ ) {
        float t1 = frames[i * 2] * scale;
        float t2 = frames[i * 2 + 1] * scale;
        
        pb.put(i).put(0).put(0);
        pb.put(i).put(t1).put(0);
        pb.put(i).put(t1).put(0);
        pb.put(i).put(t2).put(0);
    }
    mesh.setBuffer(Type.Position, 3, pb);
}
 
Example 6
Source File: BoundingSphere.java    From Ultraino with MIT License 5 votes vote down vote up
/**
 * Calculates a minimum bounding sphere for the copyTo of points. The algorithm
 * was originally found in C++ at
 * <p><a href="http://www.flipcode.com/cgi-bin/msg.cgi?showThread=COTD-SmallestEnclosingSpheres&forum=cotd&id=-1">
 * http://www.flipcode.com/cgi-bin/msg.cgi?showThread=COTD-SmallestEnclosingSpheres&forum=cotd&id=-1</a><br><strong>broken link</strong></p>
 * <p>and translated to java by Cep21</p>
 *
 * @param points
 *            The points to calculate the minimum bounds from.
 */
public void calcWelzl(FloatBuffer points) {
    if (center == null) {
        center = new Vector3f();
    }
    FloatBuffer buf = BufferUtils.createFloatBuffer(points.limit());
    points.rewind();
    buf.put(points);
    buf.flip();
    recurseMini(buf, buf.limit() / 3, 0, 0);
}
 
Example 7
Source File: Matrix3f.java    From Ultraino with MIT License 5 votes vote down vote up
/**
 * <code>toFloatBuffer</code> returns a FloatBuffer object that contains
 * the matrix data.
 * 
 * @return matrix data as a FloatBuffer.
 */
public FloatBuffer toFloatBuffer() {
    FloatBuffer fb = BufferUtils.createFloatBuffer(9);

    fb.put(m00).put(m01).put(m02);
    fb.put(m10).put(m11).put(m12);
    fb.put(m20).put(m21).put(m22);
    fb.rewind();
    return fb;
}
 
Example 8
Source File: NativeMeshUtil.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static long getTriangleIndexVertexArray(Mesh mesh){
    ByteBuffer triangleIndexBase = BufferUtils.createByteBuffer(mesh.getTriangleCount() * 3 * 4);
    ByteBuffer vertexBase = BufferUtils.createByteBuffer(mesh.getVertexCount() * 3 * 4);
    int numVertices = mesh.getVertexCount();
    int vertexStride = 12; //3 verts * 4 bytes per.
    int numTriangles = mesh.getTriangleCount();
    int triangleIndexStride = 12; //3 index entries * 4 bytes each.

    IndexBuffer indices = mesh.getIndicesAsList();
    FloatBuffer vertices = mesh.getFloatBuffer(Type.Position);
    vertices.rewind();

    int verticesLength = mesh.getVertexCount() * 3;
    for (int i = 0; i < verticesLength; i++) {
        float tempFloat = vertices.get();
        vertexBase.putFloat(tempFloat);
    }

    int indicesLength = mesh.getTriangleCount() * 3;
    for (int i = 0; i < indicesLength; i++) {
        triangleIndexBase.putInt(indices.get(i));
    }
    vertices.rewind();
    vertices.clear();

    return createTriangleIndexVertexArray(triangleIndexBase, vertexBase, numTriangles, numVertices, vertexStride, triangleIndexStride);
}
 
Example 9
Source File: MeshCollisionShape.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void createCollisionMesh(Mesh mesh) {
    this.triangleIndexBase = BufferUtils.createByteBuffer(mesh.getTriangleCount() * 3 * 4);
    this.vertexBase = BufferUtils.createByteBuffer(mesh.getVertexCount() * 3 * 4);
    this.numVertices = mesh.getVertexCount();
    this.vertexStride = 12; // 3 verts * 4 bytes per.
    this.numTriangles = mesh.getTriangleCount();
    this.triangleIndexStride = 12; // 3 index entries * 4 bytes each.

    IndexBuffer indices = mesh.getIndicesAsList();
    FloatBuffer vertices = mesh.getFloatBuffer(Type.Position);
    vertices.rewind();

    int verticesLength = mesh.getVertexCount() * 3;
    for (int i = 0; i < verticesLength; i++) {
        float tempFloat = vertices.get();
        vertexBase.putFloat(tempFloat);
    }

    int indicesLength = mesh.getTriangleCount() * 3;
    for (int i = 0; i < indicesLength; i++) {
        triangleIndexBase.putInt(indices.get(i));
    }
    vertices.rewind();
    vertices.clear();

    this.createShape(null);
}
 
Example 10
Source File: BufferUtils.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Ensures there is at least the <code>required</code> number of entries left after the current position of the
 * buffer. If the buffer is too small a larger one is created and the old one copied to the new buffer.
 * @param buffer buffer that should be checked/copied (may be null)
 * @param required minimum number of elements that should be remaining in the returned buffer
 * @return a buffer large enough to receive at least the <code>required</code> number of entries, same position as
 * the input buffer, not null
 */
public static FloatBuffer ensureLargeEnough(FloatBuffer buffer, int required) {
    if (buffer == null || (buffer.remaining() < required)) {
        int position = (buffer != null ? buffer.position() : 0);
        FloatBuffer newVerts = createFloatBuffer(position + required);
        if (buffer != null) {
            buffer.rewind();
            newVerts.put(buffer);
            newVerts.position(position);
        }
        buffer = newVerts;
    }
    return buffer;
}
 
Example 11
Source File: HullCollisionShape.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected float[] getPoints(Mesh mesh) {
    FloatBuffer vertices = mesh.getFloatBuffer(Type.Position);
    vertices.rewind();
    int components = mesh.getVertexCount() * 3;
    float[] pointsArray = new float[components];
    for (int i = 0; i < components; i += 3) {
        pointsArray[i] = vertices.get();
        pointsArray[i + 1] = vertices.get();
        pointsArray[i + 2] = vertices.get();
    }
    return pointsArray;
}
 
Example 12
Source File: MeshCollisionShape.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void createCollisionMesh(Mesh mesh) {
    triangleIndexBase = BufferUtils.createByteBuffer(mesh.getTriangleCount() * 3 * 4);
    vertexBase = BufferUtils.createByteBuffer(mesh.getVertexCount() * 3 * 4);
    numVertices = mesh.getVertexCount();
    vertexStride = 12; //3 verts * 4 bytes per.
    numTriangles = mesh.getTriangleCount();
    triangleIndexStride = 12; //3 index entries * 4 bytes each.

    IndexBuffer indices = mesh.getIndexBuffer();
    FloatBuffer vertices = mesh.getFloatBuffer(Type.Position);
    vertices.rewind();

    int verticesLength = mesh.getVertexCount() * 3;
    for (int i = 0; i < verticesLength; i++) {
        float tempFloat = vertices.get();
        vertexBase.putFloat(tempFloat);
    }

    int indicesLength = mesh.getTriangleCount() * 3;
    for (int i = 0; i < indicesLength; i++) {
        triangleIndexBase.putInt(indices.get(i));
    }
    vertices.rewind();
    vertices.clear();

    createShape();
}
 
Example 13
Source File: Line.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Fit this line to the specified points.
 *
 * @param points a buffer containing location vectors, or null
 */
public void orthogonalLineFit(FloatBuffer points) {
    if (points == null) {
        return;
    }

    TempVars vars = TempVars.get();

    Vector3f compVec1 = vars.vect1;
    Vector3f compVec2 = vars.vect2;
    Matrix3f compMat1 = vars.tempMat3;
    Eigen3f compEigen1 = vars.eigen;

    points.rewind();

    // compute average of points
    int length = points.remaining() / 3;

    BufferUtils.populateFromBuffer(origin, points, 0);
    for (int i = 1; i < length; i++) {
        BufferUtils.populateFromBuffer(compVec1, points, i);
        origin.addLocal(compVec1);
    }

    origin.multLocal(1f / length);

    // compute sums of products
    float sumXX = 0.0f, sumXY = 0.0f, sumXZ = 0.0f;
    float sumYY = 0.0f, sumYZ = 0.0f, sumZZ = 0.0f;

    points.rewind();
    for (int i = 0; i < length; i++) {
        BufferUtils.populateFromBuffer(compVec1, points, i);
        compVec1.subtract(origin, compVec2);
        sumXX += compVec2.x * compVec2.x;
        sumXY += compVec2.x * compVec2.y;
        sumXZ += compVec2.x * compVec2.z;
        sumYY += compVec2.y * compVec2.y;
        sumYZ += compVec2.y * compVec2.z;
        sumZZ += compVec2.z * compVec2.z;
    }

    //find the smallest eigen vector for the direction vector
    compMat1.m00 = sumYY + sumZZ;
    compMat1.m01 = -sumXY;
    compMat1.m02 = -sumXZ;
    compMat1.m10 = -sumXY;
    compMat1.m11 = sumXX + sumZZ;
    compMat1.m12 = -sumYZ;
    compMat1.m20 = -sumXZ;
    compMat1.m21 = -sumYZ;
    compMat1.m22 = sumXX + sumYY;

    compEigen1.calculateEigen(compMat1);
    direction = compEigen1.getEigenVector(0);

    vars.release();
}
 
Example 14
Source File: SkinningControl.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Method to apply skinning transforms to a mesh's buffers
 *
 * @param mesh           the mesh
 * @param offsetMatrices the offset matices to apply
 */
private void applySkinning(Mesh mesh, Matrix4f[] offsetMatrices) {
    int maxWeightsPerVert = mesh.getMaxNumWeights();
    if (maxWeightsPerVert <= 0) {
        throw new IllegalStateException("Max weights per vert is incorrectly set!");
    }
    int fourMinusMaxWeights = 4 - maxWeightsPerVert;

    // NOTE: This code assumes the vertex buffer is in bind pose
    // resetToBind() has been called this frame
    VertexBuffer vb = mesh.getBuffer(Type.Position);
    FloatBuffer fvb = (FloatBuffer) vb.getData();
    fvb.rewind();

    VertexBuffer nb = mesh.getBuffer(Type.Normal);
    FloatBuffer fnb = (FloatBuffer) nb.getData();
    fnb.rewind();

    // get boneIndexes and weights for mesh
    IndexBuffer ib = IndexBuffer.wrapIndexBuffer(mesh.getBuffer(Type.BoneIndex).getData());
    FloatBuffer wb = (FloatBuffer) mesh.getBuffer(Type.BoneWeight).getData();

    wb.rewind();

    float[] weights = wb.array();
    int idxWeights = 0;

    TempVars vars = TempVars.get();

    float[] posBuf = vars.skinPositions;
    float[] normBuf = vars.skinNormals;

    int iterations = (int) FastMath.ceil(fvb.limit() / ((float) posBuf.length));
    int bufLength = posBuf.length;
    for (int i = iterations - 1; i >= 0; i--) {
        // read next set of positions and normals from native buffer
        bufLength = Math.min(posBuf.length, fvb.remaining());
        fvb.get(posBuf, 0, bufLength);
        fnb.get(normBuf, 0, bufLength);
        int verts = bufLength / 3;
        int idxPositions = 0;

        // iterate vertices and apply skinning transform for each effecting bone
        for (int vert = verts - 1; vert >= 0; vert--) {
            // Skip this vertex if the first weight is zero.
            if (weights[idxWeights] == 0) {
                idxPositions += 3;
                idxWeights += 4;
                continue;
            }

            float nmx = normBuf[idxPositions];
            float vtx = posBuf[idxPositions++];
            float nmy = normBuf[idxPositions];
            float vty = posBuf[idxPositions++];
            float nmz = normBuf[idxPositions];
            float vtz = posBuf[idxPositions++];

            float rx = 0, ry = 0, rz = 0, rnx = 0, rny = 0, rnz = 0;

            for (int w = maxWeightsPerVert - 1; w >= 0; w--) {
                float weight = weights[idxWeights];
                Matrix4f mat = offsetMatrices[ib.get(idxWeights++)];

                rx += (mat.m00 * vtx + mat.m01 * vty + mat.m02 * vtz + mat.m03) * weight;
                ry += (mat.m10 * vtx + mat.m11 * vty + mat.m12 * vtz + mat.m13) * weight;
                rz += (mat.m20 * vtx + mat.m21 * vty + mat.m22 * vtz + mat.m23) * weight;

                rnx += (nmx * mat.m00 + nmy * mat.m01 + nmz * mat.m02) * weight;
                rny += (nmx * mat.m10 + nmy * mat.m11 + nmz * mat.m12) * weight;
                rnz += (nmx * mat.m20 + nmy * mat.m21 + nmz * mat.m22) * weight;
            }

            idxWeights += fourMinusMaxWeights;

            idxPositions -= 3;
            normBuf[idxPositions] = rnx;
            posBuf[idxPositions++] = rx;
            normBuf[idxPositions] = rny;
            posBuf[idxPositions++] = ry;
            normBuf[idxPositions] = rnz;
            posBuf[idxPositions++] = rz;
        }

        fvb.position(fvb.position() - bufLength);
        fvb.put(posBuf, 0, bufLength);
        fnb.position(fnb.position() - bufLength);
        fnb.put(normBuf, 0, bufLength);
    }

    vars.release();

    vb.updateData(fvb);
    nb.updateData(fnb);

}
 
Example 15
Source File: Model.java    From Robot-Overlord-App with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Regenerate the optimized rendering buffers for the fixed function pipeline.
 * Also recalculate the bounding box.
 * @param gl2
 */
private void updateBuffers(GL2 gl2) {
	int numVertexes = vertexArray.size()/3;
	Iterator<Float> fi;
	int j=0;

	Point3d boundBottom = new Point3d(Double.MAX_VALUE,Double.MAX_VALUE,Double.MAX_VALUE);
	Point3d boundTop = new Point3d(-Double.MAX_VALUE,-Double.MAX_VALUE,-Double.MAX_VALUE);

	FloatBuffer vertices = FloatBuffer.allocate(vertexArray.size());
	fi = vertexArray.iterator();
	Point3d p = new Point3d();
	while(fi.hasNext()) {
		p.x = fi.next().floatValue();
		p.y = fi.next().floatValue();
		p.z = fi.next().floatValue();
		adjust.transform(p);
		vertices.put(j++, (float)p.x);
		vertices.put(j++, (float)p.y);
		vertices.put(j++, (float)p.z);
		
		// also recalculate the bounding limits			
		if(boundBottom.x>p.x) boundBottom.x=p.x;
		if(boundBottom.y>p.y) boundBottom.y=p.y;
		if(boundBottom.z>p.z) boundBottom.z=p.z;
		if(boundTop.x<p.x) boundTop.x=p.x;
		if(boundTop.y<p.y) boundTop.y=p.y;
		if(boundTop.z<p.z) boundTop.z=p.z;
	}
	
	cuboid.setBounds(boundTop, boundBottom);

	int s=(Float.SIZE/8);  // bits per float / bits per byte = bytes per float
	int totalBufferSize = numVertexes*3*s;
	int vboIndex=0;
	
	// bind a buffer
	vertices.rewind();
	gl2.glBindBuffer(GL2.GL_ARRAY_BUFFER, VBO[vboIndex]);
    // Write out vertex buffer to the currently bound VBO.
    gl2.glBufferData(GL2.GL_ARRAY_BUFFER, totalBufferSize, vertices, GL2.GL_STATIC_DRAW);
    vboIndex++;
    
	if(hasNormals) {
		j=0;
	    // repeat for normals
		Matrix3d pose = new Matrix3d();
		adjust.get(pose);
		FloatBuffer normals = FloatBuffer.allocate(normalArray.size());
		fi = normalArray.iterator();
		while(fi.hasNext()) {
			p.x = fi.next().floatValue();
			p.y = fi.next().floatValue();
			p.z = fi.next().floatValue();
			pose.transform(p);
			normals.put(j++, (float)p.x);
			normals.put(j++, (float)p.y);
			normals.put(j++, (float)p.z);
		}
		
		normals.rewind();
		gl2.glBindBuffer(GL2.GL_ARRAY_BUFFER, VBO[vboIndex]);
	    gl2.glBufferData(GL2.GL_ARRAY_BUFFER, totalBufferSize, normals, GL2.GL_STATIC_DRAW);
	    vboIndex++;
	}

	if(hasColors) {
	    // repeat for colors
		FloatBuffer colors = FloatBuffer.allocate(colorArray.size());
		fi = colorArray.iterator();
		while(fi.hasNext()) {
			colors.put(fi.next().floatValue());
		}
		
		colors.rewind();
		gl2.glBindBuffer(GL2.GL_ARRAY_BUFFER, VBO[vboIndex]);
	    gl2.glBufferData(GL2.GL_ARRAY_BUFFER, totalBufferSize, colors, GL2.GL_STATIC_DRAW);
	    vboIndex++;
	}
	
	if(hasUVs) {
	    // repeat for textures
		FloatBuffer texCoords = FloatBuffer.allocate(texCoordArray.size());
		fi = texCoordArray.iterator();
		while(fi.hasNext()) {
			texCoords.put(fi.next().floatValue());
		}
		
	    texCoords.rewind();
		gl2.glBindBuffer(GL2.GL_ARRAY_BUFFER, VBO[vboIndex]);
	    gl2.glBufferData(GL2.GL_ARRAY_BUFFER, numVertexes*2*s, texCoords, GL2.GL_STATIC_DRAW);
	    vboIndex++;
	}
}
 
Example 16
Source File: BatchNode.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private void doTransformsTangents(FloatBuffer bindBufPos, FloatBuffer bindBufNorm, FloatBuffer bindBufTangents,FloatBuffer bufPos, FloatBuffer bufNorm, FloatBuffer bufTangents, int start, int end, Matrix4f transform) {
    TempVars vars = TempVars.get();
    Vector3f pos = vars.vect1;
    Vector3f norm = vars.vect2;
    Vector3f tan = vars.vect3;

    int length = (end - start) * 3;
    int tanLength = (end - start) * 4;

    // offset is given in element units
    // convert to be in component units
    int offset = start * 3;
    int tanOffset = start * 4;

    
    bindBufPos.rewind();
    bindBufNorm.rewind();
    bindBufTangents.rewind();
    bindBufPos.get(tmpFloat, 0, length);
    bindBufNorm.get(tmpFloatN, 0, length);
    bindBufTangents.get(tmpFloatT, 0, tanLength);

    int index = 0;
    int tanIndex = 0;
    while (index < length) {
        pos.x = tmpFloat[index];
        norm.x = tmpFloatN[index++];
        pos.y = tmpFloat[index];
        norm.y = tmpFloatN[index++];
        pos.z = tmpFloat[index];
        norm.z = tmpFloatN[index];

        tan.x = tmpFloatT[tanIndex++];
        tan.y = tmpFloatT[tanIndex++];
        tan.z = tmpFloatT[tanIndex++];

        transform.mult(pos, pos);
        transform.multNormal(norm, norm);
        transform.multNormal(tan, tan);

        index -= 2;
        tanIndex -= 3;

        tmpFloat[index] = pos.x;
        tmpFloatN[index++] = norm.x;
        tmpFloat[index] = pos.y;
        tmpFloatN[index++] = norm.y;
        tmpFloat[index] = pos.z;
        tmpFloatN[index++] = norm.z;

        tmpFloatT[tanIndex++] = tan.x;
        tmpFloatT[tanIndex++] = tan.y;
        tmpFloatT[tanIndex++] = tan.z;

        //Skipping 4th element of tangent buffer (handedness)
        tanIndex++;

    }
    vars.release();
    bufPos.position(offset);
    //using bulk put as it's faster
    bufPos.put(tmpFloat, 0, length);
    bufNorm.position(offset);
    //using bulk put as it's faster
    bufNorm.put(tmpFloatN, 0, length);
    bufTangents.position(tanOffset);
    //using bulk put as it's faster
    bufTangents.put(tmpFloatT, 0, tanLength);
}
 
Example 17
Source File: PMDNode.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private void softwareSkinUpdate(PMDMesh mesh){
        int maxWeightsPerVert = 2;//mesh.getMaxNumWeights();
        int fourMinusMaxWeights = 4 - maxWeightsPerVert;
//        Matrix4f[] offsetMatrices = mesh.getBoneMatrixArray();

        // NOTE: This code assumes the vertex buffer is in bind pose
        // resetToBind() has been called this frame
        resetToBind(mesh);
        VertexBuffer vb = mesh.getBuffer(VertexBuffer.Type.Position);
        FloatBuffer fvb = (FloatBuffer) vb.getData();
        fvb.rewind();

        VertexBuffer nb = mesh.getBuffer(VertexBuffer.Type.Normal);
        FloatBuffer fnb = (FloatBuffer) nb.getData();
        fnb.rewind();
        
        FloatBuffer fvb2 = (FloatBuffer)mesh.getVbBackup().getData();
        fvb2.rewind();
        FloatBuffer fnb2 = (FloatBuffer)mesh.getNbBackup().getData();
        fnb2.rewind();
        
        // get boneIndexes and weights for mesh
        ShortBuffer ib = (ShortBuffer) mesh.getBuffer(VertexBuffer.Type.BoneIndex).getData();
        FloatBuffer wb = (FloatBuffer) mesh.getBuffer(VertexBuffer.Type.BoneWeight).getData();

        ib.rewind();
        wb.rewind();

//        float[] weights = wb.array();
//        short[] indices = ib.array();
        int idxWeights = 0;

        TempVars vars = TempVars.get();
        float[] posBuf = vars.skinPositions;
        float[] normBuf = vars.skinNormals;

        int iterations = (int) FastMath.ceil(fvb.capacity() / ((float)posBuf.length));
        int bufLength = posBuf.length * 3;
        for (int i = iterations-1; i >= 0; i--){
            // read next set of positions and normals from native buffer
            bufLength = Math.min(posBuf.length, fvb.remaining());
            fvb2.get(posBuf, 0, bufLength);
            fnb2.get(normBuf, 0, bufLength);
            int verts = bufLength / 3;
            int idxPositions = 0;

            // iterate vertices and apply skinning transform for each effecting bone
            for (int vert = verts - 1; vert >= 0; vert--){
                float nmx = normBuf[idxPositions];
                float vtx = posBuf[idxPositions++];
                float nmy = normBuf[idxPositions];
                float vty = posBuf[idxPositions++];
                float nmz = normBuf[idxPositions];
                float vtz = posBuf[idxPositions++];

                float rx=0, ry=0, rz=0, rnx=0, rny=0, rnz=0;

                for (int w = maxWeightsPerVert - 1; w >= 0; w--){
                    float weight = wb.get(idxWeights); //weights[idxWeights];
                    Matrix4f mat = mesh.getBoneMatrixArray()[ib.get(idxWeights++)];//offsetMatrices[indices[idxWeights++]];

                    rx += (mat.m00 * vtx + mat.m01 * vty + mat.m02 * vtz + mat.m03) * weight;
                    ry += (mat.m10 * vtx + mat.m11 * vty + mat.m12 * vtz + mat.m13) * weight;
                    rz += (mat.m20 * vtx + mat.m21 * vty + mat.m22 * vtz + mat.m23) * weight;

                    rnx += (nmx * mat.m00 + nmy * mat.m01 + nmz * mat.m02) * weight;
                    rny += (nmx * mat.m10 + nmy * mat.m11 + nmz * mat.m12) * weight;
                    rnz += (nmx * mat.m20 + nmy * mat.m21 + nmz * mat.m22) * weight;
                }

                idxWeights += fourMinusMaxWeights;

                idxPositions -= 3;
                normBuf[idxPositions] = rnx;
                posBuf[idxPositions++] = rx;
                normBuf[idxPositions] = rny;
                posBuf[idxPositions++] = ry;
                normBuf[idxPositions] = rnz;
                posBuf[idxPositions++] = rz;
            }


//            fvb.position(fvb2.position()-bufLength);
            fvb.put(posBuf, 0, bufLength);
//            fnb.position(fnb2.position()-bufLength);
            fnb.put(normBuf, 0, bufLength);
        }
        vb.setUpdateNeeded();
        nb.setUpdateNeeded();
        vars.release();
        
//        mesh.updateBound();
    }
 
Example 18
Source File: ParticlePointMesh.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void updateParticleData(Particle[] particles, Camera cam, Matrix3f inverseRotation) {
    VertexBuffer pvb = getBuffer(VertexBuffer.Type.Position);
    FloatBuffer positions = (FloatBuffer) pvb.getData();

    VertexBuffer cvb = getBuffer(VertexBuffer.Type.Color);
    ByteBuffer colors = (ByteBuffer) cvb.getData();

    VertexBuffer svb = getBuffer(VertexBuffer.Type.Size);
    FloatBuffer sizes = (FloatBuffer) svb.getData();

    VertexBuffer tvb = getBuffer(VertexBuffer.Type.TexCoord);
    FloatBuffer texcoords = (FloatBuffer) tvb.getData();

    float sizeScale = emitter.getWorldScale().x;

    // update data in vertex buffers
    positions.rewind();
    colors.rewind();
    sizes.rewind();
    texcoords.rewind();
    for (int i = 0; i < particles.length; i++){
        Particle p = particles[i];
        
        positions.put(p.position.x)
                 .put(p.position.y)
                 .put(p.position.z);

        sizes.put(p.size * sizeScale);
        colors.putInt(p.color.asIntABGR());

        int imgX = p.imageIndex % imagesX;
        int imgY = p.imageIndex/imagesX;

        float startX = ((float) imgX) / imagesX;
        float startY = ((float) imgY) / imagesY;
        float endX   = startX + (1f / imagesX);
        float endY   = startY + (1f / imagesY);

        texcoords.put(startX).put(startY).put(endX).put(endY);
    }
    positions.flip();
    colors.flip();
    sizes.flip();
    texcoords.flip();

    // force renderer to re-send data to GPU
    pvb.updateData(positions);
    cvb.updateData(colors);
    svb.updateData(sizes);
    tvb.updateData(texcoords);
}
 
Example 19
Source File: BatchNode.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void doTransforms(FloatBuffer bindBufPos, FloatBuffer bindBufNorm, FloatBuffer bindBufTangents, FloatBuffer bufPos, FloatBuffer bufNorm, FloatBuffer bufTangents, int start, int end, Matrix4f transform) {
    TempVars vars = TempVars.get();
    Vector3f pos = vars.vect1;
    Vector3f norm = vars.vect2;
    Vector3f tan = vars.vect3;

    int length = (end - start) * 3;
    int tanLength = (end - start) * 4;

    // offset is given in element units
    // convert to be in component units
    int offset = start * 3;
    int tanOffset = start * 4;

    bindBufPos.rewind();
    bindBufPos.get(tmpFloat, 0, length);

    if (bindBufNorm != null) {
        bindBufNorm.rewind();
        bindBufNorm.get(tmpFloatN, 0, length);
    }

    if (bindBufTangents != null) {
        bindBufTangents.rewind();
        bindBufTangents.get(tmpFloatT, 0, tanLength);
    }

    int index = 0;
    int tanIndex = 0;
    int index1, index2, tanIndex1, tanIndex2;

    while (index < length) {
        index1 = index + 1;
        index2 = index + 2;

        pos.x = tmpFloat[index];
        pos.y = tmpFloat[index1];
        pos.z = tmpFloat[index2];
        transform.mult(pos, pos);
        tmpFloat[index] = pos.x;
        tmpFloat[index1] = pos.y;
        tmpFloat[index2] = pos.z;

        if (bindBufNorm != null) {
            norm.x = tmpFloatN[index];
            norm.y = tmpFloatN[index1];
            norm.z = tmpFloatN[index2];
            transform.multNormal(norm, norm);
            tmpFloatN[index] = norm.x;
            tmpFloatN[index1] = norm.y;
            tmpFloatN[index2] = norm.z;
        }

        index += 3;

        if (bindBufTangents != null) {
            tanIndex1 = tanIndex + 1;
            tanIndex2 = tanIndex + 2;
            tan.x = tmpFloatT[tanIndex];
            tan.y = tmpFloatT[tanIndex1];
            tan.z = tmpFloatT[tanIndex2];
            transform.multNormal(tan, tan);
            tmpFloatT[tanIndex] = tan.x;
            tmpFloatT[tanIndex1] = tan.y;
            tmpFloatT[tanIndex2] = tan.z;
            tanIndex += 4;
        }

    }
    vars.release();

    //using bulk put as it's faster
    bufPos.position(offset);
    bufPos.put(tmpFloat, 0, length);

    if (bindBufNorm != null) {
        bufNorm.position(offset);
        bufNorm.put(tmpFloatN, 0, length);
    }

    if (bindBufTangents != null) {
        bufTangents.position(tanOffset);
        bufTangents.put(tmpFloatT, 0, tanLength);
    }
}
 
Example 20
Source File: Matrix4f.java    From aion-germany with GNU General Public License v3.0 3 votes vote down vote up
/**
 * <code>toFloatBuffer</code> returns a FloatBuffer object that contains the matrix data.
 *
 * @param columnMajor
 *            if true, this buffer should be filled with column major data, otherwise it will be filled row major.
 * @return matrix data as a FloatBuffer. The position is set to 0 for convenience.
 */
public FloatBuffer toFloatBuffer(boolean columnMajor) {
	FloatBuffer fb = BufferUtils.createFloatBuffer(16);
	fillFloatBuffer(fb, columnMajor);
	fb.rewind();
	return fb;
}