Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎯 Unreal

GLSL + Cube Mapping

Started by x452Alba Dec 28, 2007 at 6:12 AM 5 replies 12.2k views
Original Post
x452Alba
x452Alba
Hoping everyone had a great Chrisy. I have questions relating to the correct way to preform cube mapping, and as to how the results should appear. I am using GLSL to render, and have a few doubt's regarding my math. I load my cube map in opengl using:

unsigned int cube_target[6] = {
   GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
   GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
   GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
   GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
   GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
   GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
};

char* file_target[6] = {
   "posx.bmp", 
   "negx.bmp", 
   "negy.bmp", // Note the Y-axis is swapped!?
   "posy.bmp", 
   "posz.bmp", 
   "negz.bmp", 
};




I have to swap the Y-axis textures because if I don't the results are buggy: And if I swap the Y-axis, all is good: The following are my vertex and pixel shaders respectivily:

uniform vec3 light_position;
uniform vec3 eye_position;

varying vec3 light_direction;
varying vec3 view_direction;
varying vec3 normal;

void main()
{
   vec4 v = gl_ModelViewMatrix * gl_Vertex;
   gl_Position = ftransform();
   normal      = normalize(gl_NormalMatrix * gl_Normal);

   light_direction = normalize(light_position - v.xyz);
   view_direction  = normalize(eye_position - v.xyz);
}




uniform vec3 light; // Light Colour;
uniform vec3 ambient;
uniform vec3 specular;
uniform float exponent;

uniform samplerCube envMap;

varying vec3 light_direction;
varying vec3 view_direction;
varying vec3 normal;

void main()
{
   vec3 reflect_direction = normalize(reflect(view_direction, normal)); // <----
   vec3 diffuse = textureCube(envMap, reflect_direction); // <----

   vec3 t = normalize(view_direction + light_direction);

   float d  = dot(light_direction, normal);
   float hn = dot(t, normal);

   vec3 color = diffuse * (ambient + light * max(0.0, d)) /* Ambient lighting. */;
        color += light * specular * pow((max(hn, 0.0)), exponent); /* Phong exponent. */

   gl_FragColor = vec4(color, 1.0);
}



My next query relates to, well, how do I describe this... ummmm. When the eye/camera is over the cube map, shouldn't I see the lovely blue sky? And not the brick ground?: And ideas? Thanks ahead of time.
Trenki
Trenki
How do you load your cubemaps and how do the images look when opened with an image editor?

You probably don't have to swap the y coordinate target but have to flip the textures in the y-axis before uploading them to OpenGL as OpenGL has its (0,0) coordinate in the lower left side of the image which has to be the first pixel loaded into OpenGL.

In my last app where I used precomputed cubemaps the image files where displayed upside down when opened with an image editor. I loaded those textues with SDL and then flipped them in the y-axis and then just loaded the flipped data into the individual cube map faces. This worked.
V-man
V-man
You don't need to do a reflect calculations.
Just assume that the cubemap is in eye space.
Compute eye space normal and use that

You would just type in
vec3 diffuse = textureCube(envMap, normal);

PS, that code is invalid. Should be
vec4 diffuse = textureCube(envMap, normal);

x452Alba
x452Alba
Quote:
Original post by Trenki
How do you load your cubemaps and how do the images look when opened with an image editor?


Like so:



I just copied NVIDIAs example:

x452Alba
x452Alba
Quote:
Original post by V-man
You don't need to do a reflect calculations.
Just assume that the cubemap is in eye space.
Compute eye space normal and use that

You would just type in
vec3 diffuse = textureCube(envMap, normal);

PS, that code is invalid. Should be
vec4 diffuse = textureCube(envMap, normal);



Okay, however, the image is flipped. Is this correct? And I still don't see a lovely blue sky when the eye / camera is over the lamp... ummm. Any ideas?

uniform vec3 light; // Light Colour;uniform vec3 ambient;uniform vec3 specular;uniform float exponent;uniform samplerCube envMap;varying vec3 light_direction;varying vec3 view_direction;varying vec3 normal;void main(){// vec3 reflect_direction = normalize(reflect(view_direction, normal));   vec4 diffuse = textureCube(envMap, normal);   vec3 t = normalize(view_direction + light_direction);   float d  = dot(light_direction, normal);   float hn = dot(t, normal);   vec3 color = diffuse.xyz * (ambient + light * max(0.0, d)) /* Ambient lighting. */;        color += light * specular * pow((max(hn, 0.0)), exponent); /* Phong exponent. */   gl_FragColor = vec4(color, 1.0);}





x452Alba
x452Alba
Thanks for everyone's advice and ideas. With a little more debugging I adjusted my normal information and I am pleased with the below output:





V-man
V-man
I would just flip the cubemap when you make the texture.
I consider z as being up in my world, so I setup the faces accordingly in my DDS files.
For you it should be easy since your have 6 separate textures.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.