Archived

This topic is now archived and is closed to further replies.

Stencil Buffer For Reflections

This topic is 5304 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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);
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites