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

The following examples show how to use com.jme3.util.BufferUtils#createFloatBuffer() . 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: SkeletonInterBoneWire.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Creates buffers for points. Each line has POINT_AMOUNT of points.
 * @param skeleton
 *            the skeleton that will be showed
 * @param boneLengths
 *            the lengths of the bones
 */
public SkeletonInterBoneWire(Skeleton skeleton, Map<Integer, Float> boneLengths) {
    this.skeleton = skeleton;

    for (Bone bone : skeleton.getRoots()) {
        this.countConnections(bone);
    }

    this.setMode(Mode.Points);
    this.boneLengths = boneLengths;

    VertexBuffer pb = new VertexBuffer(Type.Position);
    FloatBuffer fpb = BufferUtils.createFloatBuffer(POINT_AMOUNT * connectionsAmount * 3);
    pb.setupData(Usage.Stream, 3, Format.Float, fpb);
    this.setBuffer(pb);

    this.updateCounts();
}
 
Example 2
Source File: DebugShapeFactory.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 *  Retrieves the vertices from the Triangle buffer.
 */
public FloatBuffer getVertices() {
    // There are 3 floats needed for each vertex (x,y,z)
    final int numberOfFloats = vertices.size() * 3;
    FloatBuffer verticesBuffer = BufferUtils.createFloatBuffer(numberOfFloats); 

    // Force the limit, set the cap - most number of floats we will use the buffer for
    verticesBuffer.limit(numberOfFloats);

    // Copy the values from the list to the direct float buffer
    for (Vector3f v : vertices) {
        verticesBuffer.put(v.x).put(v.y).put(v.z);
    }

    vertices.clear();
    return verticesBuffer;
}
 
Example 3
Source File: LODGeomap.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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);
    IndexBuffer ib = writeIndexArrayLodDiff(lod, rightLod, topLod, leftLod, bottomLod, totalSize);
    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);
    switch (ib.getFormat()) {
        case UnsignedInt:
            m.setBuffer(Type.Index, 3, (IntBuffer) ib.getBuffer());
            break;
        case UnsignedShort:
            m.setBuffer(Type.Index, 3, (ShortBuffer) ib.getBuffer());
            break;
        case UnsignedByte:
            m.setBuffer(Type.Index, 3, (ByteBuffer) ib.getBuffer());
            break;
    }
    m.setStatic();
    m.updateBound();
    return m;
}
 
Example 4
Source File: FloatToFixed.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static VertexBuffer convertToFloat(VertexBuffer vb){
    if (vb.getFormat() == Format.Float)
        return vb;

    IntBuffer ib = (IntBuffer) vb.getData();
    FloatBuffer fb = BufferUtils.createFloatBuffer(ib.capacity());
    convertToFloat(ib, fb);

    VertexBuffer newVb = new VertexBuffer(vb.getBufferType());
    newVb.setupData(vb.getUsage(),
                    vb.getNumComponents(),
                    Format.Float,
                    fb);
    return newVb;
}
 
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: SilentTangentBinormalGenerator.java    From OpenRTS with MIT License 5 votes vote down vote up
public static Mesh genNormalLines(Mesh mesh, float scale) {
	FloatBuffer vertexBuffer = (FloatBuffer) mesh.getBuffer(Type.Position).getData();
	FloatBuffer normalBuffer = (FloatBuffer) mesh.getBuffer(Type.Normal).getData();

	ColorRGBA originColor = ColorRGBA.White;
	ColorRGBA normalColor = ColorRGBA.Blue;

	Mesh lineMesh = new Mesh();
	lineMesh.setMode(Mesh.Mode.Lines);

	Vector3f origin = new Vector3f();
	Vector3f point = new Vector3f();

	FloatBuffer lineVertex = BufferUtils.createFloatBuffer(vertexBuffer.limit() * 2);
	FloatBuffer lineColor = BufferUtils.createFloatBuffer(vertexBuffer.limit() / 3 * 4 * 2);

	for (int i = 0; i < vertexBuffer.limit() / 3; i++) {
		populateFromBuffer(origin, vertexBuffer, i);
		populateFromBuffer(point, normalBuffer, i);

		int index = i * 2;

		setInBuffer(origin, lineVertex, index);
		setInBuffer(originColor, lineColor, index);

		point.multLocal(scale);
		point.addLocal(origin);
		setInBuffer(point, lineVertex, index + 1);
		setInBuffer(normalColor, lineColor, index + 1);
	}

	lineMesh.setBuffer(Type.Position, 3, lineVertex);
	lineMesh.setBuffer(Type.Color, 4, lineColor);

	lineMesh.setStatic();
	// lineMesh.setInterleaved();
	return lineMesh;
}
 
