Automatically identify color source

Hi everyone!

I want to write a simple fragment shader which multiplies an extra alpha value to each drawn object in a collection, no matter the type of the object. However, as I am new to shaders, my solution is rather clumsy:


uniform sampler2D testTexture;

// alpha value to use
uniform float alpha;

// to decide where the color of the fragment comes from
uniform int textured = 0;

void main(void) {
 if (gl_TexCoord[0].s < 0.0 || gl_TexCoord[0].s > 1.0) discard;
 if (gl_TexCoord[0].t < 0.0 || gl_TexCoord[0].t > 1.0) discard;

 if (textured > 0) {
  gl_FragColor = texture2D(testTexture, gl_TexCoord[0].xy);
  gl_FragColor = gl_FragColor * alpha;
 } else {
  gl_FragColor = gl_Color * alpha;
 }
}

As there seem to be two sources for the color (textured and gl_Color, depending on the renderable object) and I do not know how to automatically tell which one to take, I have this extra field “textured” which I have to set manually for each object.

Is there a way to automatically identify the correct source of the fragment color?

Thanks a lot!

There is no correct source. It is you who decidse whether to render an object with a texture or a solid colour; so you have to indicate to the fragment shader what to choose. How you do this is up to you. The reason for shaders is to remove
an resriction on how a mesh is rendered. The up side is you can do what every you like - chose between texture or colour or use both combined; the down side is you have to code every thing for yourself.

Is there a way to automatically identify the correct source of the fragment color?

Perhaps a separate shader for each ‘class’ of object?

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.