所以我迭代了几个网格,每个网格都存储在自己的GL_ARRAY_BUFFER中,但两者共享相同的glVertexAttribPointer结构。我在for循环中遍历每个渲染/绘制传递:
if (sConfig.mRendering.isRenderTypeActive(RENDER_TYPE_SINGLE_MESH) ||
sConfig.mRendering.isRenderTypeActive(RENDER_TYPE_TWO_MESH)) {
const int mesh_count = sConfig.mRendering.isRenderTypeActive(RENDER_TYPE_TWO_MESH) ? 2 : 1;
for (int i = 0; i < mesh_count; i++) {
std::lock_guard<std::mutex> lock(mModelDataMutex[i]);
mUniV_VertexTransform->setValue(mModelTransform[i]);
glBindBuffer(GL_ARRAY_BUFFER, mVBOs[i]); CHECK_GL_ERR;
glDrawArrays(GL_TRIANGLES, 0, (GLsizei)mVertexCount[i]); CHECK_GL_ERR;
}
}
使用以下函数调用每个VBO的数据:
bool ::updateModelData(const RenderCloud& data, VERTEX_BUFFER_ID id /*= VBID_MESH_0*/)
{
if (id < VBID_COUNT)
{
std::lock_guard<std::mutex> lock(mModelDataMutex[id]);
// Bind Point VBO
glBindBuffer(GL_ARRAY_BUFFER, mVBOs[id]); CHECK_GL_ERR_RET;
// Set coordinate attribute
glVertexAttribPointer(BTID_COORDS, 3, GL_FLOAT, GL_FALSE, RenderCloud::elemSize(), (GLvoid*)BT_COORDS_OFFSET); CHECK_GL_ERR_RET;
glEnableVertexAttribArray(BTID_COORDS); CHECK_GL_ERR_RET;
// Set the normals attribute
glVertexAttribPointer(BTID_NORMALS, 3, GL_FLOAT, GL_FALSE, RenderCloud::elemSize(), (GLvoid*)(BT_NORMALS_OFFSET)); CHECK_GL_ERR_RET;
glEnableVertexAttribArray(BTID_NORMALS); CHECK_GL_ERR_RET;
// Set the colors attribute
glVertexAttribPointer(BTID_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, RenderCloud::elemSize(), (GLvoid*)(BT_COLOR_OFFSET)); CHECK_GL_ERR_RET;
glEnableVertexAttribArray(BTID_COLOR); CHECK_GL_ERR_RET;
// Copy Data
glBufferData(GL_ARRAY_BUFFER, data.pts.size() * data.elemSize(), data.pts.data(), GL_STATIC_DRAW); CHECK_GL_ERR_RET;
mVertexCount[id] = data.pts.size();
// Unbind buffer
glBindBuffer(GL_ARRAY_BUFFER, 0); CHECK_GL_ERR_RET;
return true;
}
return false;
}
我可以验证两个VBO的数据是否存在,但只有for循环中的第一个VBO使用glDrawArrays()进行渲染
它们共享羞耻几何着色器,然后由延迟渲染堆栈使用它来应用效果。
每个glDrawArrays()调用是否都需要自己的几何着色器实例,只要它们共享相同的绘制缓冲区?
答案 0 :(得分:2)
它不是绑定的顶点缓冲区对象,它定义了一般顶点属性数据的数组,但它是存储在默认顶点数组对象中的状态。
当您调用glVertexAttribPointer
时,将定义通用顶点属性数据数组。如果此时绑定了数组缓冲区,则数组定义引用缓冲区对象。
这意味着您必须在绘制对象之前切换数组定义:
glBindBuffer(GL_ARRAY_BUFFER, mVBOs[id]);
glVertexAttribPointer(BTID_COORDS, 3, GL_FLOAT, GL_FALSE, RenderCloud::elemSize(), (GLvoid*)BT_COORDS_OFFSET);
glEnableVertexAttribArray(BTID_COORDS);
glVertexAttribPointer(BTID_NORMALS, 3, GL_FLOAT, GL_FALSE, RenderCloud::elemSize(), (GLvoid*)(BT_NORMALS_OFFSET));
glEnableVertexAttribArray(BTID_NORMALS);
glVertexAttribPointer(BTID_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, RenderCloud::elemSize(), (GLvoid*)(BT_COLOR_OFFSET));
glEnableVertexAttribArray(BTID_COLOR);
glDrawArrays(GL_TRIANGLES, 0, (GLsizei)mVertexCount[i]);
glBindBuffer(GL_ARRAY_BUFFER, 0);
但我建议使用Vertex Array Object。顶点数组对象存储通用顶点数组定义的状态:
GLuint vao[VBID_COUNT];
....
glGenVertexArrays( 1, vao[i] );
glBindVertexArray( vao[i] );
glBindBuffer(GL_ARRAY_BUFFER, mVBOs[id]);
glVertexAttribPointer(BTID_COORDS, 3, GL_FLOAT, GL_FALSE, RenderCloud::elemSize(), (GLvoid*)BT_COORDS_OFFSET);
glEnableVertexAttribArray(BTID_COORDS);
glVertexAttribPointer(BTID_NORMALS, 3, GL_FLOAT, GL_FALSE, RenderCloud::elemSize(), (GLvoid*)(BT_NORMALS_OFFSET));
glEnableVertexAttribArray(BTID_NORMALS);
glVertexAttribPointer(BTID_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, RenderCloud::elemSize(), (GLvoid*)(BT_COLOR_OFFSET));
glEnableVertexAttribArray(BTID_COLOR);
glBufferData(GL_ARRAY_BUFFER, data.pts.size() * data.elemSize(), data.pts.data(), GL_STATIC_DRAW);
mVertexCount[id] = data.pts.size();
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray( 0 );
glBindVertexArray( vao[i] );
glDrawArrays(GL_TRIANGLES, 0, (GLsizei)mVertexCount[i]);
glBindVertexArray( 0 );