GLSL shaders: how to use p3d_LightSource?

Back with another basic question about shaders that I’m hoping you guys can help with. Here’s the questions up front, then I’ll give some context: What is contained within the p3d_LightSource struct for a directional light? I’m using p3d_LightSource[0].position.xyz and the value appears to not be constant. I would expect the XYZ of my light to be where I set it, but that seems to not be the case.

Here’s my fragment shader code (I’ve tried to limit most of the work to there for organization sake):

#version 330 core

out vec4 FragColor;
in vec4 newColor;
in vec3 Normal;
in vec3 fragPos;
uniform mat4 p3d_ModelMatrix;
uniform mat4 p3d_ModelViewProjectionMatrix;
uniform struct {
    vec4 ambient;
    } p3d_LightModel;
uniform struct {
    vec4 color;
    vec4 position;
    vec3 spotDirection;
    float spotExponent;
    float spotCutoff;
    float spotCosCutoff;
    vec3 attenuation;
    sampler2DShadow shadowMap;
    mat4 shadowViewMatrix;
    } p3d_LightSource[10];

void main()
{
  vec3 light_color = vec3(1.0,0.0,0.0);
  vec3 vertex_norm = normalize(Normal);
  vec3 md_lightPos = p3d_LightSource[0].position.xyz;
  vec3 lightDir = normalize(md_lightPos);
  float diff_factor = max(dot(vertex_norm, lightDir), 0.0);
  float clamped_diff_factor = clamp(diff_factor, 0.0,1.0);
  vec3 diffuse = clamped_diff_factor * light_color;
  FragColor = vec4(diffuse, 1.0);
}

In my scene I have just simple 1x1x1 box in the center of the world. There is only one light, a directional light, defined as follows:

        directionalLight = DirectionalLight("directionalLight")
        light_as_node = self.render.attach_new_node(directionalLight)
        light_as_node.set_pos(1, 0, 0)
        light_as_node.look_at(0, 0, 0)
        light_as_node.node().set_color((1.0, 1.0, 0.0, 1))
        light_as_node.node().get_lens().set_near_far(1, 250)
        self.render.set_light(light_as_node)
        self.dlight = light_as_node

When I run the program, the top of the box has a red color, but then as I rotate around the box, different faces light up red as I move around. If I change md_lightPos to a hardcoded value (like vec3(1.0,0.0,0.0)) then only the face in the positive X direction gets lit up, as I would expect, even as I rotate around. So, what exactly is in p3d_LightSource[0].position.xyz? Why is it not constant? Is there a transform I should be doing?

for completeness sake, here’s my vertex shader code:

#version 330 core

in vec4 p3d_Vertex;
in vec3 p3d_Normal;
in mat3 p3d_NormalMatrix;
uniform float varier;
out vec4 newColor;
out vec3 Normal;
out vec3 fragPos;
uniform mat4 p3d_ModelViewProjectionMatrix;
uniform mat4 p3d_ModelMatrix;
uniform struct {
    vec4 color;
    vec4 position;
    sampler2DShadow shadowMap;
    mat4 shadowViewMatrix;
    } p3d_LightSource[3];


void main()
{
  gl_Position = p3d_ModelViewProjectionMatrix * p3d_Vertex;
  vec4 md_Position = p3d_ModelMatrix*p3d_Vertex;
  newColor = vec4(0.0,0.0,1.0,1.0);
  Normal = p3d_Normal;
  fragPos = vec3(md_Position.xyz);
}

Also, as a side question: when I declared the p3d_LightSource struct, I put in a random number that was greater than 1 (as I only have 1 light). What values end up in the other items in the struct? Is there a uniform variable to tell the shader how many total lights there are?

The light position is given in view space. I don’t have time to read through your shaders in detail right now, but I’ll add in some links for some example shaders for GLSL lighting that you could use as reference.

The size of the p3d_LightSource struct can be tweaked based on how many lights you are able to handle in your frame budget. If you declare too many, the remaining lights will automatically be black and not contribute. If you declare too few, then Panda will cull based on the most high-priority lights, as defined by the lights’ priority attributes.

Here are the links:

(Slightly outdated):
http://rdb.name/glsl-lighting.zip

Thank you @rdb! I appreciate you taking the time to answer my noob questions. From your examples I was able to glean what the solution is. Since the light position was in viewspace, I needed to transform the vertex normals to viewspace using the p3d_NormalMatrix. Before, I was thinking I needed to transform the light position, but from your code I saw that it was the vertex normals that needed to be transformed. I’ll continue to look through the links as I march forward with learning shaders, I think they’ll be very helpful. Thanks again!