Accessing values of D3DXMATRIX?

Started by
10 comments, last by 31337 20 years, 6 months ago
quote:Original post by 31337
The problem is is that the reason why I''m even dealing with my own matrix class is because its for my 3D engine Glaucomy which works with OpenGL too...

Ohh, i see. I''m not sure how your engine works but if the developer has to decide at compile time which API (either D3D or OpenGL) they''re going to use, you can solve this problem by using templates. This is what you can do. You can write two classes.

The first one will just be a standard matrix class but it will store matrices in column major format (for OpenGL). Try to make the interface match the one for the DX matrices. This is pretty basic stuff.

Now you have to make these two classes work together in your engine. The trick here is to write a second matrix class that is a template class around the D3D matrices and your OpenGL matrix class. This second class will just provide a generic matrix interface. It will be extremely thin. This is what I mean:
template <class MATRIX_TYPE>class CMatrix{private:  MATRIX_TYPE m_matrix;public:  CMatrix() { ZeroMemory(&m_matrix, sizeof(MATRIX_TYPE)) };  virtual ~CMatrix() {}  // assignment operators  MATRIX_TYPE& operator *= ( CONST MATRIX_TYPE& rhs )  {    return m_matrix *= rhs;  }  MATRIX_TYPE& operator += ( CONST MATRIX_TYPE& rhs )  {    return m_matrix += rhs;  }  MATRIX_TYPE& operator -= ( CONST MATRIX_TYPE& rhs )  {    return m_matrix -= rhs;  }  MATRIX_TYPE& operator *= ( FLOAT f )  {    return m_matrix *= f;  }  MATRIX_TYPE& operator /= ( FLOAT f )  {    return m_matrix /= f;  }  // More matrix stuff...};

You get the idea. This is just a very thin wrapper around your matrix interfaces. But the cool thing is that with a template, you don''t incur the overhead of using inheritance. The compiler will know at compile time which type of matrix you are using. Also, since these methods are essentially a one line call into the underlying methods of either the D3D matrix or your OpenGL version, you should put them all in the class definition because, according to the C++ standard, all functions implemented within the class definition are inlined by the compiler. For methods this small, we definately want the compiler to inline them.

You would use this class like so...
CMatrix matTranslate;
or
CMatrix matTranslate;

Here I''ve assumed that you named your OpenGL matrix class COGLMATRIX but you can name it whatever you want.

If you allow the engine to switch between D3D and OpenGL during runtime, then you''ll either have to use inheritance, or just impelment everything in either row-major or column-major and transpose the data when needed.

Hope this helps,
neneboricua
Advertisement
Well, I don''t want to use templates because I don''t want 3D API to be compile time, but rather run time. Thanks for the advice though.
OpenGL Revolutions http://students.hightechhigh.org/~jjensen/

This topic is closed to new replies.

Advertisement