绘制多个重复的模型时,使用实例化方式绘制可以大大加快显然速度。
绘制效果如下图所示:
<强> 强>
attribute vec3 pos; attribute vec2  coord; attribute vec3 正常; attribute vec3 补偿; uniform mat4 M; uniform mat4 V; uniform mat4 P; uniform mat4 纳米; 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(抵消,,1.0); ,,,,,,,M_normal =, mat3 (NM), *,正常; ,,,,,,,gl_Position =, P *, V *, M_WordPos; }
偏移量为每次绘制的偏移量。
<强> 强>
float nIndexOffset [],=, { ,,,,,,,-100.0 f, f 0.0,, 0.0度, ,,,,,,,0.0 f, f 0.0,, 0.0度, ,,,,,,,100.0 f, f 0.0, 0.0度 ,,,},
createBufferObject为封装创建OpenGL缓冲区的函数
定义如下:
GLuint OpenGLOperate:: createBufferObject (GLenum nBufferType, GLsizeiptr 大小,,\ ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,const GLvoid *数据,GLenum 使用) { ,,,GLuint BufferObjIndex =, 0; ,,,m_OpenGLCore→glGenBuffers(1),和BufferObjIndex); ,,,m_OpenGLCore→glBindBuffer (BufferObjIndex nBufferType也); ,,,m_OpenGLCore→glBufferData (nBufferType、,大小,数据,使用); ,,,m_OpenGLCore→glBindBuffer (nBufferType, 0); ,,,return BufferObjIndex; }
创建抵消的反馈
m_OffsetIndex =, m_OpenGLOperate→createBufferObject (GL_ARRAY_BUFFER,, \ ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,sizeof(浮动),*,9日\ ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,(void *) nIndexOffset, GL_STATIC_DRAW);
为抵消赋值
OpenGLCore→glBindBuffer (m_OffsetIndex GL_ARRAY_BUFFER也); OpenGLCore→glEnableVertexAttribArray (m_OffsetLocation); OpenGLCore→glVertexAttribPointer (m_OffsetLocation,, 3,, GL_FLOAT,, GL_FALSE,, \ sizeof(浮动),*,3,(void *) 0); OpenGLCore→glBindBuffer (GL_ARRAY_BUFFER, 0); OpenGLCore→glVertexAttribDivisor (m_OffsetLocation, 1);
调用绘制指令:
OpenGLCore→glDrawElementsInstanced (m_VertexCount, GL_TRIANGLES, \ ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,GL_UNSIGNED_INT 0,, 3);