rotating objects once they are already drawn

Started by
5 comments, last by vrybak 20 years, 1 month ago
I am trying to draw 2 triangles. I want to be able to rotate one of the triangles upon a button click. But I cant figure out why both of them are always rotating. Is there any way to tag a drawn object and then refer to it when I want to do a rotation or scaling operation?
Advertisement
here is what you are probably doing:

rotate( someDegrees )
drawTriangleOne();
drawTriangleTwo();

when you should actually be doing this:

drawTriangleOne();
rotate( someDegrees )
drawTriangleTwo();

there is no such thing as rotating an object when its already drawn... the rotating & translating takes place before drawing.... BUT... you can always redraw (& if you redraw enough with some movement between each time, you get animation)

I hope that was simple enough for you.
ok, but what if I want to draw some objects first, and then when the user requests I want to rotate one of them. Kind of like in a paint program. They let me draw a square first and then i can select the move button and move it around the screen. Is it possible to do something like that in openGL?
See it like this:
Main Program  Main Loop (while the user doesn't want to exit)    Check for input and handle it    Draw the 'scene'    Swap Buffers (double buffering)  End Main LoopEnd Program


[edited by - Tree Penguin on March 5, 2004 12:33:03 PM]
What you are trying to do is not an OpenGL issue, but rather one of general programming technique. It can certainly be done. Off the top of my head I would think you could have a CheckKeyPress() routine called in your DrawScene or main loop. It detects a particular key press or mouse click and sets a global flag. In your draw routine you would have an if statement checking for the flag being true and if it is perform a rotate command.

pseudo code might look like this:

DrawScene()
{
CheckKeyPress()
DrawTriangleOne()
if rotateFlag is true
then rotate(someDegrees)
endif
DrawTriangleTwo()

increment someDegrees
if someDegrees greater than 359
then someDegrees = 0
}

In your CheckKeyPress routine you should probably check for which triangle was clicked and the modify the if statements to rotate the appropriate one in your DrawScene() routine. Dectecting which triangle was clicked is more tricky, but all the info you need is in the NeHe tutorials.

Hope that helps.
i would advise u create a class for the tris
class TRIANGLE
{
float x,y,z;
float rx,ry,rz;
bool mouseintri(int x,int y);
}

//youi get the idea thus using a class or a struct u can(basiclly using an array) create alot of tris and rotate them individualy(i think that is spealt correct)
Spy
perhaps a better way if you are doing something like this especially when you are rotating a single triangle and there''s other triangles drawn afterwards is to use glPushMatrix and glPopMatrix:

CheckKeyPress();DrawTriangleOne();if(rotateFlag){   glPushMatrix();   rotate(someDegrees);   glPopMatrix();}DrawTriangleTwo();someDegrees += increment;if (someDegrees >= 360)   someDegrees = 0


(I unbasiced the example in question! )
The Love Of Trees

This topic is closed to new replies.

Advertisement