2013-10-15 36 views
0

這樣我就可以使用OpenGL3.2 +繪製一個旋轉的立方體,它從0,0,0和向左走翻譯抽2個立方體在OpenGL,但是當我嘗試並繪製第二個(朝右),它並沒有使...使用GLM

這是我的顯示功能:

void display()         
    { 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glUseProgram(myShader.handle()); 

    GLuint matLocation = glGetUniformLocation(myShader.handle(), "ProjectionMatrix"); 
    glUniformMatrix4fv(matLocation, 1, GL_FALSE, &ProjectionMatrix[0][0]); 

    spinY+=0.03; 
    if(spinY>360) spinY = 0; 

    glm::mat4 viewMatrix; 
    viewMatrix = glm::translate(glm::mat4(1.0),glm::vec3(0,0,-100));  //viewing matrix 
    ModelViewMatrix = glm::translate(viewMatrix,glm::vec3(-30,0,0));  //translate object from the origin 
    ModelViewMatrix = glm::rotate(ModelViewMatrix,spinY, glm::vec3(0,1,0));     //rotate object about y axis 

    glUniformMatrix4fv(glGetUniformLocation(myShader.handle(), "ModelViewMatrix"), 1, GL_FALSE, &ModelViewMatrix[0][0]); //pass matrix to shader 

    //Add the following line just before the line to draw the cube to 
    //check that the origin of the cube in eye space is (-30, 0, -100); 
    result = glm::vec3(ModelViewMatrix * glm::vec4(0,0,0,1)); 
    std::cout<<glm::to_string(result)<<std::endl; //print matrix to get coordinates. 

    myCube.render(); 
    glUseProgram(0); 
    } 

我希望能夠使用相同的立方體類/大小等,而只是再次渲染它(我假設這是最有效/最好的方式)。

我想這

void display()         
    { 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glUseProgram(myShader.handle()); 

    GLuint matLocation = glGetUniformLocation(myShader.handle(), "ProjectionMatrix"); 
    glUniformMatrix4fv(matLocation, 1, GL_FALSE, &ProjectionMatrix[0][0]); 

    spinY+=0.03; 
    if(spinY>360) spinY = 0; 

    glm::mat4 viewMatrix; 
    viewMatrix = glm::translate(glm::mat4(1.0),glm::vec3(0,0,-100));  //viewing matrix 
    ModelViewMatrix = glm::translate(viewMatrix,glm::vec3(-30,0,0));  //translate object from the origin 
    ModelViewMatrix = glm::rotate(ModelViewMatrix,spinY, glm::vec3(0,1,0));     //rotate object about y axis 

    glUniformMatrix4fv(glGetUniformLocation(myShader.handle(), "ModelViewMatrix"), 1, GL_FALSE, &ModelViewMatrix[0][0]); //pass matrix to shader 

    //Add the following line just before the line to draw the cube to 
    //check that the origin of the cube in eye space is (-30, 0, -100); 
    result = glm::vec3(ModelViewMatrix * glm::vec4(0,0,0,1)); 
    std::cout<<glm::to_string(result)<<std::endl; //print matrix to get coordinates. 

    myCube.render(); 

    glm::mat4 viewMatrix_TWO; 
    viewMatrix_TWO = glm::translate(glm::mat4(1.0),glm::vec3(0,0,-100));  //viewing matrix 
    ModelViewMatrix_TWO = glm::translate(viewMatrix_TWO,glm::vec3(30,0,0));  //translate object from the origin 
    ModelViewMatrix_TWO = glm::rotate(ModelViewMatrix_TWO,spinY, glm::vec3(0,1,0));     //rotate object about y axis 

    glUniformMatrix4fv(glGetUniformLocation(myShader.handle(), "ModelViewMatrix_TWO"), 1, GL_FALSE, &ModelViewMatrix[0][0]); //pass matrix to shader 

    myCube.render(); 

    glUseProgram(0); 
    } 

很顯然,我已經實現了它錯了...我怎樣才能得到屏幕的立方體兩側?謝謝。

UPDATE

我意識到,我沒有創建第二個立方體對象,但現在實現的,它仍然無法正常工作......難道我困惑如何視圖/模型矩陣互動?我會爲每個對象一個新的....

新代碼:

myCube.render(); 

spinX+=0.03; 
if(spinX>360) spinX = 0; 

glm::mat4 viewMatrix_Two,ModelViewMatrix_Two; 
viewMatrix_Two = glm::translate(glm::mat4(1.0),glm::vec3(0,0,-100));  //viewing matrix 
ModelViewMatrix_Two = glm::translate(viewMatrix_Two,glm::vec3(30,0,0));  //translate object from the origin 
ModelViewMatrix_Two = glm::rotate(ModelViewMatrix_Two,spinX, glm::vec3(0,1,0));     //rotate object about y axis 

glUniformMatrix4fv(glGetUniformLocation(myShader.handle(), "ModelViewMatrix_Two"), 1, GL_FALSE, &ModelViewMatrix_Two[0][0]); //pass matrix to shader 

myCube_Two.render(); 

UPDATE

着色器:

uniform mat4 ModelViewMatrix; 
    //uniform mat4 ModelViewMatrix_Two; //NOT NEEDED - USED SAME SHADER OBJECT 
    uniform mat4 ProjectionMatrix; 

    in vec3 in_Position; // Position coming in 
    in vec3 in_Color;  // colour coming in 
    out vec3 ex_Color;  // colour leaving the vertex, this will be sent to the fragment shader 

    void main(void) 
    { 
    gl_Position = ProjectionMatrix * ModelViewMatrix * vec4(in_Position, 1.0); 
    //gl_Position = ProjectionMatrix * ModelViewMatrix_Two * vec4(in_Position, 1.0); 
    ex_Color = in_Color; 
    } 

回答

1

最後,我創建了第二個立方體對象,第二觀察矩陣,並用它們在我的着色器已經建立的模型矩陣似乎都立方體被稱爲/單獨渲染。

正確的代碼是:

glm::mat4 viewMatrix_Two, ModelViewMatrix_Two; 
    viewMatrix_Two = glm::translate(glm::mat4(1.0),glm::vec3(0,0,-200)); 
    ModelViewMatrix = glm::translate(viewMatrix_Two,glm::vec3(30,0,0)); 
    ModelViewMatrix = glm::rotate(ModelViewMatrix,spinX, glm::vec3(1,0,0)); 

    glUniformMatrix4fv(glGetUniformLocation(myShader.handle(), "ModelViewMatrix"), 1, GL_FALSE, &ModelViewMatrix[0][0]); //pass matrix to shader 

    myCube_Two.render(); 
0

除非你的着色器具有均勻稱爲ModelViewMatrix_Two,這是行不通的。我不明白了一個道理,爲什麼你的shader會需要另一個統一的模型視圖,因爲你沒有在同一呼叫繪製兩個立方體。如果這不是問題,你可以發佈你的着色器代碼嗎?

+0

哎呀,我想了解你...你說得對,我還沒有在我的着色器給予ModelViewMatrix_Two ......我想試試。 – Reanimation

+0

已經糾正了統一,現在只呈現則傳遞到shader第一... – Reanimation

+1

它解決了ModelViewMatrix。感謝的您的幫助......我只需要第二個矩陣視圖和我着色器使用與現有型號矩陣...現在工作正常。我會在下面發佈正確的代碼。謝謝。 – Reanimation