使用正向渲染的阴影映射的基本思想(glsl 1.2)

时间:2014-03-21 06:28:13

标签: opengl glsl

好吧,让我们看看我是否理解阴影映射背后的想法。

1.- I must create the same number of FBOs that lights (maximum 8).
2.- I must create the same number of depth textures (shadow maps) that FBOs.
3.- For every FBO I must perform offscreen render (render to texture, drawing the scene from the point of view of each of the lights, maximum 8 times).

到此为止我是对的?

嗯,但现在我如何绘制最终场景+阴影?我是否必须为每个阴影贴图绘制?

main(){

    //Number of lights = 8;

    glUseProgram(programa);
      glUniformli(shadowM0,4);
      glActivateTexture(GL_TEXTURE4);
      glBindTexture(GL_TEXTURE_2D,depthT0);
      //Draw Escene?

      glUniformli(shadowM1,5);
      glActivateTexture(GL_TEXTURE5);
      glBindTexture(GL_TEXTURE_2D,depthT1);
      //Draw Escene?

      glUniformli(shadowM2,6);
      glActivateTexture(GL_TEXTURE6);
      glBindTexture(GL_TEXTURE_2D,depthT2);
      //Draw Escene?

      ...
    glUseProgram(0);

    ...  
}

如果不是这样,我该如何进行场景的最终渲染?

如果上述任何步骤我错过了某些内容,或者我可以通过正向渲染为我提供改进想法的建议,也会感激不尽。

1 个答案:

答案 0 :(得分:2)

不止一种方式。

最简单的解决方案是 - 只使用一个shadowmap / FBO

foreach light {
  render shadowmap;
  foreach object {
    calculate lighting for given light with given shadowmap;
    additively blend with framebuffer;
  }
}

添加剂我的意思是glBlendFunc(GL_ONE, GL_ONE);

其他方法是一次计算多个灯,但它引入了其他问题,例如如果着色器需要计算8个灯,但是你只有7个?如果你有16,你需要画两次(像往常一样使用添加剂混合)。

glUseProgram(programa);
  glUniformli(shadowM0,4);
  glActivateTexture(GL_TEXTURE4);
  glBindTexture(GL_TEXTURE_2D,depthT0);

  glUniformli(shadowM1,5);
  glActivateTexture(GL_TEXTURE5);
  glBindTexture(GL_TEXTURE_2D,depthT1);

  glUniformli(shadowM2,6);
  glActivateTexture(GL_TEXTURE6);
  glBindTexture(GL_TEXTURE_2D,depthT2);

  // ... <set all other shadowmaps>

  // now draw. once. and use all shadowmaps you've set in shader