Search in sources :

Example 6 with ShortBuffer

use of java.nio.ShortBuffer 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 7 with ShortBuffer

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

the class ParticleTriMesh method initParticleData.

//    private Particle[] particlesCopy;
@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();
}
Also used : VertexBuffer(com.jme3.scene.VertexBuffer) FloatBuffer(java.nio.FloatBuffer) ByteBuffer(java.nio.ByteBuffer) ShortBuffer(java.nio.ShortBuffer)

Example 8 with ShortBuffer

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

the class Torus method setIndexData.

private void setIndexData() {
    // allocate connectivity
    int triCount = 2 * circleSamples * radialSamples;
    ShortBuffer sib = BufferUtils.createShortBuffer(3 * triCount);
    setBuffer(Type.Index, 3, sib);
    int i;
    // generate connectivity
    int connectionStart = 0;
    int index = 0;
    for (int circleCount = 0; circleCount < circleSamples; circleCount++) {
        int i0 = connectionStart;
        int i1 = i0 + 1;
        connectionStart += radialSamples + 1;
        int i2 = connectionStart;
        int i3 = i2 + 1;
        for (i = 0; i < radialSamples; i++, index += 6) {
            //                if (true) {
            sib.put((short) i0++);
            sib.put((short) i2);
            sib.put((short) i1);
            sib.put((short) i1++);
            sib.put((short) i2++);
            sib.put((short) i3++);
        //                    getIndexBuffer().put(i0++);
        //                    getIndexBuffer().put(i2);
        //                    getIndexBuffer().put(i1);
        //                    getIndexBuffer().put(i1++);
        //                    getIndexBuffer().put(i2++);
        //                    getIndexBuffer().put(i3++);
        //                } else {
        //                    getIndexBuffer().put(i0++);
        //                    getIndexBuffer().put(i1);
        //                    getIndexBuffer().put(i2);
        //                    getIndexBuffer().put(i1++);
        //                    getIndexBuffer().put(i3++);
        //                    getIndexBuffer().put(i2++);
        //                }
        }
    }
}
Also used : ShortBuffer(java.nio.ShortBuffer)

Example 9 with ShortBuffer

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

the class TerrainPatch method generateLodEntropies.

/**
     * This calculation is slow, so don't use it often.
     */
public void generateLodEntropies() {
    float[] entropies = new float[getMaxLod() + 1];
    for (int i = 0; i <= getMaxLod(); i++) {
        int curLod = (int) Math.pow(2, i);
        IndexBuffer idxB = geomap.writeIndexArrayLodDiff(curLod, false, false, false, false, totalSize);
        Buffer ib;
        if (idxB.getBuffer() instanceof IntBuffer)
            ib = (IntBuffer) idxB.getBuffer();
        else
            ib = (ShortBuffer) idxB.getBuffer();
        entropies[i] = EntropyComputeUtil.computeLodEntropy(mesh, ib);
    }
    lodEntropy = entropies;
}
Also used : FloatBuffer(java.nio.FloatBuffer) ShortBuffer(java.nio.ShortBuffer) IndexBuffer(com.jme3.scene.mesh.IndexBuffer) IntBuffer(java.nio.IntBuffer) Buffer(java.nio.Buffer) VertexBuffer(com.jme3.scene.VertexBuffer) IndexBuffer(com.jme3.scene.mesh.IndexBuffer) IntBuffer(java.nio.IntBuffer) ShortBuffer(java.nio.ShortBuffer)

Example 10 with ShortBuffer

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

the class EntropyComputeUtil method computeLodEntropy.

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;
}
Also used : CollisionResults(com.jme3.collision.CollisionResults) VertexBuffer(com.jme3.scene.VertexBuffer) BoundingBox(com.jme3.bounding.BoundingBox) Vector3f(com.jme3.math.Vector3f) IntBuffer(java.nio.IntBuffer) FloatBuffer(java.nio.FloatBuffer) Ray(com.jme3.math.Ray) ShortBuffer(java.nio.ShortBuffer)

Aggregations

ShortBuffer (java.nio.ShortBuffer)227 ByteBuffer (java.nio.ByteBuffer)78 FloatBuffer (java.nio.FloatBuffer)54 IntBuffer (java.nio.IntBuffer)45 DoubleBuffer (java.nio.DoubleBuffer)23 LongBuffer (java.nio.LongBuffer)16 Test (org.junit.Test)14 Buffer (java.nio.Buffer)11 BufferOverflowException (java.nio.BufferOverflowException)11 CharBuffer (java.nio.CharBuffer)11 VertexBuffer (com.jme3.scene.VertexBuffer)8 BufferUnderflowException (java.nio.BufferUnderflowException)7 BytePointer (org.bytedeco.javacpp.BytePointer)7 IndexBuffer (com.jme3.scene.mesh.IndexBuffer)6 IOException (java.io.IOException)5 Vector3f (com.jme3.math.Vector3f)4 ArrayList (java.util.ArrayList)4 Bitmap (android.graphics.Bitmap)3 Mesh (com.jme3.scene.Mesh)3 InvalidMarkException (java.nio.InvalidMarkException)3