Modify lighting to use a point light

This commit is contained in:
austin committed 2020-07-01 17:43:00 -07:00
1 parent e29ad8b418
commit b3991a4651
7 files changed
+183 -81

No files matched your search

+24 -24
View File
@@ -15,7 +15,7 @@
// GLOBALS
static Mesh mesh;
static Camera camera;
static LightList lights;
static Light light;
static VertexList localVerts;
static VertexList transVerts;
static FaceList localFaces;
@@ -33,7 +33,7 @@ static void ComputeNormals(void);
static void ClearDepthBuffer(Engine_Buffer &buffer);
static void TransformToClipSpace(void);
static void TransformToScreenSpace(void);
static void ComputeLighting(void);
static void LightMesh(void);
// PUBLIC FUNCTIONS
@@ -68,15 +68,17 @@ int Engine_Init(char *objFilename, char *mtlFilename)
// Mesh configuration
mesh.position.z = 50;
mesh.position.y = -50;
mesh.position.z = 125;
mesh.position.y = -125;
mesh.scale = 1.0f;
// Light configuration
lights.ambient.intensity = 1.0f;
lights.diffuse.intensity = 1.0f;
lights.diffuse.direction = Vector(1.0f, 1.0f, 1.0f);
light.position = Point(-100.0f, 200.0f, 0.0f);
light.color = {1.0f, 1.0f, 1.0f, 1.0f};
light.intensity = 2.0f;
light.falloffConstant = 1.0f;
light.falloffLinear = 0.001f;
// Transformation matrices that do not change
@@ -106,7 +108,7 @@ void Engine_Render(Engine_Buffer &buffer, uint32_t input)
// Light vertices and/or faces
ComputeLighting();
LightMesh();
// Transform vertices to screen space
@@ -127,29 +129,29 @@ static void CheckInputs(uint32_t input)
{
if (CHECK_BIT(input, TRANSLATE_X_POS))
{
mesh.position.x += 10;
light.position.x += 10;
}
else if (CHECK_BIT(input, TRANSLATE_X_NEG))
{
mesh.position.x -= 10;
light.position.x -= 10;
}
if (CHECK_BIT(input, TRANSLATE_Z_POS))
{
mesh.position.z += 10;
light.position.z += 10;
}
else if (CHECK_BIT(input, TRANSLATE_Z_NEG))
{
mesh.position.z -= 10;
light.position.z -= 10;
}
if (CHECK_BIT(input, TRANSLATE_Y_POS))
{
mesh.position.y += 10;
light.position.y += 10;
}
else if (CHECK_BIT(input, TRANSLATE_Y_NEG))
{
mesh.position.y -= 10;
light.position.y -= 10;
}
if (CHECK_BIT(input, ROTATE_X_POS))
@@ -181,11 +183,15 @@ static void CheckInputs(uint32_t input)
if (CHECK_BIT(input, SCALE_UP))
{
mesh.scale += 0.1f;
light.color.b = 0.0f;
light.color.g = 0.0f;
light.color.r = 1.0f;
}
else if (CHECK_BIT(input, SCALE_DOWN))
{
mesh.scale -= 0.1f;
light.color.b = 1.0f;
light.color.g = 1.0f;
light.color.r = 1.0f;
}
}
@@ -265,25 +271,19 @@ static void TransformToScreenSpace(void)
}
}
static void ComputeLighting(void)
static void LightMesh(void)
{
for (size_t f = 0; f < transFaces.size; ++f)
{
Face &face = transFaces.data[f];
Material &material = materials.data[face.materialIndex];
// TODO: Fix weird lighting
material.ambient = {1.0,1.0,1.0,1.0};
material.diffuse = {0.5,0.5,0.5,0.5};
// Gouraud shading
for (int i = 0; i < 3; ++i)
{
Vertex &vert = transVerts.data[face.vertIndex[i]];
vert.color =
lights.ambient.ComputeColor(material.ambient)
+ lights.diffuse.ComputeColor(material.diffuse, vert.normal);
vert.color = ComputeLight(vert, material, light);
}
}
}
+6 -1
View File
@@ -187,11 +187,16 @@ void RenderMesh(
+ (barycenter[1] * c1)
+ (barycenter[2] * c2);
// Ensure no color channel exceeds 1.0
ScaleColor(shading);
// Light the texture
color.b *= shading.b;
color.g *= shading.g;
color.r *= shading.r;
// Interpolate 1/z for the z-buffer
float zInv =
1.0f /
+78
View File
@@ -0,0 +1,78 @@
#include "color.h"
#include "light.h"
// PRIVATE PROTOTYPES
static ColorF32 ComputeAmbientLight(
ColorF32 surfaceReflectivity, ColorF32 lightIntensity);
static ColorF32 ComputeDiffuseLight(
ColorF32 surfaceReflectivity, ColorF32 lightIntensity,
Point &vertPosition, Vector &vertNormal,
Point &lightPosition);
// PUBLIC FUNCTIONS
ColorF32 ComputeLight(Vertex &vert, Material &material, Light &light)
{
// Distance from light to vertex
Vector d = light.position - vert.point;
float distance = d.Length();
// Intensity is a function of the falloff factors and the distance
ColorF32 intensity =
(light.intensity * light.color)
/ (light.falloffConstant + light.falloffLinear * distance);
// Compute ambient light
ColorF32 ambientLight = ComputeAmbientLight(
material.ambient, intensity);
// Compute diffuse light
ColorF32 diffuseLight = ComputeDiffuseLight(
material.diffuse, intensity,
vert.point, vert.normal,
light.position);
// Total light is sum of all lights
ColorF32 result = ambientLight + diffuseLight;
return result;
}
// PRIVATE FUNCTIONS
static ColorF32 ComputeAmbientLight(
ColorF32 surfaceReflectivity, ColorF32 lightIntensity)
{
ColorF32 result;
result = surfaceReflectivity * lightIntensity;
return result;
}
static ColorF32 ComputeDiffuseLight(
ColorF32 surfaceReflectivity, ColorF32 lightIntensity,
Point &vertPosition, Vector &vertNormal,
Point &lightPosition)
{
// Light is a function of the position of the light source and the surface
Vector direction = lightPosition - vertPosition;
direction.Normalize();
float dot = Vector::Dot(vertNormal, direction);
ColorF32 result = surfaceReflectivity * lightIntensity * dot;
return result;
}