glVertexAttribPointer - OpenGLES / OpenGL

时间:2013-10-17 00:27:44

标签: c++ opengl opengl-es buffer

我正在尝试将OpenGLES代码移植到OpenGL,我对glVertexAttribPointer有点困惑。这是OpenGLES代码的一部分:

//DRAWING OBJECT
// Get buffers from mesh

Mesh mesh = obj.getMesh();
FloatBuffer _vb = mesh.get_vb();
ShortBuffer _ib = mesh.get_ib();
short[] _indices = mesh.get_indices();

//the vertex info
_vb.position(TRIANGLE_VERTICES_DATA_POS_OFFSET);
gl.glVertexAttribPointer(gl.glGetAttribLocation(shader.get_program(), "aPosition"), 3, gl.GL_FLOAT, false,TRIANGLE_VERTICES_DATA_STRIDE_BYTES, _vb);
gl.glEnableVertexAttribArray(gl.glGetAttribLocation(shader.get_program(), "aPosition"));
// Draw with indices
gl.glDrawElements(gl.GL_TRIANGLES, _indices.length, gl.GL_UNSIGNED_SHORT, _ib);

那么如何使用OpenGL创建缓冲区呢?因为在OpenGLES中,顶点直接取自glVertexAttribArray函数中的当前数组缓冲区。 我试图使用glBufferData,但它没有用。

1 个答案:

答案 0 :(得分:0)

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 * SIZEOF_FLOAT);
    bb.order(ByteOrder.nativeOrder());
    FloatBuffer fb = bb.asFloatBuffer();
    fb.put(coords);
    fb.position(0);
    return fb;
}