summaryrefslogtreecommitdiff
path: root/graphics.h
blob: 089156af33b9eb6d08dbc3b48412293151e897db (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#define HZ2MS(hz)	(1000/(hz))
#define min(a, b)	((a)<(b)?(a):(b))
#define max(a, b)	((a)>(b)?(a):(b))

typedef enum {
	ORTHOGRAPHIC,
	PERSPECTIVE,
} Projection;

enum {
	/* culling modes */
	CullNone,
	CullFront,
	CullBack,

	/* render options */
	ROBlend	= 0x01,
	RODepth	= 0x02,
	ROAbuff	= 0x04,

	/* primitive types */
	PPoint = 0,
	PLine,
	PTriangle,

	/* light types */
	LightPoint = 0,
	LightDirectional,
	LightSpot,

	/* raster formats */
	COLOR32 = 0,	/* RGBA32 */
	FLOAT32,	/* F32 */

	/* texture types */
	RAWTexture = 0,	/* unmanaged */
	sRGBTexture,

	/* upscaling filters */
	UFNone = 0,	/* nearest neighbour */
	UFScale2x,
	UFScale3x,
	UFScale4x,

	/* vertex attribute types */
	VAPoint = 0,
	VANumber,
};

typedef struct Color Color;
typedef struct Texture Texture;
typedef struct Cubemap Cubemap;
typedef struct Vertexattr Vertexattr;
typedef struct Vertex Vertex;
typedef struct LightSource LightSource;
typedef struct Material Material;
typedef struct Primitive Primitive;
typedef struct Model Model;
typedef struct Entity Entity;
typedef struct Scene Scene;
typedef struct Shaderparams Shaderparams;
typedef struct SUparams SUparams;
typedef struct Shadertab Shadertab;
typedef struct Rendertime Rendertime;
typedef struct Renderer Renderer;
typedef struct Renderjob Renderjob;
typedef struct Fragment Fragment;
typedef struct Astk Astk;
typedef struct Abuf Abuf;
typedef struct Raster Raster;
typedef struct Framebuf Framebuf;
typedef struct Framebufctl Framebufctl;
typedef struct Viewport Viewport;
typedef struct Camera Camera;

struct Color
{
	double r, g, b, a;
};

struct Texture
{
	Memimage *image;
	int type;
};

struct Cubemap
{
	char *name;
	Texture *faces[6];
};

/*
 * a more general approach worth investigating.
 * it could be made to handle types other than double.
 *
 * examples:
 * 	double intens;
 * 	addvattr(v, "intensity", 1, &intens);
 *
 * 	Point3 p;
 * 	addvattr(v, "normal", 3, &p);
 *
 * 	Matrix3 m;
 * 	addvattr(v, "proj", 4*4, m);
 */
//struct Vertexattr
//{
//	char *id;
//	int type;
//	ulong len;
//	double val[];
//};

struct Vertexattr
{
	char *id;
	int type;
	union {
		Point3 p;
		double n;
	};
};

struct Vertex
{
	Point3 p;		/* position */
	Point3 n;		/* surface normal */
	Color c;		/* shading color */
	Point2 uv;		/* texture coordinate */
	Material *mtl;
	Point3 tangent;

	/* TODO it'd be neat to use a dynamic hash table instead */
	Vertexattr *attrs;	/* attributes (aka varyings) */
	ulong nattrs;
};

struct LightSource
{
	Point3 p;
	Point3 dir;
	Color c;
	int type;
	/* spotlights */
	double θu;	/* umbra angle. anything beyond is unlit */
	double θp;	/* penumbra angle. anything within is fully lit */
};

struct Material
{
	char *name;
	Color ambient;
	Color diffuse;
	Color specular;
	double shininess;
	Texture *diffusemap;
	Texture *specularmap;
	Texture *normalmap;
};

struct Primitive
{
	int type;
	Vertex v[3];
	Material *mtl;
	Point3 tangent;		/* used for normal mapping */
};

struct Model
{
	Primitive *prims;
	ulong nprims;
	Material *materials;
	ulong nmaterials;

	Texture *tex;		/* texture map (TODO get rid of it, use materials) */

	int (*addprim)(Model*, Primitive);
	int (*addmaterial)(Model*, Material);
};

struct Entity
{
	RFrame3;
	char *name;
	Model *mdl;

	Entity *prev, *next;
};

struct Scene
{
	char *name;
	Entity ents;
	ulong nents;
	Cubemap *skybox;

	void (*addent)(Scene*, Entity*);
	void (*delent)(Scene*, Entity*);
	Entity *(*getent)(Scene*, char*);
};

struct Shaderparams
{
	SUparams *su;
	Vertex *v;
	Point p;	/* fragment position (fshader-only) */
	uint idx;	/* vertex index (vshader-only) */

	Vertexattr *(*getuniform)(Shaderparams*, char*);
	Vertexattr *(*getattr)(Shaderparams*, char*);
	void (*setattr)(Shaderparams*, char*, int, void*);
	void (*toraster)(Shaderparams*, char*, void*);
};

/* shader unit params */
struct SUparams
{
	Framebuf *fb;
	Renderjob *job;
	Camera *camera;
	Entity *entity;
	Primitive *eb, *ee;

	uvlong uni_time;

	Point3 (*vshader)(Shaderparams*);
	Color (*fshader)(Shaderparams*);
};

struct Shadertab
{
	char *name;
	Point3 (*vshader)(Shaderparams*);	/* vertex shader */
	Color (*fshader)(Shaderparams*);	/* fragment shader */
};

struct Rendertime
{
	uvlong t0, t1;
};

struct Renderer
{
	Channel *jobq;
	ulong nprocs;
	int doprof;	/* enable profiling */
};

struct Renderjob
{
	Ref;
	uvlong id;
	Renderer *rctl;
	Framebuf *fb;
	Camera *camera;
	Scene *scene;
	Shadertab *shaders;
	Channel *donec;
	Rectangle *cliprects;	/* one per rasterizer */
	int ncliprects;

	struct {
		/* renderer, entityproc, tilers, rasterizers */
		Rendertime R, E, *Tn, *Rn;
	} times;

	Renderjob *next;
};

struct Fragment
{
	Color c;
	float z;
};

struct Astk
{
	Point p;
	Fragment *items;
	ulong size;
	int active;
};

struct Abuf
{
	QLock;
	Astk *stk;	/* framebuffer fragment stacks */
	Astk **act;	/* active fragment stacks */
	ulong nact;
};

struct Raster
{
	Rectangle r;
	char *name;
	ulong chan;
	ulong *data;

	Raster *next;
};

struct Framebuf
{
	Rectangle r;
	Rectangle clipr;
	Raster *rasters;	/* [0] color, [1] depth, [n] user-defined */
	Abuf abuf;		/* A-buffer */

	void (*createraster)(Framebuf*, char*, ulong);
	Raster *(*fetchraster)(Framebuf*, char*);
};

struct Framebufctl
{
	QLock;
	Framebuf *fb[2];	/* double buffering */
	uint idx;		/* front buffer index */
	uint upfilter;		/* upscaling filter */

	void (*draw)(Framebufctl*, Image*, char*, Point, Point);
	void (*memdraw)(Framebufctl*, Memimage*, char*, Point, Point);
	void (*swap)(Framebufctl*);
	void (*reset)(Framebufctl*);
	void (*createraster)(Framebufctl*, char*, ulong);
	Raster *(*fetchraster)(Framebufctl*, char*);
	Framebuf *(*getfb)(Framebufctl*);
	Framebuf *(*getbb)(Framebufctl*);
};

struct Viewport
{
	RFrame;
	Framebufctl *fbctl;
	Rectangle r;

	void (*draw)(Viewport*, Image*, char*);
	void (*memdraw)(Viewport*, Memimage*, char*);
	void (*setscale)(Viewport*, double, double);
	void (*setscalefilter)(Viewport*, int);
	Framebuf *(*getfb)(Viewport*);
	int (*getwidth)(Viewport*);
	int (*getheight)(Viewport*);
	void (*createraster)(Viewport*, char*, ulong);
	Raster *(*fetchraster)(Viewport*, char*);
};

struct Camera
{
	RFrame3;		/* VCS */
	Viewport *view;
	Scene *scene;
	Renderer *rctl;
	double fov;		/* vertical FOV */
	struct {
		double n, f;	/* near and far clipping planes */
	} clip;
	Matrix3 proj;		/* VCS to clip space xform */
	Projection projtype;
	int cullmode;
	uint rendopts;

	struct {
		uvlong min, avg, max, acc, n, v;
		uvlong nframes;
	} stats;
};

/* camera */
Camera *Cam(Rectangle, Renderer*, Projection, double, double, double);
Camera *newcamera(void);
void delcamera(Camera*);
void reloadcamera(Camera*);
void configcamera(Camera*, Projection, double, double, double);
void placecamera(Camera*, Scene*, Point3, Point3, Point3);
void movecamera(Camera*, Point3);
void rotatecamera(Camera*, Point3, double);
void aimcamera(Camera*, Point3);
void shootcamera(Camera*, Shadertab*);

/* viewport */
Viewport *mkviewport(Rectangle);
void rmviewport(Viewport*);

/* render */
Renderer *initgraphics(void);

/* xform */
Point3 model2world(Entity*, Point3);
Point3 world2vcs(Camera*, Point3);
Point3 vcs2clip(Camera*, Point3);
Point3 world2clip(Camera*, Point3);
Point3 clip2ndc(Point3);
Point3 ndc2viewport(Framebuf*, Point3);
Point3 viewport2ndc(Framebuf*, Point3);
Point3 ndc2vcs(Camera*, Point3);
Point3 viewport2vcs(Camera*, Point3);
Point3 vcs2world(Camera*, Point3);
Point3 viewport2world(Camera*, Point3);
Point3 world2model(Entity*, Point3);
void perspective(Matrix3, double, double, double, double);
void orthographic(Matrix3, double, double, double, double, double, double);

/* obj */
int loadobjmodel(Model*, OBJ*);
Model *readobjmodel(char*);

/* scene */
Model *newmodel(void);
Model *dupmodel(Model*);
void delmodel(Model*);
Entity *newentity(char*, Model*);
Entity *dupentity(Entity*);
void delentity(Entity*);
Scene *newscene(char*);
Scene *dupscene(Scene*);
void delscene(Scene*);
void clearscene(Scene*);

/* vertex */
void addvattr(Vertex*, char*, int, void*);
Vertexattr *getvattr(Vertex*, char*);

/* texture */
Texture *alloctexture(int, Memimage*);
Texture *duptexture(Texture*);
void freetexture(Texture*);
Color neartexsampler(Texture*, Point2);
Color bilitexsampler(Texture*, Point2);
Color sampletexture(Texture*, Point2, Color(*)(Texture*, Point2));
Cubemap *readcubemap(char*[6]);
Cubemap *dupcubemap(Cubemap*);
void freecubemap(Cubemap*);
Color samplecubemap(Cubemap*, Point3, Color(*)(Texture*, Point2));

/* util */
Point minpt(Point, Point);
Point maxpt(Point, Point);
Point2 modulapt2(Point2, Point2);
Point2 minpt2(Point2, Point2);
Point2 maxpt2(Point2, Point2);
Point3 modulapt3(Point3, Point3);
Point3 minpt3(Point3, Point3);
Point3 maxpt3(Point3, Point3);
Memimage *rgb(ulong);
Memimage *dupmemimage(Memimage*);

/* color */
ulong col2ul(Color);
Color ul2col(ulong);
int hasalpha(ulong);
Color srgb2linear(Color);
Color linear2srgb(Color);
Color aces(Color);
Color aces2(Color);

/* shadeop */
double sign(double);
double step(double, double);
double smoothstep(double, double, double);
Color getlightcolor(LightSource*, Point3);

extern Rectangle UR;	/* unit rectangle */