feat: added lighting

This commit is contained in:
2025-12-20 21:07:16 +01:00
parent 7ab3825598
commit beacb9a985
2 changed files with 26 additions and 10 deletions

View File

@@ -18,6 +18,7 @@ class Renderer {
Rendertarget *target;
bool clearTarget = true;
vec3 sunDir = vec3(1.0, -1.0, 1.0).normalize();
void toScreenSpace(vec3 *p) {
p->x() = p->x() / p->z() * decimal(2.0) * decimal(SCREEN_SPACE_SIZE) +
@@ -56,6 +57,7 @@ class Renderer {
polygon testP;
for (int f = 0; f < model->faces.size(); f += 3) {
testP.small = false;
for (int p = 0; p < 3; p++) {
testP.points[p] = verts[std::get<0>(model->faces[f + p])];
testP.colors[p] =
@@ -63,6 +65,8 @@ class Renderer {
testP.normals[p] =
model->normals[std::get<1>(model->faces[f + p])];
}
if ((testP.avgNormal() * vec3(0.0, 0.0, 1.0)) > decimal(0.))
continue;
testP.calcDelta();
@@ -81,20 +85,19 @@ class Renderer {
for (int x = startX; x < endX; x++) {
for (int y = startY; y < endY; y++) {
if (testP.contains(pos)) {
if (testP.small)
continue;
vec3 factors = testP.calcBarycentric(pos);
factors = factors.normalize();
decimal depth = testP.calcDepth(factors);
if (depth < target->getDepth(x, y)) {
// std::cout << factors << std::endl;
vec3 normal = testP.calcNormal(factors);
vec3 color = testP.calcColor(factors);
decimal lightFac =
std::max(
normal *
(-vec3(1.0, -1.0, 1.0).normalize()),
decimal(0.0)) +
std::max(normal * (-sunDir), decimal(0.0)) +
decimal(0.5);
target->setDepth(x, y, depth);
// decimal reflection = target->setDepth(x, y,
// depth);
target->set(x, y,
(color * decimal(120.0)) * lightFac);