Jump to content
  • Advertisement
Sign in to follow this  
Halsafar

Basic Reflections with shader

This topic is 2901 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

I have been struggling with trying to implement basic reflections with a shader. I am polishing up a grad students projects which is a hockey game. Trying to add some reflection support for the ice. Here is what I do:

setReflectionFrameBufferObject()
setupCamera()
glScale3f(1, -1, 1)
setClipPlaneForIce()
renderPlayers()
glScale3f(1, 1, 1)
setDefaultClipPlane()
setDefaultFrameBuffer()
setMySpecialShader()
renderRegularScene()


I have verified that the reflection frame buffer generates the proper texture containing the reflection of the scene. I rendered it in orth mode to the screen so I can verify.

The problem is in my vertex shader. It is spitting on terrible texture coordinates. The reflections are appearing in the wrong spot. To avoid cutting apart my very large shader to get at just the reflection stuff I was hoping to find a tutorial or example on reflections using shaders in GLSL. Sadly I found no such example. So I am here on gamedev asking if someone can guide the shader step in this process.

Thanks!

Share this post


Link to post
Share on other sites
Advertisement
It sounds like you trying to mix two different ways of doing reflections. Here are the steps I use to render reflections. Stick to them and you won't have any issues.

NOTE: This only works with scene AND camera +Y as up.


void preRenderScene() {
// Begin rendering to the framebuffer
ReflectionFBO->begin();

// Preserve previous matrix
glPushMatrix();

// Reset the matrix
glLoadIdentity();

// Scale the scene by (-1, 1, 1)
glScalef(-1.0f, 1.0f, 1.0f);

// Invert the camera over the plane of reflection
gluLookAt(
CameraPosition.x, 2.0f * height - CameraPosition.y, CameraPosition.z,
CameraCenter.x, 2.0f * height - CameraCenter.y, CameraCenter.z,
0.0f, -1.0f, 0.0
);

// Draw the reflected objects
renderObjects();

// Restore the previous matrix
glPopMatrix();

// Finish the FBO
ReflectionFBO->end();
}

void renderScene() {
// Draw the objects
renderObjects();

// Draw the Ice
renderIce();
}

void renderObjects() {
// Apply the camera
gluLookAt(
CameraPosition.x, CameraPosition.y, CameraPosition.z,
CameraCenter.x, CameraCenter.y, CameraCenter.z,
0.0f, 1.0f, 0.0f
);

// Extract the frustum
// Render the objects
}

void renderIce() {
// Your code here
}


All this will work. If it doesn't, you either don't have +Y as up, or your shader has problems.

Additional Note: glScalef(1, 1, 1) does NOTHING.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!