PDA

View Full Version : FBO & cube map multitexturing



dletozeun
03-03-2008, 11:58 AM
Hello,

Still working on cube maps, I need to use multitexturing to apply diffuse and specular colors using diffuse and specular reflection cube maps.

First I try to do this without render to texture using a fbo.

I use a very simple GLSL shader to compute lighting:

vertex shader:

varying vec3 normal;
varying vec3 position;
uniform vec3 eyePos;

void main()

{
normal = normalize(gl_NormalMatrix * gl_Normal);

gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;


gl_Position = ftransform();
position = gl_Position.xyz;

}

fragment shader:

uniform samplerCube cubeMapD;
uniform samplerCube cubeMapS;
uniform vec3 eyePos;
varying vec3 normal;
varying vec3 position;

void main()

{
vec3 eyeDir=normalize(eyePos-position);

vec3 texCoords=normalize(eyeDir-2.0*dot(normal, eyeDir)*normal);


gl_FragColor = vec4(1.0, 0.6, 0.1, 1.0)*textureCube(cubeMapD, texCoords)/3.0 + textureCube(cubeMapS, texCoords)/5.0;

}

You see that in this code I compute twice the cube map texture coordinates but the classic way to do this does not work :
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;

whereas I generate automatically the texture coordinates doing this after loading the cube map texture using glTexImage2D:

glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);

glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glEnable(GL_TEXTURE_GEN_R);


glEnable(GL_TEXTURE_CUBE_MAP_ARB);
glEnable(GL_NORMALIZE);

this is the render I obtain when I use texCoords in the textureCube function:

http://img513.imageshack.us/img513/8341/capture1vb4.th.png (http://img513.imageshack.us/my.php?image=capture1vb4.png)

this what I obtain when I use gl_TexCoord[0].stp in the textureCube function:

http://img408.imageshack.us/img408/1999/capture2cx4.th.png (http://img408.imageshack.us/my.php?image=capture2cx4.png)

seeing this last picture, the texture coordinates clearly seem to not be generated...

Does someone see the problem?

Another strange thing is when I set up the texture units from TU1 to TU0 like this:

glActiveTexture( GL_TEXTURE1_ARB );
glDisable(GL_TEXTURE_2D);
skyBoxS->bind(); // specular reflection map

glActiveTexture( GL_TEXTURE0_ARB );
glDisable(GL_TEXTURE_2D);
skyBoxD->bind(); // diffuse reflection map

I works, but when I do the same from TU0 to TU1 I see simply nothing in the framebuffer! I don't understand why...



Thank you for your help.

dletozeun
03-04-2008, 05:32 AM
Nobody see the problem? Even about the problem with texture units order?

Zengar
03-04-2008, 06:11 AM
Quote: " A vertex shader replaces the transformation, texture coordinate generation and lighting parts of OpenGL, and it also adds texture access at the vertex level." (the vertex sahder extension)
Solution: generate the texture coordinates in the shader yourself.

I don't understand the second question. What is working/not working?

dletozeun
03-04-2008, 06:28 AM
Thank you for your answer

According to your quote, automatic texture coordinates generation do not work for a shader? Just texture coordinates given by glTexCoord* or glMultiTexCoord* can be used by the shaders?

for the second question:

I just need to bind two cube map textures that I want both to apply on the same polygon using multitexturing.
So I bind the first cube map with skyBoxS->bind() ( it just call glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, glID)) and the second skyBoxD->bind(); I need two TU to do this : TU0 and TU1. But the very strange thing is when I set up the TU0 first and TU1 after, nothing is rendered or visible at least. When I do the same in the reverse order with TU1 set up first and TU0, it works!?

V-man
03-04-2008, 08:29 AM
For the shaders, make sure you setup the sampler uniforms.
You need to of course bind the shader first with glUseProgram(pppp) and then
glUniform1i(location1, 0); //for sampler 0
glUniform1i(location1, 1); //for sampler 1

and in your C++ code :

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBEMAP, texid);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_CUBEMAP, texid2);

dletozeun
03-04-2008, 09:08 AM
Ahh yes! I am so stupid...

I Have already done this:

glUniform1i(location1, 0); //for sampler 0
glUniform1i(location1, 1); //for sampler 1

but I called glUseProgram after! It is not good with the fixed functionality ^^
Thanks a lot.