首先,我有一个简单的白色矩形棱镜,如下所示:

这是代码:

 glBegin(GL_QUADS);

      glColor3f(255,255,255);
      glVertex3f( 0.3, 0, 2.5);//sisi bawah
      glVertex3f(0.5, 0, 2.5);
      glVertex3f(0.5, 0,  2.6);
      glVertex3f( 0.3, 0,  2.6);

      glVertex3f(0.5, 0,  2.6);//sisi kiri
      glVertex3f( 0.3, 0,  2.6);
      glVertex3f( 0.3, 1.5,  2.6);
      glVertex3f(0.5, 1.5,  2.6);

      glVertex3f(0.5, 0, 2.5);//sisi depan
      glVertex3f(0.5, 0,  2.6);
      glVertex3f(0.5, 1.5,  2.6);
      glVertex3f(0.5, 1.5,  2.5);

      glVertex3f( 0.3, 0, 2.5);//sisi belakang
      glVertex3f( 0.3, 0,  2.6);
      glVertex3f( 0.3, 1.5,  2.6);
      glVertex3f( 0.3, 1.5, 2.5);

      glVertex3f( 0.3, 1.5, 2.5);//sisi atas
      glVertex3f(0.5, 1.5, 2.5);
      glVertex3f(0.5, 1.5,  2.6);
      glVertex3f( 0.3, 1.5,  2.6);

      glVertex3f(0.5, 0,  2.5);//sisi kanan
      glVertex3f( 0.3, 0,  2.5);
      glVertex3f( 0.3, 1.5,  2.5);
      glVertex3f(0.5, 1.5,  2.5);

glEnd();

然后,我要绘制另一个矩形棱镜,如下所示:



我可以仅使用glTranslate重绘另一个棱镜,而不必手动插入数字吗?

最佳答案

是的,创建一个函数以在围绕其模型坐标drawPrism绘制棱镜时调用draw命令。然后在代码中可以执行以下操作:

//draw prism 1
glPushMatrix();
glTranslate(x1, y1, z1);
drawPrism();
glPopMatrix();

//draw prism 2
glPushMatrix();
glTranslate(x2, y2, z2);
drawPrism();
glPopMatrix();

08-16 04:07