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

The following examples show how to use java.nio.FloatBuffer#flip() . 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: Mat4Test.java    From jglm with Apache License 2.0 6 votes vote down vote up
@Test
public void testNewWithBuffer() {
	FloatBuffer buffer = FloatBuffer.allocate(16);
	buffer.put(1f).put(2f).put(3f).put(4f).put(5f).put(6f).put(7f).put(8f)
		.put(9f).put(10f).put(11f).put(12f).put(13f).put(14f).put(15f).put(16f);
	buffer.flip();
	
	Mat4 m1 = new Mat4(buffer);
	Mat4 m2 = new Mat4(
			1f, 2f, 3f, 4f,
			5f, 6f, 7f, 8f,
			9f, 10f, 11f, 12f,
			13f, 14f, 15f, 16f
	);
	
	Assert.assertEquals(m2, m1);
}
 
Example 2
Source File: Util.java    From 3DGameEngine with Apache License 2.0 6 votes vote down vote up
public static FloatBuffer CreateFlippedBuffer(Vertex[] vertices)
{
	FloatBuffer buffer = CreateFloatBuffer(vertices.length * Vertex.SIZE);
	
	for(int i = 0; i < vertices.length; i++)
	{
		buffer.put(vertices[i].GetPos().GetX());
		buffer.put(vertices[i].GetPos().GetY());
		buffer.put(vertices[i].GetPos().GetZ());
		buffer.put(vertices[i].GetTexCoord().GetX());
		buffer.put(vertices[i].GetTexCoord().GetY());
		buffer.put(vertices[i].GetNormal().GetX());
		buffer.put(vertices[i].GetNormal().GetY());
		buffer.put(vertices[i].GetNormal().GetZ());
		buffer.put(vertices[i].GetTangent().GetX());
		buffer.put(vertices[i].GetTangent().GetY());
		buffer.put(vertices[i].GetTangent().GetZ());
	}
	
	buffer.flip();
	
	return buffer;
}
 
Example 3
Source File: BufferUtils.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" 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 4
Source File: GPUImageTwoInputFilter.java    From TikTok with Apache License 2.0 6 votes vote down vote up
public void setFilterSecondTextureCoordinateAttribute(int width, int height, int left, int top, int right, int bottom, PointF centerPoint, float cosignx, float singnx){
    float widthScale = width/(float)(right-left);
    float heightScale = height/(float)(bottom-top);
    Log.e("MagicFace","MagicFace:widthScale:" + widthScale + ",heightScale:" + heightScale);
    float v1x = (((left-centerPoint.x)*cosignx-(top-centerPoint.y)*singnx + centerPoint.x)/width*2-1)*widthScale;
    float v1y = (((left-centerPoint.x)*singnx+(top-centerPoint.y)*cosignx+centerPoint.y)/height*2-1)*heightScale;
    float v2x = (((right-centerPoint.x)*cosignx-(top-centerPoint.y)*singnx+centerPoint.x)/width*2-1)*widthScale;
    float v2y = (((right-centerPoint.x)*singnx+(top-centerPoint.y)*cosignx+centerPoint.y)/height*2-1)*heightScale;
    float v3x = (((left-centerPoint.x)*cosignx-(bottom-centerPoint.y)*singnx+centerPoint.x)/width*2-1)*widthScale;
    float v3y = (((left-centerPoint.x)*singnx+(bottom-centerPoint.y)*cosignx+centerPoint.y)/height*2-1)*heightScale;
    float v4x = (((right-centerPoint.x)*cosignx-(bottom-centerPoint.y)*singnx+centerPoint.x)/width*2-1)*widthScale;
    float v4y = (((right-centerPoint.x)*singnx+(bottom-centerPoint.y)*cosignx+centerPoint.y)/height*2-1)*heightScale;
    float[] testArray = {
            v1x, v1y,
            v2x, v2y,
            v3x, v3y,
            v4x, v4y
    };

    mTexture2CoordinatesBuffer = ByteBuffer.allocateDirect(testArray.length * 4)
            .order(ByteOrder.nativeOrder());
    FloatBuffer fBuffer = mTexture2CoordinatesBuffer.asFloatBuffer();
    fBuffer.put(testArray);
    fBuffer.flip();
}
 
