我使用固定功能管道而不是着色器在OpenGL中进行阴影贴图。我已经成功地实现了阴影贴图,但是在阴影投射的东西上没有纹理。
当我在渲染之前将纹理添加到我的球体时,它看起来是扭曲的。我可以在我的光源周围移动,它会根据光源的位置更多/更少地扭曲它。
可能的原因是什么?
这是我的glLightfv(GL_LIGHT1,GL_DIFFUSE,白色); glLightfv(GL_LIGHT1,GL_SPECULAR,白色);
//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);
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);
glColor4f(1.0f, 0.0f, 1.0f, 1.0f);
world->textured = true;
renderObjects();
sun->center = Vertex(lightX, lightY, lightZ);
glDisable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
glColor3f(1,1,0);
glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE ) ;
glTranslatef(sun->center.x, sun->center.y, sun->center.z);
sun->lighting = false;
sun->z = -lightZ +1;
sun->render();
glColor3f(1, 1, 1);
glDisable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
//Disable textures and texgen
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);
//reset matrices
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
update();
渲染对象:
void Widget::renderObjects()
{
glPushMatrix();
glColor4f(1.0, 1.0, 1.0, 1.0);
glPushMatrix();
//glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, emission);
//glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
arcBall->q.rotate();
world->render();
glPushMatrix();
torso->frame = frame;
torso->draw();
glPushMatrix();
renderParticles();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
}
对于踢球,这是我的第一个&amp;第二遍也是。
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
lightPosition = VECTOR3D(lightX, lightY, lightZ);
glLoadIdentity();
gluLookAt( lightPosition.x, lightPosition.y, lightPosition.z,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
glGetFloatv(GL_MODELVIEW_MATRIX, lightViewMatrix);
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
renderObjects();
//Read the depth buffer into the shadow map texture
glBindTexture(GL_TEXTURE_2D, shadowMapTexture);
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);
//Use dim light to represent shadowed areas
glLightfv(GL_LIGHT1, GL_POSITION, VECTOR4D(lightPosition));
glLightfv(GL_LIGHT1, GL_AMBIENT, white*0.2f);
glLightfv(GL_LIGHT1, GL_DIFFUSE, white*0.2f);
glLightfv(GL_LIGHT1, GL_SPECULAR, black);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHTING);
renderObjects();
答案 0 :(得分:0)
由于您使用固定功能管道进行此操作,我想您正在使用纹理矩阵(每纹理图像单元状态)。
当您尝试从常规纹理中采样时,您确定纹理矩阵是否已正确设置?听起来像你使用与你的阴影贴图一样的漫反射贴图使用相同的矩阵。通常,在对阴影贴图以外的所有内容进行采样时,您需要使用标识矩阵。
glActiveTexture (GL_TEXTURE0);
glMatrixMode (GL_TEXTURE);
glLoadMatrixf (shadow_matrix);
glBindTexture (shadow_map);
// Draw shadow pass
glBindTexture (diffuse_map);
// Draw the base diffuse pass
在此示例中,第二次绘制仍然受到必须为阴影贴图加载的矩阵的影响。您可以通过以下两种方式解决: