Java Code Examples for java.nio.FloatBuffer#limit()

The following examples show how to use java.nio.FloatBuffer#limit() . 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: DOMOutputCapsule.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void write(FloatBuffer value, String name, FloatBuffer defVal) throws IOException {
    if (value == null) {
        return;
    }

    Element el = appendElement(name);
    el.setAttribute("size", String.valueOf(value.limit()));
    StringBuilder buf = new StringBuilder();
    int pos = value.position();
    value.rewind();
    int ctr = 0;
    while (value.hasRemaining()) {
        ctr++;
        buf.append(value.get());
        buf.append(" ");
    }
    if (ctr != value.limit())
        throw new IOException("'" + name
            + "' buffer contention resulted in write data consistency.  "
            + ctr + " values written when should have written "
            + value.limit());
    buf.setLength(Math.max(0, buf.length() - 1));
    value.position(pos);
    el.setAttribute(dataAttributeName, buf.toString());
    currentElement = (Element) el.getParentNode();
}
 
Example 2
Source File: BufferUtils.java    From Ultraino with MIT License 6 votes vote down vote up
/**
 * Ensures there is at least the <code>required</code> number of entries
 * left after the current position of the buffer. If the buffer is too small
 * a larger one is created and the old one copied to the new buffer.
 *
 * @param buffer
 *            buffer that should be checked/copied (may be null)
 * @param required
 *            minimum number of elements that should be remaining in the
 *            returned buffer
 * @return a buffer large enough to receive at least the
 *         <code>required</code> number of entries, same position as the
 *         input buffer, not null
 */
public static FloatBuffer ensureLargeEnough(FloatBuffer buffer, int required) {
    if (buffer != null) {
        buffer.limit(buffer.capacity());
    }
    if (buffer == null || (buffer.remaining() < required)) {
        int position = (buffer != null ? buffer.position() : 0);
        FloatBuffer newVerts = createFloatBuffer(position + required);
        if (buffer != null) {
            buffer.flip();
            newVerts.put(buffer);
            newVerts.position(position);
        }
        buffer = newVerts;
    }
    return buffer;
}
 
Example 3
Source File: DataSetUtilsHelper.java    From chart-fx with Apache License 2.0 6 votes vote down vote up
protected static double[] readDoubleArrayFromBuffer(final FloatBuffer floatBuffer,
        final DoubleBuffer doubleBuffer) {
    double[] retArray;
    if (floatBuffer != null) {
        retArray = new double[floatBuffer.limit()];
        for (int i = 0; i < retArray.length; i++) {
            retArray[i] = floatBuffer.get(i);
        }
        return retArray;
    }
    if (doubleBuffer != null) {
        retArray = new double[doubleBuffer.limit()];
        for (int i = 0; i < retArray.length; i++) {
            retArray[i] = doubleBuffer.get(i);
        }
        // alt:
        // doubleBuffer.get(retArray);
        return retArray;
    }
    throw new InvalidParameterException("floatBuffer and doubleBuffer must not both be null");
}
 
Example 4
Source File: DMesh.java    From Lemur with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected void morph( FloatBuffer sourcePos, FloatBuffer sourceNorms,
                      FloatBuffer targetPos, FloatBuffer targetNorms ) {
    if( deform == null )
        return;

    int count = sourcePos.limit() / 3;
    Vector3f v = new Vector3f();
    Vector3f normal = new Vector3f();

    for( int i = 0; i < count; i++ ) {
        v.x = sourcePos.get();
        v.y = sourcePos.get();
        v.z = sourcePos.get();
        normal.x = sourceNorms.get();
        normal.y = sourceNorms.get();
        normal.z = sourceNorms.get();

        morphVertex(v, normal);

        targetPos.put(v.x).put(v.y).put(v.z);
        targetNorms.put(normal.x).put(normal.y).put(normal.z);
    }
}
 
Example 5
Source File: UVCoordinatesGenerator.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * This method returns the bounding tube of the given mesh.
 * 
 * @param mesh
 *            the mesh
 * @return bounding tube of the given mesh
 */