Example 5
Source File: AnimationLoader.java    From OpenGL-Animation with The Unlicense 6 votes vote down vote up
private void processTransforms(String jointName, String[] rawData, KeyFrameData[] keyFrames, boolean root){
	FloatBuffer buffer = BufferUtils.createFloatBuffer(16);
	float[] matrixData = new float[16];
	for(int i=0;i<keyFrames.length;i++){
		for(int j=0;j<16;j++){
			matrixData[j] = Float.parseFloat(rawData[i*16 + j]);
		}
		buffer.clear();
		buffer.put(matrixData);
		buffer.flip();
		Matrix4f transform = new Matrix4f();
		transform.load(buffer);
		transform.transpose();
		if(root){
			//because up axis in Blender is different to up axis in game
			Matrix4f.mul(CORRECTION, transform, transform);
		}
		keyFrames[i].addJointTransform(new JointTransformData(jointName, transform));
	}
}
 
Example 6
Source File: GPUImageTwoInputFilter.java    From TikTok with Apache License 2.0 6 votes vote down vote up
public void setFilterSecondTextureCoordinateAttribute(int width,int height,int left,int top,int right,int bottom){
    float widthScale = width/(float)(right-left);
    float heightScale = height/(float)(bottom-top);
    Log.e("MagicFace","MagicFace:widthScale:" + widthScale + ",heightScale:" + heightScale);
    float v1x = -left/(float)width*widthScale;
    float v1y = heightScale-top/(float)height*heightScale;
    float v2x = widthScale-left/(float)width*widthScale;
    float v2y = heightScale-top/(float)height*heightScale;
    float v3x = -left/(float)width*widthScale;
    float v3y = -top/(float)height*heightScale;
    float v4x = widthScale-left/(float)width*widthScale;
    float v4y = -top/(float)height*heightScale;
    float[] testArray = {
            v1x, v1y,
            v2x, v2y,
            v3x, v3y,
            v4x, v4y
    };

    mTexture2CoordinatesBuffer = ByteBuffer.allocateDirect(testArray.length * 4)
            .order(ByteOrder.nativeOrder());
    FloatBuffer fBuffer = mTexture2CoordinatesBuffer.asFloatBuffer();
    fBuffer.put(testArray);
    fBuffer.flip();
}
 
Example 7
Source File: TextureView.java    From jaamsim with Apache License 2.0 6 votes vote down vote up
private void updateTexCoordBuffer(Renderer renderer) {
	GL2GL3 gl = renderer.getGL();
	int texCoordBuffSize = _texCoords.size()*2*4;
	_texCoordHandle = renderer.getTexMemManager().allocateBuffer(texCoordBuffSize, gl);
	int buffID = _texCoordHandle.bind();

	FloatBuffer texData = FloatBuffer.allocate(_texCoords.size()*2);

	for (Vec2d v : _texCoords) {
		texData.put((float)v.x); texData.put((float)v.y);
	}
	texData.flip();
	gl.glBindBuffer(GL2GL3.GL_ARRAY_BUFFER, buffID);
	gl.glBufferData(GL2GL3.GL_ARRAY_BUFFER, texCoordBuffSize, texData, GL2GL3.GL_STATIC_DRAW);

	int texCoordVar = gl.glGetAttribLocation(progHandle, "texCoord");
	gl.glEnableVertexAttribArray(texCoordVar);

	gl.glBindBuffer(GL2GL3.GL_ARRAY_BUFFER, buffID);
	gl.glVertexAttribPointer(texCoordVar, 2, GL2GL3.GL_FLOAT, false, 0, 0);

}
 
Example 8
Source File: BufferUtils.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Generate a new FloatBuffer using the given array of Vector4 objects. The
 * FloatBuffer will be 4 * data.length long and contain the vector data.
 *
 * @param data
 *            array of Vector4 objects to place into a new FloatBuffer
 * @return a new direct, flipped FloatBuffer, or null if data was null
 */
public static FloatBuffer createFloatBuffer(Vector4f... data) {
    if (data == null) {
        return null;
    }
    FloatBuffer buff = createFloatBuffer(4 * data.length);
    for (int x = 0; x < data.length; x++) {
        if (data[x] != null) {
            buff.put(data[x].getX()).put(data[x].getY()).put(data[x].getZ()).put(data[x].getW());
        } else {
            buff.put(0).put(0).put(0).put(0);
        }
    }
    buff.flip();
    return buff;
}
 
