Skip to main content
gamedev.net
🔒 Locked

Stencil Buffer Reflections

Started by riksweeney Aug 24, 2006 at 5:34 AM 3 replies 700+ views
Original Post
riksweeney
riksweeney
Hi, I'm trying to implement reflections with the Stencil Buffer. It all seems to work OK: the objects get reflected in the floor but I can see the objects on the other side of the floor. Here's the code for enabling the Stencil Buffer and doing the reflection:

glColorMask(0,0,0,0);
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 1, 1);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);


glDisable(GL_DEPTH_TEST);

renderBSP(0);

glEnable(GL_DEPTH_TEST);
glColorMask(1,1,1,1);
glStencilFunc(GL_EQUAL, 1, 1);

glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

glPushMatrix();
{
	glScalef(1.0f, 1.0f, -1.0f);

	glCullFace(GL_FRONT);

	renderEntities(1);

	glCullFace(GL_BACK);
}
glPopMatrix();

glDisable(GL_STENCIL_TEST);

renderBSP(1);

renderEntities(0);

glFlush();
SDL_GL_SwapBuffers();
Here is the (very minimal) code renderBSP(int)

if (bsp.faces.vNormal.x == 0 && bsp.faces.vNormal.y == 0 && bsp.faces.vNormal.z == 1)
{
	if (blend == 1)
	{
		glEnable(GL_BLEND);
		glColor4f(1.0f, 1.0f, 1.0f, 0.8f);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	}
}

glDrawElements(wireframe == 1 ? GL_TRIANGLES : GL_LINES, bsp.faces.numMeshVerts, GL_UNSIGNED_INT, &bsp.type1Faces[0]);

if (bsp.faces.vNormal.x == 0 && bsp.faces.vNormal.y == 0 && bsp.faces.vNormal.z == 1)
{
	if (blend == 1)
	{
		glDisable(GL_BLEND);
	}
}
The code in renderEntities(int) just draws the objects and translates the object so that it reflects in the floor correctly, as the floor isn't at 0. Basically, does any know how I can prevent the reflect object from appearing below the floor? Thanks Richard
The_Neverending_Loop
The_Neverending_Loop
If I understand your problem correctly, the objects that shouldent get reflected are getting reflected, basically cause your inverting everythings from top to bottom, the objects in the bottom now move to the top as well!, but of course in real life a mirror only reflects the objects in front of it. I read up on this in "More OpenGL Game Programming" I dont know the exact code to do it right of the top of my head, but once I get home Ill try to get it for you. But basically you can try to research this on your own, what you want to do is define the reflective surface as a plan and then use openGL to cull anything from being rendered on the other back side of that plane, and thats basically how you would fix the problem. But if this doesnt sound liek the your problem then tell me otherwise. If you need mroe help Ill be glad to help you out as soon as I get home to my OGL resources.
riksweeney
riksweeney
Hi, this sounds correct. I want to clip the reflected image so that it doesn't show up underneath the floor. I guess I can use glClipPlane to achieve this?

Richard
The_Neverending_Loop
The_Neverending_Loop
I believe that might be the command but then again Iam not sure. check it out to see if thats what you would need if anythign Ill tell you what you need when I get home.
Vexator
Vexator
jep you need to setup a custom clip plane, there's a tutorial on nehe.gamedev.net
Wunderwerk Engine is an OpenGL-based, shader-driven, cross-platform game engine. It is targeted at aspiring game designers who have been kept from realizing their ideas due to lacking programming skills.

blog.wunderwerk-engine.com

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.