-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcatgl.h
644 lines (563 loc) · 17.3 KB
/
catgl.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
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
//---------------------------------------------------------
// catgl
//
// ©2015-2018 Yuichiro Nakada
//---------------------------------------------------------
#ifndef CATGL_H_
#define CATGL_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <math.h>
#include <memory.h>
#ifdef CATGL_GLES_IMPLEMENTATION
#define CATGL_GLES
#define CATGL_IMPLEMENTATION
#endif
#define CATGL_PI 3.14159265358979
#define CATGL_MODE_POINT GL_POINTS
#define CATGL_MODE_LINE GL_LINES
#define CATGL_MODE_TRIANGLES GL_TRIANGLES
//int caMode = 4;// = CATGL_MODE_TRIANGLES;
typedef struct _CATGL_VERTEX
{
float x, y, z;
float r, g, b, a;
float u, v;
} CATGL_VERTEX;
void (*caMouseEvent)(int button, int action, int x, int y);
void (*caKeyEvent)(int key, int action);
#ifdef _WIN32
//define something for Windows (32-bit and 64-bit, this part is common)
#include <windows.h>
#include <GL/gl.h>
//#include <glext.h>
#ifdef _WIN64
//define something for Windows (64-bit only)
#endif
#elif __APPLE__
#if TARGET_IPHONE_SIMULATOR
// iOS Simulator
#elif TARGET_OS_IPHONE
// iOS device
#include <OpenGLES/ES1/gl.h>
#elif TARGET_OS_MAC
// Other kinds of Mac OS
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
#else
// Unsupported platform
#endif
#elif __ANDROID__
#ifdef CATGL_GLES
#include <GLES/gl.h>
#endif
#include <GLES2/gl2.h>
//#include <GLES2/gl2ext.h>
// for debug
#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__))
// #ifdef DEBUG
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__))
// #else
// #define LOGD(...)
// #endif
// for OpenGL ES2
#define glBindFragDataLocation(...) // use 'gl_FragColor' only!!
#define CATGL_ASSETS(s) caGetPath(s)
#ifdef CATGL_IMPLEMENTATION
#include "catgl_android.h"
#define NANOVG_GLES2_IMPLEMENTATION
#else
#define NANOVG_GLES2
#endif
#define nvgCreate nvgCreateGLES2
#define nvgDelete nvgDeleteGLES2
/*#include <GLES3/gl3.h>
#define NANOVG_GLES3_IMPLEMENTATION
#define nvgCreate nvgCreateGLES3
#define nvgDelete nvgDeleteGLES3*/
#define CATGL_ACTION_UP AMOTION_EVENT_ACTION_UP
#define CATGL_ACTION_DOWN AMOTION_EVENT_ACTION_DOWN
#define CATGL_ACTION_MOVE AMOTION_EVENT_ACTION_MOVE
#define CATGL_MOUSE_BUTTON_LEFT AINPUT_SOURCE_TOUCHSCREEN
#define CATGL_MOUSE_BUTTON_RIGHT -1
#define CATGL_MOUSE_BUTTON_MIDDLE -1
#define CATGL_KEY_ESCAPE AKEYCODE_ESCAPE
#define CATGL_KEY_A AKEYCODE_A
#define CATGL_KEY_B AKEYCODE_B
#define CATGL_KEY_C AKEYCODE_C
#define CATGL_KEY_D AKEYCODE_D
#define CATGL_KEY_E AKEYCODE_E
#define CATGL_KEY_F AKEYCODE_F
#define CATGL_KEY_P AKEYCODE_P
#define CATGL_KEY_T AKEYCODE_T
#define CATGL_KEY_W AKEYCODE_W
#define CATGL_KEY_UP AKEYCODE_F
#define CATGL_KEY_DOWN AKEYCODE_V
#define CATGL_KEY_RIGHT AKEYCODE_B
#define CATGL_KEY_LEFT AKEYCODE_C
#define CATGL_KEY_PLUS AKEYCODE_PLUS
#define CATGL_KEY_MINUS AKEYCODE_MINUS
#elif __linux
#ifndef GL_GLEXT_PROTOTYPES
#define GL_GLEXT_PROTOTYPES
#endif
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glext.h>
//#include <GL/glx.h>
//#include <GL/glut.h>
// for debug
#define LOGE(...) (fprintf(stderr, __VA_ARGS__))
// #ifdef DEBUG
#define LOGD(...) (fprintf(stderr, __VA_ARGS__))
// #else
// #define LOGD(...)
// #endif
// #define CATGL_ASSETS(s) ("assets/" s)
#define CATGL_ASSETS(s) caGetPath(s)
#ifdef CATGL_IMPLEMENTATION
#include "catgl_glfw.h"
#define NANOVG_GL2_IMPLEMENTATION
#else
#define NANOVG_GL2
#endif
#define nvgCreate nvgCreateGL2
#define nvgDelete nvgDeleteGL2
#define CATGL_ACTION_UP GLFW_RELEASE // 0
#define CATGL_ACTION_DOWN GLFW_PRESS // 1
#define CATGL_ACTION_MOVE 2
#define CATGL_MOUSE_BUTTON_LEFT GLFW_MOUSE_BUTTON_LEFT
#define CATGL_MOUSE_BUTTON_RIGHT GLFW_MOUSE_BUTTON_RIGHT
#define CATGL_MOUSE_BUTTON_MIDDLE GLFW_MOUSE_BUTTON_MIDDLE
#define CATGL_KEY_ESCAPE GLFW_KEY_ESCAPE
#define CATGL_KEY_SPACE GLFW_KEY_SPACE
#define CATGL_KEY_ENTER GLFW_KEY_ENTER
#define CATGL_KEY_KP_ENTER GLFW_KEY_KP_ENTER
#define CATGL_KEY_A GLFW_KEY_A
#define CATGL_KEY_B GLFW_KEY_B
#define CATGL_KEY_C GLFW_KEY_C
#define CATGL_KEY_D GLFW_KEY_D
#define CATGL_KEY_E GLFW_KEY_E
#define CATGL_KEY_F GLFW_KEY_F
#define CATGL_KEY_P GLFW_KEY_P
#define CATGL_KEY_T GLFW_KEY_T
#define CATGL_KEY_W GLFW_KEY_W
#define CATGL_KEY_UP GLFW_KEY_UP
#define CATGL_KEY_DOWN GLFW_KEY_DOWN
#define CATGL_KEY_RIGHT GLFW_KEY_RIGHT
#define CATGL_KEY_LEFT GLFW_KEY_LEFT
#define CATGL_KEY_PLUS GLFW_KEY_KP_ADD
#define CATGL_KEY_MINUS GLFW_KEY_KP_SUBTRACT
#elif __unix // all unices not caught above
// Unix
#elif __posix
// POSIX
#endif
#include "catgl_obj.h"
#ifdef CATGL_NANOVG
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
#pragma GCC diagnostic ignored "-Wsign-compare"
#include "nanovg.h"
#include "nanovg_gl.h"
#include "catgl_ui.h"
#pragma GCC diagnostic pop
#endif
#ifdef CATGL_IMPLEMENTATION
#ifdef CATGL_NANOVG
#include "fonts/entypo.h"
#include "fonts/mplus-1c-regular-sub.h"
#include "fonts/mplus-1c-black-sub.h"
//#include "fonts/mplus-1c-light-sub.h"
#define nvgCreateEx(vg,opt) \
vg = nvgCreate(opt); \
nvgCreateFontMem(vg, "sans", mplus_1c_regular_sub_ttf, sizeof(mplus_1c_regular_sub_ttf), 0); \
nvgFontFace(vg, "sans"); \
nvgCreateFontMem(vg, "sans-bold", mplus_1c_black_sub_ttf, sizeof(mplus_1c_black_sub_ttf), 0); \
nvgFontFace(vg, "sans-bold"); \
nvgCreateFontMem(vg, "icons", (unsigned char*)entypo_ttf, sizeof(entypo_ttf), 0); \
nvgFontFace(vg, "icons");
// nvgCreateFontMem(vg, "icons", (unsigned char*)icons, sizeof(icons), 0);
// nvgFontFace(vg, "icons");
typedef struct {
int handle;
float a; // alpha
float x, y, w, h; // position and size of the sprite rectangle on screen
float sx, sy, sw, sh; // sprite location on texture
int width; // image size
int height;
NVGcontext *c;
NVGpaint pattern;
} CATGL_SPRITE;
void caSpriteLoad(CATGL_SPRITE *s, char *name, NVGcontext *vg)
{
s->c = vg;
s->handle = nvgCreateImage(s->c, CATGL_ASSETS(name), 0);
nvgImageSize(s->c, s->handle, &s->width, &s->height);
s->x = s->y = s->sx = s->sy = 0;
s->w = s->sw = s->width;
s->h = s->sh = s->height;
s->a = 1;
}
void caSpriteRender(CATGL_SPRITE *s)
{
// Aspect ration of pixel in x an y dimensions. This allows us to scale
// the sprite to fill the whole rectangle.
float ax = s->w / s->sw;
float ay = s->h / s->sh;
s->pattern = nvgImagePattern(s->c, s->x -s->sx*ax, s->y -s->sy*ay, s->width*ax, s->height*ay, 0, s->handle, s->a);
// s->pattern = nvgImagePattern(s->c, s->x, s->y, s->width, s->height, 0, s->handle, 1);
nvgBeginPath(s->c);
nvgRect(s->c, s->x, s->y, s->w, s->h);
nvgFillPaint(s->c, s->pattern);
nvgFill(s->c);
}
void caSpriteDelete(CATGL_SPRITE *s)
{
nvgDeleteImage(s->c, s->handle);
s->handle = 0;
s->c = 0;
}
int caSpriteIntersect(CATGL_SPRITE *a, CATGL_SPRITE *b)
{
if (b->x <= a->x+a->w/2 && b->y <= a->y+a->h/2
&& b->x+b->w/2 >= a->x && b->y+b->h/2 >= a->y) {
return 1;
} else {
return 0;
}
}
#endif
int caMode = 4;// = CATGL_MODE_TRIANGLES;
// ファイルの内容をメモリに割り当て
char *caGetFileContents(const char *file_name)
{
char *buf;
FILE *fp;
size_t read_size, buf_size;
fp = fopen(file_name, "r");
if (!fp) {
LOGE("Cannot open %s.\n", file_name);
return 0;
}
buf_size = BUFSIZ;
buf = malloc(sizeof(char) * buf_size);
if (!buf) {
LOGE("Memory allocation error.\n");
return 0;
}
read_size = 0;
for (;;) {
size_t s;
s = fread(buf + read_size, sizeof(char), BUFSIZ, fp);
read_size += s;
if (s < BUFSIZ) break;
buf_size += BUFSIZ;
buf = realloc(buf, sizeof(char) * buf_size);
if (!buf) {
LOGE("Memory allocation error.\n");
return 0;
}
}
*(buf + read_size) = '\0';
return buf;
}
// 行列4x4
void caMultMatrix(float *a, float *b, float *r)
{
r[ 0] = a[0] * b[0] + a[4] * b[1] + a[8] * b[2] + a[12] * b[3];
r[ 1] = a[1] * b[0] + a[5] * b[1] + a[9] * b[2] + a[13] * b[3];
r[ 2] = a[2] * b[0] + a[6] * b[1] + a[10] * b[2] + a[14] * b[3];
r[ 3] = a[3] * b[0] + a[7] * b[1] + a[11] * b[2] + a[15] * b[3];
r[ 4] = a[0] * b[4] + a[4] * b[5] + a[8] * b[6] + a[12] * b[7];
r[ 5] = a[1] * b[4] + a[5] * b[5] + a[9] * b[6] + a[13] * b[7];
r[ 6] = a[2] * b[4] + a[6] * b[5] + a[10] * b[6] + a[14] * b[7];
r[ 7] = a[3] * b[4] + a[7] * b[5] + a[11] * b[6] + a[15] * b[7];
r[ 8] = a[0] * b[8] + a[4] * b[9] + a[8] * b[10]+ a[12] * b[11];
r[ 9] = a[1] * b[8] + a[5] * b[9] + a[9] * b[10]+ a[13] * b[11];
r[10] = a[2] * b[8] + a[6] * b[9] + a[10] * b[10]+ a[14] * b[11];
r[11] = a[3] * b[8] + a[7] * b[9] + a[11] * b[10]+ a[15] * b[11];
r[12] = a[0] * b[12]+ a[4] * b[13]+ a[8] * b[14]+ a[12] * b[15];
r[13] = a[1] * b[12]+ a[5] * b[13]+ a[9] * b[14]+ a[13] * b[15];
r[14] = a[2] * b[12]+ a[6] * b[13]+ a[10] * b[14]+ a[14] * b[15];
r[15] = a[3] * b[12]+ a[7] * b[13]+ a[11] * b[14]+ a[15] * b[15];
}
// 合成(glMultMatrixf)
/*void caMultMatrix(float *s1, float *s2, float *r)
{
int x, y;
for (y=0; y<4; y++) {
for (x=0; x<4; x++) {
r[y*4+x] = s2[y*4]*s1[x] + s2[y*4+1]*s1[x+4] + s2[y*4+2]*s1[x+8] + s2[y*4+3]*s1[x+12];
}
}
}
// gluLookAt
void caLookAt(float eyex, float eyey, float eyez, float centerx, float centery, float centerz, float upx, float upy, float upz)
{
float forward[3], side[3], up[3];
forward[0] = centerx - eyex;
forward[1] = centery - eyey;
forward[2] = centerz - eyez;
up[0] = upx;
up[1] = upy;
up[2] = upz;
caNormalize(forward);
caCross(forward, up, side);
caNormalize(side);
caCross(side, forward, up);
float m[]= {
side[0], up[0], -forward[0], 0,
side[1], up[1], -forward[1], 0,
side[2], up[2], -forward[2], 0,
0, 0, 0, 1
};
float mov[]= {
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
-eyex, -eyey, -eyez, 1
};
caMultiplication(m, mov, multi);
//glLoadMatrixf(multi);
}
// gluPerspective
void caPerspective(float fovy, float aspect, float znear, float zfar)
{
float radian=2*PI*fovy/360.0;
float t = (float)(1.0 / tan(radian/2));
float m[]= {
t / aspect, 0, 0, 0,
0, t, 0, 0,
0, 0, (zfar + znear) / (znear - zfar), -1,
0, 0, (2 * zfar * znear) / (znear - zfar), 0
};
//glLoadMatrixf(m);
}
// 基準座標変換
void makeOrthoMatrix(float left, float right, float bottom, float top, float *ret)
{
float dx = right - left;
float dy = top - bottom;
float dz = 0;
float tx = (dx != 0) ? -(right left) / dx : 0;
float ty = (dy != 0) ? -(top bottom) / dy : 0;
float tz = 0;
ret[0] = 2.0f / dx; ret[3] = tx;
ret[5] = 2.0f / dy; ret[7] = ty;
ret[10] = -2.0f / dz; ret[11] = tz;
ret[15] = 1;
}*/
void caPrintMatrix(float *m)
{
LOGD("m[0]:% 7.5f m[4]:% 7.5f m[8] :% 7.5f m[12]:% 7.5f\n", m[0], m[4], m[8], m[12]);
LOGD("m[1]:% 7.5f m[5]:% 7.5f m[9] :% 7.5f m[13]:% 7.5f\n", m[1], m[5], m[9], m[13]);
LOGD("m[2]:% 7.5f m[6]:% 7.5f m[10]:% 7.5f m[14]:% 7.5f\n", m[2], m[6], m[10], m[14]);
LOGD("m[3]:% 7.5f m[7]:% 7.5f m[11]:% 7.5f m[16]:% 7.5f\n", m[3], m[7], m[11], m[15]);
}
// glLoadIdentity
void caMakeUnit(float *m)
{
memset(m, 0, sizeof(float)*16);
m[0] = m[5] = m[10]= m[15] = 1.0f;
}
void caRotationX(float *m, float degree)
{
float rad = ((float)degree * CATGL_PI / 180.0);
m[ 5] = cos(rad);
m[ 9] = - sin(rad);
m[ 6] = sin(rad);
m[10] = cos(rad);
}
void caRotationY(float *m, float degree)
{
float rad = ((float)degree * CATGL_PI / 180.0);
m[ 0] = cos(rad);
m[ 8] = sin(rad);
m[ 2] = - sin(rad);
m[10] = cos(rad);
}
void caRotationZ(float *m, float degree)
{
float rad = ((float)degree * CATGL_PI / 180.0);
m[ 0] = cos(rad);
m[ 4] = - sin(rad);
m[ 1] = sin(rad);
m[ 5] = cos(rad);
}
void caSetPosition(float *m, float x, float y, float z)
{
m[12] = x;
m[13] = y;
m[14] = z;
}
// 射影行列を近面、遠面、視野角、アスペクト比で指定
void caMakeProjectionMatrix(float *m, float n, float f, float hfov, float r)
{
float w = 1.0f / tan(hfov * 0.5f * CATGL_PI / 180);
float h = w * r;
float q = 1.0f / (f - n);
memset(&m[1], 0, sizeof(float)*15);
m[0] = w;
m[5] = h;
m[10]= -(f + n) * q;
m[11]= -1.0f;
m[14]= -2.0f * f * n * q;
// m[1] = m[2] = m[3] = m[4] = m[6] = m[7]
// = m[8] = m[9] = m[12] = m[13] = m[15] = 0.0f;
}
void caPrintShaderInfo(GLuint shader, const char *str)
{
int logSize, length;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logSize);
if (logSize > 1) {
GLchar infoLog[logSize];
glGetShaderInfoLog(shader, logSize, &length, infoLog);
//LOGD("Compile Error in %s\n%s\n", str, infoLog);
LOGD("Compile Error\n%s\n", infoLog);
}
}
GLuint caLoadShader(GLenum shaderType, const char* pSource)
{
GLuint shader = glCreateShader(shaderType);
glShaderSource(shader, 1, &pSource, 0);
glCompileShader(shader);
GLint compiled;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if (!compiled) caPrintShaderInfo(shader, pSource);
return shader;
}
#define CATGL_ATT_VERTEX 0
#define CATGL_ATT_TEXTURE 1
GLuint caCreateProgram(const char* pVertexSource, const char *pv, const char* pFragmentSource, const char *fc)
{
GLuint vertexShader = caLoadShader(GL_VERTEX_SHADER, pVertexSource);
GLuint pixelShader = caLoadShader(GL_FRAGMENT_SHADER, pFragmentSource);
GLuint program = glCreateProgram();
glAttachShader(program, vertexShader);
glDeleteShader(vertexShader);
glAttachShader(program, pixelShader);
glDeleteShader(pixelShader);
glBindAttribLocation(program, CATGL_ATT_TEXTURE, "texcoord");
glBindAttribLocation(program, 0, pv);
glBindFragDataLocation(program, 0, fc);
glLinkProgram(program);
//GLint linkStatus = GL_FALSE;
//glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
glUseProgram(program);
return program;
}
GLuint caCreateObject(void *obj, GLuint num, GLuint att[3])
{
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(CATGL_VERTEX) * num, obj, GL_STATIC_DRAW);
size_t offset = 0;
glEnableVertexAttribArray(att[0]);
glVertexAttribPointer(att[0], 3, GL_FLOAT, GL_FALSE, sizeof(CATGL_VERTEX), (const void*)offset);
offset += sizeof(float) * 3;
if (att[1]<65535) {
glEnableVertexAttribArray(att[1]);
glVertexAttribPointer(att[1], 4, GL_FLOAT, GL_FALSE, sizeof(CATGL_VERTEX), (const void*)offset);
}
offset += sizeof(float) * 4;
if (att[2]<65535) {
glEnableVertexAttribArray(att[2]);
glVertexAttribPointer(att[2], 2, GL_FLOAT, GL_FALSE, sizeof(CATGL_VERTEX), (const void*)offset);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
return vbo;
}
GLuint caCreateObject_(void *obj, int size, GLuint num, GLuint d[], int len)
{
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, size * num, obj, GL_STATIC_DRAW);
// LOGD("size:%d, num:%d, len:%d\n", size, num, len);
size_t offset = 0;
for (int i=0; i<len; i++) {
glEnableVertexAttribArray(d[i*2]);
glVertexAttribPointer(d[i*2], d[i*2+1], GL_FLOAT, GL_FALSE, size, (const void*)offset);
offset += sizeof(float) * d[i*2+1];
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
return vbo;
}
/*int caErrorHandle(char *name)
{
GLenum error_code= glGetError();
if (error_code == GL_NO_ERROR) return 1;
do {
const char* msg= "";
switch (error_code) {
case GL_INVALID_ENUM: msg = "INVALID_ENUM"; break;
case GL_INVALID_VALUE: msg = "INVALID_VALUE"; break;
case GL_INVALID_OPERATION: msg = "INVALID_OPERATION"; break;
case GL_OUT_OF_MEMORY: msg = "OUT_OF_MEMORY"; break;
case GL_INVALID_FRAMEBUFFER_OPERATION: msg = "INVALID_FRAMEBUFFER_OPERATION"; break;
default: msg = "Unknown"; break;
}
LOGE("ERROR: %04x'%s' %s\n", error_code, msg, name);
error_code= glGetError();
} while (error_code != GL_NO_ERROR);
return 0;
}*/
GLuint caCreateTexture(unsigned char *tex, int w, int h)
{
GLuint id;
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, /*GL_CLAMP_TO_EDGE*/GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, /*GL_CLAMP_TO_EDGE*/GL_MIRRORED_REPEAT);
//glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
//glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
return id;
}
#ifndef CATGL_NANOVG
#define STB_IMAGE_IMPLEMENTATION
#endif
#include "stb_image.h"
GLuint caLoadTexture(char *name/*, int &width, int &height, int &bpp*/)
{
unsigned char *pixels;
int width, height, bpp;
pixels = stbi_load(CATGL_ASSETS(name), &width, &height, &bpp, 4/*RGBA*/);
/* if (!pixels)*/ LOGE("load:%s pixels:%x %dx%d\n", CATGL_ASSETS(name), (unsigned int)pixels, width, height);
GLuint id = caCreateTexture(pixels, width, height);
stbi_image_free(pixels);
return id;
}
// draw model for .obj
void caDrawObject(GLuint *vbo, CATGL_MODEL *m)
{
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glDrawArrays(caMode, 0, m->num_vertices);
}
void caCreateObject_GL1(GLuint *vbo, CATGL_MODEL *m)
{
glGenBuffers(2, vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * m->num_vertices *3, m->vertices, GL_STATIC_DRAW);
}
void caDeleteObject_GL1(GLuint *vbo)
{
glDeleteBuffers(2, vbo);
}
#endif // !CATGL_IMPLEMENTATION
#ifdef __cplusplus
}
#endif
#endif // !CATGL_H_