🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

[GLSL] Multitexture-Layers detecting the active ones.

Started by
4 comments, last by Dani008 17 years, 10 months ago
Hello, Does anybody know how to detect in a GLSL script what multitexturing layers are used? I need it because some parts of my game use multitexturing and others don't. I thought it would work through a uniform, controlled by the main application, but it seems like it doesn't change the uniform in runtime, just when flipping the buffers. Fragmentpart:
uniform sampler2D tex0,tex1;

// Some vars for later use
varying vec3 normal;
varying vec3 pos;

void main()
{
  // What should be in the following condition to give back true, when
  // multitexture-layer 1(the second) is active? How to detect this?
  if(false) {
    // Do something when multitexturing is on.
  }else{
    gl_FragColor = texture2D(tex0,gl_TexCoord[0].st) * gl_Color;
  }
}
Vertexpart:
varying vec3 normal;
varying vec3 pos;

void main(void)
{
  gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
  gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1;
  
  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
  gl_FrontColor = gl_Color;
  
  normal = gl_NormalMatrix * gl_Normal;
  pos = vec3(gl_Position);
}
Thanks
Advertisement
Well, you have a few different options. First, you could write a different shader for each number of texture layers. Just write and compile both shaders, and switch the programs, using glUseProgram(), between the two dependent upon how many textures you are using. The other option, which is also very simple, is to pass in, as a variable to your shader, how many textures you are using. You pass in the variable in your main program, and read it from a variable in the shader, how to pass variables to shaders is common knowledge and available in any decent tutorial on GLSL.
That's the issue with supporting shaders in a way which is not integrated.
So, short answer: you cannot - at least, GL won't help with a special func returning the information.
Somewhat more involved answer: you should parse out the source and check the number of uniforms. GL can help you thuru ActiveUniforms, which enumerates the various uniforms to be used... if you find more than one uniform, it's likely the shader is using "multitexturing". The issue is that if a uniform is not "active" then the uniform is not there, which is odd in some cases.

Anyway, after experimenting with it I am convinced a good engine should not allow using shaders explicitly.

Previously "Krohm"

Hello,

Thanks for your replies. It helped a bit. First it didn't work, but the layercount works now. But can you tell me what is the format of the sampler2D datatype? I know it's integer, isn't it? But is it big or little endian? I am using a different programming language and had to implement all the functions myself. Passing the textures isn't working at the moment. Just the first texture is even without my help passed to the shader sucessfully. The whole application crashes when using the ID of the texture, passed by me in the shader and as there is no debug output or such I can't get the value of it.
Thanks
You should not pass the texture id, but rather the texture number 0,1,2,3 and so on.
Check the shader tutorials on my site for a better explanation.
Ahh, I thought I have to pass the texture ID which I got from glGenTextures, but it's the texturelayer. Thanks thanks thanks!
Thanks

This topic is closed to new replies.

Advertisement