Search code examples
openglshaderglfwglewvao

Using two different shaders for two different VAOS


I have two seperate VAO objects that render in a while loop as such. Each one needs to be drawn with a different shader program. I can get them both rendering individually, but together only the skybox renders.

do{
    // Clear the screen
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Access the active camera
    glm::mat4 ViewMatrix;
    if(worldCamMode)
    {
        worldCam->update();
        ViewMatrix = worldCamParent->getViewMtx();
    }
    else
    {
        playerCam->update();
        ViewMatrix = playerCamParent->getViewMtx();
    }

    // Retrieve the projection and model matrices
    glm::mat4 ProjectionMatrix = getProjectionMatrix();
    glm::mat4 ModelMatrix = glm::mat4(1.0f);
    glm::mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;
    glm::mat4 MVPX = ProjectionMatrix * ViewMatrix;

    // Render the skybox first
    glm::mat4 view = glm::mat4(glm::mat3(ViewMatrix));
    cloudySkybox->render(cubemapTexture, view, ProjectionMatrix, skyboxProgramID);

    int viewHandle = glGetUniformLocation(lampID, "view");
    if (viewHandle == -1) {
        std::cout << "Uniform: view is not an active uniform label\n";
    }
    glUniformMatrix4fv( viewHandle, 1, false, &MVPX[0][0]);
    glUseProgram(lampID);
    testObj.render(lampID);

    // Swap buffers
    glfwSwapBuffers(window);
    glfwPollEvents();

} // Check if the ESC key was pressed or the window was closed
while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
       glfwWindowShouldClose(window) == 0 );

The first object to render is cloudySkybox. This is the only thing that renders and testObj does not r ender unless I comment out the cloudySkybox line. I have narrowed it down to see what breaks the testObj. testObj renders fine until I use the line glUseProgram(shaderID); and then it stops rendering. Does anyone have any clue why this might be? The testObj.render() function has the same format with glUseProgram(differentShader)

void skybox::render(unsigned int cubemapTexture, glm::mat4 view, glm::mat4 projection, int shaderID)
{
    glUseProgram(shaderID);
    //glDepthMask(GL_FALSE);

    //int viewUniformHandle = glGetUniformLocation(shaderID, "view");
    //if (viewUniformHandle == -1)
    //  exit(1);
    //glUniformMatrix4fv( viewUniformHandle, 1, false, glm::value_ptr(view) );

    //int projectionUniformHandle = glGetUniformLocation(shaderID, "projection");
    //if (projectionUniformHandle == -1)
    //  exit(1);
    //glUniformMatrix4fv( projectionUniformHandle, 1, false, glm::value_ptr(projection) );

    //glBindVertexArray(skyboxVAO);
    //glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
    //glDrawArrays(GL_TRIANGLES, 0, 36);
    //glDepthMask(GL_TRUE);

    //glBindVertexArray(0);
    //glFlush();
}



void Model::render(int programID)
{
    glUseProgram(programID);

    int modelUniformHandle = glGetUniformLocation(programID, "model");
    if (modelUniformHandle == -1)
        exit(1);

    glm::mat4 model;

    for (int i = 0; i < VAOs.size(); i++) {

        glUseProgram(programID);
        glBindVertexArray(VAOs[i]);
        model = glm::mat4(1.0f);

        glBindTexture(GL_TEXTURE_2D, TexID[this->shape[i].mesh.material_ids[i]]);
        glUniformMatrix4fv(modelUniformHandle, 1, false, glm::value_ptr(model));
        glDrawElements(GL_TRIANGLES, this->shape[i].mesh.indices.size(), GL_UNSIGNED_INT, 0);
        glBindVertexArray(0);
        glFlush();
    }
}

Solution

  • glUniform operates on the program object that was made part of current state by calling glUseProgram. Set program glUseProgram(lampID) before calling glUniformMatrix4fv