diff --git a/polygon.hpp b/polygon.hpp index b4d0951..f13876e 100644 --- a/polygon.hpp +++ b/polygon.hpp @@ -58,6 +58,14 @@ struct polygon { 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) { // if (skip) // return false; @@ -82,7 +90,10 @@ struct polygon { vec3 result = normals[0] * barycentrics[0] + normals[1] * barycentrics[1] + normals[2] * barycentrics[2]; - return result.normalize(); + if (result.isSmall()) + return normals[0]; + else + return result.normalize(); } vec3 calcColor(vec3 barycentrics) { @@ -97,8 +108,8 @@ struct polygon { } vec3 calcBarycentric(vec3 s) { - if (small) - return vec3(decimal(0.333), decimal(0.333), decimal(0.333)); + // if (small) + // return vec3(decimal(0.333), decimal(0.333), decimal(0.333)); vec3 result; result[0] = (points[1].x() - s.x()) * (points[2].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()); result = result * baryFactor; 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)); } }; diff --git a/renderer.hpp b/renderer.hpp index aed4255..1a30e73 100644 --- a/renderer.hpp +++ b/renderer.hpp @@ -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);