Jump to content

  • Log In with Google      Sign In   
  • Create Account

Loading shaders from file error on glGetUniformLocation


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 sothro   Members   -  Reputation: 145

Like
0Likes
Like

Posted 05 January 2014 - 04:32 AM

Hi,

 

I'm just starting with opengl and i was following a tutorial on http://openglbook.com.(currently on chapter 4)

 

My problem is that when i try to do one of these lines :

 

ModelMatrixUniformLocation = glGetUniformLocation(ShaderIds[0], "ModelMatrix");
ViewMatrixUniformLocation = glGetUniformLocation(ShaderIds[0], "ViewMatrix");
ProjectionMatrixUniformLocation = glGetUniformLocation(ShaderIds[0], "ProjectionMatrix");

 

my program crashes and this error is triggered:

ExitOnGLError("ERROR: Could not get the shader uniform locations");

 

if anyone knows what it could be i would be very grateful.
 



Sponsor:

#2 richardurich   Members   -  Reputation: 1187

Like
1Likes
Like

Posted 05 January 2014 - 08:31 AM

Did you change the names in the shaders? If you had "modelMatrix" in the shader code, you'd fail to find "ModelMatrix" since it is case-sensititve. Are you compiling and linking the right shaders? If you're reusing your prior tutorial's code, sometimes people forget to update the file names since they usually don't change for the first few tutorials.

 

It's really hard to blindly help, so any extra information on what you actually typed would help.



#3 exunit   Members   -  Reputation: 123

Like
2Likes
Like

Posted 05 January 2014 - 11:57 AM

Two points: 1 you can only get the location of a uniform that is active (it needs to be used within the body of the shader)

 

and 2: you can get a list of all uniforms available in a shader after it's linked running this code:

int numUni = -1;
glGetProgramiv( mProgram, GL_ACTIVE_UNIFORMS, &numUni );
for(int i = 0; i < numUni; ++i){

    int namelen =-1, num=-1;
    GLenum type = GL_ZERO;
    char name[256]; //assume no variable names longer than 256

    /* Get the name of the ith Uniform */
    glGetActiveUniform(mProgram,
                       static_cast<GLuint>(i),
                       sizeof(name)-1,
                       &namelen,
                       &num,
                       &type,
                       name));
    name[namelen] = 0;

    /* Get the location of the named uniform */
    GLuint location = glGetUniformLocation( mProgram, name );

    std::cout << name << " " << location << std::endl;
}

hope that helps.



#4 sothro   Members   -  Reputation: 145

Like
0Likes
Like

Posted 06 January 2014 - 12:51 PM

thanks for the help allready

if used the coded exunit and their are no active uniforms.

 

I am using opengl version 3.00 can this be a problem to?

 

I have added my shader files.

 

 i have changed something in my shader loading function can this be the problem ?

the change is this:

 //added by me because i got a error he coulden't parse it to const glchar *
                glsl_source_gl=glsl_source;

 

this is my leader function

GLuint LoadShader(const char* filename, GLenum shader_type)
{
    GLuint shader_id = 0;
    FILE* file;
    long file_size = -1;
      GLchar* glsl_source;
     //added by me
     const GLchar* glsl_source_gl;
    
    if (NULL != (file = fopen(filename, "rb")) &&
        0 == fseek(file, 0, SEEK_END) &&
        -1 != (file_size = ftell(file)))
    {
        rewind(file);
        
        if (NULL != (glsl_source = (char*)malloc(file_size + 1)))
        {
            if (file_size == (long)fread(glsl_source, sizeof(char), file_size, file))
            {
                //added by me because i got a error he coulden't parse it to const glchar *
                glsl_source_gl=glsl_source;

                glsl_source[file_size] = '\0';

                if (0 != (shader_id = glCreateShader(shader_type)))
                {
                    glShaderSource(shader_id, 1, (&glsl_source_gl), NULL);
                    glCompileShader(shader_id);
                    ExitOnGLError("Could not compile a shader");

                }
                else
                    fprintf(stderr, "ERROR: Could not create a shader.\n");
            }
            else
                fprintf(stderr, "ERROR: Could not read file %s\n", filename);
            
            free(glsl_source);
        }
        else
            fprintf(stderr, "ERROR: Could not allocate %i bytes.\n", file_size);

        fclose(file);
    }
    else
        fprintf(stderr, "ERROR: Could not open file %s\n", filename);

    return shader_id;
}

 

 

And this is my drawcube method:

 

