Thread: OpenGL Matrices

  1. #1
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    499

    Lightbulb 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. #2

    Join Date
    May 2005
    Posts
    1,042
    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;
    }
    Last edited by BobMcGee123; 03-13-2006 at 07:01 PM.
    I'm not immature, I'm refined in the opposite direction.

  3. #3
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    499
    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. #4
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    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
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  5. #5
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    You should avoid inverting a matrix if you can, its a timely process.

  6. #6
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    499
    Well actually I'm writing a game engine, and I need to have certain functions in it.

  7. #7

    Join Date
    May 2005
    Posts
    1,042
    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).

    Look for cramer's rule and gaussian elimination online. Here are a few links to help you out (google)

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

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

    http://www.gamedev.net/community/for...age=1&#1906773
    I'm not immature, I'm refined in the opposite direction.

  8. #8
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    499
    Sorry, I really don't understand linear algebra. Maybe I'll just leave inverting matrices until I need them.

  9. #9
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    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
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  10. #10

    Join Date
    May 2005
    Posts
    1,042
    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).
    Last edited by BobMcGee123; 03-15-2006 at 07:11 AM.
    I'm not immature, I'm refined in the opposite direction.

  11. #11
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    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?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  12. #12

    Join Date
    May 2005
    Posts
    1,042
    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.
    Last edited by BobMcGee123; 03-15-2006 at 07:24 AM.
    I'm not immature, I'm refined in the opposite direction.

  13. #13

    Join Date
    May 2005
    Posts
    1,042
    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 '
    I'm not immature, I'm refined in the opposite direction.

  14. #14
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Eh, I think I've gotten the first 4 bullets here in high school..
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  15. #15
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  2. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  3. OpenGL .dll vs video card dll
    By Silvercord in forum Game Programming
    Replies: 14
    Last Post: 02-12-2003, 07:57 PM
  4. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM