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

The following examples show how to use com.jme3.util.BufferUtils#createShortBuffer() . 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: WireSphere.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public WireSphere(float radius) {
        updatePositions(radius);
        ShortBuffer ib = BufferUtils.createShortBuffer(samples * 2 * 2 + zSamples * samples * 2 /*+ 3 * 2*/);
        setBuffer(Type.Index, 2, ib);

//        ib.put(new byte[]{
//            (byte) 0, (byte) 1,
//            (byte) 2, (byte) 3,
//            (byte) 4, (byte) 5,
//        });

//        int curNum = 3 * 2;
        int curNum = 0;
        for (int j = 0; j < 2 + zSamples; j++) {
            for (int i = curNum; i < curNum + samples - 1; i++) {
                ib.put((short) i).put((short) (i + 1));
            }
            ib.put((short) (curNum + samples - 1)).put((short) curNum);
            curNum += samples;
        }

        setMode(Mode.Lines);

        updateBound();
        updateCounts();
    }
 
Example 2
Source File: WireSphere.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public WireSphere(float radius) {
        updatePositions(radius);
        ShortBuffer ib = BufferUtils.createShortBuffer(samples * 2 * 2 + zSamples * samples * 2 /*+ 3 * 2*/);
        setBuffer(Type.Index, 2, ib);

//        ib.put(new byte[]{
//            (byte) 0, (byte) 1,
//            (byte) 2, (byte) 3,
//            (byte) 4, (byte) 5,
//        });

//        int curNum = 3 * 2;
        int curNum = 0;
        for (int j = 0; j < 2 + zSamples; j++) {
            for (int i = curNum; i < curNum + samples - 1; i++) {
                ib.put((short) i).put((short) (i + 1));
            }
            ib.put((short) (curNum + samples - 1)).put((short) curNum);
            curNum += samples;
        }

        setMode(Mode.Lines);

        updateBound();
        updateCounts();
    }
 
Example 3
Source File: BoundingSphereDebug.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * sets the indices for rendering the sphere.
 */
private void setIndexData() {

    // allocate connectivity
    int nbSegments = (radialSamples) * 3;

    ShortBuffer idxBuf = BufferUtils.createShortBuffer(2 * nbSegments);
    setBuffer(Type.Index, 2, idxBuf);

    int idx = 0;
    int segDone = 0;
    while (segDone < nbSegments) {
        idxBuf.put((short) idx);
        idxBuf.put((short) (idx + 1));
        idx++;
        segDone++;
        if (segDone == radialSamples || segDone == radialSamples * 2) {
            idx++;
        }

    }

}
 
Example 4
Source File: PMDSkinData.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public PMDSkinData(DataInputStreamLittleEndian is) throws IOException {
//        PMDSkinVertData skinVertData[];
        skinName = is.readString(20);
//        System.out.println("skinName = "+skinName);
        skinVertCount = is.readInt();
        skinType = is.readByte();
//        System.out.println("skinVertCount = "+skinVertCount);
//        skinVertData = new PMDSkinVertData[skinVertCount];
//        for(int i=0;i<skinVertCount;i++) {
//            skinVertData[i] = new PMDSkinVertData(is);
//        }
        indexBuf = BufferUtils.createShortBuffer(skinVertCount);
        skinBuf = BufferUtils.createFloatBuffer(skinVertCount * 3);
        for(int i=0;i<skinVertCount;i++) {
            indexBuf.put((short)is.readInt());
            skinBuf.put(is.readFloat());
            skinBuf.put(is.readFloat());
            skinBuf.put(-is.readFloat());
        }
    }
 
Example 5
Source File: VertexBuffer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Creates a {@link Buffer} that satisfies the given type and size requirements
 * of the parameters. The buffer will be of the type specified by
 * {@link Format format} and would be able to contain the given number
 * of elements with the given number of components in each element.
 */
