   GLSL Non-Textured and Textured
  Hey guys, I'm back and this time I'm moving up a few rungs on the OpenGL ladder :D I've begun to work with shaders, specifically GLSL and am quite impressed with it so far.It's a little sad that for some obtuse reason that OpenGL apps on my brand-spankin'-new laptop are capped to 60 frames a second (and not a single tick over) and when I run the simplest of Fragment shaders and Vertex shaders, the app drops to a crawl of 29 fps, but since I'm not working on GLSL for my computer and more for other people with gaming machines I'm not too phased.Now... I was constructing a shader program that would shade my model in the infamous "toon-style" after having made a per-pixel lighting shader for directional lights, and I ran into a little issue.It was all fine and good to work with untextured materials on their own, or textured materials on their own, but when it came to both under the one shader I was completely dumbfounded that I could not find any information on how to construct a lighting shader that operated on both non-textured and textured geometry, even coming across someone telling a newbie that they HAD to apply a 1x1 white texture to their mesh in order for it to work with textures :/ now that's just simply not on.This is totally unacceptable in my opinion because Fixed Function is capable of lighting and shading un-textured and textured geometry, why can't the supposedly superior GLSLang do it?  I thought FF was becoming deprecated since GL 3.0?So I set about figuring a way to get past this problem of my untextured geometry being black (It seems when you bind 0 to the texture state, GLSL returns a black texel with full opacity.  So: vec4(0.0, 0.0, 0.0, 1.0);)And before someone asks me why on earth my geometry didn't have all textures or just all material colors, I am working with a cartoon styled model and art theme.  The majority of the model(s) are made up of materials without a texture, and use a single texture to map detail on parts that need it.I tried a half dozen things which I can't remember so don't ask and have finally settled for the following shaders - Vertex Shader:varying vec3 lightDir,normal;void main(){lightDir = normalize(vec3(gl_LightSource[0].position));normal = gl_NormalMatrix * gl_Normal;vec4 diffuse = gl_Color * gl_LightSource[0].diffuse;    gl_TexCoord[0] = gl_MultiTexCoord0;    gl_FrontColor = diffuse;gl_Position = ftransform();}Fragment Shader:uniform sampler2D tex;varying vec3 lightDir,normal;void main(){float intensity;vec4 color = gl_Color;vec3 l = normalize(vec3(gl_LightSource[0].position));vec3 n = normalize(normal);intensity = dot(l,n);if (intensity >= 0.75) color = gl_Color * 0.9;else if (intensity > 0.5) color = gl_Color * 0.8;else if (intensity > 0.35) color = gl_Color * 0.7;else if (intensity > 0.2) color = gl_Color * 0.6;else color = gl_Color * 0.6;    //Textured material fix    if (texture2D(tex,gl_TexCoord[0].st) != vec4(0.0,0.0,0.0,1.0))    {   color *=  texture2D(tex,gl_TexCoord[0].st);    }gl_FragColor = color;}And before the syntax police dives on top of me, remember that I only started GLSL 3-4 days ago and I wrote these myself with a little help from references on the internet (and of course, knowledge of how these things kind of work :P )Now I'm really just curious as to whether there is a far simpler way to do this as I shouldn't have to sacrifice pure black but opaque textures in order to do this.I did scout around as I mentioned above and found nothing relatively useful.  And before I'm told that I have to split my shaders up so there's one for textured geometry and one for un-textured geometry, remember that I'm just lumping the geometry into an array and sending it off with Vertex pointers and what not.   
  There's two ways:1) Make a shader that requires a diffuse texture. If you don't want to use a texture, then you supply that 1x1 white texture as a default one.2) Make two shaders - one that does texturing and one that doesn't.You should note that the FF pipeline is actually implemented inside the graphics driver using shaders! So anthing the FFP does, you can do too.The FF pipeline probably takes the second option - inside the driver there might be hundreds of different shaders that it uses depending on the options you've got enabled. This is often called the 'shader permutation problem'.Usually each of the different options inside your shader would be surrounded by a preprocessor block, like:#ifdef DO_TEXTURING   color *=  texture2D(tex,gl_TexCoord[0].st);#endif;Quote:Original post by HodgmanYou should note that the FF pipeline is actually implemented inside the graphics driver using shaders! So anthing the FFP does, you can do too.Hrm yes that was what I was hinting at when I said: Quote:This is totally unacceptable in my opinion because Fixed Function is capable of lighting and shading un-textured and textured geometry, why can't the supposedly superior GLSLang do it? because I knew that we could do whatever the FFP can do with our own GLSL shaders, problem is I can't seem to find anything that says how to find out if a texture was bound or not (eg; glBindTexture(myTexture); or glBindTexture(0);) I'd like to know how we can find out if there was a texture bound, because my drawing code calls.   ;  u could have a flag uniforma single bool, int, float which u set if textured and then check against itor just use the 2 shaders and manag them in some way so that u don't have to manag them manuelly   ;Quote:Original post by RexHunter99problem is I can't seem to find anything that says how to find out if a texture was bound or notYeah, that functionality doesn't exist in GLSL. As Danny02 says, you can create a uniform bool and put the value in there yourself, but branching inside shaders is usually slow. In some cases it's preferable to have two different shaders instead.You can select the correct shader at the same time that you set the parameters for the shader (binding a texture is similar to setting a uniform variable).if( material->textured ){  glBindTexture(myTexture);  /*set textured shader*/;}else{  glBindTexture(0);  /*set non-textured shader*/;}