By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,115 Members | 1,006 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,115 IT Pros & Developers. It's quick & easy.

Trouble with graphics...

P: 27
Hi all,

I have a problem with the graphics I'm creating for my VC application, I am using OpenGL and have created the basics of my model/graphics ok. My model contains 'hotspots' which are just points in the model were something happens if the user clicks on. I have them working fine until the model is rotated at which point I am unsure of how to update the hotspot value to reflect the new hotspot position on screen.

I am rotating the model using

glPushMatrix();
glLoadIdentity();
glRotated(ax, 1, 0, 0);
glRotated(ay, 0, 1, 0);
glMultMatrixd(m_rotation);
glGetDoublev(GL_MODELVIEW_MATRIX, m_rotation);
glPopMatrix();

where ax and ay are double values that are related to the movement of the mouse.

I think that I should use glProject/UnProject to convert between screen and world coords but am unsure how to get the new world coords for the model after rotation - I think that I would use

GLdouble mvmatrix[16];
glGetDoublev(GL_MODELVIEW_MATRIX, mvmatrix);

but I'm not sure as to what to do with the values of the modelview once I get it? I don't know if this would be the best place to get an answer but there is no OpenGL section (that i'm aware of) on this forum. Any help/pointers would be greatly appreciated as I have been struggling with this for a while.

Thank you in advance.
Dec 19 '06 #1
Share this Question
Share on Google+
4 Replies


100+
P: 145
Do you have the "red book"?

Since you know the rotations, you can manually rotate the hotspot locations (by a matrix-vector multiplication) and update the hotspot locations.

The actual rotations are in Appendix F. For instance, rotation of the point (x,y,z,1) by an angle of a about the axis (1,0,0) is given by

[1,0,0,0; 0,cos(a),-sin(a),0; 0,sin(a),cos(a),0; 0,0,0,1]*[x,y,z,1]

This will give you new coordinates of your hot spot. Don't forget that the fourth coordinate of the updated point needs to be used to rescale the x, y, and z coordinates! -- Paul
Dec 19 '06 #2

P: 27
Hey Paul,

Thanks for the reply, I was working on your suggestions but unfortunately I am still having trouble and was hoping you could possible point out where I'm going wrong. Here is what I have...

-----------------------------------------------------

// for rotation around x-axis
x = ((x * 1) + (y * 0) + (z * 0) + (w * 0));
y = ((x * 0) + (y * cos(xRotation)) + (z * sin(xRotation)) + (w * 0));
z = ((x * 0) + (y * -sin(xRotation)) + (z * cos(xRotation)) + (w * 0));
w = ((x * 0) + (y * 0) + (z * 0) + (w * 1));

// for rotation around y-axis
x = ((x * cos(yRotation)) + (y * 0) + (z * -sin(yRotation)) + (w * 0));
y = ((x * 0) + (y * 1) + (z * 0) + (w * 0));
z = ((x * sin(yRotation)) + (y * 0) + (z * cos(yRotation)) + (w * 0));
w = ((x * 0) + (y * 0) + (z * 0) + (w * 1));

GLdouble wx, wy, wz;
GLdouble mvmatrix[16], projmatrix[16];
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);

gluUnProject(x, y, z, mvmatrix, projmatrix, viewport, &wx, &wy, &wz);

------------------------------------------------

where x, y, z are the object coords of the particular hotspot and w = 1. xRotation and yRotation is the total rotation around the x and y axis respectively. I have used the matrix from red book as below;

Mx = [1, 0, 0, 0; 0, cos a, -sin a, 0; 0, sin a, cos a, 0; 0, 0, 0, 1] for rotation around x - glRotated(ax, 1, 0, 0) and
My = [cos a, 0, sin a, 0; 0, 1, 0, 0; -sin a, 0, cos a, 0; 0, 0, 0, 1] for rotation around the y axis - glRotated(ay, 0, 1, 0).

I know some of the lines in the code are not really necessary, just to 'show the working'! I was unsure if it was ok to do the matrix calculations like this one after another? The results I get are different from the new position of the hotspots (or at least were the ought to be)!

Any help would be greatly appreciated, thank you in advance. Cheers
Dec 21 '06 #3

100+
P: 145
Hey Paul,

Thanks for the reply, I was working on your suggestions but unfortunately I am still having trouble and was hoping you could possible point out where I'm going wrong. Here is what I have...

-----------------------------------------------------

// for rotation around x-axis
x = ((x * 1) + (y * 0) + (z * 0) + (w * 0));
y = ((x * 0) + (y * cos(xRotation)) + (z * sin(xRotation)) + (w * 0));
z = ((x * 0) + (y * -sin(xRotation)) + (z * cos(xRotation)) + (w * 0));
w = ((x * 0) + (y * 0) + (z * 0) + (w * 1));

// for rotation around y-axis
x = ((x * cos(yRotation)) + (y * 0) + (z * -sin(yRotation)) + (w * 0));
y = ((x * 0) + (y * 1) + (z * 0) + (w * 0));
z = ((x * sin(yRotation)) + (y * 0) + (z * cos(yRotation)) + (w * 0));
w = ((x * 0) + (y * 0) + (z * 0) + (w * 1));

GLdouble wx, wy, wz;
GLdouble mvmatrix[16], projmatrix[16];
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);

gluUnProject(x, y, z, mvmatrix, projmatrix, viewport, &wx, &wy, &wz);

------------------------------------------------

where x, y, z are the object coords of the particular hotspot and w = 1. xRotation and yRotation is the total rotation around the x and y axis respectively. I have used the matrix from red book as below;

Mx = [1, 0, 0, 0; 0, cos a, -sin a, 0; 0, sin a, cos a, 0; 0, 0, 0, 1] for rotation around x - glRotated(ax, 1, 0, 0) and
My = [cos a, 0, sin a, 0; 0, 1, 0, 0; -sin a, 0, cos a, 0; 0, 0, 0, 1] for rotation around the y axis - glRotated(ay, 0, 1, 0).

I know some of the lines in the code are not really necessary, just to 'show the working'! I was unsure if it was ok to do the matrix calculations like this one after another? The results I get are different from the new position of the hotspots (or at least were the ought to be)!

Any help would be greatly appreciated, thank you in advance. Cheers
Hmm, interesting problem. Well, the rotations are non-commutative, so the order you apply them does matter. So, you'd need to apply them in the same order as you do to the scene.

Also, if you're rotating around some other axis, do note that any rotation about an arbitrary axis is equivalent to a rotation about two easier axes, but it can be a trick to determine the new angles.

I'm not sure if I have further advice at this point; I'll try to get back to your post and give it another look as soon as I can. Thanks -- Paul
Dec 21 '06 #4

P: 27
Thanks Paul, I appreciate all your help.
Dec 22 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.