Loading shaders from file error on glGetUniformLocation

Started by
6 comments, last by stdbool 10 years, 3 months ago

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.

Advertisement

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.

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.

Our Current Game: Smith and Winston Ikari Warriors + Space Harrier + Voxel Destruction

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);
}

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 :)

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.

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

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

This topic is closed to new replies.

Advertisement