ShadowTM's picture

Development OpenTK Mathematics

Hello I have a suggestion for the development of mathematics opentk. Based on this library, I began to develop a game engine, but saw an obvious shortage of functions for vector mathematics, vectors, matrices ... Some types of non-existent bounding box, sphere, frastum ... plane, ray. No mathematics intersections and collisions. The list goes on.
I think that is the foundation of any engine or game, the basic concepts without which it can not do.
This mathematics is written by me on C + +. I can add opentk necessary methods and classes without disrupting the overall structure, if they will be included in future releases, because I want to continue using this wonderful library.

Thank you.

For exemple i attach class Matrix on C++

	class ENGINE_EXPORT Matrix
	{
	public:
 
		float M11;	float M12;	float M13;	float M14;		
		float M21;	float M22;	float M23;	float M24;		
		float M31;	float M32;	float M33;	float M34;		
		float M41;	float M42;	float M43;	float M44;
 
		static Matrix Identity;
 
		Matrix()
			:
				M11(1.0f),	M12(0.0f),	M13(0.0f),	M14(0.0f),
				M21(0.0f),	M22(1.0f),	M23(0.0f),	M24(0.0f),
				M31(0.0f),	M32(0.0f),	M33(1.0f),	M34(0.0f),
				M41(0.0f),	M42(0.0f),	M43(0.0f),	M44(1.0f)
		{			
		}
 
		Matrix(float m11, float m12, float m13, float m14, float m21, float m22, float m23, float m24, float m31, float m32, float m33, float m34, float m41, float m42, float m43, float m44);
 
		Matrix(const std::string& str);
 
		Vector3 VectorUp ()
		{			
			Vector3 vector;
			vector.X = M21;	vector.Y = M22;	vector.Z = M23;
			return vector;
		}
		void SetVectorUp(const Vector3& value)			
		{
			M21 = value.X;	M22 = value.Y;	M23 = value.Z;
		}
 
		Vector3 VectorDown()
		{	
			Vector3 vector;
			vector.X = -M21; vector.Y = -M22; vector.Z = -M23;
			return vector;		
		}
		void SetVectorDown (const Vector3& value)
		{
			M21 = -value.X;	M22 = -value.Y;	M23 = -value.Z;
		}
 
		Vector3 VectorRight()
		{			
			Vector3 vector;
			vector.X = M11;	vector.Y = M12;	vector.Z = M13;
			return vector;
		}
		void SetVectorRight(const Vector3& value)
		{
			M11 = value.X; M12 = value.Y; M13 = value.Z;
		}
 
		Vector3 VectorLeft()
		{			
			Vector3 vector;
			vector.X = -M11; vector.Y = -M12; vector.Z = -M13;
			return vector;
		}
		void SetVectorLeft(const Vector3& value)
		{
			M11 = -value.X;	M12 = -value.Y;	M13 = -value.Z;
		}
 
		Vector3 VectorForward ()
		{			
			Vector3 vector;
			vector.X = -M31; vector.Y = -M32; vector.Z = -M33;
			return vector;
		}
		void SetVectorForward (const Vector3& value)
		{
			M31 = -value.X;	M32 = -value.Y;	M33 = -value.Z;		
		}
 
		Vector3 VectorBackward()
		{			
			Vector3 vector;
			vector.X = M31;	vector.Y = M32;	vector.Z = M33;
			return vector;
		}
		void SetVectorBackward(const Vector3& value)
		{
			M31 = value.X;	M32 = value.Y;	M33 = value.Z;
		}
 
		Vector3 Translation ()
		{			
			Vector3 vector;
			vector.X = M41;	vector.Y = M42;	vector.Z = M43;
			return vector;
		}
		void SetTranslation (const Vector3& value)
		{
			M41 = value.X;	M42 = value.Y;	M43 = value.Z;
		}		
 
		ENGINE_FORSE_INLINE const float* ToArrayPointer() const { return (const float*)this; }
 
		Matrix operator -(const Matrix& matrix1)
		{
			Matrix matrix;
			matrix.M11 = -matrix1.M11;	matrix.M12 = -matrix1.M12;	matrix.M13 = -matrix1.M13;	matrix.M14 = -matrix1.M14;
			matrix.M21 = -matrix1.M21;	matrix.M22 = -matrix1.M22;	matrix.M23 = -matrix1.M23;	matrix.M24 = -matrix1.M24;
			matrix.M31 = -matrix1.M31;	matrix.M32 = -matrix1.M32;	matrix.M33 = -matrix1.M33;	matrix.M34 = -matrix1.M34;
			matrix.M41 = -matrix1.M41;	matrix.M42 = -matrix1.M42;	matrix.M43 = -matrix1.M43;	matrix.M44 = -matrix1.M44;
			return matrix;
		}
 
		friend bool   operator == (const Matrix& matrix1, const Matrix& matrix2);
		friend bool   operator != (const Matrix& matrix1, const Matrix& matrix2);
		friend Matrix operator +  (const Matrix& matrix1, const Matrix& matrix2);
		friend Matrix operator -  (const Matrix& matrix1, const Matrix& matrix2);
		friend Matrix operator *  (const Matrix& matrix1, const Matrix& matrix2);
		friend Matrix operator *  (const Matrix matrix, float scaleFactor);
		friend Matrix operator *  (float scaleFactor, const Matrix& matrix);
		friend Matrix operator /  (const Matrix& matrix1, const Matrix& matrix2);
		friend Matrix operator /  (const Matrix& matrix1, float divider);
 
		std::string ToString();
 
		float	Determinant();
 
		static Matrix	CreateBillboard(const Vector3& objectPosition, const Vector3& cameraPosition, const Vector3& cameraUpVector, const Vector3& cameraForwardVector);
		static void		CreateBillboard(const Vector3& objectPosition, const Vector3& cameraPosition, const Vector3& cameraUpVector, const Vector3& cameraForwardVector, Matrix& result);
 
		static Matrix	CreateConstrainedBillboard(const Vector3& objectPosition, const Vector3& cameraPosition, const Vector3& rotateAxis, const Vector3& cameraForwardVector, const Vector3& objectForwardVector);
		static void		CreateConstrainedBillboard(const Vector3& objectPosition, const Vector3& cameraPosition, const Vector3& rotateAxis, const Vector3& cameraForwardVector, const Vector3& objectForwardVector, Matrix& result);
 
		static Matrix	CreateTranslation(const Vector3& position);
		static void		CreateTranslation(const Vector3& position, Matrix& result);
		static Matrix	CreateTranslation(float xPosition, float yPosition, float zPosition);
		static void		CreateTranslation(float xPosition, float yPosition, float zPosition, Matrix& result);
 
		static Matrix	CreateScale(float xScale, float yScale, float zScale);
		static void		CreateScale(float xScale, float yScale, float zScale, Matrix& result);
		static Matrix	CreateScale(const Vector3& scales);
		static void		CreateScale(const Vector3& scales, Matrix& result);
		static Matrix	CreateScale(float scale);
		static void		CreateScale(float scale, Matrix& result);
 
		static Matrix	CreateRotationX(float radians);
		static void		CreateRotationX(float radians, Matrix& result);
		static Matrix	CreateRotationY(float radians);
		static void		CreateRotationY(float radians, Matrix& result);
		static Matrix	CreateRotationZ(float radians);
		static void		CreateRotationZ(float radians, Matrix& result);
 
		static Matrix	CreateFromAxisAngle(Vector3& axis, float angle);
		static void		CreateFromAxisAngle(const Vector3& axis, float angle, Matrix& result);
 
		static Matrix	CreatePerspectiveFieldOfView(float fieldOfView, float aspectRatio, float nearPlaneDistance, float farPlaneDistance);
		static void		CreatePerspectiveFieldOfView(float fieldOfView, float aspectRatio, float nearPlaneDistance, float farPlaneDistance, Matrix& result);
		static Matrix	CreatePerspective(float width, float height, float nearPlaneDistance, float farPlaneDistance);
		static void		CreatePerspective(float width, float height, float nearPlaneDistance, float farPlaneDistance, Matrix& result);
		static Matrix	CreatePerspectiveOffCenter(float left, float right, float bottom, float top, float nearPlaneDistance, float farPlaneDistance);
		static void		CreatePerspectiveOffCenter(float left, float right, float bottom, float top, float nearPlaneDistance, float farPlaneDistance, Matrix& result);
		static Matrix	CreateOrthographic(float width, float height, float zNearPlane, float zFarPlane);
		static void		CreateOrthographic(float width, float height, float zNearPlane, float zFarPlane, Matrix& result);
		static Matrix	CreateOrthographicOffCenter(float left, float right, float bottom, float top, float zNearPlane, float zFarPlane);
		static void		CreateOrthographicOffCenter(float left, float right, float bottom, float top, float zNearPlane, float zFarPlane, Matrix& result);
		static Matrix	CreateLookAt(const Vector3& cameraPosition, const Vector3& cameraTarget, const Vector3 cameraUpVector);
		static void		CreateLookAt(const Vector3& cameraPosition, const Vector3& cameraTarget, const Vector3& cameraUpVector, Matrix& result);
		static Matrix	CreateWorld(const Vector3& position, const Vector3& forward, const Vector3& up);
		static void		CreateWorld(const Vector3& position, const Vector3& forward, const Vector3& up, Matrix& result);
		static Matrix	CreateFromQuaternion(const Quaternion& quaternion);
		static void		CreateFromQuaternion(const Quaternion& quaternion, Matrix& result);
		static Matrix	CreateFromYawPitchRoll(float yaw, float pitch, float roll);
		static void		CreateFromYawPitchRoll(float yaw, float pitch, float roll, Matrix& result);
		static Matrix	CreateShadow(const Vector3& lightDirection, Plane plane);
		static void		CreateShadow(const Vector3& lightDirection, const Plane& plane, Matrix& result);
		static Matrix	Createconstlection(const Plane& value);
		static void		Createconstlection(const Plane& value, Matrix& result);	
 
		static Matrix	Transform(const Matrix& value, const Quaternion& rotation);
		static void		Transform(const Matrix& value, const Quaternion& rotation, Matrix& result);
		static Matrix	Transpose(const Matrix& matrix);
		static void		Transpose(const Matrix& matrix, Matrix& result);		
 
		static Matrix	Invert(const Matrix& matrix);
		static void		Invert(const Matrix& matrix, Matrix& result);
 
		static Matrix	Lerp(const Matrix& matrix1, const Matrix& matrix2, float amount);
		static void		Lerp(const Matrix matrix1, const Matrix matrix2, float amount, Matrix& result);
 
		static Matrix	Negate(const Matrix& matrix);
		static void		Negate(const Matrix& matrix, Matrix& result);
 
		static Matrix	Add(const Matrix& matrix1, const Matrix& matrix2);
		static void		Add(const Matrix& matrix1, const Matrix& matrix2, Matrix& result);
 
		static Matrix	Subtract(const Matrix& matrix1, const Matrix& matrix2);
		static void		Subtract(const Matrix& matrix1, const Matrix& matrix2, Matrix& result);
 
		static Matrix	Multiply(const Matrix& matrix1, const Matrix& matrix2);
		static void		Multiply(const Matrix& matrix1, const Matrix& matrix2, Matrix& result);
		static Matrix	Multiply(const Matrix& matrix1, float scaleFactor);
		static void		Multiply(const Matrix& matrix1, float scaleFactor, Matrix& result);
 
		static Matrix	Divide(const Matrix& matrix1, const Matrix& matrix2);
		static void		Divide(const Matrix& matrix1, const Matrix& matrix2, Matrix& result);
		static Matrix	Divide(const Matrix& matrix1, float divider);
		static void		Divide(const Matrix& matrix1, float divider, Matrix& result);
 
		void			Decompose(Vector3& scale, Quaternion& rotation, Vector3& translation);
 
		private:
			struct CanonicalBasis
			{
				Vector3 Row0;
				Vector3 Row1;
				Vector3 Row2;
			};
 
			struct VectorBasis
			{
				Vector3* Element0;
				Vector3* Element1;
				Vector3* Element2;
			};	
	};

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

Contributions to the math library are indeed welcome. If you have some code that you think would be useful in the math library, please attach it to a new feature request.

As a rule of thumb, it is desirable to follow the naming conventions of XNA to ease ports between that and OpenTK. New functions should also be added to Vector[234] at the same time where applicable.

(Hint: when posting source code, select it with the mouse and click on the "C#" or "C++" buttons to enable syntax highlighting).

ShadowTM's picture

Excellent. In the near future I will make changes.