簡體   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