From 3857a7ad606f4f050411d27fc94116731fddb1b3 Mon Sep 17 00:00:00 2001 From: rodri Date: Tue, 28 Apr 2020 12:18:55 +0000 Subject: transcribe macros into actual functions. remove unused memdraw code. --- graphics.h | 40 +++++++++++++++++++++++++++++++++------- render.c | 52 ++++++++++++++++++++++------------------------------ 2 files changed, 55 insertions(+), 37 deletions(-) diff --git a/graphics.h b/graphics.h index b9582f3..44f213d 100644 --- a/graphics.h +++ b/graphics.h @@ -3,20 +3,45 @@ typedef enum { Ppersp /* perspective */ } Projection; +typedef struct Color Color; typedef struct Vertex Vertex; +typedef struct Framebuffer Framebuffer; +typedef struct Viewport Viewport; typedef struct Camera Camera; -struct Vertex { +struct Color +{ + double r, g, b; +}; + +struct Vertex +{ Point3 p; /* position */ Point3 n; /* surface normal */ + Color c; /* shading color */ +}; + +struct Framebuffer +{ + Rectangle r; /* frame geometry */ + int bpp; /* bytes per pixel */ + uchar *color; /* pixel color buffer */ + float *depth; /* pixel depth buffer */ +}; + +struct Viewport +{ + RFrame; + Framebuffer; }; -struct Camera { +struct Camera +{ RFrame3; /* VCS */ Image *viewport; double fov; /* vertical FOV */ struct { - double n, f; + double n, f; /* near and far clipping planes */ } clip; Matrix3 proj; /* VCS to NDC xform */ Projection ptype; @@ -29,10 +54,11 @@ void aimcamera(Camera*, Point3); void reloadcamera(Camera*); /* rendering */ -#define FPS2MS(n) (1000/(n)) -#define WORLD2VCS(cp, p) (rframexform3((p), *(cp))) -#define VCS2NDC(cp, p) (xform3((p), (cp)->proj)) -#define WORLD2NDC(cp, p) (VCS2NDC((cp), WORLD2VCS((cp), (p)))) +#define FPS (60) /* frame rate */ +#define MS2FR (1e3/FPS) /* ms per frame */ +Point3 world2vcs(Camera*, Point3); +Point3 vcs2ndc(Camera*, Point3); +Point3 world2ndc(Camera*, Point3); int isclipping(Point3); Point toviewport(Camera*, Point3); Point2 fromviewport(Camera*, Point); diff --git a/render.c b/render.c index d8b7088..3a121d0 100644 --- a/render.c +++ b/render.c @@ -1,35 +1,9 @@ #include #include #include -#include #include #include -//static Memimage* -//imagetomemimage(Image *src) -//{ -// Memimage *dst; -// uchar *buf; -// uint buflen; -// -// buflen = Dx(src->r)*Dy(src->r); -// buflen *= (chantodepth(src->chan)+7)/8; -// buf = malloc(buflen); -// if(buf == nil) -// sysfatal("malloc: %r"); -// dst = allocmemimage(src->r, src->chan); -// if(dst == nil) -// sysfatal("allocmemimage: %r"); -// if(src->repl){ -// dst->flags |= Frepl; -// dst->clipr = Rect(-1e6, -1e6, 1e6, 1e6); -// } -// unloadimage(src, src->r, buf, buflen); -// loadmemimage(dst, src->r, buf, buflen); -// free(buf); -// return dst; -//} - static Point2 flatten(Camera *c, Point3 p) { @@ -52,6 +26,24 @@ flatten(Camera *c, Point3 p) return p2; } +Point3 +world2vcs(Camera *c, Point3 p) +{ + return rframexform3(p, *c); +} + +Point3 +vcs2ndc(Camera *c, Point3 p) +{ + return xform3(p, c->proj); +} + +Point3 +world2ndc(Camera *c, Point3 p) +{ + return vcs2ndc(c, world2vcs(c, p)); +} + /* requires p to be in NDC */ int isclipping(Point3 p) @@ -94,7 +86,7 @@ perspective(Matrix3 m, double fov, double a, double n, double f) { double cotan; - cotan = 1/tan(fov/2*DEG); + cotan = 1/tan(fov/2); identity3(m); m[0][0] = cotan/a; m[1][1] = cotan; @@ -118,8 +110,8 @@ orthographic(Matrix3 m, double l, double r, double b, double t, double n, double void line3(Camera *c, Point3 p0, Point3 p1, int end0, int end1, Image *src) { - p0 = WORLD2NDC(c, p0); - p1 = WORLD2NDC(c, p1); + p0 = world2ndc(c, p0); + p1 = world2ndc(c, p1); if(isclipping(p0) || isclipping(p1)) return; line(c->viewport, toviewport(c, p0), toviewport(c, p1), end0, end1, 0, src, ZP); @@ -128,7 +120,7 @@ line3(Camera *c, Point3 p0, Point3 p1, int end0, int end1, Image *src) Point string3(Camera *c, Point3 p, Image *src, Font *f, char *s) { - p = WORLD2NDC(c, p); + p = world2ndc(c, p); if(isclipping(p)) return Pt(-1,-1); return string(c->viewport, toviewport(c, p), src, ZP, f, s); -- cgit v1.2.3