使用OpenGL显示两个对象.纹理不像预期的那样行为

Display two objects using OpenGL. Textures not behaving as expected

本文关键字:纹理 显示 OpenGL 对象 两个 使用      更新时间:2023-10-16

嗨,我试图使用opengl。)在前景中,2)矩形板只有一个纹理(深灰色木材)作为背景。当我评论矩形板的显示的代码部分时,旋转立方体显示两个纹理(木制板条箱笑脸)。否则,多维数据集仅显示木制板条箱质地,深灰色木材质地也显示在矩形板上,即笑脸从旋转立方体。请查找图像1)http://oi68.tinypic.com/2la4r3c.jpg(带有矩形板的代码的部分)和2)http://i67.tinypic.com/9U9RPF.jpg(未注释代码的矩形板)。代码的恢复部分在下面粘贴

// Rotating Cube ===================================================
// Texture of wooden crate
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);
glUniform1i(glGetUniformLocation(ourShader_box.Program, "ourTexture1"), 0);
// Texture of a smiley
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);
glUniform1i(glGetUniformLocation(ourShader_box.Program, "ourTexture2"), 1);
// lets use the box shader for the cube
ourShader_box.Use();
// transformations for the rotating cube ---------------------------------
glm::mat4 model_box, model1, model2;
glm::mat4 view_box;
glm::mat4 perspective;
perspective = glm::perspective(45.0f, (GLfloat)width_screen/(GLfloat)height_screen, 0.1f, 200.0f);
model1 = glm::rotate(model_box, (GLfloat)glfwGetTime()*1.0f, glm::vec3(0.5f, 1.0f, 0.0f));
model2 = glm::rotate(model_box, (GLfloat)glfwGetTime()*1.0f, glm::vec3(0.0f, 1.0f, 0.5f));
model_box = model1 * model2;
view_box= glm::translate(view_box, glm::vec3(1.0f, 0.0f, -3.0f));
GLint modelLoc_box = glGetUniformLocation(ourShader_box.Program, "model");
GLint viewLoc_box = glGetUniformLocation(ourShader_box.Program, "view");
GLint projLoc_box = glGetUniformLocation(ourShader_box.Program, "perspective");
glUniformMatrix4fv(modelLoc_box, 1, GL_FALSE, glm::value_ptr(model_box));
glUniformMatrix4fv(viewLoc_box, 1, GL_FALSE, glm::value_ptr(view_box));
glUniformMatrix4fv(projLoc_box, 1, GL_FALSE, glm::value_ptr(perspective));  
// --------------------------------------------------------------------
// Draw calls
glBindVertexArray(VAO_box);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
// Rectangular Plate =====================================================
// Background Shader
ourShader_bg.Use();
// Texture of dark grey wood
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, texture_wood);
glUniform1i(glGetUniformLocation(ourShader_bg.Program, "ourTexture3"), 2);
// Transformations -------------------------------------------
glm::mat4 model_bg;
glm::mat4 view_bg;
GLint modelLoc_bg = glGetUniformLocation(ourShader_bg.Program, "model");
GLint viewLoc_bg= glGetUniformLocation(ourShader_bg.Program, "view");
GLint projLoc_bg = glGetUniformLocation(ourShader_bg.Program, "perspective");
glUniformMatrix4fv(modelLoc_bg, 1, GL_FALSE, glm::value_ptr(model_bg));
glUniformMatrix4fv(viewLoc_bg, 1, GL_FALSE, glm::value_ptr(view_bg));
glUniformMatrix4fv(projLoc_bg, 1, GL_FALSE, glm::value_ptr(perspective));   
// -----------------------------------------------------------
// Draw calls
glBindVertexArray(VAO_bg);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
// =================================================================

我有两个有关此代码的问题。

  • 为什么笑脸消失了?
  • 这是否应该如何渲染多个对象?我知道OpenGL不在乎对象,它只关心顶点,但是在这种情况下,这些是独立的,不相交的对象。因此,我是否应该将它们作为两个VBO绑定到一个VAO的两个VBO或单独的VBO绑定到每个对象的两个VAO?还是这样,无论哪种方式都很好 - 取决于编码器的选择和代码的优雅?

您使用的是相同的着色器,相同的矩阵,并且对两个对象(三角形)具有相同的几何类型,那么为什么要将着色器设置两次?你尝试过;

  • 设置着色器
  • 绑定缓冲区#1
  • 绑定纹理#1
  • 绘制对象#1
  • 绑定缓冲区#2
  • 绑定纹理#2
  • 绘制对象#2