-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzglOglesCore.cpp
executable file
·700 lines (589 loc) · 14.3 KB
/
zglOglesCore.cpp
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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
/*
* Copyright (C) 2010 ZiiLABS Pte Ltd
* All Rights Reserved.
*
* This software and its associated documentation may contain some
* proprietary, confidential and trade secret information of
* ZiiLABS Pte Ltd and except as provided by written agreement with
* ZiiLABS Pte Ltd
*
* a) no part may be disclosed, distributed, reproduced, transmitted,
* transcribed, stored in a retrieval system, adapted or translated
* in any form or by any means electronic, mechanical, magnetic,
* optical, chemical, manual or otherwise,
*
* and
*
* b) the recipient is not entitled to discover through reverse
* engineering or reverse compiling or other such techniques or
* processes the trade secrets contained therein or in the
* documentation.
*/
#include "zglCore.h"
#include "zglBasicType.h"
#if defined(__ARMEL__) || defined (ANDROID)
#include <GLES/gl.h>
#include "zgl_jni.h"
void zglCore::reset()
{
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glColorMask(true, true, true, true);
glClearDepthf(1.0f);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
glDepthMask(true);
glDisable(GL_ALPHA_TEST);
glDisable(GL_SCISSOR_TEST);
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_MULTISAMPLE);
/*glPointSize(3);
glLineWidth(3);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);*/
glEnableClientState(GL_VERTEX_ARRAY);
}
void zglCore::flush()
{
glFlush();
}
void zglCore::setViewport(int x, int y, int width, int height)
{
glViewport(x, y, width, height);
glScissor(x, y, width, height);
}
void zglCore::clearBuffer(const unsigned char* clear_color, bool is_clear_color,
bool is_clear_depth)
{
if (!is_clear_color && !is_clear_depth)
{
return;
}
glClearColor(
(float)clear_color[0] / 255.0f,
(float)clear_color[1] / 255.0f,
(float)clear_color[2] / 255.0f,
(float)clear_color[3] / 255.0f);
glDepthMask(true);
glEnable(GL_SCISSOR_TEST);
glClear((is_clear_color ? GL_COLOR_BUFFER_BIT : 0) | (is_clear_depth ? GL_DEPTH_BUFFER_BIT : 0));
glDisable(GL_SCISSOR_TEST);
}
void zglCore::setBlendType(zglBlendType blend_type)
{
switch (blend_type)
{
case BLEND_OFF:
glDisable(GL_ALPHA_TEST);
glDisable(GL_BLEND);
break;
case BLEND_HALF:
glDisable(GL_ALPHA_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA);
break;
case BLEND_ADD:
glDisable(GL_ALPHA_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA , GL_ONE);
break;
default:
{
static GLenum SRC_FACTOR[9] =
{ GL_ZERO, GL_ONE, GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR,
GL_SRC_ALPHA_SATURATE, GL_SRC_ALPHA,
GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA,
GL_ONE_MINUS_DST_ALPHA };
static GLenum DST_FACTOR[8] =
{ GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR,
GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA,
GL_ONE_MINUS_DST_ALPHA };
int type = (int)blend_type;
type -= 3;
if(type >= 72) type = type%72;
int i = type / 8;
int j = type % 8;
glDisable(GL_ALPHA_TEST);
glEnable(GL_BLEND);
glBlendFunc(SRC_FACTOR[i], DST_FACTOR[j]);
}
}
}
void zglCore::setCullFace(bool enable)
{
if(enable)
{
glEnable(GL_CULL_FACE);
}
else
{
glDisable(GL_CULL_FACE);
}
}
void zglCore::setDepthTest(zglDepthType type, bool mask)
{
switch(type)
{
case DEPTH_NEVER: glDepthFunc(GL_NEVER); break;
default:
case DEPTH_LESS: glDepthFunc(GL_LESS); break;
case DEPTH_EQUAL: glDepthFunc(GL_EQUAL); break;
case DEPTH_LEQUAL: glDepthFunc(GL_LEQUAL); break;
case DEPTH_GREATER: glDepthFunc(GL_GREATER); break;
case DEPTH_NOTEQUAL: glDepthFunc(GL_NOTEQUAL); break;
case DEPTH_GEQUAL: glDepthFunc(GL_GEQUAL); break;
case DEPTH_ALWAYS: glDepthFunc(GL_ALWAYS); break;
}
glDepthMask(mask);
}
void zglCore::resetProjection()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
}
void zglCore::setProjection(const zglMatrix& mat)
{
glMatrixMode(GL_PROJECTION);
glLoadMatrixf((float*)&mat);
}
void zglCore::resetModelView()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void zglCore::setModelView(const zglMatrix& mat)
{
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf((float*)&mat);
}
void zglCore::setVertexPointer2(const float* vert_buf, size_t stride)
{
glVertexPointer(2, GL_FLOAT, (GLsizei)stride, vert_buf);
}
void zglCore::setVertexPointer3(const float* vert_buf, size_t stride)
{
glVertexPointer(3, GL_FLOAT, (GLsizei)stride, vert_buf);
}
void zglCore::setDefaultNormal()
{
glDisableClientState(GL_NORMAL_ARRAY);
glNormal3f(0.0f, 0.0f, -1.0f);
}
void zglCore::setNormal(const float* normal)
{
glDisableClientState(GL_NORMAL_ARRAY);
glNormal3f(normal[0], normal[1], normal[2]);
}
void zglCore::setNormalPointer(const float* normal, size_t stride)
{
if(normal != NULL)
{
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, stride, normal);
}
else
{
glDisableClientState(GL_NORMAL_ARRAY);
}
}
void zglCore::setColor4(const unsigned char* color)
{
glDisableClientState(GL_COLOR_ARRAY);
glColor4f((float)color[0] / 255.0f, (float)color[1] / 255.0f, (float)color[2] / 255.0f, (float)color[3] / 255.0f);
}
void zglCore::setColorPointer4(const unsigned char* color_buf, size_t stride)
{
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(4, GL_UNSIGNED_BYTE, (GLsizei)stride, color_buf);
}
void zglCore::setTexCoordPointer2(const float* uv_buf, size_t stride)
{
glTexCoordPointer(2, GL_FLOAT, (GLsizei)stride, uv_buf);
}
void zglCore::setLightParam(int index, const float* ambientColor,
const float* diffuseColor, const float* specularColor, const float* position)
{
GLint light;
switch(index)
{
case 0: light = GL_LIGHT0; break;
case 1: light = GL_LIGHT1; break;
case 2: light = GL_LIGHT2; break;
case 3: light = GL_LIGHT3; break;
case 4: light = GL_LIGHT4; break;
case 5: light = GL_LIGHT5; break;
case 6: light = GL_LIGHT6; break;
case 7: light = GL_LIGHT7; break;
default:
return;
}
if(ambientColor)
{
glLightfv(light, GL_AMBIENT, ambientColor);
}
if(diffuseColor)
{
glLightfv(light, GL_DIFFUSE, diffuseColor);
}
if(specularColor)
{
glLightfv(light, GL_SPECULAR, specularColor);
}
if(position)
{
glLightfv(light, GL_POSITION, position);
}
glEnable(light);
}
void zglCore::unSetLightParam(int index)
{
GLint light;
switch(index)
{
case 0: light = GL_LIGHT0; break;
case 1: light = GL_LIGHT1; break;
case 2: light = GL_LIGHT2; break;
case 3: light = GL_LIGHT3; break;
case 4: light = GL_LIGHT4; break;
case 5: light = GL_LIGHT5; break;
case 6: light = GL_LIGHT6; break;
case 7: light = GL_LIGHT7; break;
default:
return;
}
glDisable(light);
}
void zglCore::setLight(bool enable)
{
if(enable)
{
glEnable(GL_LIGHTING);
}
else
{
glDisable(GL_LIGHTING);
}
}
void zglCore::setFogParam(int mode, const float* color,
float density, float start, float end)
{
switch(mode)
{
case FOG_LINEAR:
glFogx(GL_FOG_MODE, GL_LINEAR);
break;
case FOG_EXP:
glFogx(GL_FOG_MODE, GL_EXP);
break;
case FOG_EXP2:
glFogx(GL_FOG_MODE, GL_EXP2);
break;
default:
break;
}
if(color != NULL)
{
glFogfv(GL_FOG_COLOR, color);
}
glFogf(GL_FOG_DENSITY, density);
glHint(GL_FOG_HINT, GL_DONT_CARE);
glFogf(GL_FOG_START, start);
glFogf(GL_FOG_END, end);
glEnable(GL_FOG);
}
void zglCore::unSetFogParam()
{
glDisable(GL_FOG);
}
void zglCore::setMaterial(const float* ambient,const float* diffuse,
const float* specular,const float* emission,
float exponent)
{
if(ambient)
{
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient);
}
if(diffuse)
{
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse);
}
if(specular)
{
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
}
if(emission)
{
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, emission);
}
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, exponent);
}
void zglCore::setDefaultMaterial()
{
float data[4];
data[0] = 0.2f;
data[1] = 0.2f;
data[2] = 0.2f;
data[3] = 1.0f;
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, data);
data[0] = 0.8f;
data[1] = 0.8f;
data[2] = 0.8f;
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, data);
data[0] = 0.0f;
data[1] = 0.0f;
data[2] = 0.0f;
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, data);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, data);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 0.0f);
}
void zglCore::drawArrays(zglDrawType draw_type, size_t first, size_t count)
{
const GLenum gl_draw_mode[] =
{
GL_POINTS, // DRAW_POINTS
GL_LINES, // DRAW_LINES
GL_LINE_STRIP, // DRAW_LINE_STRIP
GL_LINE_LOOP, // DRAW_LINE_LOOP
GL_TRIANGLES, // DRAW_TRIANGLES
GL_TRIANGLE_STRIP, // DRAW_TRI_STRIP
GL_TRIANGLE_FAN, // DRAW_TRI_FAN
};
if (draw_type == DRAW_QUADS)
{
for (first = 0; first < count; first += 4)
{
glDrawArrays(GL_TRIANGLE_FAN, (GLint)first, 4);
}
}
else
{
glDrawArrays(gl_draw_mode[draw_type], (GLint)first, (GLsizei)count);
}
}
void zglCore::drawElements(zglDrawType draw_type, size_t count, const unsigned short* index_array)
{
const GLenum gl_draw_mode[] =
{
GL_POINTS, // DRAW_POINTS
GL_LINES, // DRAW_LINES
GL_LINE_STRIP, // DRAW_LINE_STRIP
GL_LINE_LOOP, // DRAW_LINE_LOOP
GL_TRIANGLES, // DRAW_TRIANGLES
GL_TRIANGLE_STRIP, // DRAW_TRI_STRIP
GL_TRIANGLE_FAN, // DRAW_TRI_FAN
};
if (draw_type == DRAW_QUADS)
{
size_t offset;
for (offset = 0; offset < count; offset += 4)
{
glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_SHORT, index_array + offset);
}
}
else
{
glDrawElements(gl_draw_mode[draw_type], (GLsizei)count, GL_UNSIGNED_SHORT, index_array);
}
}
unsigned int zglCore::genTexture(unsigned short width, unsigned short height,
zglPixelFormat pixel_format, const char* image)
{
unsigned int tex_obj;
GLenum errno;
glEnable(GL_TEXTURE_2D);
//ANDROID_BITMAP_FORMAT_RGBA_8888:
//ANDROID_BITMAP_FORMAT_RGB_565:
//ANDROID_BITMAP_FORMAT_RGBA_4444:
//ANDROID_BITMAP_FORMAT_A_8:
glGenTextures(1, (GLuint*)&tex_obj);
glBindTexture(GL_TEXTURE_2D, tex_obj);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
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_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
//glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
//GLenum dataFormat;
switch(pixel_format)
{
default: return 0;
case PIXEL_A8:
{
//dataFormat = GL_ALPHA;
glTexImage2D(GL_TEXTURE_2D,
0,
GL_ALPHA,
width,
height,
0,
GL_ALPHA,
GL_UNSIGNED_BYTE,
image);
}
break;
//TODO: only RGB or RGBA support nowadays.(24bit, and 32 bits)
case PIXEL_RGB888:
{
//dataFormat = GL_RGB;
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGB,
width,
height,
0,
GL_RGB,
GL_UNSIGNED_BYTE,
image);
}
break;
case PIXEL_RGBA8888:
{
//dataFormat = GL_RGBA;
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA,
width,
height,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
image);
}
break;
case PIXEL_RGB565:
{
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGB,
width,
height,
0,
GL_RGB,
GL_UNSIGNED_SHORT_5_6_5,
image);
}
break;
}
/*glTexImage2D(GL_TEXTURE_2D,
0,
dataFormat,
width,
height,
0,
dataFormat,
GL_UNSIGNED_BYTE,
image);
*/
errno = glGetError();
if(errno != GL_NO_ERROR)
{
switch(errno){
case GL_OUT_OF_MEMORY:
LOGE("glTexImage2D: GL_OUT_OF_MEMORY");
return 0;
case GL_INVALID_ENUM:
LOGE("glTexImage2D: GL_INVALID_ENUM");
return 0;
case GL_INVALID_VALUE:
LOGE("glTexImage2D: GL_INVALID_VALUE");
return 0;
case GL_INVALID_OPERATION:
LOGE("glTexImage2D: GL_INVALID_OPERATION");
return 0;
default:
LOGE("glTexImage2D: error = %d",errno);
return 0;
}
}
#if 0
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
#endif
return tex_obj;
}
void zglCore::deleteTexture(unsigned int tex_obj)
{
glDeleteTextures(1, (const GLuint*)&tex_obj);
}
void zglCore::setTexture(unsigned int tex_obj, bool is_linear)
{
if (tex_obj == 0)
{
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
else
{
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindTexture(GL_TEXTURE_2D, tex_obj);
if (is_linear)
{
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
else
{
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
}
}
}
unsigned int zglCore::genBuffers(zglBufferType buffer, const void* data_buffer, size_t count, bool dynamic)
{
GLuint buffer_obj = 0;
GLenum target = buffer == INDEX_BUFFER ? GL_ELEMENT_ARRAY_BUFFER : GL_ARRAY_BUFFER;
GLenum usage = dynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW;
glGenBuffers(1, &buffer_obj);
glBindBuffer(target, buffer_obj);
glBufferData(target, count, data_buffer, usage);
glBindBuffer(target, 0);
GLenum err = glGetError();
if(err != GL_NO_ERROR)
{
//GL_OUT_OF_MEMORY
return 0;
}
return (unsigned int)buffer_obj;
}
void zglCore::deleteBuffers(unsigned int buffer_obj)
{
glDeleteBuffers(1, (const GLuint*)&buffer_obj);
}
bool zglCore::updateBuffer(zglBufferType buffer, unsigned int buffer_obj,
const void* data_buffer, size_t count)
{
GLenum target = buffer == INDEX_BUFFER ? GL_ELEMENT_ARRAY_BUFFER : GL_ARRAY_BUFFER;
glBindBuffer(target, (GLuint)buffer_obj);
glBufferSubData(target, 0, count, data_buffer);
glBindBuffer(target, 0);
GLenum err = glGetError();
if(err != GL_NO_ERROR)
{
//GL_OUT_OF_MEMORY
return false;
}
return true;
}
void zglCore::setBuffer(zglBufferType buffer, unsigned int buffer_obj)
{
GLenum target = buffer == INDEX_BUFFER ? GL_ELEMENT_ARRAY_BUFFER : GL_ARRAY_BUFFER;
glBindBuffer(target, (GLuint)buffer_obj);
}
bool zglCore::isBufferSupported()
{
return true;
}
#endif