Determine affine transform of object and apply it to other

I am wondering if the following is possible with opengl and if yes, how:

  1. I am loading Pixel coordinates of the corners of a animated (moving) series ofParalellograms from a file.

  2. I use glUnproject() and gl_Begin(GL_QUADS) to draw the parallelogram and I animate it in the display function using swapbuffers.

Points 1. and 2. are implemnted and work fine

  1. I want to see, what change happens to the parallelogram from one frame to the other (i.e. one buffer to the next) and apply the same transformation to another object (i.e. another GL_Quads) in the same scene.

Is this possible? I thought I might be able to look at the modelview matrix or something, but this doesn’t work, i.e. it always has the same values.

The code i use for 1. and 2. oint* points;


    GLdouble objx[4],objy[4],objz[4];
    GLint viewport[4];
    GLdouble modelview[16],projection[16];


    for(int i=0;i<4;i++){
      int x=(int)points[i].get()[0];
      int y=winH-(int)points[i].get()[1]; /          
      GLdouble z; // Get the right z coordinate

      glReadPixels (x, y, 1, 1, GL_DEPTH_COMPONENT, GL_DOUBLE, &z); 
      gluUnProject(x,y,z,modelview,projection,viewport,&objx[i], &objy[i],&objz[i]);


    glBegin (GL_QUADS);
    glTexCoord2f (0.0f,0.0f); 
    glVertex3f (objx[0], objy[0], 0);
    glTexCoord2f (1.0f, 0.0f);
    glVertex3f (objx[1], objy[1], 0);
    glTexCoord2f (1.0f, 1.0f);
    glVertex3f (objx[3], objy[3], 0);
    glTexCoord2f (0.0f, 1.0f);
    glVertex3f (objx[2], objy[2], 0);
    glEnd ();

Well, since you are the one sending the parallelogram data to OpenGL in the first place, it would probably be muuuuch easier to just use the original data to do the calculations.

I assume that since you are drawing parallelograms, you are working in 2D?

In this case, it should be simple to calculate what matrix accomplishes a change between two frames of the parallelogram.

  1. Find the “center” of the parallelogram in one frame by calculating where its diagonals intersect. Do the same for the next frame.
  2. The difference between these centers is the translation portion of the matrix.
  3. Pick one point on the parallelogram and calculate its angle to a horizontal line around the center of the parallelograms in both frames, which should be simple enough using the arccos function.
  4. The difference between these angles is the rotation portion of the matrix, probably done around the z axis.

Compose a matrix using the glTranslate and glRotate functions, and you can then apply the translation to other objects in your scene.


[This message has been edited by j (edited 12-11-2002).]