glGetUniformLocation error 1282

Started by
4 comments, last by Hector San Roman Lanza 11 years, 6 months ago
Hi!
I try to get the uniform location from shader, but it give me an error 1282(GL_INVALID_OPERATION)
This is the code:

Shader.hpp

class Shader : public object
{
public:
Shader();
virtual ~Shader();
GLuint ID;
void Load(const char* filename);
protected:
private:
GLuint vertexShaderID,
fragmentShaderID;
void Initialize(void);
GLuint CreateShader(std::string shader, GLenum type_shader);
}
;

Shader.cpp

void Shader::Load(const char* filename)
{
ifstream in(filename, ios::binary);
if(!in)
{
cerr << "Could open file" << filename << endl; // Lanzamos un mensaje de error
exit(1); // exit(1) usado para abortar el programa
}

std::string glsl_source((std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>());

in.close();

if(glsl_source.length() == 0)
{
exit(1);
}
std::string fs_shader = "#define COMPILING_FS\n" + glsl_source;
std::string vs_shader = "#define COMPILING_VS\n" + glsl_source;
std::cout<<vs_shader<<std::endl;
ID = glCreateProgram();
CreateShader(fragmentShaderID,fs_shader,GL_FRAGMENT_SHADER);
CreateShader(vertexShaderID,vs_shader,GL_VERTEX_SHADER);
Initialize();
}
GLuint Shader::CreateShader(std::string shader, GLenum type_shader)
{
GLuint shader_id = 0;
shader_id = glCreateShader(type_shader);
const char* glsl_cstr;
glShaderSource(shader_id, 1, &(glsl_cstr=shader.c_str()),NULL);
glCompileShader(shader_id);
ExitOnGLError("Could compile shader");

return shader_id;
}
void Shader::Initialize(void)
{
glAttachShader(ID, fragmentShaderID);
ExitOnGLError("Could attach fragmentshader");
glAttachShader(ID, vertexShaderID);
ExitOnGLError("Could attach vertexshader");
glLinkProgram(ID);
ExitOnGLError("Could link program");
}


main


ModelMatrixUniformLocation = glGetUniformLocation(shader->ID, "ModelMatrix"); //Error in here
cout<<glGetError()<<endl; // output '1282'
cout<<shader->ID<<endl; // output '1'


Can anyone see the error?
Advertisement
One condition for glGetUniformLocation to fail with that error code is that your program didn't link correctly. You should add checks to your code to see if there are compile and/or link errors.

Add this after compiling each shader file.

GLint compiled = 0;
GLint length = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);

if(length > 1) {
std::string log(length, '\0');
glGetShaderInfoLog(shader, length, &length, &log[0]);
}

Likewise after attempting to link the shader, but replace GL_COMPILE_STATUS with GL_LINK_STATUS and the shader ID with the program ID.
With gl_compile_status I don't have any error, but with gl_link_status the function glGetShaderiv return 0 and the log is empty.
Sorry, I was using glGetShaderiv instead of glGetProgramiv
I apologize, I quickly glanced over my own code to verify the code I posted but didn't notice the obvious that the functions are different; you need to use glGetProgramiv and glGetProgramInfoLog when querying the program object and its link status. The code otherwise should be the same.

edit: Good, you apparently found and corrected my mistake already.
Even so your code helped me a lot.
thanks!!

This topic is closed to new replies.

Advertisement