From 263444216730830b712a4f8f3a087513c5ac93e9 Mon Sep 17 00:00:00 2001 From: rodri Date: Fri, 6 Mar 2020 21:05:07 +0000 Subject: redesign of the camera abstraction. general unused/useless data cleaning. --- libgraphics/camera.c | 67 ++++++++++++++++++++++++++-------------------------- libgraphics/render.c | 39 ++++++++++++++++++++---------- 2 files changed, 61 insertions(+), 45 deletions(-) (limited to 'libgraphics') diff --git a/libgraphics/camera.c b/libgraphics/camera.c index c850477..bb55490 100644 --- a/libgraphics/camera.c +++ b/libgraphics/camera.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "../geometry.h" #include "../graphics.h" @@ -13,45 +14,45 @@ max(int a, int b) static void verifycfg(Camera *c) { - assert(c->viewport != nil); + assert(c->viewport.fb != nil); if(c->ptype == Ppersp) - assert(c->fov >= 1 && c->fov < 360); - assert(c->clip.n > 0 && c->clip.n < c->clip.f); + assert(c->fov > 0 && c->fov < 360); + assert(c->clipn > 0 && c->clipn < c->clipf); } -void -perspective(Matrix3 m, double fov, double a, double n, double f) +static void +updatefb(Camera *c, Rectangle r, ulong chan) { - double cotan; + Memimage *fb; - cotan = 1/tan(fov/2*DEG); - identity3(m); - m[0][0] = cotan/a; - m[1][1] = cotan; - m[2][2] = -(f+n)/(f-n); - m[2][3] = -2*f*n/(f-n); - m[3][2] = -1; + fb = allocmemimage(r, chan); + if(fb == nil) + sysfatal("allocmemimage: %r"); + c->viewport.fb = fb; + c->viewport.p = Pt2(r.min.x,r.max.y,1); } -void -orthographic(Matrix3 m, double l, double r, double b, double t, double n, double f) +Camera* +alloccamera(Rectangle r, ulong chan) { - identity3(m); - m[0][0] = 2/(r - l); - m[1][1] = 2/(t - b); - m[2][2] = -2/(f - n); - m[0][3] = -(r + l)/(r - l); - m[1][3] = -(t + b)/(t - b); - m[2][3] = -(f + n)/(f - n); + Camera *c; + + c = malloc(sizeof(Camera)); + if(c == nil) + sysfatal("malloc: %r"); + memset(c, 0, sizeof *c); + c->viewport.bx = Vec2(1,0); + c->viewport.by = Vec2(0,-1); + updatefb(c, r, chan); + c->updatefb = updatefb; } void -configcamera(Camera *c, Image *v, double fov, double n, double f, Projection p) +configcamera(Camera *c, double fov, double n, double f, Projection p) { - c->viewport = v; c->fov = fov; - c->clip.n = n; - c->clip.f = f; + c->clipn = n; + c->clipf = f; c->ptype = p; reloadcamera(c); } @@ -84,19 +85,19 @@ reloadcamera(Camera *c) switch(c->ptype){ case Portho: /* - r = Dx(c->viewport->r)/2; - t = Dy(c->viewport->r)/2; + r = Dx(c->viewport.fb->r)/2; + t = Dy(c->viewport.fb->r)/2; l = -r; b = -t; */ l = t = 0; - r = Dx(c->viewport->r); - b = Dy(c->viewport->r); - orthographic(c->proj, l, r, b, t, c->clip.n, c->clip.f); + r = Dx(c->viewport.fb->r); + b = Dy(c->viewport.fb->r); + orthographic(c->proj, l, r, b, t, c->clipn, c->clipf); break; case Ppersp: - a = (double)Dx(c->viewport->r)/Dy(c->viewport->r); - perspective(c->proj, c->fov, a, c->clip.n, c->clip.f); + a = (double)Dx(c->viewport.fb->r)/Dy(c->viewport.fb->r); + perspective(c->proj, c->fov, a, c->clipn, c->clipf); break; default: sysfatal("unknown projection type"); } diff --git a/libgraphics/render.c b/libgraphics/render.c index ddc0663..a1819ec 100644 --- a/libgraphics/render.c +++ b/libgraphics/render.c @@ -41,26 +41,41 @@ Point toviewport(Camera *c, Point3 p) { Point2 p2; - RFrame rf = { - c->viewport->r.min.x, c->viewport->r.max.y, 1, - 1, 0, 0, - 0, -1, 0 - }; - p2 = invrframexform(flatten(c, p), rf); + p2 = invrframexform(flatten(c, p), c->viewport); return (Point){p2.x, p2.y}; } Point2 fromviewport(Camera *c, Point p) { - RFrame rf = { - c->viewport->r.min.x, c->viewport->r.max.y, 1, - 1, 0, 0, - 0, -1, 0 - }; + return rframexform(Pt2(p.x,p.y,1), c->viewport); +} + +void +perspective(Matrix3 m, double fov, double a, double n, double f) +{ + double cotan; + + cotan = 1/tan(fov/2*DEG); + identity3(m); + m[0][0] = cotan/a; + m[1][1] = cotan; + m[2][2] = -(f+n)/(f-n); + m[2][3] = -2*f*n/(f-n); + m[3][2] = -1; +} - return rframexform(Pt2(p.x, p.y, 1), rf); +void +orthographic(Matrix3 m, double l, double r, double b, double t, double n, double f) +{ + identity3(m); + m[0][0] = 2/(r - l); + m[1][1] = 2/(t - b); + m[2][2] = -2/(f - n); + m[0][3] = -(r + l)/(r - l); + m[1][3] = -(t + b)/(t - b); + m[2][3] = -(f + n)/(f - n); } void -- cgit v1.2.3