Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

glGetUniformLocation() returning -1 even though used in vertex shader

Tags:

c++

opengl

glsl

I'm trying to render a simple cube with normals. I'm using the following code to initialize the shaders.

void initShader(const char* vertexShaderPath, const char* fragmentShaderPath){
    cout<<"Initializing Shaders"<<endl<<"==="<<endl;
    cout<<"Vertex shader ";
    vs = loadShader(vertexShaderPath, GL_VERTEX_SHADER);
    cout<<"Fragment shader ";
    fs = loadShader(fragmentShaderPath, GL_FRAGMENT_SHADER);

    cout<<"Creating program: ";
    program = glCreateProgram();
    if(0==program) cout<< "Failed"<<endl; else cout<< "OK"<<endl;

    glAttachShader(program, vs);
    glAttachShader(program, fs);

    cout<<"Linking program: ";
    glLinkProgram(program); 
    GLint linkstatus = 0;
    glGetProgramiv(program, GL_LINK_STATUS, &linkstatus);
    if(GL_TRUE==linkstatus) cout<<"OK"<<endl; else cout<<"Failed" <<endl;

    cout<<endl<<endl<<"Memory status"<<endl<<"==="<<endl;

    mvpmat = glGetUniformLocation(program,"mvpMatrix");
    mvmat = glGetUniformLocation(program,"mvMatrix");
    normalmat = glGetUniformLocation(program,"normalMatrix");

    cout << "mvpMatrix: "<<mvpmat<<" mvMatrix: "<<mvmat<<" normalMatrix: " << normalmat<<endl;
}

The output of the last cout statement appears as follows (There is no error during compilation or linking).

mvpMatrix: 0 mvMatrix: -1 normalMatrix: 1

This is my vertex shader code

uniform mat4 mvpMatrix;
uniform mat4 mvMatrix;
uniform mat3 normalMatrix;
attribute vec3 vertex;
attribute vec3 normal;
varying vec3 outNormal;
varying vec3 outPos;

void main(){
    gl_Position = mvpMatrix*vec4(vertex,1.0);
    outPos = (mvMatrix*vec4(vertex,1.0)).xyz;
    outNormal = normalize(normalMatrix*normal);
}

I've used all mvpMatrix, mvMatrix and normalMatrix in the code. Thus i'm really confused why getUniformLocation() is returning mvMatrix = -1?

like image 709
Ash Catchem Avatar asked Dec 14 '22 23:12

Ash Catchem


1 Answers

If your fragment shader does not use the input of outPos it will be optimized away. Uniforms are defined post-link. So both fragment and vertex shader count.

like image 184
starmole Avatar answered May 19 '23 18:05

starmole