Search in sources :

Example 41 with FloatBuffer

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

the class BufferUtils method createFloatBuffer.

/**
     * Generate a new FloatBuffer using the given array of Vector4 objects. The
     * FloatBuffer will be 4 * data.length long and contain the vector data.
     *
     * @param data
     *            array of Vector4 objects to place into a new FloatBuffer
     */
public static FloatBuffer createFloatBuffer(Vector4f... data) {
    if (data == null) {
        return null;
    }
    FloatBuffer buff = createFloatBuffer(4 * data.length);
    for (int x = 0; x < data.length; x++) {
        if (data[x] != null) {
            buff.put(data[x].getX()).put(data[x].getY()).put(data[x].getZ()).put(data[x].getW());
        } else {
            buff.put(0).put(0).put(0).put(0);
        }
    }
    buff.flip();
    return buff;
}
Also used : FloatBuffer(java.nio.FloatBuffer)

Example 42 with FloatBuffer

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

the class TestTexture3D method simpleInitApp.

@Override
public void simpleInitApp() {
    //mouseInput.setCursorVisible(true);
    flyCam.setMoveSpeed(10);
    //creating a sphere
    Sphere sphere = new Sphere(32, 32, 1);
    //getting the boundingbox
    sphere.updateBound();
    BoundingBox bb = (BoundingBox) sphere.getBound();
    Vector3f min = bb.getMin(null);
    float[] ext = new float[] { bb.getXExtent() * 2, bb.getYExtent() * 2, bb.getZExtent() * 2 };
    //we need to change the UV coordinates (the sphere is assumet to be inside the 3D image box)
    sphere.clearBuffer(Type.TexCoord);
    VertexBuffer vb = sphere.getBuffer(Type.Position);
    FloatBuffer fb = (FloatBuffer) vb.getData();
    float[] uvCoordinates = BufferUtils.getFloatArray(fb);
    //now transform the coordinates so that they are in the range of <0; 1>
    for (int i = 0; i < uvCoordinates.length; i += 3) {
        uvCoordinates[i] = (uvCoordinates[i] - min.x) / ext[0];
        uvCoordinates[i + 1] = (uvCoordinates[i + 1] - min.y) / ext[1];
        uvCoordinates[i + 2] = (uvCoordinates[i + 2] - min.z) / ext[2];
    }
    //apply new texture coordinates
    VertexBuffer uvCoordsBuffer = new VertexBuffer(Type.TexCoord);
    uvCoordsBuffer.setupData(Usage.Static, 3, com.jme3.scene.VertexBuffer.Format.Float, BufferUtils.createFloatBuffer(uvCoordinates));
    sphere.setBuffer(uvCoordsBuffer);
    //create geometry, and apply material and our 3D texture
    Geometry g = new Geometry("sphere", sphere);
    Material material = new Material(assetManager, "jme3test/texture/tex3D.j3md");
    try {
        Texture texture = this.getTexture();
        material.setTexture("Texture", texture);
    } catch (IOException e) {
        e.printStackTrace();
    }
    g.setMaterial(material);
    rootNode.attachChild(g);
    //add some light so that it is visible
    PointLight light = new PointLight();
    light.setColor(ColorRGBA.White);
    light.setPosition(new Vector3f(5, 5, 5));
    light.setRadius(20);
    rootNode.addLight(light);
    light = new PointLight();
    light.setColor(ColorRGBA.White);
    light.setPosition(new Vector3f(-5, -5, -5));
    light.setRadius(20);
    rootNode.addLight(light);
}
Also used : Sphere(com.jme3.scene.shape.Sphere) Geometry(com.jme3.scene.Geometry) VertexBuffer(com.jme3.scene.VertexBuffer) BoundingBox(com.jme3.bounding.BoundingBox) Vector3f(com.jme3.math.Vector3f) FloatBuffer(java.nio.FloatBuffer) Material(com.jme3.material.Material) IOException(java.io.IOException) PointLight(com.jme3.light.PointLight) Texture(com.jme3.texture.Texture)

Example 43 with FloatBuffer

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

the class ParticlePointMesh method initParticleData.

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

Example 44 with FloatBuffer

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

the class BitmapTextPage method assemble.

