DEADSOFTWARE

Fix build
[nanogl.git] / nanoWrap.cpp
1 /*
2 Copyright (C) 2007-2009 Olli Hinkka
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 */
20 /*
21 #include <e32def.h>
22 #include <e32std.h>
23 */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "gl.h"
30 #include "glesinterface.h"
31 #include "nanogl.h"
32
33 #define GL_TEXTURE0_ARB 0x84C0
34 #define GL_TEXTURE1_ARB 0x84C1
35
36 struct nanoState
37 {
38 GLboolean alpha_test;
39 GLboolean blend;
40 GLboolean clip_planei;
41 GLboolean color_logic_op;
42 GLboolean color_material;
43 GLboolean cull_face;
44 GLboolean depth_test;
45 GLboolean dither;
46 GLboolean fog;
47 GLboolean lighti;
48 GLboolean lighting;
49 GLboolean line_smooth;
50 GLboolean matrix_palette_oes;
51 GLboolean multisample;
52 GLboolean normalize;
53 GLboolean point_smooth;
54 GLboolean point_sprite_oes;
55 GLboolean polygon_offset_fill;
56 GLboolean rescale_normal;
57 GLboolean sample_alpha_to_coverage;
58 GLboolean sample_alpha_to_one;
59 GLboolean sample_coverage;
60 GLboolean scissor_test;
61 GLboolean stencil_test;
62 GLboolean depthmask;
63 GLclampf depth_range_near;
64 GLclampf depth_range_far;
65 GLenum depth_func;
66 GLenum cullface;
67 GLenum shademodel;
68 GLenum sfactor;
69 GLenum dfactor;
70 GLenum matrixmode;
71 };
72
73 static struct nanoState nanoglState;
74
75 static struct nanoState nanoglInitState =
76 {
77 GL_FALSE,
78 GL_FALSE,
79 GL_FALSE,
80 GL_FALSE,
81 GL_FALSE,
82 GL_FALSE,
83 GL_FALSE,
84 GL_TRUE,
85 GL_FALSE,
86 GL_FALSE,
87 GL_FALSE,
88 GL_FALSE,
89 GL_FALSE,
90 GL_TRUE,
91 GL_FALSE,
92 GL_FALSE,
93 GL_FALSE,
94 GL_FALSE,
95 GL_FALSE,
96 GL_FALSE,
97 GL_FALSE,
98 GL_FALSE,
99 GL_FALSE,
100 GL_FALSE,
101 GL_TRUE,
102 0.0f,
103 1.0f,
104 GL_LESS,
105 GL_BACK,
106 GL_SMOOTH,
107 GL_ONE,
108 GL_ZERO,
109 GL_MODELVIEW,
110 };
111
112 struct booleanstate
113 {
114 GLboolean value;
115 GLboolean changed;
116 };
117
118 struct floatstate
119 {
120 GLfloat value;
121 GLboolean changed;
122 };
123
124 struct uintstate
125 {
126 GLuint value;
127 GLboolean changed;
128 };
129
130 struct ptrstate
131 {
132 GLint size;
133 GLenum type;
134 GLsizei stride;
135 GLvoid* ptr;
136 GLboolean changed;
137 GLboolean enabled;
138 };
139
140
141 struct nanotmuState
142 {
143 struct booleanstate texture_2d;
144 struct floatstate texture_env_mode;
145 struct uintstate boundtexture;
146 struct ptrstate vertex_array;
147 struct ptrstate color_array;
148 struct ptrstate texture_coord_array;
149 struct ptrstate normal_array;
150
151 };
152
153 static struct nanotmuState tmuState0;
154 static struct nanotmuState tmuState1;
155
156 static struct nanotmuState tmuInitState =
157 {
158 {GL_FALSE, GL_FALSE},
159 {GL_MODULATE,GL_FALSE},
160 {0x7fffffff,GL_FALSE},
161 {4,GL_FLOAT,0, NULL, GL_FALSE, GL_FALSE},
162 {4,GL_FLOAT,0, NULL, GL_FALSE, GL_FALSE},
163 {4,GL_FLOAT,0, NULL, GL_FALSE, GL_FALSE},
164 {3,GL_FLOAT,0, NULL, GL_FALSE, GL_FALSE},
165 };
166
167 static struct nanotmuState* activetmuState = &tmuState0;
168
169 extern "C++" GlESInterface* glEsImpl;
170
171 static GLenum wrapperPrimitiveMode = GL_QUADS;
172 GLboolean useTexCoordArray = GL_FALSE;
173 static GLenum activetmu = GL_TEXTURE0;
174 static GLenum clientactivetmu = GL_TEXTURE0;
175
176 #if defined(__MULTITEXTURE_SUPPORT__)
177 GLboolean useMultiTexCoordArray = GL_FALSE;
178 #endif
179
180
181 #if !defined (__WINS__)
182 //#define __FORCEINLINE __forceinline
183 #define __FORCEINLINE inline
184 #else
185 #define __FORCEINLINE
186 #endif
187
188 static GLboolean delayedttmuchange = GL_FALSE;
189 static GLenum delayedtmutarget = GL_TEXTURE0;
190
191 struct VertexAttrib
192 {
193 float x;
194 float y;
195 float z;
196 #if !defined(__MULTITEXTURE_SUPPORT__)
197 float padding;
198 #endif
199 unsigned char red;
200 unsigned char green;
201 unsigned char blue;
202 unsigned char alpha;
203
204 float s;
205 float t;
206 #if defined(__MULTITEXTURE_SUPPORT__)
207 float s_multi;
208 float t_multi;
209 #endif
210 };
211
212 static VertexAttrib vertexattribs[60000];
213
214 static GLushort indexArray[50000];
215
216 static GLuint vertexCount = 0;
217 static GLuint indexCount = 0;
218 static GLuint vertexMark = 0;
219 static int indexbase = 0;
220
221 static VertexAttrib* ptrVertexAttribArray = NULL;
222 static VertexAttrib* ptrVertexAttribArrayMark = NULL;
223
224 static VertexAttrib currentVertexAttrib;
225 #if defined(__MULTITEXTURE_SUPPORT__)
226 static VertexAttrib currentVertexAttribInit = {0.0f,0.0f,0.0f,
227 255,255,255,255,
228 0.0f,0.0f,
229 0.0f,0.0f
230 };
231 #else
232 static VertexAttrib currentVertexAttribInit = {0.0f,0.0f,0.0f,0.0f,
233 255,255,255,255,
234 0.0f,0.0f,
235 };
236 #endif
237 static GLushort* ptrIndexArray = NULL;
238
239 static GLboolean arraysValid = GL_FALSE;
240
241 void InitGLStructs()
242 {
243 ptrVertexAttribArray = vertexattribs;
244 ptrVertexAttribArrayMark = ptrVertexAttribArray;
245 ptrIndexArray = indexArray;
246
247 memcpy(&nanoglState, &nanoglInitState, sizeof(struct nanoState));
248 memcpy(&tmuState0,&tmuInitState,sizeof(struct nanotmuState));
249 memcpy(&tmuState1,&tmuInitState,sizeof(struct nanotmuState));
250 memcpy(&currentVertexAttrib,&currentVertexAttribInit,sizeof(struct VertexAttrib));
251
252 activetmuState = &tmuState0;
253 wrapperPrimitiveMode = GL_QUADS;
254 useTexCoordArray = GL_FALSE;
255 activetmu = GL_TEXTURE0;
256 clientactivetmu = GL_TEXTURE0;
257 delayedttmuchange = GL_FALSE;
258 delayedtmutarget = GL_TEXTURE0;
259 vertexCount = 0;
260 indexCount = 0;
261 vertexMark = 0;
262 indexbase = 0;
263 arraysValid = GL_FALSE;
264 }
265
266 void ResetNanoState()
267 {
268
269 if (tmuState0.color_array.enabled)
270 {
271 glEsImpl->glEnableClientState(GL_COLOR_ARRAY);
272 }
273 else
274 {
275 glEsImpl->glDisableClientState(GL_COLOR_ARRAY);
276 }
277
278 if (tmuState0.vertex_array.enabled)
279 {
280 glEsImpl->glEnableClientState(GL_VERTEX_ARRAY);
281 }
282 else
283 {
284 glEsImpl->glDisableClientState(GL_VERTEX_ARRAY);
285 }
286
287 if (tmuState0.texture_coord_array.enabled)
288 {
289 glEsImpl->glEnableClientState(GL_TEXTURE_COORD_ARRAY);
290 }
291 else
292 {
293 glEsImpl->glDisableClientState(GL_TEXTURE_COORD_ARRAY);
294 }
295
296 if (tmuState0.normal_array.enabled)
297 {
298 glEsImpl->glEnableClientState(GL_NORMAL_ARRAY);
299 }
300 else
301 {
302 glEsImpl->glDisableClientState(GL_NORMAL_ARRAY);
303 }
304 glEsImpl->glVertexPointer(tmuState0.vertex_array.size,
305 tmuState0.vertex_array.type,
306 tmuState0.vertex_array.stride,
307 tmuState0.vertex_array.ptr);
308
309 glEsImpl->glTexCoordPointer(tmuState0.texture_coord_array.size,
310 tmuState0.texture_coord_array.type,
311 tmuState0.texture_coord_array.stride,
312 tmuState0.texture_coord_array.ptr);
313
314 glEsImpl->glColorPointer(tmuState0.color_array.size,
315 tmuState0.color_array.type,
316 tmuState0.color_array.stride,
317 tmuState0.color_array.ptr);
318
319 glEsImpl->glNormalPointer(
320 tmuState0.normal_array.type,
321 tmuState0.normal_array.stride,
322 tmuState0.normal_array.ptr);
323
324 glEsImpl->glMatrixMode(nanoglState.matrixmode);
325
326
327
328 glEsImpl->glColor4f (currentVertexAttrib.red, currentVertexAttrib.green, currentVertexAttrib.blue, currentVertexAttrib.alpha);
329
330 glEsImpl->glBlendFunc(nanoglState.sfactor, nanoglState.dfactor);
331
332 //glEsImpl->glBindTexture(GL_TEXTURE_2D, stackTextureState);
333
334 glEsImpl->glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, activetmuState->texture_env_mode.value);
335
336 arraysValid = GL_FALSE;
337 }
338
339 void FlushOnStateChange()
340 {
341 if (delayedttmuchange)
342 {
343 delayedttmuchange = GL_FALSE;
344 #ifndef USE_CORE_PROFILE
345 glEsImpl->glActiveTexture(delayedtmutarget);
346 #endif
347 }
348
349 if (!vertexCount)
350 return;
351
352 if (!arraysValid)
353 {
354 glEsImpl->glClientActiveTexture(GL_TEXTURE0);
355 glEsImpl->glVertexPointer(3, GL_FLOAT, sizeof(VertexAttrib), &vertexattribs[0].x);
356 glEsImpl->glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(VertexAttrib), &vertexattribs[0].red);
357 glEsImpl->glTexCoordPointer(2, GL_FLOAT, sizeof(VertexAttrib), &vertexattribs[0].s);
358 glEsImpl->glEnableClientState(GL_VERTEX_ARRAY);
359 glEsImpl->glEnableClientState(GL_TEXTURE_COORD_ARRAY);
360 glEsImpl->glEnableClientState(GL_COLOR_ARRAY);
361 #if defined(__MULTITEXTURE_SUPPORT__)
362 glEsImpl->glClientActiveTexture(GL_TEXTURE1);
363 glEsImpl->glTexCoordPointer(2, GL_FLOAT, sizeof(VertexAttrib), &vertexattribs[0].s_multi);
364 glEsImpl->glEnableClientState(GL_TEXTURE_COORD_ARRAY);
365 glEsImpl->glClientActiveTexture(GL_TEXTURE0);
366 #endif
367 arraysValid = GL_TRUE;
368 }
369
370 glEsImpl->glDrawElements( GL_TRIANGLES,vertexCount,GL_UNSIGNED_SHORT, indexArray );
371
372
373 #if defined(__MULTITEXTURE_SUPPORT__)
374 useMultiTexCoordArray = GL_FALSE;
375 #endif
376 vertexCount = 0;
377 indexCount = 0;
378 ptrVertexAttribArray = vertexattribs;
379 ptrVertexAttribArrayMark = ptrVertexAttribArray;
380 ptrIndexArray = indexArray;
381 useTexCoordArray = GL_FALSE;
382 }
383 void nanoGL_Flush()
384 {
385 FlushOnStateChange();
386 }
387 void nanoGL_Reset()
388 {
389 ResetNanoState();
390 }
391 void glBegin(GLenum mode)
392 {
393 wrapperPrimitiveMode = mode;
394 vertexMark = vertexCount;
395 ptrVertexAttribArrayMark = ptrVertexAttribArray;
396 indexbase = indexCount;
397 }
398
399 void glEnd(void)
400 {
401 vertexCount+=((unsigned char*)ptrVertexAttribArray-(unsigned char*)ptrVertexAttribArrayMark)/sizeof(VertexAttrib);
402 if (vertexCount < 3)
403 {
404 return;
405 }
406 switch (wrapperPrimitiveMode)
407 {
408 case GL_QUADS:
409 {
410 *ptrIndexArray++ = indexCount;
411 *ptrIndexArray++ = indexCount+1;
412 *ptrIndexArray++ = indexCount+2;
413 *ptrIndexArray++ = indexCount;
414 *ptrIndexArray++ = indexCount+2;
415 *ptrIndexArray++ = indexCount+3;
416 indexCount+=4;
417 vertexCount+=2;
418 }
419 break;
420 case GL_TRIANGLES:
421 {
422 int vcount = (vertexCount-vertexMark)/3;
423 for (int count = 0; count < vcount; count++)
424 {
425 *ptrIndexArray++ = indexCount;
426 *ptrIndexArray++ = indexCount+1;
427 *ptrIndexArray++ = indexCount+2;
428 indexCount+=3;
429 }
430 }
431 break;
432 case GL_TRIANGLE_STRIP:
433 {
434 *ptrIndexArray++ = indexCount;
435 *ptrIndexArray++ = indexCount+1;
436 *ptrIndexArray++ = indexCount+2;
437 indexCount+=3;
438 int vcount = ((vertexCount-vertexMark)-3);
439 if (vcount && ((long)ptrIndexArray & 0x02))
440 {
441 *ptrIndexArray++ = indexCount-1; // 2
442 *ptrIndexArray++ = indexCount-2; // 1
443 *ptrIndexArray++ = indexCount; // 3
444 indexCount++;
445 vcount-=1;
446 int odd = vcount&1;
447 vcount/=2;
448 unsigned int* longptr = (unsigned int*) ptrIndexArray;
449
450 for (int count = 0; count < vcount; count++)
451 {
452 *(longptr++) = (indexCount-2) | ((indexCount-1)<<16);
453 *(longptr++) = (indexCount) | ((indexCount)<<16);
454 *(longptr++) = (indexCount-1) | ((indexCount+1)<<16);
455 indexCount+=2;
456 }
457 ptrIndexArray = (unsigned short*)(longptr);
458 if (odd)
459 {
460 *ptrIndexArray++ = indexCount-2; // 2
461 *ptrIndexArray++ = indexCount-1; // 1
462 *ptrIndexArray++ = indexCount; // 3
463 indexCount++;
464 }
465 }
466 else
467 {
468 //already aligned
469 int odd = vcount&1;
470 vcount/=2;
471 unsigned int* longptr = (unsigned int*) ptrIndexArray;
472
473 for (int count = 0; count < vcount; count++)
474 {
475 *(longptr++) = (indexCount-1) | ((indexCount-2)<<16);
476 *(longptr++) = (indexCount) | ((indexCount-1)<<16);
477 *(longptr++) = (indexCount) | ((indexCount+1)<<16);
478 indexCount+=2;
479
480 }
481 ptrIndexArray = (unsigned short*)(longptr);
482 if (odd)
483 {
484
485 *ptrIndexArray++ = indexCount-1; // 2
486 *ptrIndexArray++ = indexCount-2; // 1
487 *ptrIndexArray++ = indexCount; // 3
488 indexCount++;
489 }
490 }
491 vertexCount+=(vertexCount-vertexMark-3)*2;
492 }
493 break;
494 case GL_POLYGON:
495 case GL_TRIANGLE_FAN:
496 {
497 *ptrIndexArray++ = indexCount++;
498 *ptrIndexArray++ = indexCount++;
499 *ptrIndexArray++ = indexCount++;
500 int vcount = ((vertexCount-vertexMark)-3);
501 for (int count = 0; count < vcount; count++)
502 {
503 *ptrIndexArray++ = indexbase;
504 *ptrIndexArray++ = indexCount-1;
505 *ptrIndexArray++ = indexCount++;
506 vertexCount+=2;
507 }
508 }
509 break;
510
511 default:
512 break;
513 }
514 if( ptrVertexAttribArray - vertexattribs > 20000 * sizeof(VertexAttrib) ||
515 ptrIndexArray - indexArray > 15000 * sizeof(GLushort) )
516 FlushOnStateChange();
517 }
518
519 void glEnable (GLenum cap)
520 {
521 GLboolean statechanged = GL_FALSE;
522 switch(cap)
523 {
524 case GL_ALPHA_TEST:
525 {
526 if (!nanoglState.alpha_test)
527 {
528 nanoglState.alpha_test = GL_TRUE;
529 statechanged = GL_TRUE;
530 }
531 break;
532 }
533 case GL_BLEND:
534 {
535 if (!nanoglState.blend)
536 {
537 nanoglState.blend = GL_TRUE;
538 statechanged = GL_TRUE;
539 }
540 break;
541 }
542 //case GL_CLIP_PLANEi
543 case GL_COLOR_LOGIC_OP:
544 {
545 if (!nanoglState.color_logic_op)
546 {
547 nanoglState.color_logic_op = GL_TRUE;
548 statechanged = GL_TRUE;
549 }
550 break;
551 }
552 case GL_COLOR_MATERIAL:
553 {
554 if (!nanoglState.color_material)
555 {
556 nanoglState.color_material = GL_TRUE;
557 statechanged = GL_TRUE;
558 }
559 break;
560 }
561 case GL_CULL_FACE:
562 {
563 if (!nanoglState.cull_face)
564 {
565 nanoglState.cull_face = GL_TRUE;
566 statechanged = GL_TRUE;
567 }
568 break;
569 }
570 case GL_DEPTH_TEST:
571 {
572 if (!nanoglState.depth_test)
573 {
574 nanoglState.depth_test = GL_TRUE;
575 statechanged = GL_TRUE;
576 }
577 break;
578 }
579 case GL_DITHER:
580 {
581 if (!nanoglState.dither)
582 {
583 nanoglState.dither = GL_TRUE;
584 statechanged = GL_TRUE;
585 }
586 break;
587 }
588 case GL_FOG:
589 //case GL_LIGHTi
590 {
591 if (!nanoglState.fog)
592 {
593 nanoglState.fog = GL_TRUE;
594 statechanged = GL_TRUE;
595 }
596 break;
597 }
598 case GL_LIGHTING:
599 {
600 if (!nanoglState.lighting)
601 {
602 nanoglState.lighting = GL_TRUE;
603 statechanged = GL_TRUE;
604 }
605 break;
606 }
607 case GL_LINE_SMOOTH:
608 {
609 if (!nanoglState.line_smooth)
610 {
611 nanoglState.line_smooth = GL_TRUE;
612 statechanged = GL_TRUE;
613 }
614 break;
615 }
616 /* case GL_MATRIX_PALETTE_OES:
617 {
618 if (!nanoglState.matrix_palette_oes)
619 {
620 nanoglState.matrix_palette_oes = GL_TRUE;
621 statechanged = GL_TRUE;
622 }
623 break;
624 }*/
625 case GL_MULTISAMPLE:
626 {
627 if (!nanoglState.multisample)
628 {
629 nanoglState.multisample = GL_TRUE;
630 statechanged = GL_TRUE;
631 }
632 break;
633 }
634 case GL_NORMALIZE:
635 {
636 if (!nanoglState.normalize)
637 {
638 nanoglState.normalize = GL_TRUE;
639 statechanged = GL_TRUE;
640 }
641 break;
642 }
643 /* case GL_POINT_SPRITE_OES:
644 {
645 if (!nanoglState.point_sprite_oes)
646 {
647 nanoglState.point_sprite_oes = GL_TRUE;
648 statechanged = GL_TRUE;
649 }
650 break;
651 }*/
652 case GL_POLYGON_OFFSET_FILL:
653 {
654 if (!nanoglState.polygon_offset_fill)
655 {
656 nanoglState.polygon_offset_fill = GL_TRUE;
657 statechanged = GL_TRUE;
658 }
659 break;
660 }
661 case GL_RESCALE_NORMAL:
662 {
663 if (!nanoglState.rescale_normal)
664 {
665 nanoglState.rescale_normal = GL_TRUE;
666 statechanged = GL_TRUE;
667 }
668 break;
669 }
670 case GL_SAMPLE_ALPHA_TO_COVERAGE:
671 {
672 if (!nanoglState.sample_alpha_to_coverage)
673 {
674 nanoglState.sample_alpha_to_coverage = GL_TRUE;
675 statechanged = GL_TRUE;
676 }
677 break;
678 }
679 case GL_SAMPLE_ALPHA_TO_ONE:
680 {
681 if (!nanoglState.sample_alpha_to_one)
682 {
683 nanoglState.sample_alpha_to_one = GL_TRUE;
684 statechanged = GL_TRUE;
685 }
686 break;
687 }
688 case GL_SAMPLE_COVERAGE:
689 {
690 if (!nanoglState.sample_coverage)
691 {
692 nanoglState.sample_coverage = GL_TRUE;
693 statechanged = GL_TRUE;
694 }
695 break;
696 }
697 case GL_SCISSOR_TEST:
698 {
699 if (!nanoglState.scissor_test)
700 {
701 nanoglState.scissor_test = GL_TRUE;
702 statechanged = GL_TRUE;
703 }
704 break;
705 }
706 case GL_STENCIL_TEST:
707 {
708 return;
709 /* if (!nanoglState.stencil_test)
710 {
711 nanoglState.stencil_test = GL_TRUE;
712 statechanged = GL_TRUE;
713 }*/
714 break;
715 }
716 case GL_TEXTURE_2D:
717 {
718 if (!activetmuState->texture_2d.value)
719 {
720 FlushOnStateChange();
721 glEsImpl->glEnable(cap);
722 activetmuState->texture_2d.value = GL_TRUE;
723 return;
724 }
725 break;
726 }
727 default:
728 break;
729 }
730
731 if (statechanged)
732 {
733 FlushOnStateChange();
734 glEsImpl->glEnable(cap);
735 }
736 }
737
738 void glDisable (GLenum cap)
739 {
740 GLboolean statechanged = GL_FALSE;
741 switch(cap)
742 {
743 case GL_ALPHA_TEST:
744 {
745 if (nanoglState.alpha_test)
746 {
747 nanoglState.alpha_test = GL_FALSE;
748 statechanged = GL_TRUE;
749 }
750 break;
751 }
752 case GL_BLEND:
753 {
754 if (nanoglState.blend)
755 {
756 nanoglState.blend = GL_FALSE;
757 statechanged = GL_TRUE;
758 }
759 break;
760 }
761 //case GL_CLIP_PLANEi
762 case GL_COLOR_LOGIC_OP:
763 {
764 if (nanoglState.color_logic_op)
765 {
766 nanoglState.color_logic_op = GL_FALSE;
767 statechanged = GL_TRUE;
768 }
769 break;
770 }
771 case GL_COLOR_MATERIAL:
772 {
773 if (nanoglState.color_material)
774 {
775 nanoglState.color_material = GL_FALSE;
776 statechanged = GL_TRUE;
777 }
778 break;
779 }
780 case GL_CULL_FACE:
781 {
782 if (nanoglState.cull_face)
783 {
784 nanoglState.cull_face = GL_FALSE;
785 statechanged = GL_TRUE;
786 }
787 break;
788 }
789 case GL_DEPTH_TEST:
790 {
791 if (nanoglState.depth_test)
792 {
793 nanoglState.depth_test = GL_FALSE;
794 statechanged = GL_TRUE;
795 }
796 break;
797 }
798 case GL_DITHER:
799 {
800 if (nanoglState.dither)
801 {
802 nanoglState.dither = GL_FALSE;
803 statechanged = GL_TRUE;
804 }
805 break;
806 }
807 case GL_FOG:
808 //case GL_LIGHTi
809 {
810 if (nanoglState.fog)
811 {
812 nanoglState.fog = GL_FALSE;
813 statechanged = GL_TRUE;
814 }
815 break;
816 }
817 case GL_LIGHTING:
818 {
819 if (nanoglState.lighting)
820 {
821 nanoglState.lighting = GL_FALSE;
822 statechanged = GL_TRUE;
823 }
824 break;
825 }
826 case GL_LINE_SMOOTH:
827 {
828 if (nanoglState.line_smooth)
829 {
830 nanoglState.line_smooth = GL_FALSE;
831 statechanged = GL_TRUE;
832 }
833 break;
834 }
835 /* case GL_MATRIX_PALETTE_OES:
836 {
837 if (nanoglState.matrix_palette_oes)
838 {
839 nanoglState.matrix_palette_oes = GL_FALSE;
840 statechanged = GL_TRUE;
841 }
842 break;
843 }*/
844 case GL_MULTISAMPLE:
845 {
846 if (nanoglState.multisample)
847 {
848 nanoglState.multisample = GL_FALSE;
849 statechanged = GL_TRUE;
850 }
851 break;
852 }
853 case GL_NORMALIZE:
854 {
855 if (nanoglState.normalize)
856 {
857 nanoglState.normalize = GL_FALSE;
858 statechanged = GL_TRUE;
859 }
860 break;
861 }
862 /* case GL_POINT_SPRITE_OES:
863 {
864 if (nanoglState.point_sprite_oes)
865 {
866 nanoglState.point_sprite_oes = GL_FALSE;
867 statechanged = GL_TRUE;
868 }
869 break;
870 }*/
871 case GL_POLYGON_OFFSET_FILL:
872 {
873 if (nanoglState.polygon_offset_fill)
874 {
875 nanoglState.polygon_offset_fill = GL_FALSE;
876 statechanged = GL_TRUE;
877 }
878 break;
879 }
880 case GL_RESCALE_NORMAL:
881 {
882 if (nanoglState.rescale_normal)
883 {
884 nanoglState.rescale_normal = GL_FALSE;
885 statechanged = GL_TRUE;
886 }
887 break;
888 }
889 case GL_SAMPLE_ALPHA_TO_COVERAGE:
890 {
891 if (nanoglState.sample_alpha_to_coverage)
892 {
893 nanoglState.sample_alpha_to_coverage = GL_FALSE;
894 statechanged = GL_TRUE;
895 }
896 break;
897 }
898 case GL_SAMPLE_ALPHA_TO_ONE:
899 {
900 if (nanoglState.sample_alpha_to_one)
901 {
902 nanoglState.sample_alpha_to_one = GL_FALSE;
903 statechanged = GL_TRUE;
904 }
905 break;
906 }
907 case GL_SAMPLE_COVERAGE:
908 {
909 if (nanoglState.sample_coverage)
910 {
911 nanoglState.sample_coverage = GL_FALSE;
912 statechanged = GL_TRUE;
913 }
914 break;
915 }
916 case GL_SCISSOR_TEST:
917 {
918 if (nanoglState.scissor_test)
919 {
920 nanoglState.scissor_test = GL_FALSE;
921 statechanged = GL_TRUE;
922 }
923 break;
924 }
925 case GL_STENCIL_TEST:
926 {
927 return;
928 /* if (nanoglState.stencil_test)
929 {
930 nanoglState.stencil_test = GL_FALSE;
931 statechanged = GL_TRUE;
932 }*/
933 break;
934 }
935 case GL_TEXTURE_2D:
936 {
937 if (activetmuState->texture_2d.value)
938 {
939 FlushOnStateChange();
940 glEsImpl->glDisable(cap);
941 activetmuState->texture_2d.value = GL_FALSE;
942 return;
943
944
945 }
946 break;
947 }
948 default:
949 break;
950 }
951
952 if (statechanged)
953 {
954 FlushOnStateChange();
955 glEsImpl->glDisable(cap);
956 }
957 }
958
959 void glVertex2f(GLfloat x, GLfloat y)
960 {
961 glVertex3f(x,y,0.0f);
962 }
963
964
965 __FORCEINLINE unsigned int ClampTo255(float value)
966 {
967 unsigned int retval = (unsigned int)(value);
968 if (retval > 255)
969 {
970 retval = 255;
971 }
972 return retval;
973 }
974
975 void glColor3f( GLfloat red, GLfloat green, GLfloat blue)
976 {
977 currentVertexAttrib.red = (unsigned char)ClampTo255(red*255.0f);
978 currentVertexAttrib.green = (unsigned char)ClampTo255(green*255.0f);
979 currentVertexAttrib.blue = (unsigned char)ClampTo255(blue*255.0f);
980 currentVertexAttrib.alpha = 255;
981 }
982
983
984 void glTexCoord2fv( const GLfloat *v )
985 {
986 memcpy(&currentVertexAttrib.s, v, 2*sizeof(float));
987 }
988
989
990 void glTexCoord2f(GLfloat s, GLfloat t)
991 {
992 currentVertexAttrib.s = s;
993 currentVertexAttrib.t = t;
994 }
995
996 void glViewport (GLint x, GLint y, GLsizei width, GLsizei height)
997 {
998 FlushOnStateChange();
999 glEsImpl->glViewport(x,y,width,height);
1000 }
1001
1002 void glLoadIdentity (void)
1003 {
1004 FlushOnStateChange();
1005 glEsImpl->glLoadIdentity();
1006 }
1007
1008 void glColor4f (GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
1009 {
1010 currentVertexAttrib.red = (unsigned char)ClampTo255(red*255.0f);
1011 currentVertexAttrib.green = (unsigned char)ClampTo255(green*255.0f);
1012 currentVertexAttrib.blue = (unsigned char)ClampTo255(blue*255.0f);
1013 currentVertexAttrib.alpha = (unsigned char)ClampTo255(alpha*255.0f);
1014 }
1015
1016 void glOrtho (GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat zNear, GLfloat zFar)
1017 {
1018 FlushOnStateChange();
1019 #ifdef USE_CORE_PROFILE
1020 glEsImpl->glOrtho(left,right,bottom,top, zNear,zFar);
1021 #else
1022 glEsImpl->glOrthof(left,right,bottom,top, zNear,zFar);
1023 #endif
1024 }
1025
1026 void glMatrixMode (GLenum mode)
1027 {
1028 if (nanoglState.matrixmode == mode)
1029 {
1030 return;
1031 }
1032 nanoglState.matrixmode = mode;
1033 FlushOnStateChange();
1034 glEsImpl->glMatrixMode(mode);
1035 }
1036
1037 void glTexParameterf (GLenum target, GLenum pname, GLfloat param)
1038 {
1039 if (pname == GL_TEXTURE_BORDER_COLOR)
1040 {
1041 return; // not supported by opengl es
1042 }
1043 if ( (pname == GL_TEXTURE_WRAP_S ||
1044 pname == GL_TEXTURE_WRAP_T) &&
1045 param == GL_CLAMP)
1046 {
1047 param = 0x812F;
1048 }
1049
1050 FlushOnStateChange();
1051 glEsImpl->glTexParameterf(target, pname,param);
1052 }
1053
1054 void glTexParameterfv( GLenum target, GLenum pname, const GLfloat *params)
1055 {
1056 glTexParameterf(target, pname, params[0]);
1057 }
1058
1059 void glTexImage2D (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels)
1060 {
1061 FlushOnStateChange();
1062 internalformat = format;
1063 glEsImpl->glTexImage2D(target, level, internalformat, width, height,border,format,type,pixels);
1064 }
1065
1066 void glDrawBuffer(GLenum /*mode*/)
1067 {
1068 }
1069
1070 void glTranslatef (GLfloat x, GLfloat y, GLfloat z)
1071 {
1072 FlushOnStateChange();
1073 glEsImpl->glTranslatef(x,y,z);
1074 }
1075
1076 void glRotatef (GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
1077 {
1078 FlushOnStateChange();
1079 glEsImpl->glRotatef(angle, x, y, z);
1080 }
1081
1082 void glScalef (GLfloat x, GLfloat y, GLfloat z)
1083 {
1084 FlushOnStateChange();
1085 glEsImpl->glScalef(x,y,z);
1086 }
1087
1088 void glDepthRange(GLclampf zNear, GLclampf zFar)
1089 {
1090 if ((nanoglState.depth_range_near == zNear) &&(nanoglState.depth_range_far == zFar))
1091 {
1092 return;
1093 }
1094 else
1095 {
1096 nanoglState.depth_range_near = zNear;
1097 nanoglState.depth_range_far = zFar;
1098 }
1099 FlushOnStateChange();
1100 #ifdef USE_CORE_PROFILE
1101 glEsImpl->glDepthRange(zNear, zFar);
1102 #else
1103 glEsImpl->glDepthRangef(zNear, zFar);
1104 #endif
1105 }
1106
1107 void glDepthFunc (GLenum func)
1108 {
1109 if (nanoglState.depth_func == func)
1110 {
1111 return;
1112 }
1113 else
1114 {
1115 nanoglState.depth_func = func;
1116 }
1117 FlushOnStateChange();
1118 glEsImpl->glDepthFunc(func);
1119 }
1120
1121 void glFinish (void)
1122 {
1123 FlushOnStateChange();
1124 glEsImpl->glFinish();
1125 }
1126
1127 void glGetFloatv (GLenum pname, GLfloat *params)
1128 {
1129 FlushOnStateChange();
1130 glEsImpl->glGetFloatv(pname, params);
1131 }
1132
1133 void glCullFace (GLenum mode)
1134 {
1135 if (nanoglState.cullface == mode)
1136 {
1137 return;
1138 }
1139 else
1140 {
1141 nanoglState.cullface = mode;
1142 }
1143 FlushOnStateChange();
1144 glEsImpl->glCullFace(mode);
1145 }
1146
1147 void glFrustum (GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat zNear, GLfloat zFar)
1148 {
1149 FlushOnStateChange();
1150 glEsImpl->glFrustumf(left,right,bottom,top,zNear,zFar);
1151 }
1152
1153 void glClear (GLbitfield mask)
1154 {
1155 FlushOnStateChange();
1156 glEsImpl->glClear(mask);
1157 }
1158
1159
1160 void glVertex3f( GLfloat x, GLfloat y, GLfloat z )
1161 {
1162 GLfloat* vert = (GLfloat*)ptrVertexAttribArray++;
1163 *vert++ = x;
1164 *vert++ = y;
1165 *vert++ = z;
1166 #if defined(__MULTITEXTURE_SUPPORT__)
1167 memcpy(vert, &currentVertexAttrib.red, 5*sizeof(GLfloat));
1168 #else
1169 memcpy(vert+1, &currentVertexAttrib.red, 3*sizeof(GLfloat));
1170 #endif
1171
1172 }
1173
1174 void glColor4fv( const GLfloat *v )
1175 {
1176 currentVertexAttrib.red = (unsigned char)ClampTo255(v[0]*255.0f);
1177 currentVertexAttrib.green = (unsigned char)ClampTo255(v[1]*255.0f);
1178 currentVertexAttrib.blue = (unsigned char)ClampTo255(v[2]*255.0f);
1179 currentVertexAttrib.alpha = (unsigned char)ClampTo255(v[3]*255.0f);
1180 }
1181
1182 void glColor3ubv( const GLubyte* v)
1183 {
1184 currentVertexAttrib.red = v[0];
1185 currentVertexAttrib.green = v[1];
1186 currentVertexAttrib.blue = v[2];
1187 currentVertexAttrib.alpha = 255;
1188 }
1189
1190 void glColor4ubv( const GLubyte *v )
1191 {
1192 //*((unsigned int*)(&currentVertexAttrib.red)) = *((unsigned int*)(v));
1193 currentVertexAttrib.red = v[0];
1194 currentVertexAttrib.green = v[1];
1195 currentVertexAttrib.blue = v[2];
1196 currentVertexAttrib.alpha = v[3];
1197 }
1198
1199 void glColor3fv( const GLfloat *v )
1200 {
1201 currentVertexAttrib.red = (unsigned char)ClampTo255(v[0]*255.0f);
1202 currentVertexAttrib.green = (unsigned char)ClampTo255(v[1]*255.0f);
1203 currentVertexAttrib.blue = (unsigned char)ClampTo255(v[2]*255.0f);
1204 currentVertexAttrib.alpha = 255;
1205 }
1206
1207 //-- nicknekit: xash3d funcs --
1208
1209 void glColor4ub( GLubyte red, GLubyte green, GLubyte blue, GLubyte alpha)
1210 {
1211 currentVertexAttrib.red = red;
1212 currentVertexAttrib.green = green;
1213 currentVertexAttrib.blue = blue;
1214 currentVertexAttrib.alpha = alpha;
1215 }
1216
1217 void glColor3ub( GLubyte red, GLubyte green, GLubyte blue)
1218 {
1219 currentVertexAttrib.red = red;
1220 currentVertexAttrib.green = green;
1221 currentVertexAttrib.blue = blue;
1222 currentVertexAttrib.alpha = 255;
1223 }
1224
1225 void glNormal3fv( const GLfloat *v )
1226 {
1227 FlushOnStateChange();
1228 glEsImpl->glNormal3f(v[0],v[1],v[2]);
1229 }
1230
1231 void glCopyTexImage2D( GLenum target, GLint level, GLenum internalformat, GLint x, GLint y, GLsizei width, GLsizei height, GLint border )
1232 {
1233 FlushOnStateChange();
1234 glEsImpl->glCopyTexImage2D(target, level, internalformat, x, y, width, height, border);
1235 }
1236
1237 void glTexImage1D(GLenum target, GLint level, GLint internalformat, GLsizei width, GLint border, GLenum format, GLenum type, const GLvoid *pixels)
1238 {
1239 glTexImage2D(GL_TEXTURE_2D, level, internalformat, width, 1, border, format, type, pixels);
1240 }
1241
1242 void glTexImage3D(GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid *pixels)
1243 {
1244 glTexImage2D(GL_TEXTURE_2D, level, internalformat, width, height, border, format, type, pixels);
1245 }
1246
1247 void glTexSubImage1D( GLenum target, GLint level, GLint xoffset, GLsizei width, GLenum format, GLenum type, const GLvoid *pixels )
1248 {
1249 glTexSubImage2D(target,level,xoffset,0,width,1,format,type,pixels);
1250 }
1251
1252 void glTexSubImage3D( GLenum target, GLint level,
1253 GLint xoffset, GLint yoffset,
1254 GLint zoffset, GLsizei width,
1255 GLsizei height, GLsizei depth,
1256 GLenum format,
1257 GLenum type, const GLvoid *pixels)
1258 {
1259 glTexSubImage2D(target,level,xoffset,yoffset,width,height,format,type,pixels);
1260 }
1261
1262
1263
1264 GLboolean glIsTexture(GLuint texture)
1265 {
1266 FlushOnStateChange();
1267 return glEsImpl->glIsTexture(texture);
1268 }
1269
1270
1271 void glTexGeni( GLenum coord, GLenum pname, GLint param )
1272 {
1273 //for mirrors? not needed for original hl?
1274 }
1275
1276 void glTexGenfv( GLenum coord, GLenum pname, const GLfloat *params )
1277 {
1278 //for mirrors? not needed for original hl?
1279 }
1280
1281 //-- --//
1282
1283 void glHint (GLenum target, GLenum mode)
1284 {
1285 FlushOnStateChange();
1286 glEsImpl->glHint(target, mode);
1287 }
1288
1289 void glBlendFunc (GLenum sfactor, GLenum dfactor)
1290 {
1291 if ((nanoglState.sfactor == sfactor) && (nanoglState.dfactor == dfactor))
1292 {
1293 return;
1294 }
1295 nanoglState.sfactor = sfactor;
1296 nanoglState.dfactor = dfactor;
1297 FlushOnStateChange();
1298 glEsImpl->glBlendFunc(sfactor, dfactor);
1299 }
1300
1301 void glPopMatrix (void)
1302 {
1303 FlushOnStateChange();
1304 glEsImpl->glPopMatrix();
1305 }
1306
1307 void glShadeModel (GLenum mode)
1308 {
1309 if (nanoglState.shademodel == mode)
1310 {
1311 return;
1312 }
1313 nanoglState.shademodel = mode;
1314 FlushOnStateChange();
1315 glEsImpl->glShadeModel(mode);
1316 }
1317
1318 void glPushMatrix (void)
1319 {
1320 FlushOnStateChange();
1321 glEsImpl->glPushMatrix();
1322 }
1323
1324 void glTexEnvf (GLenum target, GLenum pname, GLfloat param)
1325 {
1326 if (target == GL_TEXTURE_ENV)
1327 {
1328 if (pname == GL_TEXTURE_ENV_MODE)
1329 {
1330 if (param == activetmuState->texture_env_mode.value)
1331 {
1332 return;
1333 }
1334 else
1335 {
1336 FlushOnStateChange();
1337 glEsImpl->glTexEnvf(target, pname, param);
1338 activetmuState->texture_env_mode.value = param;
1339 return;
1340 }
1341 }
1342 }
1343 FlushOnStateChange();
1344 glEsImpl->glTexEnvf(target, pname, param);
1345 }
1346
1347 void glVertex3fv( const GLfloat *v )
1348 {
1349 GLfloat* vert = (GLfloat*)ptrVertexAttribArray++;
1350 memcpy(vert, v, 3*sizeof(GLfloat));
1351 #if defined(__MULTITEXTURE_SUPPORT__)
1352 memcpy(vert+3, &currentVertexAttrib.red, 5*sizeof(GLfloat));
1353 #else
1354 memcpy(vert+4, &currentVertexAttrib.red, 3*sizeof(GLfloat));
1355 #endif
1356 }
1357
1358
1359 void glDepthMask (GLboolean flag)
1360 {
1361 if (nanoglState.depthmask == flag)
1362 {
1363 return;
1364 }
1365 nanoglState.depthmask = flag;
1366 FlushOnStateChange();
1367 glEsImpl->glDepthMask(flag);
1368 }
1369
1370 void glBindTexture (GLenum target, GLuint texture)
1371 {
1372 if (activetmuState->boundtexture.value == texture)
1373 {
1374 return;
1375 }
1376 FlushOnStateChange();
1377 activetmuState->boundtexture.value = texture;
1378 glEsImpl->glBindTexture(target, texture);
1379 }
1380
1381
1382 void glGetIntegerv (GLenum pname, GLint *params)
1383 {
1384 FlushOnStateChange();
1385 glEsImpl->glGetIntegerv(pname, params);
1386 }
1387
1388 GLubyte nano_extensions_string[4096];
1389 const GLubyte* glGetString (GLenum name)
1390 {
1391
1392 if (name == GL_EXTENSIONS)
1393 {
1394 #if defined(__MULTITEXTURE_SUPPORT__)
1395 sprintf((char*)nano_extensions_string,"%s %s",glEsImpl->glGetString(name),"GL_ARB_multitexture EXT_texture_env_add");
1396 #else
1397 sprintf((char*)nano_extensions_string,"%s %s",glEsImpl->glGetString(name),"EXT_texture_env_add");
1398 #endif
1399 return nano_extensions_string;
1400 }
1401 return glEsImpl->glGetString(name);
1402 }
1403
1404 void glAlphaFunc (GLenum func, GLclampf ref)
1405 {
1406 FlushOnStateChange();
1407 glEsImpl->glAlphaFunc(func,ref);
1408 }
1409
1410 void glFlush (void)
1411 {
1412 FlushOnStateChange();
1413 glEsImpl->glFlush();
1414 }
1415
1416 void glReadPixels (GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *pixels)
1417 {
1418 if (format == GL_DEPTH_COMPONENT)
1419 {
1420 // OpenglEs 1.1 does not support reading depth buffer without an extension
1421 memset(pixels, 0xff,4);
1422 return;
1423 }
1424 FlushOnStateChange();
1425 glEsImpl->glReadPixels(x,y,width,height,format,type,pixels);
1426 }
1427
1428 void glReadBuffer( GLenum /*mode*/ )
1429 {
1430
1431 }
1432
1433 void glLoadMatrixf (const GLfloat *m)
1434 {
1435 FlushOnStateChange();
1436 glEsImpl->glLoadMatrixf(m);
1437 }
1438
1439 void glTexSubImage2D (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels)
1440 {
1441 FlushOnStateChange();
1442 glEsImpl->glTexSubImage2D(target,level,xoffset,yoffset,width,height,format,type,pixels);
1443 }
1444
1445 void glClearColor (GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
1446 {
1447 FlushOnStateChange();
1448 glEsImpl->glClearColor(red,green,blue,alpha);
1449 }
1450
1451 GLenum glGetError (void)
1452 {
1453 //FlushOnStateChange();
1454 return GL_NO_ERROR;//glEsImpl->glGetError();
1455 }
1456
1457 void glActiveTexture (GLenum texture)
1458 {
1459 if (activetmu == texture)
1460 {
1461 return;
1462 }
1463 if (delayedttmuchange)
1464 {
1465 delayedttmuchange = GL_FALSE;
1466 }
1467 else
1468 {
1469 delayedttmuchange = GL_TRUE;
1470 delayedtmutarget = texture;
1471 }
1472 if (texture == GL_TEXTURE0)
1473 {
1474 activetmuState = &tmuState0;
1475 }
1476 else
1477 {
1478 activetmuState = &tmuState1;
1479 }
1480 activetmu = texture;
1481 }
1482
1483 void glClientActiveTexture (GLenum texture)
1484 {
1485 clientactivetmu = texture;
1486 }
1487
1488 void glPolygonMode( GLenum face, GLenum mode )
1489 {
1490 }
1491
1492 void glDeleteTextures( GLsizei n, const GLuint *textures )
1493 {
1494 FlushOnStateChange();
1495 glEsImpl->glDeleteTextures(n,textures);
1496 }
1497
1498 void glClearDepth( GLclampf depth )
1499 {
1500 FlushOnStateChange();
1501 glEsImpl->glClearDepthf( depth );
1502 }
1503
1504 void glClipPlane( GLenum plane, const GLdouble *equation )
1505 {
1506 FlushOnStateChange();
1507 float array[4];
1508 array[0] = (GLfloat)(equation[0]);
1509 array[1] = (GLfloat)(equation[1]);
1510 array[2] = (GLfloat)(equation[2]);
1511 array[3] = (GLfloat)(equation[3]);
1512 glEsImpl->glClipPlanef( plane, array );
1513 }
1514
1515 void glScissor( GLint x, GLint y, GLsizei width, GLsizei height )
1516 {
1517 FlushOnStateChange();
1518 glEsImpl->glScissor( x, y, width,height);
1519 }
1520
1521 void glPointSize( GLfloat size )
1522 {
1523 FlushOnStateChange();
1524 glEsImpl->glPointSize( size );
1525 }
1526
1527 void glArrayElement(GLint i) {}
1528 void glLineWidth(GLfloat width) {}
1529 void glCallList( GLuint list ) {}
1530 void glColorMask( GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha ) {}
1531 void glStencilFunc( GLenum func, GLint ref, GLuint mask ) {}
1532 void glStencilOp( GLenum fail, GLenum zfail, GLenum zpass ) {}
1533
1534 struct ptrstate vertex_array;
1535 struct ptrstate color_array;
1536 struct ptrstate texture_coord_array;
1537
1538 void glDrawElements( GLenum mode, GLsizei count, GLenum type, const GLvoid *indices )
1539 {
1540 // ensure that all primitives specified between glBegin/glEnd pairs
1541 // are rendered first, and that we have correct tmu in use..
1542 FlushOnStateChange();
1543 // setup correct vertex/color/texcoord pointers
1544 if (arraysValid ||
1545 tmuState0.vertex_array.changed ||
1546 tmuState0.color_array.changed ||
1547 tmuState0.texture_coord_array.changed || tmuState0.normal_array.changed)
1548 {
1549 glEsImpl->glClientActiveTexture(GL_TEXTURE0);
1550 }
1551 if (arraysValid || tmuState0.vertex_array.changed)
1552 {
1553 if (tmuState0.vertex_array.enabled)
1554 {
1555 glEsImpl->glEnableClientState(GL_VERTEX_ARRAY);
1556 }
1557 else
1558 {
1559 glEsImpl->glDisableClientState(GL_VERTEX_ARRAY);
1560 }
1561 glEsImpl->glVertexPointer(tmuState0.vertex_array.size,
1562 tmuState0.vertex_array.type,
1563 tmuState0.vertex_array.stride,
1564 tmuState0.vertex_array.ptr);
1565 tmuState0.vertex_array.changed = GL_FALSE;
1566 }
1567 if (arraysValid || tmuState0.color_array.changed)
1568 {
1569 if (tmuState0.color_array.enabled)
1570 {
1571 glEsImpl->glEnableClientState(GL_COLOR_ARRAY);
1572 }
1573 else
1574 {
1575 glEsImpl->glDisableClientState(GL_COLOR_ARRAY);
1576 }
1577 glEsImpl->glColorPointer(tmuState0.color_array.size,
1578 tmuState0.color_array.type,
1579 tmuState0.color_array.stride,
1580 tmuState0.color_array.ptr);
1581 tmuState0.color_array.changed = GL_FALSE;
1582 }
1583 if (arraysValid || tmuState0.normal_array.changed)
1584 {
1585 if (tmuState0.normal_array.enabled)
1586 {
1587 glEsImpl->glEnableClientState(GL_NORMAL_ARRAY);
1588 }
1589 else
1590 {
1591 glEsImpl->glDisableClientState(GL_NORMAL_ARRAY);
1592 }
1593 glEsImpl->glNormalPointer(tmuState0.normal_array.type,
1594 tmuState0.normal_array.stride,
1595 tmuState0.normal_array.ptr);
1596 tmuState0.normal_array.changed = GL_FALSE;
1597 }
1598 if (arraysValid || tmuState0.texture_coord_array.changed)
1599 {
1600 tmuState0.texture_coord_array.changed = GL_FALSE;
1601 if (tmuState0.texture_coord_array.enabled)
1602 {
1603 glEsImpl->glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1604 }
1605 else
1606 {
1607 glEsImpl->glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1608 }
1609 glEsImpl->glTexCoordPointer(tmuState0.texture_coord_array.size,
1610 tmuState0.texture_coord_array.type,
1611 tmuState0.texture_coord_array.stride,
1612 tmuState0.texture_coord_array.ptr);
1613 }
1614
1615 if (arraysValid || tmuState1.texture_coord_array.changed)
1616 {
1617 tmuState1.texture_coord_array.changed = GL_FALSE;
1618 glEsImpl->glClientActiveTexture(GL_TEXTURE1);
1619 if (tmuState1.texture_coord_array.enabled)
1620 {
1621 glEsImpl->glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1622 }
1623 else
1624 {
1625 glEsImpl->glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1626 }
1627 glEsImpl->glTexCoordPointer(tmuState1.texture_coord_array.size,
1628 tmuState1.texture_coord_array.type,
1629 tmuState1.texture_coord_array.stride,
1630 tmuState1.texture_coord_array.ptr);
1631 }
1632
1633 arraysValid = GL_FALSE;
1634 glEsImpl->glDrawElements(mode, count, type, indices);
1635 }
1636
1637 void glEnableClientState(GLenum array)
1638 {
1639 struct nanotmuState* clientstate = NULL;
1640 if (clientactivetmu == GL_TEXTURE0)
1641 {
1642 clientstate = &tmuState0;
1643 }
1644 else if (clientactivetmu == GL_TEXTURE1)
1645 {
1646 clientstate = &tmuState1;
1647 }
1648 else
1649 {
1650 return;
1651 }
1652 switch (array)
1653 {
1654 case GL_VERTEX_ARRAY:
1655 if (clientstate->vertex_array.enabled)
1656 {
1657 return;
1658 }
1659 clientstate->vertex_array.enabled = GL_TRUE;
1660 clientstate->vertex_array.changed = GL_TRUE;
1661 break;
1662 case GL_COLOR_ARRAY:
1663 if (clientstate->color_array.enabled)
1664 {
1665 return;
1666 }
1667 clientstate->color_array.enabled = GL_TRUE;
1668 clientstate->color_array.changed = GL_TRUE;
1669
1670 break;
1671 case GL_NORMAL_ARRAY:
1672 if (clientstate->normal_array.enabled)
1673 {
1674 return;
1675 }
1676 clientstate->normal_array.enabled = GL_TRUE;
1677 clientstate->normal_array.changed = GL_TRUE;
1678
1679 break;
1680 case GL_TEXTURE_COORD_ARRAY:
1681 if (clientstate->texture_coord_array.enabled)
1682 {
1683 return;
1684 }
1685 clientstate->texture_coord_array.enabled = GL_TRUE;
1686 clientstate->texture_coord_array.changed = GL_TRUE;
1687 break;
1688 default:
1689 break;
1690 }
1691 }
1692 void glDisableClientState(GLenum array)
1693 {
1694 struct nanotmuState* clientstate = NULL;
1695 if (clientactivetmu == GL_TEXTURE0)
1696 {
1697 clientstate = &tmuState0;
1698 }
1699 else if (clientactivetmu == GL_TEXTURE1)
1700 {
1701 clientstate = &tmuState1;
1702 }
1703 else
1704 {
1705 return;
1706 }
1707 switch (array)
1708 {
1709 case GL_VERTEX_ARRAY:
1710 if (!clientstate->vertex_array.enabled)
1711 {
1712 return;
1713 }
1714 clientstate->vertex_array.enabled = GL_FALSE;
1715 clientstate->vertex_array.changed = GL_TRUE;
1716 break;
1717 case GL_COLOR_ARRAY:
1718 if (!clientstate->color_array.enabled)
1719 {
1720 return;
1721 }
1722 clientstate->color_array.enabled = GL_FALSE;
1723 clientstate->color_array.changed = GL_TRUE;
1724
1725 break;
1726 case GL_NORMAL_ARRAY:
1727 if (!clientstate->normal_array.enabled)
1728 {
1729 return;
1730 }
1731 clientstate->normal_array.enabled = GL_FALSE;
1732 clientstate->normal_array.changed = GL_TRUE;
1733
1734 break;
1735 case GL_TEXTURE_COORD_ARRAY:
1736 if (!clientstate->texture_coord_array.enabled)
1737 {
1738 return;
1739 }
1740 clientstate->texture_coord_array.enabled = GL_FALSE;
1741 clientstate->texture_coord_array.changed = GL_TRUE;
1742 break;
1743 default:
1744 break;
1745 }
1746 }
1747 void glVertexPointer( GLint size, GLenum type,GLsizei stride, const GLvoid *pointer )
1748 {
1749 if (tmuState0.vertex_array.size == size &&
1750 tmuState0.vertex_array.stride == stride &&
1751 tmuState0.vertex_array.type == type &&
1752 tmuState0.vertex_array.ptr == pointer)
1753 {
1754 return;
1755 }
1756 tmuState0.vertex_array.size = size;
1757 tmuState0.vertex_array.stride = stride;
1758 tmuState0.vertex_array.type = type;
1759 tmuState0.vertex_array.ptr = (GLvoid*)pointer;
1760 tmuState0.vertex_array.changed = GL_TRUE;
1761 }
1762 void glTexCoordPointer( GLint size, GLenum type, GLsizei stride, const GLvoid *pointer )
1763 {
1764 struct nanotmuState* clientstate = NULL;
1765 if (clientactivetmu == GL_TEXTURE0)
1766 {
1767 clientstate = &tmuState0;
1768 }
1769 else if (clientactivetmu == GL_TEXTURE1)
1770 {
1771 clientstate = &tmuState1;
1772 }
1773 if (clientstate->texture_coord_array.size == size &&
1774 clientstate->texture_coord_array.stride == stride &&
1775 clientstate->texture_coord_array.type == type &&
1776 clientstate->texture_coord_array.ptr == pointer)
1777 {
1778 return;
1779 }
1780 clientstate->texture_coord_array.size = size;
1781 clientstate->texture_coord_array.stride = stride;
1782 clientstate->texture_coord_array.type = type;
1783 clientstate->texture_coord_array.ptr = (GLvoid*)pointer;
1784 clientstate->texture_coord_array.changed = GL_TRUE;
1785 }
1786 void glColorPointer( GLint size, GLenum type, GLsizei stride, const GLvoid *pointer )
1787 {
1788 if (tmuState0.color_array.size == size &&
1789 tmuState0.color_array.stride == stride &&
1790 tmuState0.color_array.type == type &&
1791 tmuState0.color_array.ptr == pointer)
1792 {
1793 return;
1794 }
1795 tmuState0.color_array.size = size;
1796 tmuState0.color_array.stride = stride;
1797 tmuState0.color_array.type = type;
1798 tmuState0.color_array.ptr = (GLvoid*)pointer;
1799 tmuState0.color_array.changed = GL_TRUE;
1800 }
1801
1802 void glNormalPointer( GLenum type, GLsizei stride, const GLvoid *pointer )
1803 {
1804 int size = 0;
1805 if (tmuState0.normal_array.size == size &&
1806 tmuState0.normal_array.stride == stride &&
1807 tmuState0.normal_array.type == type &&
1808 tmuState0.normal_array.ptr == pointer)
1809 {
1810 return;
1811 }
1812 tmuState0.normal_array.size = size;
1813 tmuState0.normal_array.stride = stride;
1814 tmuState0.normal_array.type = type;
1815 tmuState0.normal_array.ptr = (GLvoid*)pointer;
1816 tmuState0.normal_array.changed = GL_TRUE;
1817 }
1818 void glPolygonOffset( GLfloat factor, GLfloat units )
1819 {
1820 FlushOnStateChange();
1821 glEsImpl->glPolygonOffset(factor, units);
1822 }
1823 void glStencilMask( GLuint mask ) {}
1824 void glClearStencil( GLint s ) {}
1825
1826 #if defined(__MULTITEXTURE_SUPPORT__)
1827
1828 extern "C" void glMultiTexCoord2fARB( GLenum target, GLfloat s, GLfloat t );
1829
1830 void glMultiTexCoord2fARB( GLenum target, GLfloat s, GLfloat t )
1831 {
1832 if (target == GL_TEXTURE0)
1833 {
1834 glTexCoord2f(s,t);
1835 }
1836 else
1837 {
1838 currentVertexAttrib.s_multi = s;
1839 currentVertexAttrib.t_multi = t;
1840 }
1841 }
1842 #endif
1843
1844 /* Vladimir */
1845 /*void glDrawArrays( GLenum mode, int first, int count)
1846 {
1847 FlushOnStateChange();
1848 glEsImpl->glDrawArrays(mode, first , count);
1849 }*/
1850 void glMultMatrixf (const GLfloat *m)
1851 {
1852 FlushOnStateChange();
1853 glEsImpl->glMultMatrixf(m);
1854 }
1855
1856 void glPixelStorei (GLenum pname, GLint param)
1857 {
1858 FlushOnStateChange();
1859 glEsImpl->glPixelStorei(pname, param);
1860 }
1861
1862
1863 void glFogf (GLenum pname, GLfloat param)
1864 {
1865 FlushOnStateChange();
1866 glEsImpl->glFogf(pname, param);
1867 }
1868
1869 void glFogfv (GLenum pname, const GLfloat *params)
1870 {
1871 FlushOnStateChange();
1872 glEsImpl->glFogfv(pname, params);
1873 }
1874
1875 void glGetTexParameteriv (GLenum target, GLenum pname, GLint *params)
1876 {
1877 FlushOnStateChange();
1878 glEsImpl->glGetTexParameteriv(target, pname, params);
1879 }
1880
1881
1882 // This gives: called unimplemented OpenGL ES API (Android)
1883 void glTexParameteri (GLenum target, GLenum pname, GLint param)
1884 {
1885 if (pname == GL_TEXTURE_BORDER_COLOR) {
1886 return; // not supported by opengl es
1887 }
1888 if ( (pname == GL_TEXTURE_WRAP_S ||
1889 pname == GL_TEXTURE_WRAP_T) &&
1890 param == GL_CLAMP) {
1891 param = 0x812F;
1892 }
1893
1894 FlushOnStateChange();
1895 glEsImpl->glTexParameteri(target, pname, param);
1896 }
1897
1898 void glTexParameterx (GLenum target, GLenum pname, GLfixed param)
1899 {
1900 if (pname == GL_TEXTURE_BORDER_COLOR) {
1901 return; // not supported by opengl es
1902 }
1903 if ( (pname == GL_TEXTURE_WRAP_S ||
1904 pname == GL_TEXTURE_WRAP_T) &&
1905 param == GL_CLAMP) {
1906 param = 0x812F;
1907 }
1908 FlushOnStateChange();
1909 glEsImpl->glTexParameterx(target, pname, param);
1910 }
1911
1912 void glGenTextures (GLsizei n, GLuint *textures)
1913 {
1914 FlushOnStateChange();
1915 glEsImpl->glGenTextures(n, textures);
1916 }
1917
1918 void glFrontFace (GLenum mode)
1919 {
1920 FlushOnStateChange();
1921 glEsImpl->glFrontFace(mode);
1922 }
1923 // End Vladimir
1924
1925 void glTexEnvi (GLenum target, GLenum pname, GLint param)
1926 {
1927 if (target == GL_TEXTURE_ENV)
1928 {
1929 if (pname == GL_TEXTURE_ENV_MODE)
1930 {
1931 if (param == activetmuState->texture_env_mode.value)
1932 {
1933 return;
1934 }
1935 else
1936 {
1937 FlushOnStateChange();
1938 glEsImpl->glTexEnvi(target, pname, param);
1939 activetmuState->texture_env_mode.value = param;
1940 return;
1941 }
1942 }
1943 }
1944 FlushOnStateChange();
1945 glEsImpl->glTexEnvi(target, pname, param);
1946 }
1947
1948 #ifdef __MULTITEXTURE_SUPPORT__
1949 void glMultiTexCoord3fARB(GLenum a, GLfloat b, GLfloat c, GLfloat)
1950 {
1951 return glMultiTexCoord2fARB(a, b, c);
1952 }
1953
1954 void glMultiTexCoord2f(GLenum, GLfloat, GLfloat)
1955 {
1956
1957 }
1958 #endif
1959 void glDrawArrays( GLenum mode, GLint first, GLsizei count )
1960 {
1961 // ensure that all primitives specified between glBegin/glEnd pairs
1962 // are rendered first, and that we have correct tmu in use..
1963 if( mode == GL_QUADS ) mode = GL_TRIANGLE_FAN;
1964 FlushOnStateChange();
1965 // setup correct vertex/color/texcoord pointers
1966 if (arraysValid ||
1967 tmuState0.vertex_array.changed ||
1968 tmuState0.color_array.changed ||
1969 tmuState0.texture_coord_array.changed || tmuState0.normal_array.changed)
1970 {
1971 glEsImpl->glClientActiveTexture(GL_TEXTURE0);
1972 }
1973 if (arraysValid || tmuState0.vertex_array.changed)
1974 {
1975 if (tmuState0.vertex_array.enabled)
1976 {
1977 glEsImpl->glEnableClientState(GL_VERTEX_ARRAY);
1978 }
1979 else
1980 {
1981 glEsImpl->glDisableClientState(GL_VERTEX_ARRAY);
1982 }
1983 glEsImpl->glVertexPointer(tmuState0.vertex_array.size,
1984 tmuState0.vertex_array.type,
1985 tmuState0.vertex_array.stride,
1986 tmuState0.vertex_array.ptr);
1987 tmuState0.vertex_array.changed = GL_FALSE;
1988 }
1989 if (arraysValid || tmuState0.color_array.changed)
1990 {
1991 if (tmuState0.color_array.enabled)
1992 {
1993 glEsImpl->glEnableClientState(GL_COLOR_ARRAY);
1994 }
1995 else
1996 {
1997 glEsImpl->glDisableClientState(GL_COLOR_ARRAY);
1998 }
1999 glEsImpl->glColorPointer(tmuState0.color_array.size,
2000 tmuState0.color_array.type,
2001 tmuState0.color_array.stride,
2002 tmuState0.color_array.ptr);
2003 tmuState0.color_array.changed = GL_FALSE;
2004 }
2005 if (arraysValid || tmuState0.normal_array.changed)
2006 {
2007 if (tmuState0.normal_array.enabled)
2008 {
2009 glEsImpl->glEnableClientState(GL_NORMAL_ARRAY);
2010 }
2011 else
2012 {
2013 glEsImpl->glDisableClientState(GL_NORMAL_ARRAY);
2014 }
2015 glEsImpl->glNormalPointer(tmuState0.normal_array.type,
2016 tmuState0.normal_array.stride,
2017 tmuState0.normal_array.ptr);
2018 tmuState0.normal_array.changed = GL_FALSE;
2019 }
2020 if (arraysValid || tmuState0.texture_coord_array.changed)
2021 {
2022 tmuState0.texture_coord_array.changed = GL_FALSE;
2023 if (tmuState0.texture_coord_array.enabled)
2024 {
2025 glEsImpl->glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2026 }
2027 else
2028 {
2029 glEsImpl->glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2030 }
2031 glEsImpl->glTexCoordPointer(tmuState0.texture_coord_array.size,
2032 tmuState0.texture_coord_array.type,
2033 tmuState0.texture_coord_array.stride,
2034 tmuState0.texture_coord_array.ptr);
2035 }
2036
2037 if (arraysValid || tmuState1.texture_coord_array.changed)
2038 {
2039 tmuState1.texture_coord_array.changed = GL_FALSE;
2040 glEsImpl->glClientActiveTexture(GL_TEXTURE1);
2041 if (tmuState1.texture_coord_array.enabled)
2042 {
2043 glEsImpl->glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2044 }
2045 else
2046 {
2047 glEsImpl->glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2048 }
2049 glEsImpl->glTexCoordPointer(tmuState1.texture_coord_array.size,
2050 tmuState1.texture_coord_array.type,
2051 tmuState1.texture_coord_array.stride,
2052 tmuState1.texture_coord_array.ptr);
2053 }
2054
2055 arraysValid = GL_FALSE;
2056 glEsImpl->glDrawArrays(mode, first, count);
2057 }
2058 /*void glNormalPointer(GLenum type, GLsizei stride, const void *ptr)
2059 {
2060 glEsImpl->glNormalPointer( type, stride, ptr );
2061 }*/
2062
2063 void glCopyTexSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height)
2064 {
2065 FlushOnStateChange();
2066 glEsImpl->glCopyTexSubImage2D( target, level, xoffset, yoffset, x, y, width, height );
2067 }
2068
2069 void glGenFramebuffers (GLsizei n, GLuint* framebuffers)
2070 {
2071 FlushOnStateChange();
2072 glEsImpl->glGenFramebuffers( n, framebuffers );
2073 }
2074
2075
2076 void glGenRenderbuffers( GLsizei n, GLuint* renderbuffers )
2077 {
2078 FlushOnStateChange();
2079 glEsImpl->glGenRenderbuffers( n, renderbuffers );
2080 }
2081
2082 void glBindRenderbuffer(GLenum target, GLuint renderbuffer)
2083 {
2084 FlushOnStateChange();
2085 glEsImpl->glBindRenderbuffer( target, renderbuffer );
2086 }
2087
2088 void glBindFramebuffer(GLenum target, GLuint framebuffer)\
2089 {
2090 FlushOnStateChange();
2091 glEsImpl->glBindFramebuffer( target, framebuffer );
2092 }
2093
2094 void glFramebufferRenderbuffer(GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer)
2095 {
2096 FlushOnStateChange();
2097 glEsImpl->glFramebufferRenderbuffer( target, attachment, renderbuffertarget, renderbuffer );
2098 }
2099
2100 void glDeleteFramebuffers(GLsizei n, const GLuint *framebuffers)
2101 {
2102 FlushOnStateChange();
2103 glEsImpl->glDeleteFramebuffers(n, framebuffers);
2104 }
2105
2106 void glDeleteRenderbuffers(GLsizei n, const GLuint *renderbuffers)
2107 {
2108 FlushOnStateChange();
2109 glEsImpl->glDeleteRenderbuffers( n, renderbuffers );
2110 }
2111 void glFramebufferTexture2D(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level)
2112 {
2113 FlushOnStateChange();
2114 glEsImpl->glFramebufferTexture2D(target, attachment,textarget,texture,level);
2115 }
2116
2117 void glRenderbufferStorage(GLenum target, GLenum internalformat, GLsizei width, GLsizei height)
2118 {
2119 FlushOnStateChange();
2120 glEsImpl->glRenderbufferStorage(target, internalformat, width, height );
2121 }
2122