summaryrefslogtreecommitdiff
path: root/camera.c
blob: c0c897d2107ef8a615155854a1a05f6f59254dca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <draw.h>
#include <memdraw.h>
#include <geometry.h>
#include "libobj/obj.h"
#include "graphics.h"
#include "internal.h"

static void
updatestats(Camera *c, uvlong v)
{
	c->stats.v = v;
	c->stats.n++;
	c->stats.acc += v;
	c->stats.avg = c->stats.acc/c->stats.n;
	c->stats.min = v < c->stats.min || c->stats.n == 1? v: c->stats.min;
	c->stats.max = v > c->stats.max || c->stats.n == 1? v: c->stats.max;
	c->stats.nframes++;
}

static void
updatetimes(Camera *c, Renderjob *j)
{
	c->times.R[c->times.cur] = j->times.R;
	c->times.E[c->times.cur] = j->times.E;
	c->times.Tn[c->times.cur] = j->times.Tn;
	c->times.Rn[c->times.cur] = j->times.Rn;
	c->times.cur = ++c->times.cur % nelem(c->times.R);
}

static void
verifycfg(Camera *c)
{
	assert(c->vp != nil);
	if(c->projtype == PERSPECTIVE)
		assert(c->fov > 0 && c->fov < 360*DEG);
	assert(c->clip.n > 0 && c->clip.n < c->clip.f);
}

void
configcamera(Camera *c, Viewport *v, double fov, double n, double f, Projection p)
{
	c->vp = v;
	c->fov = fov;
	c->clip.n = n;
	c->clip.f = f;
	c->projtype = p;
	reloadcamera(c);
}

void
placecamera(Camera *c, Point3 p, Point3 focus, Point3 up)
{
	c->p = p;
	c->bz = focus.w == 0? focus: normvec3(subpt3(c->p, focus));
	c->bx = normvec3(crossvec3(up, c->bz));
	c->by = crossvec3(c->bz, c->bx);
}

void
aimcamera(Camera *c, Point3 focus)
{
	placecamera(c, c->p, focus, c->by);
}

void
reloadcamera(Camera *c)
{
	double a;
	double l, r, b, t;

	verifycfg(c);
	switch(c->projtype){
	case ORTHOGRAPHIC:
		r = Dx(c->vp->fbctl->fb[0]->r)/2;
		t = Dy(c->vp->fbctl->fb[0]->r)/2;
		l = -r;
		b = -t;
		orthographic(c->proj, l, r, b, t, c->clip.n, c->clip.f);
		break;
	case PERSPECTIVE:
		a = (double)Dx(c->vp->fbctl->fb[0]->r)/Dy(c->vp->fbctl->fb[0]->r);
		perspective(c->proj, c->fov, a, c->clip.n, c->clip.f);
		break;
	default: sysfatal("unknown projection type");
	}
}

void
shootcamera(Camera *c, Shadertab *s)
{
	Renderjob *job;
	uvlong t0, t1;

	job = emalloc(sizeof *job);
	memset(job, 0, sizeof *job);
	job->fb = c->vp->fbctl->getbb(c->vp->fbctl);
	job->scene = c->s;
	job->shaders = s;
	job->donec = chancreate(sizeof(void*), 0);

	c->vp->fbctl->reset(c->vp->fbctl);
	t0 = nanosec();
	sendp(c->rctl->c, job);
	recvp(job->donec);
	t1 = nanosec();
	c->vp->fbctl->swap(c->vp->fbctl);

	updatetimes(c, job);
	chanfree(job->donec);
	free(job);

	updatestats(c, t1-t0);
}