public static Buffer createBuffer(Format format, int components, int numElements){
    if (components < 1 || components > 4)
        throw new IllegalArgumentException("Num components must be between 1 and 4");

    int total = numElements * components;

    switch (format){
        case Byte:
        case UnsignedByte:
            return BufferUtils.createByteBuffer(total);
        case Half:
            return BufferUtils.createByteBuffer(total * 2);
        case Short:
        case UnsignedShort:
            return BufferUtils.createShortBuffer(total);
        case Int:
        case UnsignedInt:
            return BufferUtils.createIntBuffer(total);
        case Float:
            return BufferUtils.createFloatBuffer(total);
        case Double:
            return BufferUtils.createDoubleBuffer(total);
        default:
            throw new UnsupportedOperationException("Unrecoginized buffer format: "+format);
    }
}
 
Example 6
Source File: MeshLoader.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void startLodFaceList(String submeshindex, String numfaces) {
    int index = Integer.parseInt(submeshindex);
    mesh = geoms.get(index).getMesh();
    int faceCount = Integer.parseInt(numfaces);

    VertexBuffer originalIndexBuffer = mesh.getBuffer(Type.Index);
    vb = new VertexBuffer(VertexBuffer.Type.Index);
    if (originalIndexBuffer.getFormat() == Format.UnsignedInt) {
        // LOD buffer should also be integer
        ib = BufferUtils.createIntBuffer(faceCount * 3);
        sb = null;
        vb.setupData(Usage.Static, 3, Format.UnsignedInt, ib);
    } else {
        sb = BufferUtils.createShortBuffer(faceCount * 3);
        ib = null;
        vb.setupData(Usage.Static, 3, Format.UnsignedShort, sb);
    }

    List<VertexBuffer> levels = lodLevels.get(index);
    if (levels == null) {
        // Create the LOD levels list
        levels = new ArrayList<VertexBuffer>();

        // Add the first LOD level (always the original index buffer)
        levels.add(originalIndexBuffer);
        lodLevels.put(index, levels);
    }
    levels.add(vb);
}
 
Example 7
Source File: PMDLoaderGLSLSkinning2.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
PMDSkinMesh createSkinMesh(PMDMaterial pmdMaterial) {
        boolean textureFlag = true;
        if (pmdMaterial.getTextureFileName().length() == 0) {
            textureFlag = false;
        }
        PMDSkinMesh mesh = new PMDSkinMesh();
        mesh.setMode(Mesh.Mode.Triangles);
        mesh.setBuffer(skinvb);
        mesh.setSkinvb2(skinvb2);
        mesh.setBuffer(skinnb);
//        mesh.setSkinnb2(skinnb2);
        if (textureFlag) {
            mesh.setBuffer(skintb);
        }
        mesh.setBuffer(skinbib);
        mesh.setBuffer(skinwb);
        VertexBuffer ib = new VertexBuffer(VertexBuffer.Type.Index);
        ShortBuffer isb = meshConverter.getSkinMeshData().indexShortBufferMap.get(pmdMaterial);
        ib.setupData(VertexBuffer.Usage.Static, 1, VertexBuffer.Format.UnsignedShort, isb);
        mesh.setBuffer(ib);
        mesh.setBoneIndexArray(skinIndexArray);
        ShortBuffer boneIndexBuffer = BufferUtils.createShortBuffer(skinIndexArray.length);
        for(int i=0;i<skinIndexArray.length;i++) {
            boneIndexBuffer.put((short)skinIndexArray[i]);
        }
        mesh.setBoneIndexBuffer(boneIndexBuffer);
        mesh.setBoneMatrixArray(new Matrix4f[skinIndexArray.length]);
        for (int i = 0; i < mesh.getBoneMatrixArray().length; i++) {
            mesh.getBoneMatrixArray()[i] = new Matrix4f();
            mesh.getBoneMatrixArray()[i].loadIdentity();
        }
        FloatBuffer boneMatrixBuffer = BufferUtils.createFloatBuffer(skinIndexArray.length * 16);
        mesh.setBoneMatrixBuffer(boneMatrixBuffer);
        return mesh;
    }
 
