shithub: libgraphics

Download patch

ref: 3857a7ad606f4f050411d27fc94116731fddb1b3
parent: 91d8cc8d9adfec7e9b5f415ebd7b7f740cde6e2d
author: rodri <rgl@antares-labs.eu>
date: Tue Apr 28 08:18:55 EDT 2020

transcribe macros into actual functions. remove unused memdraw code.

--- a/graphics.h
+++ b/graphics.h
@@ -3,20 +3,45 @@
 	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 Camera {
+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
+{
 	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 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);
--- 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 @@
 	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 @@
 {
 	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 @@
 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 @@
 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);