GLSL Questions

Started by
6 comments, last by Duel93 17 years, 6 months ago
I've worked with Cg and now I'm learning GLSL. I'm stuck on a couple of issues. I've read some topics on this here, but am still a little confused. 1) With Cg, it's easy to use just a vertex shader on a textured object. When trying to do this with GLSL, my object does not texture, but is black. I want to just apply vertex changes and let the pipeline handle the fragment part. My vertex shader outputs the modelviewprojection position and I copy the gl_MultiTexCoord0 to gl_Texcoord[0], as with Cg. Do I need to do anything else? I'm only binding the vertex shader, so I'm not accidentally binding a fragment shader. 2) Again with Cg, it's easy to create modular shaders. I can have a separate shader that, for example, may just do lighting. I can include that shader in any other shader where needed. When uploading light values, I just upload it to the lighting shader. It seems with GLSL, I have to include a lighting routine in each shader where needed, or if I programmatically handle includes with a "#pragma include", I still have to upload light values to each shader where needed, since when parsing the shader, you just append this "include" with the original shader. The GLSL book has one paragraph on modularity that I can see, and it basically just says that it "supports modular shaders"...but how? Thanks for any help on this.
Advertisement
Well, in GLSL it's easy to just use a vertex shader on a textured object.
1. Create your vertex shader.
2. Create your shader program in code.
3. Use glUseProgram(shader) to turn the shader on before you render your textured object.
4. Use glUseProgram(0) to turn the shader off.

I'm also not completely sure of the "modularity" of shaders but I do know that if you create a light for your scene in OpenGL then you can easily apply that light per shader by using glLightSource[0] (or whatever light number) in GLSL.
There must be something basic I'm missing, or a state that is not set correctly. I'm applying the vertex shader as you said, but still get the non-textured black object as a result (And the texture is set).

As far as the modularity, perhaps I chose a bad example. The light shader was used to make my point about having to upload values for each shader, instead of once for a 'utility' shader. It just seems very inefficient and burdensome to have to do that.
Post your OpenGL code (simplified if possible) as well as your shader code and perhaps someone else or myself can help.

I'm not trying to be argumentative about this but to me it seems more ineffectient to have a 'utility' shader for lighting when all you have to do is set your light and lighting position in OpenGL code then simply use the light source number in your shader. If you use glLightSource[0] and make light changes in the OpenGL state machine then you never have to send the light to the shader ever.

I'm sure there's a way to use some time of 'utility' shader with GLSL but I just haven't found a practical application that would need such a thing yet, therefore I've done no research on it.
The light example, as stated earlier, is a bad example. I understand that you have access to OpenGL light properties within the shader. I was just trying to give an example where you can modularize the shader.

I am trying to use glAttachShader, which will allow you to attach a number of modules. So, if I have a utility shader that does wind effects, I can attach that to a shader that requires the use of it. I may have a tree shader, and a flag shader that require wind effects. Instead of having a wind effect function in my flag and tree shader, I can just attach my utility shader to use the wind effect functions.

My problem now is that those functions are undefined when compiled. I don't understand how to provide a "#include", so to speak (I know GLSL does not support it).

For example, my flag shader will call "windEffect()", which resides in my utility shader. However, when the flag shader is compiled, windEffect is undefined. How do I make extern functions visible?

Thanks for your help
You have to declare the function before it is used, much like with C or C++.
Note; declare, not define.
IIRC you can only have one shader bound at a time. So if you have a second shader that has the functions you need I don't see anyway to get access to it... Unless I am not understanding you. It may be easier to just put the required functions with each shader at the top of the program...
Thanks phantom! The shader compiles now.

This topic is closed to new replies.

Advertisement