diff --git a/polygon_draw.hh b/polygon_draw.hh index 5bd02b5..40190c9 100644 --- a/polygon_draw.hh +++ b/polygon_draw.hh @@ -34,10 +34,10 @@ void DrawPolygon(auto&& points, auto&& Plot) requires std::ranges::input_range=Yindex); // Index of X within left[] and right[] - auto leftx = left[Xprop].get(); - auto endx = right[Xprop].get(); + auto leftx = (int)left[Xprop].get(); + auto endx = (int)right[Xprop].get(); auto num_steps = endx - leftx; - auto rounding_error = int(leftx)-leftx; + auto rounding_error = 0;//int(leftx)-leftx; // For each prop except X coordinate, create slopes between left and right edge auto props = [&](std::index_sequence, auto&& GetSlope) diff --git a/renderer.cc b/renderer.cc index 49f3769..19c2af6 100644 --- a/renderer.cc +++ b/renderer.cc @@ -391,3 +391,22 @@ int main() // Assemble directional vectors: The normal, and four tangent vectors, all at 90° angles to the previous. std::array polyvectors { poly.normal, poly.tangent, poly.bitangent, poly.tangent*-1, poly.bitangent*-1 }; + + std::span span(&vertices[poly.first], poly.size); + // Create a matrix that translates l&u coordinates into xyz coordinates + // solve([x0 = u0*a + v0*b + c, x1 = u1*a + v1*b + c, x2 = u2*a + v2*b + c], [a,b,c]) + // a = (v0*(x2-x1) + v1*(x0-x2) + v2*(x1-x0)) / det + // b = (u0*(x2-x1) + u1*(x0-x2) + u2*(x1-x0)) / det + // c = (u0*(v2*x1 - v1*x2) + u1*(v0*x2 - v2*x0) + u2*(v1*x0 - v0*x1)) / det + + auto U = AsArray(std::get<5>(span[0]), std::get<5>(span[1]), std::get<5>(span[2])); + auto V = AsArray(std::get<6>(span[0]), std::get<6>(span[1]), std::get<6>(span[2])); + auto V201 = AsArray(V[2],V[0],V[1]), V120 = AsArray(V[1],V[2],V[0]); + auto Solve = [U,V,V201,V120, invdet = 1.f / Dot(U, V201-V120)](auto a,auto b,auto c) + { + auto cab = AsArray(c,a,b), bca = AsArray(b,c,a), d = cab - bca; + return AsArray(-Dot(V, d), Dot(U, d), Dot(U, V201*bca - V120*cab)) * invdet; + }; + auto a = Solve(std::get<0>(span[0]), std::get<0>(span[1]), std::get<0>(span[2])); + auto b = Solve(std::get<1>(span[0]), std::get<1>(span[1]), std::get<1>(span[2])); + auto c = Solve(std::get<2>(span[0]), std::get<2>(span[1]), std::get<2>(span[2])); @@ -401,5 +421,7 @@ int main() // Camera location for viewing "up" from this point at the surface - auto l = AsArray(x,y,z) + (poly.normal + poly.tangent + poly.bitangent) * .02f; + // Place the camera on the center of the luxel + auto luxpos = AsArray(lu+half, lv+half, 1); + auto l = AsArray(Dot(a, luxpos), Dot(b, luxpos), Dot(c, luxpos)); // Render the five camera views, and calculate the average of all rendered pixels. std::array color{};