Representing matrices in opengl
Im remaking my matrix class and I was wondering if a union between float array[16] and float mat[4][4] will work ok.
Edit: Will mat[0][3] be the fourth element of "array"? Im using C++ btw
[edited by - executor_2k2 on June 25, 2002 6:15:30 AM]
I just represent mine internally as float[16], and provide various accessors and mutators to provide different views/access points to the data... eg
- an Item(int row, int column) accessor/mutator to allow row/column access
- accessor and mutator for each of the rotational axes and the translation part of the matrix (each a 3-component vector)
It just feels a lot neater and safer than mucking about with unions and allowing free-for-all access to the data in random manners
- an Item(int row, int column) accessor/mutator to allow row/column access
- accessor and mutator for each of the rotational axes and the translation part of the matrix (each a 3-component vector)
It just feels a lot neater and safer than mucking about with unions and allowing free-for-all access to the data in random manners
You can grab an array class there. And a multidimensional array class there
Alternatively...
Will provide mat(n) and mat(x,y) as a substitute to mat[n] or mat[x][y] (which would require the use of a proxy class).
Edit: note that you cannot replace operator() by operator[] since the processing of the comma as parameter separator is specific to operator().
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
[edited by - Fruny on June 25, 2002 2:43:55 AM]
Alternatively...
class Matrix4f{public: float& operator()( size_t n ) { return data[n]; }; const float& operator()( size_t n ) const { return data[n]; }; float& operator()( size_t x, size_t y ) { return data[x+4*y]; }; const float& operator()( size_t n ) const { return data[x+4*y]; };private: float data[16];};
Will provide mat(n) and mat(x,y) as a substitute to mat[n] or mat[x][y] (which would require the use of a proxy class).
Edit: note that you cannot replace operator() by operator[] since the processing of the comma as parameter separator is specific to operator().
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
[edited by - Fruny on June 25, 2002 2:43:55 AM]
I could tell you, but then I would have to kill you. But seriously the best way to answer a question like that is to use your debugger. Set the matrix entries to 00, 01, 02, 03, 10, 11, 12, 13, 20, etc and see where they end up in the array.
As far as i know the memory layout of array[4][4] is exactly the same as array[16]...the only difference is the way you access it.
Remember its always possible to say *(array + i) and access the memory that way (it doesn''t matter which way it was created)
- Ces
Remember its always possible to say *(array + i) and access the memory that way (it doesn''t matter which way it was created)
- Ces
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement