0

I was looking at a code example for a phong lighting shader. It used the predefined variables of gl_Normal, gl_Vertex, and gl_ModelViewProjectionMatrix in the vertex shader. My current vertex shader looks like this.

#version 150 core

in vec4 in_Position;
in vec4 in_Color;
in vec2 in_TextureCoord;

uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;

out vec4 pass_Color;
out vec2 pass_TextureCoord;

void main(void) {
    gl_Position = projection * view * model * in_Position;

    pass_Color = in_Color;
    pass_TextureCoord = in_TextureCoord;
}

I was wondering is it possible to set the value of the predefined vars. For example, I would set the value of gl_ModelViewProjectionMatrix to my projection uniform. I'm asking this because whenever I try to use the predefined vars, the shader doesn't work.

genpfault
  • 51,148
  • 11
  • 85
  • 139
irishpatrick
  • 137
  • 1
  • 13

1 Answers1

2

Predefined variables like gl_ProjectionMatrix are only available in the compatibility profile. Since you specify the core profile for your shader, you will not be able to use them:

#version 150 core

If you were using the compatibility profile (and I'm in no way suggesting that it would be a good idea), you could use them. You would then set the value of gl_ProjectionMatrix with:

glMatrixMode(GL_PROJECTION);
glLoadMatrix(...);

These are of course also compatibility profile calls, and not available in the core profile. But it's really not any simpler than setting the value of your projection uniform with a glUniformMatrix4fv() call. So there's no good reason to move back to the past.

I wrote a more detailed description of what happened to built-in GLSL variables in the transition to the core profile in an answer here: GLSL - Using custom output attribute instead of gl_Position.

Community
  • 1
  • 1
Reto Koradi
  • 53,228
  • 8
  • 93
  • 133