Example 8
Source File: SkinMeshData.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
void createSkinCommonVertData() {
        skinvfbb = BufferUtils.createByteBuffer(getVertexList().size() * 3 * 4);
        skinvfb = skinvfbb.asFloatBuffer();

        skinvfb2 = BufferUtils.createFloatBuffer(getVertexList().size() * 3);

        skinnfbb = BufferUtils.createByteBuffer(getVertexList().size() * 3 * 4);
        skinnfb = skinnfbb.asFloatBuffer();

        skintfbb = BufferUtils.createByteBuffer(getVertexList().size() * 2 * 4);
        skintfb = skintfbb.asFloatBuffer();

        skinbisbb = BufferUtils.createByteBuffer(getVertexList().size() * 2 * 2);
        skinbisb = skinbisbb.asShortBuffer();

        wfbb = BufferUtils.createByteBuffer(getVertexList().size() * 2 * 4);
        wfb = wfbb.asFloatBuffer();

        for (PMDVertex v : getVertexList()) {
            skinvfb.put(v.getPos().x).put(v.getPos().y).put(v.getPos().z);
            v.getNormal().normalize();
            skinnfb.put(v.getNormal().x).put(v.getNormal().y).put(v.getNormal().z);
//            float f1 = v.getUv().getU();
//            float f2 = v.getUv().getV();
//                tfb.put(v.getUv().getU()).put(1f - v.getUv().getV());
//            f1 = f1 - FastMath.floor(f1);
//            f2 = f2 - FastMath.floor(f2);
//            f2 = 1 - f2;
//            skintfb.put(f1).put(f2);
            skintfb.put(v.getUv().getU()).put(1f - v.getUv().getV());
//            skinbisb.put((short) meshConverter.getSkinMeshData()
//                    .getBoneList().indexOf(v.getBoneNum1()))
//                    .put((short) meshConverter.getSkinMeshData()
//                    .getBoneList().indexOf(v.getBoneNum2()));
            short b1 = (short) getBoneList().indexOf(v.getBoneNum1());
            short b2 = (short) getBoneList().indexOf(v.getBoneNum2());
            if (b1 < 0) {
                b1 = 0;
            }
            if (b2 < 0) {
                b2 = 0;
            }
            skinbisb.put(b1).put(b2);
            float weight = (float) v.getBoneWeight() / 100.0f;
            wfb.put(weight).put(1f - weight);
        }
        skinvfb.position(0);
        skinvfb2.position(0);
        skinvfb2.put(skinvfb);
        skinnfb.position(0);
//        skinnfb2.position(0);
//        skinnfb2.put(skinnfb);
        skinIndexArray = new int[getBoneList().size()];
        for (int i = 0; i < skinIndexArray.length; i++) {
            if (i < getBoneList().size()) {
                skinIndexArray[i] = getBoneList().get(i).shortValue();
            } else {
                skinIndexArray[i] = 0;
            }
        }
        for (PMDMaterial key : indexMap.keySet()) {
            List<Integer> indexList = indexMap.get(key);
            ShortBuffer isb = BufferUtils.createShortBuffer(indexList.size());
            for (Integer index : indexList) {
                isb.put(index.shortValue());
            }
            indexShortBufferMap.put(key, isb);
        }
        indexMap = null;
        boneList = null;
        vertexList = null;
    }
 
Example 9
Source File: VertexBuffer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Reduces the capacity of the buffer to the given amount
 * of elements, any elements at the end of the buffer are truncated
 * as necessary.
 *
 * @param numElements The number of elements to reduce to.
 */
public void compact(int numElements){
    int total = components * numElements;
    data.clear();
    switch (format){
        case Byte:
        case UnsignedByte:
        case Half:
            ByteBuffer bbuf = (ByteBuffer) data;
            bbuf.limit(total);
            ByteBuffer bnewBuf = BufferUtils.createByteBuffer(total);
            bnewBuf.put(bbuf);
            data = bnewBuf;
            break;
        case Short:
        case UnsignedShort:
            ShortBuffer sbuf = (ShortBuffer) data;
            sbuf.limit(total);
            ShortBuffer snewBuf = BufferUtils.createShortBuffer(total);
            snewBuf.put(sbuf);
            data = snewBuf;
            break;
        case Int:
        case UnsignedInt:
            IntBuffer ibuf = (IntBuffer) data;
            ibuf.limit(total);
            IntBuffer inewBuf = BufferUtils.createIntBuffer(total);
            inewBuf.put(ibuf);
            data = inewBuf;
            break;
        case Float:
            FloatBuffer fbuf = (FloatBuffer) data;
            fbuf.limit(total);
            FloatBuffer fnewBuf = BufferUtils.createFloatBuffer(total);
            fnewBuf.put(fbuf);
            data = fnewBuf;
            break;
        default:
            throw new UnsupportedOperationException("Unrecognized buffer format: "+format);
    }
    data.clear();
    setUpdateNeeded();
    dataSizeChanged = true;
}
 
