使用堆叠矩阵绘制立方体
Drawing cubes with stacked matrix
我正在尝试了解 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
[] [] []
您想在中心绘制一个立方体,第二个立方体平移到右侧,第三个立方体平移到左侧(沿 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();
您可以在中心绘制立方体并保存 (push) 模型矩阵,向右移动一步 并绘制下一个立方体,最后恢复 (pop) 模型矩阵,向左走一步 绘制第三个立方体:
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();
最后你想做什么。
在中心绘制立方体并保存(push)模型矩阵,向右走一步并绘制下一个立方体,最后恢复( pop)模型矩阵,转180度,向右一步平局第三个立方体。但是你必须围绕向上向量旋转,在你的情况下是 (0, 1, 0),而不是围绕 X 轴:
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 个矩阵。放入堆栈的每个矩阵也应该再次取下。这意味着 glPushMatrix
和 glPopMatrix
的数量应该始终相等。不然你要么叠个没完没了,要么你就想从空叠中拿东西
我正在尝试了解 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
[] [] []
您想在中心绘制一个立方体,第二个立方体平移到右侧,第三个立方体平移到左侧(沿 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();
您可以在中心绘制立方体并保存 (push) 模型矩阵,向右移动一步 并绘制下一个立方体,最后恢复 (pop) 模型矩阵,向左走一步 绘制第三个立方体:
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();
最后你想做什么。
在中心绘制立方体并保存(push)模型矩阵,向右走一步并绘制下一个立方体,最后恢复( pop)模型矩阵,转180度,向右一步平局第三个立方体。但是你必须围绕向上向量旋转,在你的情况下是 (0, 1, 0),而不是围绕 X 轴:
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 个矩阵。放入堆栈的每个矩阵也应该再次取下。这意味着 glPushMatrix
和 glPopMatrix
的数量应该始终相等。不然你要么叠个没完没了,要么你就想从空叠中拿东西