我有一个由很多对象组成的场景,当我渲染它们时,我会遍历场景中的所有项目并在它们上调用 glDrawElements。这工作正常。但是,现在我正在实现场景的阴影映射,并且使用相同的方式绘制到阴影缓冲区。但是,只有场景中的第一个元素以正确的方式绘制到纹理,我通过将深度纹理附加到场景元素来检查这一点,您将在下面看到。
代码如下:
glBindFramebuffer(GL_FRAMEBUFFER, vFrameBuffers[ShadowFB]);
glViewport(0,0,1024,1024);
glClearDepth(1.0f);
glClear(GL_DEPTH_BUFFER_BIT);
glUseProgram(shadowShader);
depth_loc = glGetUniformLocation(shadowShader, "depthProj");
model_view_loc = glGetUniformLocation(shadowShader, "modelmat");
glUniformMatrix4fv(depth_loc, 1, GL_FALSE, depthproj);
depthproj = mat4(1.0);
depthproj = Ortho(-250.0f, 250.0f, -200.0f, 200.0f, -200.0f, 200.0f);
depthproj = depthproj * RotateX(15);
depthproj = depthproj * RotateY(220);
for (int i = 0; i < _objectArray.size(); i++)
{
modelmat = mat4(1.0);
modelmat = modelmat*Translate(_objectArray[i]->getLocation()->getX(),
_objectArray[i]->getLocation()->getY(),
_objectArray[i]->getLocation()->getZ());
glUniformMatrix4fv(model_view_loc, 1, GL_FALSE, modelmat);
if (_objectArray[i]->getType() == TypeMountain)
{
glBindVertexArray(vArrays[ShadowMountainA]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vElements[ShadowMountainE]);
glDrawElements(GL_TRIANGLES, 40*40*6, GL_UNSIGNED_SHORT, NULL);
glBindVertexArray(0);
}
else if (_objectArray[i]->getType() == TypePasture)
{
glBindVertexArray(vArrays[ShadowPastureA]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vElements[ShadowPastureE]);
glDrawElements(GL_TRIANGLES, 40*40*6, GL_UNSIGNED_SHORT, NULL);
glBindVertexArray(0);
}
else if (_objectArray[i]->getType() == TypeWater)
{
glBindVertexArray(vArrays[ShadowWaterA]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vElements[ShadowWaterE]);
glDrawElements(GL_TRIANGLES, 40*40*6, GL_UNSIGNED_SHORT, NULL);
glBindVertexArray(0);
}
else if (_objectArray[i]->getType() == TypeTree)
{
glBindVertexArray(vArrays[ShadowTreeA]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vElements[ShadowTreeE]);
glDrawElements(GL_TRIANGLES, 180, GL_UNSIGNED_SHORT, NULL);
glBindVertexArray(0);
}
}
glUseProgram(0);
当我只在山对象上运行它时,这是我得到的结果图像,你可以看到它是正确的。 它也单独适用于其他场景元素。我的问题是如何使用多次绘制在同一个缓冲区中毫无问题地绘制?
编辑:只是因为这里要求的是阴影着色器组件:
顶点着色器:
#version 330 core
uniform mat4 depthProj;
uniform mat4 modelmat;
layout(location = 0) attribute vec4 vPosition;
void main()
{
gl_Position = depthProj * modelmat * vPosition;
}
片段着色器:
#version 330 core
layout(location = 0) out float fragmentDepth;
void main()
{
}