Example 9
Source File: BufferUtil.java    From Lwjgl3-Game-Engine-Programming-Series with MIT License 5 votes vote down vote up
public static FloatBuffer createFlippedBuffer(float... values)
{
	FloatBuffer buffer = createFloatBuffer(values.length);
	buffer.put(values);
	buffer.flip();
	
	return buffer;
}
 
Example 10
Source File: LwjglRasteriser.java    From tectonicus with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void setProjectionMatrix(Matrix4f matrix)
{
	GL11.glMatrixMode(GL11.GL_PROJECTION);
	GL11.glLoadIdentity();
	
	FloatBuffer buffer = BufferUtils.createFloatBuffer(16);
	matrix.store(buffer);
	buffer.flip();
	GL11.glLoadMatrix(buffer);
}
 
Example 11
Source File: LwjglMesh.java    From tectonicus with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static FloatBuffer realloc(FloatBuffer existing, final int existingSize, final int newSize)
{
	FloatBuffer newBuffer = BufferUtils.createFloatBuffer(newSize);
	
	if (existing != null)
	{
		existing.flip();
		newBuffer.put(existing);
	}
	
	return newBuffer;
}
 
Example 12
Source File: Util.java    From 3DGameEngine with Apache License 2.0 5 votes vote down vote up
public static FloatBuffer CreateFlippedBuffer(Matrix4f value)
{
	FloatBuffer buffer = CreateFloatBuffer(4 * 4);
	
	for(int i = 0; i < 4; i++)
		for(int j = 0; j < 4; j++)
			buffer.put(value.Get(i, j));
	
	buffer.flip();
	
	return buffer;
}
 
Example 13
Source File: BufferUtils.java    From aion-germany with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Generate a new FloatBuffer using the given array of float primitives.
 *
 * @param data
 *            array of float primitives to place into a new FloatBuffer
 */
public static FloatBuffer createFloatBuffer(float... data) {
	if (data == null) {
		return null;
	}
	FloatBuffer buff = createFloatBuffer(data.length);
	buff.clear();
	buff.put(data);
	buff.flip();
	return buff;
}
 
Example 14
Source File: URendererOGL.java    From ure with MIT License 5 votes vote down vote up
private void renderBuffer() {
    if (tris == 0) return;

    FloatBuffer v = BufferUtils.createFloatBuffer(tris * 3 * 3);
    FloatBuffer c = BufferUtils.createFloatBuffer(tris * 3 * 4);
    FloatBuffer u = BufferUtils.createFloatBuffer(tris * 3 * 2);

    v.put(verts_pos, 0, v.capacity());
    c.put(verts_col, 0, c.capacity());
    u.put(verts_uv, 0, u.capacity());

    v.flip();
    c.flip();
    u.flip();

    GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
    GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
    GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);

    GL11.glVertexPointer(3, GL_FLOAT, 0, v);
    GL11.glColorPointer(4, GL_FLOAT, 0, c);
    GL11.glTexCoordPointer(2, GL_FLOAT, 0, u);


    GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, tris * 3);
    tris = 0;
}
 
Example 15
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 16
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 17
Source File: GLUtils.java    From trekarta with GNU General Public License v3.0 4 votes vote down vote up
public static void glUniform3fv(int location, int count, float[] val) {
    FloatBuffer buf = MapRenderer.getFloatBuffer(count * 3);
    buf.put(val);
    buf.flip();
    gl.uniform3fv(location, count, buf);
}
 
Example 18
Source File: GLUtils.java    From trekarta with GNU General Public License v3.0 4 votes vote down vote up
public static void glUniform4fv(int location, int count, float[] val) {
    FloatBuffer buf = MapRenderer.getFloatBuffer(count * 4);
    buf.put(val);
    buf.flip();
    gl.uniform4fv(location, count, buf);
}
 
Example 19
Source File: ParticlePointMesh.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void updateParticleData(Particle[] particles, Camera cam, Matrix3f inverseRotation) {
    VertexBuffer pvb = getBuffer(VertexBuffer.Type.Position);
    FloatBuffer positions = (FloatBuffer) pvb.getData();

    VertexBuffer cvb = getBuffer(VertexBuffer.Type.Color);
    ByteBuffer colors = (ByteBuffer) cvb.getData();

    VertexBuffer svb = getBuffer(VertexBuffer.Type.Size);
    FloatBuffer sizes = (FloatBuffer) svb.getData();

    VertexBuffer tvb = getBuffer(VertexBuffer.Type.TexCoord);
    FloatBuffer texcoords = (FloatBuffer) tvb.getData();

    float sizeScale = emitter.getWorldScale().x;

    // update data in vertex buffers
    positions.rewind();
    colors.rewind();
    sizes.rewind();
    texcoords.rewind();
    for (int i = 0; i < particles.length; i++){
        Particle p = particles[i];
        
        positions.put(p.position.x)
                 .put(p.position.y)
                 .put(p.position.z);

        sizes.put(p.size * sizeScale);
        colors.putInt(p.color.asIntABGR());

        int imgX = p.imageIndex % imagesX;
        int imgY = (p.imageIndex - imgX) / imagesY;

        float startX = ((float) imgX) / imagesX;
        float startY = ((float) imgY) / imagesY;
        float endX   = startX + (1f / imagesX);
        float endY   = startY + (1f / imagesY);

        texcoords.put(startX).put(startY).put(endX).put(endY);
    }
    positions.flip();
    colors.flip();
    sizes.flip();
    texcoords.flip();

    // force renderer to re-send data to GPU
    pvb.updateData(positions);
    cvb.updateData(colors);
    svb.updateData(sizes);
    tvb.updateData(texcoords);
}
 
Example 20
Source File: MediaEffectDrawer.java    From libcommon with Apache License 2.0 4 votes vote down vote up
protected MediaEffectDrawer(final int numTex,
	final boolean isOES, final String vss, final String fss) {

	mTexTarget = isOES ? GL_TEXTURE_EXTERNAL_OES : GL_TEXTURE_2D;
	final FloatBuffer pVertex = ByteBuffer.allocateDirect(VERTEX_SZ * FLOAT_SZ)
		.order(ByteOrder.nativeOrder()).asFloatBuffer();
	pVertex.put(VERTICES);
	pVertex.flip();
	final FloatBuffer pTexCoord = ByteBuffer.allocateDirect(VERTEX_SZ * FLOAT_SZ)
		.order(ByteOrder.nativeOrder()).asFloatBuffer();
	pTexCoord.put(TEXCOORD);
	pTexCoord.flip();

	// テクスチャ用のロケーションは最低でも1つは確保する
	muTexLoc = new int[numTex > 0 ? numTex : 1];
	hProgram = GLHelper.loadShader(vss, fss);
	GLES20.glUseProgram(hProgram);
	final int maPositionLoc = GLES20.glGetAttribLocation(hProgram, "aPosition");
	final int maTextureCoordLoc = GLES20.glGetAttribLocation(hProgram, "aTextureCoord");
       muMVPMatrixLoc = GLES20.glGetUniformLocation(hProgram, "uMVPMatrix");
       muTexMatrixLoc = GLES20.glGetUniformLocation(hProgram, "uTexMatrix");
       muTexLoc[0] = GLES20.glGetUniformLocation(hProgram, "sTexture");
       for (int i = 1; i < numTex; i++) {
		muTexLoc[i] = GLES20.glGetUniformLocation(hProgram,
			String.format(Locale.US, "sTexture%d", i + 1));
	}
       // モデルビュー変換行列を初期化
	Matrix.setIdentityM(mMvpMatrix, 0);
	//
	if (muMVPMatrixLoc >= 0) {
       	GLES20.glUniformMatrix4fv(muMVPMatrixLoc, 1, false, mMvpMatrix, 0);
	}
	if (muTexMatrixLoc >= 0) {
		// ここは単位行列に初期化するだけなのでmMvpMatrixを流用
       	GLES20.glUniformMatrix4fv(muTexMatrixLoc, 1, false, mMvpMatrix, 0);
	}
	// 頂点座標配列を割り当てる
	GLES20.glVertexAttribPointer(maPositionLoc,
		2, GLES20.GL_FLOAT, false, VERTEX_SZ, pVertex);
	GLES20.glEnableVertexAttribArray(maPositionLoc);
	// テクスチャ座標配列を割り当てる
	GLES20.glVertexAttribPointer(maTextureCoordLoc,
		2, GLES20.GL_FLOAT, false, VERTEX_SZ, pTexCoord);
	GLES20.glEnableVertexAttribArray(maTextureCoordLoc);
}