开发者

Render object without creating a new buffer

开发者 https://www.devze.com 2023-01-25 16:47 出处:网络
I have code where I made a cube (using 12 triangles) and it moves back and forth in the z-direct开发者_运维问答ion.

I have code where I made a cube (using 12 triangles) and it moves back and forth in the z-direct开发者_运维问答ion.

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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消