Java Code Examples for java.nio.ByteBuffer.asFloatBuffer()

The following are Jave code examples for showing how to use asFloatBuffer() of the java.nio.ByteBuffer class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: spellcaster   File: RenderObject.java   View Source Code Vote up 6 votes
private void setTextureBuffer()
{

    // Set the texture buffer
    float[] uvs = new float[] {
            0.0f, 0.0f,
            0.0f, 1.0f,
            1.0f, 1.0f,
            1.0f, 0.0f
    };

    // The texture buffer
    ByteBuffer bb = ByteBuffer.allocateDirect(uvs.length * 4);
    bb.order(ByteOrder.nativeOrder());
    mTextureBuffer = bb.asFloatBuffer();
    mTextureBuffer.put(uvs);
    mTextureBuffer.position(0);
}
 
Example 2
Project: buildAPKsSamples   File: Square.java   View Source Code Vote up 6 votes
/**
 * Sets up the drawing object data for use in an OpenGL ES context.
 */
public Square() {
    // initialize vertex byte buffer for shape coordinates
    ByteBuffer bb = ByteBuffer.allocateDirect(
    // (# of coordinate values * 4 bytes per float)
            squareCoords.length * 4);
    bb.order(ByteOrder.nativeOrder());
    vertexBuffer = bb.asFloatBuffer();
    vertexBuffer.put(squareCoords);
    vertexBuffer.position(0);

    // initialize byte buffer for the draw list
    ByteBuffer dlb = ByteBuffer.allocateDirect(
            // (# of coordinate values * 2 bytes per short)
            drawOrder.length * 2);
    dlb.order(ByteOrder.nativeOrder());
    drawListBuffer = dlb.asShortBuffer();
    drawListBuffer.put(drawOrder);
    drawListBuffer.position(0);
}
 
Example 3
Project: AndroidCollection   File: Texture.java   View Source Code Vote up 6 votes
public Texture(Context mContext) {
    this.mContext = mContext;
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(texture.length * 4);
    byteBuffer.order(ByteOrder.nativeOrder());
    mTextureBuffer = byteBuffer.asFloatBuffer();
    mTextureBuffer.put(texture);
    mTextureBuffer.position(0);

    ByteBuffer vByteBuffer = ByteBuffer.allocateDirect(coordinates.length * 4);
    vByteBuffer.order(ByteOrder.nativeOrder());
    mVertexBuffer = vByteBuffer.asFloatBuffer();
    mVertexBuffer.put(coordinates);
    mVertexBuffer.position(0);

    ByteBuffer iByteBuffer = ByteBuffer.allocateDirect(indices.length * 2);
    iByteBuffer.order(ByteOrder.nativeOrder());
    mIndexBuffer = iByteBuffer.asShortBuffer();
    mIndexBuffer.put(indices);
    mIndexBuffer.position(0);
}
 
Example 4
Project: Amazing   File: Square.java   View Source Code Vote up 6 votes
public Square() {
    // initialize vertex byte buffer for shape coordinates
    ByteBuffer bb = ByteBuffer.allocateDirect(
            // (# of coordinate values * 4 bytes per float)
            squareCoords.length * 4);
    bb.order(ByteOrder.nativeOrder());
    vertexBuffer = bb.asFloatBuffer();
    vertexBuffer.put(squareCoords);
    vertexBuffer.position(0);

    // initialize byte buffer for the draw list
    ByteBuffer dlb = ByteBuffer.allocateDirect(
            // (# of coordinate values * 2 bytes per short)
            drawOrder.length * 2);
    dlb.order(ByteOrder.nativeOrder());
    drawListBuffer = dlb.asShortBuffer();
    drawListBuffer.put(drawOrder);
    drawListBuffer.position(0);
}
 
Example 5
Project: AndroidCollection   File: Square.java   View Source Code Vote up 6 votes
public Square() {
    ByteBuffer vByteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
    vByteBuffer.order(ByteOrder.nativeOrder());
    mVertexBuffer = vByteBuffer.asFloatBuffer();
    mVertexBuffer.put(vertices);
    mVertexBuffer.position(0);

    ByteBuffer iByteBuffer = ByteBuffer.allocateDirect(indices.length * 2);
    iByteBuffer.order(ByteOrder.nativeOrder());
    mIndexBuffer = iByteBuffer.asShortBuffer();
    mIndexBuffer.put(indices);
    mIndexBuffer.position(0);

    ByteBuffer cByteBuffer = ByteBuffer.allocateDirect(colors.length * 4);
    cByteBuffer.order(ByteOrder.nativeOrder());
    mColorBuffer = cByteBuffer.asFloatBuffer();
    mColorBuffer.put(colors);
    mColorBuffer.position(0);
}
 