/* package */static BoundingTube getBoundingTube(Mesh mesh) {
    Vector3f center = new Vector3f();
    float maxx = -Float.MAX_VALUE, minx = Float.MAX_VALUE;
    float maxy = -Float.MAX_VALUE, miny = Float.MAX_VALUE;
    float maxz = -Float.MAX_VALUE, minz = Float.MAX_VALUE;

    FloatBuffer positions = mesh.getFloatBuffer(VertexBuffer.Type.Position);
    int limit = positions.limit();
    for (int i = 0; i < limit; i += 3) {
        float x = positions.get(i);
        float y = positions.get(i + 1);
        float z = positions.get(i + 2);
        center.addLocal(x, y, z);
        maxx = x > maxx ? x : maxx;
        minx = x < minx ? x : minx;
        maxy = y > maxy ? y : maxy;
        miny = y < miny ? y : miny;
        maxz = z > maxz ? z : maxz;
        minz = z < minz ? z : minz;
    }
    center.divideLocal(limit / 3);

    float radius = Math.max(maxx - minx, maxy - miny) * 0.5f;
    return new BoundingTube(radius, maxz - minz, center);
}
 
Example 6
Source File: BatchNode.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void doCopyBuffer(FloatBuffer inBuf, int offset, FloatBuffer outBuf, int componentSize) {
    TempVars vars = TempVars.get();
    Vector3f pos = vars.vect1;

    // offset is given in element units
    // convert to be in component units
    offset *= componentSize;

    for (int i = 0; i < inBuf.limit() / componentSize; i++) {
        pos.x = inBuf.get(i * componentSize);
        pos.y = inBuf.get(i * componentSize + 1);
        pos.z = inBuf.get(i * componentSize + 2);

        outBuf.put(offset + i * componentSize, pos.x);
        outBuf.put(offset + i * componentSize + 1, pos.y);
        outBuf.put(offset + i * componentSize + 2, pos.z);
    }
    vars.release();
}
 
Example 7
Source File: DebugShapeFactory.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" 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 8
Source File: BufferUtils.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Generates a Vector3f array from the given FloatBuffer.
 *
 * @param buff
 *            the FloatBuffer to read from
 * @return a newly generated array of Vector3f objects
 */
public static Vector3f[] getVector3Array(FloatBuffer buff) {
    buff.clear();
    Vector3f[] verts = new Vector3f[buff.limit() / 3];
    for (int x = 0; x < verts.length; x++) {
        Vector3f v = new Vector3f(buff.get(), buff.get(), buff.get());
        verts[x] = v;
    }
    return verts;
}
 
Example 9
Source File: Util.java    From oreon-engine with GNU General Public License v3.0 5 votes vote down vote up
public static Vertex[] toVertexArray(FloatBuffer data)
{
	Vertex[] vertices = new Vertex[data.limit() / Vertex.FLOATS];
	
	for(int i=0; i<vertices.length; i++)
	{
		vertices[i] = new Vertex();
		vertices[i].setPosition(new Vec3f(data.get(),data.get(),data.get()));
		vertices[i].setUVCoord(new Vec2f(data.get(),data.get()));
		vertices[i].setNormal(new Vec3f(data.get(),data.get(),data.get()));
	}
	
	return vertices;
}
 
Example 10
Source File: MaskedTilemapScript.java    From remixed-dungeon with GNU General Public License v3.0 5 votes vote down vote up
public void drawQuadSet(FloatBuffer vertices, FloatBuffer mask, int size) {

        if (size == 0) {
            return;
        }

        if(vertices.limit() < 16 * size){
            throw new AssertionError();
        }

        if(mask.limit() < 8 * size){
            throw new AssertionError();
        }


        vertices.position(0);
        aXY.vertexPointer(2, 4, vertices);

        vertices.position(2);
        aUV.vertexPointer(2, 4, vertices);

        mask.position(0);
        //vertices.position(2);
        //aUV_mask.vertexPointer(2, 4, vertices);
        aUV_mask.vertexPointer(2, 2, mask);

        GLES20.glDrawElements(
                GLES20.GL_TRIANGLES,
                Quad.SIZE * size,
                GLES20.GL_UNSIGNED_SHORT,
                Quad.getIndices(size));
    }
 
Example 11
Source File: Points.java    From ShapesInOpenGLES2.0 with MIT License 5 votes vote down vote up
/**
 * create buffers for the Points shape object
 * @param pointPositions
 * @param pointColors
 */
public void createBuffers(float[] pointPositions, float[] pointColors) {
    // First, copy cube information into client-side floating point buffers.
    FloatBuffer pointPositionsBuffer;
    FloatBuffer pointColorsBuffer;

    try{
        vertexCount = pointPositions.length / POSITION_DATA_SIZE;

        pointPositionsBuffer = ByteBuffer.allocateDirect(pointPositions.length * BYTES_PER_FLOAT)
                .order(ByteOrder.nativeOrder()).asFloatBuffer();
        pointPositionsBuffer.put(pointPositions).position(0);

        pointColorsBuffer = ByteBuffer.allocateDirect(pointColors.length * BYTES_PER_FLOAT)
                .order(ByteOrder.nativeOrder()).asFloatBuffer();
        pointColorsBuffer.put(pointColors).position(0);

        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, glPointBuffer[0]);
        GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, pointPositionsBuffer.capacity() * BYTES_PER_FLOAT, pointPositionsBuffer, GLES20.GL_STATIC_DRAW);

        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, glPointBuffer[1]);
        GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, pointColorsBuffer.capacity() * BYTES_PER_FLOAT, pointColorsBuffer, GLES20.GL_STATIC_DRAW);

        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);

        aPointPositionsBufferIdx = glPointBuffer[0];
        aPointColorsBufferIdx = glPointBuffer[1];

        pointPositionsBuffer.limit(0);
        pointPositionsBuffer = null;
        pointColorsBuffer.limit(0);
        pointColorsBuffer = null;
    }catch (Exception e){
        Log.d(Tag,"point buffer creation failed:", e);
    }
}
 
Example 12
Source File: Lines.java    From ShapesInOpenGLES2.0 with MIT License 5 votes vote down vote up
/**
 * create buffers for the Lines shape object
 * @param linePositions
 * @param lineColors
 */
public void createBuffers(float[] linePositions, float[] lineColors) {
    final int lineDataLength = linePositions.length;
    vertexCount = linePositions.length / POSITION_DATA_SIZE;

    final FloatBuffer lineBuffer = ByteBuffer.allocateDirect(lineDataLength * BYTES_PER_FLOAT)
            .order(ByteOrder.nativeOrder()).asFloatBuffer();
    lineBuffer.put(linePositions);
    final FloatBuffer colorBuffer = ByteBuffer.allocateDirect(lineColors.length * BYTES_PER_FLOAT)
            .order(ByteOrder.nativeOrder()).asFloatBuffer();
    colorBuffer.put(lineColors);

    lineBuffer.position(0);
    colorBuffer.position(0);

    FloatBuffer linePositionsBuffer = lineBuffer;
    FloatBuffer lineColorsBuffers = colorBuffer;

    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, glLineBuffer[0]);
    GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, linePositionsBuffer.capacity() * BYTES_PER_FLOAT, linePositionsBuffer, GLES20.GL_STATIC_DRAW);

    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, glLineBuffer[1]);
    GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, lineColorsBuffers.capacity() * BYTES_PER_FLOAT, lineColorsBuffers, GLES20.GL_STATIC_DRAW);

    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);

    aLinePositionsBufferIdx = glLineBuffer[0];
    aLineColorsBufferIdx = glLineBuffer[1];

    linePositionsBuffer.limit(0);
    linePositionsBuffer = null;
    lineColorsBuffers.limit(0);
    lineColorsBuffers = null;
}
 
Example 13
Source File: BinaryOutputCapsule.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
protected void write(FloatBuffer value) throws IOException {
    if (value == null) {
        write(NULL_OBJECT);
        return;
    }
    value.rewind();
    int length = value.limit();
    write(length);
    for (int x = 0; x < length; x++) {
        writeForBuffer(value.get());
    }
    value.rewind();
}
 
Example 14
Source File: GeoMap.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Deprecated
public GeoMap(FloatBuffer heightData, int width, int height, int maxval){
    hdata = new float[heightData.limit()];
    heightData.get(hdata);
    this.width = width;
    this.height = height;
    this.maxval = maxval;
}
 
Example 15
Source File: TestPointerToBuffer.java    From jcuda with MIT License 5 votes vote down vote up
/**
 * Copy data from the given buffer into an array, using the given
 * pointer, and return whether the array contents matches the
 * expected result
 * 
 * @param buffer The buffer
 * @param pointer The pointer
 * @param elements The number of elements
 * @param expected The expected result
 * @return Whether the contents of the array matched the expected result
 */
