Dynamic Environment Cubemap

Started by
3 comments, last by Andr 7 years, 9 months ago
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):
Screen+Shot%202016-06-29%20at%2019.41.33
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)
correct.png incorrect.png
The actual test scene (with the car reflecting everything, and notice the road appearing in the boot instead of the sky)
fullscene.png
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

Any extra info you need just ask. Thanks in advance.

Advertisement

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.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

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:

Screen+Shot%202016-06-29%20at%2023.44.38

Screen+Shot%202016-06-29%20at%2023.45.02

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:

Screen_Shot_2016_06_29_at_23_56_30.png

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

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.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

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.

This topic is closed to new replies.

Advertisement