Search in sources :

Example 11 with ShortBuffer

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

the class LODGeomap method createMesh.

public Mesh createMesh(Vector3f scale, Vector2f tcScale, Vector2f tcOffset, float offsetAmount, int totalSize, boolean center, int lod, boolean rightLod, boolean topLod, boolean leftLod, boolean bottomLod) {
    FloatBuffer pb = writeVertexArray(null, scale, center);
    FloatBuffer texb = writeTexCoordArray(null, tcOffset, tcScale, offsetAmount, totalSize);
    FloatBuffer nb = writeNormalArray(null, scale);
    Buffer ib;
    IndexBuffer idxB = writeIndexArrayLodDiff(lod, rightLod, topLod, leftLod, bottomLod, totalSize);
    if (idxB.getBuffer() instanceof IntBuffer)
        ib = (IntBuffer) idxB.getBuffer();
    else
        ib = (ShortBuffer) idxB.getBuffer();
    FloatBuffer bb = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 3);
    FloatBuffer tanb = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 3);
    writeTangentArray(nb, tanb, bb, texb, scale);
    Mesh m = new Mesh();
    m.setMode(Mode.TriangleStrip);
    m.setBuffer(Type.Position, 3, pb);
    m.setBuffer(Type.Normal, 3, nb);
    m.setBuffer(Type.Tangent, 3, tanb);
    m.setBuffer(Type.Binormal, 3, bb);
    m.setBuffer(Type.TexCoord, 2, texb);
    if (ib instanceof IntBuffer)
        m.setBuffer(Type.Index, 3, (IntBuffer) ib);
    else if (ib instanceof ShortBuffer)
        m.setBuffer(Type.Index, 3, (ShortBuffer) ib);
    m.setStatic();
    m.updateBound();
    return m;
}
Also used : FloatBuffer(java.nio.FloatBuffer) ShortBuffer(java.nio.ShortBuffer) IndexBuffer(com.jme3.scene.mesh.IndexBuffer) IntBuffer(java.nio.IntBuffer) Buffer(java.nio.Buffer) IndexBuffer(com.jme3.scene.mesh.IndexBuffer) IntBuffer(java.nio.IntBuffer) Mesh(com.jme3.scene.Mesh) FloatBuffer(java.nio.FloatBuffer) ShortBuffer(java.nio.ShortBuffer)

Example 12 with ShortBuffer

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

the class IrUtils method convertIrMeshToJmeMesh.

/**
     * Convert IrMesh to jME3 mesh.
     */
