#include "math.hpp" #include #include //The issue with the % operator //is that it doesn't do negative //numbers properly. For example //(-2)%2=-1, whilst we want it //to be 1. int mod(int a,int b){ int r = a%b; if(r<0)return(r+b); return r; } Vector2f::Vector2f(){} Vector2f::Vector2f(float _x, float _y){ x=_x; y=_y; } Vector2f Vector2f::operator+=(Vector2f _vec){ x+=_vec.x; y+=_vec.y; return(*this); } Vector3f::Vector3f(){} Vector3f::Vector3f(float _x, float _y, float _z){ x=_x; y=_y; z=_z; } Vector3f Vector3f::operator+=(Vector3f _vec){ x+=_vec.x; y+=_vec.y; z+=_vec.z; return(*this); } Vector3f Vector3f::operator+(Vector3f _vec){ return(Vector3f(x+_vec.x,y+_vec.y,z+_vec.z)); } Vector3f Vector3f::operator*(float c){ return(Vector3f(c*x,c*y,c*z)); } Vector3f Vector3f::operator-(){ return(Vector3f(-x,-y,-z)); } Vector3f operator*(float c, Vector3f v){ return(Vector3f(c*v.x,c*v.y,c*v.z)); } Vector3i::Vector3i(){} Vector3i::Vector3i(int _x, int _y, int _z){ x=_x; y=_y; z=_z; } Vector3i::Vector3i(Vector3f v){ x=(int)floorf(v.x); y=(int)floorf(v.y); z=(int)floorf(v.z); } Vector3i Vector3i::operator+=(Vector3i _vec){ x+=_vec.x; y+=_vec.y; z+=_vec.z; return(*this); } Vector3i Vector3i::operator-(){ return(Vector3i(-x,-y,-z)); } bool Vector3i::operator==(Vector3i _vec){ return(x==_vec.x && y==_vec.y && z==_vec.z); } Matrix4f::Matrix4f(){ //Create the identity matrix for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ if(i==j)M[i][j]=1; else M[i][j]=0; } } } struct Matrix4f NullMatrix(){ Matrix4f m; for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ m.M[i][j]=0; } } return(m); } Matrix4f Matrix4f::operator*(Matrix4f m){ Matrix4f ret = NullMatrix(); for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ for(int k=0;k<4;k++){ ret.M[i][j] += M[i][k]*m.M[k][j]; }}} return(ret); } void Matrix4f::Translate(Vector3f t){ M[0][3] = t.x; M[1][3] = t.y; M[2][3] = t.z; } struct Matrix4f ScaleMatrix(float s){ Matrix4f m = Matrix4f(); m.M[0][0] = s; m.M[1][1] = s; m.M[2][2] = s; m.M[3][3] = 1; return m; } struct Matrix4f PerspectiveProjectionMatrix(float nearZ,float farZ,float screenRatio /*Screen height divided by width*/){ Matrix4f m = Matrix4f(); m.M[1][1]=1.0/screenRatio; m.M[2][2]=(nearZ+farZ)/(farZ-nearZ); m.M[3][2]=1.0; m.M[2][3]=2*farZ*nearZ/(nearZ-farZ); m.M[3][3]=0; return m; } struct Matrix4f TranslationMatrix(Vector3f translation){ Matrix4f m = Matrix4f(); m.M[0][3]=translation.x; m.M[1][3]=translation.y; m.M[2][3]=translation.z; return m; } struct Matrix4f YawRotationMatrix(float yaw){ Matrix4f m = Matrix4f(); m.M[0][0]=cos(yaw); m.M[0][2]=-sin(yaw); m.M[2][0]=sin(yaw); m.M[2][2]=cos(yaw); return m; } struct Matrix4f PitchRotationMatrix(float pitch){ Matrix4f m = Matrix4f(); m.M[1][1]=cos(pitch); m.M[1][2]=sin(pitch); m.M[2][1]=-sin(pitch); m.M[2][2]=cos(pitch); return m; } Vertex::Vertex(){} Vertex::Vertex(Vector3f _pos,Vector2f _texturePos){ pos = _pos; texturePos = _texturePos; }