Example 10
Source File: Grid.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Creates a grid debug shape.
 * @param xLines
 * @param yLines
 * @param lineDist 
 */
public Grid(int xLines, int yLines, float lineDist){
    xLines -= 2;
    yLines -= 2;
    int lineCount = xLines + yLines + 4;

    FloatBuffer fpb = BufferUtils.createFloatBuffer(6 * lineCount);
    ShortBuffer sib = BufferUtils.createShortBuffer(2 * lineCount);

    float xLineLen = (yLines + 1) * lineDist;
    float yLineLen = (xLines + 1) * lineDist;
    int curIndex = 0;

    // add lines along X
    for (int i = 0; i < xLines + 2; i++){
        float y = (i) * lineDist;

        // positions
        fpb.put(0)       .put(0).put(y);
        fpb.put(xLineLen).put(0).put(y);

        // indices
        sib.put( (short) (curIndex++) );
        sib.put( (short) (curIndex++) );
    }

    // add lines along Y
    for (int i = 0; i < yLines + 2; i++){
        float x = (i) * lineDist;

        // positions
        fpb.put(x).put(0).put(0);
        fpb.put(x).put(0).put(yLineLen);

        // indices
        sib.put( (short) (curIndex++) );
        sib.put( (short) (curIndex++) );
    }

    fpb.flip();
    sib.flip();

    setBuffer(Type.Position, 3, fpb);
    setBuffer(Type.Index, 2, sib);
    
    setMode(Mode.Lines);

    updateBound();
    updateCounts();
}
 
Example 11
Source File: Sphere.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * sets the indices for rendering the sphere.
 */
private void setIndexData() {
    // allocate connectivity
    triCount = 2 * (zSamples - 2) * radialSamples;
    ShortBuffer idxBuf = BufferUtils.createShortBuffer(3 * triCount);
    setBuffer(Type.Index, 3, idxBuf);

    // generate connectivity
    int index = 0;
    for (int iZ = 0, iZStart = 0; iZ < (zSamples - 3); iZ++) {
        int i0 = iZStart;
        int i1 = i0 + 1;
        iZStart += (radialSamples + 1);
        int i2 = iZStart;
        int i3 = i2 + 1;
        for (int i = 0; i < radialSamples; i++, index += 6) {
            if (!interior) {
                idxBuf.put((short) i0++);
                idxBuf.put((short) i1);
                idxBuf.put((short) i2);
                idxBuf.put((short) i1++);
                idxBuf.put((short) i3++);
                idxBuf.put((short) i2++);
            } else { // inside view
                idxBuf.put((short) i0++);
                idxBuf.put((short) i2);
                idxBuf.put((short) i1);
                idxBuf.put((short) i1++);
                idxBuf.put((short) i2++);
                idxBuf.put((short) i3++);
            }
        }
    }

    // south pole triangles
    for (int i = 0; i < radialSamples; i++, index += 3) {
        if (!interior) {
            idxBuf.put((short) i);
            idxBuf.put((short) (vertCount - 2));
            idxBuf.put((short) (i + 1));
        } else { // inside view
            idxBuf.put((short) i);
            idxBuf.put((short) (i + 1));
            idxBuf.put((short) (vertCount - 2));
        }
    }

    // north pole triangles
    int iOffset = (zSamples - 3) * (radialSamples + 1);
    for (int i = 0; i < radialSamples; i++, index += 3) {
        if (!interior) {
            idxBuf.put((short) (i + iOffset));
            idxBuf.put((short) (i + 1 + iOffset));
            idxBuf.put((short) (vertCount - 1));
        } else { // inside view
            idxBuf.put((short) (i + iOffset));
            idxBuf.put((short) (vertCount - 1));
            idxBuf.put((short) (i + 1 + iOffset));
        }
    }
}
 
Example 12
Source File: MBox.java    From Lemur with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
protected void refreshGeometry() {
    // The nunmber of quads along a side is 1
    // plus the number of "slices"... or splits.
    // A box with 0 slices is just a regular 6 quad box.
    // A box with 1 slice all around has four quads per side, etc.
    //
    // Vertex count is quads + 1
    // Total number of vertexes if all sides are on is:
    // top/bottom = (xSlices + 2) * (zSlices + 2) * 2
    // front/back = (xSlices + 2) * (ySlices + 2) * 2
    // left/right = (zSlices + 2) * (ySlices + 2) * 2

    int xVertCount = slices[0] + 2;
    int yVertCount = slices[1] + 2;
    int zVertCount = slices[2] + 2;
    int xQuadCount = slices[0] + 1;
    int yQuadCount = slices[1] + 1;
    int zQuadCount = slices[2] + 1;

    int upVertCount = xVertCount * zVertCount;
    int frontVertCount = xVertCount * yVertCount;
    int sideVertCount = zVertCount * yVertCount;
    int upTriCount = xQuadCount * zQuadCount * 2;
    int frontTriCount = xQuadCount * yQuadCount * 2;
    int sideTriCount = zQuadCount * yQuadCount * 2;

    int vertCount = 0;
    int triCount = 0;

    if( (sideMask & TOP_MASK) != 0 ) {
        vertCount += upVertCount;
        triCount += upTriCount;
    }
    if( (sideMask & BOTTOM_MASK) != 0 ) {
        vertCount += upVertCount;
        triCount += upTriCount;
    }
    if( (sideMask & FRONT_MASK) != 0 ) {
        vertCount += frontVertCount;
        triCount += frontTriCount;
    }
    if( (sideMask & BACK_MASK) != 0 ) {
        vertCount += frontVertCount;
        triCount += frontTriCount;
    }
    if( (sideMask & LEFT_MASK) != 0 ) {
        vertCount += sideVertCount;
        triCount += sideTriCount;
    }
    if( (sideMask & RIGHT_MASK) != 0 ) {
        vertCount += sideVertCount;
        triCount += sideTriCount;
    }

    FloatBuffer verts = BufferUtils.createFloatBuffer(vertCount * 3);
    FloatBuffer norms = BufferUtils.createFloatBuffer(vertCount * 3);
    FloatBuffer texes = BufferUtils.createFloatBuffer(vertCount * 2);
    ShortBuffer index = BufferUtils.createShortBuffer(triCount * 3);

    int lastIndex = 0;
    if( (sideMask & TOP_MASK) != 0 ) {
        lastIndex = fillSide(lastIndex, TOP, 0, xVertCount, 2, zVertCount, 1,
                             verts, norms, texes, index);
    }
    if( (sideMask & BOTTOM_MASK) != 0 ) {
        lastIndex = fillSide(lastIndex, BOTTOM, 0, xVertCount, 2, zVertCount, 1,
                             verts, norms, texes, index);
    }
    if( (sideMask & FRONT_MASK) != 0 ) {
        lastIndex = fillSide(lastIndex, FRONT, 0, xVertCount, 1, yVertCount, 2,
                             verts, norms, texes, index);
    }
    if( (sideMask & BACK_MASK) != 0 ) {
        lastIndex = fillSide(lastIndex, BACK, 0, xVertCount, 1, yVertCount, 2,
                             verts, norms, texes, index);
    }
    if( (sideMask & LEFT_MASK) != 0 ) {
        lastIndex = fillSide(lastIndex, LEFT, 2, zVertCount, 1, yVertCount, 0,
                              verts, norms, texes, index);
    }
    if( (sideMask & RIGHT_MASK) != 0 ) {
        lastIndex = fillSide(lastIndex, RIGHT, 2, zVertCount, 1, yVertCount, 0,
                             verts, norms, texes, index);
    }

    index.flip();
    norms.flip();
    verts.flip();
    texes.flip();

    setBuffer(Type.Index, 3, index);

    setBuffer(Type.Position, 3, verts);
    setBuffer(Type.TexCoord, 2, texes);

    setBuffer(Type.Normal, 3, norms);

    updateBound();
    clearCollisionData();

}
 
Example 13
Source File: ParticleTriMesh.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@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();
    }
 
Example 14
Source File: Torus.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
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++);
//                }
            }
        }
    }
 
Example 15
Source File: Sphere.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * sets the indices for rendering the sphere.
 */
private void setIndexData() {
    // allocate connectivity
    triCount = 2 * (zSamples - 2) * radialSamples;
    ShortBuffer idxBuf = BufferUtils.createShortBuffer(3 * triCount);
    setBuffer(Type.Index, 3, idxBuf);

    // generate connectivity
    int index = 0;
    for (int iZ = 0, iZStart = 0; iZ < (zSamples - 3); iZ++) {
        int i0 = iZStart;
        int i1 = i0 + 1;
        iZStart += (radialSamples + 1);
        int i2 = iZStart;
        int i3 = i2 + 1;
        for (int i = 0; i < radialSamples; i++, index += 6) {
            if (!interior) {
                idxBuf.put((short) i0++);
                idxBuf.put((short) i1);
                idxBuf.put((short) i2);
                idxBuf.put((short) i1++);
                idxBuf.put((short) i3++);
                idxBuf.put((short) i2++);
            } else { // inside view
                idxBuf.put((short) i0++);
                idxBuf.put((short) i2);
                idxBuf.put((short) i1);
                idxBuf.put((short) i1++);
                idxBuf.put((short) i2++);
                idxBuf.put((short) i3++);
            }
        }
    }

    // south pole triangles
    for (int i = 0; i < radialSamples; i++, index += 3) {
        if (!interior) {
            idxBuf.put((short) i);
            idxBuf.put((short) (vertCount - 2));
            idxBuf.put((short) (i + 1));
        } else { // inside view
            idxBuf.put((short) i);
            idxBuf.put((short) (i + 1));
            idxBuf.put((short) (vertCount - 2));
        }
    }

    // north pole triangles
    int iOffset = (zSamples - 3) * (radialSamples + 1);
    for (int i = 0; i < radialSamples; i++, index += 3) {
        if (!interior) {
            idxBuf.put((short) (i + iOffset));
            idxBuf.put((short) (i + 1 + iOffset));
            idxBuf.put((short) (vertCount - 1));
        } else { // inside view
            idxBuf.put((short) (i + iOffset));
            idxBuf.put((short) (vertCount - 1));
            idxBuf.put((short) (i + 1 + iOffset));
        }
    }
}
 
Example 16
Source File: ParticleTriMesh.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@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);
    VertexBuffer pvb = new VertexBuffer(VertexBuffer.Type.Position);
    pvb.setupData(Usage.Stream, 3, Format.Float, pb);
    
    //if the buffer is already set only update the data
    VertexBuffer buf = getBuffer(VertexBuffer.Type.Position);
    if (buf != null) {
        buf.updateData(pb);
    } else {
        setBuffer(pvb);
    }
    
    // set colors
    ByteBuffer cb = BufferUtils.createByteBuffer(numParticles * 4 * 4);
    VertexBuffer cvb = new VertexBuffer(VertexBuffer.Type.Color);
    cvb.setupData(Usage.Stream, 4, Format.UnsignedByte, cb);
    cvb.setNormalized(true);
    
    buf = getBuffer(VertexBuffer.Type.Color);
    if (buf != null) {
        buf.updateData(cb);
    } else {
        setBuffer(cvb);
    }

    // set texcoords
    VertexBuffer tvb = new VertexBuffer(VertexBuffer.Type.TexCoord);
    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();
    tvb.setupData(Usage.Static, 2, Format.Float, tb);
    
    buf = getBuffer(VertexBuffer.Type.TexCoord);
    if (buf != null) {
        buf.updateData(tb);
    } else {
        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();
    
    VertexBuffer ivb = new VertexBuffer(VertexBuffer.Type.Index);
    ivb.setupData(Usage.Static, 3, Format.UnsignedShort, ib);
    
    buf = getBuffer(VertexBuffer.Type.Index);
    if (buf != null) {
        buf.updateData(ib);
    } else {
        setBuffer(ivb);
    }
    
}
 
Example 17
Source File: VertexBuffer.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Reduces the capacity of the buffer to the given amount
 * of elements, any elements at the end of the buffer are truncated
 * as necessary.
 *
 * @param numElements The number of elements to reduce to.
 */
public void compact(int numElements) {
    int total = components * numElements;
    data.clear();
    switch (format) {
        case Byte:
        case UnsignedByte:
        case Half:
            ByteBuffer bbuf = (ByteBuffer) data;
            bbuf.limit(total);
            ByteBuffer bnewBuf = BufferUtils.createByteBuffer(total);
            bnewBuf.put(bbuf);
            data = bnewBuf;
            break;
        case Short:
        case UnsignedShort:
            ShortBuffer sbuf = (ShortBuffer) data;
            sbuf.limit(total);
            ShortBuffer snewBuf = BufferUtils.createShortBuffer(total);
            snewBuf.put(sbuf);
            data = snewBuf;
            break;
        case Int:
        case UnsignedInt:
            IntBuffer ibuf = (IntBuffer) data;
            ibuf.limit(total);
            IntBuffer inewBuf = BufferUtils.createIntBuffer(total);
            inewBuf.put(ibuf);
            data = inewBuf;
            break;
        case Float:
            FloatBuffer fbuf = (FloatBuffer) data;
            fbuf.limit(total);
            FloatBuffer fnewBuf = BufferUtils.createFloatBuffer(total);
            fnewBuf.put(fbuf);
            data = fnewBuf;
            break;
        default:
            throw new UnsupportedOperationException("Unrecognized buffer format: " + format);
    }
    data.clear();
    setUpdateNeeded();
    dataSizeChanged = true;
}
 
Example 18
Source File: Grid.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Creates a grid debug shape.
 * @param xLines
 * @param yLines
 * @param lineDist 
 */
public Grid(int xLines, int yLines, float lineDist){
    xLines -= 2;
    yLines -= 2;
    int lineCount = xLines + yLines + 4;

    FloatBuffer fpb = BufferUtils.createFloatBuffer(6 * lineCount);
    ShortBuffer sib = BufferUtils.createShortBuffer(2 * lineCount);

    float xLineLen = (yLines + 1) * lineDist;
    float yLineLen = (xLines + 1) * lineDist;
    int curIndex = 0;

    // add lines along X
    for (int i = 0; i < xLines + 2; i++){
        float y = (i) * lineDist;

        // positions
        fpb.put(0)       .put(0).put(y);
        fpb.put(xLineLen).put(0).put(y);

        // indices
        sib.put( (short) (curIndex++) );
        sib.put( (short) (curIndex++) );
    }

    // add lines along Y
    for (int i = 0; i < yLines + 2; i++){
        float x = (i) * lineDist;

        // positions
        fpb.put(x).put(0).put(0);
        fpb.put(x).put(0).put(yLineLen);

        // indices
        sib.put( (short) (curIndex++) );
        sib.put( (short) (curIndex++) );
    }

    fpb.flip();
    sib.flip();

    setBuffer(Type.Position, 3, fpb);
    setBuffer(Type.Index, 2, sib);
    
    setMode(Mode.Lines);

    updateBound();
    updateCounts();
    setStatic();
}
 
Example 19
Source File: IndexBuffer.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Creates an index buffer that can contain the given amount of vertices. 
 * <br/>
 * Returns either {@link IndexByteBuffer}, {@link IndexShortBuffer} or 
 * {@link IndexIntBuffer}
 * 
 * @param vertexCount The amount of vertices to contain
 * @param indexCount The amount of indices to contain
 * @return A new, apropriately sized index buffer
 */
public static IndexBuffer createIndexBuffer(int vertexCount, int indexCount){
    if (vertexCount < 128)
        return new IndexByteBuffer(BufferUtils.createByteBuffer (indexCount));
    else if (vertexCount < 65536)
        return new IndexShortBuffer(BufferUtils.createShortBuffer(indexCount));
    else
        return new IndexIntBuffer(BufferUtils.createIntBuffer(indexCount));
}
 
Example 20
Source File: IndexBuffer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * Creates an index buffer that can contain the given amount
 * of vertices.
 * Returns {@link IndexShortBuffer}
 * 
 * @param vertexCount The amount of vertices to contain
 * @param indexCount The amount of indices
 * to contain.
 * @return A new index buffer
 */
public static IndexBuffer createIndexBuffer(int vertexCount, int indexCount){
    if (vertexCount > 65535){
        return new IndexIntBuffer(BufferUtils.createIntBuffer(indexCount));
    }else{
        return new IndexShortBuffer(BufferUtils.createShortBuffer(indexCount));
    }
}