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

+122 -1
View File
@@ -1,12 +1,23 @@
#include "camera.h"
#include "engine.h"
#include "loader.h"
#include "matrix.h"
#include "transform.h"
#include "util.h"
#include <cstdio>
// GLOBALS
static Mesh mesh;
static Camera camera;
int Engine_Init(char *filename)
// PRIVATE PROTOTYPES
static void CheckInputs(uint32_t input);
// PUBLIC FUNCTIONS
int Engine_Init(Engine_Buffer &buffer, char *filename)
{
int result = LoadMesh(filename, mesh);
@@ -15,14 +26,124 @@ int Engine_Init(char *filename)
return -1;
}
mesh.position.z = 250;
camera.SetFOV(90.0f, buffer.width, buffer.height);
return 0;
}
void Engine_Render(Engine_Buffer &buffer, uint32_t input)
{
CheckInputs(input);
Matrix tTranslate = Transform_Translate(
mesh.position.x, mesh.position.y, mesh.position.z);
Matrix tRotate = Transform_Rotate(
mesh.rotation[0], mesh.rotation[1], mesh.rotation[2]);
Matrix tScale = Transform_Scale(
mesh.scale);
Matrix tView = Transform_View(
camera.position, camera.rotation);
Matrix tPersp = Transform_Perspective(
camera.xZoom, camera.yZoom, camera.nearClip, camera.farClip);
for (size_t v = 0; v < mesh.verts.size(); ++v)
{
mesh.vertsTransformed[v] =
mesh.verts[v] * tScale * tRotate * tTranslate * tView * tPersp;
mesh.vertsTransformed[v] =
mesh.vertsTransformed[v] / mesh.vertsTransformed[v].w;
}
Matrix tScreen = Transform_Screen(camera.xScale, camera.yScale);
for (size_t v = 0; v < mesh.verts.size(); ++v)
{
mesh.vertsTransformed[v] = mesh.vertsTransformed[v] * tScreen;
}
for(size_t f = 0; f < mesh.faces.size(); ++f)
{
unsigned int v0 = mesh.faces[f].vertIndex[0];
unsigned int v1 = mesh.faces[f].vertIndex[1];
unsigned int v2 = mesh.faces[f].vertIndex[2];
}
}
void Engine_Shutdown(void)
{
}
// PRIVATE FUNCTIONS
static void CheckInputs(uint32_t input)
{
if (CHECK_BIT(input, TRANSLATE_X_POS))
{
mesh.position.x += 10;
}
else if (CHECK_BIT(input, TRANSLATE_X_NEG))
{
mesh.position.x -= 10;
}
if (CHECK_BIT(input, TRANSLATE_Z_POS))
{
mesh.position.z += 10;
}
else if (CHECK_BIT(input, TRANSLATE_Z_NEG))
{
mesh.position.z -= 10;
}
if (CHECK_BIT(input, TRANSLATE_Y_POS))
{
mesh.position.y += 10;
}
else if (CHECK_BIT(input, TRANSLATE_Y_NEG))
{
mesh.position.y -= 10;
}
if (CHECK_BIT(input, ROTATE_X_POS))
{
mesh.rotation[0] += .10;
}
else if (CHECK_BIT(input, ROTATE_X_NEG))
{
mesh.rotation[0] -= .10;
}
if (CHECK_BIT(input, ROTATE_Z_POS))
{
mesh.rotation[1] += .10;
}
else if (CHECK_BIT(input, ROTATE_Z_NEG))
{
mesh.rotation[1] -= .10;
}
if (CHECK_BIT(input, ROTATE_Y_POS))
{
mesh.rotation[2] += .10;
}
else if (CHECK_BIT(input, ROTATE_Y_NEG))
{
mesh.rotation[2] -= .10;
}
if (CHECK_BIT(input, SCALE_UP))
{
mesh.scale += 0.1f;
}
else if (CHECK_BIT(input, SCALE_DOWN))
{
mesh.scale -= 0.1f;
}
}
+2
View File
@@ -67,6 +67,8 @@ int LoadMesh(char *filename, Mesh &mesh)
printf("Verts: %lu\n", mesh.verts.size());
printf("Faces: %lu\n", mesh.faces.size());
mesh.vertsTransformed.resize(mesh.verts.size());
fclose(fp);
return 0;
+3 -3
View File
@@ -6,8 +6,8 @@
// CONSTANTS
const unsigned int WINDOW_WIDTH = 800;
const unsigned int WINDOW_HEIGHT = 600;
const unsigned int WINDOW_WIDTH = 1920;
const unsigned int WINDOW_HEIGHT = 1080;
const unsigned int WINDOW_FPS = 30;
@@ -33,7 +33,7 @@ int main(int argc, char *argv[])
buffer.width = platform.surface->w;
buffer.height = platform.surface->h;
result = Engine_Init(filename);
result = Engine_Init(buffer, filename);
if (result < 0)
{
+104
View File
@@ -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;
}