OPENGL的buffer_offset

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OPENGL的buffer_offset相关的知识,希望对你有一定的参考价值。

BUFFER_OFFSET(0)是什么意思,求解释。

GLfloat vertices[][ 3 ] =
-1.0, -1.0, -1.0 ,
1.0, -1.0, -1.0 ,
1.0, 1.0, -1.0 ,
-1.0, 1.0, -1.0 ,
-1.0, -1.0, 1.0 ,
1.0, -1.0, 1.0 ,
1.0, 1.0, 1.0 ,
-1.0, 1.0, 1.0
;

GLubyte indices[][ 4 ] =
0, 1, 2, 3 ,
4, 7, 6, 5 ,
0, 4, 5, 1 ,
3, 2, 6, 7 ,
0, 3, 7, 4 ,
1, 5, 6, 2
;

glGenBuffers( NUM_BUFFERS, buffers );// build buffers

glBindBuffer( GL_ARRAY_BUFFER, buffers[ VERTICES ] ); // Active buffers
/* Allocate memory for arrays */
glBufferData( GL_ARRAY_BUFFER, sizeof( vertices ), vertices, GL_STATIC_DRAW );
glVertexPointer( 3, GL_FLOAT, 0, BUFFER_OFFSET( 0 ) ); // Proscribe the formate of the data
glEnableClientState( GL_VERTEX_ARRAY ); // Build array
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, buffers[ INDICES ] ); // Active Buffers
// Allocate memory for indices
glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof( indices ), indices, GL_STATIC_DRAW );
glDrawElements( GL_QUADS, 24, GL_UNSIGNED_BYTE, BUFFER_OFFSET( 0 ) );

参考技术A #define BUFFER_OFFSET(offset) ((void*)(offset))

由于这个例子中坐标数组和index数组是保存在buffer object中,因此glVertexPointer()和glDrawElements()的最后一个参数不再代表数据首地址,而是数据在buffer object中的offset(单位是字节)
为什么要这么做呢?因为buffer中往往把同一个顶点的坐标、颜色、法线等数据放在一块,大概排列就是
vertex1, color1, normal1, vertex2, color2, normal2, vertex3,...
vertex在buffer object里的offset是BUFFER_OFFSET(0),color在buffer object里的offset是BUFFER_OFFSET(sizeof(vertex)),normal在buffer object里的offset是BUFFER_OFFSET(sizeof(vertex)+sizeof(color))

以上是关于OPENGL的buffer_offset的主要内容,如果未能解决你的问题,请参考以下文章

qt opengl的图形怎么刷新

OpenGL是啥?

OpenGL - 002_2OpenGL 常见专业名词解析

glfw 包含了opengl吗

不同的opengl上下文是不是有不同的opengl扩展?

qt 3d 和opengl哪个好