gpt4 book ai didi

linux - Debian 和 Win7、Ubuntu 上的 OpenGL 阴影体积差异

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:25:03 25 4
gpt4 key购买 nike

在带有最新 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);

最佳答案

确保您传递给 glutInitDisplayMode()模式包含 GLUT_STENCIL,否则您可能无法获得模板缓冲区。

关于linux - Debian 和 Win7、Ubuntu 上的 OpenGL 阴影体积差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14327951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com