parent
c82f690943
commit
33cb124d77
@ -0,0 +1,45 @@
|
||||
#ifndef CAMERA_H
|
||||
|
||||
#include "point.h"
|
||||
#include "util.h"
|
||||
#include <cmath>
|
||||
|
||||
|
||||
// STRUCTURE
|
||||
struct Camera
|
||||
{
|
||||
inline Camera(void)
|
||||
{
|
||||
position.x = 0.0f;
|
||||
position.y = 0.0f;
|
||||
position.z = 0.0f;
|
||||
|
||||
rotation[0] = 0.0f;
|
||||
rotation[1] = 0.0f;
|
||||
rotation[2] = 0.0f;
|
||||
|
||||
nearClip = 1.0f;
|
||||
farClip = 1000.0f;
|
||||
}
|
||||
|
||||
inline void SetFOV(float fov, int winWidth, int winHeight)
|
||||
{
|
||||
xZoom = 1.0f / tanf(DEG_TO_RAD(fov/2.0f));
|
||||
yZoom = (xZoom * winWidth) / winHeight;
|
||||
|
||||
xScale = (0.5f * winWidth) - 0.5f;
|
||||
yScale = (0.5f * winHeight) - 0.5f;
|
||||
}
|
||||
|
||||
|
||||
Point position;
|
||||
float rotation[3];
|
||||
float nearClip, farClip;
|
||||
float xZoom, yZoom;
|
||||
float xScale, yScale;
|
||||
};
|
||||
|
||||
|
||||
#define CAMERA_H
|
||||
#endif
|
||||
|
@ -0,0 +1,80 @@
|
||||
#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
|
||||
inline Matrix operator*(Matrix m1, Matrix 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
|
||||
inline Point operator*(Point v, Matrix 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;
|
||||
}
|
||||
|
||||
|
||||
#define MATRIX_H
|
||||
#endif
|
||||
|
@ -0,0 +1,17 @@
|
||||
#ifndef TRANSFORM_H
|
||||
|
||||
|
||||
#include "matrix.h"
|
||||
|
||||
|
||||
Matrix Transform_Translate(float x, float y, float z);
|
||||
Matrix Transform_Rotate(float x, float y, float z);
|
||||
Matrix Transform_Scale(float s);
|
||||
Matrix Transform_View(Point &position, float rotation[]);
|
||||
Matrix Transform_Perspective(float zoomX, float zoomY, float nearClip, float farClip);
|
||||
Matrix Transform_Screen(float xScale, float yScale);
|
||||
|
||||
|
||||
#define TRANSFORM_H
|
||||
#endif
|
||||
|
@ -0,0 +1,104 @@
|
||||
#include "matrix.h"
|
||||
#include "transform.h"
|
||||
#include <cmath>
|
||||
|
||||
|
||||
// PUBLIC FUNCTIONS
|
||||
Matrix Transform_Translate(float x, float y, float z)
|
||||
{
|
||||
Matrix result;
|
||||
|
||||
result.e41 = x;
|
||||
result.e42 = y;
|
||||
result.e43 = z;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Matrix Transform_Rotate(float x, float y, float z)
|
||||
{
|
||||
// YXZ Euler rotation
|
||||
float cosThetaY = cosf(y);
|
||||
float sinThetaY = sinf(y);
|
||||
|
||||
Matrix tRotateY;
|
||||
|
||||
tRotateY.e11 = cosThetaY;
|
||||
tRotateY.e13 = -sinThetaY;
|
||||
tRotateY.e31 = sinThetaY;
|
||||
tRotateY.e33 = cosThetaY;
|
||||
|
||||
|
||||
float cosThetaX = cosf(x);
|
||||
float sinThetaX = sinf(x);
|
||||
|
||||
Matrix tRotateX;
|
||||
|
||||
tRotateX.e22 = cosThetaX;
|
||||
tRotateX.e23 = sinThetaX;
|
||||
tRotateX.e32 = -sinThetaX;
|
||||
tRotateX.e33 = cosThetaX;
|
||||
|
||||
|
||||
float cosThetaZ = cosf(z);
|
||||
float sinThetaZ = sinf(z);
|
||||
|
||||
Matrix tRotateZ;
|
||||
|
||||
tRotateZ.e11 = cosThetaZ;
|
||||
tRotateZ.e12 = sinThetaZ;
|
||||
tRotateZ.e21 = -sinThetaZ;
|
||||
tRotateZ.e22 = cosThetaZ;
|
||||
|
||||
|
||||
Matrix result = tRotateY * tRotateX * tRotateZ;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Matrix Transform_Scale(float s)
|
||||
{
|
||||
Matrix result;
|
||||
|
||||
result.e11 = s;
|
||||
result.e22 = s;
|
||||
result.e33 = s;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Matrix Transform_View(Point &position, float rotation[])
|
||||
{
|
||||
Matrix tInvTranslate = Transform_Translate(-position.x, -position.y, -position.z);
|
||||
|
||||
Matrix tInvRotate = Transform_Rotate(-rotation[0], -rotation[1], -rotation[2]);
|
||||
|
||||
Matrix result = tInvTranslate * tInvRotate;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Matrix Transform_Perspective(float zoomX, float zoomY, float nearClip, float farClip)
|
||||
{
|
||||
Matrix result;
|
||||
|
||||
result.e11 = zoomX;
|
||||
result.e22 = zoomY;
|
||||
result.e33 = (farClip + nearClip) / (farClip - nearClip);
|
||||
result.e34 = 1;
|
||||
result.e43 = (-2.0f * farClip * nearClip) / (farClip - nearClip);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Matrix Transform_Screen(float xScale, float yScale)
|
||||
{
|
||||
Matrix result;
|
||||
|
||||
result.e11 = xScale;
|
||||
result.e41 = xScale;
|
||||
result.e22 = -yScale;
|
||||
result.e42 = yScale;
|
||||
|
||||
return result;
|
||||
}
|
Loading…
Reference in new issue