Example 7
Source File: PMDSkinMesh.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
    public synchronized PMDSkinMesh clone() {
        PMDSkinMesh newMesh = (PMDSkinMesh)super.clone();
//        newMesh.boneMatrixArray = new Matrix4f[boneMatrixArray.length];
        newMesh.skinvb2 = new VertexBuffer(VertexBuffer.Type.Position);
        FloatBuffer skinvfb2 = BufferUtils.clone((FloatBuffer)this.skinvb2.getData());
        newMesh.skinvb2.setupData(VertexBuffer.Usage.Dynamic, 3, VertexBuffer.Format.Float, skinvfb2);
        
//        newMesh.skinnb2 = new VertexBuffer(VertexBuffer.Type.Normal);
//        FloatBuffer skinnfb2 = BufferUtils.clone((FloatBuffer)this.skinnb2.getData());
//        newMesh.skinnb2.setupData(VertexBuffer.Usage.Static, 3, VertexBuffer.Format.Float, skinnfb2);
        
        VertexBuffer skinvb1 = new VertexBuffer(VertexBuffer.Type.Position);
//        FloatBuffer skinvfb1 = BufferUtils.clone((FloatBuffer)this.skinvb2.getData());
        FloatBuffer skinvfb1 = BufferUtils.clone((FloatBuffer)this.getBuffer(VertexBuffer.Type.Position).getData());
        skinvb1.setupData(VertexBuffer.Usage.Dynamic, 3, VertexBuffer.Format.Float, skinvfb1);
        newMesh.clearBuffer(VertexBuffer.Type.Position);
        newMesh.setBuffer(skinvb1);
        
//        VertexBuffer skinnb1 = new VertexBuffer(VertexBuffer.Type.Normal);
//        FloatBuffer skinnfb1 = BufferUtils.clone((FloatBuffer)this.skinnb2.getData());
//        FloatBuffer skinnfb1 = BufferUtils.clone((FloatBuffer)this.getBuffer(VertexBuffer.Type.Normal).getData());
//        skinnb1.setupData(VertexBuffer.Usage.Stream, 3, VertexBuffer.Format.Float, skinnfb1);
//        newMesh.clearBuffer(VertexBuffer.Type.Normal);
//        newMesh.setBuffer(skinnb1);
        FloatBuffer newBoneMatrixBuffer = BufferUtils.createFloatBuffer(boneMatrixBuffer.capacity());
        boneMatrixBuffer.position(0);
        newBoneMatrixBuffer.put(boneMatrixBuffer);
        newBoneMatrixBuffer.position(0);
        newMesh.setBoneMatrixBuffer(newBoneMatrixBuffer);
        return newMesh;
    }
 
Example 8
Source File: BoundingSphere.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Calculates a minimum bounding sphere for the set of points. The algorithm
 * was originally found in C++ at
 * <p><a href="http://www.flipcode.com/cgi-bin/msg.cgi?showThread=COTD-SmallestEnclosingSpheres&forum=cotd&id=-1">
 * http://www.flipcode.com/cgi-bin/msg.cgi?showThread=COTD-SmallestEnclosingSpheres&forum=cotd&id=-1</a><br><strong>broken link</strong></p>
 * <p>and translated to java by Cep21</p>
 *
 * @param points
 *            The points to calculate the minimum bounds from.
 */
public void calcWelzl(FloatBuffer points) {
    if (center == null) {
        center = new Vector3f();
    }
    FloatBuffer buf = BufferUtils.createFloatBuffer(points.limit());
    points.rewind();
    buf.put(points);
    buf.flip();
    recurseMini(buf, buf.limit() / 3, 0, 0);
}
 
Example 9
Source File: VectorSet.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Access the buffer containing all the Vector3f values in this set. No
 * further add() is allowed.
 *
 * @return a new buffer, flipped
 */
public FloatBuffer toBuffer() {
    int numFloats = 3 * set.size();
    FloatBuffer buffer = BufferUtils.createFloatBuffer(numFloats);
    for (Vector3f tempVector : set) {
        buffer.put(tempVector.x);
        buffer.put(tempVector.y);
        buffer.put(tempVector.z);
    }
    buffer.flip();

    return buffer;
}
 
