Skip to content

Instantly share code, notes, and snippets.

@bakercp
Created October 24, 2013 22:10
Show Gist options
  • Save bakercp/7146013 to your computer and use it in GitHub Desktop.
Save bakercp/7146013 to your computer and use it in GitHub Desktop.
Convert a homography matrix to a gl 4x4 matrix to apply the homography to a gl texture
/* calculate a new homography matrix if the src or dst quads have changed */
if(x->changed)
{
// cvFindHomography(x,x,x); // can also use this, it is supposedly more accurate, but requires some unnessesary copies and i didn't see a difference
cvWarpPerspectiveQMatrix( x->cvsrc, x->cvdst, x->homographyMatrix ); // calculate homography
x->changed = 0;
}
// output the transformation matrix (format it as a 4x4 matrix that works with opengl)
// x->homographyMatrix->data.fl;
// is a 3 X 3 homography matrix. we need a 4 x 4 transformation matrix. they are related!
float *data = x->homographyMatrix->data.fl;
op2 = out2_bp;
// WE NEED A 4x4 GLMATRIX that we can glPushMatrix()
// 0 [ 0 0 0 0 ]
// 1 [ 0 0 0 0 ]
// 2 [ 0 0 0 0 ]
// 3 [ 0 0 0 0 ]
// [data[0], data[1], 0, data[2]]
// [data[3], data[4], 0, data[5]]
// [ 0, 0, 1, 0]
// [data[5], data[7], 0, 1]
// first row
op2 = out2_bp + 0*out2_minfo.dimstride[0];
*op2++ = data[0];
*op2++ = data[1];
*op2++ = 0;
*op2++ = data[2];
// second row
op2 = out2_bp + 1*out2_minfo.dimstride[0];
*op2++ = data[3];
*op2++ = data[4];
*op2++ = 0;
*op2++ = data[5];
// third row
op2 = out2_bp + 2*out2_minfo.dimstride[0];
*op2++ = 0;
*op2++ = 0;
*op2++ = 1;
*op2++ = 0;
// fourth row
op2 = out2_bp + 3*out2_minfo.dimstride[0];
*op2++ = data[6];
*op2++ = data[7];
*op2++ = 0;
*op2++ = 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment