OpenGl - Vertex Buffer Object Is Not Drawn To Screen
I am trying to go from displaying my GL_QUADS with glBegin() to displaying them with a VBO. Unfortunately after my change the program does not display the QUADS anymore. All I see is a black screen. That is what I am doing:
I define two VBOs. One for vertex data, one for indices, and allocate some space in them:
glGenBuffersARB(2, pboIds);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, pboIds[0]);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, 50000*sizeof(GLfloat), 0, GL_STREAM_DRAW_ARB);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, pboIds[1]);
glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 50000*sizeof(GLint), 0, GL_STREAM_DRAW_ARB);
Then I map them and write information to them:
float *ptr = (float*)glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_READ_WRITE_ARB);
int *ptrind = (int*)glMapBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB开发者_如何学Python, GL_READ_WRITE_ARB);
int nVertexCount=0;
for (...){
*ptr=vertX; ++ptr; *ptrind=nVertexCount; ++ptrind; nVertexCount++;
*ptr=vertY; ++ptr; *ptrind=nVertexCount; ++ptrind; nVertexCount++;
*ptr=vertZ; ++ptr; *ptrind=nVertexCount; ++ptrind; nVertexCount++;
}
glUnmapBufferARB(GL_ARRAY_BUFFER_ARB);
glUnmapBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB);
Finally, I try to display them:
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, 0);
glDrawElements(GL_QUADS, nVertexCount, GL_INT, (pboIds+1));
glDisableClientState(GL_VERTEX_ARRAY);
My program compiles just fine. I don't get any crashes when executing it. But all I see - is a black screen. I use the same routine to determine the vertex coordinates as I did with glBegin(GL_QUADS). Back then I could see white quads on the screen. Maybe I am doing something wrong in my VBO code?
EDIT: Unfortunately I am under windows. Do I have to do something about these to stop using the ARB extension? I have that from an example code:
#ifdef _WIN32
PFNGLGENBUFFERSARBPROC pglGenBuffersARB = 0;
PFNGLBINDBUFFERARBPROC pglBindBufferARB = 0;
PFNGLBUFFERDATAARBPROC pglBufferDataARB = 0;
PFNGLBUFFERSUBDATAARBPROC pglBufferSubDataARB = 0;
PFNGLDELETEBUFFERSARBPROC pglDeleteBuffersARB = 0;
PFNGLGETBUFFERPARAMETERIVARBPROC pglGetBufferParameterivARB = 0;
PFNGLMAPBUFFERARBPROC pglMapBufferARB = 0;
PFNGLUNMAPBUFFERARBPROC pglUnmapBufferARB = 0;
#define glGenBuffersARB pglGenBuffersARB
#define glBindBufferARB pglBindBufferARB
#define glBufferDataARB pglBufferDataARB
#define glBufferSubDataARB pglBufferSubDataARB
#define glDeleteBuffersARB pglDeleteBuffersARB
#define glGetBufferParameterivARB pglGetBufferParameterivARB
#define glMapBufferARB pglMapBufferARB
#define glUnmapBufferARB pglUnmapBufferARB
#endif
glDrawElements(GL_QUADS, nVertexCount, GL_INT, (pboIds+1));
You don't pass the buffer object name to this function. It uses whatever buffer object happens to be bound to GL_ELEMENT_ARRAY_BUFFER at the time. That value is the offset from the beginning of the buffer; it specifies where the first index is.
Also, stop using the ARB extension version of buffer objects. Just use the core stuff.
Unfortunately I am under windows.
I don't see what that has anything to do with it. Use an OpenGL loading library, if you can't do it yourself.
精彩评论