我想了解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
[] [] []
您可能想要簡化您的矩陣操作並避免重複的更改。即g * glPushMatrix(); //頂部的矩陣1 glPushMatrix(); //頂部的Matrix 2沒有做任何不同的事情。另外,如果你只想翻譯,然後做* glTranslatef(3.0,0.0,0.0); *然後渲染對象,而不是做推和彈。請記住,它們是昂貴的。 – Ketan
這是舊的固定管道的opengl,寧可學習opengl 3.0。 – JPX