Add flat shading with ambient and diffuse light

This commit is contained in:
austin committed 2020-07-01 17:42:47 -07:00
1 parent f642b0ec02
commit 63e8bd1b93
8 files changed
+308 -18

No files matched your search

+63 -13
View File
@@ -2,16 +2,19 @@
#include "color.h"
#include "engine.h"
#include "geometry.h"
#include "light.h"
#include "loader.h"
#include "matrix.h"
#include "transform.h"
#include "util.h"
#include "vec.h"
#include <cstdio>
// GLOBALS
static Mesh mesh;
static Camera camera;
static LightList lights;
// PRIVATE PROTOTYPES
@@ -29,9 +32,17 @@ int Engine_Init(Engine_Buffer &buffer, char *filename)
}
mesh.position.z = 250;
mesh.material.kDiffuse = {1.0,1.0,1.0,1.0};
mesh.material.kAmbient = {1.0,1.0,1.0,1.0};
camera.SetFOV(90.0f, buffer.width, buffer.height);
lights.diffuse = (LightDiffuse*)malloc(sizeof(LightDiffuse));
lights.diffuseCount = 1;
lights.ambient.intensity = 1.0;
lights.diffuse[0].intensity = 0.5;
lights.diffuse[0].direction = Vector(1,1,1);
return 0;
}
@@ -39,30 +50,69 @@ void Engine_Render(Engine_Buffer &buffer, uint32_t input)
{
CheckInputs(input);
// Local space to world space
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 tScale = Transform_Scale(mesh.scale);
Matrix tView = Transform_View(
camera.position, camera.rotation);
for (size_t v = 0; v < mesh.verts.size(); ++v)
{
mesh.vertsTransformed[v] =
mesh.verts[v] * tScale * tRotate * tTranslate;
}
// Cull backfaces before computing colors
mesh.CullBackfaces(camera.position);
// Color the faces (flat shading)
for (size_t f = 0; f < mesh.culledFaces.size(); ++f)
{
unsigned int v0 = mesh.culledFaces[f].vertIndex[0];
unsigned int v1 = mesh.culledFaces[f].vertIndex[1];
unsigned int v2 = mesh.culledFaces[f].vertIndex[2];
Vector v01 = mesh.vertsTransformed[v1] - mesh.vertsTransformed[v0];
Vector v02 = mesh.vertsTransformed[v2] - mesh.vertsTransformed[v0];
Vector normal = Vector::Cross(v01, v02);
normal.Normalize();
ColorF32 totalColor = lights.ambient.ComputeColor(mesh.material.kAmbient);
for (int c = 0; c < lights.diffuseCount; ++c)
{
totalColor += lights.diffuse[c].ComputeColor(
mesh.material.kDiffuse, normal);
}
mesh.faces[f].color = ColorF32::ConvertToU32(totalColor);
}
// World space to camera (view) space
Matrix tView = Transform_View(camera.position, camera.rotation);
// Camera space to perspective
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] * tView * tPersp;
mesh.vertsTransformed[v] =
mesh.vertsTransformed[v] / mesh.vertsTransformed[v].w;
}
// Perspective to screen
Matrix tScreen = Transform_Screen(camera.xScale, camera.yScale);
for (size_t v = 0; v < mesh.verts.size(); ++v)
@@ -70,16 +120,16 @@ void Engine_Render(Engine_Buffer &buffer, uint32_t input)
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];
ColorU32 white = {0xff,0xff,0xff,0xff};
// Fill each face with its respective color
for(size_t f = 0; f < mesh.culledFaces.size(); ++f)
{
unsigned int v0 = mesh.culledFaces[f].vertIndex[0];
unsigned int v1 = mesh.culledFaces[f].vertIndex[1];
unsigned int v2 = mesh.culledFaces[f].vertIndex[2];
FillTriangle(
buffer.pixels, buffer.width, white.u32,
buffer.pixels, buffer.width, mesh.faces[f].color.u32,
mesh.vertsTransformed[v0].x, mesh.vertsTransformed[v0].y,
mesh.vertsTransformed[v1].x, mesh.vertsTransformed[v1].y,
mesh.vertsTransformed[v2].x, mesh.vertsTransformed[v2].y);