Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Движение луча света по поверхности Безье / bezier / Matrix
.cpp
/*
File: Matrix.cpp
Author: Thomas Mшlhave (AKA Thoooms)
Info: This file contains the declarations and functions
needed to perform some basic matrix math.
Features;
Multiply 4x1 with 4x1
rotate about x-axis
zero out a 4x4 matrix
make an 4x4 identity matrix
To do:
4x1 matrix - DONE
4x4 matrix - DONE
4x1 X 4x4 function - DONE
*/
#include "matrix.h"
#include <math.h> //for sin and cos
#ifndef __MAT4X4_CPP_INCLUDED__
#define __MAT4X4_CPP_INCLUDED__
void Matrix4x4::MultiplyWithScalar(float num)
{
for (int row = 0; row < 4; row++)
{
for (int col = 0; col < 4; col++)
{
M[row][col] *= num;
}
}
}
void Matrix4x4::ZeroOutMatrix(void)
{
M[0][0] = 0.0f;
M[0][1] = 0.0f;
M[0][2] = 0.0f;
M[0][3] = 0.0f;
M[1][0] = 0.0f;
M[1][1] = 0.0f;
M[1][2] = 0.0f;
M[1][3] = 0.0f;
M[2][0] = 0.0f;
M[2][1] = 0.0f;
M[2][2] = 0.0f;
M[2][3] = 0.0f;
M[3][0] = 0.0f;
M[3][1] = 0.0f;
M[3][2] = 0.0f;
M[3][3] = 0.0f;
}
Matrix4x4 Matrix4x4::MultiplyWith4x4(Matrix4x4* a)
{
Matrix4x4 result;
result.ZeroOutMatrix();
for (int row = 0; row < 4; row++)
{
for (int col = 0; col < 4; col++)
{
for (int haps = 0; haps < 4; haps++)
{
result.M[row][col] += M[row][haps] * a->M[haps][col];
}
}
}
return result;
}
void Matrix4x4::MakeIdentityMatrix_4x4(void)
{
/*Constructs the following matrix
0 1 2 3
0 [ 1 0 0 0 ]
1 [ 0 1 0 0 ]
2 [ 0 0 1 0 ]
3 [ 0 0 0 1 ]
*/
ZeroOutMatrix();
M[0][0] = 1.0f;
M[1][1] = 1.0f;
M[2][2] = 1.0f;
M[3][3] = 1.0f;
}
float Matrix4x1::GetLenght()
{
return float (sqrt(M[0]*M[0]+M[1]*M[1]+M[2]*M[2]));
}
Matrix4x1 Matrix4x1::CrossProduct(Matrix4x1* a)
{
Matrix4x1 b;
b.M[0] = M[1]*a->M[2]-M[2]*a->M[1];
b.M[1] = M[2]*a->M[0]-M[0]*a->M[2];
b.M[2] = M[0]*a->M[1]-M[1]*a->M[0];
b.M[3] = 1.0f;
return b;
}
void Matrix4x1::InsertVertex(unsigned int row, float* num)
{
M[row] = *num;
}
void Matrix4x1::Translate(float dx, float dy, float dz)
{
//does it fast without matrix math
M[0] += dx;
M[1] += dy;
M[2] += dz;
}
void Matrix4x1::Scale(float sx, float sy, float sz)
{
//does it relative fast without matrix math
M[0] *= sx;
M[1] *= sy;
M[2] *= sz;
}
Matrix4x1 Matrix4x1::MultiplyWith4x4(Matrix4x4 *a)
{
Matrix4x1 c;
c.M[0] = M[0]*a->M[0][0] + M[1]*a->M[0][1] + M[2]*a->M[0][2] + M[3]*a->M[0][3];
c.M[1] = M[0]*a->M[1][0] + M[1]*a->M[1][1] + M[2]*a->M[1][2] + M[3]*a->M[1][3];
c.M[2] = M[0]*a->M[2][0] + M[1]*a->M[2][1] + M[2]*a->M[2][2] + M[3]*a->M[2][3];
c.M[3] = M[0]*a->M[3][0] + M[1]*a->M[3][1] + M[2]*a->M[3][2] + M[3]*a->M[3][3];
c.params = params;
return c;
}
void Matrix4x1::DivideWith_W( void )
{
if (M[3] == 0) //if w is 0
{
return;
}
M[0] /= M[3];
M[1] /= M[3];
M[2] /= M[3];
M[3] /= M[3];
}
void Matrix4x1::ProjectPerspective (float distance_from_screen) // returns the projektet coordinate
{
//first build perspective matrix
/*
0 1 2 3
0 [ 1 0 0 0 ]
1 [ 0 1 0 0 ]
2 [ 0 0 0 0 ]
3 [ 0 0 1/d 1 ]
*/
Matrix4x4 perspective; //perspective matrix
perspective.ZeroOutMatrix();
perspective.M[0][0] = 1;
perspective.M[1][1] = 1;
perspective.M[3][3] = 1;
perspective.M[3][2] = (1.0f/distance_from_screen);
*this = MultiplyWith4x4(&perspective);
DivideWith_W();
}
Matrix4x1 Matrix4x1::XRotate(float Angle)
{
Matrix4x4 rot;
//first build x-rotation matrix
/*
0 1 2 3
0 [ 1 0 0 0 ]
1 [ 0 cos a -sin a 0 ]
2 [ 0 sin a cos a 0 ]
3 [ 0 0 0 1 ]
*/
rot.MakeIdentityMatrix_4x4();
rot.M[1][1] = (float) cos(Angle*TM_PI_DIV_180);
rot.M[1][2] = (float) - sin(Angle*TM_PI_DIV_180);
rot.M[2][1] = (float) sin(Angle*TM_PI_DIV_180);
rot.M[2][2] = (float) cos(Angle*TM_PI_DIV_180);
return MultiplyWith4x4(&rot);
}
Matrix4x1 Matrix4x1::ZRotate(float Angle)
{
Matrix4x4 rot;
//first build z-rotation matrix
/*
0 1 2 3
0 [ cos a -sin a 0 0 ]
1 [ sin a cos a 0 0 ]
2 [ 0 0 1 0 ]
3 [ 0 0 0 1 ]
*/
rot.MakeIdentityMatrix_4x4();
rot.M[0][0] = (float) cos(Angle*TM_PI_DIV_180);
rot.M[0][1] = (float) - sin(Angle*TM_PI_DIV_180);
rot.M[1][0] = (float) sin(Angle*TM_PI_DIV_180);
rot.M[1][1] = (float) cos(Angle*TM_PI_DIV_180);
return MultiplyWith4x4(&rot);
}
Matrix4x1 Matrix4x1::YRotate(float Angle)
{
Matrix4x4 rot;
//first build z-rotation matrix
/*
0 1 2 3
0 [ cos a 0 sin a 0 ]
1 [ 0 1 0 0 ]
2 [ -sin a 0 cos a 0 ]
3 [ 0 0 0 1 ]
*/
rot.MakeIdentityMatrix_4x4();
rot.M[0][0] = (float) cos(Angle*TM_PI_DIV_180);
rot.M[0][2] = (float) sin(Angle*TM_PI_DIV_180);
rot.M[2][0] = (float) -sin(Angle*TM_PI_DIV_180);
rot.M[2][2] = (float) cos(Angle*TM_PI_DIV_180);
return MultiplyWith4x4(&rot);
}
Matrix4x1::Matrix4x1()
{
M[0] = M[1] = M[2] = 0.0f;
M[3] = 1.0f;
}
Matrix4x1::Matrix4x1(float x, float y, float z, float w)
{
M[0] = x;
M[1] = y;
M[2] = z;
M[3] = w;
}
void Matrix4x1::MultiplyWithScalar(float num)
{
for (int row = 0; row < 4; row++)
{
M[row] *= num;
}
}
#endif __MAT4X4_CPP_INCLUDED__