OpenGL - 使用立方体阴影贴图渲染点光源?

时间:2015-09-16 15:02:26

标签: c++ opengl lighting shadow-mapping

我在渲染立方体阴影贴图时遇到问题。消极和积极似乎被逆转,我无法弄清楚为什么。试图扭转目标和方向,但我得到了事件更奇怪的输出。

代码:

// init
glGenTextures(1, &mShadowCubeTex);
glBindTexture(GL_TEXTURE_CUBE_MAP, mShadowCubeTex);
for(int i = 0; i < 6; i++)
  glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_DEPTH_COMPONENT24, SHADOW_WIDTH, SHADOW_WIDTH, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
/* + filters + wrapping */
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);

glGenFramebuffers(1, &mShadowFbo);
glBindFramebuffer(GL_FRAMEBUFFER, mShadowFbo);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_COMPONENT, mShadowCubeTex, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);

// render
static const struct {
  GLenum eTarget;
  vec3   vDirection;
  vec3   vUp;
} oPointLightConfigs[6] = {
  { GL_TEXTURE_CUBE_MAP_POSITIVE_X, vec3( 1.0f, 0.0, 0.0f), vec3(0.0f, 1.0f, 0.0f) },
  { GL_TEXTURE_CUBE_MAP_NEGATIVE_X, vec3(-1.0f, 0.0, 0.0f), vec3(0.0f, 1.0f, 0.0f) },
  { GL_TEXTURE_CUBE_MAP_POSITIVE_Y, vec3( 0.0f, 1.0, 0.0f), vec3(0.0f, 0.0f,-1.0f) },
  { GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, vec3( 0.0f,-1.0, 0.0f), vec3(0.0f, 0.0f, 1.0f) },
  { GL_TEXTURE_CUBE_MAP_POSITIVE_Z, vec3( 0.0f, 0.0, 1.0f), vec3(0.0f, 1.0f, 0.0f) },
  { GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, vec3( 0.0f, 0.0,-1.0f), vec3(0.0f, 1.0f, 0.0f) }
};

// shadow pass
for(int i = 0; i < 6; i++)
{
  glBindFramebuffer(target, mShadowFbo);
  glViewport(0, 0, SHADOW_WIDTH, SHADOW_WIDTH);
  glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, oPointLightConfigs[i].eTarget, mShadowCubeTex, 0);

  glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
  glDrawBuffer(GL_NONE);
  glReadBuffer(GL_NONE);
  glCullFace(GL_FRONT);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  for(CModel* pModel : mModels)
  {
    if(pModel->hasOption(NO_SHADOW_CAST))
    {
      mNullProgram->use(); // no color output

      mat4 M = pModel->getModelMatrix();
      mat4 V = lookAt(pPointLight->getPosition(), oPointLightConfigs[i].vDirection, oPointLightConfigs[i].vUp);
      mat4 P = mCamera->getProjectionMatrix();

      pNullProgram->set("MVP", P * V * M);

      pModel->draw();
    }
  }
}

// color pass
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDrawBuffer(GL_BACK);
glReadBuffer(GL_BACK);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

pLightProgram->use();

glActiveTexture(GL_TEXTURE6); 
glBindTexture(GL_TEXTURE_2D, mShadowCubeTex);

pLightProgram->set("V", mCamera->getViewMatrix());
pLightProgram->set("P", mCamera->getProjectionMatrix());

/* set program light uniforms */

for(CModel* pModel : mModels)
{
  pLightProgram->set("M", pModel->getModelMatrix());

  pModel->draw()
}


// lighting fragment shader
float computeShadowFactor(in samplerCube sShadowMap, in vec3 vL) {
  /* vL = vLightPosition - vVertexPosition */
  vec3 vLAbs = abs(vL);
  float fNear = 0.1;
  float fFar  = 1000.0;
  float fDepth = max(vLAbs.x, max(vLAbs.y, vLAbs.z));
  fDepth = ((fFar + fNear) / (fFar - fNear)) - ((2.0 * fFar * fNear) / (fFar - fNear)) / fDepth;
  fDepth = (fDepth + 1.0) * 0.5;

  float fShadow = texture(sShadowMap, -vL).r; /* -vL = vVertexPosition - vLightPosition */
  if(fDepth > fShadow) 
    return 0.0;
  return 1.0;
}

输出:

enter image description here

enter image description here

更新

我将+ oPointLightConfigs [i] .vDirection添加到...

V = lookat(pPointLight->mPosition, pPointLight->mPosition + oPointLightConfigs[i].vDirection, oPointLightConfigs[i].vUp);

输出:

enter image description here

至少现在我得到一个阴影(而不是6个)。但它以某种方式反转,Y轴没有阴影。当阴影从一个立方体贴图面转到另一个立方体贴图面时,它会被中断任何提示?

1 个答案:

答案 0 :(得分:0)

这些问题是由立方体映射中反转的Y引起的。

我用过:

{ GL_TEXTURE_CUBE_MAP_POSITIVE_Y, math::vec3( 0.0f,-1.0, 0.0f), math::vec3(0.0f, 0.0f, 1.0f) },
{ GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, math::vec3( 0.0f, 1.0, 0.0f), math::vec3(0.0f, 0.0f, 1.0f) },

用于设置阴影立方体贴图和:

vL.y *= -1.0;
在对阴影立方体贴图纹理进行采样之前,

在computeShadowFactor()内部。

注意:我的+ Z轴进入监视器 - 左手坐标系。