Example 10
Source File: DebugMeshCallback.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public FloatBuffer getVertices() {
    FloatBuffer buf = BufferUtils.createFloatBuffer(list.size() * 3);
    for (int i = 0; i < list.size(); i++) {
        Vector3f vector3f = list.get(i);
        buf.put(vector3f.x);
        buf.put(vector3f.y);
        buf.put(vector3f.z);
    }
    return buf;
}
 
Example 11
Source File: DebugMeshCallback.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public FloatBuffer getVertices() {
    FloatBuffer buf = BufferUtils.createFloatBuffer(list.size() * 3);
    for (int i = 0; i < list.size(); i++) {
        Vector3f vector3f = list.get(i);
        buf.put(vector3f.x);
        buf.put(vector3f.y);
        buf.put(vector3f.z);
    }
    return buf;
}
 
Example 12
Source File: SkinMeshData.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void read(DataInputStream is, byte[] buf) throws IOException {
    skinvfbb = BufferUtil.read(is, buf);
    skinvfb = skinvfbb.asFloatBuffer();
    skinnfbb = BufferUtil.read(is, buf);
    skinnfb = skinnfbb.asFloatBuffer();
    if (is.readBoolean()) {
        skintfbb = BufferUtil.read(is, buf);
        skintfb = skintfbb.asFloatBuffer();
    }
    wfbb = BufferUtil.read(is, buf);
    wfb = wfbb.asFloatBuffer();
    skinbisbb = BufferUtil.read(is, buf);
    skinbisb = skinbisbb.asShortBuffer();
    int length = is.readInt();
    skinIndexArray = new int[length];
    for (int i = 0; i < length; i++) {
        skinIndexArray[i] = is.readInt();
    }
    int size = is.readInt();
    indexShortBufferMap = new HashMap<PMDMaterial, ShortBuffer>();
    for(int i=0;i<size;i++) {
        PMDMaterial mat = model.getMaterial()[is.readInt()];
        ShortBuffer sb = BufferUtil.read(is, buf).asShortBuffer();
        indexShortBufferMap.put(mat, sb);
    }
    skinvfb2 = BufferUtils.createFloatBuffer(skinvfb.capacity());
    skinvfb.position(0);
    skinvfb2.put(skinvfb);
}
 
Example 13
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 14
Source File: GeoMap.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Deprecated
public FloatBuffer getHeightData(){
    if (!isLoaded())
        return null;
    return BufferUtils.createFloatBuffer(hdata);
}
 
