我将基于OpenGL 3.3(“台式机”)的游戏引擎移植到OpenGL ES 3.2 (“移动”)时遇到问题。虽然所有内容在桌面上均可完美运行,但在移动设备上,所有内容均可正常运行,但阴影贴图。
我的问题很简单:有人可以在我共享的代码中发现问题,还是指出正确的方向?
我非常简单的光照测试级别已在桌面上正确呈现。中间列的右侧有一个黄色(不可见)的光,因此阴影应投射到左侧,如以下桌面屏幕截图所示:
但这在手机上是非常错误的:
如您所见,手机上的阴影是“完全错误的”。它应该像在桌面上一样位于左侧,但相反,它的方向错误,看上去“被砍掉了”。
创建CUBE MAP纹理以保留阴影的代码:
static GLuint r_createShadowmapTexture() {
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
for (unsigned int i = 0; i < 6; ++i)
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_DEPTH_COMPONENT32F, r_shadowSize, r_shadowSize, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_COMPARE_FUNC, GL_GREATER);
return tex;
}
为阴影贴图设置帧缓冲区的代码:
glGenFramebuffers(1, &r_shadowDepthMapFBO);
glBindFramebuffer(GL_FRAMEBUFFER, r_shadowDepthMapFBO);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
渲染阴影贴图的代码:
static struct {
vec3 centerOffset, up;
} const g_shadowMapTransforms[6] = {
{ {1.0f, 0.0f, 0.0f}, {0.0f, -1.0f, 0.0f} },
{ {-1.0f, 0.0f, 0.0f}, {0.0f, -1.0f, 0.0f} },
{ {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 1.0f} },
{ {0.0f, -1.0f, 0.0f}, {0.0f, 0.0f, -1.0f} },
{ {0.0f, 0.0f, 1.0f}, {0.0f, -1.0f, 0.0f} },
{ {0.0f, 0.0f, -1.0f}, {0.0f, -1.0f, 0.0f} },
};
static void r_shadowMapPass(const point_light_t* light, GLuint cubemapTexture) {
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glDepthMask(GL_TRUE);
glBindFramebuffer(GL_FRAMEBUFFER, r_shadowDepthMapFBO);
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, cubemapTexture, 0);
const float farPlane = min(r_far, light->radius);
mat4x4 shadowProj;
mat4x4_perspective(shadowProj, (float)M_PI_2, 1.0f, r_near, farPlane);
mat4x4 shadowVP[6];
for (int i = 0; i < 6; ++i) {
vec3 center;
vec3_add(center, light->origin, g_shadowMapTransforms[i].centerOffset);
mat4x4 view;
mat4x4_look_at(view, (float*)light->origin, center, (float*)g_shadowMapTransforms[i].up);
mat4x4_mul(shadowVP[i], shadowProj, view);
}
rtech_shadowmap_enable();
rtech_shadowmap_setLightPos((float*)light->origin);
rtech_shadowmap_setFarPlane(farPlane);
rtech_shadowmap_setShadowMatrices(shadowVP);
render_entity_t* re = r_entities;
glClear(GL_DEPTH_BUFFER_BIT);
for (uint32_t i = 0; i < r_numEntities; ++i, ++re) {
if (!re->castsShadow)
continue;
rtech_shadowmap_setModelMatrix(re->m);
if (re->hasSolid) {
const render_solids_t* const rs = r_solids + re->index;
for (uint32_t j = 0; j < rs->count; ++j) {
const uint32_t ao = rs->offset + j;
r_renderBlock(ao, rs, j);
}
} else {
render_model_t* const rm = &r_models[re->index];
for (uint32_t j = 0; j < rm->count; ++j)
r_renderArrayObject(rm->arrayObjects[j], rm->numIndices[j]);
}
}
glDepthMask(GL_FALSE);
}
在桌面上,我为所有着色器(顶点,几何和片段)添加以下前缀:
#version 330 core
在移动设备上,我为所有着色器添加了以下前缀:
#version 320 es
precision highp float;
precision highp int;
precision highp sampler2D;
precision highp samplerCubeShadow;
shadowmap顶点着色器:
layout (location = 0) in vec3 aPos;
uniform mat4 gModel;
void main()
{
gl_Position = gModel * vec4(aPos, 1.0);
}
阴影贴图几何着色器:
layout (triangles) in;
layout (triangle_strip, max_vertices=18) out;
uniform mat4 gShadowMatrices[6];
out vec4 FragPos; // FragPos from GS (output per emitvertex)
void main()
{
for(int face = 0; face < 6; ++face)
{
gl_Layer = face; // built-in variable that specifies to which face we render.
for(int i = 0; i < 3; ++i) // for each triangle's vertices
{
FragPos = gl_in[i].gl_Position;
gl_Position = gShadowMatrices[face] * FragPos;
EmitVertex();
}
EndPrimitive();
}
}
shadowmap片段着色器:
in vec4 FragPos;
uniform vec3 gLightPos;
uniform float gFarPlane;
void main()
{
// get distance between fragment and light source
float lightDistance = length(FragPos.xyz - gLightPos);
// map to [0;1] range by dividing by gFarPlane
lightDistance = lightDistance / gFarPlane;
// write this as modified depth
gl_FragDepth = lightDistance;
}
实际上使用阴影贴图的点光通道的摘录:
...
uniform samplerCubeShadow gShadowCubeMap;
uniform float gFarPlane;
...
float ShadowCalcSinglePass(vec3 fragPos, vec3 lightPos) {
vec3 fragToLight = fragPos - lightPos;
float currentDepth = (length(fragToLight) - (0.005 * gFarPlane)) / gFarPlane;
return texture(gShadowCubeMap, vec4(fragToLight, currentDepth));
}
我知道这是一个很开放的问题,涉及很多代码。我正在尝试提供必要的相关信息,如果需要或要求,我可以分享更多信息!
按照@MichaelKenzel的建议,我尝试将阴影贴图渲染到屏幕上。如下所示,它非常适合桌面。但是在移动设备上,这显示了“全红”阴影贴图,(由于我的着色器执行1.0-采样),因此Texture()函数为每个像素返回0。但是,从上面的“错误”图像可以看出,某处存在阴影,因此以这种方式读取深度缓冲区似乎是一个问题。 注意:下面的图像与上面的图像来自不同的测试级别,其中一个阴影更复杂
in vec2 TexCoord0;
uniform samplerCube gTexture;
uniform int gSide;
out vec4 FragColor;
void main()
{
vec3 vec;
switch (gSide) {
case 0: vec = vec3(1.0, TexCoord0.xy); break;
case 1: vec = vec3(-1.0, TexCoord0.xy); break;
case 2: vec = vec3(TexCoord0.x, 1.0, TexCoord0.y); break;
case 3: vec = vec3(TexCoord0.x, -1.0, TexCoord0.y); break;
case 4: vec = vec3(TexCoord0.xy, 1.0); break;
case 5: vec = vec3(TexCoord0.xy, -1.0); break;
default: vec = vec3(1.0, 0.0, 0.0); break;
}
FragColor = vec4(vec3(1.0, 0.0, 0.0) * (1.0 - texture(gTexture, vec).r), /*alpha*/ 1.0);
}
渲染阴影贴图的代码调试四边形:
void r_debugPassShadowMap2() {
if (!r_shadowmapDebug.initialized) {
r_shadowmapDebug.initialized = true;
const float margin = 16;
const float mapWidth = (r_windowWidth - (margin * 7.f)) / 6.f;
mat4x4 t, s;
mat4x4_translate(t, 1, 1, 0); // quad verts range from -1x-1x0 to 1x1x0
mat4x4_identity(s);
s[0][0] = s[1][1] = s[2][2] = mapWidth / 2;
mat4x4 m;
mat4x4_mul(m, s, t);
mat4x4 p;
mat4x4_ortho(p, 0, (float)r_windowWidth, (float)r_windowHeight, 0, -1, 1);
for (uint32_t i = 0; i < 6; ++i) {
mat4x4 v;
mat4x4_translate(v, margin + (margin + mapWidth) * i, margin, 0);
mat4x4 mv;
mat4x4_mul(mv, v, m);
mat4x4_mul(r_shadowmapDebug.sideWVP[i], p, mv);
}
}
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glDisable(GL_BLEND);
rtech_shadowmapdebug_enable();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, r_shadowmapDebug.texture);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_COMPARE_MODE, GL_NONE);
for (uint32_t i = 0; i < 6; ++i) {
rtech_shadowmapdebug_setWVP(r_shadowmapDebug.sideWVP[i]);
rtech_shadowmapdebug_setSide(i);
glBindVertexArray(r_quadAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, NULL);
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
}
由于@MorrisonChang的链接,我尝试了一些不同的尝试,并且产生了很大的不同:我已经将GL_TEXTURE_MAG_FILTER和GL_TEXTURE_MIN_FILTER设置为GL_LINEAR以获得阴影贴图纹理。当我将其更改为GL_NEAREST时,我突然在移动设备上获得了更多结果!但是,如下图所示,由于某种原因,它只能渲染到一张脸,而不是全部渲染到6张!但至少我走得更远!
答案 0 :(得分:1)
我找到了答案,并且(对我来说)这令人困惑,但我发现它是符合规范的。从此更改几何着色器后,我立即使它起作用:
void main()
{
for(int face = 0; face < 6; ++face)
{
gl_Layer = face; // built-in variable that specifies to which face we render.
for(int i = 0; i < 3; ++i) // for each triangle's vertices
{
FragPos = gl_in[i].gl_Position;
gl_Position = gShadowMatrices[face] * FragPos;
EmitVertex();
}
EndPrimitive();
}
}
对此:
void emitFace(mat4 m) {
for(int i = 0; i < 3; ++i)
{
FragPos = gl_in[i].gl_Position;
gl_Position = m * FragPos;
EmitVertex();
}
EndPrimitive();
}
void main()
{
gl_Layer = 0;
emitFace(gShadowMatrices[0]);
gl_Layer = 1;
emitFace(gShadowMatrices[1]);
gl_Layer = 2;
emitFace(gShadowMatrices[2]);
gl_Layer = 3;
emitFace(gShadowMatrices[3]);
gl_Layer = 4;
emitFace(gShadowMatrices[4]);
gl_Layer = 5;
emitFace(gShadowMatrices[5]);
}
显然,通过for循环变量分配给gl_Layer在OpenGL ES中不起作用!
在此页面https://www.khronos.org/registry/OpenGL-Refpages/es3/html/gl_Layer.xhtml上
如果着色器为gl_Layer静态分配了一个值,则将启用分层渲染模式。
它也说:
如果几何体阶段未对gl_Layer进行静态分配,则片段阶段中的输入gl_Layer将为零。
但是它没有说“永远不应该为gl_Layer分配一个非静态值”,并且编译器也没有发出警告!
在OpenGL Core中,规范实际上说的完全相同:https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/gl_Layer.xhtml
但是,无论如何,我的NVIDIA驱动程序都支持对gl_Layer的动态分配,因为这样做了,所以我从未意识到这是错误的...