繁体   English   中英

使用OpenGL显示两个对象。 纹理表现异常

[英]Display two objects using OpenGL. Textures not behaving as expected

嗨,我正在尝试使用OpenGL来显示两个对象。1)一个旋转的多维数据集 ,在前景中混合了两种纹理(一个木箱图案和一个笑脸 ),以及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不关心对象,只关心顶点,但是在这种情况下,它们是分离的,不相交的对象。 因此,我应该将它们组织为绑定到单个VAO的两个VBO还是每个对象绑定到两个VAO的单独的VBO? 还是这样,无论哪种方式都很好-取决于编码器的选择和代码的优美性?

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM