c++ - 具有多个阴影的阴影贴图

标签 c++ c opengl mapping shadow

我已经按照本教程使用 ARB 扩展实现了阴影贴图 http://www.paulsprojects.net/tutorials/smt/smt.html 我修改了代码以添加另一个光源。我已经创建并初始化了另外 2 个矩阵(lightViewMatrix2 和 lightProjectionMatrix2)和另一个 shadowMap 纹理。 在本教程的第 3 步,绑定(bind)第一个阴影贴图纹理后,我绑定(bind)了第二个。 我得到的结果是我只看到由 2 个不同光源产生的 2 个阴影之间的交叉点。 如教程所示,我已经在 init 函数中初始化了所有矩阵,这是我的显示函数。

void Display(void){
//First pass - from light's point of view
//Calculate & save matrices LIGHT 1
glPushMatrix();
    glLoadIdentity();
    gluLookAt(  lightPosition[0], lightPosition[1], lightPosition[2],
                0.0f, 0.0f , 0.0f,
                0.0f, 1.0f, 0.0f);
    glGetFloatv(GL_MODELVIEW_MATRIX, lightViewMatrix);
glPopMatrix();

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadMatrixf(lightProjectionMatrix);

glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(lightViewMatrix);

//Use viewport the same size as the shadow map
glViewport(0, 0, shadowMapSize, shadowMapSize);

//Draw back faces into the shadow map
glCullFace(GL_FRONT);

//Disable color writes, and use flat shading for speed
glShadeModel(GL_FLAT);
glColorMask(0, 0, 0, 0);

//Draw the scene
DrawScene();

//Read the depth buffer into the shadow map texture
glBindTexture(GL_TEXTURE_2D, shadowMapTexture[0]);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, shadowMapSize, shadowMapSize);


//First pass - from light's point of view
//Calculate & save matrices LIGHT 2
glPushMatrix();
    glLoadIdentity();
    gluLookAt(  lightPosition2[0], lightPosition2[1], lightPosition2[2],
                0.0f, 0.0f , 0.0f,
                0.0f, 1.0f, 0.0f);
    glGetFloatv(GL_MODELVIEW_MATRIX, lightViewMatrix2);
glPopMatrix();

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadMatrixf(lightProjectionMatrix2);

glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(lightViewMatrix2);

//Use viewport the same size as the shadow map
glViewport(0, 0, shadowMapSize, shadowMapSize);

//Draw the scene
DrawScene();

//Read the depth buffer into the shadow map texture
glBindTexture(GL_TEXTURE_2D, shadowMapTexture[1]);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, shadowMapSize, shadowMapSize);

//restore states
glCullFace(GL_BACK);
glShadeModel(GL_SMOOTH);
glColorMask(1, 1, 1, 1);


//2nd pass - Draw from camera's point of view
glClear(GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadMatrixf(cameraProjectionMatrix);

glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(cameraViewMatrix);

glViewport(0, 0, windowWidth, windowHeight);

float white2[3]={0.2f,0.2f,0.2f};

//light to represent shadowed areas
glLightfv(GL_LIGHT1, GL_POSITION, VECTOR4D(lightPosition));
glLightfv(GL_LIGHT1, GL_AMBIENT, white2);
glLightfv(GL_LIGHT1, GL_DIFFUSE, white2);
glLightfv(GL_LIGHT1, GL_SPECULAR, black1);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHTING);

DrawScene();


//3rd pass
//Draw with bright light LIGHT1
glLightfv(GL_LIGHT1, GL_DIFFUSE, white1);
glLightfv(GL_LIGHT1, GL_SPECULAR, white1);

//Calculate texture matrix for projection
//This matrix takes us from eye space to the light's clip space
//It is postmultiplied by the inverse of the current view matrix when specifying texgen
static MATRIX4X4 biasMatrix(0.5f, 0.0f, 0.0f, 0.0f,
                            0.0f, 0.5f, 0.0f, 0.0f,
                            0.0f, 0.0f, 0.5f, 0.0f,
                            0.5f, 0.5f, 0.5f, 1.0f);    //bias from [-1, 1] to [0, 1]

MATRIX4X4 textureMatrix=biasMatrix*lightProjectionMatrix*lightViewMatrix;

