HI, I would like to know if it is possible to find out the currently translated x,y coordinate in the middle of the draw() function.
For example, let’s say I called ofTranslate() several times with random x,y values and I want to know where the object should be located at the end of the draw() loop, I can of course store all previous x,y information and add them all…
ofDrawRectangle(0,0,10,10) // this rectangle should be placed at (150, 250)
But would there be any possible way to get the current translated x,y coordinates without having to calculate all the previous values?
Then do the following to transform your vertex:
float xp = mv * x + mv * y + mv * z + mv;
float yp = mv * x + mv * y + mv * z + mv;
float zp = mv * x + mv * y + mv * z + mv;
float wp = mv * x + mv * y + mv * z + mv;
xp /= wp;
yp /= wp;
zp /= wp;
Which in OF becomes (not tested)
ofMatrix4x4 currentMatrix = ofGetCurrentMatrix(ofMatrixMode::OF_MATRIX_MODELVIEW);
origin *= currentMatrix;
ofLog() << "Translated origin" << origin; // should give you the new origin [100,200]
Hi, Hennio. Thank you for your solution.
Sorry for the late reply, I couldn’t try this for a long time.
I just tried your code but I couldn’t get the expected result.
First of all, origin = currentMatrix; produces an error, "No viable overloaded '=’"
So if I change it to origin *= currentMatrix.getTranslation(); it compiles but I get 0,0,0 as new origin. not [100,200]
Shouldn’t the result always be (0,0,0) as anything that is multiplied to origin(0,0,0) becomes (0,0,0)?
actually that’s not correct. multiplying by the model view only as you were doing first would give you is the coordinates of the point as if the camera was the 0,0 of the world. multiplying by the projection and the modelview would give you the 2d projection of the coordinates.
what you want is to multiply the point by the model matrix alone which is not directly available in OF but you can get by multiplying the model view by the inverse of the view like: