But if I use this code, I don´t get any error and the program works!
main
glUseProgram(shader->ID);
const GLenum ErrorValue = glGetError();
cout<<ErrorValue<<endl; // output '0'
ExitOnGLError("ERROR: Could use the shader"); // Any error here and the program continue..
The reason the latter works is that glGetError() is called twice, and as the first one is called without resulting in an error, glGetError() returns 0 inside the ExitOnGLError() function.
IIRC, 1282 Means invalid value. - Are you sure that shader->ID was created from a glLinkProgram() successfully prior to that?
The reason the latter works is that glGetError() is called twice, and as the first one is called without resulting in an error, glGetError() returns 0 inside the ExitOnGLError() function.
IIRC, 1282 Means invalid value. - Are you sure that shader->ID was created from a glLinkProgram() successfully prior to that?
Show more code, preferably a minimum complete program. There is likely more to this problem that what you have shown. For example, if you're only checking the error state after glUseProgram, then the error could be at any point before that because error bits are never reset by functions other that glGetError so the error can come from earlier functions within your program.
ModelMatrixUniformLocation = shader->GetUniformLocation("ModelMatrix");
ViewMatrixUniformLocation = shader->GetUniformLocation("ViewMatrix");
ProjectionMatrixUniformLocation = shader->GetUniformLocation("ProjectionMatrix");
ExitOnGLError("ERROR: Could not get the uniform locations");
Texture* texture0 = content.Load<Texture>("Textures/cube.bmp");
Tex0Loc = shader->GetUniformLocation("texture0");
ExitOnGLError("Error: Could not load texture");
}
void Engine::DrawMesh(void)
{
glUseProgram(shader->ID);
ExitOnGLError("ERROR: Could not use the shader"); // Error 1282
glUniformMatrix4fv(ModelMatrixUniformLocation, 1, GL_FALSE, value_ptr(ModelMatrix));
ExitOnGLError("ERROR: Could not set the model matrix");
ViewMatrix = lookAt(
vec3(0.0, 2.0, 4.0), //eye
vec3(0.0 ,0.0 , 0.0), //direction
vec3(0.0, 1.0, 0.0) //up
);
glUniformMatrix4fv(ViewMatrixUniformLocation, 1, GL_FALSE, value_ptr(ViewMatrix));
ExitOnGLError("ERROR: Could not set the view matrix");
ProjectionMatrix = perspective(45.0f, (float) CurrentWidth / CurrentHeight , 0.1f, 100.0f);
glUniformMatrix4fv(ProjectionMatrixUniformLocation, 1, GL_FALSE, value_ptr(ProjectionMatrix));
ExitOnGLError("ERROR: Could not set the projection matrix");
glUniform1i(Tex0Loc, 0);
mesh->Draw();
glUseProgram(0);
}
Shader.cpp
void Shader::Load(const char* filename)
{
ifstream in(filename, ios::binary);
if(!in)
{
cerr << "Could not open the file" << filename << endl; // Lanzamos un mensaje de error
exit(1); // exit(1) usado para abortar el programa
}
//Leemos los datos
std::string glsl_source((std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>());
// Terminamos de leer el documento
in.close();
//miramos si hay algo
if(glsl_source.length() == 0)
{
cerr<<"ERROR: Any data in the file"<<endl;
exit(1);
}
std::string fs_shader = "#version 330\n #define COMPILING_FS\n" + glsl_source;
std::string vs_shader = "#version 330\n #define COMPILING_VS\n" + glsl_source;
// Creamos el programa
ID = glCreateProgram();
fragmentShaderID = CreateShader(fs_shader,GL_FRAGMENT_SHADER);
vertexShaderID = CreateShader(vs_shader,GL_VERTEX_SHADER);
Initialize();
}
void Shader::Initialize(void)
{
glAttachShader(ID, fragmentShaderID);
ExitOnGLError("No se puede vincular el Fragment Shader");
glAttachShader(ID, vertexShaderID);
ExitOnGLError("No se puede vincular el Vertex Shader");
glLinkProgram(ID);
ExitOnGLError("No se puede conectar al programa");
CheckLink(ID);
}
In Engine::CreateMesh you're creating the shader and returning it by value. This means the shader object has to be copied from the function content.Load<Shader>() to the object shader. If you have not written a proper constructors, an assignment operator and a destructor to treat the object as a value, you cannot use the shader object as if it was a value either but that is what you're currently doing.
Look up the Rule of Three; here's a clicky for an example.