PDA

View Full Version : Rotation about a user defined point



DaBrain
06-18-2001, 02:26 PM
I am trying to rotate an object about a user defined point. The user clicks with the right mouse button and the point they click should be where the object rotates about.

My code works, but it rotates about the origin. Maybe advice on how to translate the origin to this user defined point would suffice.

At any rate I don't know how to project the screen coordinates onto the coordinates of the 3d object.

DaViper
06-19-2001, 12:10 AM
read out Depth-Buffer -->gluUnproject --> translate

that should do it...

[This message has been edited by DaViper (edited 06-19-2001).]

kistompika
06-19-2001, 04:59 AM
You don't know if there's something in the depth buffer, so you have to specify a z coordinate for the rotating point.(When the user clicks on the (x,y) screen coordinates, you have to transform it to world coordinates.)

When you have the point(x,y,z) to rotate things around it, you first translate the object to rotate by(-x,-y,-z), you rotate it, and translate it by(x,y,z).

Hope that's what you wanted.

Kistompika

[This message has been edited by kistompika (edited 06-19-2001).]

DaBrain
06-22-2001, 10:01 AM
Ok. I have tried a ton of things and I can't get them to work. I am posting my code so you can help better. This is a section of the code that is registered as the glutMotionFunc().



.
.
.
GLdouble *mmatrix = 0;
GLdouble *pmmatrix = 0;
GLint *vport = 0;
GLdouble *rx0 = 0;
GLdouble *rx1 = 0;
GLdouble *ry0 = 0;
GLdouble *ry1 = 0;
GLdouble *rz0 = 0;
GLdouble *rz1 = 0;
GLfloat fx0 = 0;
GLfloat fy0 = 0;
GLfloat fz0 = 0;
glGetDoublev(GL_MODELVIEW_MATRIX, mmatrix);
glGetDoublev(GL_PROJECTION_MATRIX, pmatrix);
glGetIntegerv(GL_VIEWPORT, vport);
gluUnProject(ixo, iyo, 0, mmatrix, pmatrix, vport, rx0, ry0, rz0);
gluUnProject(ixo, iyo, 1, mmatrix, pmatrix, vport, rx1, ry1, rz1);
fx0 = *rx1 - *rx0;
fy0 = *ry1 - *ry0;
fz0 = *rz1 - *rz0;
glPushMatrix();
glTranslatef(fscale*xtranso, fscale*ytranso, 0.0);
glTranslatef(fscale*fxtrans, fscale*fytrans, 0.0);
glTranslate(-fx0, -fy0, -fz0);
glRotatef(ixrotate,1.0,0.0,0.0);
glRotatef(iyrotate,0.0,1.0,0.0);
glTranslatef(fx0, fy0, fz0);
.
.
.

fscale, fxtrans, xtranso, and ixrotate are figured calculated in another function.


[This message has been edited by DaBrain (edited 06-22-2001).]

chennes
06-22-2001, 11:59 AM
Well, for starters, I am pretty sure that you need to allocate the memory for the glGet calls... don't just use pointers to nothing - dimension the arrays.

Have you tried printing out (to the console) the results of those calls? I'm surpirsed your code doesn't seg fault.

Then again, my memory could be playing tricks on me again, and this is wrong... look it up in the red book if my suggestion doesn't work.

Chris

DaBrain
06-22-2001, 12:13 PM
Originally posted by chennes:
Well, for starters, I am pretty sure that you need to allocate the memory for the glGet calls... don't just use pointers to nothing - dimension the arrays.

Have you tried printing out (to the console) the results of those calls? I'm surpirsed your code doesn't seg fault.

Then again, my memory could be playing tricks on me again, and this is wrong... look it up in the red book if my suggestion doesn't work.

Chris

The gluUnProject call just asked for a pointer.

I havent tried printing out the resluts to the calls. I thought about that as a problem, but to be honest I don't know how to do that. I didn't write this program. I just have to fix it. http://www.opengl.org/discussion_boards/ubb/smile.gif Makes things interesting huh?

chennes
06-22-2001, 12:54 PM
Originally posted by DaBrain:
The gluUnProject call just asked for a pointer.

Right, but an array is a pointer - just a pointer to a specific block of memory. I use arrays for mine - try declaring it as a
GLfloat name[16] for the matrices and name[4] for the viewport.

Chris

DaBrain
06-22-2001, 01:38 PM
Ok. Problem number 2. I think that my object is being drawn correctly and this method is working, but the origin is in a spot that I dont want it. Currently I am translating first and then rotating and it places my origin at the far right side of my object. If I rotate and then translate I get backwards movement when I rotate 180 degrees, but my object rotates the way I want it to. Is there an easy way to keep track of the angle you have rotated and reverse the coordinates when you rotate over 180?

[This message has been edited by DaBrain (edited 06-22-2001).]