在具有最新 opengl 的 windows7 和具有 freeglut3 应用程序的 Ubuntu 12.04 中,我可以使用阴影体积方法正确绘制阴影。但是,当我在旧版本的 Debian GNU/Linux 6.0.6 上运行我的代码时,无法正确绘制阴影,不幸的是我应该绘制这些阴影 :(
我认为我有剔除问题,因为当我绘制彩色阴影体积时,我可以正确看到体积。但是,剔除体积的正面和背面会产生问题,并且无法绘制阴影。我对阴影一无所知。
这是我的投射阴影代码:
glDisable(GL_LIGHTING);
glDepthMask(GL_FALSE);
glDepthFunc(GL_LEQUAL);
glEnable(GL_STENCIL_TEST);
glColorMask(0, 0, 0, 0);
glStencilFunc(GL_ALWAYS, 1, 0xffffffff);
// first pass, stencil operation decreases stencil value
glFrontFace(GL_CCW);
glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
for (int i = 0; i < triangles.size(); ++i)
if (triangles[i]->visible)
for (int j = 0; j < 3; ++j)
{
Triangle *k = triangles[i]->neigh[j];
if ((k == NULL) || (!k->visible))
{
// here we have an edge, we must draw a polygon
p1 = triangles[i]->p[j];
p2 = triangles[i]->p[(j+1)%3];
//calculate the length of the vector
v1.x = (p1.x - wlp[0])*100;
v1.y = (p1.y - wlp[1])*100;
v1.z = (p1.z - wlp[2])*100;
v2.x = (p2.x - wlp[0])*100;
v2.y = (p2.y - wlp[1])*100;
v2.z = (p2.z - wlp[2])*100;
//draw the polygon
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(p1.x, p1.y, p1.z);
glVertex3f(p1.x + v1.x, p1.y + v1.y, p1.z + v1.z);
glVertex3f(p2.x, p2.y, p2.z);
glVertex3f(p2.x + v2.x, p2.y + v2.y, p2.z + v2.z);
glEnd();
}
}
// second pass, stencil operation increases stencil value
glFrontFace(GL_CW);
glStencilOp(GL_KEEP, GL_KEEP, GL_DECR);
for (int i = 0; i < triangles.size(); ++i)
if (triangles[i]->visible)
for (int j = 0; j < 3; ++j)
{
Triangle *k = triangles[i]->neigh[j];
if ((k == NULL) || (!k->visible))
{
// here we have an edge, we must draw a polygon
p1 = triangles[i]->p[j];
p2 = triangles[i]->p[(j+1)%3];
//calculate the length of the vector
v1.x = (p1.x - wlp[0])*100;
v1.y = (p1.y - wlp[1])*100;
v1.z = (p1.z - wlp[2])*100;
v2.x = (p2.x - wlp[0])*100;
v2.y = (p2.y - wlp[1])*100;
v2.z = (p2.z - wlp[2])*100;
//draw the polygon
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(p1.x, p1.y, p1.z);
glVertex3f(p1.x + v1.x, p1.y + v1.y, p1.z + v1.z);
glVertex3f(p2.x, p2.y, p2.z);
glVertex3f(p2.x + v2.x, p2.y + v2.y, p2.z + v2.z);
glEnd();
}
}
glFrontFace(GL_CCW);
glColorMask(1, 1, 1, 1);
//draw a shadowing rectangle covering the entire screen
glColor4f(0.0f, 0.0f, 0.0f, 0.4f);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glStencilFunc(GL_NOTEQUAL, 0, 0xffffffff);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
glPushMatrix();
glLoadIdentity();
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(-1, 1,-1);
glVertex3f(-1,-1,-1);
glVertex3f( 1, 1,-1);
glVertex3f( 1,-1,-1);
glEnd();
glPopMatrix();
glDisable(GL_BLEND);
glDepthFunc(GL_LEQUAL);
glDepthMask(GL_TRUE);
glEnable(GL_LIGHTING);
glDisable(GL_STENCIL_TEST);
glShadeModel(GL_SMOOTH);
glPopMatrix();
这是我的初始化:
glEnable(GL_DEPTH_TEST); // Enable depth Buffering
glEnable(GL_COLOR_MATERIAL); // Enable color tracking
glEnable(GL_NORMALIZE); // Enables vector normalization (optional)
glClearColor( 0.4f, 0.4f, 0.8f, 1.0f ); // Set initial value of color buffer (Set background color also)
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
glDepthFunc(GL_LEQUAL);
// Lighting initialization
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT1, GL_POSITION , light.position); //Set position.
glLightfv(GL_LIGHT1, GL_AMBIENT , light.ambient); //Set ambient light.
glLightfv(GL_LIGHT1, GL_DIFFUSE , light.diffuse); //Set diffuse component.
glLightfv(GL_LIGHT1, GL_SPECULAR , light.specular); //Set specular component.
glEnable(GL_LIGHT1);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat.ambient); // Set Material Ambience
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat.diffuse); // Set Material Diffuse
glMaterialfv(GL_FRONT, GL_SPECULAR, mat.specular); // Set Material Specular
glCullFace(GL_BACK); // Set Culling Face To Back Face
glEnable(GL_CULL_FACE); // Enable Culling
glClearColor(0.1f, 1.0f, 0.5f, 1.0f); // Set Clear Color (Greenish Color)
// Initialize camera
glMatrixMode(GL_PROJECTION); // Switch to projection matrix
glLoadIdentity(); // Clear current matrix to identity matrix
gluPerspective(80, 1 , 0.5 , 1000000); // Set projection of camera (You can modify the arguments if needed.)
glMatrixMode(GL_MODELVIEW);