绘制多个重复的模型时,使用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);
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。