0

I'm trying to implement cube mapping in FxPlug using GLSL code from http://antongerdelan.net/opengl/cubemaps.html. The only major modifications I made to the code were variable name changes and using gl_Vertex and gl_Normal instead of attributes.

vertex program snippet:

varying vec3 pos_eye;
varying vec3 n_eye;

pos_eye = vec3(ViewMatrix * ModelMatrix * gl_Vertex);
n_eye = vec3(ViewMatrix * ModelMatrix * vec4(gl_Normal, 0.0));
gl_Position = ProjectionMatrix * ViewMatrix * ModelMatrix * gl_Vertex;

fragment program snippet:

vec3 incident_eye = normalize(pos_eye);
vec3 norm = normalize(n_eye);
vec3 reflected = reflect(incident_eye, norm);
reflected = vec3(InverseViewMatrix * vec4(reflected, 0.0));
vec3 cubemapColor = textureCube(cubemap, reflected).rgb;

When I load in a model then insert a camera, the cube map actually starts out inverted, but once I move the camera away at a certain distance, I get the proper results. Has anyone seen this problem before when implementing cube maps? I'm thinking this is an issue with Motion and/or FxPlug with how it handles the cameras.

A video of the problem in action can be found at http://youtu.be/Yj85y-VKjCY

In the video, I load up a basic sphere, scale it up, and enable the skybox. The cubemap on the sphere starts upside-down. When I rotate the sphere, the cube map doesn't even move (Disregard the black texturing artifacts throughout the video when the camera moves; that seems to happen when there's insufficient memory or something). After zooming into the sphere, I get a more realistic "reflection" distortion, but the cube map is still upside-down. It isn't until I start zooming out into a reasonable distance that the cube map transforms into how it's supposed to look. What I did notice is that the cube map looks correct when the white rectangle that surrounds the sphere is fully visible.

I would appreciate any advice on how to solve this problem. Thanks in advance!

1 Answers1

0

After further investigation, I figured out that the cube map distortion results from incorrectly calculated reflection directions. It turns out that the "actual" position of the camera is about 2000 units into the positive Z direction. To account for this difference, I created a separate view matrix similar to the original in the vertex shader, but I subtract 2000 from the separate view matrix's 15th entry (i.e. the Z element of the "position" column).

mat4 AdjustedViewMatrix = ViewMatrix;
AdjustedViewMatrix[3][2] += -2000.0;

pos_eye = vec3(AdjustedViewMatrix * ModelMatrix * gl_Vertex); 

Though I still use the original view matrix for storing the gl_Position, using the adjusted view matrix for calculating the varying eye space position prevents cube map distortion when close to the object.