Because what you’re doing is with all the rotates and transforms is basically just multiplying a vectors, you can do all the multiplications and translations to a ofxVec3f vector and then multiply your point by that late to get the point, or you can call glGetFloatv() at the end of all your transformations to store everything you’ve done. Check out this page http://www.songho.ca/opengl/gl-transform.html for some info on that GL method and how it’s done. The somewhat less than friendly docs on that are here: http://www.opengl.org/sdk/docs/man/xhtml/glGet.xml
The basic idea is to store everything you’ve done in an array and then multiply your point by the appropriate values in the matrix that you get back from glGetFloatv()
i’ve been trying to wrap my head around this very same problem, and mr. noble’s code sample helps quite a fair bit but it doesn’t completely solve my problem.
what i’ve observed is that the modelview matrix contains the rotation that was calculated but does not seem to take the preceding translation (the one before the rotation) into consideration.
… and the so value of the translation needs to be added back first in order to get the correct absolute 3d position.
// center the object
glTranslatef(ofGetWidth()/2, ofGetHeight()/2, 0);
// rotate around z and y
glRotatef(ofNoise(ofGetFrameNum() * 0.002) * 360, 0, 0, 1);
glRotatef(ofNoise((ofGetFrameNum() + 251) * 0.002) * 360, 0, 1, 0);
// calculate the position of the rotated point
ofxVec3f point(200, 0, 0);
ofxVec3f result = point * mat;
// invert the y position
res.y *= -1;
// add back that original translation, which makes me scratch my head
res += ofxVec3f(ofGetWidth()/2, ofGetHeight()/2, 0);
is this b/c of the way that oF handles the opengl viewport, setting 0, 0 to be the upper left corner of the screen?
i now understand that gluLookAt does in fact affect the modelview matrix, so the position values in that matrix were reflecting this. i’m still not 100% certain if there’s a better way to do this, but i needed to add the eyeX and eyeY and dist values (from the gluLookAt function) to get the absolute position.
by the way, really easy way to determine the position of an object after transformations:
I had the same question as ozke, trying to get the absolute position of the point and found this thread. Unfortunately it doesn’t work for me, so I think I have misunderstood something.
Here is what my draw code is :
the problem is that the model view matrix that OF uses by default is not the identity since we set the 0,0 at the top,left and move the camera back from z=0 so the coordinates of the screen are those of the size of the window
When you download the current model view and multiply that by your point you are actually multiplying that transformation twice + the translation, to get the exact same result as the red circle you would need to set the modelview to the identity like:
Hey everybody. I tried the example by Arturo but unfortunately I still can’t find the answer to the problem. Arturo is applying only a translation. In my case I am also adding a rotation.
Why is the blue and red circles not aligned and how can I make them to do so? The translation bit works. It’s the rotation that’s giving me a headache. Any help much appreciated.