// Here is where one might add JmeCloneable related stuff except
// the old clone() method doesn't actually bother to clone anything.
// The arrays and the pageQuads are shared across all BitmapTextPage
// clones and it doesn't seem to bother anything.  That means the
// fields could probably just as well be static... but this code is
// all very fragile.  I'm not tipping that particular boat today. -pspeed
void assemble(Letters quads) {
    pageQuads.clear();
    quads.rewind();
    while (quads.nextCharacter()) {
        if (quads.isPrintable()) {
            if (quads.getCharacterSetPage() == page) {
                pageQuads.add(quads.getQuad());
            }
        }
    }
    Mesh m = getMesh();
    int vertCount = pageQuads.size() * 4;
    int triCount = pageQuads.size() * 2;
    VertexBuffer pb = m.getBuffer(Type.Position);
    VertexBuffer tb = m.getBuffer(Type.TexCoord);
    VertexBuffer ib = m.getBuffer(Type.Index);
    VertexBuffer cb = m.getBuffer(Type.Color);
    FloatBuffer fpb = (FloatBuffer) pb.getData();
    FloatBuffer ftb = (FloatBuffer) tb.getData();
    ShortBuffer sib = (ShortBuffer) ib.getData();
    ByteBuffer bcb = (ByteBuffer) cb.getData();
    // increase capacity of buffers as needed
    fpb.rewind();
    fpb = BufferUtils.ensureLargeEnough(fpb, vertCount * 3);
    fpb.limit(vertCount * 3);
    pb.updateData(fpb);
    ftb.rewind();
    ftb = BufferUtils.ensureLargeEnough(ftb, vertCount * 2);
    ftb.limit(vertCount * 2);
    tb.updateData(ftb);
    bcb.rewind();
    bcb = BufferUtils.ensureLargeEnough(bcb, vertCount * 4);
    bcb.limit(vertCount * 4);
    cb.updateData(bcb);
    sib.rewind();
    sib = BufferUtils.ensureLargeEnough(sib, triCount * 3);
    sib.limit(triCount * 3);
    ib.updateData(sib);
    m.updateCounts();
    // go for each quad and append it to the buffers
    if (pos != null) {
        for (int i = 0; i < pageQuads.size(); i++) {
            LetterQuad fq = pageQuads.get(i);
            fq.storeToArrays(pos, tc, idx, color, i);
            fpb.put(pos);
            ftb.put(tc);
            sib.put(idx);
            bcb.put(color);
        }
    } else {
        for (int i = 0; i < pageQuads.size(); i++) {
            LetterQuad fq = pageQuads.get(i);
            fq.appendPositions(fpb);
            fq.appendTexCoords(ftb);
            fq.appendIndices(sib, i);
            fq.appendColors(bcb);
        }
    }
    fpb.rewind();
    ftb.rewind();
    sib.rewind();
    bcb.rewind();
    updateModelBound();
}
Also used : VertexBuffer(com.jme3.scene.VertexBuffer) Mesh(com.jme3.scene.Mesh) FloatBuffer(java.nio.FloatBuffer) ShortBuffer(java.nio.ShortBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 45 with FloatBuffer

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

the class BoundingSphereDebug method setGeometryData.

/**
     * builds the vertices based on the radius
     */
private void setGeometryData() {
    setMode(Mode.Lines);
    FloatBuffer posBuf = BufferUtils.createVector3Buffer((radialSamples + 1) * 3);
    FloatBuffer colBuf = BufferUtils.createVector3Buffer((radialSamples + 1) * 4);
    setBuffer(Type.Position, 3, posBuf);
    setBuffer(Type.Color, 4, colBuf);
    // generate geometry
    float fInvRS = 1.0f / radialSamples;
    // 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];
    for (int iR = 0; iR <= radialSamples; iR++) {
        posBuf.put(afCos[iR]).put(afSin[iR]).put(0);
        colBuf.put(ColorRGBA.Blue.r).put(ColorRGBA.Blue.g).put(ColorRGBA.Blue.b).put(ColorRGBA.Blue.a);
    }
    for (int iR = 0; iR <= radialSamples; iR++) {
        posBuf.put(afCos[iR]).put(0).put(afSin[iR]);
        colBuf.put(ColorRGBA.Green.r).put(ColorRGBA.Green.g).put(ColorRGBA.Green.b).put(ColorRGBA.Green.a);
    }
    for (int iR = 0; iR <= radialSamples; iR++) {
        posBuf.put(0).put(afCos[iR]).put(afSin[iR]);
        colBuf.put(ColorRGBA.Yellow.r).put(ColorRGBA.Yellow.g).put(ColorRGBA.Yellow.b).put(ColorRGBA.Yellow.a);
    }
    updateBound();
    setStatic();
}
Also used : FloatBuffer(java.nio.FloatBuffer)

Aggregations

FloatBuffer (java.nio.FloatBuffer)291 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