noobprogrammer
noobprogrammer

Reputation: 1154

Redraw glQuads at another position by glTranslate - OpenGL

At first I have a simple white rectangular prism like this:

enter image description here

Here's 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 like this:

enter image description here

Can I redraw the another prism just by using glTranslate so I don't have to manually insert the numbers?

Upvotes: 4

Views: 275

Answers (1)

pippin1289
pippin1289

Reputation: 4939

yes, create a function to call the draw commands on drawing a prism about its model coordinates, 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();

Upvotes: 6

Related Questions