0

I have two shaders created using the code provided by theCherno in his OpenGL tutorial, and I'm trying to render two different "models".

The project is pretty big, so I'll do my best to include relevant pieces of code. this is the Model::Draw() function:

void Model::Draw(Shader shader, Camera camera)
{
    //Setting uniforms
    shader.SetUniformMat4("view", camera.GetViewMatrix());
    shader.SetUniformMat4("projection", camera.GetProjectionMatrix());
    shader.SetUniformMat4("model", m_Transform);
    m_Mesh.Draw(shader);
}

m_Mesh.Draw(shader) calls Mesh::Draw(Shader shader):

void Mesh::Draw(Shader shader) {

    shader.Bind();

    GlCall(glBindVertexArray(VAO));
    GlCall(glDrawElements(GL_TRIANGLES, (GLsizei) Indices.size(), GL_UNSIGNED_INT, 0));
    //GlCall(glBindVertexArray(0));

    //glActiveTexture(GL_TEXTURE0);
}

This is the main rendering loop:

while (!glfwWindowShouldClose(window))
{

    simple.SetUniform3f("objColor", 1.0f, 1.0f, 1.0f);
    lightModel.Draw(simple, context.ActiveCamera);

    //Render
    shaded.SetUniform3f("objectColor", glm::vec3(1.0f));
    shaded.SetUniform3f("lightColor", glm::vec3(1.0f));
    shaded.SetUniform3f("lightPos", glm::vec3(1.0f));
    model.Draw(shaded, context.ActiveCamera);



    //OPENGL
    glfwSwapBuffers(window);
    glfwPollEvents();
}

By using the glGetError() function I got that the error is 1281 on Shader.Bind(), which is just a single line of code:

glUseProgram(m_RenderId);

It also prints that the three uniforms model, view and perspective in the shader do not exist (this is a check that my shader class does and it issues it when glGetUniformLocation(m_RenderId, uniformName) returns -1), even if they are present.

Also by debugging, I found out that both shaders have two different m_RenderId, so I think there's no problem there. The error occurs the second time the while loop runs. So I might be forgetting to do something at the end of the draw call but by looking online I couldn't find anything.

And what is strange is that it gives me an error only if I try to render two different meshes with two different shaders, If I try to render both the model mesh and the lightModel with the same shader it works fine.

I am pretty new to OpenGL and I've done my best to actually understand what the code is doing but this error just doesn't make sense to me, so I excuse in advance if this is a silly mistake. I might be also missing some code pieces in the question if, more is necessary I'll add it.

How can I fix it?

Fabrizio
  • 1,138
  • 4
  • 18
  • 41
  • @Rabbid76 yes, sorry, editing it right now. – Fabrizio Apr 23 '20 at 20:33
  • You have to install the proper program (`glUseProgram`), before you can set the value of a uniform by [`glUniform*`](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glUniform.xhtml). Each program has its own default uniform block and its own uniforms. Even if the uniforms have the same name, you have to set the values for each program separately. – Rabbid76 Apr 23 '20 at 20:35
  • What does the destructor or the class `Shader` do? Does if call [`glDeleteProgram`](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glDeleteProgram.xhtml)? if yes, then put a break point in the destructor of the class `Shader` and read [OpenGL object in C++ RAII class no longer works](https://stackoverflow.com/questions/46839586/opengl-object-in-c-raii-class-no-longer-works) – Rabbid76 Apr 23 '20 at 20:40
  • @Rabbid76 I tried calling simple.Bind(); which calls glUseProgram for that shader before setting uniforms for it and I tried doing the same with "shaded", but It still crashes on the second cycle, calling glUseProgram on "simple" shader. – Fabrizio Apr 23 '20 at 20:41
  • 1
    @Rabbid76 yes, and it gets called, so I'm trying to figure out how to solve this using the answer to the question you linked (if it's possible), thanks for the advice! – Fabrizio Apr 23 '20 at 20:46
  • 1
    To solve the issue, pass the objects by reference (`&`) to the methods. (e.g: `Model::Draw(Shader &shader, Camera &camera)`, `Mesh::Draw(Shader &shader)`) – Rabbid76 Apr 23 '20 at 20:52
  • This fixed It, thank you for your help! – Fabrizio Apr 23 '20 at 20:57

0 Answers0