Java Code Examples for com.jme3.util.BufferUtils#populateFromBuffer()

The following examples show how to use com.jme3.util.BufferUtils#populateFromBuffer() . 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: Pose.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Applies the offsets of this pose to the vertex buffer given by the blend factor.
 *
 * @param blend Blend factor, 0 = no change to vertex buffer, 1 = apply full offsets
 * @param vertbuf Vertex buffer to apply this pose to
 */
public void apply(float blend, FloatBuffer vertbuf){
    for (int i = 0; i < indices.length; i++){
        Vector3f offset = offsets[i];
        int vertIndex   = indices[i];

        tempVec.set(offset).multLocal(blend);

        // acquire vertex
        BufferUtils.populateFromBuffer(tempVec2, vertbuf, vertIndex);

        // add offset multiplied by factor
        tempVec2.addLocal(tempVec);

        // write modified vertex
        BufferUtils.setInBuffer(tempVec2, vertbuf, vertIndex);
    }
}
 
Example 2
Source File: FloatToFixed.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private static void convertNormals(FloatBuffer input, ByteBuffer output){
    if (output.capacity() < input.capacity())
        throw new RuntimeException("Output must be at least as large as input!");

    input.clear();
    output.clear();
    Vector3f temp = new Vector3f();
    int vertexCount = input.capacity() / 3;
    for (int i = 0; i < vertexCount; i++){
        BufferUtils.populateFromBuffer(temp, input, i);

        // offset and scale vector into -128 ... 127
        temp.multLocal(127).addLocal(0.5f, 0.5f, 0.5f);

        // quantize
        byte v1 = (byte) temp.getX();
        byte v2 = (byte) temp.getY();
        byte v3 = (byte) temp.getZ();

        // store
        output.put(v1).put(v2).put(v3);
    }
}
 
Example 3
Source File: Pose.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Applies the offsets of this pose to the vertex buffer given by the blend factor.
 *
 * @param blend Blend factor, 0 = no change to vertex buffer, 1 = apply full offsets
 * @param vertbuf Vertex buffer to apply this pose to
 */
public void apply(float blend, FloatBuffer vertbuf){
    for (int i = 0; i < indices.length; i++){
        Vector3f offset = offsets[i];
        int vertIndex   = indices[i];

        tempVec.set(offset).multLocal(blend);

        // acquire vertex
        BufferUtils.populateFromBuffer(tempVec2, vertbuf, vertIndex);

        // add offset multiplied by factor
        tempVec2.addLocal(tempVec);

        // write modified vertex
        BufferUtils.setInBuffer(tempVec2, vertbuf, vertIndex);
    }
}
 
Example 4
Source File: Mesh.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Gets the triangle vertex positions at the given triangle index 
 * and stores them into the v1, v2, v3 arguments.
 * 
 * @param index The index of the triangle. 
 * Should be between 0 and {@link #getTriangleCount()}.
 * 
 * @param v1 Vector to contain first vertex position
 * @param v2 Vector to contain second vertex position
 * @param v3 Vector to contain third vertex position
 */
public void getTriangle(int index, Vector3f v1, Vector3f v2, Vector3f v3){
    VertexBuffer pb = getBuffer(Type.Position);
    IndexBuffer ib = getIndicesAsList();
    if (pb != null && pb.getFormat() == Format.Float && pb.getNumComponents() == 3){
        FloatBuffer fpb = (FloatBuffer) pb.getData();

        // aquire triangle's vertex indices
        int vertIndex = index * 3;
        int vert1 = ib.get(vertIndex);
        int vert2 = ib.get(vertIndex+1);
        int vert3 = ib.get(vertIndex+2);

        BufferUtils.populateFromBuffer(v1, fpb, vert1);
        BufferUtils.populateFromBuffer(v2, fpb, vert2);
        BufferUtils.populateFromBuffer(v3, fpb, vert3);
    }else{
        throw new UnsupportedOperationException("Position buffer not set or "
                                              + " has incompatible format");
    }
}
 
Example 5
Source File: EntropyComputeUtil.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static float computeLodEntropy(Mesh terrainBlock, Buffer lodIndices){
    // Bounding box for the terrain block
    BoundingBox bbox = (BoundingBox) terrainBlock.getBound();

    // Vertex positions for the block
    FloatBuffer positions = terrainBlock.getFloatBuffer(Type.Position);

    // Prepare to cast rays
    Vector3f pos = new Vector3f();
    Vector3f dir = new Vector3f(0, -1, 0);
    Ray ray = new Ray(pos, dir);

    // Prepare collision results
    CollisionResults results = new CollisionResults();

    // Set the LOD indices on the block
    VertexBuffer originalIndices = terrainBlock.getBuffer(Type.Index);

    terrainBlock.clearBuffer(Type.Index);
    if (lodIndices instanceof IntBuffer)
        terrainBlock.setBuffer(Type.Index, 3, (IntBuffer)lodIndices);
    else if (lodIndices instanceof ShortBuffer) {
        terrainBlock.setBuffer(Type.Index, 3, (ShortBuffer) lodIndices);
    } else {
        terrainBlock.setBuffer(Type.Index, 3, (ByteBuffer) lodIndices);
    }

    // Recalculate collision mesh
    terrainBlock.createCollisionData();

    float entropy = 0;
    for (int i = 0; i < positions.limit() / 3; i++){
        BufferUtils.populateFromBuffer(pos, positions, i);

        float realHeight = pos.y;

        pos.addLocal(0, bbox.getYExtent(), 0);
        ray.setOrigin(pos);

        results.clear();
        terrainBlock.collideWith(ray, Matrix4f.IDENTITY, bbox, results);

        if (results.size() > 0){
            Vector3f contactPoint = results.getClosestCollision().getContactPoint();
            float delta = Math.abs(realHeight - contactPoint.y);
            entropy = Math.max(delta, entropy);
        }
    }

    // Restore original indices
    terrainBlock.clearBuffer(Type.Index);
    terrainBlock.setBuffer(originalIndices);

    return entropy;
}
 
Example 6
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 7
Source File: BoundingSphere.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Used from calcWelzl. This function recurses to calculate a minimum
 * bounding sphere a few points at a time.
 *
 * @param points
 *            The array of points to look through.
 * @param p
 *            The size of the list to be used.
 * @param b
 *            The number of points currently considering to include with the
 *            sphere.
 * @param ap
 *            A variable simulating pointer arithmatic from C++, and offset
 *            in <code>points</code>.
 */
private void recurseMini(FloatBuffer points, int p, int b, int ap) {
    //TempVars vars = TempVars.get();

    Vector3f tempA = new Vector3f(); //vars.vect1;
    Vector3f tempB = new Vector3f(); //vars.vect2;
    Vector3f tempC = new Vector3f(); //vars.vect3;
    Vector3f tempD = new Vector3f(); //vars.vect4;

    switch (b) {
        case 0:
            this.radius = 0;
            this.center.set(0, 0, 0);
            break;
        case 1:
            this.radius = 1f - RADIUS_EPSILON;
            BufferUtils.populateFromBuffer(center, points, ap - 1);
            break;
        case 2:
            BufferUtils.populateFromBuffer(tempA, points, ap - 1);
            BufferUtils.populateFromBuffer(tempB, points, ap - 2);
            setSphere(tempA, tempB);
            break;
        case 3:
            BufferUtils.populateFromBuffer(tempA, points, ap - 1);
            BufferUtils.populateFromBuffer(tempB, points, ap - 2);
            BufferUtils.populateFromBuffer(tempC, points, ap - 3);
            setSphere(tempA, tempB, tempC);
            break;
        case 4:
            BufferUtils.populateFromBuffer(tempA, points, ap - 1);
            BufferUtils.populateFromBuffer(tempB, points, ap - 2);
            BufferUtils.populateFromBuffer(tempC, points, ap - 3);
            BufferUtils.populateFromBuffer(tempD, points, ap - 4);
            setSphere(tempA, tempB, tempC, tempD);
            //vars.release();
            return;
    }
    for (int i = 0; i < p; i++) {
        BufferUtils.populateFromBuffer(tempA, points, i + ap);
        if (tempA.distanceSquared(center) - (radius * radius) > RADIUS_EPSILON - 1f) {
            for (int j = i; j > 0; j--) {
                BufferUtils.populateFromBuffer(tempB, points, j + ap);
                BufferUtils.populateFromBuffer(tempC, points, j - 1 + ap);
                BufferUtils.setInBuffer(tempC, points, j + ap);
                BufferUtils.setInBuffer(tempB, points, j - 1 + ap);
            }
            recurseMini(points, i, b + 1, ap + 1);
        }
    }
    //vars.release();
}
 
Example 8
Source File: SilentTangentBinormalGenerator.java    From OpenRTS with MIT License 4 votes vote down vote up
private static List<VertexData> processTriangleStrip(Mesh mesh, int[] index, Vector3f[] v, Vector2f[] t) {
	IndexBuffer indexBuffer = mesh.getIndexBuffer();
	FloatBuffer vertexBuffer = (FloatBuffer) mesh.getBuffer(Type.Position).getData();
	FloatBuffer textureBuffer = (FloatBuffer) mesh.getBuffer(Type.TexCoord).getData();

	List<VertexData> vertices = initVertexData(vertexBuffer.limit() / 3);

	index[0] = indexBuffer.get(0);
	index[1] = indexBuffer.get(1);

	populateFromBuffer(v[0], vertexBuffer, index[0]);
	populateFromBuffer(v[1], vertexBuffer, index[1]);

	populateFromBuffer(t[0], textureBuffer, index[0]);
	populateFromBuffer(t[1], textureBuffer, index[1]);

	for (int i = 2; i < indexBuffer.size(); i++) {
		index[2] = indexBuffer.get(i);
		BufferUtils.populateFromBuffer(v[2], vertexBuffer, index[2]);
		BufferUtils.populateFromBuffer(t[2], textureBuffer, index[2]);

		boolean isDegenerate = isDegenerateTriangle(v[0], v[1], v[2]);
		TriangleData triData = processTriangle(index, v, t);

		if (triData != null && !isDegenerate) {
			vertices.get(index[0]).triangles.add(triData);
			vertices.get(index[1]).triangles.add(triData);
			vertices.get(index[2]).triangles.add(triData);
		}

		Vector3f vTemp = v[0];
		v[0] = v[1];
		v[1] = v[2];
		v[2] = vTemp;

		Vector2f tTemp = t[0];
		t[0] = t[1];
		t[1] = t[2];
		t[2] = tTemp;

		index[0] = index[1];
		index[1] = index[2];
	}

	return vertices;
}
 
Example 9
Source File: EntropyComputeUtil.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static float computeLodEntropy(Mesh terrainBlock, Buffer lodIndices){
    // Bounding box for the terrain block
    BoundingBox bbox = (BoundingBox) terrainBlock.getBound();

    // Vertex positions for the block
    FloatBuffer positions = terrainBlock.getFloatBuffer(Type.Position);

    // Prepare to cast rays
    Vector3f pos = new Vector3f();
    Vector3f dir = new Vector3f(0, -1, 0);
    Ray ray = new Ray(pos, dir);

    // Prepare collision results
    CollisionResults results = new CollisionResults();

    // Set the LOD indices on the block
    VertexBuffer originalIndices = terrainBlock.getBuffer(Type.Index);

    terrainBlock.clearBuffer(Type.Index);
    if (lodIndices instanceof IntBuffer)
        terrainBlock.setBuffer(Type.Index, 3, (IntBuffer)lodIndices);
    else if (lodIndices instanceof ShortBuffer) {
        terrainBlock.setBuffer(Type.Index, 3, (ShortBuffer) lodIndices);
    }

    // Recalculate collision mesh
    terrainBlock.createCollisionData();

    float entropy = 0;
    for (int i = 0; i < positions.limit() / 3; i++){
        BufferUtils.populateFromBuffer(pos, positions, i);

        float realHeight = pos.y;

        pos.addLocal(0, bbox.getYExtent(), 0);
        ray.setOrigin(pos);

        results.clear();
        terrainBlock.collideWith(ray, Matrix4f.IDENTITY, bbox, results);

        if (results.size() > 0){
            Vector3f contactPoint = results.getClosestCollision().getContactPoint();
            float delta = Math.abs(realHeight - contactPoint.y);
            entropy = Math.max(delta, entropy);
        }
    }

    // Restore original indices
    terrainBlock.clearBuffer(Type.Index);
    terrainBlock.setBuffer(originalIndices);

    return entropy;
}
 
Example 10
Source File: Line.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
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 / (float) 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 11
Source File: BoundingSphere.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Used from calcWelzl. This function recurses to calculate a minimum
 * bounding sphere a few points at a time.
 *
 * @param points
 *            The array of points to look through.
 * @param p
 *            The size of the list to be used.
 * @param b
 *            The number of points currently considering to include with the
 *            sphere.
 * @param ap
 *            A variable simulating pointer arithmatic from C++, and offset
 *            in <code>points</code>.
 */
private void recurseMini(FloatBuffer points, int p, int b, int ap) {
    //TempVars vars = TempVars.get();

    Vector3f tempA = new Vector3f(); //vars.vect1;
    Vector3f tempB = new Vector3f(); //vars.vect2;
    Vector3f tempC = new Vector3f(); //vars.vect3;
    Vector3f tempD = new Vector3f(); //vars.vect4;

    switch (b) {
        case 0:
            this.radius = 0;
            this.center.set(0, 0, 0);
            break;
        case 1:
            this.radius = 1f - RADIUS_EPSILON;
            BufferUtils.populateFromBuffer(center, points, ap - 1);
            break;
        case 2:
            BufferUtils.populateFromBuffer(tempA, points, ap - 1);
            BufferUtils.populateFromBuffer(tempB, points, ap - 2);
            setSphere(tempA, tempB);
            break;
        case 3:
            BufferUtils.populateFromBuffer(tempA, points, ap - 1);
            BufferUtils.populateFromBuffer(tempB, points, ap - 2);
            BufferUtils.populateFromBuffer(tempC, points, ap - 3);
            setSphere(tempA, tempB, tempC);
            break;
        case 4:
            BufferUtils.populateFromBuffer(tempA, points, ap - 1);
            BufferUtils.populateFromBuffer(tempB, points, ap - 2);
            BufferUtils.populateFromBuffer(tempC, points, ap - 3);
            BufferUtils.populateFromBuffer(tempD, points, ap - 4);
            setSphere(tempA, tempB, tempC, tempD);
            //vars.release();
            return;
    }
    for (int i = 0; i < p; i++) {
        BufferUtils.populateFromBuffer(tempA, points, i + ap);
        if (tempA.distanceSquared(center) - (radius * radius) > RADIUS_EPSILON - 1f) {
            for (int j = i; j > 0; j--) {
                BufferUtils.populateFromBuffer(tempB, points, j + ap);
                BufferUtils.populateFromBuffer(tempC, points, j - 1 + ap);
                BufferUtils.setInBuffer(tempC, points, j + ap);
                BufferUtils.setInBuffer(tempB, points, j - 1 + ap);
            }
            recurseMini(points, i, b + 1, ap + 1);
        }
    }
    //vars.release();
}