Reflection with Cube Map & GLSL [Solved]

Started by
5 comments, last by jslider 16 years, 1 month ago
I've been working on a water shader with OpenGL and GLSL. While attempting reflection I've run into a bit of a snag. I'm fairly certain I've missed the application of a matrix but I haven't been able to track down which. I've tried passing the transposed texture matrix from the app source but end up with technically wonky output. Any suggestions would be appreciated. I'm using a cube map and trying to create the reflection vector for my first attempt at reflecting my environment. Vertex Shader in GLSL:

attribute float time;
varying vec3 vCubeMap;

void main()
{
    vec3 vVertex = vec3(gl_ModelViewMatrix * gl_Vertex);
    vec3 vNormal = vec3(gl_NormalMatrix * gl_Normal);
    vec3 vView = -normalize(vVertex); 

    vCubeMap = reflect(vView, vNormal);
    
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}



Fragment Shader:

uniform samplerCube CubeMap;
varying vec3 vCubeMap;

void main (void)
{
    gl_FragColor = textureCube(CubeMap, vCubeMap);
}



[Edited by - jslider on March 31, 2008 1:44:55 AM]
Advertisement
I'd like to help, but I don't know what issues are u gettin? An image would be good point, where to begin ;-).

Anyway for water rendering would be better to use plannar reflections.

My current blog on programming, linux and stuff - http://gameprogrammerdiary.blogspot.com

Planar reflections will most likely be the next iteration. I wanted to try the cube map technique first.

Problems with this particular shader:

1) The texture that is applied to the plane is always the view from the default camera position looking down Z.

2) When the texture is rendered to the plane its the back of the texture rather than the front.

3) The location rendered is fixed to the eye position. When the camera moves the texture moves with it.

I've applied a grid to the cube map to see exactly whats getting reflected at any given time. You can see the values are incorrect.

reflection via cube map incorrect
Well, afaik the reflection vector should be computed like this (I'm using it this way) - It's from my old demo, because now I use hybrid rendering (It's slower, but it's accurate):
vec3 normal = gl_Normal.xyz;vec3 cameraDir = normalize(cameraPos - gl_Vertex.xyz);vec3 reflectionVec = (normalize(reflect(normal, cameraDir));


This method should work, I'm not sure if reflection vectors can be calculated after multiplying vertices and normals with modelview matrix (And I think they can't).

My current blog on programming, linux and stuff - http://gameprogrammerdiary.blogspot.com

This topic: Environment Map topic applies the transforms before calculating the reflection. He's got the position - camera switched to camera - position though which inverts the Y plane.

Yann Lombard's Article follows the same format of not applying transforms before the reflection vector into the cube map is calculated.

The reflection seems to stay focused on a given point. As you move the camera the same location is always in front of the camera. I'll twiddle with it until I find a better question to ask.

Thanks!
With the cube mapped reflection would I expect to see a change in the reflection that occurs in front of the camera when I strafe? Would I expect to see much of a change with forward motion? Is the cube map infinitely far away so that motion in regards to it doesn't register unless you're rotating?

It seems to match the behavior I'm seeing. I've put up a series of 6 planes for a skybox and applied the textures that get loaded into the cube map onto those planes. The reflection doesn't match even when the planes are scaled to the same size as the textures being applied.

I'm not sure if I'm justifying wonky output or if I'm explaining away that oh so common user error. As you can see with the image below I've strafed to the left, but the image reflected directly in front of the camera is the same image seen at (0,0,0). I've placed the newest iteration of the GLSL shader source below. The reflection I return is also really high up on the plane of the cube map. Its rendering sky where if it were matched up directly I'd expect to see the mountains then sky.

cubemap reflection

Vertex Shader:
attribute float time;varying vec3 vReflection;uniform vec3 camera;void main(){    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;         vec3 vView = gl_Vertex.xyz - camera.xyz;     vReflection = reflect(vView, gl_Normal);}


Fragment Shader:
uniform samplerCube CubeMap;varying vec3 vReflection;void main (void){    gl_FragColor = textureCube(CubeMap, vReflection);}


Ahh user error strikes again!

So once I tied the skybox to the camera, not allowing the user to get closer to the skybox, the reflections look perfect. The environment being "infinitely far away" makes the reflection look the way it should.

This topic is closed to new replies.

Advertisement