I was studying some OpenGL codes and I found the Mesa code for the gluPickMatrix function:
void gluPickMatrix(GLdouble x, GLdouble y, GLdouble width, GLdouble height,
GLint viewport[4])
{
GLfloat m[16];
GLfloat sx, sy;
GLfloat tx, ty;
sx = viewport[2] / width;
sy = viewport[3] / height;
tx = (viewport[2] + 2.0 * (viewport[0] - x)) / width;
ty = (viewport[3] + 2.0 * (viewport[1] - y)) / height;
#define M(row, col) m[col*4+row]
M(0, 0) = sx;
M(0, 1) = 0.0;
M(0, 2) = 0.0;
M(0, 3) = tx;
M(1, 0) = 0.0;
M(1, 1) = sy;
M(1, 2) = 0.0;
M(1, 3) = ty;
M(2, 0) = 0.0;
M(2, 1) = 0.0;
M(2, 2) = 1.0;
M(2, 3) = 0.0;
M(3, 0) = 0.0;
M(3, 1) = 0.0;
M(3, 2) = 0.0;
M(3, 3) = 1.0;
#undef M
glMultMatrixf(m);
}
My doubt in this case is with the M macro and the ‘col*4+row’ operation. This equation, as I understand, is to obtain the indexes of the pick matrix and then assign them values.
Is this macro a better approach than simply using m[0] = something?
Is it faster? or more efficient?
It’s not faster in any way, the compiler will see the expanded code (the preprocessor expands the macros before the compiler gets at it).
It’s just a convenience to save typing few characters, and make the code more readable – hardcoding the equivalent constants would be much less clear.