summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrodri <rgl@antares-labs.eu>2020-04-28 12:18:55 +0000
committerrodri <rgl@antares-labs.eu>2020-04-28 12:18:55 +0000
commit3857a7ad606f4f050411d27fc94116731fddb1b3 (patch)
tree16a3da7365dd7e1ba89f7baabdcad75793a7e947
parent91d8cc8d9adfec7e9b5f415ebd7b7f740cde6e2d (diff)
downloadlibgraphics-3857a7ad606f4f050411d27fc94116731fddb1b3.tar.gz
libgraphics-3857a7ad606f4f050411d27fc94116731fddb1b3.tar.bz2
libgraphics-3857a7ad606f4f050411d27fc94116731fddb1b3.zip
transcribe macros into actual functions. remove unused memdraw code.
-rw-r--r--graphics.h40
-rw-r--r--render.c52
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 <u.h>
#include <libc.h>
#include <draw.h>
-#include <memdraw.h>
#include <geometry.h>
#include <graphics.h>
-//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);