# OpenGL [GLSL] Calculating Reflection vector for a cubemap

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

## Recommended Posts

I am attempting to do a reflective cube mapping my placing a sphere inside of a cube mapped environment. I believe I have most things working, but am not calculating the reflection vector correctly. From what I have been reading in online tutorials, the reflection vector is calculated by using the GLSL reflect() function, which takes the camera vector to the fragment, and the fragment normal. In order to find the camera vector, from what I'm reading (I'm very new to OpenGL and GLSL), you multiply the camera position in view space (0,0,0) by the inverse of the model view matrix, to find it's position in world space. Here is the relevant code from my vertex shader:
normal = normalize(gl_NormalMatrix * gl_Normal);
vec3 cameraPosition = vec3(gl_ModelViewMatrixInverse * vec4(0,0,0,1.0));
cameraPosition = normalize(cameraPosition-gl_Vertex.xyz);

vec4 reflection = vec4(reflect(cameraPosition.xyz, normal), 1.0);
reflectDir = reflection.xyz;


The resulting image is this: It looks more like the cube is inside the sphere and being "blown up", than it does the sphere being inside the cube. (the reason you just see a white background is because I haven't implemented the quad drawing yet). It also looks like the textures are being flipped in some odd ways. Anyone know what I may be doing wrong here? Thanks for the help.

##### Share on other sites
Alright, I have a makeshift solution, but I'm not sure why it works:

1. Make the reflection vector negative.

2. Load the negative y face before the positive one.

##### Share on other sites
Quote:
 1. Make the reflection vector negative.

Maybe the reflect call takes the view vector (as in the vector from the camera to the vertex).
You are computing the opposite direction (from vertex to camera):
cameraPosition = normalize(cameraPosition-gl_Vertex.xyz);

1. 1
2. 2
3. 3
Rutin
25
4. 4
5. 5
khawk
14

• 11
• 11
• 23
• 10
• 9
• ### Forum Statistics

• Total Topics
633648
• Total Posts
3013115
×