基于Qt的OpenGL可编程管线学习- 使用Instanced方式绘制
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于Qt的OpenGL可编程管线学习- 使用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);
本文出自 “不会飞的纸飞机” 博客,请务必保留此出处http://douzhq.blog.51cto.com/12552184/1930997
以上是关于基于Qt的OpenGL可编程管线学习- 使用Instanced方式绘制的主要内容,如果未能解决你的问题,请参考以下文章