Stencil Buffer For Reflections

Started by
6 comments, last by Zumichu 20 years, 10 months ago
Below is the funcion I wrote to draw a reflection. The problem is the "reflection object" seems to draw to the stencil buffer fine, but when I draw the reflected object the color buffer gets updated where the reflection plane isn't (opposite of what it should be). nothing gets drawn to where the stecil buffer was set.

void ReflectionObject::Draw()
{
	//Enable Stencil and Disable Color Buffer

	glStencilFunc(GL_ALWAYS,1,0xff);
	glStencilOp(GL_ALWAYS,GL_KEEP,GL_REPLACE);
	glColorMask(false,false,false,false);

	//Draw Reflection Object to stencil buffer

	Object::Draw();

	//Apply reflection matrix

	glPushMatrix();
	glMatrixMode(GL_MODELVIEW);
	glMultMatrixf(m_ReflectionPlane.GetRelfectionMatrix());

        //Enable Color Buffer and Set Stencil Test

	glStencilFunc(GL_EQUAL,1,0xff);
	glStencilOp(GL_KEEP,GL_KEEP,GL_KEEP);
	glColorMask(true,true,true,true);

	glEnable(GL_STENCIL_TEST);

	//draw object to reflect

	vector<object*>::iterator it;
	for (it = m_ObjectsToReflect.begin(); it != m_ObjectsToReflect.end(); ++it)
	{
		(*it)->Draw();
	}

	glPopMatrix();

	glDisable(GL_STENCIL_TEST);
}
[I did absolutely nothing, and it was everything that I thought it could be]
Advertisement
What are you doing about the depth buffer?
Good point. That would explain why the reflection object is not being drawn on the reflection plane, but why is it being drawn outside the plane?
[I did absolutely nothing, and it was everything that I thought it could be]
I''m not exactly sure what would be causing it to draw outside the stencil area. The only way I could duplicate that behavior was to set glClearStencil to 1.

Also, I noticed that you''re passing GL_ALWAYS to glStencilOp, which isn''t a valid parameter, so that call should fail.
hmm, I do call glClearStencil(0) every frame.

And I guess technically the call to glStencilOp should fail, it doesn''t matter what that argument is considering the stencil func is set to GL_ALWAYS. Anyway, I did change it, and I get the same behavior.

hmm, I''ll keep messing with it, but I''m a little lost here.
[I did absolutely nothing, and it was everything that I thought it could be]
For the record, I was able to take your code, add in the calls to glDepthMask, change the first glStencilOp, and get correct results, so it's probably due to something external to the code you posted.
Ok, thank you very much. I''ll see what I can do.

BTW, for the depth testing stuff, would it be good enough to just disable depth testing as I''m drawing the reflected object? or is using glDepthMask better? I''m not up on using glDepthMask, so if you could run through how you used it, I''d appreciate it.
[I did absolutely nothing, and it was everything that I thought it could be]
Using glDepthMask is really the only way to go. If you just disable depth buffer tests while rendering the reflected objects, they won''t show up correctly.

Use glDepthMask in exactly the same way you''re using glColorMask now. i.e. Disable it with GL_FALSE before rendering your reflecting object, and then reenable it when you''re done.

This topic is closed to new replies.

Advertisement