2017-09-15 139 views
1

我想了解OpenGL的某些部分。我必須創建一個由幾個部分組成的機器人。在開始時,我想連續繪製3個立方體。起初我想在中間畫一個立方體,然後剩下兩個。用堆疊矩陣繪製立方體

你能告訴我我做錯了什麼嗎?

void drawScene(GLFWwindow* window) { 
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 


    mat4 P=perspective(50.0f*PI/180.0f,aspect,1.0f,50.0f); 
    mat4 V=lookAt(
        vec3(0.0f,0.0f,-15.0f), 
        vec3(0.0f,0.0f,0.0f), 
        vec3(0.0f,1.0f,0.0f)); 
    glMatrixMode(GL_PROJECTION); 
    glLoadMatrixf(value_ptr(P)); 
    glMatrixMode(GL_MODELVIEW); 

    mat4 M=mat4(1.0f); 
    glLoadMatrixf(value_ptr(V*M)); 

    glPushMatrix();       //create matrix 1 on the top 
    Models::cube.drawSolid();    //draw cube on 0,0,0 coords 
    glTranslatef(3.0, 0.0, 0.0);   //apply translation to matrix 1 
    Models::cube.drawSolid();    //draw cube on 3,0,0 coords 
    glPopMatrix();       
    glRotatef(180*PI/180, 1.0, 0.0, 0.0); 
    glTranslatef(3.0, 0.0, 0.0); 
    Models::cube.drawSolid(); 

    glfwSwapBuffers(window); 
} 

並且是給我同樣的錯誤的結果另一個例子:

void drawScene(GLFWwindow* window) { 
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 


    mat4 P=perspective(50.0f*PI/180.0f,aspect,1.0f,50.0f); 
    mat4 V=lookAt(
        vec3(0.0f,0.0f,-15.0f), 
        vec3(0.0f,0.0f,0.0f), 
        vec3(0.0f,1.0f,0.0f)); 
    glMatrixMode(GL_PROJECTION); 
    glLoadMatrixf(value_ptr(P)); 
    glMatrixMode(GL_MODELVIEW); 

    mat4 M=mat4(1.0f); 
    glLoadMatrixf(value_ptr(V*M)); 

    glPushMatrix(); //Matrix 1 on the top 
    glPushMatrix(); //Matrix 2 on the top 
    Models::cube.drawSolid(); //Draw cube on matrix 2 
    glTranslatef(3.0, 0.0, 0.0); //Apply translation on matrix 2 
    glPushMatrix(); //Matrix 3 on the top 
    Models::cube.drawSolid(); //Draw cube on matrix 3 
    glPopMatrix(); //Matrix 2 on the top 
    glPopMatrix(); //Matrix 1 on the top 
    glRotatef(180*PI/180, 1.0, 0.0, 0.0); //Apply translation on matrix 1 
    glTranslatef(3.0, 0.0, 0.0); //Apply translation on matrix 1 
    Models::cube.drawSolid(); //Draw cube on matrix 1 

    glfwSwapBuffers(window); 
} 

在這兩種情況下,我不得不最後立方體6.0,而不是3.0翻譯。我真的不明白爲什麼。在我看來,在支持矩陣1之後,我對它們施加了影響。

要清楚,我想要做這樣的事情:

Draw Cube in 0,0,0 
    [] 
go to 3,0,0 
Draw Cube 
    [] [] 
Go back to 0,0,0 
Rotate in 180 degrees 
Go to -3,0,0 
Draw Cube 
[] [] [] 
+0

您可能想要簡化您的矩陣操作並避免重複的更改。即g * glPushMatrix(); //頂部的矩陣1 glPushMatrix(); //頂部的Matrix 2沒有做任何不同的事情。另外,如果你只想翻譯,然後做* glTranslatef(3.0,0.0,0.0); *然後渲染對象,而不是做推和彈。請記住,它們是昂貴的。 – Ketan

+0

這是舊的固定管道的opengl,寧可學習opengl 3.0。 – JPX

回答

1

要繪製在中心的立方體,第二個立方體轉換成平移到左側(沿右邊和第三魔方X軸)。
這可以用不同的方式來完成:


可以繪製立方體的中心,去一步向右和繪製下一立方體最後再去兩步向左一個平局第三個立方體:

float step = 3.0f; 

Models::cube.drawSolid(); 
glTranslatef(1.0f * step, 0.0f, 0.0f); 
Models::cube.drawSolid(); 
glTranslatef(-2.0f * step, 0.0f, 0.0f); 
Models::cube.drawSolid(); 


可以繪製立方體的中心,並保存()模型matirx,去一步向右和繪製下一立方終於恢復(流行)模型矩陣,去一個步驟向左的繪製第三魔方:

glPushMatrix(); 
Models::cube.drawSolid(); 
glTranslatef(1.0f * step, 0.0f, 0.0f); 
Models::cube.drawSolid(); 
glPopMatrix();       
glTranslatef(-1.0f * step, 0.0f, 0.0f); 
Models::cube.drawSolid(); 


你desided做最後的東西。
繪製立方體的中心,並保存()模型matirx,去一步向右和繪製下一立方終於恢復(流行)模型矩陣,轉身180°度,去一步到右邊繪製第三個立方體。但是你要圍了上去載體,它是旋轉(0,1,0)你的情況,而不是繞X軸:

enter image description here

glPushMatrix(); 
Models::cube.drawSolid(); 
glTranslatef(1.0f * step, 0.0f, 0.0f); 
Models::cube.drawSolid(); 
glPopMatrix();       
glRotatef(180*PI/180, 0.0, 1.0f, 0.0f); // rotate around the up vector 
glTranslatef(1.0f * step, 0.0f, 0.0f); 
Models::cube.drawSolid(); 


順便說一句,在你的第二個例子是你將3個矩陣推到棧上,但你只彈出2個矩陣。放在堆疊上的每個矩陣也應該再次取下。這意味着glPushMatrixglPopMatrix的數量應始終相等。否則你要麼沒有結束就堆積起來,否則你會試圖從空棧中取出一些東西。