forked from dojoe/imvitation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
k.i.t.t.h
369 lines (298 loc) · 8.41 KB
/
k.i.t.t.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#ifndef _K_I_T_T_H
#define _K_I_T_T_H
#ifdef WIN32
#define _CRT_SECURE_NO_WARNINGS 1
#include <Windows.h>
#else
#include <errno.h>
#endif
#include <algorithm>
#include <GL/glew.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <GLFW/glfw3.h>
#include <SOIL/SOIL.h>
#include <bass.h>
#include "antsy.h"
#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
#include <vector>
#include <set>
#include <map>
#include <string>
#ifdef _MSC_VER
inline int snprintf(char *str, size_t size, const char *format, ...)
{
int ret;
va_list va;
va_start(va, format);
ret = _vsnprintf_s(str, size, _TRUNCATE, format, va);
va_end(va);
return ret;
}
#endif
#if defined(WIN32) && defined(DEBUG)
#include "guicon.h"
#endif
using namespace glm;
typedef void(*loaderDrawFunc)(void *priv, float progress);
void loadBegin(loaderDrawFunc callback, void *priv, int expectedProgressCalls);
void loadProgress();
void loadEnd();
#ifdef DEBUG
#define LOG(msg, ...) { loadProgress(); printf("%7.3f " msg, (float)glfwGetTime(), __VA_ARGS__); }
#else
#define LOG(...) loadProgress()
#endif
#define WRN LOG
void ERR(const char* fmt, ...);
const char *getGlErrorName(GLenum error);
void _checkGlError(const char *file, int line);
#define checkGlError() _checkGlError(__FILE__, __LINE__)
#define __STR(x) #x
#define STR(x) __STR(x)
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*x))
const float PI = 3.14159265358979323846f;
extern unsigned long seed;
static void skrand(unsigned int s)
{
seed = s;
}
static inline int krand()
{
seed = seed * 214013L + 2531011L;
return ((seed >> 16) & 0x7fff);
}
#define KRAND_MAX 0x7FFF
static inline float rand(float min, float max)
{
return min + (float)krand() / KRAND_MAX * (max - min);
}
static inline int rand(int min, int max)
{
return min + krand() % (max - min + 1);
}
static inline int randsign()
{
return rand(0, 1) * 2 - 1;
}
static inline mat2 rotate(mat2 m, float angle)
{
float a = angle * PI / 180, c = cos(a), s = sin(a);
return mat2(c, -s, s, c) * m;
}
static inline mat2 scale(mat2 m, vec2 s)
{
return mat2(s.x, 0.0f, 0.0f, s.y) * m;
}
class Scene {
public:
Scene(const char *aName, float aStartTime, float aEndTime);
virtual void draw(float time);
float StartTime() { return startTime; };
float EndTime() { return endTime; }
float Duration() { return endTime - startTime; }
protected:
const char *name;
void *TweakBar;
virtual void _draw(float time) = 0;
private:
float startTime, endTime;
bool BarVisible;
};
class Demo {
public:
int sizex, sizey;
float aspect; /* sizex / sizey */
float inv_aspect; /* sizey / sizex */
int xborder, yborder;
Demo(int sx, int sy, float wantAspect) : scenes(NULL), aspect(wantAspect), inv_aspect(1.0f / wantAspect) { setSize(sx, sy); }
mat4 fix_aspect(mat4 m) { return scale(m, vec3(inv_aspect, 1.0f, 1.0f)); }
void setScenes(Scene** demoScenes) { scenes = demoScenes; }
void draw(float time);
Scene* getScene(int number);
void setSize(int width, int height);
void setViewport() { glViewport(xborder, yborder, sizex, sizey); }
private:
Scene** scenes;
};
extern Demo demo;
enum blendMode { NO_BLEND, BLEND_ALPHA, BLEND_INVERT_ALPHA, BLEND_ADD_ALPHA, BLEND_ADD_AND_DIM };
enum depthMode { NO_DEPTH, DEPTH_READ, DEPTH_FULL };
enum cullMode { NO_CULL, CULL_FRONT, CULL_BACK };
void setBlendMode(enum blendMode mode);
void setDepthMode(enum depthMode mode);
void setCullMode(enum cullMode mode);
void setTexFilter(int unit, int filter);
void drawArray(GLenum mode, float *verts, float *normals, float *texCoords, float *colors, GLuint *indices, size_t count, GLsizei numInstances = 1, int vertDims = 3);
void drawQuad(vec2 bl, vec2 br, vec2 tl, vec2 tr, vec2 tbl, vec2 ttr);
void drawParallelogram(vec2 bl, vec2 br, vec2 tl, vec2 tbl, vec2 ttr);
void drawRect(vec2 bl, vec2 tr, vec2 tbl, vec2 ttr);
void drawFullscreenQuad(bool flipY = true);
void drawUnitQuad();
class Fbo
{
public:
Fbo(int width, int height, GLenum format, bool depthBuffer = true);
~Fbo();
GLuint getHandle() { return handle; }
GLuint getColorTexture() { return cTex; }
void bind();
void unbind();
private:
GLuint handle, cTex, dTex;
GLsizei width, height;
bool hasDepth;
GLint prevFbo;
};
extern Fbo *gFullScreenFbo;
class File
{
public:
File(const char *fname, bool loadAsString = false);
~File();
uint8_t *getData() { return data; }
size_t getSize() { return size; }
private:
uint8_t *data;
size_t size;
};
class Texture {
public:
Texture(const char *fname, int force_channels = SOIL_LOAD_AUTO, unsigned int flags = 0);
GLuint tex;
int width;
int height;
int channels;
float aspect; /* width / height */
};
Texture *loadCacheTexture(std::string fname);
class GlslShader
{
public:
GlslShader(const char *sourceFile);
~GlslShader();
void bindTexture(const char *name, GLuint tex, int texUnit);
void bindFbo(const char *name, Fbo *fbo, int texUnit) {
bindTexture(name, fbo->getColorTexture(), texUnit);
}
void bindTexture(const char *name, Texture *tex, int texUnit) {
bindTexture(name, tex->tex, texUnit);
}
void uniform(const char *name, int value) {
glUniform1i(uloc(name), value);
}
void uniform(const char *name, float value) {
glUniform1f(uloc(name), value);
}
void uniform(const char *name, vec2 value) {
glUniform2f(uloc(name), value.x, value.y);
}
void uniform(const char *name, vec3 value) {
glUniform3f(uloc(name), value.x, value.y, value.z);
}
void uniform(const char *name, vec4 value) {
glUniform4f(uloc(name), value.x, value.y, value.z, value.w);
}
void uniform(const char *name, mat4 value) {
glUniformMatrix4fv(uloc(name), 1, GL_FALSE, &value[0][0]);
}
void uniform(const char *name, mat3 value) {
glUniformMatrix3fv(uloc(name), 1, GL_FALSE, &value[0][0]);
}
void uniform(const char *name, mat2 value) {
glUniformMatrix2fv(uloc(name), 1, GL_FALSE, &value[0][0]);
}
void uniform(const char *name, int *value, GLsizei count) {
glUniform1iv(uloc(name), count, value);
}
void uniform(const char *name, float *value, GLsizei count) {
glUniform1fv(uloc(name), count, value);
}
void uniform(const char *name, vec2 *value, GLsizei count) {
glUniform2fv(uloc(name), count, &value->x);
}
void uniform(const char *name, vec3 *value, GLsizei count) {
glUniform3fv(uloc(name), count, &value->x);
}
void uniform(const char *name, vec4 *value, GLsizei count) {
glUniform4fv(uloc(name), count, &value->x);
}
void uniform(const char *name, mat4 *value, GLsizei count) {
glUniformMatrix4fv(uloc(name), count, GL_FALSE, &(*value)[0][0]);
}
void uniform(const char *name, mat3 *value, GLsizei count) {
glUniformMatrix3fv(uloc(name), count, GL_FALSE, &(*value)[0][0]);
}
void uniform(const char *name, mat2 *value, GLsizei count) {
glUniformMatrix2fv(uloc(name), count, GL_FALSE, &(*value)[0][0]);
}
void bind();
void unbind();
private:
GLuint vs, gs, fs, prog;
std::set<std::string> warnedUniforms;
void compile(GLenum shader, const char *source, GLenum type);
GLint uloc(const GLchar *name);
};
class Music {
public:
Music(const char *fname);
void play();
void pause(bool pause);
bool paused();
bool stopped();
float getTime();
float getBeat();
void setTime(float time);
private:
static Music *instance;
HSTREAM stream;
float startTime;
float pausedTime;
float& StartTime();
};
extern Music *gMusic;
class ObjLoader {
public:
ObjLoader(const char *fname, float scale = 1.0f, bool blender = true, bool splitMaterials = false);
vec3 *getVerts3fv(int oid = 0) {
return &verts[ranges[oid].start];
}
float *getVerts(int oid = 0) {
return value_ptr(*getVerts3fv(oid));
}
float *getColors(int oid = 0) {
return colors.size() ? value_ptr(colors[ranges[oid].start]) : NULL;
}
vec2 *getUVs2fv(int oid = 0) {
return uvs.size() ? &uvs[ranges[oid].start] : NULL;
}
float *getUVs(int oid = 0) {
return uvs.size() ? value_ptr(uvs[ranges[oid].start]) : NULL;
}
float *getNormals(int oid = 0) {
return normals.size() ? value_ptr(normals[ranges[oid].start]) : NULL;
}
size_t getCount(int oid = 0) {
return ranges[oid].count;
}
void draw(int oid = 0) {
drawArray(GL_TRIANGLES, getVerts(oid), getNormals(oid), getUVs(oid), getColors(oid), NULL, getCount(oid));
}
private:
typedef std::vector<vec3> vert_t;
typedef std::vector<vec4> color_t;
typedef std::vector<vec2> uv_t;
typedef std::vector<vec3> normal_t;
typedef struct { int start, count; } range_t;
typedef std::vector<range_t> ranges_t;
vert_t verts;
color_t colors;
uv_t uvs;
normal_t normals;
ranges_t ranges;
};
#endif