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

@@ -58,6 +58,14 @@ struct polygon {
small = true;*/ small = true;*/
} }
vec3 avgNormal() {
vec3 result;
for (int i = 0; i++; i < 3) {
result += normals[i];
}
return result * decimal(0.3333);
}
const bool contains(const vec3 &p) { const bool contains(const vec3 &p) {
// if (skip) // if (skip)
// return false; // return false;
@@ -82,6 +90,9 @@ struct polygon {
vec3 result = normals[0] * barycentrics[0] + vec3 result = normals[0] * barycentrics[0] +
normals[1] * barycentrics[1] + normals[1] * barycentrics[1] +
normals[2] * barycentrics[2]; normals[2] * barycentrics[2];
if (result.isSmall())
return normals[0];
else
return result.normalize(); return result.normalize();
} }
@@ -97,8 +108,8 @@ struct polygon {
} }
vec3 calcBarycentric(vec3 s) { vec3 calcBarycentric(vec3 s) {
if (small) // if (small)
return vec3(decimal(0.333), decimal(0.333), decimal(0.333)); // return vec3(decimal(0.333), decimal(0.333), decimal(0.333));
vec3 result; vec3 result;
result[0] = (points[1].x() - s.x()) * (points[2].y() - s.y()) - result[0] = (points[1].x() - s.x()) * (points[2].y() - s.y()) -
(points[2].x() - s.x()) * (points[1].y() - s.y()); (points[2].x() - s.x()) * (points[1].y() - s.y());
@@ -106,7 +117,9 @@ struct polygon {
(points[0].x() - s.x()) * (points[2].y() - s.y()); (points[0].x() - s.x()) * (points[2].y() - s.y());
result = result * baryFactor; result = result * baryFactor;
result[2] = decimal(1.0) - result[1] - result[0]; result[2] = decimal(1.0) - result[1] - result[0];
return result; // return result;
return vec3::max(vec3::min(result, vec3(1.0, 1.0, 1.0)),
vec3(0.0, 0.0, 0.0));
} }
}; };

View File

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