1. ## OpenGL Matrices

OK, I've got several things I need to know.

#1: I need an algorithm for finding the inverse of a given matrix.

#2: I want to be able to modify OpenGL's projection matrix manually, instead of using glRotatef and glTranslatef. I know how to grab the matrix using glGetFloatv or something like that, but I don't know how to send the matrix back to replace the existing matrix.

Thanks

2. glRotatef and glTranslatef do not modify the projection matrix by default, they modify the glmodelview matrix. You can ultimately rotate anything that is expressed as a matrix as far as I know (i.e you can rotate textures and stuff)

You can modify any matrix by, as you stated, by getting the matrix from OpenGL using glGetFloatfv. You modify the matrix, then you put it back by calling glLoadMatrixf. Be careful to set the proper glmatrixmode first.

http://msdn.microsoft.com/library/de...unc03_3260.asp

My 3x3 invert function. Note that when a 3x3 matrix is a pure rotation matrix, the determinant is always 1, and you can calculate the inverse by either transposting the matrix (swap the row and column entries) or by rebuilding the matrix with the negative angles...this (the code below) is how linear algebra tells you how to compute the inverse of any 3x3 matrix (i.e, this could be from cryptography or something, where you decode a message by multiplying the encoded message by this matrix, assuming the original message was multiplied by the original matrix):

Code:
```void	Matrix3x3::Invert(void)
{
Matrix3x3 inverse;
double det, invDet;

inverse.mat[0][0] = mat[1][1] * mat[2][2] - mat[1][2] * mat[2][1];
inverse.mat[1][0] = mat[1][2] * mat[2][0] - mat[1][0] * mat[2][2];
inverse.mat[2][0] = mat[1][0] * mat[2][1] - mat[1][1] * mat[2][0];

det = mat[0][0] * inverse.mat[0][0] + mat[0][1] * inverse.mat[1][0] + mat[0][2] * inverse.mat[2][0];

if(det	==	0)
det	=	1;

invDet = 1.0f / det;

inverse.mat[0][1] = mat[0][2] * mat[2][1] - mat[0][1] * mat[2][2];
inverse.mat[0][2] = mat[0][1] * mat[1][2] - mat[0][2] * mat[1][1];
inverse.mat[1][1] = mat[0][0] * mat[2][2] - mat[0][2] * mat[2][0];
inverse.mat[1][2] = mat[0][2] * mat[1][0] - mat[0][0] * mat[1][2];
inverse.mat[2][1] = mat[0][1] * mat[2][0] - mat[0][0] * mat[2][1];
inverse.mat[2][2] = mat[0][0] * mat[1][1] - mat[0][1] * mat[1][0];

mat[0][0] = inverse.mat[0][0] * invDet;
mat[0][1] = inverse.mat[0][1] * invDet;
mat[0][2] = inverse.mat[0][2] * invDet;

mat[1][0] = inverse.mat[1][0] * invDet;
mat[1][1] = inverse.mat[1][1] * invDet;
mat[1][2] = inverse.mat[1][2] * invDet;

mat[2][0] = inverse.mat[2][0] * invDet;
mat[2][1] = inverse.mat[2][1] * invDet;
mat[2][2] = inverse.mat[2][2] * invDet;
}```

3. OK, but I really wanted an algorithm for finding the inverse of a 4x4 matrix. I'm not really that good at math, so I don't know how to modify the algorithm you gave me.

4. Strange, my math library doesn't have functions available for inverse/adjoint/determinant either...

I'm using a 4x4 matrix library as well. I havn't had the need yet for inverse operations though..

Sorry I can't help though :d

5. You should avoid inverting a matrix if you can, its a timely process.

6. Well actually I'm writing a game engine, and I need to have certain functions in it.

7. You should avoid inverting a matrix if you can, its a timely process.
Well, if you included it into something that's being computed consistantly realtime it is.

