Jump to content
  • Advertisement
Sign in to follow this  
PragmaticZach

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.

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

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 this post


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


Link to post
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);

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!