public static IVertex lerp(IVertex start, IVertex end, float delta) { return new IVertex() { position = Int3.lerp(start.position, end.position, delta), normal = Float3.lerp(start.normal, end.normal, delta), color = start.color.lerpTo(end.color, delta), uv = Float2.lerp(start.uv, end.uv, delta), }; }
private void RenderTriangle2(Vertex _v1, Vertex _v2, Vertex _v3, Material material, Float3 lightDirection) { IVertex v1 = new IVertex(_v1); IVertex v2 = new IVertex(_v2); IVertex v3 = new IVertex(_v3); if (v1.y > v2.y) Utils.swap(ref v1, ref v2); if (v1.y > v3.y) Utils.swap(ref v1, ref v3); if (v2.y > v3.y) Utils.swap(ref v2, ref v3); int triangleYHeight = v3.y - v1.y + 1; int firstSegmentHeight = v2.y - v1.y + 1; int secondSegmentHeight = v3.y - v2.y + 1; for (int y = v1.y; y <= v3.y; y++) { bool isFirstSegment = y < v2.y; int segmentStartY = isFirstSegment ? v1.y : v2.y; float alpha = (float)(y - v1.y) / (float)triangleYHeight; IVertex A = IVertex.lerp(v1, v3, alpha); float beta = (float)(y - segmentStartY) / (float)(isFirstSegment ? firstSegmentHeight : secondSegmentHeight); IVertex B = IVertex.lerp(isFirstSegment ? v1 : v2, isFirstSegment ? v2 : v3, beta); if (A.x > B.x) Utils.swap(ref A, ref B); for (int x = A.x; x <= B.x; x++) { // check extremes float delta = (A.x == B.x) ? 1.0f : (float)(x - A.x) / (float)(B.x - A.x); IVertex C = IVertex.lerp(A, B, delta); float lc = getLamberComponent(C.normal, lightDirection); int intLambert = (int)(lc * 255); Color c = Color.FromArgb(intLambert, intLambert, intLambert); c = tex2D(material.diffuseTexture, C.u, C.v); DrawPointToFrameBuffer(x, y, C.z, c); } } }