public static Mesh convertIrMeshToJmeMesh(IrMesh mesh) {
    Map<IrVertex, Integer> vertexToVertexIndex = new HashMap<IrVertex, Integer>();
    List<IrVertex> vertices = new ArrayList<IrVertex>();
    List<Integer> indexes = new ArrayList<Integer>();
    int vertexIndex = 0;
    for (IrPolygon polygon : mesh.polygons) {
        if (polygon.vertices.length != 3) {
            throw new UnsupportedOperationException("IrMesh must be triangulated first");
        }
        for (IrVertex vertex : polygon.vertices) {
            // Is this vertex already indexed?
            Integer existingIndex = vertexToVertexIndex.get(vertex);
            if (existingIndex == null) {
                // Not indexed yet, allocate index.
                indexes.add(vertexIndex);
                vertexToVertexIndex.put(vertex, vertexIndex);
                vertices.add(vertex);
                vertexIndex++;
            } else {
                // Index already allocated for this vertex, reuse it.
                indexes.add(existingIndex);
            }
        }
    }
    Mesh jmeMesh = new Mesh();
    jmeMesh.setMode(Mesh.Mode.Triangles);
    FloatBuffer posBuf = null;
    FloatBuffer normBuf = null;
    FloatBuffer tangBuf = null;
    FloatBuffer uv0Buf = null;
    FloatBuffer uv1Buf = null;
    ByteBuffer colorBuf = null;
    ByteBuffer boneIndices = null;
    FloatBuffer boneWeights = null;
    IndexBuffer indexBuf = null;
    IrVertex inspectionVertex = vertices.get(0);
    if (inspectionVertex.pos != null) {
        posBuf = BufferUtils.createVector3Buffer(vertices.size());
        jmeMesh.setBuffer(VertexBuffer.Type.Position, 3, posBuf);
    }
    if (inspectionVertex.norm != null) {
        normBuf = BufferUtils.createVector3Buffer(vertices.size());
        jmeMesh.setBuffer(VertexBuffer.Type.Normal, 3, normBuf);
    }
    if (inspectionVertex.tang4d != null) {
        tangBuf = BufferUtils.createFloatBuffer(vertices.size() * 4);
        jmeMesh.setBuffer(VertexBuffer.Type.Tangent, 4, tangBuf);
    }
    if (inspectionVertex.tang != null || inspectionVertex.bitang != null) {
        throw new IllegalStateException("Mesh is using 3D tangents, must be converted to 4D tangents first.");
    }
    if (inspectionVertex.uv0 != null) {
        uv0Buf = BufferUtils.createVector2Buffer(vertices.size());
        jmeMesh.setBuffer(VertexBuffer.Type.TexCoord, 2, uv0Buf);
    }
    if (inspectionVertex.uv1 != null) {
        uv1Buf = BufferUtils.createVector2Buffer(vertices.size());
        jmeMesh.setBuffer(VertexBuffer.Type.TexCoord2, 2, uv1Buf);
    }
    if (inspectionVertex.color != null) {
        colorBuf = BufferUtils.createByteBuffer(vertices.size() * 4);
        jmeMesh.setBuffer(VertexBuffer.Type.Color, 4, colorBuf);
        jmeMesh.getBuffer(VertexBuffer.Type.Color).setNormalized(true);
    }
    if (inspectionVertex.boneWeightsIndices != null) {
        boneIndices = BufferUtils.createByteBuffer(vertices.size() * 4);
        boneWeights = BufferUtils.createFloatBuffer(vertices.size() * 4);
        jmeMesh.setBuffer(VertexBuffer.Type.BoneIndex, 4, boneIndices);
        jmeMesh.setBuffer(VertexBuffer.Type.BoneWeight, 4, boneWeights);
        //creating empty buffers for HW skinning 
        //the buffers will be setup if ever used.
        VertexBuffer weightsHW = new VertexBuffer(VertexBuffer.Type.HWBoneWeight);
        VertexBuffer indicesHW = new VertexBuffer(VertexBuffer.Type.HWBoneIndex);
        //setting usage to cpuOnly so that the buffer is not send empty to the GPU
        indicesHW.setUsage(VertexBuffer.Usage.CpuOnly);
        weightsHW.setUsage(VertexBuffer.Usage.CpuOnly);
        jmeMesh.setBuffer(weightsHW);
        jmeMesh.setBuffer(indicesHW);
    }
    if (vertices.size() >= 65536) {
        // too many verticies: use intbuffer instead of shortbuffer
        IntBuffer ib = BufferUtils.createIntBuffer(indexes.size());
        jmeMesh.setBuffer(VertexBuffer.Type.Index, 3, ib);
        indexBuf = new IndexIntBuffer(ib);
    } else {
        ShortBuffer sb = BufferUtils.createShortBuffer(indexes.size());
        jmeMesh.setBuffer(VertexBuffer.Type.Index, 3, sb);
        indexBuf = new IndexShortBuffer(sb);
    }
    jmeMesh.setStatic();
    int maxBonesPerVertex = -1;
    for (IrVertex vertex : vertices) {
        if (posBuf != null) {
            posBuf.put(vertex.pos.x).put(vertex.pos.y).put(vertex.pos.z);
        }
        if (normBuf != null) {
            normBuf.put(vertex.norm.x).put(vertex.norm.y).put(vertex.norm.z);
        }
        if (tangBuf != null) {
            tangBuf.put(vertex.tang4d.x).put(vertex.tang4d.y).put(vertex.tang4d.z).put(vertex.tang4d.w);
        }
        if (uv0Buf != null) {
            uv0Buf.put(vertex.uv0.x).put(vertex.uv0.y);
        }
        if (uv1Buf != null) {
            uv1Buf.put(vertex.uv1.x).put(vertex.uv1.y);
        }
        if (colorBuf != null) {
            colorBuf.putInt(vertex.color.asIntABGR());
        }
        if (boneIndices != null) {
            if (vertex.boneWeightsIndices != null) {
                if (vertex.boneWeightsIndices.length > 4) {
                    throw new UnsupportedOperationException("Mesh uses more than 4 weights per bone. " + "Call trimBoneWeights() to allieviate this");
                }
                for (int i = 0; i < vertex.boneWeightsIndices.length; i++) {
                    boneIndices.put((byte) (vertex.boneWeightsIndices[i].boneIndex & 0xFF));
                    boneWeights.put(vertex.boneWeightsIndices[i].boneWeight);
                }
                for (int i = 0; i < 4 - vertex.boneWeightsIndices.length; i++) {
                    boneIndices.put((byte) 0);
                    boneWeights.put(0f);
                }
            } else {
                boneIndices.putInt(0);
                boneWeights.put(0f).put(0f).put(0f).put(0f);
            }
            maxBonesPerVertex = Math.max(maxBonesPerVertex, vertex.boneWeightsIndices.length);
        }
    }
    for (int i = 0; i < indexes.size(); i++) {
        indexBuf.put(i, indexes.get(i));
    }
    jmeMesh.updateCounts();
    jmeMesh.updateBound();
    if (boneIndices != null) {
        jmeMesh.setMaxNumWeights(maxBonesPerVertex);
        jmeMesh.prepareForAnim(true);
        jmeMesh.generateBindPose(true);
    }
    return jmeMesh;
}
Also used : HashMap(java.util.HashMap) VertexBuffer(com.jme3.scene.VertexBuffer) ArrayList(java.util.ArrayList) Mesh(com.jme3.scene.Mesh) FloatBuffer(java.nio.FloatBuffer) ByteBuffer(java.nio.ByteBuffer) IndexBuffer(com.jme3.scene.mesh.IndexBuffer) IndexShortBuffer(com.jme3.scene.mesh.IndexShortBuffer) IndexIntBuffer(com.jme3.scene.mesh.IndexIntBuffer) IndexIntBuffer(com.jme3.scene.mesh.IndexIntBuffer) IntBuffer(java.nio.IntBuffer) IndexShortBuffer(com.jme3.scene.mesh.IndexShortBuffer) ShortBuffer(java.nio.ShortBuffer)

Example 13 with ShortBuffer

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

the class ObjectHelper method flipMeshIfRequired.

/**
     * The method flips the mesh if the scale is mirroring it. Mirroring scale has either 1 or all 3 factors negative.
     * If two factors are negative then there is no mirroring because a rotation and translation can be found that will
     * lead to the same transform when all scales are positive.
     * 
     * @param geometry
     *            the geometry that is being flipped if necessary
     * @param scale
     *            the scale vector of the given geometry
     */
private void flipMeshIfRequired(Geometry geometry, Vector3f scale) {
    float s = scale.x * scale.y * scale.z;
    if (s < 0 && geometry.getMesh() != null) {
        // negative s means that the scale is mirroring the object
        FloatBuffer normals = geometry.getMesh().getFloatBuffer(Type.Normal);
        if (normals != null) {
            for (int i = 0; i < normals.limit(); i += 3) {
                if (scale.x < 0) {
                    normals.put(i, -normals.get(i));
                }
                if (scale.y < 0) {
                    normals.put(i + 1, -normals.get(i + 1));
                }
                if (scale.z < 0) {
                    normals.put(i + 2, -normals.get(i + 2));
                }
            }
        }
        if (geometry.getMesh().getMode() == Mode.Triangles) {
            // there is no need to flip the indexes for lines and points
            LOGGER.finer("Flipping index order in triangle mesh.");
            Buffer indexBuffer = geometry.getMesh().getBuffer(Type.Index).getData();
            for (int i = 0; i < indexBuffer.limit(); i += 3) {
                if (indexBuffer instanceof ShortBuffer) {
                    short index = ((ShortBuffer) indexBuffer).get(i + 1);
                    ((ShortBuffer) indexBuffer).put(i + 1, ((ShortBuffer) indexBuffer).get(i + 2));
                    ((ShortBuffer) indexBuffer).put(i + 2, index);
                } else {
                    int index = ((IntBuffer) indexBuffer).get(i + 1);
                    ((IntBuffer) indexBuffer).put(i + 1, ((IntBuffer) indexBuffer).get(i + 2));
                    ((IntBuffer) indexBuffer).put(i + 2, index);
                }
            }
        }
    }
}
Also used : FloatBuffer(java.nio.FloatBuffer) ShortBuffer(java.nio.ShortBuffer) IntBuffer(java.nio.IntBuffer) Buffer(java.nio.Buffer) IntBuffer(java.nio.IntBuffer) FloatBuffer(java.nio.FloatBuffer) ShortBuffer(java.nio.ShortBuffer) CullHint(com.jme3.scene.Spatial.CullHint)

Example 14 with ShortBuffer

use of java.nio.ShortBuffer in project jna by java-native-access.

the class BufferArgumentsMarshalTest method testWrappedShortArrayArgument.

public void testWrappedShortArrayArgument() {
    short[] array = new short[1024];
    ShortBuffer buf = ShortBuffer.wrap(array, 512, 512);
    final short MAGIC = (short) 0xABED;
    lib.fillInt16Buffer(buf, 512, MAGIC);
    for (int i = 0; i < array.length; i++) {
        assertEquals("Bad value at index " + i, i < 512 ? 0 : MAGIC, array[i]);
    }
}
Also used : ShortBuffer(java.nio.ShortBuffer)

Example 15 with ShortBuffer

use of java.nio.ShortBuffer in project MinecraftForge by MinecraftForge.

the class CodecIBXM method convertAudioBytes.

/**
 * Converts sound bytes to little-endian format.
 * @param audio_bytes The original wave data
 * @param two_bytes_data For stereo sounds.
 * @return byte array containing the converted data.
 */
//Forge
@SuppressWarnings("unused")
private static byte[] convertAudioBytes(byte[] audio_bytes, boolean two_bytes_data) {
    ByteBuffer dest = ByteBuffer.allocateDirect(audio_bytes.length);
    dest.order(ByteOrder.nativeOrder());
    ByteBuffer src = ByteBuffer.wrap(audio_bytes);
    src.order(ByteOrder.LITTLE_ENDIAN);
    if (two_bytes_data) {
        ShortBuffer dest_short = dest.asShortBuffer();
        ShortBuffer src_short = src.asShortBuffer();
        while (src_short.hasRemaining()) {
            dest_short.put(src_short.get());
        }
    } else {
        while (src.hasRemaining()) {
            dest.put(src.get());
        }
    }
    dest.rewind();
    if (!dest.hasArray()) {
        byte[] arrayBackedBuffer = new byte[dest.capacity()];
        dest.get(arrayBackedBuffer);
        dest.clear();
        return arrayBackedBuffer;
    }
    return dest.array();
}
Also used : ByteBuffer(java.nio.ByteBuffer) 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