# OpenGL Dynamic Environment Cubemap

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

## Recommended Posts

Hello guys,

Im implementing cubemap reflections of the environment, which basically consists of rendering a cubemap in the position of the camera for all 6 directions in each frame (which i know is correct):

The issue that i'm having is when the viewing camera angle crosses an inclination threshold: (on the top pic we have a correct reflection, but on the bottom pic you start to see the top cubemap texture showing on the bottom of the cube and if you go up further you start to see the bottom texture of the cubemap on the top of the cube too)

The actual test scene (with the car reflecting everything, and notice the road appearing in the boot instead of the sky)

Here's the relevant glsl shader code:

vec3 viewDir  = normalize(-FragPos);
if (Specular.b > 0.0)
{
vec3 R = reflect(viewDir, Normal);
vec4 R_World = inverseView * vec4(R, 0.0);

// we have to flip the x axis because of RenderMan specification, i.e. legacy opengl
R_World.x = -R_World.x;

vec3 reflectionColor = texture(cubemap, R_World.xyz).rgb;
Diffuse.rgb = (Specular.b * reflectionColor) + ((1.0 - Specular.b) * Diffuse.rgb);
}


where the viewDir and Normal are in camera view space

Edited by André

##### Share on other sites

I've had issues before either in the rendering or the reflection vector.  I suggest using a sphere to debug.

In my current shader, I have to flip the x-axis of the reflection vector: texCube(envMap, vec3(refVec.x*-1, refVec.y, refVec.z) ). Pretty sure that is just an issue with my current code as I dont recall doing that before.  But I've had issues where I had to convert the vector from GL to DX coordinates because the cube map is stored in directX coordinates most likely. So try flipping y and z, and/or negating one of those after flipping.

aside from that, you could also be potentially rendering to the wrong cube face on accident or using the wrong view matrix to render to the face.

vec4 R_World = inverseView * vec4(R, 0.0);

You could also using an interpolate/varying of the world eye/camera and get rid of the matrix multiply in the pixel shader.

##### Share on other sites

I've had issues before either in the rendering or the reflection vector.  I suggest using a sphere to debug.

In my current shader, I have to flip the x-axis of the reflection vector: texCube(envMap, vec3(refVec.x*-1, refVec.y, refVec.z) ). Pretty sure that is just an issue with my current code as I dont recall doing that before.  But I've had issues where I had to convert the vector from GL to DX coordinates because the cube map is stored in directX coordinates most likely. So try flipping y and z, and/or negating one of those after flipping.

aside from that, you could also be potentially rendering to the wrong cube face on accident or using the wrong view matrix to render to the face.

vec4 R_World = inverseView * vec4(R, 0.0);

You could also using an interpolate/varying of the world eye/camera and get rid of the matrix multiply in the pixel shader.

Yea, i'm already flipping the x-axis because of the way that opengl reads textures. The weird thing is that it works fairly well until you get in a higher position than the reflective object. As for the matrix multiplication, that FragPos one of the outputs of a geometry pass and it is used for other things that are expecting it to be in viewspace (like SSAO) so i think that is going to have to stay, at least for now.

Well i have to say that the sphere debug was an excellent sugestion, there's definitely something wrong in here, even when the camera is not looking from above:

Looks like the top and bottom textures are switched. Will continue to debug based on your suggestions, thanks.

EDIT:

Yup, just switched the order of the cameras and sure enough:

Now the problem is that my cube map looks switched in the OpenGL Profiler, so the error was not really in the cubemap generation but in the reading, will investigate. Thanks again

Edited by André

##### Share on other sites

Cool. The thing I was suggesting about that matrix multiply is that I have 4 varying vectors, normal/view vec in both world space and camera space. I compute those both in the vertex shader and interpolate the values. As opposed to just have noraml/viewvec in camera space and then doing a matrix multiply inversion later. The matrix multiply in the pixel shader should be more expensive than what I'm suggesting. Probably won't change your framerate but it saves some cycles.

##### Share on other sites

Cool. The thing I was suggesting about that matrix multiply is that I have 4 varying vectors, normal/view vec in both world space and camera space. I compute those both in the vertex shader and interpolate the values. As opposed to just have noraml/viewvec in camera space and then doing a matrix multiply inversion later. The matrix multiply in the pixel shader should be more expensive than what I'm suggesting. Probably won't change your framerate but it saves some cycles.

Yes i know what you mean, but as i mentioned, i am using deferred rendering, which would imply i would have to create another texture to store the position values in world space in the geometry pass, and then in the lighting pass (the one where i also calculate the reflectons) i would have to perform texture lookups to access the positions, which is also expensive.

I will try that in the future for sure though.

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

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633655
• Total Posts
3013186
×