通过glTranslatef重写gl_Quads在另一个位置 - OpenGL

时间:2013-10-16 16:22:18

标签: c++ c opengl 3d glut

起初我有一个简单的白色矩形棱镜:

enter image description here

以下是代码:

 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();

然后,我想画另一个矩形棱镜:

enter image description here

我可以使用glTranslate重新绘制另一个棱镜,这样我就不必手动插入数字吗?

1 个答案:

答案 0 :(得分:6)

是的,创建一个函数,在绘制关于其模型坐标drawPrism的棱镜时调用绘图命令。然后在代码中你可以这样做:

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

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