 # Rotating by specifying a point

Hey guys! I am trying to rotate a cone(or any other obj drawn on the screen) using one mouse click. When there is a mouse click, the angle between line joining the origin and the clicked point is calculated and the obj is rotated. However the code doesn’t seem to be working.

The drawn obj like for eg: cone will already be a translated one, drawn using glutWireCone. I’m pasting the partial code here. Please do tell me if i am making any logical error

void rotate(int x,int y)// x,y values returned from mouse call back (the clicked point)
{
float r;
float cx,cy;

``````	cx=x*1.0;
cy=y*1.0;
r=asin(cy/cx);
printf("
``````

thetha = %f",r);
glRotatef(r, 1.0, 0.0, 0.0); //rotate on the x axis
glRotatef(r, 0.0, 1.0, 0.0); //rotate on the y axis
glRotatef(r, 0.0, 0.0, 1.0);
//glFlush();
}

First, for this to work, the calls to glRotate must be where you draw the scene, just before drawing the cone. So you better should calculate the angle r when the mouse is clicked, store the angle and later use it in glRotate when drawing the scene.

Second, the code to compute r seems ok, but you have to subtract from x,y the position of the origin in screen coordinates. If by origin you mean the center of the cone or the origin of the world coordinate system, you will have to compute its screen position. As it is right now, you are calculating the angle from x,y to the upper-left corner of the screen.

Basically I’m subtracting the value of y before i pass it onto this function. I know the part of rotating before actually drawing the diagram but what I’m asking is isn’t it possible to rotate the scene after it is drawn.
As in my cone is ready, then i decide to orient the cone wrt a point (x,y) and origin. Isn’t this possible at all?

but what I’m asking is isn’t it possible to rotate the scene after it is drawn.

The glRotate function rotates the cone for drawing only. I guess what you need is:

1- To have a description of the rotation of the cone
2- to be able to rotate the cone from the current rotation to a new one, changing the ‘description’
3- To pass the description of the rotation to opengl for drawing

In 1, you can have a simple angle, or a matrix, or a quaternion.

In 2, you have to do it yourself, Opengl doesn’t help you here. With the angle, you could add the current angle with the new rotation made with the mouse.

In 3, if the data in (1) is a simple angle, you can use glRotate (this is what you are doing now). If you had a matrix or a quaternion, you’d use glLoadMatrix.

hey guys! i am using the menu options, and when i select aan option in the menu the functions calls the menu option, but the function does not stay there for long and it returns back to the main function… can you suggest me the solution for it???

OK. Thanks I’ll try that out. However to add to my woes it may not always be a cone that is being rotated. It could be a sphere/Taurus/tetrahedron/cube. Hence i’ll have to keep track of that.

Actually i was trying to code a mechanism by which i click on rotate button, then click in drawing area specifying the point of rotation, then the already drawn obj orients automatically.

If any other techniques are there do let me know.