dhein
dhein

Reputation: 6555

glGetAttribLocation() returns -1 but the variable is uniform, How to fix it?

This is my shader 'triangles.vert':

#version 430 core
layout(location = 0) in vec4 vPosition;
layout(location = 1) in vec4 vColor;
uniform mat4 vRota;

out vec4 color;

void main()
{
    color = vColor;
    gl_Position = vRota * vPosition;
}

and this is the snippet of the C++ source:

ShaderInfo shaders[] = 
{
    { GL_VERTEX_SHADER, "triangles.vert" },
    { GL_FRAGMENT_SHADER, "triangles.frag" },
    { GL_NONE, NULL }
};

GLuint program = LoadShaders(shaders);
glUseProgram(program);

//...

int vRota_loc = glGetAttribLocation(program, "vRota");

if (vRota_loc == -1)
{
    cout << "No uniform match for 'vRota'" << endl;
}

It returns -1, but I don't know why.

Am I doing something wrong in the shader?

ps LoadShaders() is function for compiling and linking the shader programm. it is given as sourcefile by a book I'm practicing with. So I supose there won't be the error.

Upvotes: 1

Views: 301

Answers (2)

Freddy
Freddy

Reputation: 2279

A uniform is not an attribute. You need to use glGetUniformLocation

A uniform is consistent for each vertex while an attribute is per vertex.

Upvotes: 5

Colonel Thirty Two
Colonel Thirty Two

Reputation: 26609

glGetAttribLocation is for vertex attributes, i.e. in variables in the vertex shader (such as vPosition). You want glGetUniformLocation.

Upvotes: 8

Related Questions