Some usefull functions for matrix and vector

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
KillerXX7
Posts: 22
Joined: Sat Oct 07, 2006 4:50 pm
Location: Russian Federation
Contact:

Some usefull functions for matrix and vector

Post by KillerXX7 »

Here is some very simply, but usefull functions...


Add following code to matrix4.h in class defination.

Code: Select all

//! Returns Front Vector
vector3df getFrontVector() const;
			
//! Returns Up Vector
vector3df getUpVector() const;
			
//! Returns Right Vector
vector3df getRightVector() const;
			
//! Set Front Vector
void setFrontVector(const vector3df& V);

//! Set Up Vector
void setUpVector(const vector3df& V);
			
//! Set Right Vector
void setRightVector(const vector3df& V);


//! Builds GrammSchmidt - Tree orthonormal axes with front vector aligned to the given vector
void buildGrammSchmidt(const vector3df& Front);
		
//! Builds a matrix from axes vectors
void buildFromAxes(const vector3df& Front, const vector3df& Up, const vector3df& Right);

and this anywhere in irr::core namespace in matrix4.h

Code: Select all

//! Returns Front Vector
inline vector3df matrix4::getFrontVector() const{
	return vector3df(M[0], M[1], M[2]);
}
//! Returns Up Vector
inline vector3df matrix4::getUpVector() const{
	return vector3df(M[4], M[5], M[6]);
}
//! Returns Right Vector
inline vector3df matrix4::getRightVector() const{
	return vector3df(M[8], M[9], M[10]);
}


//! Set Front Vector
inline void matrix4::setFrontVector(const vector3df& V){
	M[0] = V.X; M[1] = V.Y; M[2] = V.Z;
}

//! Set Up Vector
inline void matrix4::setUpVector(const vector3df& V){
	M[4] = V.X; M[5] = V.Y; M[6] = V.Z;
}

//! Set Right Vector
inline void matrix4::setRightVector(const vector3df& V){
	M[8] = V.X; M[9] = V.Y; M[10] = V.Z;
}

//! Builds GrammSchmidt - Tree orthonormal axes with front vector aligned to the given vector 
inline void matrix4::buildGrammSchmidt(const vector3df& Dir){
	vector3df Front, Up, Right;
	Front = Dir;
	Front.normalize();
	if(abs_<f32>(Front.Z) > 0.577f)
		Right = Front.crossProduct(vector3df(-Front.Y, Front.Z, 0.0f));
	else
		Right = Front.crossProduct(vector3df(-Front.Y, Front.X, 0.0f));
	Right.normalize();
	Up = Right.crossProduct( Front );
	setFrontVector( Front );
	setUpVector( Up );
	setRightVector( Right );
}
		
//! Builds a matrix from axes vectors
inline void matrix4::buildFromAxes(const vector3df& Front, const vector3df& Up, const vector3df& Right){
	setFrontVector( Front );
	setUpVector( Up );
	setRightVector( Right );
}


And two function for vector3d:
add this into defination of vector3d class in vector3d.h

Code: Select all

//! Returns scaled vector
vector3d<T> getScaled(T Scale){return vector3d<T>(*this *= Scale);};
vector3d<T> getScaled(vector3d<T> Scale){return vector3d<T>(X * Scale.X, Y * Scale.Y, Z * Scale.Z);}

//! Returns inverted vector
vector3d<T> getInverted(){
	return vector3df(X *= -1.0f, Y *= -1.0f, Z *= -1.0f);
}
Think Fast, Act Right... OR Be Dead :)
agi_shi
Posts: 122
Joined: Mon Feb 26, 2007 12:46 am

Re: Some usefull functions for matrix and vector

Post by agi_shi »

KillerXX7 wrote:[...]
*=
Won't that assign the scale/inverted-ness, not just return it?

Also, you don't have to multiply everything yourself in the second getScaled, just do:

Code: Select all

return *this * scale;
Post Reply