math classes

Started by
9 comments, last by perplx 20 years, 5 months ago
Ok, so i''m using c++ and OpenGL on MinGW, and i''m making Math classes: Vector2, Vector3, Vector4, Matrix3, Matrix4 And I wonder, how best to implement them? ie: class Vector4 { float x, y, z, w; //OR float vector[4]; //? } , class Matrix4 { float matrix[16]; //OR float matrix[4][4];//OR Vector4 columns[4];//? } which would be the fastest? From my experience, i can overload Matrix4::operator float *() even when it''s an array of Vector4''s, and send a pointer to Matrix4 to glMultMatrixf(), or a Vector3 * to glVertex3fv() when it''s {float x,y,z,w;}, but, well, i don''t trust my compiler, and i imagine it''s not cross-platform, not clean and unsafe. Could that be done SAFELY? I haven''t found anything on this subject, even in my books on game programming. Thanks i perplx
i perplx
Advertisement
Read this.

peace and (trance) out

Mage
---------------------------------------------------There are 10 kinds of people in the world:Those that understand binary, and those that dont...Mage
quote:Original post by perplx
...
class Vector4
{
float x, y, z, w; //OR
float vector[4]; //?
}
,

class Matrix4
{
float matrix[16]; //OR
float matrix[4][4];//OR
Vector4 columns[4];//?
}

which would be the fastest?

When in doubt, use all!
class Vector4{  union  {    struct { float x, y, z, w; }; //AND    float vector[4];  // :-)  };}class Matrix4{  union  {    float matrix[16];  //AND    float matrix[4][4];//AND    Vector4 columns[4];// :-)  };} 

All member of a union start at the same place in memory, so ''y'' in Vector4 equals vector[1].

quote:
From my experience, i can overload Matrix4::operator float *() even when it''s an array of Vector4''s, and send a pointer to Matrix4 to glMultMatrixf(), or a Vector3 * to glVertex3fv() when it''s {float x,y,z,w;}, but, well, i don''t trust my compiler, and i imagine it''s not cross-platform, not clean and unsafe. Could that be done SAFELY?

I have done just that and it works for me, but I''ve only tested it in a linux environment with gcc.

In my vector class:
operator T*() { return &x; }operator const T*() const { return &x; } 

To pass a vector to OpenGL:
Vector v(10.0, 20.0, 30.0);glVertex3dv(&v); 



You''re using mingw, I think that is about as close to portable as you will possibly find for the win32 platform. Also, as for the real nature of your question, read what those other guys said
super genius
quote:Original post by Anonymous Poster

All member of a union start at the same place in memory, so ''y'' in Vector4 equals vector[1].



UUmmmm, i''m not so sure about that... Unless in an array, the compiler COULD decide any offset it wants between variables. I THINK this is what "struct packing" is about...

But then again, i''m just a stupid college boy

i perplx
i perplx
quote:Original post by perplx
Unless in an array, the compiler COULD decide any offset it wants between variables. I THINK this is what "struct packing" is about...


"All member of a union start at the same place in memory"

Note though that there are strong restrictions to the type of data members you can place in a union.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

[edited by - Fruny on October 6, 2003 11:45:52 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
With even a half-decent compiler, the union thing should work great. I use it too. I have run a *lot* of tests to see if there way any padding or gaps between the data. No problem for the. The data alignes perfectly, so big arays of that union Vector3 are no problem.

I even overloaded the [] operator so that I can do this:

Vector3 myVec;
myVec[0]=1.0f;
myVec[1]=0.0f;
...
etcetera...

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

DXSDK9 has a Matrix optimized for performance, but it does not work on every compiler.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/d3dx/structures/d3dxmatrixa16.asp



[ Games made by gamers'' minds | maxiInnovation - miniCan | mail: ChristianRoesch@gmx.de ]
[ Games made by gamers' minds | maxiInnovation - miniCan | mail: ChristianRoesch@gmx.de ]
quote:Original post by Sander
I even overloaded the [] operator so that I can do this:

Vector3 myVec;
myVec[0]=1.0f;
myVec[1]=0.0f;
...
etcetera...

I also did that. I''m thinking about rewriting my matrix class so that it consists of vectors instead of 16 real numbers. That way I can do this
Matrix m;m[2][1] = 1.0;m[3][0] = 0.0; 

For that to work the [] operator of the matrix class should return a reference to a vector. And the vector''s [] operator should return a reference to a real number.

quote:Original post by Fruny Note though that there are strong restrictions to the type of data members you can place in a union.

Yes, I noticed that when I tried to put my vector class in a union. I was a bit disappointed about that.

how much slower are classes in comparsion to arrays?
e.g.: CVector vs float vec[3];
http://www.8ung.at/basiror/theironcross.html

This topic is closed to new replies.

Advertisement