我正在与OpenGL合作,我正在尝试创建一个具有反射表面的球体。我有反思,但反射不正确。反射中的对象应弯曲并根据表面曲线变形,而是我只得到直射的反射。我没有使用GL_Stencil太多,因此将不胜感激。我提供了诸如Sphere和Draw方法之类的代码。如果有人需要更多,请告诉我。

创建:

sphere = gluNewQuadric();
gluQuadricDrawStyle(sphere,  GLU_FILL);
gluQuadricNormals(sphere, GLU_SMOOTH);
gluSphere(sphere, 1, 100, 100);
gluDeleteQuadric(sphere);

绘画:

glClearColor (0.0,0.0,0.0,1);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glLoadIdentity();

glTranslatef(0, 0, -10);

glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); //disable the color mask
glDepthMask(GL_FALSE); //disable the depth mask
glEnable(GL_STENCIL_TEST); //enable the stencil testing
glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE); //set the stencil buffer to replace our data

sphereDraw(); //the mirror surface

glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); //enable the color mask
glDepthMask(GL_TRUE); //enable the depth mask

glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); //set the stencil buffer to keep our next lot of data

glPushMatrix();
glScalef(1.0f, -1.0f, 1.0f); //flip the reflection vertically
glTranslatef(0,2,-20); //translate the reflection onto the drawing plane
glRotatef(angle,0,1,0); //rotate the reflection
//draw object as our reflection
glPopMatrix();

glDisable(GL_STENCIL_TEST); //disable the stencil testing

glEnable(GL_BLEND); //enable alpha blending
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //set the blending function
sphereDraw(); //draw our bench
glDisable(GL_BLEND); //disable alpha blending

//draw object

由于我是不熟悉GL_Stencil的新手,因此不确定它是否只是很小的事情,还是需要做更多的事情来检测反射角度。

有帮助吗?

解决方案

您是否考虑过使用 反射/环境映射?

有两种主要形式。 球形环境映射 通常通过具有预计数的环境图来起作用。但是,它可以动态完成。它的主要缺点是它取决于视图。

另一个系统是 立方环境映射. 。立方体非常容易设置,涉及在6个不同方向(即在立方体的每个面上)呈现6次的场景。立方env映射是独立的。

还有另一个系统位于球形和立方之间。它叫 双抛物面环境映射. 。它的回报是,生成双抛物面的生成非常复杂(如球形),但是(就像立方体一样)它是独立的。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top