开发者

OpenGL using GL_STENCIL with a sphere

开发者 https://www.devze.com 2023-01-28 04:21 出处:网络
I\'m working with OpenGL and I am trying to create a sphere that has a reflective surface. I have it reflecting but the reflection isn\'t correct. The object in the reflection should be bent and defor

I'm working with OpenGL and I am trying to create a sphere that has a reflective surface. I have it reflecting but the reflection isn't correct. The object in the reflection should be bent and deformed according to the curve of the surface, instead I'm getting only a straight reflection. I haven't used GL_STENCIL much so help would be very much appreciated. I have provided pieces of code such as the creation of the sphere and the draw method. If anyone needs more let me know.

Creation:

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

Drawing:

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 ve开发者_如何学Crtically
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

Since I'm new to using GL_STENCIL I wasn't sure if it's just something small or if much more needs to be done to detect that angle of reflection.


Have you considered using reflection/environment mapping?

There are 2 main forms. Spherical environment mapping usually works by having a pre-calculated environment map. It can, however, been done dynamically. Its main drawback is that it is view dependent.

The other system is Cubic Environment mapping. Cubic is very easy to set up and involves simply rendering your scene 6 times in 6 different direction (ie on to each face of the cube). Cubic env mapping is view independent.

There is another system that sits between spherical and cubic. Its called dual paraboloid environment mapping. It has the draw back that generating the dual paraboloids is quite complex (like spherical) but (like cubic) it is view independent.

0

精彩评论

暂无评论...
验证码 换一张
取 消