glut/progs/examples/glpuzzle.c
/*
   ** This is a screwball function.  What it does is the following:
   ** Given screen x and y coordinates, compute the corresponding object space 
   **   x and y coordinates given that the object space z is 0.9 + OFFSETZ.
   ** Since the tops of (most) pieces are at z = 0.9 + OFFSETZ, we use that 
   **   number.
 */
This is a screwball function.  What it does is the following:
Given screen x and y coordinates, compute the corresponding object space
x and y coordinates given that the object space z is 0.9 + OFFSETZ.
Since the tops of (most) pieces are at z = 0.9 + OFFSETZ, we use that
number.
This is a screwball function . What it does is the following : Given screen x and y coordinates compute the corresponding object space x and y coordinates given that the object space z is 0 . 9 + OFFSETZ . Since the tops of ( most ) pieces are at z = 0 . 9 + OFFSETZ we use that number .
int
computeCoords(int piece, int mousex, int mousey,
  GLfloat * selx, GLfloat * sely)
{
  GLfloat modelMatrix[16];
  GLfloat projMatrix[16];
  GLfloat finalMatrix[16];
  GLfloat in[4];
  GLfloat a, b, c, d;
  GLfloat top, bot;
  GLfloat z;
  GLfloat w;
  GLfloat height;
  if (piece == 0)
    return 0;
  height = zsize[piece] - 0.1 + OFFSETZ;
  glGetFloatv(GL_PROJECTION_MATRIX, projMatrix);
  glGetFloatv(GL_MODELVIEW_MATRIX, modelMatrix);
  multMatrices(modelMatrix, projMatrix, finalMatrix);
  if (!invertMatrix(finalMatrix, finalMatrix))
    return 0;
  in[0] = (2.0 * (mousex - viewport[0]) / viewport[2]) - 1;
  in[1] = (2.0 * ((H - mousey) - viewport[1]) / viewport[3]) - 1;
  a = in[0] * finalMatrix[0 * 4 + 2] +
    in[1] * finalMatrix[1 * 4 + 2] +
    finalMatrix[3 * 4 + 2];
  b = finalMatrix[2 * 4 + 2];
  c = in[0] * finalMatrix[0 * 4 + 3] +
    in[1] * finalMatrix[1 * 4 + 3] +
    finalMatrix[3 * 4 + 3];
  d = finalMatrix[2 * 4 + 3];
  top = height * c - a;
  bot = b - height * d;
  if (bot == 0.0)
    return 0;
  z = top / bot;
  w = c + d * z;
  *selx = (in[0] * finalMatrix[0 * 4 + 0] +
    in[1] * finalMatrix[1 * 4 + 0] +
    z * finalMatrix[2 * 4 + 0] +
    finalMatrix[3 * 4 + 0]) / w - OFFSETX;
  *sely = (in[0] * finalMatrix[0 * 4 + 1] +
    in[1] * finalMatrix[1 * 4 + 1] +
    z * finalMatrix[2 * 4 + 1] +
    finalMatrix[3 * 4 + 1]) / w - OFFSETY;
  return 1;
}
int computeCoords ( int piece , int mousex , int mousey , GLfloat * selx , GLfloat * sely ) { GLfloat modelMatrix [ 16 ] ; GLfloat projMatrix [ 16 ] ; GLfloat finalMatrix [ 16 ] ; GLfloat in [ 4 ] ; GLfloat a , b , c , d ; GLfloat top , bot ; GLfloat z ; GLfloat w ; GLfloat height ; if ( piece == 0 ) return 0 ; height = zsize [ piece ] - 0.1 + OFFSETZ ; glGetFloatv ( GL_PROJECTION_MATRIX , projMatrix ) ; glGetFloatv ( GL_MODELVIEW_MATRIX , modelMatrix ) ; multMatrices ( modelMatrix , projMatrix , finalMatrix ) ; if ( ! invertMatrix ( finalMatrix , finalMatrix ) ) return 0 ; in [ 0 ] = ( 2.0 * ( mousex - viewport [ 0 ] ) / viewport [ 2 ] ) - 1 ; in [ 1 ] = ( 2.0 * ( ( H - mousey ) - viewport [ 1 ] ) / viewport [ 3 ] ) - 1 ; a = in [ 0 ] * finalMatrix [ 0 * 4 + 2 ] + in [ 1 ] * finalMatrix [ 1 * 4 + 2 ] + finalMatrix [ 3 * 4 + 2 ] ; b = finalMatrix [ 2 * 4 + 2 ] ; c = in [ 0 ] * finalMatrix [ 0 * 4 + 3 ] + in [ 1 ] * finalMatrix [ 1 * 4 + 3 ] + finalMatrix [ 3 * 4 + 3 ] ; d = finalMatrix [ 2 * 4 + 3 ] ; top = height * c - a ; bot = b - height * d ; if ( bot == 0.0 ) return 0 ; z = top / bot ; w = c + d * z ; * selx = ( in [ 0 ] * finalMatrix [ 0 * 4 + 0 ] + in [ 1 ] * finalMatrix [ 1 * 4 + 0 ] + z * finalMatrix [ 2 * 4 + 0 ] + finalMatrix [ 3 * 4 + 0 ] ) / w - OFFSETX ; * sely = ( in [ 0 ] * finalMatrix [ 0 * 4 + 1 ] + in [ 1 ] * finalMatrix [ 1 * 4 + 1 ] + z * finalMatrix [ 2 * 4 + 1 ] + finalMatrix [ 3 * 4 + 1 ] ) / w - OFFSETY ; return 1 ; }
This is a screwball function.
This is a screwball function .
/* 
     ** Ok, now we need to solve for z: **   (a + b z) / (c + d 

     z) = height. ** ("height" is the height in object space we 

     want to solve z for) ** ** ==>  a + b z = height c +
     height d z **      bz - height d z = height c - a ** z =
     (height c - a) / (b - height d) */ /* 
     ** Ok, no problem. ** Now we solve for x and y.  We know
     that w = c + d z, so we compute it. */ /* 
     ** Now for x and y: */