I am working on a project in GLSL (of which I have no experience in). I am currently not coding anything myself, just trying to run someone else's code. Unfortunately, they wrote their code using version 120, and I am trying to run it with version 330. Note: I am running this on a Mac with version 10.15. Here is the code:
# version 330 core
in vec3 mynormal;
in vec4 myvertex;
uniform mat4 modelview;
out vec4 fragColor;
uniform vec3 color;
const int numLights = 10;
uniform bool enablelighting; // are we lighting at all (global).
uniform vec4 lightposn[numLights]; // positions of lights
uniform vec4 lightcolor[numLights]; // colors of lights
uniform int numused; // number of lights used
uniform vec4 ambient;
uniform vec4 diffuse;
uniform vec4 specular;
uniform vec4 emission;
uniform float shininess;
vec4 ComputeLight (const in vec3 direction, const in vec4 lightcolor, const in vec3 normal, const in vec3 halfvec, const in vec4 mydiffuse, const in vec4 myspecular, const in float myshininess) {
float nDotL = max(dot(normal, direction), 0.0);
vec4 diffuse = mydiffuse * lightcolor * nDotL;
float nDotH = max(dot(normal, halfvec), 0.0);
vec4 specular = myspecular * lightcolor * pow(nDotH, myshininess);
return diffuse + specular;
}
void main (void)
{
if (enablelighting) {
const vec3 eyepos = vec3(0,0,0) ;
vec4 _mypos = gl_ModelViewMatrix * myvertex ;
vec3 mypos = _mypos.xyz / _mypos.w;
vec3 eyedir = normalize(eyepos - mypos);
vec3 _normal = (gl_ModelViewMatrixInverseTranspose*vec4(mynormal,0.0)).xyz ;
vec3 normal = normalize(_normal);
gl_FragColor = ambient;
for (int i = 0; i < numused; ++i) {
vec3 light_direction;
if (lightposn[i].w == 0) {
light_direction = lightposn[i].xyz;
} else {
vec3 light_position = lightposn[i].xyz / lightposn[i].w ;
light_direction = normalize(light_position - mypos); // no attenuation
}
vec3 half = normalize (light_direction + eyedir);
vec4 col = ComputeLight(light_direction, lightcolor[i], normal, half, diffuse, specular, shininess);
gl_FragColor += col;
}
}
else gl_FragColor = color ;
}
And here are the errors that I'm getting:
Compile Error, Log Below
ERROR: 0:60: Use of undeclared identifier 'gl_ModelViewMatrix'
ERROR: 0:61: Use of undeclared identifier '_mypos'
ERROR: 0:61: Use of undeclared identifier '_mypos'
ERROR: 0:63: Use of undeclared identifier 'mypos'
ERROR: 0:64: Use of undeclared identifier 'gl_NormalMatrix'
ERROR: 0:79: Use of undeclared identifier 'mypos'
ERROR: 0:90: Use of undeclared identifier 'mypos'
ERROR: 0:101: Use of undeclared identifier 'mypos'
ERROR: 0:112: Use of undeclared identifier 'mypos'
ERROR: 0:123: Use of undeclared identifier 'mypos'
ERROR: 0:134: Use of undeclared identifier 'mypos'
ERROR: 0:145: Use of undeclared identifier 'mypos'
ERROR: 0:156: Use of undeclared identifier 'mypos'
ERROR: 0:167: Use of undeclared identifier 'mypos'
ERROR: 0:178: Use of undeclared identifier 'mypos'
ERROR: 0:193: Use of undeclared identifier 'eyedirn'
ERROR: 0:196: Use of undeclared identifier 'normal'
ERROR: 0:196: Use of undeclared identifier 'halfy'
ERROR: 0:197: Use of undeclared identifier 'colApp'
ERROR: 0:206: Use of undeclared identifier 'gl_FragColor'
ERROR: 0:208: Use of undeclared identifier 'gl_FragColor'
After googling for a while, I realize that this is an issue with compatibility between versions. However, I don't know enough about GLSL to know how to fix this. I am hoping to modify this file to run instead of trying to write it myself to be compatible for version 330. Can anyone give me advice on how to modify this code to run for me?
Up to OpenGL 2.1. geometry was drawn by glBegin
/glEnd
sequences or fixed function attributes. The vertex coordinates have been transformed by the current model view and projection matrix and no shader program was necessary.
For the use with a shader programs, the built in uniforms gl_ModelViewMatrix
, gl_NormalMatrix
, gl_ModelViewMatrixInverseTranspose
etc, give access to the Legacy OpenGL matrices. This uniforms are provided up to OpenGL Shading Language 1.20 and were then removed.
In compatibility profile OpenGL Context such applications can still be run with the most recent OpenGL version, but the vertex shader can not be upgraded to a higher version.
In "modern" OpenGL you have to manage the matrix transformations by your own. Matrices are provided to the shader program by Uniform variables (e.g. of type mat4
).
It seams that your shader mix both technologies. I think so, because of the specification of
uniform mat4 modelview;
Furthermore, the fragment shader output gl_FragColor
is deprecated. For the fragment shader output an output variable has to be declared. Your shader has such an output variable, too:
out vec4 fragColor;
To make your shader compile, you have to
gl_ModelViewMatrix
by modelview
gl_ModelViewMatrixInverseTranspose
by inverse(transpose(modelview))
gl_FragColor
by fragColor
gl_NormalMatrix
is a 3x3 matrix (mat3
) and is the upper left 3x3 of gl_ModelViewMatrixInverseTranspose
.
gl_NormalMatrix
can be substituted by mat3(inverse(transpose(modelview)))