Hi, I was wondering if there was an easy way to access the results of a OpenGL transformation. I’m trying to determine if two objects on screen are coincident with each other, and I can’t find an easy way to determine screen coordinates for both objects. One of the objects is stationary and the other moves up and down on the Y axis and spins on the Z.
If you want to check if the objects intersect in the screen and need to now the intersection you can use the stencil buffer for that.
Set the stencil operation (with glStencilOp) to GL_REPLACE and use 1 for the value (you may use other values depending of whether or not that conflicts with your use of the stencil buffer)and draw the first object. Then set the stencil operation to GL_OR and the value to 2. Draw the second object.
The objects intersect where the stencil buffer contains the value 3.
To get the intersection set the stencil function (with glStencilFunc) to GL_EQUAL and the reference value to 3. Then draw to the back buffer (make sure it is cleared!) something that fills the entire screen and use glReadPixels to get the intersection
It seems to me that you are wondering how to get actual/world space coordinate of transformed objects. if that so, than here are the solution.
float x,y,z;//original coordinates
float newX, newY, newZ;//storage for transformed vertices
float m[16];//storage for homogenous transformation matrices
//put all your transformations here
glGetFloatv(GL_MODELVIEW_MATRIX, m);//get current homogenous transformation matrices
//manually transform the vertices and put it in new*
newX = m[0]* x + m[4]*y + m[8]*z + m[12];
newY = m[1]* x + m[5]*y + m[9]*z + m[13];
newZ = m[2]* x + m[6]*y + m[10]*z + m[14];
basically, what we did was querying opengl modelview matrices(which is a 4 by 4 matrices represented as array of 16 floats in top down order) after it was multiplied with transformations. we use this matrices to manually compute our desired vertices transformation(by multiplying 4 by 4(transformation matrices) with 4 by 1(vertex as (x,y,z,1)) matrices). i did not use shortcuts/leet code for making it easier to understand.