OpenGL 4.0 VAO VBO 理解

本文详细介绍了OpenGL中的顶点数组对象(VAO)和顶点缓冲区对象(VBO)的概念及其工作原理。VAO用于存储可渲染物体的所有信息,而VBO则存储顶点数据。文章还探讨了VAO的状态管理以及如何使用这些对象提高渲染效率。

转载自:http://blog.youkuaiyun.com/zhuyingqingfen/article/details/19238651

顶点数组对象(Vertex Array Object  即VAO)是一个包含一个或数个顶点缓冲区对象(Vertex Buffer Object, 即 VBO)的对象,一般存储一个可渲染物体的所有信息。VAO的编号最多在0 to GL_MAX_VERTEX_ATTRIBS​ - 1.之间。

顶点缓冲区对象(Vertex Buffer Object VBO)是你显卡内存中的一块高速内存缓冲区,用来存储顶点的所有信息。



There is always a VAO bound to the context. The default VAO has the name 0 and is created along with the context, similar to textures 0 and framebuffer 0. When you bind a VAO, all the state stored in it will become active. Calls to glVertexAttribPointer, or glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ...), modify the currently bound VAO.

 

一个VAO通常绑定到一个上下文中,默认的VAO编号是0,这个是随着上下文创建时而创建的,还有textures 0、framebuffer 0 。当你绑定一个VAO 它的所有的状态将会激活,调用glvertexAttribPointer 或者glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,...),将会改变当前的绑定的VAO

VAO 对象

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. struct VertexAttributeState  
  2. {  
  3.     bool                bIsEnabled = false;  
  4.     int                 iSize = 4; //This is the number of elements in each attrib, 1-4.  
  5.     unsigned int        iStride = 0;  
  6.     VertexAttribType    eType = GL_FLOAT;  
  7.     bool                bIsNormalized = false;  
  8.     bool                bIsIntegral = false;  
  9.     void *              pPtrOrBufferObjectOffset = 0;  
  10.     BufferObject *      pBufferObj = 0;  
  11. };  
  12.    
  13. struct VertexArrayObjectState  
  14. {  
  15.     BufferObject *pElementArrayBufferObject = NULL;  
  16.     VertexAttributeState attributes[MAX_VERTEX_ATTRIB];  
  17. }  
  18.    
  19. static VertexArrayObjectState *pContextVAOState = new VertexArrayObjectState();  
  20. static BufferObject *pCurrentArrayBuffer = NULL;  


VertexArrayObjectState 中存储的就是一个VAO存储的所有状态。

 

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. void glBindBuffer(enum target, uint buffer)  
  2. {  
  3.     BufferObject *pBuffer = ConvNameToBufferObj(buffer);  
  4.    
  5.     switch(target)  
  6.     {  
  7.     case GL_ARRAY_BUFFER:  
  8.         pCurrentArrayBuffer = pBuffer;  
  9.         break;  
  10.     case GL_ELEMENT_ARRAY_BUFFER:  
  11.         pContextVAOState->pElementArrayBufferObject = pBuffer;  
  12.         break;  
  13.     ...  
  14.     }  
  15. }  
  16.    
  17. void glEnableVertexAttribArray(uint index)  
  18. {  
  19.     pContextVAOState->attributes[index].bIsEnabled = true;  
  20. }  
  21.    
  22. void glDisableVertexAttribArray(uint index)  
  23. {  
  24.     pContextVAOState->attributes[index].bIsEnabled = false;  
  25. }  
  26.    
  27. void glVertexAttribPointer(uint index, int size, enum type, boolean normalized, sizei stride, const void *pointer)  
  28. {  
  29.     VertexAttributeState &currAttrib = pContextVAOState->attributes[index];  
  30.    
  31.     currAttrib.iSize = size;  
  32.     currAttrib.eType = type;  
  33.     currAttrib.iStride = stride;  
  34.     currAttrib.bIsNormalized = normalized;  
  35.     currAttrib.bIsIntegral = true;  
  36.     currAttrib.pPtrOrBufferObjectOffset = pointer;  
  37.     currAttrib.pBufferObj = pCurrentArrayBuffer;  
  38. }  

 

从上面可以看出(glBindBuffer函数),GL_ARRAY_BUFFER​ 不是VAO的状态!,事实上连接属性索引和一个缓冲区的是glVertexAttribPointer.

This works just like any other state object. Allocated Vertex Array Objects will have their state set by these functions

上面这些工作原理和其他状态对象一样,分配的VAO将通过这些函数来用他们的状态集。

glDrawElements将会用你所设置的当前OpenGL的状态上下文来渲染你的物体。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值