Implement model-to-screen space transformations

This commit is contained in:
austin committed 2020-07-01 17:42:45 -07:00
1 parent c82f690943
commit 33cb124d77
12 files changed
+418 -7

No files matched your search

+45
View File
@@ -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
+1 -1
View File
@@ -33,7 +33,7 @@ struct Engine_Buffer
// FUNCTIONS
int Engine_Init(char *filename);
int Engine_Init(Engine_Buffer &buffer, char *filename);
void Engine_Render(Engine_Buffer &buffer, uint32_t input);
void Engine_Shutdown(void);
+17
View File
@@ -11,7 +11,24 @@ struct Face
struct Mesh
{
inline Mesh(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;
scale = 1.0f;
}
Point position;
float rotation[3];
float scale;
std::vector<Point> verts;
std::vector<Point> vertsTransformed;
std::vector<Face> faces;
};
+80
View File
@@ -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
+16
View File
@@ -19,6 +19,22 @@ struct Point
};
// OPERATORS
// v / f
inline Point operator/(Point v, float f)
{
Point result;
float inverse = 1.0f / f;
result.x = v.x * inverse;
result.y = v.y * inverse;
result.z = v.z * inverse;
return result;
}
#define POINT_H
#endif
+17
View File
@@ -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
+9
View File
@@ -1,10 +1,19 @@
#ifndef UTIL_H
#include <cmath>
// CONSTANTS
const float EPSILON_E3 = 1E-3f;
// MACROS
#define SET_BIT(x, bit) (x |= (1UL << bit))
#define CLEAR_BIT(x, bit) (x &= ~(1UL << bit))
#define CHECK_BIT(x, bit) (x & (1UL << bit))
#define DEG_TO_RAD(deg) ((deg * (float)M_PI) / 180.0f)
#define SWAP(a, b, temp) {temp = a; a = b; b = temp;}
#define FLOAT_EQUAL(a, b) ((fabsf(a - b) < EPSILON_E3) ? 1 : 0)
#define UTIL_H