Example 6
Project: Building-Android-UIs-with-Custom-Views   File: GLDrawer.java   View Source Code Vote up 6 votes
private void setColors(int[] faceColors) {
    colors = new float[options * 4 * faceColors.length];
    int wOffset = 0;
    for (int faceColor : faceColors) {
        float[] color = hexToRGBA(faceColor);
        for(int j = 0; j < 4; j++) {
            colors[wOffset++] = color[0];
            colors[wOffset++] = color[1];
            colors[wOffset++] = color[2];
            colors[wOffset++] = color[3];
        }
    }
    ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * (Float.SIZE / 8));
    cbb.order(ByteOrder.nativeOrder());

    colorBuffer = cbb.asFloatBuffer();
    colorBuffer.put(colors);
    colorBuffer.position(0);
}
 
Example 7
Project: AndroidCollection   File: Cube.java   View Source Code Vote up 6 votes
public Cube(float width, float height, float depth) {
    ByteBuffer vByteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
    vByteBuffer.order(ByteOrder.nativeOrder());
    mVertexBuffer = vByteBuffer.asFloatBuffer();
    mVertexBuffer.put(vertices);
    mVertexBuffer.position(0);

    ByteBuffer iByteBuffer = ByteBuffer.allocateDirect(indices.length * 2);
    iByteBuffer.order(ByteOrder.nativeOrder());
    mIndexBuffer = iByteBuffer.asShortBuffer();
    mIndexBuffer.put(indices);
    mIndexBuffer.position(0);

    ByteBuffer cByteBuffer = ByteBuffer.allocateDirect(colors.length * 4);
    cByteBuffer.order(ByteOrder.nativeOrder());
    mColorBuffer = cByteBuffer.asFloatBuffer();
    mColorBuffer.put(colors);
    mColorBuffer.position(0);
    this.width = width / 2;
    this.height = height / 2;
    this.depth = depth / 2;
}
 
Example 8
Project: debug   File: DebugIT.java   View Source Code Vote up 5 votes
@Test
public void testUseFloatBufferViewOfByteBufferWithWrongByteOrder() {
    window = glfwCreateWindow(800, 600, "", 0L, 0L);
    glfwMakeContextCurrent(window);
    createCapabilities();
    ByteBuffer bb = ByteBuffer.allocateDirect(16 * 4);
    bb.putFloat(0, 1.0f);
    FloatBuffer fb = bb.asFloatBuffer();
    assertThrows(IllegalArgumentException.class, () -> glLoadMatrixf(fb), "buffer contains values written using non-native endianness.");
}
 
Example 9
Project: homescreenarcade   File: GLVertexList.java   View Source Code Vote up 5 votes
static FloatBuffer makeFloatBuffer(int size) {
    ByteBuffer vbb = ByteBuffer.allocateDirect(size * 4);
    vbb.order(ByteOrder.nativeOrder());
    FloatBuffer texBuffer = vbb.asFloatBuffer();
    texBuffer.position(0);
    return texBuffer;
}
 
Example 10
Project: Android_OpenGL_Demo   File: Triangle.java   View Source Code Vote up 5 votes
/**
 * Sets up the drawing object data for use in an OpenGL ES context.
 */
public Triangle() {
    // initialize vertex byte buffer for shape coordinates
    ByteBuffer bb = ByteBuffer.allocateDirect(
            // (number of coordinate values * 4 bytes per float)
            triangleCoords.length * 4);
    // use the device hardware's native byte order
    bb.order(ByteOrder.nativeOrder());

    // create a floating point buffer from the ByteBuffer
    vertexBuffer = bb.asFloatBuffer();
    // add the coordinates to the FloatBuffer
    vertexBuffer.put(triangleCoords);
    // set the buffer to read the first coordinate
    vertexBuffer.position(0);

    // prepare shaders and OpenGL program
    int vertexShader = MyGLRenderer.loadShader(
            GLES20.GL_VERTEX_SHADER, vertexShaderCode);
    int fragmentShader = MyGLRenderer.loadShader(
            GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);

    mProgram = GLES20.glCreateProgram();             // create empty OpenGL Program
    GLES20.glAttachShader(mProgram, vertexShader);   // add the vertex shader to program
    GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
    GLES20.glLinkProgram(mProgram);                  // create OpenGL program executables

}
 
