PDA

View Full Version : Flow Control : Using Single Shader HELP



Jedimaster
07-02-2006, 10:30 PM
I just want to using single shader in my program.
I have 2 texture Units,one is for CubeMap,other is for 2D texture.

Now, In draw function,structure like this:

glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_CUBEMAP,cubetex);
glUniform1i(getUniLoc(ShaderProg,"NeedCubeMap"),1);
/*
Draw something too
*/


glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D,2dtex);
glUniform1i(getUniLoc(ShaderProg,"NeedCubeMap"),0);
/*
Draw something too...
*/

In Fragment Shader,Just like this:

uniform samplerCube cubemap; //0
uniform sampler2D tex2d; //1

uniform bool NeedCubeMap;

varying float LightCoeff;
varying vec3 vUV3D;
varying vec2 vUV2D;

void main(void)
{

if( NeedCubeMap ){
vec4 cubecolor = textureCube(cubemap,vUV3D);
gl_FragColor = cubecolor*LightCoeff;
}else{
vec4 tex2dcolor = texture2D(tex2d,vUV2D);
gl_FragColor = tex2dcolor*LightCoeff;
}
}

but when the program is running,it's black,nothing...I thinks maybe the frame buffer was not written,but when I cut off anything about the "NeedCubeMap",it just good. Where is the wrong ? :confused: :confused: :confused:

Relic
07-03-2006, 01:32 AM
Did use set the samplers to 0 and 1?
This needs to be in your code somewhere:
glUniform1i(getUniLoc(ShaderProg,"cubeMap"),0);
glUniform1i(getUniLoc(ShaderProg,"tex2d"),1);

Jedimaster
07-03-2006, 02:14 AM
Yes,I had combine Sampler2D with Uniform tex2d1 and the same to Cubemap

Relic
07-03-2006, 06:02 AM
Post some more code then.

Any compile or link errors of the shaders?
Any glGetError hit?
Is the shader program in use when you call glUniform? It has no program parameter, so it can only affect the currently bound program.
If you set the glClearColor to non-black, do see something rendered at all?
What's your vertex shader?

Try this:

void main(void)
{
vec4 color;
if( NeedCubeMap ){
color = textureCube(cubemap,vUV3D);
}else{
color = texture2D(tex2d,vUV2D);
}
gl_FragColor = color * vec4(LightCoeff);
}

Jedimaster
07-03-2006, 11:28 AM
:p :p :p :p
I just find where I was wrong !

I am wrong to use Sampler3D instead of SamplerCube

and the same to function texture3D and textureCube

Now Everying is correct!

Thanks Everyone!