1
0
Fork 0

Modify lighting equations

This commit is contained in:
Austin Morlan 2018-09-20 20:06:27 -07:00
parent 6dcbb35e86
commit 0203beb367
Signed by: austin
GPG Key ID: FD6B27654AF5E348
5 changed files with 28 additions and 87 deletions

View File

@ -31,7 +31,7 @@ struct Material
ColorF32 ambient;
ColorF32 diffuse;
ColorF32 specular;
float specularExp;
float glossiness;
float opacity;
};

View File

@ -19,7 +19,7 @@ struct Light
// PUBLIC FUNCTIONS
ColorF32 ComputeLight(Vertex &vert, Material &material, Light &light, Camera &camera);
ColorF32 ComputeLight(Point &position, Vector &normal, Material &material, Light &light, Camera &camera);
#define LIGHT_H

View File

@ -70,7 +70,7 @@ int EngineInit(char *objFilename, char *mtlFilename)
// Light configuration
light.position = Point(-100.0f, 200.0f, 0.0f);
light.position = Point(-300.0f, 200.0f, 0.0f);
light.color = {1.0f, 1.0f, 1.0f, 1.0f};
light.intensity = 2.0f;
light.falloffConstant = 1.0f;
@ -338,7 +338,7 @@ static void LightMesh(void)
{
Vertex &vert = verts.data[face.vertIndex[i]];
vert.color = ComputeLight(vert, material, light, camera);
vert.color = ComputeLight(vert.position, vert.normal, material, light, camera);
}
}
}

View File

@ -3,103 +3,44 @@
#include "light.h"
// PRIVATE PROTOTYPES
static ColorF32 ComputeAmbientLight(
Material &material, ColorF32 &intensity);
static ColorF32 ComputeDiffuseLight(
Material &material, ColorF32 &intensity, Vertex &vert, Light &light);
static ColorF32 ComputeSpecularLight(
Material &material, ColorF32 &intensity, Vertex &vert, Light &light,
Camera &camera);
// PUBLIC FUNCTIONS
ColorF32 ComputeLight(
Vertex &vert, Material &material, Light &light, Camera &camera)
Point &position, Vector &normal, Material &material, Light &light, Camera &camera)
{
// Distance from light to vertex
Vector direction = light.position - vert.position;
// Point light intensity is a function of the falloff factors and the distance
Vector direction = light.position - position;
VectorNormalize(direction);
float distance = VectorLength(direction);
// Point light intensity is a function of the falloff factors and the distance
ColorF32 intensity =
(light.intensity * light.color)
/ (light.falloffConstant + light.falloffLinear * distance);
/ (light.falloffConstant + (light.falloffLinear * distance));
// Compute ambient light
ColorF32 ambientLight = ComputeAmbientLight(material, intensity);
// Diffuse Light = Kr * I * (alpha + (1 - alpha) * n.d)
float dotNormalDirection = MAX(VectorDot(normal, direction), 0.0f);
float alpha = 0.2f;
ColorF32 diffuseLight =
material.diffuse
* intensity
* (alpha + (1.0f - alpha) * dotNormalDirection);
// Compute diffuse light
ColorF32 diffuseLight = ComputeDiffuseLight(material, intensity, vert, light);
// Specular Light = Ks * I * (r.v)^sp
Vector view = camera.position - position;
VectorNormalize(view);
Vector reflection = (2.0f * dotNormalDirection * normal) - direction;
float dotReflectionView = MAX(VectorDot(reflection, view), 0.0f);
// Compute specular light
ColorF32 specularLight = ComputeSpecularLight(material, intensity, vert, light, camera);
ColorF32 specularLight =
material.specular
* intensity
* powf(dotReflectionView, material.glossiness);
// Total light is sum of all lights
ColorF32 result = ambientLight + diffuseLight + specularLight;
return result;
}
// PRIVATE FUNCTIONS
static ColorF32 ComputeAmbientLight(
Material &material, ColorF32 &intensity)
{
// light = Kd * I
ColorF32 result = material.ambient * intensity;
return result;
}
static ColorF32 ComputeDiffuseLight(
Material &material, ColorF32 &intensity, Vertex &vert, Light &light)
{
// Vector from the light to the vertex
Vector direction = light.position - vert.position;
VectorNormalize(direction);
float normalDotDirection = VectorDot(vert.normal, direction);
// light = Kr * I * n.d
ColorF32 result = material.diffuse * intensity * normalDotDirection;
return result;
}
static ColorF32 ComputeSpecularLight(
Material &material, ColorF32 &intensity, Vertex &vert, Light &light,
Camera &camera)
{
// Vector from the camera to the vertex
Vector view = camera.position - vert.position;
VectorNormalize(view);
// Vector from the light to the vertex
Vector direction = light.position - vert.position;
VectorNormalize(direction);
// Reflection vector of the light vector across the vertex normal
float normalDotDirection = VectorDot(vert.normal, direction);
Vector reflection = (2.0f * normalDotDirection * vert.normal) - direction;
float reflectDotView = MAX(VectorDot(reflection, view), 0.0f);
// light = Ks * I * (r.v)^sp
ColorF32 result =
material.specular * intensity * powf(reflectDotView, material.specularExp);
ColorF32 result = diffuseLight + specularLight;
return result;

View File

@ -157,7 +157,7 @@ int ParseMTL(char *filename, EngineMemory &memory)
else if (strcmp(type, "Ns") == 0)
{
sscanf( data, "%f",
&materials.data[materialIndex].specularExp);
&materials.data[materialIndex].glossiness);
}
else if (strcmp(type, "Ka") == 0)