glsl inputs


A glsl shader can be provided a buch of different input types, either via


Examples are:

in vec4 p3d_mspos_campos; //nr 1
uniform mat4 p3d_ModelViewProjectionMatrix; //nr 2
uniform mat4 trans_world_to_model; //nr 3

I chose these examples carefully since, even though they are all automatically provided by panda, they are all different in some way:

nr 1 requires the in… command and has a “p3d_” prefix
nr 2 requires the uniform… command and has a “p3d_” prefix
nr 3 requires the uniform… command and has no “p3d_” prefixWhat is the logic behind these differences and how can i predict which style to use?

Furthermore there are also variables provided by OpenGl, such as gl_TexCoord[0] and gl_Normal, which you can always use/change without ‘importing’ them. My final question is then, what is the difference between for instance the p3d_Normal, and the gl_Normal?

Many thanks!

nr. 1 is invalid. This is not a shader input that is accepted by Panda, and if it would be, you have to use the “uniform” keyword to declare it as a uniform.
nr. 2 is the recommended way to use inputs from GLSL and it mimics the classical OpenGL gl_ModelViewProjectionMatrix inputs.
nr. 3 is the legacy Panda way of doing it, which was adopted from the Cg shader support by a developer who found the inputs provided by the nr. 2 syntax too limiting in functionality.

Keep in mind that the “in” prefix is implicit; there’s no difference whether you specify it or not. (Older GLSL versions don’t have this keyword at all, in fact.)

As for the “uniform” keyword, you need to specify it for inputs which are global to the model (such as ones that are specified via setShaderInput, or the transform matrices). Not specifying one at all will cause it to default to “varying”, which indicates a variable that is passed from one shader to another.

The difference between gl_Normal and p3d_Normal is that more recent versions of GLSL no longer have gl_Normal, so p3d_Normal is needed if you want to write your shader against a newer version of the GLSL specification.

Im pretty sure about the ‘in’ keyword. When i change it to uniform the shader stops working. Nor can i just leave it out. This is the case for these imports:

in vec4 p3d_Vertex; 
in vec4 p3d_MultiTexCoord0;
in vec4 p3d_mspos_campos;
in mat4 p3d_texmat_0; 

I am using version 110 (i think… i added the line “#version 110” at the top of the shader)

As for the difference between nr 2 and nr3, how would i know beforehand if i must use the “p3d_” prefix when i want a specific input? Is it correct that all the ‘standard’ opengl imports ( are supplied by panda using the second style and the other, panda specific, inputs use the thirds style?

Keep in mind that p3d_Vertex and p3dMultiTexCoord0 is a completely different type of input from mspos_campos and texmat_0. The former two are vertex attributes and are different for each shader invocation, the latter two are uniforms. You have to declare uniforms as uniforms, however.

This is the correct declaration in GLSL 110:

attribute vec4 p3d_Vertex;
attribute vec4 p3d_MultiTexCoord0;
uniform vec4 mspos_campos;
uniform mat4 texmat_0;

(In fact, in 110, you could just as easily use gl_Vertex, gl_MultiTexCoord0, and gl_TextureMatrix[0].

GLSL 110 does not have the “in” or “out” keywords. The “in” keyword is a feature in more recent versions of GLSL and is a bit magical; for vertex shaders, it means “attribute”, and for other shaders, it means “varying”. It is never used for uniforms.
Some, but not all, of the default OpenGL-style inputs are supported by Panda under the p3d_ prefix. However, if you use version 110, you can use the standard gl_ inputs.

I do want to point out again that p3d_mspos_campos and p3d_texmat_0 are not inputs that Panda3D supports. If they do work, then that is a bug. The names are “mspos_campos” and “texmat_0”.

Using the ‘in’ statement in v110 wasn’t returning an error message though…

Anyway, i think i understand the logic behid the input statements/styles:
Whenever something is vertex/fragment specific use attribute+out/in
Whenever something is the same for the entire model(/shaded thing) use uniform

Whenever something is implemented by gl use either p3d_ or gl_ depending on the version (still a bit vague on this, any last words are welcome, especially icw:)
Some other panda specific stuff is also available without a prefix (for instance: uniform mat4 trans_model_to_world; )

ps. my shader isnt accepting the texmat_0 call, I used it in cg to get the reflection working. Does it have a different name in glsl? Perhaps i should make this a seperate post…

You could try gl_TextureMatrix[0].

Nope, already tried that.
google tells me gl_TextureMatrix[0] is only a placeholder matrix; when nothing is stored the identity matrix. Is it supposed to be filled by panda? (when i call NP.projectTexture perhaps…)

I’m not sure what you expect to be there, but it’s going to be identity unless you actually use projectTexture / setTexScale / setTexPos or any of the texture transform methods.

Yes i did use the project texture method.
Good to know the setTexScale / setTexPos type methods also affect the gl_TextureMatrix[0] parameter.

Ill start a new thread on this issue since it is going somewhat off-topic. Thanks for your help on the gl inputs.