Strange, my math library doesn't have functions available for inverse/adjoint/determinant either...

I'm using a 4x4 matrix library as well. I havn't had the need yet for inverse operations though..

Sorry I can't help though :d
I wrote my own.

You're going to have to simply look online for the algorithm for inverting a matrix. Not all matrices are invertible, and most of linear algebra builds upon arcane abstractions that just 'happen to work' but have less of an intuitive interpretation (compared with other branches of math, and this is really just my own opinion according to my own interpretation of reality).

http://www.euclideanspace.com/maths/...ourD/index.htm

http://mathworld.wolfram.com/CramersRule.html

http://www.gamedev.net/community/for...age=1&#1906773

8. Sorry, I really don't understand linear algebra. Maybe I'll just leave inverting matrices until I need them.

9. Linear algebra?

Have you ever gone to high school?

Don't mean to sound harsh or anything..

But Cramer's rule isn't exactly linear

that is algebra 2 material, almost college level algebra :d

10. Sorry, I really don't understand linear algebra
Find an implementation of 4x4 matrix inversion online then. The first link typically has every concept implemented in code.

Linear algebra is an advanced branch of mathematics that typically comes after University Calculus 1, 2 and 3 and before Differential equations. I assure you that these topics aren't discussed heavily in high school, mostly not at all.

The only matrix math that I did in high school had to do with connectivity matrices (which I used for my pathfinding algorithm that I posted on this forum some time ago and will be using in my current computer game for path computing).

11. Really Bob? Maybe the linear algebra I took my freshmen year in highschool is different from yours?

We learned up to an introduction of matrices, no further though..

In intermediate algebra we learned alot about graphing, and the rest was factoring, hmm, the quadratic formula... etc..

In algebra two we learn all about matrices, adding, subtracting, inverting, determinants, cramers rule, reduced echelon form, identity matrices, and other various rules..

Maybe linear algebra in college is a review of all of these concepts?

12. Maybe the linear algebra I took my freshmen year in highschool is different from yours?
Probably substantially easier and watered-down, due to the fact that you simply do not have enough of a background in math in highschool to continue to the more advanced aspects of it. As I said, linear algebra in many of the curriculum of the schools I've looked at comes only after an in-depth study of calculus. At the University of Maine, which I attended for three semesters, it comes after Calculus 3, and builds upon these concepts (i.e. you didn't mention any study of vector spaces in your high scool classes).

Note that Calculus 3 @ UMaine was multi variable calculus, partial derivatives in three dimensions, vector/polar calculus, stuff of that nature and the start of the study of differential equations.

EDIT:
And, as I stated, I covered none of this in high school, and it's a pretty bad assumption that the study of eigen values and matrices is standard for all students with a high school diploma.

13. If you want a more concise comparison to see if your high school class really is similar to what I had, go to this link, on page 3 it lists the topics that are covered. I'm guessing you're already familiar with roughly half of them, but no more.

http://germain.umemat.maine.edu/faculty/hiebeler/class/syllabi/mat262fall04.pdf#search='umaine%20linear%20algebra '

14. Eh, I think I've gotten the first 4 bullets here in high school..

15. Not to hijack this but:
(which I used for my pathfinding algorithm that I posted on this forum some time ago and will be using in my current computer game for path computing).
You're really going to use that algo Bob? I thought you would use depth-first or some form of stack system we discussed to find paths, or even a node-based system. Good luck with it.

So that's linear algebra eh. Well since I've not had calculus in school and am totally self taught.....it looks to me as if I learned linear algebra before calculus. Perhaps that's why Bob and I are normally on the same page, if just not in different chapters...most of the time.

The 3D mathematics required in computer graphics is extremely complex but thankfully it's well researched and well documented.

I can't post this link here enough. It's like your own personaly library....www.amazon.com
Best prices and if you choose the right re-seller...excellent quality.

A book now costs less than a tank of gas for me, which is quite sad.