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
|
#define HZ2MS(hz) (1000/(hz))
typedef enum {
ORTHOGRAPHIC,
PERSPECTIVE,
} Projection;
enum {
PPoint,
PLine,
PTriangle,
};
enum {
LIGHT_POINT,
LIGHT_DIRECTIONAL,
LIGHT_SPOT,
};
enum {
VAPoint,
VANumber,
};
typedef struct Color Color;
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 VSparams VSparams;
typedef struct FSparams FSparams;
typedef struct SUparams SUparams;
typedef struct Shadertab Shadertab;
typedef struct Renderer Renderer;
typedef struct Rendertime Rendertime;
typedef struct Renderjob Renderjob;
typedef struct Framebuf Framebuf;
typedef struct Framebufctl Framebufctl;
typedef struct Viewport Viewport;
typedef struct Camera Camera;
struct Color
{
double r, g, b, a;
};
/*
* 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 */
OBJMaterial *mtl;
/* TODO it'd be neat to use a dynamic hash table instead */
Vertexattr *attrs; /* attributes (aka varyings) */
ulong nattrs;
};
struct LightSource
{
Point3 p;
Color c;
int type;
};
struct Material
{
Color ambient;
Color diffuse;
Color specular;
double shininess;
};
struct Primitive
{
int type;
Vertex v[3];
};
struct Model
{
Primitive *mesh;
Memimage *tex; /* texture map */
Memimage *nor; /* normals map */
Material *materials;
ulong nmaterials;
OBJ *obj;
OBJElem **elems; /* cache of renderable elems */
ulong nelems;
};
struct Entity
{
RFrame3;
Model *mdl;
Entity *prev, *next;
};
struct Scene
{
char *name;
Entity ents;
ulong nents;
void (*addent)(Scene*, Entity*);
void (*delent)(Scene*, Entity*);
};
/* shader params */
struct VSparams
{
SUparams *su;
Vertex *v;
uint idx;
};
struct FSparams
{
SUparams *su;
Point p;
Vertex v; /* only for the attributes (varyings) */
};
/* shader unit params */
struct SUparams
{
Framebuf *fb;
Memimage *frag;
Renderjob *job;
Entity *entity;
OBJElem **eb, **ee;
uvlong uni_time;
Point3 (*vshader)(VSparams*);
Color (*fshader)(FSparams*);
};
struct Shadertab
{
char *name;
Point3 (*vshader)(VSparams*); /* vertex shader */
Color (*fshader)(FSparams*); /* fragment shader */
};
struct Renderer
{
Channel *c;
};
struct Rendertime
{
uvlong t0, t1;
};
struct Renderjob
{
Ref;
Framebuf *fb;
Scene *scene;
Shadertab *shaders;
Channel *donec;
struct {
Rendertime R, E, Tn, Rn; /* renderer, entityproc, tilers, rasterizers */
} times;
Renderjob *next;
};
struct Framebuf
{
Memimage *cb; /* color buffer */
double *zb; /* z/depth buffer */
Rectangle r;
};
struct Framebufctl
{
Lock;
Framebuf *fb[2]; /* double buffering */
uint idx; /* front buffer index */
void (*draw)(Framebufctl*, Image*);
void (*memdraw)(Framebufctl*, Memimage*);
void (*swap)(Framebufctl*);
void (*reset)(Framebufctl*);
Framebuf *(*getfb)(Framebufctl*);
Framebuf *(*getbb)(Framebufctl*);
};
struct Viewport
{
RFrame;
Framebufctl *fbctl;
void (*draw)(Viewport*, Image*);
void (*memdraw)(Viewport*, Memimage*);
Framebuf *(*getfb)(Viewport*);
};
struct Camera
{
RFrame3; /* VCS */
Viewport *vp;
Scene *s;
Renderer *rctl;
double fov; /* vertical FOV */
struct {
double n, f; /* near and far clipping planes */
} clip;
Matrix3 proj; /* VCS to NDC xform */
Projection projtype;
struct {
uvlong min, avg, max, acc, n, v;
uvlong nframes;
} stats;
struct {
Rendertime R[100], E[100], Tn[100], Rn[100];
int cur;
} times;
};
/* camera */
void configcamera(Camera*, Viewport*, double, double, double, Projection);
void placecamera(Camera*, Point3, Point3, Point3);
void aimcamera(Camera*, Point3);
void reloadcamera(Camera*);
void shootcamera(Camera*, Shadertab*);
/* viewport */
Viewport *mkviewport(Rectangle);
void rmviewport(Viewport*);
/* render */
Renderer *initgraphics(void);
Point3 model2world(Entity*, Point3);
Point3 world2vcs(Camera*, Point3);
Point3 vcs2clip(Camera*, Point3);
Point3 world2clip(Camera*, Point3);
void perspective(Matrix3, double, double, double, double);
void orthographic(Matrix3, double, double, double, double, double, double);
/* scene */
int refreshmodel(Model*);
Model *newmodel(void);
void delmodel(Model*);
Entity *newentity(Model*);
void delentity(Entity*);
Scene *newscene(char*);
void delscene(Scene*);
void clearscene(Scene*);
/* vertex */
void addvattr(Vertex*, char*, int, void*);
Vertexattr *getvattr(Vertex*, char*);
/* texture */
Color neartexsampler(Memimage*, Point2);
Color bilitexsampler(Memimage*, Point2);
Color texture(Memimage*, Point2, Color(*)(Memimage*, Point2));
/* util */
double fmin(double, double);
double fmax(double, double);
Memimage *rgb(ulong);
/* shadeop */
double sign(double);
double step(double, double);
double smoothstep(double, double, double);
extern Rectangle UR; /* unit rectangle */
|