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

cubemapping in GLSL

Started by zhou_f_f Oct 7, 2009 at 7:09 PM 6 replies 1.6k views
Original Post
zhou_f_f
zhou_f_f
I have used cubemap in the fixed pipeline. When I try to implement cubemapping in GLSL, using an example from the Orange Book's site, I can not work it out. The vertex shader ==================================== varying vec3 ReflectDir; varying float LightIntensity; uniform vec3 LightPos; void main() { gl_Position = ftransform(); vec3 normal = normalize(gl_NormalMatrix * gl_Normal); vec4 pos = gl_ModelViewMatrix * gl_Vertex; vec3 eyeDir = pos.xyz; ReflectDir = reflect(eyeDir, normal); LightIntensity = max(dot(normalize(LightPos - eyeDir), normal),0.0); } The fragment shader ==================================== uniform vec3 BaseColor; uniform float MixRatio; uniform samplerCube EnvMap; varying vec3 ReflectDir; varying float LightIntensity; void main() { // Look up environment map value in cube map vec3 envColor = vec3(textureCube(EnvMap, ReflectDir)); // Add lighting to base color and mix vec3 base = LightIntensity * BaseColor; envColor = mix(envColor, base, MixRatio); gl_FragColor = vec4(envColor, 1.0); } The related openGL codes ==================================== GLuint texture[6]; ... AUX_RGBImageRec *TextureImage[6]; memset(TextureImage,0,sizeof(void *)*6); TextureImage[0]=LoadBMP("sofa_positive_x.bmp"); TextureImage[1]=LoadBMP("sofa_negative_x.bmp"); TextureImage[2]=LoadBMP("sofa_positive_y.bmp"); TextureImage[3]=LoadBMP("sofa_negative_y.bmp"); TextureImage[4]=LoadBMP("sofa_positive_z.bmp"); TextureImage[5]=LoadBMP("sofa_negative_z.bmp"); glGenTextures(1, &texture[0]); glBindTexture(GL_TEXTURE_CUBE_MAP, texture[0]); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_REPEAT); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER,GL_LINEAR); for(int loop=0; loop<6; loop++) { glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X+loop, 0, GL_RGB, TextureImage[loop]->sizeX, TextureImage[loop]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[loop]->data); } ... glBindTexture(GL_TEXTURE_CUBE_MAP, texture[0]); ... glUniform1i(glGetUniformLocation(p,"EnvMap"), 0); glUniform3f(glGetUniformLocation(p,"LightPos"), 0.0, 0.0, 4.0); glUniform3f(glGetUniformLocation(p,"BaseColor"), 0.4, 0.4, 1.0); glUniform1f(glGetUniformLocation(p,"MixRatio"), 0.8); ==================================== The result is blue cube, and there is not cubemapping effect. Can anyone help? the Orange Book OpenGL Shading Language said "We access the cube map through texture unit 4". I don't know why and how to set the texture unit.
ndhb
ndhb
Tried changing GL_TEXTURE_MIN_FILTER to GL_NEAREST?

"We access the cube map through texture unit 4" - Do you see any glActiveTexture calls in the code?
zhou_f_f
zhou_f_f
"GL_TEXTURE_MIN_FILTER to GL_NEAREST" dose not work either.
There are no OpenGL code in the CubeMapping Example. So I confused for the sentence.
V-man
V-man
Maybe this mistake?
http://www.opengl.org/wiki/GLSL_:_common_mistakes#glUniform_doesn.27t_work
modus
modus
... Looks like you're sampling your cube in eye-space, which obviously should be in agreement with the cubemap's content ...
zhou_f_f
zhou_f_f
cube mapping Result

The "BaseColor" has been transfer to the fragment shader.

sorry, I don't know how to post image. I put the image in root folder in c disk , and set src="c:\cubemappingResult.jpeg"
alt="cube mapping result", but the image can not be shown.

[Edited by - zhou_f_f on October 8, 2009 4:02:09 PM]
knighty
knighty
Maybe your cubemap is too dark. Try to give MixRatio lower values.
Chek if sofa_*.bmp are properly loded. Are they in the same folder as your .exe?



zhou_f_f
zhou_f_f
I work it out.

The cube texture should be loaded after the initiation of the light and material.

Topic Locked

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

Sign in to reply to this topic.