DEADSOFTWARE

5ea8acf7ac6060b1f1c28f5ca5c793603a942c92
[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[40000];
213
214 static GLushort indexArray[30000];
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 }
515
516 void glEnable (GLenum cap)
517 {
518 GLboolean statechanged = GL_FALSE;
519 switch(cap)
520 {
521 case GL_ALPHA_TEST:
522 {
523 if (!nanoglState.alpha_test)
524 {
525 nanoglState.alpha_test = GL_TRUE;
526 statechanged = GL_TRUE;
527 }
528 break;
529 }
530 case GL_BLEND:
531 {
532 if (!nanoglState.blend)
533 {
534 nanoglState.blend = GL_TRUE;
535 statechanged = GL_TRUE;
536 }
537 break;
538 }
539 //case GL_CLIP_PLANEi
540 case GL_COLOR_LOGIC_OP:
541 {
542 if (!nanoglState.color_logic_op)
543 {
544 nanoglState.color_logic_op = GL_TRUE;
545 statechanged = GL_TRUE;
546 }
547 break;
548 }
549 case GL_COLOR_MATERIAL:
550 {
551 if (!nanoglState.color_material)
552 {
553 nanoglState.color_material = GL_TRUE;
554 statechanged = GL_TRUE;
555 }
556 break;
557 }
558 case GL_CULL_FACE:
559 {
560 if (!nanoglState.cull_face)
561 {
562 nanoglState.cull_face = GL_TRUE;
563 statechanged = GL_TRUE;
564 }
565 break;
566 }
567 case GL_DEPTH_TEST:
568 {
569 if (!nanoglState.depth_test)
570 {
571 nanoglState.depth_test = GL_TRUE;
572 statechanged = GL_TRUE;
573 }
574 break;
575 }
576 case GL_DITHER:
577 {
578 if (!nanoglState.dither)
579 {
580 nanoglState.dither = GL_TRUE;
581 statechanged = GL_TRUE;
582 }
583 break;
584 }
585 case GL_FOG:
586 //case GL_LIGHTi
587 {
588 if (!nanoglState.fog)
589 {
590 nanoglState.fog = GL_TRUE;
591 statechanged = GL_TRUE;
592 }
593 break;
594 }
595 case GL_LIGHTING:
596 {
597 if (!nanoglState.lighting)
598 {
599 nanoglState.lighting = GL_TRUE;
600 statechanged = GL_TRUE;
601 }
602 break;
603 }
604 case GL_LINE_SMOOTH:
605 {
606 if (!nanoglState.line_smooth)
607 {
608 nanoglState.line_smooth = GL_TRUE;
609 statechanged = GL_TRUE;
610 }
611 break;
612 }
613 /* case GL_MATRIX_PALETTE_OES:
614 {
615 if (!nanoglState.matrix_palette_oes)
616 {
617 nanoglState.matrix_palette_oes = GL_TRUE;
618 statechanged = GL_TRUE;
619 }
620 break;
621 }*/
622 case GL_MULTISAMPLE:
623 {
624 if (!nanoglState.multisample)
625 {
626 nanoglState.multisample = GL_TRUE;
627 statechanged = GL_TRUE;
628 }
629 break;
630 }
631 case GL_NORMALIZE:
632 {
633 if (!nanoglState.normalize)
634 {
635 nanoglState.normalize = GL_TRUE;
636 statechanged = GL_TRUE;
637 }
638 break;
639 }
640 /* case GL_POINT_SPRITE_OES:
641 {
642 if (!nanoglState.point_sprite_oes)
643 {
644 nanoglState.point_sprite_oes = GL_TRUE;
645 statechanged = GL_TRUE;
646 }
647 break;
648 }*/
649 case GL_POLYGON_OFFSET_FILL:
650 {
651 if (!nanoglState.polygon_offset_fill)
652 {
653 nanoglState.polygon_offset_fill = GL_TRUE;
654 statechanged = GL_TRUE;
655 }
656 break;
657 }
658 case GL_RESCALE_NORMAL:
659 {
660 if (!nanoglState.rescale_normal)
661 {
662 nanoglState.rescale_normal = GL_TRUE;
663 statechanged = GL_TRUE;
664 }
665 break;
666 }
667 case GL_SAMPLE_ALPHA_TO_COVERAGE:
668 {
669 if (!nanoglState.sample_alpha_to_coverage)
670 {
671 nanoglState.sample_alpha_to_coverage = GL_TRUE;
672 statechanged = GL_TRUE;
673 }
674 break;
675 }
676 case GL_SAMPLE_ALPHA_TO_ONE:
677 {
678 if (!nanoglState.sample_alpha_to_one)
679 {
680 nanoglState.sample_alpha_to_one = GL_TRUE;
681 statechanged = GL_TRUE;
682 }
683 break;
684 }
685 case GL_SAMPLE_COVERAGE:
686 {
687 if (!nanoglState.sample_coverage)
688 {
689 nanoglState.sample_coverage = GL_TRUE;
690 statechanged = GL_TRUE;
691 }
692 break;
693 }
694 case GL_SCISSOR_TEST:
695 {
696 if (!nanoglState.scissor_test)
697 {
698 nanoglState.scissor_test = GL_TRUE;
699 statechanged = GL_TRUE;
700 }
701 break;
702 }
703 case GL_STENCIL_TEST:
704 {
705 return;
706 /* if (!nanoglState.stencil_test)
707 {
708 nanoglState.stencil_test = GL_TRUE;
709 statechanged = GL_TRUE;
710 }*/
711 break;
712 }
713 case GL_TEXTURE_2D:
714 {
715 if (!activetmuState->texture_2d.value)
716 {
717 FlushOnStateChange();
718 glEsImpl->glEnable(cap);
719 activetmuState->texture_2d.value = GL_TRUE;
720 return;
721 }
722 break;
723 }
724 default:
725 break;
726 }
727
728 if (statechanged)
729 {
730 FlushOnStateChange();
731 glEsImpl->glEnable(cap);
732 }
733 }
734
735 void glDisable (GLenum cap)
736 {
737 GLboolean statechanged = GL_FALSE;
738 switch(cap)
739 {
740 case GL_ALPHA_TEST:
741 {
742 if (nanoglState.alpha_test)
743 {
744 nanoglState.alpha_test = GL_FALSE;
745 statechanged = GL_TRUE;
746 }
747 break;
748 }
749 case GL_BLEND:
750 {
751 if (nanoglState.blend)
752 {
753 nanoglState.blend = GL_FALSE;
754 statechanged = GL_TRUE;
755 }
756 break;
757 }
758 //case GL_CLIP_PLANEi
759 case GL_COLOR_LOGIC_OP:
760 {
761 if (nanoglState.color_logic_op)
762 {
763 nanoglState.color_logic_op = GL_FALSE;
764 statechanged = GL_TRUE;
765 }
766 break;
767 }
768 case GL_COLOR_MATERIAL:
769 {
770 if (nanoglState.color_material)
771 {
772 nanoglState.color_material = GL_FALSE;
773 statechanged = GL_TRUE;
774 }
775 break;
776 }
777 case GL_CULL_FACE:
778 {
779 if (nanoglState.cull_face)
780 {
781 nanoglState.cull_face = GL_FALSE;
782 statechanged = GL_TRUE;
783 }
784 break;
785 }
786 case GL_DEPTH_TEST:
787 {
788 if (nanoglState.depth_test)
789 {
790 nanoglState.depth_test = GL_FALSE;
791 statechanged = GL_TRUE;
792 }
793 break;
794 }
795 case GL_DITHER:
796 {
797 if (nanoglState.dither)
798 {
799 nanoglState.dither = GL_FALSE;
800 statechanged = GL_TRUE;
801 }
802 break;
803 }
804 case GL_FOG:
805 //case GL_LIGHTi
806 {
807 if (nanoglState.fog)
808 {
809 nanoglState.fog = GL_FALSE;
810 statechanged = GL_TRUE;
811 }
812 break;
813 }
814 case GL_LIGHTING:
815 {
816 if (nanoglState.lighting)
817 {
818 nanoglState.lighting = GL_FALSE;
819 statechanged = GL_TRUE;
820 }
821 break;
822 }
823 case GL_LINE_SMOOTH:
824 {
825 if (nanoglState.line_smooth)
826 {
827 nanoglState.line_smooth = GL_FALSE;
828 statechanged = GL_TRUE;
829 }
830 break;
831 }
832 /* case GL_MATRIX_PALETTE_OES:
833 {
834 if (nanoglState.matrix_palette_oes)
835 {
836 nanoglState.matrix_palette_oes = GL_FALSE;
837 statechanged = GL_TRUE;
838 }
839 break;
840 }*/
841 case GL_MULTISAMPLE:
842 {
843 if (nanoglState.multisample)
844 {
845 nanoglState.multisample = GL_FALSE;
846 statechanged = GL_TRUE;
847 }
848 break;
849 }
850 case GL_NORMALIZE:
851 {
852 if (nanoglState.normalize)
853 {
854 nanoglState.normalize = GL_FALSE;
855 statechanged = GL_TRUE;
856 }
857 break;
858 }
859 /* case GL_POINT_SPRITE_OES:
860 {
861 if (nanoglState.point_sprite_oes)
862 {
863 nanoglState.point_sprite_oes = GL_FALSE;
864 statechanged = GL_TRUE;
865 }
866 break;
867 }*/
868 case GL_POLYGON_OFFSET_FILL:
869 {
870 if (nanoglState.polygon_offset_fill)
871 {
872 nanoglState.polygon_offset_fill = GL_FALSE;
873 statechanged = GL_TRUE;
874 }
875 break;
876 }
877 case GL_RESCALE_NORMAL:
878 {
879 if (nanoglState.rescale_normal)
880 {
881 nanoglState.rescale_normal = GL_FALSE;
882 statechanged = GL_TRUE;
883 }
884 break;
885 }
886 case GL_SAMPLE_ALPHA_TO_COVERAGE:
887 {
888 if (nanoglState.sample_alpha_to_coverage)
889 {
890 nanoglState.sample_alpha_to_coverage = GL_FALSE;
891 statechanged = GL_TRUE;
892 }
893 break;
894 }
895 case GL_SAMPLE_ALPHA_TO_ONE:
896 {
897 if (nanoglState.sample_alpha_to_one)
898 {
899 nanoglState.sample_alpha_to_one = GL_FALSE;
900 statechanged = GL_TRUE;
901 }
902 break;
903 }
904 case GL_SAMPLE_COVERAGE:
905 {
906 if (nanoglState.sample_coverage)
907 {
908 nanoglState.sample_coverage = GL_FALSE;
909 statechanged = GL_TRUE;
910 }
911 break;
912 }
913 case GL_SCISSOR_TEST:
914 {
915 if (nanoglState.scissor_test)
916 {
917 nanoglState.scissor_test = GL_FALSE;
918 statechanged = GL_TRUE;
919 }
920 break;
921 }
922 case GL_STENCIL_TEST:
923 {
924 return;
925 /* if (nanoglState.stencil_test)
926 {
927 nanoglState.stencil_test = GL_FALSE;
928 statechanged = GL_TRUE;
929 }*/
930 break;
931 }
932 case GL_TEXTURE_2D:
933 {
934 if (activetmuState->texture_2d.value)
935 {
936 FlushOnStateChange();
937 glEsImpl->glDisable(cap);
938 activetmuState->texture_2d.value = GL_FALSE;
939 return;
940
941
942 }
943 break;
944 }
945 default:
946 break;
947 }
948
949 if (statechanged)
950 {
951 FlushOnStateChange();
952 glEsImpl->glDisable(cap);
953 }
954 }
955
956 void glVertex2f(GLfloat x, GLfloat y)
957 {
958 glVertex3f(x,y,0.0f);
959 }
960
961
962 __FORCEINLINE unsigned int ClampTo255(float value)
963 {
964 unsigned int retval = (unsigned int)(value);
965 if (retval > 255)
966 {
967 retval = 255;
968 }
969 return retval;
970 }
971
972 void glColor3f( GLfloat red, GLfloat green, GLfloat blue)
973 {
974 currentVertexAttrib.red = (unsigned char)ClampTo255(red*255.0f);
975 currentVertexAttrib.green = (unsigned char)ClampTo255(green*255.0f);
976 currentVertexAttrib.blue = (unsigned char)ClampTo255(blue*255.0f);
977 currentVertexAttrib.alpha = 255;
978 }
979
980
981 void glTexCoord2fv( const GLfloat *v )
982 {
983 memcpy(&currentVertexAttrib.s, v, 2*sizeof(float));
984 }
985
986
987 void glTexCoord2f(GLfloat s, GLfloat t)
988 {
989 currentVertexAttrib.s = s;
990 currentVertexAttrib.t = t;
991 }
992
993 void glViewport (GLint x, GLint y, GLsizei width, GLsizei height)
994 {
995 FlushOnStateChange();
996 glEsImpl->glViewport(x,y,width,height);
997 }
998
999 void glLoadIdentity (void)
1000 {
1001 FlushOnStateChange();
1002 glEsImpl->glLoadIdentity();
1003 }
1004
1005 void glColor4f (GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
1006 {
1007 currentVertexAttrib.red = (unsigned char)ClampTo255(red*255.0f);
1008 currentVertexAttrib.green = (unsigned char)ClampTo255(green*255.0f);
1009 currentVertexAttrib.blue = (unsigned char)ClampTo255(blue*255.0f);
1010 currentVertexAttrib.alpha = (unsigned char)ClampTo255(alpha*255.0f);
1011 }
1012
1013 void glOrtho (GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat zNear, GLfloat zFar)
1014 {
1015 FlushOnStateChange();
1016 #ifdef USE_CORE_PROFILE
1017 glEsImpl->glOrtho(left,right,bottom,top, zNear,zFar);
1018 #else
1019 glEsImpl->glOrthof(left,right,bottom,top, zNear,zFar);
1020 #endif
1021 }
1022
1023 void glMatrixMode (GLenum mode)
1024 {
1025 if (nanoglState.matrixmode == mode)
1026 {
1027 return;
1028 }
1029 nanoglState.matrixmode = mode;
1030 FlushOnStateChange();
1031 glEsImpl->glMatrixMode(mode);
1032 }
1033
1034 void glTexParameterf (GLenum target, GLenum pname, GLfloat param)
1035 {
1036 if (pname == GL_TEXTURE_BORDER_COLOR)
1037 {
1038 return; // not supported by opengl es
1039 }
1040 if ( (pname == GL_TEXTURE_WRAP_S ||
1041 pname == GL_TEXTURE_WRAP_T) &&
1042 param == GL_CLAMP)
1043 {
1044 param = 0x812F;
1045 }
1046
1047 FlushOnStateChange();
1048 glEsImpl->glTexParameterf(target, pname,param);
1049 }
1050
1051 void glTexParameterfv( GLenum target, GLenum pname, const GLfloat *params)
1052 {
1053 glTexParameterf(target, pname, params[0]);
1054 }
1055
1056 void glTexImage2D (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels)
1057 {
1058 FlushOnStateChange();
1059 internalformat = format;
1060 glEsImpl->glTexImage2D(target, level, internalformat, width, height,border,format,type,pixels);
1061 }
1062
1063 void glDrawBuffer(GLenum /*mode*/)
1064 {
1065 }
1066
1067 void glTranslatef (GLfloat x, GLfloat y, GLfloat z)
1068 {
1069 FlushOnStateChange();
1070 glEsImpl->glTranslatef(x,y,z);
1071 }
1072
1073 void glRotatef (GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
1074 {
1075 FlushOnStateChange();
1076 glEsImpl->glRotatef(angle, x, y, z);
1077 }
1078
1079 void glScalef (GLfloat x, GLfloat y, GLfloat z)
1080 {
1081 FlushOnStateChange();
1082 glEsImpl->glScalef(x,y,z);
1083 }
1084
1085 void glDepthRange(GLclampf zNear, GLclampf zFar)
1086 {
1087 if ((nanoglState.depth_range_near == zNear) &&(nanoglState.depth_range_far == zFar))
1088 {
1089 return;
1090 }
1091 else
1092 {
1093 nanoglState.depth_range_near = zNear;
1094 nanoglState.depth_range_far = zFar;
1095 }
1096 FlushOnStateChange();
1097 #ifdef USE_CORE_PROFILE
1098 glEsImpl->glDepthRange(zNear, zFar);
1099 #else
1100 glEsImpl->glDepthRangef(zNear, zFar);
1101 #endif
1102 }
1103
1104 void glDepthFunc (GLenum func)
1105 {
1106 if (nanoglState.depth_func == func)
1107 {
1108 return;
1109 }
1110 else
1111 {
1112 nanoglState.depth_func = func;
1113 }
1114 FlushOnStateChange();
1115 glEsImpl->glDepthFunc(func);
1116 }
1117
1118 void glFinish (void)
1119 {
1120 FlushOnStateChange();
1121 glEsImpl->glFinish();
1122 }
1123
1124 void glGetFloatv (GLenum pname, GLfloat *params)
1125 {
1126 FlushOnStateChange();
1127 glEsImpl->glGetFloatv(pname, params);
1128 }
1129
1130 void glCullFace (GLenum mode)
1131 {
1132 if (nanoglState.cullface == mode)
1133 {
1134 return;
1135 }
1136 else
1137 {
1138 nanoglState.cullface = mode;
1139 }
1140 FlushOnStateChange();
1141 glEsImpl->glCullFace(mode);
1142 }
1143
1144 void glFrustum (GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat zNear, GLfloat zFar)
1145 {
1146 FlushOnStateChange();
1147 glEsImpl->glFrustumf(left,right,bottom,top,zNear,zFar);
1148 }
1149
1150 void glClear (GLbitfield mask)
1151 {
1152 FlushOnStateChange();
1153 glEsImpl->glClear(mask);
1154 }
1155
1156
1157 void glVertex3f( GLfloat x, GLfloat y, GLfloat z )
1158 {
1159 GLfloat* vert = (GLfloat*)ptrVertexAttribArray++;
1160 *vert++ = x;
1161 *vert++ = y;
1162 *vert++ = z;
1163 #if defined(__MULTITEXTURE_SUPPORT__)
1164 memcpy(vert, &currentVertexAttrib.red, 5*sizeof(GLfloat));
1165 #else
1166 memcpy(vert+1, &currentVertexAttrib.red, 3*sizeof(GLfloat));
1167 #endif
1168
1169 }
1170
1171 void glColor4fv( const GLfloat *v )
1172 {
1173 currentVertexAttrib.red = (unsigned char)ClampTo255(v[0]*255.0f);
1174 currentVertexAttrib.green = (unsigned char)ClampTo255(v[1]*255.0f);
1175 currentVertexAttrib.blue = (unsigned char)ClampTo255(v[2]*255.0f);
1176 currentVertexAttrib.alpha = (unsigned char)ClampTo255(v[3]*255.0f);
1177 }
1178
1179 void glColor3ubv( const GLubyte* v)
1180 {
1181 currentVertexAttrib.red = v[0];
1182 currentVertexAttrib.green = v[1];
1183 currentVertexAttrib.blue = v[2];
1184 currentVertexAttrib.alpha = 255;
1185 }
1186
1187 void glColor4ubv( const GLubyte *v )
1188 {
1189 //*((unsigned int*)(&currentVertexAttrib.red)) = *((unsigned int*)(v));
1190 currentVertexAttrib.red = v[0];
1191 currentVertexAttrib.green = v[1];
1192 currentVertexAttrib.blue = v[2];
1193 currentVertexAttrib.alpha = v[3];
1194 }
1195
1196 void glColor3fv( const GLfloat *v )
1197 {
1198 currentVertexAttrib.red = (unsigned char)ClampTo255(v[0]*255.0f);
1199 currentVertexAttrib.green = (unsigned char)ClampTo255(v[1]*255.0f);
1200 currentVertexAttrib.blue = (unsigned char)ClampTo255(v[2]*255.0f);
1201 currentVertexAttrib.alpha = 255;
1202 }
1203
1204 //-- nicknekit: xash3d funcs --
1205
1206 void glColor4ub( GLubyte red, GLubyte green, GLubyte blue, GLubyte alpha)
1207 {
1208 currentVertexAttrib.red = red;
1209 currentVertexAttrib.green = green;
1210 currentVertexAttrib.blue = blue;
1211 currentVertexAttrib.alpha = alpha;
1212 }
1213
1214 void glColor3ub( GLubyte red, GLubyte green, GLubyte blue)
1215 {
1216 currentVertexAttrib.red = red;
1217 currentVertexAttrib.green = green;
1218 currentVertexAttrib.blue = blue;
1219 currentVertexAttrib.alpha = 255;
1220 }
1221
1222 void glNormal3fv( const GLfloat *v )
1223 {
1224 FlushOnStateChange();
1225 glEsImpl->glNormal3f(v[0],v[1],v[2]);
1226 }
1227
1228 void glCopyTexImage2D( GLenum target, GLint level, GLenum internalformat, GLint x, GLint y, GLsizei width, GLsizei height, GLint border )
1229 {
1230 FlushOnStateChange();
1231 glEsImpl->glCopyTexImage2D(target, level, internalformat, x, y, width, height, border);
1232 }
1233
1234 void glTexImage1D(GLenum target, GLint level, GLint internalformat, GLsizei width, GLint border, GLenum format, GLenum type, const GLvoid *pixels)
1235 {
1236 glTexImage2D(GL_TEXTURE_2D, level, internalformat, width, 1, border, format, type, pixels);
1237 }
1238
1239 void glTexImage3D(GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid *pixels)
1240 {
1241 glTexImage2D(GL_TEXTURE_2D, level, internalformat, width, height, border, format, type, pixels);
1242 }
1243
1244 void glTexSubImage1D( GLenum target, GLint level, GLint xoffset, GLsizei width, GLenum format, GLenum type, const GLvoid *pixels )
1245 {
1246 glTexSubImage2D(target,level,xoffset,0,width,1,format,type,pixels);
1247 }
1248
1249 void glTexSubImage3D( GLenum target, GLint level,
1250 GLint xoffset, GLint yoffset,
1251 GLint zoffset, GLsizei width,
1252 GLsizei height, GLsizei depth,
1253 GLenum format,
1254 GLenum type, const GLvoid *pixels)
1255 {
1256 glTexSubImage2D(target,level,xoffset,yoffset,width,height,format,type,pixels);
1257 }
1258
1259
1260
1261 GLboolean glIsTexture(GLuint texture)
1262 {
1263 FlushOnStateChange();
1264 return glEsImpl->glIsTexture(texture);
1265 }
1266
1267
1268 void glTexGeni( GLenum coord, GLenum pname, GLint param )
1269 {
1270 //for mirrors? not needed for original hl?
1271 }
1272
1273 void glTexGenfv( GLenum coord, GLenum pname, const GLfloat *params )
1274 {
1275 //for mirrors? not needed for original hl?
1276 }
1277
1278 //-- --//
1279
1280 void glHint (GLenum target, GLenum mode)
1281 {
1282 FlushOnStateChange();
1283 glEsImpl->glHint(target, mode);
1284 }
1285
1286 void glBlendFunc (GLenum sfactor, GLenum dfactor)
1287 {
1288 if ((nanoglState.sfactor == sfactor) && (nanoglState.dfactor == dfactor))
1289 {
1290 return;
1291 }
1292 nanoglState.sfactor = sfactor;
1293 nanoglState.dfactor = dfactor;
1294 FlushOnStateChange();
1295 glEsImpl->glBlendFunc(sfactor, dfactor);
1296 }
1297
1298 void glPopMatrix (void)
1299 {
1300 FlushOnStateChange();
1301 glEsImpl->glPopMatrix();
1302 }
1303
1304 void glShadeModel (GLenum mode)
1305 {
1306 if (nanoglState.shademodel == mode)
1307 {
1308 return;
1309 }
1310 nanoglState.shademodel = mode;
1311 FlushOnStateChange();
1312 glEsImpl->glShadeModel(mode);
1313 }
1314
1315 void glPushMatrix (void)
1316 {
1317 FlushOnStateChange();
1318 glEsImpl->glPushMatrix();
1319 }
1320
1321 void glTexEnvf (GLenum target, GLenum pname, GLfloat param)
1322 {
1323 if (target == GL_TEXTURE_ENV)
1324 {
1325 if (pname == GL_TEXTURE_ENV_MODE)
1326 {
1327 if (param == activetmuState->texture_env_mode.value)
1328 {
1329 return;
1330 }
1331 else
1332 {
1333 FlushOnStateChange();
1334 glEsImpl->glTexEnvf(target, pname, param);
1335 activetmuState->texture_env_mode.value = param;
1336 return;
1337 }
1338 }
1339 }
1340 FlushOnStateChange();
1341 glEsImpl->glTexEnvf(target, pname, param);
1342 }
1343
1344 void glVertex3fv( const GLfloat *v )
1345 {
1346 GLfloat* vert = (GLfloat*)ptrVertexAttribArray++;
1347 memcpy(vert, v, 3*sizeof(GLfloat));
1348 #if defined(__MULTITEXTURE_SUPPORT__)
1349 memcpy(vert+3, &currentVertexAttrib.red, 5*sizeof(GLfloat));
1350 #else
1351 memcpy(vert+4, &currentVertexAttrib.red, 3*sizeof(GLfloat));
1352 #endif
1353 }
1354
1355
1356 void glDepthMask (GLboolean flag)
1357 {
1358 if (nanoglState.depthmask == flag)
1359 {
1360 return;
1361 }
1362 nanoglState.depthmask = flag;
1363 FlushOnStateChange();
1364 glEsImpl->glDepthMask(flag);
1365 }
1366
1367 void glBindTexture (GLenum target, GLuint texture)
1368 {
1369 if (activetmuState->boundtexture.value == texture)
1370 {
1371 return;
1372 }
1373 FlushOnStateChange();
1374 activetmuState->boundtexture.value = texture;
1375 glEsImpl->glBindTexture(target, texture);
1376 }
1377
1378
1379 void glGetIntegerv (GLenum pname, GLint *params)
1380 {
1381 FlushOnStateChange();
1382 glEsImpl->glGetIntegerv(pname, params);
1383 }
1384
1385 GLubyte nano_extensions_string[4096];
1386 const GLubyte* glGetString (GLenum name)
1387 {
1388
1389 if (name == GL_EXTENSIONS)
1390 {
1391 #if defined(__MULTITEXTURE_SUPPORT__)
1392 sprintf((char*)nano_extensions_string,"%s %s",glEsImpl->glGetString(name),"GL_ARB_multitexture EXT_texture_env_add");
1393 #else
1394 sprintf((char*)nano_extensions_string,"%s %s",glEsImpl->glGetString(name),"EXT_texture_env_add");
1395 #endif
1396 return nano_extensions_string;
1397 }
1398 return glEsImpl->glGetString(name);
1399 }
1400
1401 void glAlphaFunc (GLenum func, GLclampf ref)
1402 {
1403 FlushOnStateChange();
1404 glEsImpl->glAlphaFunc(func,ref);
1405 }
1406
1407 void glFlush (void)
1408 {
1409 FlushOnStateChange();
1410 glEsImpl->glFlush();
1411 }
1412
1413 void glReadPixels (GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *pixels)
1414 {
1415 if (format == GL_DEPTH_COMPONENT)
1416 {
1417 // OpenglEs 1.1 does not support reading depth buffer without an extension
1418 memset(pixels, 0xff,4);
1419 return;
1420 }
1421 FlushOnStateChange();
1422 glEsImpl->glReadPixels(x,y,width,height,format,type,pixels);
1423 }
1424
1425 void glReadBuffer( GLenum /*mode*/ )
1426 {
1427
1428 }
1429
1430 void glLoadMatrixf (const GLfloat *m)
1431 {
1432 FlushOnStateChange();
1433 glEsImpl->glLoadMatrixf(m);
1434 }
1435
1436 void glTexSubImage2D (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels)
1437 {
1438 FlushOnStateChange();
1439 glEsImpl->glTexSubImage2D(target,level,xoffset,yoffset,width,height,format,type,pixels);
1440 }
1441
1442 void glClearColor (GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
1443 {
1444 FlushOnStateChange();
1445 glEsImpl->glClearColor(red,green,blue,alpha);
1446 }
1447
1448 GLenum glGetError (void)
1449 {
1450 //FlushOnStateChange();
1451 return GL_NO_ERROR;//glEsImpl->glGetError();
1452 }
1453
1454 void glActiveTexture (GLenum texture)
1455 {
1456 if (activetmu == texture)
1457 {
1458 return;
1459 }
1460 if (delayedttmuchange)
1461 {
1462 delayedttmuchange = GL_FALSE;
1463 }
1464 else
1465 {
1466 delayedttmuchange = GL_TRUE;
1467 delayedtmutarget = texture;
1468 }
1469 if (texture == GL_TEXTURE0)
1470 {
1471 activetmuState = &tmuState0;
1472 }
1473 else
1474 {
1475 activetmuState = &tmuState1;
1476 }
1477 activetmu = texture;
1478 }
1479
1480 void glClientActiveTexture (GLenum texture)
1481 {
1482 clientactivetmu = texture;
1483 }
1484
1485 void glPolygonMode( GLenum face, GLenum mode )
1486 {
1487 }
1488
1489 void glDeleteTextures( GLsizei n, const GLuint *textures )
1490 {
1491 FlushOnStateChange();
1492 glEsImpl->glDeleteTextures(n,textures);
1493 }
1494
1495 void glClearDepth( GLclampf depth )
1496 {
1497 FlushOnStateChange();
1498 glEsImpl->glClearDepthf( depth );
1499 }
1500
1501 void glClipPlane( GLenum plane, const GLdouble *equation )
1502 {
1503 FlushOnStateChange();
1504 float array[4];
1505 array[0] = (GLfloat)(equation[0]);
1506 array[1] = (GLfloat)(equation[1]);
1507 array[2] = (GLfloat)(equation[2]);
1508 array[3] = (GLfloat)(equation[3]);
1509 glEsImpl->glClipPlanef( plane, array );
1510 }
1511
1512 void glScissor( GLint x, GLint y, GLsizei width, GLsizei height )
1513 {
1514 FlushOnStateChange();
1515 glEsImpl->glScissor( x, y, width,height);
1516 }
1517
1518 void glPointSize( GLfloat size )
1519 {
1520 FlushOnStateChange();
1521 glEsImpl->glPointSize( size );
1522 }
1523
1524 void glArrayElement(GLint i) {}
1525 void glLineWidth(GLfloat width) {}
1526 void glCallList( GLuint list ) {}
1527 void glColorMask( GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha ) {}
1528 void glStencilFunc( GLenum func, GLint ref, GLuint mask ) {}
1529 void glStencilOp( GLenum fail, GLenum zfail, GLenum zpass ) {}
1530
1531 struct ptrstate vertex_array;
1532 struct ptrstate color_array;
1533 struct ptrstate texture_coord_array;
1534
1535 void glDrawElements( GLenum mode, GLsizei count, GLenum type, const GLvoid *indices )
1536 {
1537 // ensure that all primitives specified between glBegin/glEnd pairs
1538 // are rendered first, and that we have correct tmu in use..
1539 FlushOnStateChange();
1540 // setup correct vertex/color/texcoord pointers
1541 if (arraysValid ||
1542 tmuState0.vertex_array.changed ||
1543 tmuState0.color_array.changed ||
1544 tmuState0.texture_coord_array.changed || tmuState0.normal_array.changed)
1545 {
1546 glEsImpl->glClientActiveTexture(GL_TEXTURE0);
1547 }
1548 if (arraysValid || tmuState0.vertex_array.changed)
1549 {
1550 if (tmuState0.vertex_array.enabled)
1551 {
1552 glEsImpl->glEnableClientState(GL_VERTEX_ARRAY);
1553 }
1554 else
1555 {
1556 glEsImpl->glDisableClientState(GL_VERTEX_ARRAY);
1557 }
1558 glEsImpl->glVertexPointer(tmuState0.vertex_array.size,
1559 tmuState0.vertex_array.type,
1560 tmuState0.vertex_array.stride,
1561 tmuState0.vertex_array.ptr);
1562 tmuState0.vertex_array.changed = GL_FALSE;
1563 }
1564 if (arraysValid || tmuState0.color_array.changed)
1565 {
1566 if (tmuState0.color_array.enabled)
1567 {
1568 glEsImpl->glEnableClientState(GL_COLOR_ARRAY);
1569 }
1570 else
1571 {
1572 glEsImpl->glDisableClientState(GL_COLOR_ARRAY);
1573 }
1574 glEsImpl->glColorPointer(tmuState0.color_array.size,
1575 tmuState0.color_array.type,
1576 tmuState0.color_array.stride,
1577 tmuState0.color_array.ptr);
1578 tmuState0.color_array.changed = GL_FALSE;
1579 }
1580 if (arraysValid || tmuState0.normal_array.changed)
1581 {
1582 if (tmuState0.normal_array.enabled)
1583 {
1584 glEsImpl->glEnableClientState(GL_NORMAL_ARRAY);
1585 }
1586 else
1587 {
1588 glEsImpl->glDisableClientState(GL_NORMAL_ARRAY);
1589 }
1590 glEsImpl->glNormalPointer(tmuState0.normal_array.type,
1591 tmuState0.normal_array.stride,
1592 tmuState0.normal_array.ptr);
1593 tmuState0.normal_array.changed = GL_FALSE;
1594 }
1595 if (arraysValid || tmuState0.texture_coord_array.changed)
1596 {
1597 tmuState0.texture_coord_array.changed = GL_FALSE;
1598 if (tmuState0.texture_coord_array.enabled)
1599 {
1600 glEsImpl->glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1601 }
1602 else
1603 {
1604 glEsImpl->glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1605 }
1606 glEsImpl->glTexCoordPointer(tmuState0.texture_coord_array.size,
1607 tmuState0.texture_coord_array.type,
1608 tmuState0.texture_coord_array.stride,
1609 tmuState0.texture_coord_array.ptr);
1610 }
1611
1612 if (arraysValid || tmuState1.texture_coord_array.changed)
1613 {
1614 tmuState1.texture_coord_array.changed = GL_FALSE;
1615 glEsImpl->glClientActiveTexture(GL_TEXTURE1);
1616 if (tmuState1.texture_coord_array.enabled)
1617 {
1618 glEsImpl->glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1619 }
1620 else
1621 {
1622 glEsImpl->glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1623 }
1624 glEsImpl->glTexCoordPointer(tmuState1.texture_coord_array.size,
1625 tmuState1.texture_coord_array.type,
1626 tmuState1.texture_coord_array.stride,
1627 tmuState1.texture_coord_array.ptr);
1628 }
1629
1630 arraysValid = GL_FALSE;
1631 glEsImpl->glDrawElements(mode, count, type, indices);
1632 }
1633
1634 void glEnableClientState(GLenum array)
1635 {
1636 struct nanotmuState* clientstate = NULL;
1637 if (clientactivetmu == GL_TEXTURE0)
1638 {
1639 clientstate = &tmuState0;
1640 }
1641 else if (clientactivetmu == GL_TEXTURE1)
1642 {
1643 clientstate = &tmuState1;
1644 }
1645 else
1646 {
1647 return;
1648 }
1649 switch (array)
1650 {
1651 case GL_VERTEX_ARRAY:
1652 if (clientstate->vertex_array.enabled)
1653 {
1654 return;
1655 }
1656 clientstate->vertex_array.enabled = GL_TRUE;
1657 clientstate->vertex_array.changed = GL_TRUE;
1658 break;
1659 case GL_COLOR_ARRAY:
1660 if (clientstate->color_array.enabled)
1661 {
1662 return;
1663 }
1664 clientstate->color_array.enabled = GL_TRUE;
1665 clientstate->color_array.changed = GL_TRUE;
1666
1667 break;
1668 case GL_NORMAL_ARRAY:
1669 if (clientstate->normal_array.enabled)
1670 {
1671 return;
1672 }
1673 clientstate->normal_array.enabled = GL_TRUE;
1674 clientstate->normal_array.changed = GL_TRUE;
1675
1676 break;
1677 case GL_TEXTURE_COORD_ARRAY:
1678 if (clientstate->texture_coord_array.enabled)
1679 {
1680 return;
1681 }
1682 clientstate->texture_coord_array.enabled = GL_TRUE;
1683 clientstate->texture_coord_array.changed = GL_TRUE;
1684 break;
1685 default:
1686 break;
1687 }
1688 }
1689 void glDisableClientState(GLenum array)
1690 {
1691 struct nanotmuState* clientstate = NULL;
1692 if (clientactivetmu == GL_TEXTURE0)
1693 {
1694 clientstate = &tmuState0;
1695 }
1696 else if (clientactivetmu == GL_TEXTURE1)
1697 {
1698 clientstate = &tmuState1;
1699 }
1700 else
1701 {
1702 return;
1703 }
1704 switch (array)
1705 {
1706 case GL_VERTEX_ARRAY:
1707 if (!clientstate->vertex_array.enabled)
1708 {
1709 return;
1710 }
1711 clientstate->vertex_array.enabled = GL_FALSE;
1712 clientstate->vertex_array.changed = GL_TRUE;
1713 break;
1714 case GL_COLOR_ARRAY:
1715 if (!clientstate->color_array.enabled)
1716 {
1717 return;
1718 }
1719 clientstate->color_array.enabled = GL_FALSE;
1720 clientstate->color_array.changed = GL_TRUE;
1721
1722 break;
1723 case GL_NORMAL_ARRAY:
1724 if (!clientstate->normal_array.enabled)
1725 {
1726 return;
1727 }
1728 clientstate->normal_array.enabled = GL_FALSE;
1729 clientstate->normal_array.changed = GL_TRUE;
1730
1731 break;
1732 case GL_TEXTURE_COORD_ARRAY:
1733 if (!clientstate->texture_coord_array.enabled)
1734 {
1735 return;
1736 }
1737 clientstate->texture_coord_array.enabled = GL_FALSE;
1738 clientstate->texture_coord_array.changed = GL_TRUE;
1739 break;
1740 default:
1741 break;
1742 }
1743 }
1744 void glVertexPointer( GLint size, GLenum type,GLsizei stride, const GLvoid *pointer )
1745 {
1746 if (tmuState0.vertex_array.size == size &&
1747 tmuState0.vertex_array.stride == stride &&
1748 tmuState0.vertex_array.type == type &&
1749 tmuState0.vertex_array.ptr == pointer)
1750 {
1751 return;
1752 }
1753 tmuState0.vertex_array.size = size;
1754 tmuState0.vertex_array.stride = stride;
1755 tmuState0.vertex_array.type = type;
1756 tmuState0.vertex_array.ptr = (GLvoid*)pointer;
1757 tmuState0.vertex_array.changed = GL_TRUE;
1758 }
1759 void glTexCoordPointer( GLint size, GLenum type, GLsizei stride, const GLvoid *pointer )
1760 {
1761 struct nanotmuState* clientstate = NULL;
1762 if (clientactivetmu == GL_TEXTURE0)
1763 {
1764 clientstate = &tmuState0;
1765 }
1766 else if (clientactivetmu == GL_TEXTURE1)
1767 {
1768 clientstate = &tmuState1;
1769 }
1770 if (clientstate->texture_coord_array.size == size &&
1771 clientstate->texture_coord_array.stride == stride &&
1772 clientstate->texture_coord_array.type == type &&
1773 clientstate->texture_coord_array.ptr == pointer)
1774 {
1775 return;
1776 }
1777 clientstate->texture_coord_array.size = size;
1778 clientstate->texture_coord_array.stride = stride;
1779 clientstate->texture_coord_array.type = type;
1780 clientstate->texture_coord_array.ptr = (GLvoid*)pointer;
1781 clientstate->texture_coord_array.changed = GL_TRUE;
1782 }
1783 void glColorPointer( GLint size, GLenum type, GLsizei stride, const GLvoid *pointer )
1784 {
1785 if (tmuState0.color_array.size == size &&
1786 tmuState0.color_array.stride == stride &&
1787 tmuState0.color_array.type == type &&
1788 tmuState0.color_array.ptr == pointer)
1789 {
1790 return;
1791 }
1792 tmuState0.color_array.size = size;
1793 tmuState0.color_array.stride = stride;
1794 tmuState0.color_array.type = type;
1795 tmuState0.color_array.ptr = (GLvoid*)pointer;
1796 tmuState0.color_array.changed = GL_TRUE;
1797 }
1798
1799 void glNormalPointer( GLenum type, GLsizei stride, const GLvoid *pointer )
1800 {
1801 int size = 0;
1802 if (tmuState0.normal_array.size == size &&
1803 tmuState0.normal_array.stride == stride &&
1804 tmuState0.normal_array.type == type &&
1805 tmuState0.normal_array.ptr == pointer)
1806 {
1807 return;
1808 }
1809 tmuState0.normal_array.size = size;
1810 tmuState0.normal_array.stride = stride;
1811 tmuState0.normal_array.type = type;
1812 tmuState0.normal_array.ptr = (GLvoid*)pointer;
1813 tmuState0.normal_array.changed = GL_TRUE;
1814 }
1815 void glPolygonOffset( GLfloat factor, GLfloat units )
1816 {
1817 FlushOnStateChange();
1818 glEsImpl->glPolygonOffset(factor, units);
1819 }
1820 void glStencilMask( GLuint mask ) {}
1821 void glClearStencil( GLint s ) {}
1822
1823 #if defined(__MULTITEXTURE_SUPPORT__)
1824
1825 extern "C" void glMultiTexCoord2fARB( GLenum target, GLfloat s, GLfloat t );
1826
1827 void glMultiTexCoord2fARB( GLenum target, GLfloat s, GLfloat t )
1828 {
1829 if (target == GL_TEXTURE0)
1830 {
1831 glTexCoord2f(s,t);
1832 }
1833 else
1834 {
1835 currentVertexAttrib.s_multi = s;
1836 currentVertexAttrib.t_multi = t;
1837 }
1838 }
1839 #endif
1840
1841 /* Vladimir */
1842 void glDrawArrays( GLenum mode, int first, int count)
1843 {
1844 FlushOnStateChange();
1845 glEsImpl->glDrawArrays(mode, first , count);
1846 }
1847 void glMultMatrixf (const GLfloat *m)
1848 {
1849 FlushOnStateChange();
1850 glEsImpl->glMultMatrixf(m);
1851 }
1852
1853 void glPixelStorei (GLenum pname, GLint param)
1854 {
1855 FlushOnStateChange();
1856 glEsImpl->glPixelStorei(pname, param);
1857 }
1858
1859
1860 void glFogf (GLenum pname, GLfloat param)
1861 {
1862 FlushOnStateChange();
1863 glEsImpl->glFogf(pname, param);
1864 }
1865
1866 void glFogfv (GLenum pname, const GLfloat *params)
1867 {
1868 FlushOnStateChange();
1869 glEsImpl->glFogfv(pname, params);
1870 }
1871
1872 void glGetTexParameteriv (GLenum target, GLenum pname, GLint *params)
1873 {
1874 FlushOnStateChange();
1875 glEsImpl->glGetTexParameteriv(target, pname, params);
1876 }
1877
1878
1879 // This gives: called unimplemented OpenGL ES API (Android)
1880 void glTexParameteri (GLenum target, GLenum pname, GLint param)
1881 {
1882 if (pname == GL_TEXTURE_BORDER_COLOR) {
1883 return; // not supported by opengl es
1884 }
1885 if ( (pname == GL_TEXTURE_WRAP_S ||
1886 pname == GL_TEXTURE_WRAP_T) &&
1887 param == GL_CLAMP) {
1888 param = 0x812F;
1889 }
1890
1891 FlushOnStateChange();
1892 glEsImpl->glTexParameteri(target, pname, param);
1893 }
1894
1895 void glTexParameterx (GLenum target, GLenum pname, GLfixed param)
1896 {
1897 if (pname == GL_TEXTURE_BORDER_COLOR) {
1898 return; // not supported by opengl es
1899 }
1900 if ( (pname == GL_TEXTURE_WRAP_S ||
1901 pname == GL_TEXTURE_WRAP_T) &&
1902 param == GL_CLAMP) {
1903 param = 0x812F;
1904 }
1905 FlushOnStateChange();
1906 glEsImpl->glTexParameterx(target, pname, param);
1907 }
1908
1909 void glGenTextures (GLsizei n, GLuint *textures)
1910 {
1911 FlushOnStateChange();
1912 glEsImpl->glGenTextures(n, textures);
1913 }
1914
1915 void glFrontFace (GLenum mode)
1916 {
1917 FlushOnStateChange();
1918 glEsImpl->glFrontFace(mode);
1919 }
1920 // End Vladimir
1921
1922 void glTexEnvi (GLenum target, GLenum pname, GLint param)
1923 {
1924 if (target == GL_TEXTURE_ENV)
1925 {
1926 if (pname == GL_TEXTURE_ENV_MODE)
1927 {
1928 if (param == activetmuState->texture_env_mode.value)
1929 {
1930 return;
1931 }
1932 else
1933 {
1934 FlushOnStateChange();
1935 glEsImpl->glTexEnvi(target, pname, param);
1936 activetmuState->texture_env_mode.value = param;
1937 return;
1938 }
1939 }
1940 }
1941 FlushOnStateChange();
1942 glEsImpl->glTexEnvi(target, pname, param);
1943 }
1944
1945 void pglMultiTexCoord3f(GLenum, GLfloat, GLfloat, GLfloat)
1946 {
1947
1948 }
1949
1950 void pglMultiTexCoord2f(GLenum, GLfloat, GLfloat)
1951 {
1952
1953 }
1954
1955 /*void glNormalPointer(GLenum type, GLsizei stride, const void *ptr)
1956 {
1957 glEsImpl->glNormalPointer( type, stride, ptr );
1958 }*/
1959
1960 void glCopyTexSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height)
1961 {
1962 FlushOnStateChange();
1963 glEsImpl->glCopyTexSubImage2D( target, level, xoffset, yoffset, x, y, width, height );
1964 }
1965
1966 void glGenFramebuffers (GLsizei n, GLuint* framebuffers)
1967 {
1968 FlushOnStateChange();
1969 glEsImpl->glGenFramebuffers( n, framebuffers );
1970 }
1971
1972
1973 void glGenRenderbuffers( GLsizei n, GLuint* renderbuffers )
1974 {
1975 FlushOnStateChange();
1976 glEsImpl->glGenFramebuffers( n, renderbuffers );
1977 }
1978
1979 void glBindRenderbuffer(GLenum target, GLuint renderbuffer)
1980 {
1981 FlushOnStateChange();
1982 glEsImpl->glBindRenderbuffer( target, renderbuffer );
1983 }
1984
1985 void glBindFramebuffer(GLenum target, GLuint framebuffer)\
1986 {
1987 FlushOnStateChange();
1988 glEsImpl->glBindFramebuffer( target, framebuffer );
1989 }
1990
1991 void glFramebufferRenderbuffer(GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer)
1992 {
1993 FlushOnStateChange();
1994 glEsImpl->glFramebufferRenderbuffer( target, attachment, renderbuffertarget, renderbuffer );
1995 }
1996
1997 void glDeleteFramebuffers(GLsizei n, const GLuint *framebuffers)
1998 {
1999 FlushOnStateChange();
2000 glEsImpl->glDeleteFramebuffers(n, framebuffers);
2001 }
2002
2003 void glDeleteRenderbuffers(GLsizei n, const GLuint *renderbuffers)
2004 {
2005 FlushOnStateChange();
2006 glEsImpl->glDeleteRenderbuffers( n, renderbuffers );
2007 }
2008 void glFramebufferTexture2D(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level)
2009 {
2010 FlushOnStateChange();
2011 glEsImpl->glFramebufferTexture2D(target, attachment,textarget,texture,level);
2012 }
2013
2014 void glRenderbufferStorage(GLenum target, GLenum internalformat, GLsizei width, GLsizei height)
2015 {
2016 FlushOnStateChange();
2017 glEsImpl->glRenderbufferStorage(target, internalformat, width, height );
2018 }
2019