Search in sources :

Example 26 with FloatBuffer

use of java.nio.FloatBuffer in project jmonkeyengine by jMonkeyEngine.

the class Sphere method setGeometryData.

/**
     * builds the vertices based on the radius, radial and zSamples.
     */
private void setGeometryData() {
    // allocate vertices
    vertCount = (zSamples - 2) * (radialSamples + 1) + 2;
    FloatBuffer posBuf = BufferUtils.createVector3Buffer(vertCount);
    // allocate normals if requested
    FloatBuffer normBuf = BufferUtils.createVector3Buffer(vertCount);
    // allocate texture coordinates
    FloatBuffer texBuf = BufferUtils.createVector2Buffer(vertCount);
    setBuffer(Type.Position, 3, posBuf);
    setBuffer(Type.Normal, 3, normBuf);
    setBuffer(Type.TexCoord, 2, texBuf);
    // generate geometry
    float fInvRS = 1.0f / radialSamples;
    float fZFactor = 2.0f / (zSamples - 1);
    // Generate points on the unit circle to be used in computing the mesh
    // points on a sphere slice.
    float[] afSin = new float[(radialSamples + 1)];
    float[] afCos = new float[(radialSamples + 1)];
    for (int iR = 0; iR < radialSamples; iR++) {
        float fAngle = FastMath.TWO_PI * fInvRS * iR;
        afCos[iR] = FastMath.cos(fAngle);
        afSin[iR] = FastMath.sin(fAngle);
    }
    afSin[radialSamples] = afSin[0];
    afCos[radialSamples] = afCos[0];
    TempVars vars = TempVars.get();
    Vector3f tempVa = vars.vect1;
    Vector3f tempVb = vars.vect2;
    Vector3f tempVc = vars.vect3;
    // generate the sphere itself
    int i = 0;
    for (int iZ = 1; iZ < (zSamples - 1); iZ++) {
        // in (-pi/2, pi/2)
        float fAFraction = FastMath.HALF_PI * (-1.0f + fZFactor * iZ);
        float fZFraction;
        if (useEvenSlices) {
            // in (-1, 1)
            fZFraction = -1.0f + fZFactor * iZ;
        } else {
            // in (-1,1)
            fZFraction = FastMath.sin(fAFraction);
        }
        float fZ = radius * fZFraction;
        // compute center of slice
        Vector3f kSliceCenter = tempVb.set(Vector3f.ZERO);
        kSliceCenter.z += fZ;
        // compute radius of slice
        float fSliceRadius = FastMath.sqrt(FastMath.abs(radius * radius - fZ * fZ));
        // compute slice vertices with duplication at end point
        Vector3f kNormal;
        int iSave = i;
        for (int iR = 0; iR < radialSamples; iR++) {
            // in [0,1)
            float fRadialFraction = iR * fInvRS;
            Vector3f kRadial = tempVc.set(afCos[iR], afSin[iR], 0);
            kRadial.mult(fSliceRadius, tempVa);
            posBuf.put(kSliceCenter.x + tempVa.x).put(kSliceCenter.y + tempVa.y).put(kSliceCenter.z + tempVa.z);
            BufferUtils.populateFromBuffer(tempVa, posBuf, i);
            kNormal = tempVa;
            kNormal.normalizeLocal();
            if (// allow interior texture vs. exterior
            !interior) {
                normBuf.put(kNormal.x).put(kNormal.y).put(kNormal.z);
            } else {
                normBuf.put(-kNormal.x).put(-kNormal.y).put(-kNormal.z);
            }
            if (textureMode == TextureMode.Original) {
                texBuf.put(fRadialFraction).put(0.5f * (fZFraction + 1.0f));
            } else if (textureMode == TextureMode.Projected) {
                texBuf.put(fRadialFraction).put(FastMath.INV_PI * (FastMath.HALF_PI + FastMath.asin(fZFraction)));
            } else if (textureMode == TextureMode.Polar) {
                float r = (FastMath.HALF_PI - FastMath.abs(fAFraction)) / FastMath.PI;
                float u = r * afCos[iR] + 0.5f;
                float v = r * afSin[iR] + 0.5f;
                texBuf.put(u).put(v);
            }
            i++;
        }
        BufferUtils.copyInternalVector3(posBuf, iSave, i);
        BufferUtils.copyInternalVector3(normBuf, iSave, i);
        if (textureMode == TextureMode.Original) {
            texBuf.put(1.0f).put(0.5f * (fZFraction + 1.0f));
        } else if (textureMode == TextureMode.Projected) {
            texBuf.put(1.0f).put(FastMath.INV_PI * (FastMath.HALF_PI + FastMath.asin(fZFraction)));
        } else if (textureMode == TextureMode.Polar) {
            float r = (FastMath.HALF_PI - FastMath.abs(fAFraction)) / FastMath.PI;
            texBuf.put(r + 0.5f).put(0.5f);
        }
        i++;
    }
    vars.release();
    // south pole
    posBuf.position(i * 3);
    posBuf.put(0f).put(0f).put(-radius);
    normBuf.position(i * 3);
    if (!interior) {
        // allow for inner
        normBuf.put(0).put(0).put(-1);
    } else // texture orientation
    // later.
    {
        normBuf.put(0).put(0).put(1);
    }
    texBuf.position(i * 2);
    if (textureMode == TextureMode.Polar) {
        texBuf.put(0.5f).put(0.5f);
    } else {
        texBuf.put(0.5f).put(0.0f);
    }
    i++;
    // north pole
    posBuf.put(0).put(0).put(radius);
    if (!interior) {
        normBuf.put(0).put(0).put(1);
    } else {
        normBuf.put(0).put(0).put(-1);
    }
    if (textureMode == TextureMode.Polar) {
        texBuf.put(0.5f).put(0.5f);
    } else {
        texBuf.put(0.5f).put(1.0f);
    }
    updateBound();
}
Also used : Vector3f(com.jme3.math.Vector3f) FloatBuffer(java.nio.FloatBuffer) TempVars(com.jme3.util.TempVars)

Example 27 with FloatBuffer

use of java.nio.FloatBuffer in project jmonkeyengine by jMonkeyEngine.

the class StripBox method duUpdateGeometryVertices.

protected void duUpdateGeometryVertices() {
    FloatBuffer fpb = BufferUtils.createVector3Buffer(8 * 3);
    Vector3f[] v = computeVertices();
    fpb.put(new float[] { v[0].x, v[0].y, v[0].z, v[1].x, v[1].y, v[1].z, v[2].x, v[2].y, v[2].z, v[3].x, v[3].y, v[3].z, v[4].x, v[4].y, v[4].z, v[5].x, v[5].y, v[5].z, v[6].x, v[6].y, v[6].z, v[7].x, v[7].y, v[7].z });
    setBuffer(Type.Position, 3, fpb);
    setMode(Mode.TriangleStrip);
    updateBound();
}
Also used : Vector3f(com.jme3.math.Vector3f) FloatBuffer(java.nio.FloatBuffer)

Example 28 with FloatBuffer

use of java.nio.FloatBuffer in project jmonkeyengine by jMonkeyEngine.

the class Arrow method setArrowExtent.

/**
     * Sets the arrow's extent.
     * This will modify the buffers on the mesh.
     * 
     * @param extent the arrow's extent.
     */
public void setArrowExtent(Vector3f extent) {
    float len = extent.length();
    //        Vector3f dir = extent.normalize();
    tempQuat.lookAt(extent, Vector3f.UNIT_Y);
    tempQuat.normalizeLocal();
    VertexBuffer pvb = getBuffer(Type.Position);
    FloatBuffer buffer = (FloatBuffer) pvb.getData();
    buffer.rewind();
    for (int i = 0; i < positions.length; i += 3) {
        Vector3f vec = tempVec.set(positions[i], positions[i + 1], positions[i + 2]);
        vec.multLocal(len);
        tempQuat.mult(vec, vec);
        buffer.put(vec.x);
        buffer.put(vec.y);
        buffer.put(vec.z);
    }
    pvb.updateData(buffer);
    updateBound();
    updateCounts();
}
Also used : VertexBuffer(com.jme3.scene.VertexBuffer) Vector3f(com.jme3.math.Vector3f) FloatBuffer(java.nio.FloatBuffer)

Example 29 with FloatBuffer

use of java.nio.FloatBuffer in project jmonkeyengine by jMonkeyEngine.

the class WireFrustum method update.

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();
}
Also used : VertexBuffer(com.jme3.scene.VertexBuffer) FloatBuffer(java.nio.FloatBuffer)

Example 30 with FloatBuffer

use of java.nio.FloatBuffer in project jmonkeyengine by jMonkeyEngine.

the class WireSphere method updatePositions.

public void updatePositions(float radius) {
    VertexBuffer pvb = getBuffer(Type.Position);
    FloatBuffer pb;
    if (pvb == null) {
        pvb = new VertexBuffer(Type.Position);
        pb = BufferUtils.createVector3Buffer(samples * 2 + samples * zSamples);
        pvb.setupData(Usage.Dynamic, 3, Format.Float, pb);
        setBuffer(pvb);
    } else {
        pb = (FloatBuffer) pvb.getData();
    }
    pb.rewind();
    // X axis
    //        pb.put(radius).put(0).put(0);
    //        pb.put(-radius).put(0).put(0);
    //
    //        // Y axis
    //        pb.put(0).put(radius).put(0);
    //        pb.put(0).put(-radius).put(0);
    //
    //        // Z axis
    //        pb.put(0).put(0).put(radius);
    //        pb.put(0).put(0).put(-radius);
    float rate = FastMath.TWO_PI / (float) samples;
    float angle = 0;
    for (int i = 0; i < samples; i++) {
        float x = radius * FastMath.cos(angle);
        float y = radius * FastMath.sin(angle);
        pb.put(x).put(y).put(0);
        angle += rate;
    }
    angle = 0;
    for (int i = 0; i < samples; i++) {
        float x = radius * FastMath.cos(angle);
        float y = radius * FastMath.sin(angle);
        pb.put(0).put(x).put(y);
        angle += rate;
    }
    float zRate = (radius * 2) / (float) (zSamples);
    float zHeight = -radius + (zRate / 2f);
    float rb = 1f / zSamples;
    float b = rb / 2f;
    for (int k = 0; k < zSamples; k++) {
        angle = 0;
        float scale = FastMath.sin(b * FastMath.PI);
        for (int i = 0; i < samples; i++) {
            float x = radius * FastMath.cos(angle);
            float y = radius * FastMath.sin(angle);
            pb.put(x * scale).put(zHeight).put(y * scale);
            angle += rate;
        }
        zHeight += zRate;
        b += rb;
    }
}
Also used : VertexBuffer(com.jme3.scene.VertexBuffer) FloatBuffer(java.nio.FloatBuffer)

Aggregations

FloatBuffer (java.nio.FloatBuffer)287 ByteBuffer (java.nio.ByteBuffer)82 IntBuffer (java.nio.IntBuffer)43 ShortBuffer (java.nio.ShortBuffer)39 Vector3f (com.jme3.math.Vector3f)27 VertexBuffer (com.jme3.scene.VertexBuffer)27 DoubleBuffer (java.nio.DoubleBuffer)17 IndexBuffer (com.jme3.scene.mesh.IndexBuffer)16 LongBuffer (java.nio.LongBuffer)10 Mesh (com.jme3.scene.Mesh)9 CharBuffer (java.nio.CharBuffer)9 FrameBuffer2D (androidx.media.filterfw.FrameBuffer2D)8 OutputPort (androidx.media.filterfw.OutputPort)7 Matrix4f (com.jme3.math.Matrix4f)7 Buffer (java.nio.Buffer)7 TempVars (com.jme3.util.TempVars)6 IOException (java.io.IOException)6 BufferOverflowException (java.nio.BufferOverflowException)6 BufferUnderflowException (java.nio.BufferUnderflowException)6 ArrayList (java.util.ArrayList)6