View Full Version : textures

08-24-2005, 11:58 AM

I'm currently reading the GLSL specs and I'm facing a question.

We assign texture coordinates for units threw arrays (gl_TexCoord[unit]) and it's the same for the texture matrix (gl_TextureMatrix[unit]). But for reading the incoming texture coordinates, this is not done threw arrays (gl_MultiTexCoord0, gl_MultiTexCoord1...).

I'm sure there might have a logical reason for not having set the later in arrays. But what is it ?
Some of the things I find not well is that we must do as much shaders as texture units are used (or use plenty if statements).

Also I would like to know how to know how much texture units are used for a vertex/fragment. For example, if a model use only 2 texture units, how to know inside the shaders that we use exactly 2 units ?

Thank you in advance.

08-25-2005, 05:33 AM

I don't really see the point in your question but well...

gl_MultiTexCoordX is a built in varying variable used in vertex shaders, when passed to the a fragment shader, it becomes gl_TexCoord[X].
As textures are usually used in fragment shading -especially when multitexturing-, I think that the GLSL conceptors allowed the use of the tex units as arrays so that we can use them in loops or in conditionnal statements, which simplifies the writing of the code.

For example :

gl_FragColor = vec4(0.0);
for (tex_unit=0; tex_unit <8; tex_unit++)
//Access the correct texel
texel = vec4(texture2D(tex[tex_unit], gl_TexCoord[tex_unit].xy));
//Blend with the one before
mix(glFragColor, texel, texel.a);
}So as you can see, you donnot need as many shaders as texture units...

The texture units that a vertex/fragment shader uses are the one you declare AND use in your code, that's it. All must be previously binded in your GL code.

Don't know if it helps,


08-25-2005, 06:20 AM
Hi and thank you for your care.

The main thing I wanted to point is that:

void TextureFunction (int texture_unit)
gl_TexCoord[texture_unit] = gl_TextureMatrix[texture_unit] * gl_MultiTexCoord0;
// note that gl_MultiTexCoord is NOT an array !
}It happens in the vertex shader. As you can see, you use arrays for the two first values but not for the last one. So such an above function could not really exists.

I was gessing we could do something like that:

void TextureFunction (int texture_unit)
gl_TexCoord[texture_unit] = gl_TextureMatrix[texture_unit] * gl_MultiTexCoord[texture_unit];
// note that all are arrays !
}The fact gl_MultiTexCoord is not an array makes me think that we cannot do such functions. But that also makes me feel that I miss some points because I was understanding textures in shaders the way I just talked about above.

Also, for my second request, let's see a scenario (very simple):

A vertex (with one vertex value and two texture coordinates) enters a vertex shader. It seems this shader must know that the entering vertex has exactly two texture coordinates. So, it seems we must use some uniform variables in order to know that. Otherwise, noone will be able to know how much texture coordinates the shader must take care: if it's only a single one, it will miss the last one, if it's more than two, it will be wrong calculating non-used texture coordinates.
Maybe there's a function or some built-in variables allowing us to know how much texture units are in used, but I don't know that.

08-25-2005, 08:16 AM
gl_MultiTexCoordX is a built in varying variable used in vertex shaders, when passed to the a fragment shader, it becomes gl_TexCoord[X].This is incorrect. glMultiTexCoordX is a built in attribute. gl_TexCoord[X] is a built in varying, primarily used in the fragment shader when you don't have a vertex shader to provide texture coordinates from fixed function OpenGL.

The definitive answer to the original poster's question can be derived from the above clarification alongside a look at the specification. In the GLSL (http://en.wikipedia.org/wiki/GLSL) spec it clearly states in section 4.3.4 that "Attribute variables cannot be declared as arrays or structures."

08-25-2005, 10:01 AM
Sorry kingjosh, that is what I meant, I guess I should try sleeping instead of debugging my code :)

I would just like to point out that I've never transformed my texture coordinates (unsing the texture matrix) in vertex shaders and the results were always correct. Maybe the fixed functionnality does it if we do not do it ourselves (like not writing the depth for a fragment when the Zbuffer is activated)?