BinaryKiller BinaryKiller - 3 months ago 17
C++ Question

opengl getUniformLocation

Im having trouble with getting a proper location of my uniform in my opengl shaders. I always but always get -1 returned from the glGetUniformLocation Function.

maybe I didnt load it well?

I read online that uniforms can get optimized out for not-using them, but I do use my uniforms in a way that can affect my shaders(or do I,I think I do).
also, everytime I encounter an answer that is related to this problem on stackoverflow or on some other site,they do talk about this fact, but NO ONE EXPLAINS about an alternative solution for this, and that realy got me struggling. is it something new just for new opengl iterations?

here is my vertex shader code:

#version 410 core

in vec4 position;
uniform mat4 mvmatrix;
uniform mat4 projmatrix;

void main(void)
{
gl_Position = projmatrix * mvmatrix * position;
}


here is my fragment shader code:

#version 410 core

out vec4 color;



void main(void)
{
color = vec4(1.0,1.0,1.0,1.0)
}


and here is the whole source code for the whole program..for now I didnt implement the whole code itself,only the part that compliles the program and shaders, but yet, Im struggling to get the location for the uniforms, Ive been struggling with this for several days...hoping someone could save me here:

codeHere

also if you need only the specific part that Im talking about:

//creating program and shaders, compiling shaders, compiling program
GLchar * v_Shader[] = {


"#version 410 core \n"
" \n"
"in vec4 position; \n"
" \n"
" \n"
"uniform mat4 mvmatrix; \n"
"uniform mat4 projmatrix; \n"
" \n"
"void main(void) \n"
"{ \n"
" gl_Position = proj_matrix * mv_matrix * position; \n"
"} \n"
};

GLchar * f_Shader[] = {
"#version 410 core \n"
" \n"
"out vec4 color; \n"
" \n"
"in VS_OUT \n"
"{ \n"
" vec4 color; \n"
"} fs_in; \n"
" \n"
"void main(void) \n"
"{ \n"
" color = vec4(1.0,1.0,1.0,1.0) \n"
"} \n"
};

//creating a opengl program
program = glCreateProgram();

//creating the vertex shader and compiling it
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, v_Shader, NULL);
glCompileShader(vertexShader);

//creating the fragment shader and compiling it
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, f_Shader, NULL);
glCompileShader(fragmentShader);

//attaching the shaders to the program and linking the program
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
glLinkProgram(program);
glUseProgram(program);
//getting the program's uniform locations
modelViewMatrixLocation = glGetUniformLocation(program, "mvmatrix");
projectionMatrixLocation = glGetUniformLocation(program, "projmatrix");


And nope, I dont like using glfw/sdl/glew/freeglut or any other library. they just confuse me all around.

Answer

You are misspelling the uniform names in the vertex shader:

uniform mat4 mvmatrix;
uniform mat4 projmatrix;
...
gl_Position = proj_matrix * mv_matrix * position;

To fix, replace gl_Position line with

gl_Position = projmatrix * mvmatrix * position;

To catch errors like this in the future, check the shader compile status:

GLint wasCompiled;
glGetShaderiv( shader, GL_COMPILE_STATUS, &wasCompiled );
GLint logLength;
glGetShaderiv( shader, GL_INFO_LOG_LENGTH, &logLength );
if (logLength > 0)
{
    GLchar* log = new GLchar[ (std::size_t)logLength + 1 ];
    GLsizei charsWritten = 0;            
    glGetShaderInfoLog( shader, logLength, &charsWritten, log );
    std::cout << std::string( log );
}

You also have a syntax error in your fragment shader:

color = vec4(1.0,1.0,1.0,1.0)     

It's missing ; at the end. You should check for shader compile errors.

Your fragment shader takes color as input from vertex shader, but vertex shader doesn't output it and you don't use color input in the fragment shader. To fix, remove these lines:

"in VS_OUT                                                          \n"
"{                                                                  \n"
"    vec4 color;                                                    \n"
"} fs_in;                                                           \n"

In the latest version in the comments you have a line:

color = vec4(1.0,0.0,0.0.1.0)

There are 2 errors. You are missing ; and the character before 1.0 should be ,, not .