Example 15
Source File: LODGeomap.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public FloatBuffer writeNormalArray(FloatBuffer store, Vector3f scale) {
    if (!isLoaded()) {
        throw new NullPointerException();
    }

    if (store != null) {
        if (store.remaining() < getWidth() * getHeight() * 3) {
            throw new BufferUnderflowException();
        }
    } else {
        store = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 3);
    }
    store.rewind();

    TempVars vars = TempVars.get();
    
    Vector3f rootPoint = vars.vect1;
    Vector3f rightPoint = vars.vect2;
    Vector3f leftPoint = vars.vect3;
    Vector3f topPoint = vars.vect4;
    Vector3f bottomPoint = vars.vect5;
    
    Vector3f tmp1 = vars.vect6;

    // calculate normals for each polygon
    for (int r = 0; r < getHeight(); r++) {
        for (int c = 0; c < getWidth(); c++) {

            rootPoint.set(0, getValue(c, r), 0);
            Vector3f normal = vars.vect8;

            if (r == 0) { // first row
                if (c == 0) { // first column
                    rightPoint.set(1, getValue(c + 1, r), 0);
                    bottomPoint.set(0, getValue(c, r + 1), 1);
                    getNormal(bottomPoint, rootPoint, rightPoint, scale, normal);
                } else if (c == getWidth() - 1) { // last column
                    leftPoint.set(-1, getValue(c - 1, r), 0);
                    bottomPoint.set(0, getValue(c, r + 1), 1);
                    getNormal(leftPoint, rootPoint, bottomPoint, scale, normal);
                } else { // all middle columns
                    leftPoint.set(-1, getValue(c - 1, r), 0);
                    rightPoint.set(1, getValue(c + 1, r), 0);
                    bottomPoint.set(0, getValue(c, r + 1), 1);
                    
                    normal.set( getNormal(leftPoint, rootPoint, bottomPoint, scale, tmp1) );
                    normal.addLocal( getNormal(bottomPoint, rootPoint, rightPoint, scale, tmp1) );
                }
            } else if (r == getHeight() - 1) { // last row
                if (c == 0) { // first column
                    topPoint.set(0, getValue(c, r - 1), -1);
                    rightPoint.set(1, getValue(c + 1, r), 0);
                    getNormal(rightPoint, rootPoint, topPoint, scale, normal);
                } else if (c == getWidth() - 1) { // last column
                    topPoint.set(0, getValue(c, r - 1), -1);
                    leftPoint.set(-1, getValue(c - 1, r), 0);
                    getNormal(topPoint, rootPoint, leftPoint, scale, normal);
                } else { // all middle columns
                    topPoint.set(0, getValue(c, r - 1), -1);
                    leftPoint.set(-1, getValue(c - 1, r), 0);
                    rightPoint.set(1, getValue(c + 1, r), 0);
                    
                    normal.set( getNormal(topPoint, rootPoint, leftPoint, scale, tmp1) );
                    normal.addLocal( getNormal(rightPoint, rootPoint, topPoint, scale, tmp1) );
                }
            } else { // all middle rows
                if (c == 0) { // first column
                    topPoint.set(0, getValue(c, r - 1), -1);
                    rightPoint.set(1, getValue(c + 1, r), 0);
                    bottomPoint.set(0, getValue(c, r + 1), 1);
                    
                    normal.set( getNormal(rightPoint, rootPoint, topPoint, scale, tmp1) );
                    normal.addLocal( getNormal(bottomPoint, rootPoint, rightPoint, scale, tmp1) );
                } else if (c == getWidth() - 1) { // last column
                    topPoint.set(0, getValue(c, r - 1), -1);
                    leftPoint.set(-1, getValue(c - 1, r), 0);
                    bottomPoint.set(0, getValue(c, r + 1), 1);

                    normal.set( getNormal(topPoint, rootPoint, leftPoint, scale, tmp1) );
                    normal.addLocal( getNormal(leftPoint, rootPoint, bottomPoint, scale, tmp1) );
                } else { // all middle columns
                    topPoint.set(0, getValue(c, r - 1), -1);
                    leftPoint.set(-1, getValue(c - 1, r), 0);
                    rightPoint.set(1, getValue(c + 1, r), 0);
                    bottomPoint.set(0, getValue(c, r + 1), 1);
                    
                    normal.set( getNormal(topPoint,  rootPoint, leftPoint, scale, tmp1 ) );
                    normal.addLocal( getNormal(leftPoint, rootPoint, bottomPoint, scale, tmp1) );
                    normal.addLocal( getNormal(bottomPoint, rootPoint, rightPoint, scale, tmp1) );
                    normal.addLocal( getNormal(rightPoint, rootPoint, topPoint, scale, tmp1) );
                }
            }
            normal.normalizeLocal();
            BufferUtils.setInBuffer(normal, store, (r * getWidth() + c)); // save the normal
        }
    }
    vars.release();
    
    return store;
}
 
Example 16
Source File: GeoMap.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
     * Creates a normal array from the normal data in this Geomap
     *
     * @param store A preallocated FloatBuffer where to store the data (optional), size must be &gt;= getWidth()*getHeight()*3
     * @return store, or a new FloatBuffer if store is null
     *
     * @throws NullPointerException If isLoaded() or hasNormalmap() is false
     */
    public FloatBuffer writeNormalArray(FloatBuffer store, Vector3f scale) {
        
        if (store!=null){
            if (store.remaining() < getWidth()*getHeight()*3)
                throw new BufferUnderflowException();
        }else{
            store = BufferUtils.createFloatBuffer(getWidth()*getHeight()*3);
        }
        store.rewind();

        Vector3f oppositePoint = new Vector3f();
        Vector3f adjacentPoint = new Vector3f();
        Vector3f rootPoint = new Vector3f();
        Vector3f tempNorm = new Vector3f();
        int normalIndex = 0;

        for (int y = 0; y < getHeight(); y++) {
            for (int x = 0; x < getWidth(); x++) {
                rootPoint.set(x, getValue(x,y), y);
                if (y == getHeight() - 1) {
                    if (x == getWidth() - 1) {  // case #4 : last row, last col
                        // left cross up
//                            adj = normalIndex - getWidth();
//                            opp = normalIndex - 1;
                        adjacentPoint.set(x, getValue(x,y-1), y-1);
                        oppositePoint.set(x-1, getValue(x-1, y), y);
                    } else {                    // case #3 : last row, except for last col
                        // right cross up
//                            adj = normalIndex + 1;
//                            opp = normalIndex - getWidth();
                        adjacentPoint.set(x+1, getValue(x+1,y), y);
                        oppositePoint.set(x, getValue(x,y-1), y-1);
                    }
                } else {
                    if (x == getWidth() - 1) {  // case #2 : last column except for last row
                        // left cross down
                        adjacentPoint.set(x-1, getValue(x-1,y), y);
                        oppositePoint.set(x, getValue(x,y+1), y+1);
//                            adj = normalIndex - 1;
//                            opp = normalIndex + getWidth();
                    } else {                    // case #1 : most cases
                        // right cross down
                        adjacentPoint.set(x, getValue(x,y+1), y+1);
                        oppositePoint.set(x+1, getValue(x+1,y), y);
//                            adj = normalIndex + getWidth();
//                            opp = normalIndex + 1;
                    }
                }



                tempNorm.set(adjacentPoint).subtractLocal(rootPoint)
                        .crossLocal(oppositePoint.subtractLocal(rootPoint));
                tempNorm.multLocal(scale).normalizeLocal();
//                    store.put(tempNorm.x).put(tempNorm.y).put(tempNorm.z);
                BufferUtils.setInBuffer(tempNorm, store,
                        normalIndex);
                normalIndex++;
            }
        }

        return store;
    }
 
