• entries
102
146
• views
79726

# Vector class

320 views

I'm going over my vector class.I was using a normal vector class with x,y,z elements and using another tVertex for meshes and models which had x,y,z plus a boneID member.
I removed the tVertex structure and added the boneID to the vector class.

Well now renaming the class Vector_3 doesn't make sense as it has 4 elements :P
But I can't find a good name for it.It will be used in all the engine and game from entity positions to mesh vertices.

Here is the current mess.

class Vector_3{public:	float x,y,z;	int iBoneID;		Vector_3(){x = 0.0f; y = 0.0f; z = 0.0f;iBoneID=0;};	Vector_3(float ix,float iy,float iz){x = ix; y = iy; z = iz;};	Vector_3(Vector_3 v1,Vector_3 v2){*this = v1 - v2;};	float magnitude() const	{		return (float)sqrt((x*x) + (y*y) + (z*z));	}	//Operators	float &operator[]( int i ) const    {        return ( float & )*( &x + i );    }	//assignment to another vector	Vector_3 operator=(const Vector_3& vec)	{		return Vector_3(x = vec.x, y = vec.y, z = vec.z);	}	//vector addition	Vector_3 operator+(Vector_3 vector)const	{		return Vector_3(vector.x + x,vector.y + y,vector.z + z);	}	//vector subtraction	Vector_3 operator-(Vector_3 vector)const	{		return Vector_3(x-vector.x, y-vector.y,z-vector.z);	}	//vector scalar addition	Vector_3 operator+(float num) const	{		return Vector_3(x + num,y + num,z + num);	}	//vector scaler multiplication	Vector_3 operator*(float num) const	{		return Vector_3(x * num,y * num,z * num);	}	//vector scaler division	Vector_3 operator/(float num) const	{		return Vector_3(x / num,y / num,z / num);	}	const Vector_3& operator+=(const Vector_3& vec)    {    		x += vec.x;        y += vec.y;        z += vec.z;        return *this;	}	const Vector_3&	operator-=(const Vector_3& vec)	{		x -= vec.x;        y -= vec.y;        z -= vec.z;        return *this;	}	const Vector_3 &operator*=(const float &s)    {        x *= s;        y *= s;        z *= s;        return *this;    }	const Vector_3 &operator*=(const Vector_3& vec)	{		x *= vec.x;		y *= vec.y;		z *= vec.z;		return *this;	}    const Vector_3 &operator/=(const float &s)    {         const float recip = 1/s; // for speed, one divecision         x *= recip;         y *= recip;         z *= recip;         return *this;    }	const Vector_3 &operator+=(const float s)	{		x += s;		y += s;		z += s;		return *this;	}	const bool operator!=(const Vector_3& vec)	{		if( x != vec.x&&			y != vec.y&&			z != vec.z)			return true;		else return false;	}	        friend const Vector_3 operator*(const float &s, const Vector_3 &vec)    {         return vec*s;    }};//Vector 4 Classclass Vector_4{public:	union	{		struct		{			Vector_3 vec3;			float w;		};		struct		{			float x,y,z,w;		};				struct		{			float x,y,z,iBoneID;		};		struct		{			float r,g,b,a;		};				float v[4];	};	Vector_4(){x = 0.0f; y = 0.0f; z = 0.0f; w = 1.0f;};	Vector_4(float ix,float iy,float iz,float iw=1.0f){x = ix; y = iy; z = iz; w = iw;};	Vector_4(Vector_3 v){vec3 = v; w = 1.0f;};	Vector_4(Vector_3 v,float iw){vec3 = v; w = iw;};	Vector_4(Vector_4 v1,Vector_4 v2){*this = v1 - v2;};		/*Vector_3 vec3() const	{		return Vector_3(x,y,z);	}*/	float magnitude() const 	{		return (float)sqrt((x*x) + (y*y) + (z*z));	}	float &operator [](int index)	{		return v[index];	}	operator float *()    {        return &x;    }  	Vector_4 operator=(const Vector_4& vec)	{		return Vector_4(x = vec.x, y = vec.y, z = vec.z,w = vec.w);	}	Vector_4 operator=(const Vector_3& vec)	{		return Vector_4(x = vec.x, y = vec.y, z = vec.z);	}	Vector_4 operator+(Vector_4 vector)const	{		return Vector_4(vector.x + x,vector.y + y,vector.z + z,vector.w + w);	}	Vector_3 operator+(Vector_3 vector)const	{		return Vector_3(vector.x + x,vector.y + y,vector.z + z);	}	Vector_4 operator-(Vector_4 vector)const	{			return Vector_4(x - vector.x, y - vector.y, z - vector.z);	}		Vector_3 operator-(Vector_3 vector)const	{			return Vector_3(x - vector.x, y - vector.y, z - vector.z);	}		Vector_4 operator+(float num)	{		return Vector_4(x + num,y + num,z + num);	}	Vector_4 operator*(float num)	{		return Vector_4(x * num,y * num,z * num);	}	Vector_4 operator/(float num) const	{		return Vector_4(x / num,y / num,z / num);	}	const Vector_4& operator+=(const Vector_4& vec)    {    		x += vec.x;        y += vec.y;        z += vec.z;		w += vec.w;        return *this;	}	const Vector_4& operator+=(const Vector_3& vec)    {    		x += vec.x;        y += vec.y;        z += vec.z;        return *this;	}	const Vector_4&	operator-=(const Vector_4& vec)	{		x -= vec.x;        y -= vec.y;        z -= vec.z;        return *this;	}	const Vector_4 &operator*=(const float &s)    {        x *= s;        y *= s;        z *= s;                  return *this;    }		const Vector_4 &operator*=(const Vector_4& vec)	{		x *= vec.x;		y *= vec.y;		z *= vec.z;		return *this;	}        const Vector_4 &operator/=(const float &s)    {         const float recip = 1/s; // for speed, one divecision         x *= recip;         y *= recip;         z *= recip;         return *this;    }	const Vector_4 &operator+=(const float s)	{		x += s;		y += s;		z += s;		return *this;	}	const bool operator!=(const Vector_4& vec)	{		if( x != vec.x&&			y != vec.y&&			z != vec.z)			return true;		else return false;	}	const Vector_4 operator*(const float &s) const    {         return Vector_4(x*s, y*s, z*s);    }        friend const Vector_4 operator*(const float &s, const Vector_4 &vec)    {         return vec*s;    }};

I may totally remove Vector_3 and only use Vector_4 as it has everything I need I guess.

I wonder if this will cause performance problems as it increase the size of a Vector by 1 float.
When drawing my vertex arrays I needed to make this modification :

glVertexPointer(3,GL_FLOAT,sizeof(Vector_3),m_pVertices);

So that i skip the last element of Vector_3 class and just use the 3 coordinates.

There are no comments to display.