Skip to main content
gamedev.net
🔒 Locked

reflections

Started by Endar Apr 26, 2006 at 1:36 AM 5 replies 600+ views
Original Post
Endar
Endar
I have some problems. I've followed the code from NeHe thingo (article) 26, and I've found that I get reflectivness only if I don't draw the surface that is supposed to be reflected off. So, I'm drawing a cube and the reflection should be shown in a section of floor. I can only see the reflection if I don't draw the floor. It is still drawn within the boundries of where the floor would be (when it's drawn into the stencil buffer). It's also kind of inside out, which I don't know if it's supposed to happen, but I think we should focus on being able to see the reflection before seeing if it's the right one. Okay, the call to "driver->render()" renders the scene graph, which at the moment only contains the cube.

// the texture of the floor
echo::video::ITexture* floor_tex = driver->createTexture("envwall.bmp");

// while the window has not received a WM_QUIT message
while( engine->run() ){

	driver->beginScene();

	// reset the world/modelview matrix to the identity matrix
	driver->resetMatrix(echo::video::WORLD_MATRIX);

	// my transformations
	node->addRotationX(xrot);
	node->addRotationY(yrot);

	// set translation
	node->setTranslation( echo::util::CVector3d(xtrans, ytrans, ztrans) );


	// TEST - reflections

	// plane equation for clipping
	double plane[4] = {0.0f, -1.0f, 0.0f, 0.0f};

	glLoadIdentity();
	glTranslatef(0.0f, -0.5f, -5.0f);

	glColorMask(0, 0, 0, 0);

	// enable the stencil testing for the floor
	glEnable(GL_STENCIL_TEST);

	// set the stencil buffer so the test always passes, has a 1-bit plane, 1 as mask
	// Keep if the test fails, keep if the test passes but the buffer test fails
	// Replace if test passes
	glStencilFunc(GL_ALWAYS, 1, 1);
	glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);

	// disable depth testing
	glDisable(GL_DEPTH_TEST);

	// apply floor texture
	floor_tex->apply();
	// draw the floor (draw it to the stencil buffer)
	// we only want to mark it in the stencil buffer
	DrawFloor();

	// So, now we want to draw the cube, but only want the pixels to show up where the stencil buffer
	// has the value of 1 (where the floor is)

	/////// Draw the reflection ///////

	glEnable(GL_DEPTH_TEST);	// enable depth testing
	glColorMask(1, 1, 1, 1);
	glStencilFunc(GL_EQUAL, 1, 1);	// we draw only where the stencil is 1 (where the floor was drawn)
	glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);	// don't change the stencil buffer

	glEnable(GL_CLIP_PLANE0);		// enable the clipping plane 
	glClipPlane(GL_CLIP_PLANE0, plane);	// equation for reflected objects



	driver->pushMatrix();
		// mirror the Y axis
		glScalef(1.0f, -1.0f, 1.0f);
		// draw the things to be reflected
		driver->render();
	driver->popMatrix();

	glDisable(GL_CLIP_PLANE0);	// disable the clip plane for drawing the floor
	glDisable(GL_STENCIL_TEST);	// disable the stencil testing

	glEnable(GL_BLEND);		// enable the blending so the reflected object will show
	glDisable(GL_LIGHTING);		// disable the lighting because we're using blending

	glColor4f(1.0f, 1.0f, 1.0f, 0.8f);	// set the color to white with 80% alpha
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	// apply floor texture
	floor_tex->apply();
	DrawFloor();

	//// Drawing the cube ////
	glEnable(GL_LIGHTING);
	glDisable(GL_BLEND);

	// draw the cube
	driver->render();

	// TEST - reflections



	// draw the console and the static messages to the screen
	engine->drawConsole();

	driver->endScene();	// actually renders the window
}

I have a fair feeling that these two lines of code are the problem:

glColor4f(1.0f, 1.0f, 1.0f, 0.8f);	// set the color to white with 80% alpha
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Because as far as I can see they define the blending for the floor. Am I doing something wrong? Have I left something out, as far as you guys can see?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Endar
Endar
I know, I know, it hasn't quite been 24 hours, but I'm off to uni for the day and I thought it would be best to bump it now.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
MARS_999
MARS_999
As of now with out a screenshot I can only guess... When you call the functions to do the reflections, are you translating the scene at all? You need to move the obejcts height along with the camera I think... I know I had to for my water. You could always render the reflection texture to a quad and see what is showing up... I didn't look through all your code, but am assuming you are using RTT of some sorts?
Endar
Endar
Quote:
Original post by MARS_999
As of now with out a screenshot I can only guess... When you call the functions to do the reflections, are you translating the scene at all? You need to move the obejcts height along with the camera I think... I know I had to for my water. You could always render the reflection texture to a quad and see what is showing up... I didn't look through all your code, but am assuming you are using RTT of some sorts?


RTT?

What do you mean about rendering the reflections texture to a quad?


I have a quad that is acting as the reflective surface. If I draw this to the stencil buffer, but not to the screen, I can see the reflection. I don't know if it's right, but I can see it.

So, as far as I can see, this means that the reflection code works, but the blending is off as I can't see the reflection through the quad.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
cwhite
cwhite
I don't really know what floor_tex->apply() does, but I bet your texture mapping is over-riding the alpha transparency you set up with your call to glColor4f(). Try removing that function to see if it makes your reflection show up. If it works, then fiddle around with floor_tex->apply() until you get it set up correctly.
MARS_999
MARS_999
Quote:
Original post by Endar
Quote:
Original post by MARS_999
As of now with out a screenshot I can only guess... When you call the functions to do the reflections, are you translating the scene at all? You need to move the obejcts height along with the camera I think... I know I had to for my water. You could always render the reflection texture to a quad and see what is showing up... I didn't look through all your code, but am assuming you are using RTT of some sorts?


RTT?

What do you mean about rendering the reflections texture to a quad?


I have a quad that is acting as the reflective surface. If I draw this to the stencil buffer, but not to the screen, I can see the reflection. I don't know if it's right, but I can see it.

So, as far as I can see, this means that the reflection code works, but the blending is off as I can't see the reflection through the quad.



RTT = render to texture...

I meant render a single quad on the screen and map the reflection texture to that quad so you can see if things are working correctly. Are you using ALPHA_TEST? I can't remember the rules on it but you may want to check out this

http://pyopengl.sourceforge.net/documentation/manual/glAlphaFunc.3G.html

and see if that will help you out...
Endar
Endar
Quote:
Original post by cwhite
I don't really know what floor_tex->apply() does, but I bet your texture mapping is over-riding the alpha transparency you set up with your call to glColor4f(). Try removing that function to see if it makes your reflection show up. If it works, then fiddle around with floor_tex->apply() until you get it set up correctly.


apply() calls glBindTexture with the right id for the texture, so taking away the apply() call simply means that the texture from the cube is mapped onto the quad that is acting as the reflective surface.

So, I'm going to be looking into glAphaFunc.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper

Topic Locked

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

Sign in to reply to this topic.