Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify column-major vs row-major (docs) #15

Open
jimon opened this issue Nov 20, 2016 · 1 comment
Open

Clarify column-major vs row-major (docs) #15

jimon opened this issue Nov 20, 2016 · 1 comment

Comments

@jimon
Copy link
Contributor

jimon commented Nov 20, 2016

Reading gb_math.h source sometimes is really confusing.
Usual math notation for matrix is Aij where i is row and j is column:

A00 A01 A02 A03
A10 A11 A12 A13
A20 A21 A22 A23
A30 A31 A32 A33

Note that this notation is memory-layout free, this is just how matrices are written down on paper in generic science/engineering notation.

Considering this:

typedef union gbMat4 {
	struct { gbVec4 x, y, z, w; };
	gbVec4 col[4];
	float e[16];
} gbMat4;

One can make an assumption that layout is column-major, awesome. But then reading something like this:

gbFloat4 *gb_float44_m(gbMat4 *m)    { return (gbFloat4 *)m; }
...
gbFloat4 *m = gb_float44_m(out);
...
m[0][0] = 1.0f / (aspect*tan_half_fovy);
m[1][1] = 1.0f / (tan_half_fovy);
m[2][2] = -(z_far + z_near) / (z_far - z_near);
m[2][3] = -1.0f;
m[3][2] = -2.0f*z_far*z_near / (z_far - z_near);

Means that m is still column-major, so it's m[j][i] notation instead of m[i][j] as accepted in generic mathematics. We can test it like this :

uint8_t a[16] = // column-major
{
	0x0, 0x1, 0x2, 0x3,
	0x4, 0x5, 0x6, 0x7,
	0x8, 0x9, 0xa, 0xb,
	0xc, 0xd, 0xe, 0xf
};
typedef uint8_t column_t[4];
column_t * c = (column_t*)a;
printf("%x\n", c[2][1]); // prints 9, which means 2 is column, 1 is row

So would be nice to write down some comments in a lib saying this, and maybe introduce some _M(i, j) macro just so algorithms are written down in more mathematics friendly notation, which should enable less error-prone code.

@jimon jimon changed the title Clarify column-major vs row-major Clarify column-major vs row-major (docs) Nov 20, 2016
@gingerBill
Copy link
Owner

I understand your concern but I don't think the operation/macro should be part of the library. I'm not going to close this issue as if others want this, I will reconsider it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants