-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmio.h
348 lines (276 loc) · 9.33 KB
/
mio.h
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include <errno.h>
#include "queue.h" // freebsd sys/queue.h
#include "tree.h" // freebsd sys/tree.h
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <GL/gl3w.h>
#ifdef __APPLE__
#define glutInitContextVersion(a,b)
#define glutInitContextFlags(a)
#define glutInitContextProfile(a)
#define GLUT_SRGB GLUT_RGB
#include <GLUT/glut.h>
#else
#define GLUT_3_2_CORE_PROFILE 0
#include <GL/freeglut.h>
#endif
void warn(const char *fmt, ...);
const char *gl_error_string(GLenum code);
void gl_assert(const char *msg);
void gl_assert_framebuffer(GLenum target, const char *msg);
#define GL_TEXTURE_MAX_ANISOTROPY_EXT 0x84FE
#define GL_COMPRESSED_RGB_S3TC_DXT1_EXT 0x83F0
#define GL_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
#define GL_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
#define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
#define GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT 0x8C4D
#define GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT 0x8C4E
#define GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT 0x8C4F
#define GL_COMPRESSED_SRGB_S3TC_DXT1_EXT 0x8C4C
#undef nelem
#define nelem(x) (sizeof(x)/sizeof(x)[0])
#define PTR(x) ((void*)(intptr_t)(x))
#ifndef MAX
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#endif
#define CLAMP(x,a,b) MIN(MAX(x,a),b)
#define strsep xstrsep
#define strlcpy xstrlcpy
#define strlcat xstrlcat
int chartorune(int *rune, char *str);
int runetochar(char *str, int *rune);
char *xstrsep(char **stringp, const char *delim);
int xstrlcpy(char *dst, const char *src, int siz);
int xstrlcat(char *dst, const char *src, int siz);
#define SLUM(x) ((x)*(x)) /* approximate sRGB to Linear conversion */
#define SRGB(r,g,b) SLUM(r),SLUM(g),SLUM(b)
#define SRGBA(r,g,b,a) SRGB(r,g,b),(a)
/* objects exposed to lua as light user data needs tags */
enum tag {
TAG_FONT = 'F',
TAG_MESH = 'M',
TAG_ANIM = 'A',
TAG_SKEL = 'S',
};
/* matrix math utils */
#include "vector.h"
struct pose {
vec3 position;
vec4 rotation;
vec3 scale;
};
void calc_mul_matrix(mat4 *model_from_bind_pose, mat4 *abs_pose_matrix, mat4 *inv_bind_matrix, int count);
void calc_inv_matrix(mat4 *inv_bind_matrix, mat4 *abs_bind_matrix, int count);
void calc_abs_matrix(mat4 *abs_pose_matrix, mat4 *pose_matrix, int *parent, int count);
void calc_matrix_from_pose(mat4 *pose_matrix, struct pose *pose, int count);
/* archive data file loading */
void register_directory(const char *dirname);
void register_archive(const char *zipname);
unsigned char *load_file(const char *filename, int *lenp);
/* resource cache */
struct cache;
void *lookup(struct cache *cache, const char *key);
struct cache *insert(struct cache *cache, const char *key, void *value);
void print_cache(struct cache *cache);
/* texture loader based on stb_image */
unsigned char *stbi_load(const char *filename, int *x, int *y, int *comp, int req_comp);
unsigned char *stbi_load_from_memory(const unsigned char *buffer, int len, int *x, int *y, int *comp, int req_comp);
int make_texture(unsigned char *data, int w, int h, int n, int srgb);
int load_texture(char *filename, int srgb);
int make_texture_array(unsigned char *data, int w, int h, int d, int n, int srgb);
int load_texture_array(char *filename, int srgb, int *d);
void icon_set_color(float r, float g, float b, float a);
void icon_begin(mat4 clip_from_view, mat4 view_from_world);
void icon_end(void);
void icon_show(int texture,
float x0, float y0, float x1, float y1,
float s0, float t0, float s1, float t1);
/* fonts based on stb_truetype */
struct font *load_font(const char *filename);
struct font *load_font_from_memory(const char *filename, unsigned char *data, int len);
void free_font(struct font *font);
float font_width(struct font *font, float size, char *str);
void text_begin(mat4 clip_from_view, mat4 view_from_world);
void text_set_color(float r, float g, float b, float a);
void text_set_font(struct font *font, float size);
float text_show(float x, float y, char *text);
float text_width(char *text);
void text_end(void);
/* drawing flat shaded primitives */
void draw_set_color(float r, float g, float b, float a);
void draw_begin(mat4 clip_from_view, mat4 view_from_world);
void draw_end(void);
void draw_line(float x0, float y0, float z0, float x1, float y1, float z1);
void draw_rect(float x0, float y0, float x1, float y1);
void draw_triangle(float x0, float y0, float z0,
float x1, float y1, float z1,
float x2, float y2, float z2);
void draw_quad(float x0, float y0, float z0,
float x1, float y1, float z1,
float x2, float y2, float z2,
float x3, float y3, float z3);
/* console */
extern lua_State *L;
void init_lua(void);
void run_string(const char *cmd);
void run_file(const char *filename);
void run_function(const char *fun);
int docall(lua_State *L, int narg, int nres);
void console_keyboard(int key, int mod);
void console_special(int key, int mod);
void console_printf(const char *fmt, ...);
void console_print(const char *s);
void console_printnl(const char *s);
void console_putc(int c);
void console_draw(mat4 clip_from_view, mat4 view_from_world, struct font *font, float size);
/* shaders */
enum {
ATT_POSITION,
ATT_NORMAL,
ATT_TANGENT,
ATT_TEXCOORD,
ATT_COLOR,
ATT_BLEND_INDEX,
ATT_BLEND_WEIGHT,
ATT_LIGHTMAP,
ATT_SPLAT,
ATT_WIND,
};
enum {
FRAG_COLOR = 0,
FRAG_NORMAL = 0,
FRAG_ALBEDO = 1,
};
enum {
MAP_COLOR = GL_TEXTURE0,
MAP_GLOSS,
MAP_NORMAL,
MAP_SHADOW,
MAP_DEPTH,
MAP_EMISSION,
MAP_LIGHT,
MAP_SPLAT,
};
int compile_shader(const char *vert_src, const char *frag_src);
/* materials */
int load_material(char *dirname, char *material);
/* models and animations */
#define MAXBONE 80
#define MAX_BONE_NAME 16
struct model {
struct skel *skel;
struct mesh *mesh;
struct anim *anim;
};
struct part {
unsigned int material;
int first, count;
};
struct skel {
enum tag tag;
int count;
char name[MAXBONE][MAX_BONE_NAME];
int parent[MAXBONE];
struct pose pose[MAXBONE];
};
struct mesh {
enum tag tag;
unsigned int vao, vbo, ibo;
int enabled;
int count;
struct part *part;
struct skel *skel;
mat4 *inv_bind_matrix;
};
struct anim_map {
struct skel *skel;
struct anim_map *next;
int anim_map[MAXBONE];
};
struct anim {
enum tag tag;
char *name;
int frames, channels;
float framerate;
int loop;
struct skel *skel;
float *data;
struct anim *next;
struct anim_map *anim_map_head;
struct pose motion;
int mask[MAXBONE];
struct pose pose[MAXBONE];
};
/* entity components */
struct transform
{
vec3 position;
vec4 rotation;
vec3 scale;
int dirty;
mat4 matrix;
};
struct skelpose
{
struct skel *skel;
struct pose pose[MAXBONE];
};
enum { LAMP_POINT, LAMP_SPOT, LAMP_SUN };
struct lamp
{
int type;
vec3 color;
float energy;
float distance;
float spot_angle;
float spot_blend;
int use_sphere;
int use_shadow;
};
void init_lamp(struct lamp *lamp);
void init_transform(struct transform *transform);
void init_skelpose(struct skelpose *skelpose, struct skel *skel);
struct model *load_iqe_from_memory(const char *filename, unsigned char *data, int len);
struct model *load_iqm_from_memory(const char *filename, unsigned char *data, int len);
struct model *load_obj_from_memory(const char *filename, unsigned char *data, int len);
struct model *load_model(const char *filename);
struct skel *load_skel(const char *filename);
struct mesh *load_mesh(const char *filename);
struct anim *load_anim(const char *filename);
void extract_raw_frame_root(struct pose *pose, struct anim *anim, int frame);
void extract_raw_frame(struct pose *pose, struct anim *anim, int frame);
void extract_frame_root(struct pose *pose, struct anim *anim, float frame);
void extract_frame(struct pose *pose, struct anim *anim, float frame);
void lerp_frame(struct pose *out, struct pose *a, struct pose *b, float t, int n);
void draw_skel(mat4 *abs_pose_matrix, int *parent, int count);
/* deferred shading */
void update_transform(struct transform *tra);
void update_transform_parent(struct transform *tra, struct transform *par);
void update_transform_parent_skel(struct transform *tra, struct transform *par, struct skelpose *skelpose, const char *bone);
void update_transform_root_motion(struct transform *tra, struct skelpose *skelpose);
void render_camera(mat4 iproj, mat4 iview);
void animate_skelpose(struct skelpose *skelpose, struct anim *anim, float frame, float blend);
void render_skelpose(struct transform *transform, struct skelpose *skelpose);
void render_mesh(struct transform *transform, struct mesh *mesh);
void render_mesh_skel(struct transform *transform, struct mesh *mesh, struct skelpose *skelpose);
void render_lamp(struct transform *transform, struct lamp *lamp);
void render_static_mesh(struct mesh *mesh, mat4 clip_from_view, mat4 view_from_model);
void render_skinned_mesh(struct mesh *mesh, mat4 clip_from_view, mat4 view_from_model, mat4 *model_from_bind_pose);
void render_point_lamp(struct lamp *lamp, mat4 clip_from_view, mat4 view_from_world, mat4 lamp_transform);
void render_spot_lamp(struct lamp *lamp, mat4 clip_from_view, mat4 view_from_world, mat4 lamp_transform);
void render_sun_lamp(struct lamp *lamp, mat4 clip_from_view, mat4 view_from_world, mat4 lamp_transform);
void render_sky(void);
void render_reshape(int w, int h);
void render_geometry_pass(void);
void render_light_pass(void);
void render_forward_pass(void);
void render_finish(void);
void render_blit(mat4 proj, mat4 view, int w, int h);
void render_debug_buffers(mat4 proj, mat4 view);