private static boolean copy(
    FloatBuffer buffer, Pointer pointer, int elements, float expected[])
{
    log("Buffer     : " + buffer);
    log("position   : " + buffer.position());
    log("limit      : " + buffer.limit());
    if (buffer.hasArray())
    {
        log("arrayOffset: " + buffer.arrayOffset() + " ");
        log("array      : " + Arrays.toString(buffer.array()));
    }

    String contents = "contents   : ";
    for (int i = buffer.position(); i < buffer.limit(); i++)
    {
        contents += buffer.get(i);
        if (i < buffer.limit() - 1)
        {
            contents += ", ";
        }
    }
    log(contents+"\n");

    float result[] = new float[elements];
    cudaMemcpy(Pointer.to(result), pointer, 
        elements * Sizeof.FLOAT, cudaMemcpyHostToHost);

    boolean passed = Arrays.equals(result, expected);
    log("result     : " + Arrays.toString(result));
    log("passed?    : " + passed);
    return passed;
}
 
Example 16
Source File: BufferUtils.java    From aion-germany with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Generates a Vector3f array from the given FloatBuffer.
 *
 * @param buff
 *            the FloatBuffer to read from
 * @return a newly generated array of Vector3f objects
 */
public static Vector3f[] getVector3Array(FloatBuffer buff) {
	buff.clear();
	Vector3f[] verts = new Vector3f[buff.limit() / 3];
	for (int x = 0; x < verts.length; x++) {
		Vector3f v = new Vector3f(buff.get(), buff.get(), buff.get());
		verts[x] = v;
	}
	return verts;
}
 
Example 17
Source File: NoosaScript.java    From remixed-dungeon with GNU General Public License v3.0 3 votes vote down vote up
public void drawQuad( FloatBuffer vertices ) {

		if(vertices.limit()<16){
			throw new AssertionError();
		}

		vertices.position( 0 );
		aXY.vertexPointer( 2, 4, vertices );
		
		vertices.position( 2 );
		aUV.vertexPointer( 2, 4, vertices );

		GLES20.glDrawElements( GLES20.GL_TRIANGLES, Quad.SIZE, GLES20.GL_UNSIGNED_SHORT, Quad.getIndices( 1 ) );
		
	}
 
Example 18
Source File: BufferUtils.java    From Ultraino with MIT License 3 votes vote down vote up
/**
 * Create a new FloatBuffer of an appropriate size to hold the specified
 * number of Vector2f object data only if the given buffer if not already
 * the right size.
 *
 * @param buf
 *            the buffer to first check and rewind
 * @param vertices
 *            number of vertices that need to be held by the newly created
 *            buffer
 * @return the requested new FloatBuffer
 */
public static FloatBuffer createVector2Buffer(FloatBuffer buf, int vertices) {
    if (buf != null && buf.limit() == 2 * vertices) {
        buf.rewind();
        return buf;
    }

    return createFloatBuffer(2 * vertices);
}
 
Example 19
Source File: BufferUtils.java    From aion-germany with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Create a new FloatBuffer of an appropriate size to hold the specified number of Vector3f object data only if the given buffer if not already the right size.
 *
 * @param buf
 *            the buffer to first check and rewind
 * @param vertices
 *            number of vertices that need to be held by the newly created buffer
 * @return the requested new FloatBuffer
 */
public static FloatBuffer createVector3Buffer(FloatBuffer buf, int vertices) {
	if (buf != null && buf.limit() == 3 * vertices) {
		buf.rewind();
		return buf;
	}

	return createFloatBuffer(3 * vertices);
}
 
Example 20
Source File: BufferUtils.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * Create a new FloatBuffer of an appropriate size to hold the specified
 * number of Vector3f object data only if the given buffer if not already
 * the right size.
 *
 * @param buf
 *            the buffer to first check and rewind
 * @param vertices
 *            number of vertices that need to be held by the newly created
 *            buffer
 * @return the requested new FloatBuffer
 */
public static FloatBuffer createVector3Buffer(FloatBuffer buf, int vertices) {
    if (buf != null && buf.limit() == 3 * vertices) {
        buf.rewind();
        return buf;
    }

    return createFloatBuffer(3 * vertices);
}