//Set up texture coordinate generation.
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGenfv(GL_S, GL_EYE_PLANE, textureMatrix.GetRow(0));
glEnable(GL_TEXTURE_GEN_S);

glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGenfv(GL_T, GL_EYE_PLANE, textureMatrix.GetRow(1));
glEnable(GL_TEXTURE_GEN_T);

glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGenfv(GL_R, GL_EYE_PLANE, textureMatrix.GetRow(2));
glEnable(GL_TEXTURE_GEN_R);

glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGenfv(GL_Q, GL_EYE_PLANE, textureMatrix.GetRow(3));
glEnable(GL_TEXTURE_GEN_Q);

//Bind & enable shadow map texture
glBindTexture(GL_TEXTURE_2D, shadowMapTexture[0]);
glEnable(GL_TEXTURE_2D);

//Enable shadow comparison
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE);

//Shadow comparison should be true (ie not in shadow) if r<=texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);

//Shadow comparison should generate an INTENSITY result
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);

//Set alpha test to discard false comparisons
glAlphaFunc(GL_GEQUAL, 0.99f);
glEnable(GL_ALPHA_TEST);

DrawScene();

//3rd pass
//Draw with bright light LIGHT2

MATRIX4X4 textureMatrix2=biasMatrix*lightProjectionMatrix2*lightViewMatrix2;

//Set up texture coordinate generation.
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGenfv(GL_S, GL_EYE_PLANE, textureMatrix2.GetRow(0));
glEnable(GL_TEXTURE_GEN_S);

glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGenfv(GL_T, GL_EYE_PLANE, textureMatrix2.GetRow(1));
glEnable(GL_TEXTURE_GEN_T);

glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGenfv(GL_R, GL_EYE_PLANE, textureMatrix2.GetRow(2));
glEnable(GL_TEXTURE_GEN_R);

glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGenfv(GL_Q, GL_EYE_PLANE, textureMatrix2.GetRow(3));
glEnable(GL_TEXTURE_GEN_Q);

//Bind & enable shadow map texture
glBindTexture(GL_TEXTURE_2D, shadowMapTexture[1]);
glEnable(GL_TEXTURE_2D);

//Enable shadow comparison
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE);

//Shadow comparison should be true (ie not in shadow) if r<=texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);

//Shadow comparison should generate an INTENSITY result
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);

//Set alpha test to discard false comparisons
glAlphaFunc(GL_GEQUAL, 0.99f);
glEnable(GL_ALPHA_TEST);

DrawScene();

//Disable textures and texgen
glDisable(GL_TEXTURE_2D);

glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glDisable(GL_TEXTURE_GEN_R);
glDisable(GL_TEXTURE_GEN_Q);

//Restore other states
glDisable(GL_LIGHTING);
glDisable(GL_ALPHA_TEST);


//Set matrices for ortho
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(-1.0f, 1.0f, -1.0f, 1.0f);

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

//reset matrices
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();

glFinish();
glutSwapBuffers();}

最佳答案

您在这里所做的是首先为第一个灯光生成阴影贴图,然后为第二个灯光生成阴影贴图,同时与第一个阴影贴图进行比较。结果,您最终得到的阴影是两者的某种组合。 您需要以与生成第一个阴影相同的方式生成第二个阴影(每个光的阴影生成应该独立于所有其他光),最后因为您需要使用一些多重纹理以某种方式进行相同的比较检查每个 z 纹理独立。

考虑使用基于着色器的方法,因为所有这些固定函数调用都已过时并且与着色器相比没有优势。我读到的关于多光阴影贴图的最好文章是 Daniel Rákos 的“大量阴影转换光与分层渲染”。它提供了一种非常有效的方法。

关于c++ - 具有多个阴影的阴影贴图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16387210/

相关文章:

c++ - 对 vector 使用 std::move

c - 将结构传递给函数会出现 'undefined reference to' 错误

c - pthread_mutex_lock() 处的段错误

c++ - 使用 OpenGL,如何在默认投影中正确使用 gluOrtho2D?

c++ - 声明与类型不兼容

c++ - c++模板规范和重载的解析

linux - 使用 FireBreath 在浏览器插件中使用 openGL

c++ - 在 OpenGL 中将纹理映射到球体时出现接缝问题

c++ - 从继承的模板类中正确实现虚函数

C 基础 - 变量和指针的问题