Thursday, October 23, 2008

Matrices from OpenGL, without OpenGL

"For programming purposes, OpenGL matrices are 16-value arrays with base vectors laid out contiguously in memory. The translation components occupy the 13th, 14th, and 15th elements of the 16-element matrix, where indices are numbered from 1 to 16 as described in section 2.11.2 of the OpenGL 2.1 Specification."

This at least clarifies the order of the values given to me by the glGetFloatv call. Now if I have a x,y,z vertex, how do I transform it by the returned matrix? I found mention on the web that I'm supposed to divide by W. But if I don't have W to begin with, then what should it be? Hmm, makes sense it should be one. Now I should be able to do the multiplication. Let's see if I'll manage to introduce a bug here:


// got vertex[0..2] already, multiply by matrix, divide components by w
vertex[3] = 1; // w
for (i=0;i<=4;i++) {
newVertex[i] = m[i]*vertex[0] + m[4+i]*vertex[1] + m[8+i]*vertex[2] + m[12+i]*vertex[3];
}
newVertex[0] /= newVertex[3];
newVertex[1] /= newVertex[3];
newVertex[2] /= newVertex[3];


Edit: Wow, it works.