我正在尝试编写适用于GLESv2 / OpenGLv3的代码。 这是一个地形绘制功能。 网格甚至不在视口中显示。我们认为,OpenGLv3不会崩溃。 而且我正尝试与GLESv2向后兼容,因此VAO无法在其中使用。 GLESv2中可用的最大索引为65536,因此我将512x512纹理分为4个部分。 glDrawElementsBaseVertex需要GLESv3,所以我不能使用它。 我相信指针需要乘以sizeof,其他args则不需要。 我在vbo中有顶点和法线坐标,所以它是6个浮点数,每个3个。 UV使用xy顶点。 四边形是6个索引。
void GLEntity::nouv_pointer(GLint vertex, GLint normal, GLint uv, uintptr_t offset) {
uintptr_t offset_=offset*6;
if(vertex!=-1) glVertexAttribPointer(
vertex, // attribute
3, // number of elements per vertex, here (x, y, z)
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is
3*sizeof(GLfloat), // 3 extra data entries between each position
(GLvoid*)(offset_*sizeof(GLfloat)) // offset of the first element
);
if(normal!=-1) glVertexAttribPointer(
normal, // attribute
3, // number of elements per vertex, here (x, y, z)
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is
3*sizeof(GLfloat), // 3 extra data entries between each position
(GLvoid*)((3+offset_)*sizeof(GLfloat)) // offset of the first element
);
if(uv!=-1) glVertexAttribPointer(
uv, // attribute
2, // number of elements per vertex, here (x, y, z)
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is
4*sizeof(GLfloat), // 4 extra data entries between each position
(GLvoid*)(offset_*sizeof(GLfloat)) // offset of the first element
);
}
void GLEntity::draw(GLint vertex, GLint normal, GLint uv) {
if(placebo) {
SDL_Log("Trying to draw uninitialized object\n");
return;
}
glBindBuffer(GL_ARRAY_BUFFER, vtnBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer);
if(vertex!=-1) glEnableVertexAttribArray(vertex);
if(normal!=-1) glEnableVertexAttribArray(normal);
if(uv!=-1) glEnableVertexAttribArray(uv);
uintptr_t basevertex=0;
//nouv_pointer(vertex, normal, uv, 0);
//glDrawArrays(GL_TRIANGLES, 0, x_top_block*y_top_block*65536);
for(unsigned int y_block=0;y_block<y_top_block;y_block++) {
for(unsigned int x_block=0;x_block<x_top_block;++x_block) {
nouv_pointer(vertex, normal, uv, basevertex);
glDrawElements(GL_TRIANGLES,
ibos_,
GL_UNSIGNED_SHORT,
(GLvoid*)0);
basevertex+=65536;
}
basevertex+=last_block_size_x*y_bs;
}
basevertex=65536*x_top_block;
for(unsigned int y_block=0;y_block<y_top_block;y_block++) {
nouv_pointer(vertex, normal, uv, basevertex);
glDrawElements(GL_TRIANGLES,
ibos_x,
GL_UNSIGNED_SHORT,
(GLvoid*)(ibos_*sizeof(GLushort)));
basevertex+=65536*x_top_block+last_block_size_x;
}
basevertex=(65536*x_top_block+last_block_size_x)*y_top_block;
for(unsigned int x_block=0;x_block<x_top_block;++x_block) {
nouv_pointer(vertex, normal, uv, basevertex);
glDrawElements(GL_TRIANGLES,
ibos_y,
GL_UNSIGNED_SHORT,
(GLvoid*)((ibos_+ibos_x)*sizeof(GLushort)));
basevertex+=x_bs*last_block_size_y;
}
basevertex=(65536*x_top_block+last_block_size_x)*y_top_block+x_bs*last_block_size_y*x_top_block;
nouv_pointer(vertex, normal, uv, basevertex);
glDrawElements(GL_TRIANGLES,
ibos_xy,
GL_UNSIGNED_SHORT,
(GLvoid*)((ibos_+ibos_x+ibos_y)*sizeof(GLushort)));
}