void CreateCube(void)
{
    const Vertex VERTICES[8] =
{
    { { -.5f, -.5f,  .5f, 1 }, { 0, 0, 1, 1 } },
    { { -.5f,  .5f,  .5f, 1 }, { 1, 0, 0, 1 } },
    { {  .5f,  .5f,  .5f, 1 }, { 0, 1, 0, 1 } },
    { {  .5f, -.5f,  .5f, 1 }, { 1, 1, 0, 1 } },
    { { -.5f, -.5f, -.5f, 1 }, { 1, 1, 1, 1 } },
    { { -.5f,  .5f, -.5f, 1 }, { 1, 0, 0, 1 } },
    { {  .5f,  .5f, -.5f, 1 }, { 1, 0, 1, 1 } },
    { {  .5f, -.5f, -.5f, 1 }, { 0, 0, 1, 1 } }
};
    const GLuint INDICES[36] =
{
    0,2,1,  0,3,2,
    4,3,0,  4,7,3,
    4,1,5,  4,0,1,
    3,6,2,  3,7,6,
    1,6,5,  1,2,6,
    7,5,6,  7,4,5
};

    ShaderIds[0] = glCreateProgram();
    ExitOnGLError("ERROR: Could not create the shader program");

    ShaderIds[1] = LoadShader("c:\\SimpleShader.fragment.glsl", GL_FRAGMENT_SHADER);
    ShaderIds[2] = LoadShader("c:\\SimpleShader.vertex.glsl", GL_VERTEX_SHADER);
    glAttachShader(ShaderIds[0], ShaderIds[1]);
    glAttachShader(ShaderIds[0], ShaderIds[2]);
 
    glLinkProgram(ShaderIds[0]);
    ExitOnGLError("ERROR: Could not link the shader program");

    ModelMatrixUniformLocation = glGetUniformLocation(ShaderIds[0], "ModelMatrix");
    ExitOnGLError("ERROR: Could not get the shader uniform locations for modelmatrix");
    ViewMatrixUniformLocation = glGetUniformLocation(ShaderIds[0], "ViewMatrix");
    ExitOnGLError("ERROR: Could not get the shader uniform locations for viewmatrix");
    ProjectionMatrixUniformLocation = glGetUniformLocation(ShaderIds[0], "ProjectionMatrix");
    ExitOnGLError("ERROR: Could not get the shader uniform locations for projectionMatrix");

    glGenVertexArrays(1, &BufferIds[0]);
    ExitOnGLError("ERROR: Could not generate the VAO");
    glBindVertexArray(BufferIds[0]);
    ExitOnGLError("ERROR: Could not bind the VAO");

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    ExitOnGLError("ERROR: Could not enable vertex attributes");

    glBindBuffer(GL_ARRAY_BUFFER, BufferIds[1]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(VERTICES), VERTICES, GL_STATIC_DRAW);
    ExitOnGLError("ERROR: Could not bind the VBO to the VAO");
 
    glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(VERTICES[0]), (GLvoid*)0);
    glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(VERTICES[0]), (GLvoid*)sizeof(VERTICES[0].Position));
    ExitOnGLError("ERROR: Could not set VAO attributes");

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, BufferIds[2]);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(INDICES), INDICES, GL_STATIC_DRAW);
    ExitOnGLError("ERROR: Could not bind the IBO to the VAO");
 
    glBindVertexArray(0);
}

Attached Files



#5 Xycaleth   Members   -  Reputation: 1135

Like
1Likes
Like

Posted 06 January 2014 - 01:11 PM

There is no version 300 for GLSL. OpenGL 3 uses version 130. You probably want to check if your shaders are compiling correctly as well using glGetShaderiv and glGetShaderInfoLog, and that your program is linking correctly using glGetProgramiv and glGetProgramInfoLog :)



#6 sothro   Members   -  Reputation: 145

Like
0Likes
Like

Posted 06 January 2014 - 01:34 PM

If done what xycaleth said and the shaders aren't compiling it gives 0 back as status.

 

    GLint status;
    glGetShaderiv(shader_id, GL_COMPILE_STATUS, &status);
    if(status==GL_FALSE)
    {
        ExitOnGLError("ERROR: Could not compile shader");
    }

 

thanks for all the help smile.png

 

this is the error that i get when compiling the shaders:

GL_FRAGMENT_SHADER:

ERROR: 0:1: '' :  Version number not supported by OGL driver
ERROR: 0:4: 'in' :  supported in GLSL 1.30 or later
ERROR: 0:6: 'out' :  supported in GLSL 1.30 or later

 

GL_VERTEX_SHADER:

ERROR: 0:1: '' :  Version number not supported by OGL driver
ERROR: 0:4: 'layout' : syntax error syntax error

 

I still don't have any idee why its not compiling i think it has something to do with my loader.


Edited by sothro, 06 January 2014 - 03:10 PM.


#7 sothro   Members   -  Reputation: 145

Like
0Likes
Like

Posted 06 January 2014 - 03:18 PM

If solved the errors from my previous post by changing the version to 330.

 

But now he crashes at :

glBindBuffer(GL_ARRAY_BUFFER, BufferIds[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(VERTICES), VERTICES, GL_STATIC_DRAW);
ExitOnGLError("ERROR: Could not bind the VBO to the VAO");

 

i found the problem,  was missing this line : glGenBuffers(2, &BufferIds[1]);

 

thanks for the great help everybody :D

Hope i can ask for your help if i get other errors


Edited by sothro, 06 January 2014 - 03:32 PM.


#8 stdbool   Members   -  Reputation: 122

Like
0Likes
Like

Posted 10 January 2014 - 10:47 PM

You need to call glUseProgram after glLinkProgram (but before actually using it).






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS