Eugene
Eugene

Reputation: 1

Render object without creating a new buffer

I have code where I made a cube (using 12 triangles) and it moves back and forth in the z-direction.

Is there a way to render this cube (say) 5 times simultaneously just at different positions in space. As of now, I would have to create a new buffer for each cube, which seems wrong.

Upvotes: 0

Views: 315

Answers (1)

Prime
Prime

Reputation: 4251

if CubeObj.draw() is your cube's draw function (likely a call to glVertexPointer then glDrawElements),

glPushMatrix();  //save the current matrix
     glTranslatef(translatex, translatey, translatez);
     //glRotatef(), glScale, etc.

     CubeObj.draw();
glPopMatrix();  //restore the matrix

the glPush/PopMatrix() calls ensure that transformation commands in the block are only applied to that particular cube.

You can call your object's draw function multiple times without reconstructing the object. That is to say, you could have another copy of the above code and change the transformation commands, and you would appear to have 2 separate cubes.

Hope this helped.

EDIT:

make sure you have a call to glLoadIdentity() at the top of your display function

Upvotes: 3

Related Questions