OpenGL using GL_STENCIL with a sphere

I work with OpenGL and I am trying to create a sphere with a reflective surface. I have this reflection, but the reflection is wrong. The object in reflection must be bent and deformed along the curved surface, instead I get only direct reflection. I did not use GL_STENCIL, so help would be greatly appreciated. I have provided code snippets such as creating a sphere and a drawing method. If anyone needs more, let me know.

Creature:

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

Picture:

 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 

Since I am new to using GL_STENCIL, I was not sure if it was just something small or much more needed to be done to detect this angle of reflection.

+4
source share
1 answer

Have you considered using reflection / imaging environments ?

There are two main forms. Spherical environment mapping usually works with a pre-computed environment map. However, this can be done dynamically. Its main disadvantage is that it depends on the species.

Another system is the display of a cubic medium . Cubic is very easy to set up and simply includes visualizing your scene 6 times in 6 different directions (that is, on each face of the cube). The cubic mapping env is view independent.

There is another system that is between spherical and cubic. It is called a double paraboloid mapping of the environment . It has the opposite side, that the generation of double paraboloids is rather complicated (for example, spherical), but (for example, cubic) it is independent of type.

+2
source

All Articles