Example 17
Source File: GeoMap.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
     * Creates a normal array from the normal data in this Geomap
     *
     * @param store A preallocated FloatBuffer where to store the data (optional), size must be >= getWidth()*getHeight()*3
     * @returns store, or a new FloatBuffer if store is null
     *
     * @throws NullPointerException If isLoaded() or hasNormalmap() is false
     */
    public FloatBuffer writeNormalArray(FloatBuffer store, Vector3f scale) {
        
        if (store!=null){
            if (store.remaining() < getWidth()*getHeight()*3)
                throw new BufferUnderflowException();
        }else{
            store = BufferUtils.createFloatBuffer(getWidth()*getHeight()*3);
        }
        store.rewind();

        Vector3f oppositePoint = new Vector3f();
        Vector3f adjacentPoint = new Vector3f();
        Vector3f rootPoint = new Vector3f();
        Vector3f tempNorm = new Vector3f();
        int normalIndex = 0;

        for (int y = 0; y < getHeight(); y++) {
            for (int x = 0; x < getWidth(); x++) {
                rootPoint.set(x, getValue(x,y), y);
                if (y == getHeight() - 1) {
                    if (x == getWidth() - 1) {  // case #4 : last row, last col
                        // left cross up
//                            adj = normalIndex - getWidth();
//                            opp = normalIndex - 1;
                        adjacentPoint.set(x, getValue(x,y-1), y-1);
                        oppositePoint.set(x-1, getValue(x-1, y), y);
                    } else {                    // case #3 : last row, except for last col
                        // right cross up
//                            adj = normalIndex + 1;
//                            opp = normalIndex - getWidth();
                        adjacentPoint.set(x+1, getValue(x+1,y), y);
                        oppositePoint.set(x, getValue(x,y-1), y-1);
                    }
                } else {
                    if (x == getWidth() - 1) {  // case #2 : last column except for last row
                        // left cross down
                        adjacentPoint.set(x-1, getValue(x-1,y), y);
                        oppositePoint.set(x, getValue(x,y+1), y+1);
//                            adj = normalIndex - 1;
//                            opp = normalIndex + getWidth();
                    } else {                    // case #1 : most cases
                        // right cross down
                        adjacentPoint.set(x, getValue(x,y+1), y+1);
                        oppositePoint.set(x+1, getValue(x+1,y), y);
//                            adj = normalIndex + getWidth();
//                            opp = normalIndex + 1;
                    }
                }



                tempNorm.set(adjacentPoint).subtractLocal(rootPoint)
                        .crossLocal(oppositePoint.subtractLocal(rootPoint));
                tempNorm.multLocal(scale).normalizeLocal();
//                    store.put(tempNorm.x).put(tempNorm.y).put(tempNorm.z);
                BufferUtils.setInBuffer(tempNorm, store,
                        normalIndex);
                normalIndex++;
            }
        }

        return store;
    }
 
