Home > database >  glGetUniformLocation returning -1 on OpenGL 4.6
glGetUniformLocation returning -1 on OpenGL 4.6

Time:08-22

I'm writing a small "engine", and the time has finally come to implement transformations. However, when I try to glGetUniformLocation, it return -1. Here is my rendering method:

void GFXRenderer::submit(EntityBase* _entity, GPUProgram _program) {
    if(_entity->mesh.has_value()) {

        mat4 mod_mat(1.0);
        
        //mod_mat = translate(mod_mat, _entity->transform.position);
        /*
        mod_mat = scale(mod_mat, _entity->transform.scale);
        
        mod_mat = rotate(mod_mat, radians(_entity->transform.rotation.x), vec3(1.0, 0.0, 0.0));
        mod_mat = rotate(mod_mat, radians(_entity->transform.rotation.y), vec3(0.0, 1.0, 0.0));
        mod_mat = rotate(mod_mat, radians(_entity->transform.rotation.z), vec3(0.0, 0.0, 1.0));
        */
        mod_mat = translate(mod_mat, vec3(0.5f, -0.5f, 0.0f));
        //mod_mat = glm::rotate(mod_mat, (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f));
        glUseProgram(_program.id);
        int transform = glGetUniformLocation(_program.vsh.id, "transform");
        std::cout << transform << std::endl;
        glUniformMatrix4fv(transform, 1, GL_FALSE, value_ptr(mod_mat));
        
        glUseProgram(_program.id);
        glBindTexture(GL_TEXTURE_2D, _entity->mesh->tex_id);
        glBindVertexArray(_entity->mesh->vao);
        glDrawElements(GL_TRIANGLES, _entity->mesh->indices.size(), GL_UNSIGNED_INT, 0);
        glUseProgram(0);
        glBindVertexArray(0);
    }
}

Here EntityBase is an object class. It contains a transform class, as follows:

class Transform {
public:
    vec3 position;
    vec3 rotation;
    vec3 scale;
    quat q_rot;
    mat4x4 matrix;
};

Ignore the quaternion and matrix. ALso, I must mention that without doing transformation - it renders flawlessly. (SIKE)

Here is my vsh :

#version 460 core

layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTex;

out vec2 tex_coord;
out mat4 f_tr_opt; // for stopping optimization 

uniform mat4 transform;

void main() {
    tex_coord = aTex;
    f_tr_opt = transform;

    gl_Position = transform * vec4(aPos, 1.0);
}

Here is my fsh:

#version 460 core

in vec2 tex_coord;
in mat4 f_tr_opt; // again, same thing
out vec4 FragColor;

uniform sampler2D texture0;

void main() {
    mat4 garbage = f_tr_opt * f_tr_opt; // named garbage for easier recognition
    FragColor = texture(texture0, tex_coord);
}

I check for compile and linking errors, all is fine. Please correct me as to what I am doing wrong here.

CodePudding user response:

See glGetUniformLocation. The uniform location must be requested from the linked program object, not from the (vertex) shader object:

int transform = glGetUniformLocation(_program.vsh.id, "transform");

int transform = glGetUniformLocation(_program.id, "transform");
  • Related