Search code examples
functionopenglglslshadervertex

GLSL shaders and functions: error C1108


I'm trying to write a basic vertex shader in GLSL and just for the sake of clarity, I'd like to add some functions to create matrices and perform other simple operations outside of the main() loop.

However, when I try to execute:

uniform float scale;

void main()
{
vec4 pos = gl_ProjectionMatrix * gl_Vertex;
pos *= scaleMatrix(scale);

gl_Position = pos;

gl_TexCoord[0] = gl_MultiTexCoord0;

gl_FrontColor = gl_Color;           
} 

mat4 scaleMatrix(const in float s) {
return mat4(s, 0.0, 0.0, 0.0,
            0.0, s, 0.0, 0.0,
            0.0, 0.0, s, 0.0,
            0.0, 0.0, 0.0, 1.0 );
}

I get the error: error C1008: undefined variable "scaleMatrix". However

uniform float scale;

void main()
{
vec4 pos = gl_ProjectionMatrix * gl_Vertex;
pos *= mat4(s, 0.0, 0.0, 0.0,
            0.0, s, 0.0, 0.0,
            0.0, 0.0, s, 0.0,
            0.0, 0.0, 0.0, 1.0 );

gl_Position = pos;

gl_TexCoord[0] = gl_MultiTexCoord0;

gl_FrontColor = gl_Color;           
} 

works just fine. Can anyone shed some light on this for me?


Solution

  • Try to put your function declaration at the top of your code file.