From 6477319da461ad78a120a3c777228eaa8194c9f8 Mon Sep 17 00:00:00 2001 From: rodri Date: Mon, 29 Jun 2020 18:28:02 +0000 Subject: bring flat triangle rasterization back, with some goodies for future shading work. add uv coordinates to the vertex. --- camera.c | 2 +- graphics.h | 14 ++++++++++++ mkfile | 1 + triangle.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 triangle.c diff --git a/camera.c b/camera.c index ca567c8..1d67868 100644 --- a/camera.c +++ b/camera.c @@ -15,7 +15,7 @@ verifycfg(Camera *c) { assert(c->viewport != nil); if(c->ptype == Ppersp) - assert(c->fov > 0 && c->fov < 360); + assert(c->fov > 0 && c->fov < 360*DEG); assert(c->clip.n > 0 && c->clip.n < c->clip.f); } diff --git a/graphics.h b/graphics.h index 44f213d..8e63b67 100644 --- a/graphics.h +++ b/graphics.h @@ -8,6 +8,7 @@ typedef struct Vertex Vertex; typedef struct Framebuffer Framebuffer; typedef struct Viewport Viewport; typedef struct Camera Camera; +typedef struct Triangle Triangle; struct Color { @@ -19,6 +20,7 @@ struct Vertex Point3 p; /* position */ Point3 n; /* surface normal */ Color c; /* shading color */ + Point2 uv; /* texture coordinate */ }; struct Framebuffer @@ -47,6 +49,11 @@ struct Camera Projection ptype; }; +struct Triangle +{ + Point p0, p1, p2; +}; + /* Camera */ void configcamera(Camera*, Image*, double, double, double, Projection); void placecamera(Camera*, Point3, Point3, Point3); @@ -64,5 +71,12 @@ Point toviewport(Camera*, Point3); Point2 fromviewport(Camera*, Point); void perspective(Matrix3, double, double, double, double); void orthographic(Matrix3, double, double, double, double, double, double); +/* temporary debug helpers */ void line3(Camera*, Point3, Point3, int, int, Image*); Point string3(Camera*, Point3, Image*, Font*, char*); + +/* triangle */ +Triangle Trian(int, int, int, int, int, int); +Triangle Trianpt(Point, Point, Point); +void triangle(Image*, Triangle, int, Image*, Point); +void filltriangle(Image*, Triangle, Image*, Point); diff --git a/mkfile b/mkfile index e3defa5..7911d47 100644 --- a/mkfile +++ b/mkfile @@ -4,6 +4,7 @@ LIB=libgraphics.a$O OFILES=\ camera.$O\ render.$O\ + triangle.$O\ HFILES=graphics.h ../libgeometry/geometry.h diff --git a/triangle.c b/triangle.c new file mode 100644 index 0000000..843ff6a --- /dev/null +++ b/triangle.c @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include + +/* + * comparison of a point p with an edge [e0 e1] + * p to the right: + + * p to the left: - + * p on the edge: 0 + */ +static int +edgeptcmp(Point e0, Point e1, Point p) +{ + Point3 e0p, e01, r; + + p = subpt(p, e0); + e1 = subpt(e1, e0); + e0p = Vec3(p.x,p.y,0); + e01 = Vec3(e1.x,e1.y,0); + r = crossvec3(e0p, e01); + + /* clamp to avoid overflow */ + return fclamp(r.z, -1, 1); /* e0.x*e1.y - e0.y*e1.x */ +} + +Triangle +Trian(int x0, int y0, int x1, int y1, int x2, int y2) +{ + return (Triangle){Pt(x0, y0), Pt(x1, y1), Pt(x2, y2)}; +} + +Triangle +Trianpt(Point p0, Point p1, Point p2) +{ + return (Triangle){p0, p1, p2}; +}; + +void +triangle(Image *dst, Triangle t, int thick, Image *src, Point sp) +{ + Point pl[4]; + + pl[0] = t.p0; + pl[1] = t.p1; + pl[2] = t.p2; + pl[3] = pl[0]; + + poly(dst, pl, nelem(pl), 0, 0, thick, src, sp); +} + +void +filltriangle(Image *dst, Triangle t, Image *src, Point sp) +{ + Point pl[3]; + + pl[0] = t.p0; + pl[1] = t.p1; + pl[2] = t.p2; + + fillpoly(dst, pl, nelem(pl), 0, src, sp); +} + +int +ptintriangle(Point p, Triangle t) +{ + /* counter-clockwise check */ + return edgeptcmp(t.p0, t.p2, p) <= 0 && + edgeptcmp(t.p2, t.p1, p) <= 0 && + edgeptcmp(t.p1, t.p0, p) <= 0; +} -- cgit v1.2.3