1
0
Fork 0
2018-soft-3d-renderer/include/matrix.h

109 lines
1.7 KiB
C
Raw Normal View History

#ifndef MATRIX_H
#include "point.h"
// STRUCTURE
struct Matrix
{
inline Matrix(void)
{
e11 = 1.0; e12 = 0.0; e13 = 0.0; e14 = 0.0;
e21 = 0.0; e22 = 1.0; e23 = 0.0; e24 = 0.0;
e31 = 0.0; e32 = 0.0; e33 = 1.0; e34 = 0.0;
e41 = 0.0; e42 = 0.0; e43 = 0.0; e44 = 1.0;
}
union
{
float e[4][4];
struct
{
float e11, e12, e13, e14;
float e21, e22, e23, e24;
float e31, e32, e33, e34;
float e41, e42, e43, e44;
};
};
};
// OPERATORS
// m1 * m2
2018-09-19 05:33:52 +00:00
inline Matrix operator*(Matrix const &m1, Matrix const &m2)
{
Matrix result;
for (int row = 0; row < 4; ++row)
{
for (int col = 0; col < 4; ++col)
{
float sum = 0.0;
for (int i = 0; i < 4; ++i)
{
sum += m1.e[row][i] * m2.e[i][col];
}
result.e[row][col] = sum;
}
}
return result;
}
// v * m
2018-09-19 05:33:52 +00:00
inline Point operator*(Point const &v, Matrix const &m)
{
Point result;
for (int col = 0; col < 4; ++col)
{
float sum = 0.0;
for (int row = 0; row < 4; ++row)
{
sum += v.e[row] * m.e[row][col];
}
result.e[col] = sum;
}
return result;
}
2018-09-11 02:51:59 +00:00
// v *=m
2018-09-19 05:33:52 +00:00
inline Point &operator*=(Point &v, Matrix const &m)
2018-09-07 01:32:15 +00:00
{
v = v * m;
return v;
}
2018-09-11 02:51:59 +00:00
// v * m
2018-09-19 05:33:52 +00:00
inline Vector operator*(Vector const &v, Matrix const &m)
2018-09-11 02:51:59 +00:00
{
Vector result;
for (int col = 0; col < 4; ++col)
{
float sum = 0.0;
for (int row = 0; row < 4; ++row)
{
sum += v.e[row] * m.e[row][col];
}
result.e[col] = sum;
}
return result;
}
#define MATRIX_H
#endif