基于Qt的OpenGL可编程管线学习(3)-使用Instanced方式绘制
绘制多个重复的模型时,使用Instanced方式绘制可以大大加快显然速度。
网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了望城免费建站欢迎大家使用!
绘制效果如下图所示:
1、Vertex Shader中定义如下:
attribute vec3 pos; attribute vec2 coord; attribute vec3 normal; attribute vec3 offset; uniform mat4 M; uniform mat4 V; uniform mat4 P; uniform mat4 NM; varying vec2 M_coord; varying vec3 M_normal; varying vec4 M_WordPos; void main() { M_coord = coord; M_WordPos = M * vec4(pos, 1.0) + vec4(offset, 1.0); M_normal = mat3(NM) * normal; gl_Position = P * V * M_WordPos; }
offset为每次绘制的偏移量。
2、CPU端实现
float nIndexOffset[] = { -100.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 100.0f, 0.0f, 0.0f };
createBufferObject为封装创建OpenGL Buffer的函数
定义如下:
GLuint OpenGLOperate::createBufferObject(GLenum nBufferType, GLsizeiptr size, \ const GLvoid *data, GLenum usage) { GLuint BufferObjIndex = 0; m_OpenGLCore->glGenBuffers(1, &BufferObjIndex); m_OpenGLCore->glBindBuffer(nBufferType, BufferObjIndex); m_OpenGLCore->glBufferData(nBufferType, size, data, usage); m_OpenGLCore->glBindBuffer(nBufferType, 0); return BufferObjIndex; }
创建Offset的FBO
m_OffsetIndex = m_OpenGLOperate->createBufferObject(GL_ARRAY_BUFFER, \ sizeof(float) * 9, \ (void*)nIndexOffset, GL_STATIC_DRAW);
为Offset赋值
OpenGLCore->glBindBuffer(GL_ARRAY_BUFFER, m_OffsetIndex); OpenGLCore->glEnableVertexAttribArray(m_OffsetLocation); OpenGLCore->glVertexAttribPointer(m_OffsetLocation, 3, GL_FLOAT, GL_FALSE, \ sizeof(float) * 3, (void*)0); OpenGLCore->glBindBuffer(GL_ARRAY_BUFFER, 0); OpenGLCore->glVertexAttribDivisor(m_OffsetLocation, 1);
调用绘制指令:
OpenGLCore->glDrawElementsInstanced(GL_TRIANGLES, m_VertexCount, \ GL_UNSIGNED_INT, 0, 3);
新闻名称:基于Qt的OpenGL可编程管线学习(3)-使用Instanced方式绘制
本文链接:http://cdiso.cn/article/jeicdo.html