0
votes

I'm trying to port an OpenGLES code to OpenGL and I'm a little confused about the glVertexAttribPointer. Here is part of the OpenGLES code:

//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);

So how can I create a buffer using OpenGL? Because in OpenGLES the vertices are directly taken from the current Array Buffer in the glVertexAttribArray function. I tried to use glBufferData but it didn't work.

1
Which version of OpenGL ES are you referring to here? I would avoid using client (non-VBO) memory in general, especially on GLES2 class hardware (or in general, any hardware that does not share memory with the CPU). In all cases, Buffer Objects have been supported in GLES since 1.0, so you can use VBOs just like you would in regular OpenGL.Andon M. Coleman
The version is the 2.0. The last parameter of glVertexAttribPointer it's a pointer to the vertex buffer (it's a FloatBuffer). But I can't use in the same way in OpenGL. I tried to use the following code in the initialization but it didn't worked: glGenBuffers(1,&buffer); glBindBuffer(GL_ARRAY_BUFFER,buffer); glBufferData(GL_ARRAY_BUFFER,sizeof(_vb),_vb,GL_STATIC_DRAW); Any suggestions?Alex Lima
Pass 0L to glVertexAttribPointer (...) if you are using a VBO, instead of _vb. You cannot use a client memory address (FloatBuffer in this case) as the vertex attrib pointer location if you are using a VBO. When using a VBO, the address is supposed to be an offset into the memory allocated by the VBO that was bound at the time you called the function.Andon M. Coleman
Oh, I see, thanks! But now when I call glBindBuffer it generates the access violation error. Do you have any idea what it is? Thanks again.Alex Lima
No, maybe if you updated your code listing it might help.Andon M. Coleman

1 Answers

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