2010-12-04 69 views
4

我正在使用OpenGL,我试图创建一个具有反射表面的球体。我有它的反映,但反映是不正确的。反射中的物体应该根据曲面的曲线弯曲和变形,而不是直接反射。我没有使用GL_STENCIL,所以非常感谢。我已经提供了一些代码,比如创建球体和绘制方法。如果有人需要更多让我知道。使用带球体的GL_STENCIL的OpenGL

创作:

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我不知道,如果只是一些小事情,或者做更多的工作,以检测反射角度。

+0

顺便说一句,如果你做的第一件事是开始使用着色器,你的生活可能会变得更容易。模板反射不会因其性质而“弯曲”。 – Kos 2010-12-04 23:48:02

回答

2

您是否考虑过使用reflection/environment mapping

有两种主要形式。 Spherical environment mapping通常具有预先计算的环境地图。但是,它可以动态完成。它的主要缺点是它依赖于视图。

另一个系统是Cubic Environment mapping。 Cubic非常容易设置,并且只需在6个不同的方向(即在立方体的每个面上)渲染6次场景。立方体env映射是视图独立的。

还有一个系统位于球形和立方之间。它叫dual paraboloid environment mapping。它的缺点是产生双抛物面是非常复杂的(像球面),但是(像立方体)它是独立的视图。

+0

我已经看过立方图,但看起来有点多。我问这个问题的原因是因为一个反射和这个NeHe Tutorial,但我宁愿有镜像比影子。尽管感谢您的评论,但如果我更深入地研究它,这将值得探索。 – ars265 2010-12-04 23:25:50