Example 11
Project: Muse-EEG-Toolkit   File: GraphGLLine.java   View Source Code Vote up 5 votes
/**
 * Convert the timeseries snapshot into a run of (x, y, z) bytes.
 */
private FloatBuffer snapshotToBuffer(TimeSeriesSnapshot snapshot) {
  float[] asFloats = snapshotToFloat(snapshot);

  // Create buffer to store result.
  ByteBuffer bb = ByteBuffer.allocateDirect(snapshot.length * VERTEX_STRIDE);
  bb.order(ByteOrder.nativeOrder());

  // Write to buffer usign floats:
  FloatBuffer vertexBuffer = bb.asFloatBuffer();
  vertexBuffer.put(asFloats);
  vertexBuffer.position(0);
  return vertexBuffer;
}
 
Example 12
Project: aos-MediaLib   File: Icon.java   View Source Code Vote up 5 votes
private void Init( float w, float h, float x, float y, float z ) {

		mWidth = w;
		mHeight = h;

		ByteBuffer vbb = ByteBuffer.allocateDirect(VERTS * 3 * 4);
		vbb.order(ByteOrder.nativeOrder());
		mFVertexBuffer = vbb.asFloatBuffer();

		ByteBuffer tbb = ByteBuffer.allocateDirect(VERTS * 2 * 4);
		tbb.order(ByteOrder.nativeOrder());
		mTexBuffer = tbb.asFloatBuffer();

		ByteBuffer ibb = ByteBuffer.allocateDirect(VERTS * 2);
		ibb.order(ByteOrder.nativeOrder());
		mIndexBuffer = ibb.asShortBuffer();

		int n = 0;
		for (int i=0; i<VERTS; i++) {
			mFVertexBuffer.put( COORDS[n++]*mWidth  + x);
			mFVertexBuffer.put( COORDS[n++]*mHeight + y);
			mFVertexBuffer.put( COORDS[n++]         + z);
		}

		for (int i = 0; i < 2*VERTS; i++) {
			mTexBuffer.put(TEX_COORDS[i]);	//TODO ???
		}

		for(int i = 0; i < VERTS; i++) {
			mIndexBuffer.put((short) i);
		}

		mFVertexBuffer.position(0);
		mTexBuffer.position(0);
		mIndexBuffer.position(0);
	}
 
Example 13
Project: Agora-Video-Source-Android   File: TextureRenderer.java   View Source Code Vote up 5 votes
public TextureRenderer(boolean isOesTexture) {
    mOesTexture = isOesTexture;

    ByteBuffer bb = ByteBuffer.allocateDirect(squareVertices.length * 4);
    bb.order(ByteOrder.nativeOrder());
    vertexBuffer = bb.asFloatBuffer();
    vertexBuffer.put(squareVertices);
    vertexBuffer.position(0);

    ByteBuffer dlb = ByteBuffer.allocateDirect(drawOrder.length * 2);
    dlb.order(ByteOrder.nativeOrder());
    drawListBuffer = dlb.asShortBuffer();
    drawListBuffer.put(drawOrder);
    drawListBuffer.position(0);

    ByteBuffer bb2 = ByteBuffer.allocateDirect(textureVertices.length * 4);
    bb2.order(ByteOrder.nativeOrder());
    textureVerticesBuffer = bb2.asFloatBuffer();
    textureVerticesBuffer.put(textureVertices);
    textureVerticesBuffer.position(0);

    int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, mVertexShader);
    int fragmentShader;
    if (mOesTexture) {
        fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, mFragmentShaderOes);
    } else {
        fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, mFragmentShaderRgba);
    }

    mProgram = GLES20.glCreateProgram();             // create empty OpenGL ES Program
    GLES20.glAttachShader(mProgram, vertexShader);   // add the vertex shader to program
    GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
    GLES20.glLinkProgram(mProgram);

    muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
    muSTMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uSTMatrix");

    Matrix.setIdentityM(mMVPMatrix, 0);
    Matrix.setIdentityM(mSTMatrix, 0);
}
 
Example 14
Project: Android-AudioRecorder-App   File: GLWave.java   View Source Code Vote up 5 votes
/**
 * Update wave position.
 *
 * @param dAngle delta angle
 */
public void update(float dAngle) {
  if (vertexBuffer == null) {
    ByteBuffer vertexByteBuffer = ByteBuffer.allocateDirect(vertices.length * SIZE_OF_FLOAT);
    vertexByteBuffer.order(ByteOrder.nativeOrder());
    vertexBuffer = vertexByteBuffer.asFloatBuffer();
    vertexBuffer.put(vertices);
    vertexBuffer.position(0);
  }
  float angle = currentAngle;
  angle += dAngle;
  currentAngle = angle;
  if (coefficient == 0 && latestCoefficient > 0) {
    coefficient = Utils.smooth(0, latestCoefficient, SMOOTH_A);
  }

  float val = (float) Math.sin(angle) * coefficient;
  if (prevVal > 0 && val <= 0 || prevVal < 0 && val >= 0) {
    coefficient = Utils.smooth(coefficient, latestCoefficient, SMOOTH_A);
    waveX = random.nextFloat() * 0.3f * (random.nextBoolean() ? 1 : -1);
  }
  prevVal = val;
  int i = 0;
  double step = 1.0 / POINTS_PER_WAVE;
  float posX = Utils.normalizeGl(waveX, fromX, toX);
  float posY = Utils.normalizeGl(val, fromY, toY);
  for (float time = 0; time < 1 - step / 2; time += step) {
    vertices[COORDS_PER_VERTEX * i + 1 + SKIP] = angle;
    vertexBuffer.put(COORDS_PER_VERTEX * i + SKIP,
        Utils.quad(time, vertices[6], posX, vertices[vertices.length - 6]));
    vertexBuffer.put(COORDS_PER_VERTEX * i + 1 + SKIP,
        Utils.quad(time, vertices[7], posY, vertices[vertices.length - 5]));
    i++;
  }
}
 
Example 15
Project: aos-MediaLib   File: FadeOutBand.java   View Source Code Vote up 5 votes
private void Init( float w, float h, float x, float y, float z ) {

		mWidth = w;
		mHeight = h;

		ByteBuffer vbb = ByteBuffer.allocateDirect(VERTS * 3 * 4);
		vbb.order(ByteOrder.nativeOrder());
		mFVertexBuffer = vbb.asFloatBuffer();

		ByteBuffer ibb = ByteBuffer.allocateDirect(VERTS * 2);
		ibb.order(ByteOrder.nativeOrder());
		mIndexBuffer = ibb.asShortBuffer();

		int n = 0;
		for (int i=0; i<VERTS; i++) {
			mFVertexBuffer.put( COORDS[n++]*mWidth  + x);
			mFVertexBuffer.put( COORDS[n++]*mHeight + y);
			mFVertexBuffer.put( COORDS[n++]         + z);
		}

		for(int i = 0; i < VERTS; i++) {
			mIndexBuffer.put((short) i);
		}


        ByteBuffer cbb = ByteBuffer.allocateDirect(COLORS.length*4);
        cbb.order(ByteOrder.nativeOrder());
        mColorBuffer = cbb.asIntBuffer();
        mColorBuffer.put(COLORS);
        
		mFVertexBuffer.position(0);
		mIndexBuffer.position(0);
        mColorBuffer.position(0);
	}
 
Example 16
Project: 19porn   File: APlayerAndroid.java   View Source Code Vote up 5 votes
public int renderTexture(int textureId){
	if(mGpuImageFilter == null){
		mGpuImageFilter = new GPUImageFilter(null);
		mGpuImageFilter.init();
	}


	int vertexNum = 6;

	float[] vertexCoordinate  = {1.0f, -1.0f,  0.0f,
			1.0f,  1.0f,  0.0f,
			-1.0f,  1.0f,  0.0f,
			-1.0f,  1.0f,  0.0f,
			-1.0f, -1.0f,  0.0f,
			1.0f, -1.0f,  0.0f};

	ByteBuffer bbVertices = ByteBuffer.allocateDirect(vertexNum * 3 * 4);
	bbVertices.order(ByteOrder.nativeOrder());
	FloatBuffer vertexBuf = bbVertices.asFloatBuffer();
	vertexBuf.put(vertexCoordinate);
	vertexBuf.position(0);

	float[] textureCoordinate1  = {1.0f, 1.0f,
			1.0f, 0.0f,
			0.0f, 0.0f,
			0.0f, 0.0f,
			0.0f, 1.0f,
			1.0f, 1.0f};


	ByteBuffer bbColors1 = ByteBuffer.allocateDirect(vertexNum * 2 * 4);
	bbColors1.order(ByteOrder.nativeOrder());
	FloatBuffer textureCoordinateBuf = bbColors1.asFloatBuffer();
	textureCoordinateBuf.put(textureCoordinate1);
	textureCoordinateBuf.position(0);


	mGpuImageFilter.draw(textureId,vertexBuf,textureCoordinateBuf);
	return 1;
}
 
Example 17
Project: AppRTC-Android   File: GlUtil.java   View Source Code Vote up 5 votes
public static FloatBuffer createFloatBuffer(float[] coords) {
  // Allocate a direct ByteBuffer, using 4 bytes per float, and copy coords into it.
  ByteBuffer bb = ByteBuffer.allocateDirect(coords.length * 4);
  bb.order(ByteOrder.nativeOrder());
  FloatBuffer fb = bb.asFloatBuffer();
  fb.put(coords);
  fb.position(0);
  return fb;
}
 
Example 18
Project: Amazing   File: Triangle.java   View Source Code Vote up 4 votes
public Triangle() {
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(triangleCoords.length * 4);

    byteBuffer.order(ByteOrder.nativeOrder());

    vertexBuffer = byteBuffer.asFloatBuffer();

    vertexBuffer.put(triangleCoords);
    vertexBuffer.position(0);

    int vertexShader = MyRender.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);

    int fragmentShader = MyRender.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);

    program = GLES20.glCreateProgram();

    GLES20.glAttachShader(program, vertexShader);

    GLES20.glAttachShader(program, fragmentShader);

    GLES20.glLinkProgram(program);
}
 
Example 19
Project: OpenJSharp   File: NativeFloat32Array.java   View Source Code Vote up 4 votes
@Override
public Float32ArrayData createArrayData(final ByteBuffer nb, final int start, final int end) {
    return new Float32ArrayData(nb.asFloatBuffer(), start, end);
}
 
Example 20
Project: 19porn   File: SurfaceRenderer.java   View Source Code Vote up 4 votes
private void CreateFlatModel(){
	vertexNum = 6;
	
	float[] vertexCoordinate  = {1.0f, -1.0f,  0.0f,
			 					1.0f,  1.0f,  0.0f,
			 				   -1.0f,  1.0f,  0.0f,
			 				   -1.0f,  1.0f,  0.0f,
			 				   -1.0f, -1.0f,  0.0f,
			 				    1.0f, -1.0f,  0.0f};
	
	ByteBuffer bbVertices = ByteBuffer.allocateDirect(vertexNum * 3 * 4);
    bbVertices.order(ByteOrder.nativeOrder());
    vertexBuf = bbVertices.asFloatBuffer();
    vertexBuf.put(vertexCoordinate);
    vertexBuf.position(0);

    float[] textureCoordinate1  = {1.0f, 1.0f,
            1.0f, 0.0f,
            0.0f, 0.0f,
            0.0f, 0.0f,
            0.0f, 1.0f,
            1.0f, 1.0f};
    
    
    ByteBuffer bbColors1 = ByteBuffer.allocateDirect(vertexNum * 2 * 4);
    bbColors1.order(ByteOrder.nativeOrder());
    textureCoordinateBuf1 = bbColors1.asFloatBuffer();
    textureCoordinateBuf1.put(textureCoordinate1);
    textureCoordinateBuf1.position(0);

    float[] textureCoordinate2  = {1.0f, 0.0f,
            1.0f, 1.0f,
            0.0f, 1.0f,
            0.0f, 1.0f,
            0.0f, 0.0f,
            1.0f, 0.0f};


    ByteBuffer bbColors = ByteBuffer.allocateDirect(vertexNum * 2 * 4);
    bbColors.order(ByteOrder.nativeOrder());
    textureCoordinateBuf2 = bbColors.asFloatBuffer();
    textureCoordinateBuf2.put(textureCoordinate2);
    textureCoordinateBuf2.position(0);

}