<p>像<a href="https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glRotate.xml" rel="nofollow noreferrer">^{<cd1>}</a>、<a href="https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glScale.xml" rel="nofollow noreferrer">^{<cd2>}</a>和<a href="https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glTranslate.xml" rel="nofollow noreferrer">^{<cd3>}</a>这样的矩阵运算定义了一个矩阵,并将当前矩阵乘以新矩阵。<br/>
如果必须对对象应用变换,则可以将模型矩阵乘以当前矩阵。如果此变换只应用于单个对象,则在绘制对象后,逆模型矩阵可以乘以当前矩阵。这将撤消转换。<br/>
但这并不是我们想要的方法。你知道吗</p>
<p>通常的方法是在模型转换之前存储当前矩阵,在绘制对象之后恢复当前矩阵。因此,<a href="https://www.khronos.org/opengl/wiki/Legacy_OpenGL" rel="nofollow noreferrer">Legacy OpenGL</a>矩阵被组织在一个堆栈上。存储当前矩阵时,可以将其推送到堆栈上。要还原它,可以从堆栈中弹出它。你知道吗</p>
<p>使用<a href="https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glPushMatrix.xml" rel="nofollow noreferrer">^{<cd4>}/^{<cd5>}</a>存储和还原当前矩阵。e、 g.:</p>
<pre class="lang-py prettyprint-override"><code>glRotatef(1, 3, 1, 1)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glPushMatrix()
glTranslate(0.9, 0, 2)
draw_cuboid()
glPopMatrix()
glPushMatrix()
glRotate(90,1,0,0)
glTranslate(0,-1,0)
draw_torus()
glPopMatrix()
glPushMatrix()
draw_cylinder()
glPopMatrix()
pygame.display.flip()
</code></pre>
<p>注意,这样就没有必要以相反的顺序实现逆矩阵运算,因为<code>glPopMatrix</code>总是与<code>glPushMatrix</code>相反。你知道吗</p>
<p>进一步注意,有多个不同的电流矩阵(见<a href="https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glMatrixMode.xml" rel="nofollow noreferrer">^{<cd8>}</a>)。首先通过模型视图矩阵(<code>GL_MODELVIEW</code>)变换Ecah顶点坐标。然后用(<code>GL_PROJECTION</code>)投影矩阵对其进行变换。你知道吗</p>
<p>建议将透视投影矩阵(<a href="https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluPerspective.xml" rel="nofollow noreferrer">^{<cd11>}</a>)设置为<code>GL_PROJECTION</code>矩阵,将视图和模型矩阵设置为<code>GL_MODELVIEW</code>矩阵:</p>
<pre class="lang-py prettyprint-override"><code>display = (800,600)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
glMatrixMode(GL_PROJECTION)
gluPerspective(75, (display[0]/display[1]), 0.1, 50.0)
glMatrixMode(GL_MODELVIEW)
glTranslatef(0, 0, -5)
</code></pre>