Original Post
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.
