我正在使用OpenGL,我正在尝试创建一个具有反射表面的球体.我反映但反射不正确.反射中的物体应根据表面的曲线弯曲和变形,而不是直接反射.我没有太多使用GL_STENCIL,所以非常感谢帮助.我提供了一些代码片段,例如球体的创建和绘制方法.如果有人需要更多,请告诉我.
创建:
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,我不确定它是否只是一些小的,或者是否需要做更多来检测反射角.
最佳答案 你考虑过使用
reflection/environment mapping吗?
主要有两种形式. Spherical environment mapping通常通过预先计算的环境地图来工作.然而,它可以动态完成.它的主要缺点是它依赖于视图.
另一个系统是Cubic Environment mapping.立方体非常容易设置,只需在6个不同的方向(即在立方体的每个面上)渲染场景6次.立方体env映射是独立于视图的.
还有另一个系统位于球形和立方体之间.它被称为dual paraboloid environment mapping.它有一个缺点,即产生双抛物线非常复杂(如球形)但(如立方体)它是独立的视图.