为什么我的模特采用了我的天空盒纹理?

时间:2016-01-06 03:23:46

标签: c++ opengl

我已经看了好几天这个问题,但我无法弄清楚为什么我的模型会占用我的天空盒纹理。它一直在困扰着我。这是它的样子。This is what my model looks like after the skybox is loaded in. What my model looks like before my skybox is loaded into the scene.

core.cpp主循环

while (!m_Window.closed())
{
    m_Window.varUpdate();

    m_Window.Do_Movement();

    glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
    m_Window.clear();


    m_ModelShader.enable();
    glm::mat4 model;
    model = glm::scale(model, glm::vec3(0.2f));
    //model = glm::translate(model, glm::vec3(5.0f));
    glm::mat4 view = m_Window.m_Camera.GetViewMatrix();
    glm::mat4 projection = glm::perspective(m_Window.m_Camera.Zoom, (float)m_Window.getWindowX() / (float)m_Window.getWindowY(), 0.1f, 100.0f);
    glUniformMatrix4fv(glGetUniformLocation(m_ModelShader.m_ProgramID, "model"), 1, GL_FALSE, glm::value_ptr(model));
    glUniformMatrix4fv(glGetUniformLocation(m_ModelShader.m_ProgramID, "view"), 1, GL_FALSE, glm::value_ptr(view));
    glUniformMatrix4fv(glGetUniformLocation(m_ModelShader.m_ProgramID, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
    glUniform3f(glGetUniformLocation(m_ModelShader.m_ProgramID, "cameraPos"), m_Window.m_Camera.Position.x, m_Window.m_Camera.Position.y, m_Window.m_Camera.Position.z);
    m_Model.Draw(m_ModelShader);

    glDepthFunc(GL_LEQUAL);
    m_SkyboxShader.enable();
    glm::mat4 projectionS = glm::perspective(m_Window.m_Camera.Zoom, (float)m_Window.getWindowX() / (float)m_Window.getWindowY(), 0.1f, 100.0f);
    glm::mat4 viewS = m_Window.m_Camera.GetViewMatrix(); //This is usually set to glm::mat4(glm::mat3(m_Window.m_Camera.GetViewMatrix())); to center on the camera.
    glUniformMatrix4fv(glGetUniformLocation(m_SkyboxShader.m_ProgramID, "projection"), 1, GL_FALSE, glm::value_ptr(projectionS));
    glUniformMatrix4fv(glGetUniformLocation(m_SkyboxShader.m_ProgramID, "view"), 1, GL_FALSE, glm::value_ptr(viewS));
    glBindVertexArray(sVAO);
    glActiveTexture(GL_TEXTURE0);
    glUniform1i(glGetUniformLocation(m_SkyboxShader.m_ProgramID, "skybox"), 0);
    glBindTexture(GL_TEXTURE_CUBE_MAP, skyboxTex);
    glDrawArrays(GL_TRIANGLES, 0, 36);
    glBindVertexArray(0);
    glDepthFunc(GL_LESS);



    m_Window.update();
}

1 个答案:

答案 0 :(得分:1)

OpenGL是一个状态机,即它会记住你所做的一切,并继续使用你设置的最后一个配置。当您绘制模型(m_Model.Draw)时,OpenGL仍然具有从前一个绘图迭代中绑定和激活的天空盒纹理...并因此应用它。

是好的做法
  • 在渲染帧结束时清理OpenGL状态
  • 在渲染帧开始时清理OpenGL状态
  • 在批次
  • 之后立即清理特定图纸批次的OpenGL状态集

  • 在绘制特定批次之前,将所有OpenGL状态设置/取消设置为下一个绘图批次所需的状态。

在你的情况下,我建议你在绘制天空盒之后解开纹理。