简体   繁体   English

使用OpenGL的VBO时出现问题

[英]Problem with using OpenGL's VBO

I just tried to render the first redbook example ( the white Quad ) by using VBOs. 我只是尝试使用VBO渲染第一个红皮书示例(白色Quad)。
It works fine with immediate mode and vertex arrays. 它适用于立即模式和顶点数组。

But when using VBOs the screen stays black. 但是当使用VBO时,屏幕保持黑色。 I think i must have missed something important. 我想我一定错过了重要的事情。

init: 在里面:

unsigned int bufIds[2];
glGenBuffers( 2, bufIds );
GLfloat vertices[] = {
  0.25, 0.25, 0.0,
  0.75, 0.25, 0.0,
  0.75, 0.75, 0.0,
  0.25, 0.75, 0.0
};
glBindBuffer( GL_ARRAY_BUFFER, bufIds[0] );
glBufferData( GL_ARRAY_BUFFER, 12, vertices, GL_STATIC_DRAW );
glBindBuffer( GL_ARRAY_BUFFER, 0 );

glClearColor( 0, 0, 0, 1 );
glColor3f( 1, 1, 1 );
glOrtho( 0.0, 1.0, 0.0, 1.0, -1.0, 1.0 );

renderloop for VBO (not working): VBO的renderloop(不工作):

glClear( GL_COLOR_BUFFER_BIT );
glEnableClientState( GL_VERTEX_ARRAY );
glBindBuffer( GL_ARRAY_BUFFER, bufIds[0] );
glVertexPointer( 3, GL_FLOAT, 0, 0 );
glDrawArrays( GL_QUADS, 0, 12 );
glBindBuffer( GL_ARRAY_BUFFER, 0 );
glDisableClientState( GL_VERTEX_ARRAY );

renderloop for vertex arrays (working): 顶点数组的renderloop(工作):

glClear( GL_COLOR_BUFFER_BIT );
glEnableClientState( GL_VERTEX_ARRAY );
glBindBuffer( GL_ARRAY_BUFFER, 0 );
glVertexPointer( 3, GL_FLOAT, 0, vertices );
glDrawArrays( GL_QUADS, 0, 12 );
glDisableClientState( GL_VERTEX_ARRAY );

argh i just figured it out by trying to read back the contents of the buffer: 我想通过尝试回读缓冲区的内容来解决这个问题:

i need to allocate the buffer with 12 * sizeof( GLfloat ) instead of only 12 我需要分配12 * sizeof(GLfloat)而不是12的缓冲区

glBufferData( GL_ARRAY_BUFFER, 12 * sizeof( GLfloat ), vertices, GL_STATIC_DRAW );

my read back code 我的回读代码

GLfloat vertices2[12];
glBindBuffer( GL_ARRAY_BUFFER, bufIds[0] );
glGetBufferSubData ( GL_ARRAY_BUFFER, 0, 12 * sizeof( GLfloat ), vertices2 );
glBindBuffer( GL_ARRAY_BUFFER, 0 );

for ( int i = 0; i < 4; i ++ ) {
    LOG_DEBUG << "point " << i << ": " << vertices2[ i * 3 + 0 ] << " / " << vertices2[ i * 3 + 1 ] << " / " << vertices2[ i * 3 + 2 ];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM