Redraw glQuads elsewhere glTranslate - OpenGL

First I have a simple white rectangular prism:

enter image description here

Here is the code:

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

Then I want to draw another rectangular prism:

enter image description here

Is it possible to redraw another prism using only glTranslate, so I don’t need to manually insert numbers?

+4
source share
1 answer

so, create a function call for drawing commands for drawing a prism with respect to its coordinate model drawPrism. Then in the code you can do this:

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

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

All Articles