#### Archived

This topic is now archived and is closed to further replies.

# math classes

This topic is 5471 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites

peace and (trance) out

Mage

##### Share on other sites
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);

##### Share on other sites
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

##### Share on other sites
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

##### Share on other sites
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]

##### Share on other sites
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...

##### Share on other sites
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 ]

##### Share on other sites
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.

##### Share on other sites
how much slower are classes in comparsion to arrays?
e.g.: CVector vs float vec[3];

1. 1
Rutin
26
2. 2
3. 3
4. 4
5. 5

• 10
• 13
• 19
• 14
• 9
• ### Forum Statistics

• Total Topics
632942
• Total Posts
3009339
• ### Who's Online (See full list)

There are no registered users currently online

×