Example 18
Source File: LODGeomap.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public FloatBuffer writeNormalArray(FloatBuffer store, Vector3f scale) {
    if (!isLoaded()) {
        throw new NullPointerException();
    }

    if (store != null) {
        if (store.remaining() < getWidth() * getHeight() * 3) {
            throw new BufferUnderflowException();
        }
    } else {
        store = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 3);
    }
    store.rewind();

    TempVars vars = TempVars.get();
    
    Vector3f rootPoint = vars.vect1;
    Vector3f rightPoint = vars.vect2;
    Vector3f leftPoint = vars.vect3;
    Vector3f topPoint = vars.vect4;
    Vector3f bottomPoint = vars.vect5;
    
    Vector3f tmp1 = vars.vect6;

    // calculate normals for each polygon
    for (int r = 0; r < getHeight(); r++) {
        for (int c = 0; c < getWidth(); c++) {

            rootPoint.set(0, getValue(c, r), 0);
            Vector3f normal = vars.vect8;

            if (r == 0) { // first row
                if (c == 0) { // first column
                    rightPoint.set(1, getValue(c + 1, r), 0);
                    bottomPoint.set(0, getValue(c, r + 1), 1);
                    getNormal(bottomPoint, rootPoint, rightPoint, scale, normal);
                } else if (c == getWidth() - 1) { // last column
                    leftPoint.set(-1, getValue(c - 1, r), 0);
                    bottomPoint.set(0, getValue(c, r + 1), 1);
                    getNormal(leftPoint, rootPoint, bottomPoint, scale, normal);
                } else { // all middle columns
                    leftPoint.set(-1, getValue(c - 1, r), 0);
                    rightPoint.set(1, getValue(c + 1, r), 0);
                    bottomPoint.set(0, getValue(c, r + 1), 1);
                    
                    normal.set( getNormal(leftPoint, rootPoint, bottomPoint, scale, tmp1) );
                    normal.addLocal( getNormal(bottomPoint, rootPoint, rightPoint, scale, tmp1) );
                }
            } else if (r == getHeight() - 1) { // last row
                if (c == 0) { // first column
                    topPoint.set(0, getValue(c, r - 1), -1);
                    rightPoint.set(1, getValue(c + 1, r), 0);
                    getNormal(rightPoint, rootPoint, topPoint, scale, normal);
                } else if (c == getWidth() - 1) { // last column
                    topPoint.set(0, getValue(c, r - 1), -1);
                    leftPoint.set(-1, getValue(c - 1, r), 0);
                    getNormal(topPoint, rootPoint, leftPoint, scale, normal);
                } else { // all middle columns
                    topPoint.set(0, getValue(c, r - 1), -1);
                    leftPoint.set(-1, getValue(c - 1, r), 0);
                    rightPoint.set(1, getValue(c + 1, r), 0);
                    
                    normal.set( getNormal(topPoint, rootPoint, leftPoint, scale, tmp1) );
                    normal.addLocal( getNormal(rightPoint, rootPoint, topPoint, scale, tmp1) );
                }
            } else { // all middle rows
                if (c == 0) { // first column
                    topPoint.set(0, getValue(c, r - 1), -1);
                    rightPoint.set(1, getValue(c + 1, r), 0);
                    bottomPoint.set(0, getValue(c, r + 1), 1);
                    
                    normal.set( getNormal(rightPoint, rootPoint, topPoint, scale, tmp1) );
                    normal.addLocal( getNormal(bottomPoint, rootPoint, rightPoint, scale, tmp1) );
                } else if (c == getWidth() - 1) { // last column
                    topPoint.set(0, getValue(c, r - 1), -1);
                    leftPoint.set(-1, getValue(c - 1, r), 0);
                    bottomPoint.set(0, getValue(c, r + 1), 1);

                    normal.set( getNormal(topPoint, rootPoint, leftPoint, scale, tmp1) );
                    normal.addLocal( getNormal(leftPoint, rootPoint, bottomPoint, scale, tmp1) );
                } else { // all middle columns
                    topPoint.set(0, getValue(c, r - 1), -1);
                    leftPoint.set(-1, getValue(c - 1, r), 0);
                    rightPoint.set(1, getValue(c + 1, r), 0);
                    bottomPoint.set(0, getValue(c, r + 1), 1);
                    
                    normal.set( getNormal(topPoint,  rootPoint, leftPoint, scale, tmp1 ) );
                    normal.addLocal( getNormal(leftPoint, rootPoint, bottomPoint, scale, tmp1) );
                    normal.addLocal( getNormal(bottomPoint, rootPoint, rightPoint, scale, tmp1) );
                    normal.addLocal( getNormal(rightPoint, rootPoint, topPoint, scale, tmp1) );
                }
            }
            normal.normalizeLocal();
            BufferUtils.setInBuffer(normal, store, (r * getWidth() + c)); // save the normal
        }
    }
    vars.release();
    
    return store;
}
 
Example 19
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 20
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();

}