DEADSOFTWARE

Add VBO support
[nanogl.git] / nanoWrap.cpp
1 /*
2 Copyright (C) 2007-2009 Olli Hinkka
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.
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.
13 See the GNU General Public License for more details.
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.
19 */
20 /*
21 #include <e32def.h>
22 #include <e32std.h>
23 */
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include "gl.h"
30 #include "glesinterface.h"
31 #include "nanogl.h"
33 #define GL_TEXTURE0_ARB 0x84C0
34 #define GL_TEXTURE1_ARB 0x84C1
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 GLboolean stupidglesbug;
64 GLclampf depth_range_near;
65 GLclampf depth_range_far;
66 GLenum depth_func;
67 GLenum cullface;
68 GLenum shademodel;
69 GLenum sfactor;
70 GLenum dfactor;
71 GLenum matrixmode;
72 };
74 static struct nanoState nanoglState;
76 static struct nanoState nanoglInitState =
77 {
78 GL_FALSE,
79 GL_FALSE,
80 GL_FALSE,
81 GL_FALSE,
82 GL_FALSE,
83 GL_FALSE,
84 GL_FALSE,
85 GL_TRUE,
86 GL_FALSE,
87 GL_FALSE,
88 GL_FALSE,
89 GL_FALSE,
90 GL_FALSE,
91 GL_TRUE,
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_FALSE,
102 GL_TRUE,
103 GL_FALSE,
104 0.0f,
105 1.0f,
106 GL_LESS,
107 GL_BACK,
108 GL_SMOOTH,
109 GL_ONE,
110 GL_ZERO,
111 GL_MODELVIEW,
112 };
114 struct booleanstate
116 GLboolean value;
117 GLboolean changed;
118 };
120 struct floatstate
122 GLfloat value;
123 GLboolean changed;
124 };
126 struct uintstate
128 GLuint value;
129 GLboolean changed;
130 };
132 struct ptrstate
134 GLint size;
135 GLenum type;
136 GLsizei stride;
137 GLvoid *ptr;
138 GLboolean changed;
139 GLboolean enabled;
140 };
142 struct nanotmuState
144 struct booleanstate texture_2d;
145 struct floatstate texture_env_mode;
146 struct uintstate boundtexture;
147 struct ptrstate vertex_array;
148 struct ptrstate color_array;
149 struct ptrstate texture_coord_array;
150 struct ptrstate normal_array;
151 };
153 static struct nanotmuState tmuState0;
154 static struct nanotmuState tmuState1;
156 static struct nanotmuState tmuInitState =
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 };
167 static struct nanotmuState *activetmuState = &tmuState0;
169 extern "C++" GlESInterface *glEsImpl;
171 static GLenum wrapperPrimitiveMode = GL_QUADS;
172 GLboolean useTexCoordArray = GL_FALSE;
173 static GLenum activetmu = GL_TEXTURE0;
174 static GLenum clientactivetmu = GL_TEXTURE0;
176 #if defined( __MULTITEXTURE_SUPPORT__ )
177 GLboolean useMultiTexCoordArray = GL_FALSE;
178 #endif
180 #if !defined( __WINS__ )
181 //#define __FORCEINLINE __forceinline
182 #define __FORCEINLINE inline
183 #else
184 #define __FORCEINLINE
185 #endif
187 static GLboolean delayedttmuchange = GL_FALSE;
188 static GLenum delayedtmutarget = GL_TEXTURE0;
190 struct VertexAttrib
192 float x;
193 float y;
194 float z;
195 #if !defined( __MULTITEXTURE_SUPPORT__ )
196 float padding;
197 #endif
198 unsigned char red;
199 unsigned char green;
200 unsigned char blue;
201 unsigned char alpha;
203 float s;
204 float t;
205 #if defined( __MULTITEXTURE_SUPPORT__ )
206 float s_multi;
207 float t_multi;
208 #endif
209 };
211 static VertexAttrib vertexattribs[60000];
213 static GLushort indexArray[50000];
215 static GLuint vertexCount = 0;
216 static GLuint indexCount = 0;
217 static GLuint vertexMark = 0;
218 static int indexbase = 0;
220 static VertexAttrib *ptrVertexAttribArray = NULL;
221 static VertexAttrib *ptrVertexAttribArrayMark = NULL;
223 static VertexAttrib currentVertexAttrib;
224 #if defined( __MULTITEXTURE_SUPPORT__ )
225 static VertexAttrib currentVertexAttribInit = {0.0f, 0.0f, 0.0f, 255, 255, 255, 255, 0.0f, 0.0f, 0.0f, 0.0f};
226 #else
227 static VertexAttrib currentVertexAttribInit = {
228 0.0f, 0.0f, 0.0f, 0.0f, 255, 255, 255, 255, 0.0f, 0.0f,
229 };
230 #endif
231 static GLushort *ptrIndexArray = NULL;
233 static GLboolean arraysValid = GL_FALSE;
235 static GLboolean skipnanogl;
237 void InitGLStructs( )
239 ptrVertexAttribArray = vertexattribs;
240 ptrVertexAttribArrayMark = ptrVertexAttribArray;
241 ptrIndexArray = indexArray;
243 memcpy( &nanoglState, &nanoglInitState, sizeof( struct nanoState ) );
244 memcpy( &tmuState0, &tmuInitState, sizeof( struct nanotmuState ) );
245 memcpy( &tmuState1, &tmuInitState, sizeof( struct nanotmuState ) );
246 memcpy( &currentVertexAttrib, &currentVertexAttribInit, sizeof( struct VertexAttrib ) );
248 activetmuState = &tmuState0;
249 wrapperPrimitiveMode = GL_QUADS;
250 useTexCoordArray = GL_FALSE;
251 activetmu = GL_TEXTURE0;
252 clientactivetmu = GL_TEXTURE0;
253 delayedttmuchange = GL_FALSE;
254 delayedtmutarget = GL_TEXTURE0;
255 vertexCount = 0;
256 indexCount = 0;
257 vertexMark = 0;
258 indexbase = 0;
259 arraysValid = GL_FALSE;
262 void ResetNanoState( )
265 if ( tmuState0.color_array.enabled )
267 glEsImpl->glEnableClientState( GL_COLOR_ARRAY );
269 else
271 glEsImpl->glDisableClientState( GL_COLOR_ARRAY );
274 if ( tmuState0.vertex_array.enabled )
276 glEsImpl->glEnableClientState( GL_VERTEX_ARRAY );
278 else
280 glEsImpl->glDisableClientState( GL_VERTEX_ARRAY );
283 if ( tmuState0.texture_coord_array.enabled )
285 glEsImpl->glEnableClientState( GL_TEXTURE_COORD_ARRAY );
287 else
289 glEsImpl->glDisableClientState( GL_TEXTURE_COORD_ARRAY );
292 if ( tmuState0.normal_array.enabled )
294 glEsImpl->glEnableClientState( GL_NORMAL_ARRAY );
296 else
298 glEsImpl->glDisableClientState( GL_NORMAL_ARRAY );
300 glEsImpl->glVertexPointer( tmuState0.vertex_array.size,
301 tmuState0.vertex_array.type,
302 tmuState0.vertex_array.stride,
303 tmuState0.vertex_array.ptr );
305 glEsImpl->glTexCoordPointer( tmuState0.texture_coord_array.size,
306 tmuState0.texture_coord_array.type,
307 tmuState0.texture_coord_array.stride,
308 tmuState0.texture_coord_array.ptr );
310 glEsImpl->glColorPointer( tmuState0.color_array.size,
311 tmuState0.color_array.type,
312 tmuState0.color_array.stride,
313 tmuState0.color_array.ptr );
315 glEsImpl->glNormalPointer(
316 tmuState0.normal_array.type,
317 tmuState0.normal_array.stride,
318 tmuState0.normal_array.ptr );
320 glEsImpl->glMatrixMode( nanoglState.matrixmode );
322 glEsImpl->glColor4f( currentVertexAttrib.red/255.0f, currentVertexAttrib.green/255.0f,
323 currentVertexAttrib.blue/255.0f, currentVertexAttrib.alpha/255.0f );
325 glEsImpl->glBlendFunc( nanoglState.sfactor, nanoglState.dfactor );
327 //glEsImpl->glBindTexture(GL_TEXTURE_2D, stackTextureState);
329 glEsImpl->glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, activetmuState->texture_env_mode.value );
331 arraysValid = GL_FALSE;
332 skipnanogl = GL_FALSE;
335 void FlushOnStateChange( )
337 if( skipnanogl )
338 return;
339 if ( delayedttmuchange )
341 delayedttmuchange = GL_FALSE;
342 #ifndef USE_CORE_PROFILE
343 glEsImpl->glActiveTexture( delayedtmutarget );
344 #endif
347 if ( !vertexCount )
348 return;
350 if ( !arraysValid )
352 glEsImpl->glClientActiveTexture( GL_TEXTURE0 );
353 glEsImpl->glVertexPointer( 3, GL_FLOAT, sizeof( VertexAttrib ), &vertexattribs[0].x );
354 glEsImpl->glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( VertexAttrib ), &vertexattribs[0].red );
355 glEsImpl->glTexCoordPointer( 2, GL_FLOAT, sizeof( VertexAttrib ), &vertexattribs[0].s );
356 glEsImpl->glEnableClientState( GL_VERTEX_ARRAY );
357 glEsImpl->glEnableClientState( GL_TEXTURE_COORD_ARRAY );
358 glEsImpl->glEnableClientState( GL_COLOR_ARRAY );
359 #if defined( __MULTITEXTURE_SUPPORT__ )
360 glEsImpl->glClientActiveTexture( GL_TEXTURE1 );
361 glEsImpl->glTexCoordPointer( 2, GL_FLOAT, sizeof( VertexAttrib ), &vertexattribs[0].s_multi );
362 glEsImpl->glEnableClientState( GL_TEXTURE_COORD_ARRAY );
363 glEsImpl->glClientActiveTexture( GL_TEXTURE0 );
364 #endif
365 arraysValid = GL_TRUE;
368 glEsImpl->glDrawElements( GL_TRIANGLES, vertexCount, GL_UNSIGNED_SHORT, indexArray );
370 #if defined( __MULTITEXTURE_SUPPORT__ )
371 useMultiTexCoordArray = GL_FALSE;
372 #endif
373 vertexCount = 0;
374 indexCount = 0;
375 ptrVertexAttribArray = vertexattribs;
376 ptrVertexAttribArrayMark = ptrVertexAttribArray;
377 ptrIndexArray = indexArray;
378 useTexCoordArray = GL_FALSE;
380 void nanoGL_Flush( )
382 FlushOnStateChange( );
384 void nanoGL_Reset( )
386 ResetNanoState( );
388 void glBegin( GLenum mode )
390 wrapperPrimitiveMode = mode;
391 vertexMark = vertexCount;
392 ptrVertexAttribArrayMark = ptrVertexAttribArray;
393 indexbase = indexCount;
396 void glEnd( void )
398 vertexCount += ( (unsigned char *)ptrVertexAttribArray - (unsigned char *)ptrVertexAttribArrayMark ) / sizeof( VertexAttrib );
399 if ( vertexCount < 3 )
401 return;
403 switch ( wrapperPrimitiveMode )
405 case GL_QUADS:
407 *ptrIndexArray++ = indexCount;
408 *ptrIndexArray++ = indexCount + 1;
409 *ptrIndexArray++ = indexCount + 2;
410 *ptrIndexArray++ = indexCount;
411 *ptrIndexArray++ = indexCount + 2;
412 *ptrIndexArray++ = indexCount + 3;
413 indexCount += 4;
414 vertexCount += 2;
416 break;
417 case GL_TRIANGLES:
419 int vcount = ( vertexCount - vertexMark ) / 3;
420 for ( int count = 0; count < vcount; count++ )
422 *ptrIndexArray++ = indexCount;
423 *ptrIndexArray++ = indexCount + 1;
424 *ptrIndexArray++ = indexCount + 2;
425 indexCount += 3;
428 break;
429 case GL_TRIANGLE_STRIP:
431 *ptrIndexArray++ = indexCount;
432 *ptrIndexArray++ = indexCount + 1;
433 *ptrIndexArray++ = indexCount + 2;
434 indexCount += 3;
435 int vcount = ( ( vertexCount - vertexMark ) - 3 );
436 if ( vcount && ( (long)ptrIndexArray & 0x02 ) )
438 *ptrIndexArray++ = indexCount - 1; // 2
439 *ptrIndexArray++ = indexCount - 2; // 1
440 *ptrIndexArray++ = indexCount; // 3
441 indexCount++;
442 vcount -= 1;
443 int odd = vcount & 1;
444 vcount /= 2;
445 unsigned int *longptr = (unsigned int *)ptrIndexArray;
447 for ( int count = 0; count < vcount; count++ )
449 *( longptr++ ) = ( indexCount - 2 ) | ( ( indexCount - 1 ) << 16 );
450 *( longptr++ ) = ( indexCount ) | ( ( indexCount ) << 16 );
451 *( longptr++ ) = ( indexCount - 1 ) | ( ( indexCount + 1 ) << 16 );
452 indexCount += 2;
454 ptrIndexArray = (unsigned short *)( longptr );
455 if ( odd )
457 *ptrIndexArray++ = indexCount - 2; // 2
458 *ptrIndexArray++ = indexCount - 1; // 1
459 *ptrIndexArray++ = indexCount; // 3
460 indexCount++;
463 else
465 //already aligned
466 int odd = vcount & 1;
467 vcount /= 2;
468 unsigned int *longptr = (unsigned int *)ptrIndexArray;
470 for ( int count = 0; count < vcount; count++ )
472 *( longptr++ ) = ( indexCount - 1 ) | ( ( indexCount - 2 ) << 16 );
473 *( longptr++ ) = ( indexCount ) | ( ( indexCount - 1 ) << 16 );
474 *( longptr++ ) = ( indexCount ) | ( ( indexCount + 1 ) << 16 );
475 indexCount += 2;
477 ptrIndexArray = (unsigned short *)( longptr );
478 if ( odd )
481 *ptrIndexArray++ = indexCount - 1; // 2
482 *ptrIndexArray++ = indexCount - 2; // 1
483 *ptrIndexArray++ = indexCount; // 3
484 indexCount++;
487 vertexCount += ( vertexCount - vertexMark - 3 ) * 2;
489 break;
490 case GL_POLYGON:
491 case GL_TRIANGLE_FAN:
493 *ptrIndexArray++ = indexCount++;
494 *ptrIndexArray++ = indexCount++;
495 *ptrIndexArray++ = indexCount++;
496 int vcount = ( ( vertexCount - vertexMark ) - 3 );
497 for ( int count = 0; count < vcount; count++ )
499 *ptrIndexArray++ = indexbase;
500 *ptrIndexArray++ = indexCount - 1;
501 *ptrIndexArray++ = indexCount++;
502 vertexCount += 2;
505 break;
507 default:
508 break;
510 if ( ptrVertexAttribArray - vertexattribs > 20000 * sizeof( VertexAttrib ) ||
511 ptrIndexArray - indexArray > 15000 * sizeof( GLushort ) )
512 FlushOnStateChange( );
515 void glEnable( GLenum cap )
517 if( skipnanogl )
519 glEsImpl->glEnable( cap );
520 return;
522 GLboolean statechanged = GL_FALSE;
523 switch ( cap )
525 case GL_ALPHA_TEST:
527 if ( !nanoglState.alpha_test )
529 nanoglState.alpha_test = GL_TRUE;
530 statechanged = GL_TRUE;
532 break;
534 case GL_BLEND:
536 if ( !nanoglState.blend )
538 nanoglState.blend = GL_TRUE;
539 statechanged = GL_TRUE;
541 break;
543 //case GL_CLIP_PLANEi
544 case GL_COLOR_LOGIC_OP:
546 if ( !nanoglState.color_logic_op )
548 nanoglState.color_logic_op = GL_TRUE;
549 statechanged = GL_TRUE;
551 break;
553 case GL_COLOR_MATERIAL:
555 if ( !nanoglState.color_material )
557 nanoglState.color_material = GL_TRUE;
558 statechanged = GL_TRUE;
560 break;
562 case GL_CULL_FACE:
564 if ( !nanoglState.cull_face )
566 nanoglState.cull_face = GL_TRUE;
567 statechanged = GL_TRUE;
569 break;
571 case GL_DEPTH_TEST:
573 if ( !nanoglState.depth_test )
575 nanoglState.depth_test = GL_TRUE;
576 statechanged = GL_TRUE;
578 break;
580 case GL_DITHER:
582 if ( !nanoglState.dither )
584 nanoglState.dither = GL_TRUE;
585 statechanged = GL_TRUE;
587 break;
589 case GL_FOG:
590 //case GL_LIGHTi
592 if ( !nanoglState.fog )
594 nanoglState.fog = GL_TRUE;
595 statechanged = GL_TRUE;
597 break;
599 case GL_LIGHTING:
601 if ( !nanoglState.lighting )
603 nanoglState.lighting = GL_TRUE;
604 statechanged = GL_TRUE;
606 break;
608 case GL_LINE_SMOOTH:
610 if ( !nanoglState.line_smooth )
612 nanoglState.line_smooth = GL_TRUE;
613 statechanged = GL_TRUE;
615 break;
617 /* case GL_MATRIX_PALETTE_OES:
619 if (!nanoglState.matrix_palette_oes)
621 nanoglState.matrix_palette_oes = GL_TRUE;
622 statechanged = GL_TRUE;
624 break;
625 }*/
626 case GL_MULTISAMPLE:
628 if ( !nanoglState.multisample )
630 nanoglState.multisample = GL_TRUE;
631 statechanged = GL_TRUE;
633 break;
635 case GL_NORMALIZE:
637 if ( !nanoglState.normalize )
639 nanoglState.normalize = GL_TRUE;
640 statechanged = GL_TRUE;
642 break;
644 /* case GL_POINT_SPRITE_OES:
646 if (!nanoglState.point_sprite_oes)
648 nanoglState.point_sprite_oes = GL_TRUE;
649 statechanged = GL_TRUE;
651 break;
652 }*/
653 case GL_POLYGON_OFFSET_FILL:
655 if ( !nanoglState.polygon_offset_fill )
657 nanoglState.polygon_offset_fill = GL_TRUE;
658 statechanged = GL_TRUE;
660 break;
662 case GL_RESCALE_NORMAL:
664 if ( !nanoglState.rescale_normal )
666 nanoglState.rescale_normal = GL_TRUE;
667 statechanged = GL_TRUE;
669 break;
671 case GL_SAMPLE_ALPHA_TO_COVERAGE:
673 if ( !nanoglState.sample_alpha_to_coverage )
675 nanoglState.sample_alpha_to_coverage = GL_TRUE;
676 statechanged = GL_TRUE;
678 break;
680 case GL_SAMPLE_ALPHA_TO_ONE:
682 if ( !nanoglState.sample_alpha_to_one )
684 nanoglState.sample_alpha_to_one = GL_TRUE;
685 statechanged = GL_TRUE;
687 break;
689 case GL_SAMPLE_COVERAGE:
691 if ( !nanoglState.sample_coverage )
693 nanoglState.sample_coverage = GL_TRUE;
694 statechanged = GL_TRUE;
696 break;
698 case GL_SCISSOR_TEST:
700 if ( !nanoglState.scissor_test )
702 nanoglState.scissor_test = GL_TRUE;
703 statechanged = GL_TRUE;
705 break;
707 case GL_STENCIL_TEST:
709 return;
710 if (!nanoglState.stencil_test)
712 nanoglState.stencil_test = GL_TRUE;
713 statechanged = GL_TRUE;
715 break;
717 case GL_TEXTURE_2D:
719 if ( !activetmuState->texture_2d.value )
721 FlushOnStateChange( );
722 glEsImpl->glEnable( cap );
723 activetmuState->texture_2d.value = GL_TRUE;
724 return;
726 break;
728 #if 0 // todo: implement cubemap texgen
729 case GL_TEXTURE_GEN_S:
730 case GL_TEXTURE_GEN_T:
731 case GL_TEXTURE_GEN_R:
732 case GL_TEXTURE_GEN_Q:
734 FlushOnStateChange( );
735 nanoglState.texgen = true;
736 return;
738 #endif
739 default:
740 break;
743 if ( statechanged )
745 FlushOnStateChange( );
746 glEsImpl->glEnable( cap );
750 void glDisable( GLenum cap )
752 if( skipnanogl )
754 glEsImpl->glDisable( cap );
755 return;
757 GLboolean statechanged = GL_FALSE;
758 switch ( cap )
760 case GL_ALPHA_TEST:
762 if ( nanoglState.alpha_test )
764 nanoglState.alpha_test = GL_FALSE;
765 statechanged = GL_TRUE;
767 break;
769 case GL_BLEND:
771 if ( nanoglState.blend )
773 nanoglState.blend = GL_FALSE;
774 statechanged = GL_TRUE;
776 break;
778 //case GL_CLIP_PLANEi
779 case GL_COLOR_LOGIC_OP:
781 if ( nanoglState.color_logic_op )
783 nanoglState.color_logic_op = GL_FALSE;
784 statechanged = GL_TRUE;
786 break;
788 case GL_COLOR_MATERIAL:
790 if ( nanoglState.color_material )
792 nanoglState.color_material = GL_FALSE;
793 statechanged = GL_TRUE;
795 break;
797 case GL_CULL_FACE:
799 if ( nanoglState.cull_face )
801 nanoglState.cull_face = GL_FALSE;
802 statechanged = GL_TRUE;
804 break;
806 case GL_DEPTH_TEST:
808 if ( nanoglState.depth_test )
810 nanoglState.depth_test = GL_FALSE;
811 statechanged = GL_TRUE;
813 break;
815 case GL_DITHER:
817 if ( nanoglState.dither )
819 nanoglState.dither = GL_FALSE;
820 statechanged = GL_TRUE;
822 break;
824 case GL_FOG:
825 //case GL_LIGHTi
827 if ( nanoglState.fog )
829 nanoglState.fog = GL_FALSE;
830 statechanged = GL_TRUE;
832 break;
834 case GL_LIGHTING:
836 if ( nanoglState.lighting )
838 nanoglState.lighting = GL_FALSE;
839 statechanged = GL_TRUE;
841 break;
843 case GL_LINE_SMOOTH:
845 if ( nanoglState.line_smooth )
847 nanoglState.line_smooth = GL_FALSE;
848 statechanged = GL_TRUE;
850 break;
852 /* case GL_MATRIX_PALETTE_OES:
854 if (nanoglState.matrix_palette_oes)
856 nanoglState.matrix_palette_oes = GL_FALSE;
857 statechanged = GL_TRUE;
859 break;
860 }*/
861 case GL_MULTISAMPLE:
863 if ( nanoglState.multisample )
865 nanoglState.multisample = GL_FALSE;
866 statechanged = GL_TRUE;
868 break;
870 case GL_NORMALIZE:
872 if ( nanoglState.normalize )
874 nanoglState.normalize = GL_FALSE;
875 statechanged = GL_TRUE;
877 break;
879 /* case GL_POINT_SPRITE_OES:
881 if (nanoglState.point_sprite_oes)
883 nanoglState.point_sprite_oes = GL_FALSE;
884 statechanged = GL_TRUE;
886 break;
887 }*/
888 case GL_POLYGON_OFFSET_FILL:
890 if ( nanoglState.polygon_offset_fill )
892 nanoglState.polygon_offset_fill = GL_FALSE;
893 statechanged = GL_TRUE;
895 break;
897 case GL_RESCALE_NORMAL:
899 if ( nanoglState.rescale_normal )
901 nanoglState.rescale_normal = GL_FALSE;
902 statechanged = GL_TRUE;
904 break;
906 case GL_SAMPLE_ALPHA_TO_COVERAGE:
908 if ( nanoglState.sample_alpha_to_coverage )
910 nanoglState.sample_alpha_to_coverage = GL_FALSE;
911 statechanged = GL_TRUE;
913 break;
915 case GL_SAMPLE_ALPHA_TO_ONE:
917 if ( nanoglState.sample_alpha_to_one )
919 nanoglState.sample_alpha_to_one = GL_FALSE;
920 statechanged = GL_TRUE;
922 break;
924 case GL_SAMPLE_COVERAGE:
926 if ( nanoglState.sample_coverage )
928 nanoglState.sample_coverage = GL_FALSE;
929 statechanged = GL_TRUE;
931 break;
933 case GL_SCISSOR_TEST:
935 if ( nanoglState.scissor_test )
937 nanoglState.scissor_test = GL_FALSE;
938 statechanged = GL_TRUE;
940 break;
942 case GL_STENCIL_TEST:
944 return;
945 if (nanoglState.stencil_test)
947 nanoglState.stencil_test = GL_FALSE;
948 statechanged = GL_TRUE;
950 break;
952 case GL_TEXTURE_2D:
954 if ( activetmuState->texture_2d.value )
956 FlushOnStateChange( );
957 glEsImpl->glDisable( cap );
958 activetmuState->texture_2d.value = GL_FALSE;
959 return;
961 break;
963 #if 0
964 case GL_TEXTURE_GEN_S:
965 case GL_TEXTURE_GEN_T:
966 case GL_TEXTURE_GEN_R:
967 case GL_TEXTURE_GEN_Q:
969 FlushOnStateChange( );
970 nanoglState.texgen = false;
971 return;
973 #endif
974 default:
975 break;
978 if ( statechanged )
980 FlushOnStateChange( );
981 glEsImpl->glDisable( cap );
985 void glVertex2f( GLfloat x, GLfloat y )
987 glVertex3f( x, y, 0.0f );
990 __FORCEINLINE unsigned int ClampTo255( float value )
992 unsigned int retval = (unsigned int)( value );
993 if ( retval > 255 )
995 retval = 255;
997 return retval;
1000 void glColor3f( GLfloat red, GLfloat green, GLfloat blue )
1002 currentVertexAttrib.red = (unsigned char)ClampTo255( red * 255.0f );
1003 currentVertexAttrib.green = (unsigned char)ClampTo255( green * 255.0f );
1004 currentVertexAttrib.blue = (unsigned char)ClampTo255( blue * 255.0f );
1005 currentVertexAttrib.alpha = 255;
1008 void glTexCoord2fv( const GLfloat *v )
1010 memcpy( &currentVertexAttrib.s, v, 2 * sizeof( float ) );
1013 void glTexCoord2f( GLfloat s, GLfloat t )
1015 currentVertexAttrib.s = s;
1016 currentVertexAttrib.t = t;
1019 void glViewport( GLint x, GLint y, GLsizei width, GLsizei height )
1021 FlushOnStateChange( );
1022 glEsImpl->glViewport( x, y, width, height );
1025 void glLoadIdentity( void )
1027 FlushOnStateChange( );
1028 glEsImpl->glLoadIdentity( );
1031 void glColor4f( GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha )
1032 {
1033 if( nanoglState.stupidglesbug )
1035 currentVertexAttrib.red = (unsigned char)ClampTo255( ( red * alpha ) * 255.0f );
1036 currentVertexAttrib.green = (unsigned char)ClampTo255( ( green * alpha ) * 255.0f );
1037 currentVertexAttrib.blue = (unsigned char)ClampTo255( ( blue * alpha ) * 255.0f );
1039 else
1041 currentVertexAttrib.red = (unsigned char)ClampTo255( red * 255.0f );
1042 currentVertexAttrib.green = (unsigned char)ClampTo255( green * 255.0f );
1043 currentVertexAttrib.blue = (unsigned char)ClampTo255( blue * 255.0f );
1045 currentVertexAttrib.alpha = (unsigned char)ClampTo255( alpha * 255.0f );
1048 void glOrtho( GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat zNear, GLfloat zFar )
1050 FlushOnStateChange( );
1051 #ifdef USE_CORE_PROFILE
1052 glEsImpl->glOrtho( left, right, bottom, top, zNear, zFar );
1053 #else
1054 glEsImpl->glOrthof( left, right, bottom, top, zNear, zFar );
1055 #endif
1058 void glMatrixMode( GLenum mode )
1060 if ( nanoglState.matrixmode == mode )
1062 return;
1064 nanoglState.matrixmode = mode;
1065 FlushOnStateChange( );
1066 glEsImpl->glMatrixMode( mode );
1069 void glTexParameterf( GLenum target, GLenum pname, GLfloat param )
1071 if ( pname == GL_TEXTURE_BORDER_COLOR )
1073 return; // not supported by opengl es
1075 if ( ( pname == GL_TEXTURE_WRAP_S ||
1076 pname == GL_TEXTURE_WRAP_T ) &&
1077 param == GL_CLAMP )
1079 param = 0x812F;
1082 FlushOnStateChange( );
1083 glEsImpl->glTexParameterf( target, pname, param );
1086 void glTexParameterfv( GLenum target, GLenum pname, const GLfloat *params )
1088 glTexParameterf( target, pname, params[0] );
1091 void glTexImage2D( GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels )
1093 FlushOnStateChange( );
1094 internalformat = format;
1095 glEsImpl->glTexImage2D( target, level, internalformat, width, height, border, format, type, pixels );
1098 void glDrawBuffer( GLenum /*mode*/ )
1102 void glTranslatef( GLfloat x, GLfloat y, GLfloat z )
1104 FlushOnStateChange( );
1105 glEsImpl->glTranslatef( x, y, z );
1108 void glRotatef( GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
1110 FlushOnStateChange( );
1111 glEsImpl->glRotatef( angle, x, y, z );
1114 void glScalef( GLfloat x, GLfloat y, GLfloat z )
1116 FlushOnStateChange( );
1117 glEsImpl->glScalef( x, y, z );
1120 void glDepthRange( GLclampf zNear, GLclampf zFar )
1122 if ( ( nanoglState.depth_range_near == zNear ) && ( nanoglState.depth_range_far == zFar ) )
1124 return;
1126 else
1128 nanoglState.depth_range_near = zNear;
1129 nanoglState.depth_range_far = zFar;
1131 FlushOnStateChange( );
1132 #ifdef USE_CORE_PROFILE
1133 glEsImpl->glDepthRange( zNear, zFar );
1134 #else
1135 glEsImpl->glDepthRangef( zNear, zFar );
1136 #endif
1139 void glDepthFunc( GLenum func )
1141 if ( nanoglState.depth_func == func )
1143 return;
1145 else
1147 nanoglState.depth_func = func;
1149 FlushOnStateChange( );
1150 glEsImpl->glDepthFunc( func );
1153 void glFinish( void )
1155 FlushOnStateChange( );
1156 glEsImpl->glFinish( );
1159 void glGetFloatv( GLenum pname, GLfloat *params )
1161 FlushOnStateChange( );
1162 glEsImpl->glGetFloatv( pname, params );
1165 void glCullFace( GLenum mode )
1167 if ( nanoglState.cullface == mode )
1169 return;
1171 else
1173 nanoglState.cullface = mode;
1175 FlushOnStateChange( );
1176 glEsImpl->glCullFace( mode );
1179 void glFrustum( GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat zNear, GLfloat zFar )
1181 FlushOnStateChange( );
1182 glEsImpl->glFrustumf( left, right, bottom, top, zNear, zFar );
1185 void glClear( GLbitfield mask )
1187 FlushOnStateChange( );
1188 glEsImpl->glClear( mask );
1191 void glVertex3f( GLfloat x, GLfloat y, GLfloat z )
1193 GLfloat *vert = (GLfloat *)ptrVertexAttribArray++;
1194 *vert++ = x;
1195 *vert++ = y;
1196 *vert++ = z;
1197 #if defined( __MULTITEXTURE_SUPPORT__ )
1198 memcpy( vert, &currentVertexAttrib.red, 5 * sizeof( GLfloat ) );
1199 #else
1200 memcpy( vert + 1, &currentVertexAttrib.red, 3 * sizeof( GLfloat ) );
1201 #endif
1204 void glColor4fv( const GLfloat *v )
1205 {
1206 if( nanoglState.stupidglesbug )
1208 currentVertexAttrib.red = (unsigned char)ClampTo255( ( v[0] * v[3] ) * 255.0f );
1209 currentVertexAttrib.green = (unsigned char)ClampTo255( ( v[1] * v[3] ) * 255.0f );
1210 currentVertexAttrib.blue = (unsigned char)ClampTo255( ( v[2] * v[3] ) * 255.0f );
1212 else
1214 currentVertexAttrib.red = (unsigned char)ClampTo255( v[0] * 255.0f );
1215 currentVertexAttrib.green = (unsigned char)ClampTo255( v[1] * 255.0f );
1216 currentVertexAttrib.blue = (unsigned char)ClampTo255( v[2] * 255.0f );
1218 currentVertexAttrib.alpha = (unsigned char)ClampTo255( v[3] * 255.0f );
1219 if( skipnanogl )
1220 glEsImpl->glColor4f( currentVertexAttrib.red/255.0f, currentVertexAttrib.green/255.0f,
1221 currentVertexAttrib.blue/255.0f, currentVertexAttrib.alpha/255.0f );
1224 void glColor3ubv( const GLubyte *v )
1226 currentVertexAttrib.red = v[0];
1227 currentVertexAttrib.green = v[1];
1228 currentVertexAttrib.blue = v[2];
1229 currentVertexAttrib.alpha = 255;
1230 if( skipnanogl )
1231 glEsImpl->glColor4f( currentVertexAttrib.red/255.0f, currentVertexAttrib.green/255.0f,
1232 currentVertexAttrib.blue/255.0f, currentVertexAttrib.alpha/255.0f );
1235 void glColor4ubv( const GLubyte *v )
1237 //*((unsigned int*)(&currentVertexAttrib.red)) = *((unsigned int*)(v));
1238 if( nanoglState.stupidglesbug )
1240 currentVertexAttrib.red = (unsigned char)ClampTo255( v[0] * v[3] / 255.0f );
1241 currentVertexAttrib.green = (unsigned char)ClampTo255( v[1] * v[3] / 255.0f );
1242 currentVertexAttrib.blue = (unsigned char)ClampTo255( v[2] * v[3] / 255.0f );
1244 else
1246 currentVertexAttrib.red = v[0];
1247 currentVertexAttrib.green = v[1];
1248 currentVertexAttrib.blue = v[2];
1250 currentVertexAttrib.alpha = v[3];
1251 if( skipnanogl )
1252 glEsImpl->glColor4f( currentVertexAttrib.red/255.0f, currentVertexAttrib.green/255.0f,
1253 currentVertexAttrib.blue/255.0f, currentVertexAttrib.alpha/255.0f );
1256 void glColor3fv( const GLfloat *v )
1258 currentVertexAttrib.red = (unsigned char)ClampTo255( v[0] * 255.0f );
1259 currentVertexAttrib.green = (unsigned char)ClampTo255( v[1] * 255.0f );
1260 currentVertexAttrib.blue = (unsigned char)ClampTo255( v[2] * 255.0f );
1261 currentVertexAttrib.alpha = 255;
1262 if( skipnanogl )
1263 glEsImpl->glColor4f( currentVertexAttrib.red/255.0f, currentVertexAttrib.green/255.0f,
1264 currentVertexAttrib.blue/255.0f, currentVertexAttrib.alpha/255.0f );
1267 //-- nicknekit: xash3d funcs --
1269 void glColor4ub( GLubyte red, GLubyte green, GLubyte blue, GLubyte alpha )
1271 if( nanoglState.stupidglesbug )
1273 currentVertexAttrib.red = (unsigned char)ClampTo255( red * alpha / 255.0f );
1274 currentVertexAttrib.green = (unsigned char)ClampTo255( green * alpha / 255.0f );
1275 currentVertexAttrib.blue = (unsigned char)ClampTo255( blue * alpha / 255.0f );
1277 else
1279 currentVertexAttrib.red = red;
1280 currentVertexAttrib.green = green;
1281 currentVertexAttrib.blue = blue;
1283 currentVertexAttrib.alpha = alpha;
1284 if( skipnanogl )
1285 glEsImpl->glColor4f( currentVertexAttrib.red/255.0f, currentVertexAttrib.green/255.0f,
1286 currentVertexAttrib.blue/255.0f, currentVertexAttrib.alpha/255.0f );
1289 void glColor3ub( GLubyte red, GLubyte green, GLubyte blue )
1291 currentVertexAttrib.red = red;
1292 currentVertexAttrib.green = green;
1293 currentVertexAttrib.blue = blue;
1294 currentVertexAttrib.alpha = 255;
1295 if( skipnanogl )
1296 glEsImpl->glColor4f( currentVertexAttrib.red/255.0f, currentVertexAttrib.green/255.0f,
1297 currentVertexAttrib.blue/255.0f, currentVertexAttrib.alpha/255.0f );
1300 void glNormal3fv( const GLfloat *v )
1302 FlushOnStateChange( );
1303 glEsImpl->glNormal3f( v[0], v[1], v[2] );
1306 void glCopyTexImage2D( GLenum target, GLint level, GLenum internalformat, GLint x, GLint y, GLsizei width, GLsizei height, GLint border )
1308 FlushOnStateChange( );
1309 glEsImpl->glCopyTexImage2D( target, level, internalformat, x, y, width, height, border );
1312 void glTexImage1D( GLenum target, GLint level, GLint internalformat, GLsizei width, GLint border, GLenum format, GLenum type, const GLvoid *pixels )
1314 glTexImage2D( GL_TEXTURE_2D, level, internalformat, width, 1, border, format, type, pixels );
1317 void glTexImage3D( GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid *pixels )
1319 glTexImage2D( GL_TEXTURE_2D, level, internalformat, width, height, border, format, type, pixels );
1322 void glTexSubImage1D( GLenum target, GLint level, GLint xoffset, GLsizei width, GLenum format, GLenum type, const GLvoid *pixels )
1324 glTexSubImage2D( target, level, xoffset, 0, width, 1, format, type, pixels );
1327 void glTexSubImage3D( GLenum target, GLint level,
1328 GLint xoffset, GLint yoffset,
1329 GLint zoffset, GLsizei width,
1330 GLsizei height, GLsizei depth,
1331 GLenum format,
1332 GLenum type, const GLvoid *pixels )
1334 glTexSubImage2D( target, level, xoffset, yoffset, width, height, format, type, pixels );
1337 GLboolean glIsTexture( GLuint texture )
1339 FlushOnStateChange( );
1340 return glEsImpl->glIsTexture( texture );
1343 // TODO: add native normal/reflection map texgen support
1345 void glTexGeni( GLenum coord, GLenum pname, GLint param )
1347 FlushOnStateChange();
1348 //glEsImpl->glTexGeniOES( coord, pname, param );
1351 void glTexGenfv( GLenum coord, GLenum pname, const GLfloat *params )
1353 FlushOnStateChange();
1354 //glEsImpl->glTexGenfvOES( coord, pname, params );
1357 //-- --//
1359 void glHint( GLenum target, GLenum mode )
1361 FlushOnStateChange( );
1362 glEsImpl->glHint( target, mode );
1365 void glBlendFunc( GLenum sfactor, GLenum dfactor )
1367 if( skipnanogl )
1369 glEsImpl->glBlendFunc( sfactor, dfactor );
1370 return;
1372 if ( ( nanoglState.sfactor == sfactor ) && ( nanoglState.dfactor == dfactor ) )
1374 return;
1377 if( sfactor == GL_SRC_ALPHA && dfactor == GL_ONE )
1379 sfactor = GL_ONE; // workaround gles bug
1380 nanoglState.stupidglesbug = GL_TRUE;
1382 else
1383 nanoglState.stupidglesbug = GL_FALSE;
1385 nanoglState.sfactor = sfactor;
1386 nanoglState.dfactor = dfactor;
1387 FlushOnStateChange( );
1388 glEsImpl->glBlendFunc( sfactor, dfactor );
1391 void glPopMatrix( void )
1393 FlushOnStateChange( );
1394 glEsImpl->glPopMatrix( );
1397 void glShadeModel( GLenum mode )
1399 if ( nanoglState.shademodel == mode )
1401 return;
1403 nanoglState.shademodel = mode;
1404 FlushOnStateChange( );
1405 glEsImpl->glShadeModel( mode );
1408 void glPushMatrix( void )
1410 FlushOnStateChange( );
1411 glEsImpl->glPushMatrix( );
1414 void glTexEnvf( GLenum target, GLenum pname, GLfloat param )
1416 if( skipnanogl )
1418 glEsImpl->glTexEnvf( target, pname, param );
1419 return;
1421 if ( target == GL_TEXTURE_ENV )
1423 if ( pname == GL_TEXTURE_ENV_MODE )
1425 if ( param == activetmuState->texture_env_mode.value )
1427 return;
1429 else
1431 FlushOnStateChange( );
1432 glEsImpl->glTexEnvf( target, pname, param );
1433 activetmuState->texture_env_mode.value = param;
1434 return;
1438 FlushOnStateChange( );
1439 glEsImpl->glTexEnvf( target, pname, param );
1442 void glVertex3fv( const GLfloat *v )
1444 GLfloat *vert = (GLfloat *)ptrVertexAttribArray++;
1445 memcpy( vert, v, 3 * sizeof( GLfloat ) );
1446 #if defined( __MULTITEXTURE_SUPPORT__ )
1447 memcpy( vert + 3, &currentVertexAttrib.red, 5 * sizeof( GLfloat ) );
1448 #else
1449 memcpy( vert + 4, &currentVertexAttrib.red, 3 * sizeof( GLfloat ) );
1450 #endif
1453 void glDepthMask( GLboolean flag )
1455 if( !skipnanogl )
1457 if ( nanoglState.depthmask == flag )
1459 return;
1461 nanoglState.depthmask = flag;
1462 FlushOnStateChange( );
1464 glEsImpl->glDepthMask( flag );
1467 void glBindTexture( GLenum target, GLuint texture )
1469 if( skipnanogl )
1471 glEsImpl->glBindTexture( target,texture );
1472 return;
1474 if ( activetmuState->boundtexture.value == texture )
1476 return;
1478 FlushOnStateChange( );
1479 activetmuState->boundtexture.value = texture;
1480 glEsImpl->glBindTexture( target, texture );
1483 void glGetIntegerv( GLenum pname, GLint *params )
1485 FlushOnStateChange( );
1486 glEsImpl->glGetIntegerv( pname, params );
1489 GLubyte nano_extensions_string[4096];
1490 const GLubyte *glGetString( GLenum name )
1493 if ( name == GL_EXTENSIONS )
1495 #if defined( __MULTITEXTURE_SUPPORT__ )
1496 sprintf( (char *)nano_extensions_string, "%s %s", glEsImpl->glGetString( name ), "GL_ARB_multitexture EXT_texture_env_add" );
1497 #else
1498 sprintf( (char *)nano_extensions_string, "%s %s", glEsImpl->glGetString( name ), "EXT_texture_env_add" );
1499 #endif
1500 return nano_extensions_string;
1502 return glEsImpl->glGetString( name );
1505 void glAlphaFunc( GLenum func, GLclampf ref )
1507 FlushOnStateChange( );
1508 glEsImpl->glAlphaFunc( func, ref );
1511 void glFlush( void )
1513 FlushOnStateChange( );
1514 glEsImpl->glFlush( );
1517 void glReadPixels( GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *pixels )
1519 if ( format == GL_DEPTH_COMPONENT )
1521 // OpenglEs 1.1 does not support reading depth buffer without an extension
1522 memset( pixels, 0xff, 4 );
1523 return;
1525 FlushOnStateChange( );
1526 glEsImpl->glReadPixels( x, y, width, height, format, type, pixels );
1529 void glReadBuffer( GLenum /*mode*/ )
1533 void glLoadMatrixf( const GLfloat *m )
1535 FlushOnStateChange( );
1536 glEsImpl->glLoadMatrixf( m );
1539 void glTexSubImage2D( GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels )
1541 FlushOnStateChange( );
1542 glEsImpl->glTexSubImage2D( target, level, xoffset, yoffset, width, height, format, type, pixels );
1545 void glClearColor( GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha )
1547 FlushOnStateChange( );
1548 glEsImpl->glClearColor( red, green, blue, alpha );
1551 GLenum glGetError( void )
1553 //FlushOnStateChange();
1554 return GL_NO_ERROR; //glEsImpl->glGetError();
1557 void glActiveTexture( GLenum texture )
1559 if( skipnanogl )
1561 glEsImpl->glActiveTexture( texture );
1562 return;
1564 if ( activetmu == texture )
1566 return;
1568 if ( delayedttmuchange )
1570 delayedttmuchange = GL_FALSE;
1572 else
1574 delayedttmuchange = GL_TRUE;
1575 delayedtmutarget = texture;
1577 if ( texture == GL_TEXTURE0 )
1579 activetmuState = &tmuState0;
1581 else
1583 activetmuState = &tmuState1;
1585 activetmu = texture;
1588 void glClientActiveTexture( GLenum texture )
1590 if( skipnanogl )
1592 glEsImpl->glClientActiveTexture( texture );
1593 return;
1595 clientactivetmu = texture;
1598 void glPolygonMode( GLenum face, GLenum mode )
1602 void glDeleteTextures( GLsizei n, const GLuint *textures )
1604 FlushOnStateChange( );
1605 glEsImpl->glDeleteTextures( n, textures );
1608 void glClearDepth( GLclampf depth )
1610 FlushOnStateChange( );
1611 glEsImpl->glClearDepthf( depth );
1614 void glClipPlane( GLenum plane, const GLdouble *equation )
1616 FlushOnStateChange( );
1617 float array[4];
1618 array[0] = ( GLfloat )( equation[0] );
1619 array[1] = ( GLfloat )( equation[1] );
1620 array[2] = ( GLfloat )( equation[2] );
1621 array[3] = ( GLfloat )( equation[3] );
1622 glEsImpl->glClipPlanef( plane, array );
1625 void glScissor( GLint x, GLint y, GLsizei width, GLsizei height )
1627 FlushOnStateChange( );
1628 glEsImpl->glScissor( x, y, width, height );
1631 void glPointSize( GLfloat size )
1633 FlushOnStateChange( );
1634 glEsImpl->glPointSize( size );
1637 void glArrayElement( GLint i )
1640 void glLineWidth( GLfloat width )
1643 void glCallList( GLuint list )
1646 void glColorMask( GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha )
1649 void glStencilFunc( GLenum func, GLint ref, GLuint mask )
1652 void glStencilOp( GLenum fail, GLenum zfail, GLenum zpass )
1656 struct ptrstate vertex_array;
1657 struct ptrstate color_array;
1658 struct ptrstate texture_coord_array;
1660 void glDrawElements( GLenum mode, GLsizei count, GLenum type, const GLvoid *indices )
1662 if( skipnanogl )
1664 glEsImpl->glDrawElements( mode, count, type, indices );
1665 return;
1667 // ensure that all primitives specified between glBegin/glEnd pairs
1668 // are rendered first, and that we have correct tmu in use..
1669 FlushOnStateChange( );
1670 // setup correct vertex/color/texcoord pointers
1671 if ( arraysValid ||
1672 tmuState0.vertex_array.changed ||
1673 tmuState0.color_array.changed ||
1674 tmuState0.texture_coord_array.changed || tmuState0.normal_array.changed )
1676 glEsImpl->glClientActiveTexture( GL_TEXTURE0 );
1678 if ( arraysValid || tmuState0.vertex_array.changed )
1680 if ( tmuState0.vertex_array.enabled )
1682 glEsImpl->glEnableClientState( GL_VERTEX_ARRAY );
1684 else
1686 glEsImpl->glDisableClientState( GL_VERTEX_ARRAY );
1688 glEsImpl->glVertexPointer( tmuState0.vertex_array.size,
1689 tmuState0.vertex_array.type,
1690 tmuState0.vertex_array.stride,
1691 tmuState0.vertex_array.ptr );
1692 tmuState0.vertex_array.changed = GL_FALSE;
1694 if ( arraysValid || tmuState0.color_array.changed )
1696 if ( tmuState0.color_array.enabled )
1698 glEsImpl->glEnableClientState( GL_COLOR_ARRAY );
1700 else
1702 glEsImpl->glDisableClientState( GL_COLOR_ARRAY );
1704 glEsImpl->glColorPointer( tmuState0.color_array.size,
1705 tmuState0.color_array.type,
1706 tmuState0.color_array.stride,
1707 tmuState0.color_array.ptr );
1708 tmuState0.color_array.changed = GL_FALSE;
1710 if ( arraysValid || tmuState0.normal_array.changed )
1712 if ( tmuState0.normal_array.enabled )
1714 glEsImpl->glEnableClientState( GL_NORMAL_ARRAY );
1716 else
1718 glEsImpl->glDisableClientState( GL_NORMAL_ARRAY );
1720 glEsImpl->glNormalPointer( tmuState0.normal_array.type,
1721 tmuState0.normal_array.stride,
1722 tmuState0.normal_array.ptr );
1723 tmuState0.normal_array.changed = GL_FALSE;
1725 if ( arraysValid || tmuState0.texture_coord_array.changed )
1727 tmuState0.texture_coord_array.changed = GL_FALSE;
1728 if ( tmuState0.texture_coord_array.enabled )
1730 glEsImpl->glEnableClientState( GL_TEXTURE_COORD_ARRAY );
1732 else
1734 glEsImpl->glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1736 glEsImpl->glTexCoordPointer( tmuState0.texture_coord_array.size,
1737 tmuState0.texture_coord_array.type,
1738 tmuState0.texture_coord_array.stride,
1739 tmuState0.texture_coord_array.ptr );
1742 if ( arraysValid || tmuState1.texture_coord_array.changed )
1744 tmuState1.texture_coord_array.changed = GL_FALSE;
1745 glEsImpl->glClientActiveTexture( GL_TEXTURE1 );
1746 if ( tmuState1.texture_coord_array.enabled )
1748 glEsImpl->glEnableClientState( GL_TEXTURE_COORD_ARRAY );
1750 else
1752 glEsImpl->glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1754 glEsImpl->glTexCoordPointer( tmuState1.texture_coord_array.size,
1755 tmuState1.texture_coord_array.type,
1756 tmuState1.texture_coord_array.stride,
1757 tmuState1.texture_coord_array.ptr );
1760 arraysValid = GL_FALSE;
1761 glEsImpl->glDrawElements( mode, count, type, indices );
1764 bool vboarray;
1766 void glEnableClientState( GLenum array )
1768 if( skipnanogl )
1770 glEsImpl->glEnableClientState( array );
1771 if( array == GL_VERTEX_ARRAY )
1772 vboarray = true;
1773 return;
1775 struct nanotmuState *clientstate = NULL;
1776 if ( clientactivetmu == GL_TEXTURE0 )
1778 clientstate = &tmuState0;
1780 else if ( clientactivetmu == GL_TEXTURE1 )
1782 clientstate = &tmuState1;
1784 else
1786 return;
1788 switch ( array )
1790 case GL_VERTEX_ARRAY:
1791 if ( clientstate->vertex_array.enabled )
1793 return;
1795 clientstate->vertex_array.enabled = GL_TRUE;
1796 clientstate->vertex_array.changed = GL_TRUE;
1797 break;
1798 case GL_COLOR_ARRAY:
1799 if ( clientstate->color_array.enabled )
1801 return;
1803 clientstate->color_array.enabled = GL_TRUE;
1804 clientstate->color_array.changed = GL_TRUE;
1806 break;
1807 case GL_NORMAL_ARRAY:
1808 if ( clientstate->normal_array.enabled )
1810 return;
1812 clientstate->normal_array.enabled = GL_TRUE;
1813 clientstate->normal_array.changed = GL_TRUE;
1815 break;
1816 case GL_TEXTURE_COORD_ARRAY:
1817 if ( clientstate->texture_coord_array.enabled )
1819 return;
1821 clientstate->texture_coord_array.enabled = GL_TRUE;
1822 clientstate->texture_coord_array.changed = GL_TRUE;
1823 break;
1824 default:
1825 break;
1828 void glDisableClientState( GLenum array )
1830 if( skipnanogl )
1832 glEsImpl->glDisableClientState( array );
1833 if( array == GL_VERTEX_ARRAY )
1834 vboarray = false;
1835 return;
1837 struct nanotmuState *clientstate = NULL;
1838 if ( clientactivetmu == GL_TEXTURE0 )
1840 clientstate = &tmuState0;
1842 else if ( clientactivetmu == GL_TEXTURE1 )
1844 clientstate = &tmuState1;
1846 else
1848 return;
1850 switch ( array )
1852 case GL_VERTEX_ARRAY:
1853 if ( !clientstate->vertex_array.enabled )
1855 return;
1857 clientstate->vertex_array.enabled = GL_FALSE;
1858 clientstate->vertex_array.changed = GL_TRUE;
1859 break;
1860 case GL_COLOR_ARRAY:
1861 if ( !clientstate->color_array.enabled )
1863 return;
1865 clientstate->color_array.enabled = GL_FALSE;
1866 clientstate->color_array.changed = GL_TRUE;
1868 break;
1869 case GL_NORMAL_ARRAY:
1870 if ( !clientstate->normal_array.enabled )
1872 return;
1874 clientstate->normal_array.enabled = GL_FALSE;
1875 clientstate->normal_array.changed = GL_TRUE;
1877 break;
1878 case GL_TEXTURE_COORD_ARRAY:
1879 if ( !clientstate->texture_coord_array.enabled )
1881 return;
1883 clientstate->texture_coord_array.enabled = GL_FALSE;
1884 clientstate->texture_coord_array.changed = GL_TRUE;
1885 break;
1886 default:
1887 break;
1890 void glVertexPointer( GLint size, GLenum type, GLsizei stride, const GLvoid *pointer )
1893 if( skipnanogl )
1895 glEsImpl->glVertexPointer( size, type, stride, pointer );
1896 return;
1898 if ( tmuState0.vertex_array.size == size &&
1899 tmuState0.vertex_array.stride == stride &&
1900 tmuState0.vertex_array.type == type &&
1901 tmuState0.vertex_array.ptr == pointer )
1903 return;
1905 tmuState0.vertex_array.size = size;
1906 tmuState0.vertex_array.stride = stride;
1907 tmuState0.vertex_array.type = type;
1908 tmuState0.vertex_array.ptr = (GLvoid *)pointer;
1909 tmuState0.vertex_array.changed = GL_TRUE;
1911 void glTexCoordPointer( GLint size, GLenum type, GLsizei stride, const GLvoid *pointer )
1913 if( skipnanogl )
1915 glEsImpl->glTexCoordPointer( size, type, stride, pointer );
1916 return;
1918 struct nanotmuState *clientstate = NULL;
1919 if ( clientactivetmu == GL_TEXTURE0 )
1921 clientstate = &tmuState0;
1923 else if ( clientactivetmu == GL_TEXTURE1 )
1925 clientstate = &tmuState1;
1927 if ( clientstate->texture_coord_array.size == size &&
1928 clientstate->texture_coord_array.stride == stride &&
1929 clientstate->texture_coord_array.type == type &&
1930 clientstate->texture_coord_array.ptr == pointer )
1932 return;
1934 clientstate->texture_coord_array.size = size;
1935 clientstate->texture_coord_array.stride = stride;
1936 clientstate->texture_coord_array.type = type;
1937 clientstate->texture_coord_array.ptr = (GLvoid *)pointer;
1938 clientstate->texture_coord_array.changed = GL_TRUE;
1940 void glColorPointer( GLint size, GLenum type, GLsizei stride, const GLvoid *pointer )
1942 if ( tmuState0.color_array.size == size &&
1943 tmuState0.color_array.stride == stride &&
1944 tmuState0.color_array.type == type &&
1945 tmuState0.color_array.ptr == pointer )
1947 return;
1949 tmuState0.color_array.size = size;
1950 tmuState0.color_array.stride = stride;
1951 tmuState0.color_array.type = type;
1952 tmuState0.color_array.ptr = (GLvoid *)pointer;
1953 tmuState0.color_array.changed = GL_TRUE;
1956 void glNormalPointer( GLenum type, GLsizei stride, const GLvoid *pointer )
1958 int size = 0;
1959 if ( tmuState0.normal_array.size == size &&
1960 tmuState0.normal_array.stride == stride &&
1961 tmuState0.normal_array.type == type &&
1962 tmuState0.normal_array.ptr == pointer )
1964 return;
1966 tmuState0.normal_array.size = size;
1967 tmuState0.normal_array.stride = stride;
1968 tmuState0.normal_array.type = type;
1969 tmuState0.normal_array.ptr = (GLvoid *)pointer;
1970 tmuState0.normal_array.changed = GL_TRUE;
1972 void glPolygonOffset( GLfloat factor, GLfloat units )
1974 FlushOnStateChange( );
1975 glEsImpl->glPolygonOffset( factor, units );
1977 void glStencilMask( GLuint mask )
1980 void glClearStencil( GLint s )
1984 #if defined( __MULTITEXTURE_SUPPORT__ )
1986 extern "C" void glMultiTexCoord2fARB( GLenum target, GLfloat s, GLfloat t );
1988 void glMultiTexCoord2fARB( GLenum target, GLfloat s, GLfloat t )
1990 if ( target == GL_TEXTURE0 )
1992 glTexCoord2f( s, t );
1994 else
1996 currentVertexAttrib.s_multi = s;
1997 currentVertexAttrib.t_multi = t;
2000 #endif
2002 /* Vladimir */
2003 /*void glDrawArrays( GLenum mode, int first, int count)
2005 FlushOnStateChange();
2006 glEsImpl->glDrawArrays(mode, first , count);
2007 }*/
2008 void glMultMatrixf( const GLfloat *m )
2010 FlushOnStateChange( );
2011 glEsImpl->glMultMatrixf( m );
2014 void glPixelStorei( GLenum pname, GLint param )
2016 FlushOnStateChange( );
2017 glEsImpl->glPixelStorei( pname, param );
2020 void glFogf( GLenum pname, GLfloat param )
2022 FlushOnStateChange( );
2023 glEsImpl->glFogf( pname, param );
2026 void glFogfv( GLenum pname, const GLfloat *params )
2028 FlushOnStateChange( );
2029 glEsImpl->glFogfv( pname, params );
2032 void glGetTexParameteriv( GLenum target, GLenum pname, GLint *params )
2034 FlushOnStateChange( );
2035 glEsImpl->glGetTexParameteriv( target, pname, params );
2038 // This gives: called unimplemented OpenGL ES API (Android)
2039 void glTexParameteri( GLenum target, GLenum pname, GLint param )
2041 if ( pname == GL_TEXTURE_BORDER_COLOR )
2043 return; // not supported by opengl es
2045 if ( ( pname == GL_TEXTURE_WRAP_S ||
2046 pname == GL_TEXTURE_WRAP_T ) &&
2047 param == GL_CLAMP )
2049 param = 0x812F;
2052 FlushOnStateChange( );
2053 glEsImpl->glTexParameteri( target, pname, param );
2056 void glTexParameterx( GLenum target, GLenum pname, GLfixed param )
2058 if ( pname == GL_TEXTURE_BORDER_COLOR )
2060 return; // not supported by opengl es
2062 if ( ( pname == GL_TEXTURE_WRAP_S ||
2063 pname == GL_TEXTURE_WRAP_T ) &&
2064 param == GL_CLAMP )
2066 param = 0x812F;
2068 FlushOnStateChange( );
2069 glEsImpl->glTexParameterx( target, pname, param );
2072 void glGenTextures( GLsizei n, GLuint *textures )
2074 FlushOnStateChange( );
2075 glEsImpl->glGenTextures( n, textures );
2078 void glFrontFace( GLenum mode )
2080 FlushOnStateChange( );
2081 glEsImpl->glFrontFace( mode );
2083 // End Vladimir
2085 void glTexEnvi( GLenum target, GLenum pname, GLint param )
2087 if( skipnanogl )
2089 glEsImpl->glTexEnvi( target, pname, param );
2090 return;
2092 if ( target == GL_TEXTURE_ENV )
2094 if ( pname == GL_TEXTURE_ENV_MODE )
2096 if ( param == activetmuState->texture_env_mode.value )
2098 return;
2100 else
2102 FlushOnStateChange( );
2103 glEsImpl->glTexEnvi( target, pname, param );
2104 activetmuState->texture_env_mode.value = param;
2105 return;
2109 FlushOnStateChange( );
2110 glEsImpl->glTexEnvi( target, pname, param );
2113 #ifdef __MULTITEXTURE_SUPPORT__
2114 void glMultiTexCoord3fARB( GLenum a, GLfloat b, GLfloat c, GLfloat )
2116 return glMultiTexCoord2fARB( a, b, c );
2119 void glMultiTexCoord2f( GLenum a, GLfloat b, GLfloat c )
2121 glMultiTexCoord2fARB(a,b,c);
2123 #endif
2124 void glDrawArrays( GLenum mode, GLint first, GLsizei count )
2126 if( skipnanogl )
2128 glEsImpl->glDrawArrays( mode, first, count );
2129 return;
2131 // ensure that all primitives specified between glBegin/glEnd pairs
2132 // are rendered first, and that we have correct tmu in use..
2133 if ( mode == GL_QUADS )
2134 mode = GL_TRIANGLE_FAN;
2135 FlushOnStateChange( );
2136 // setup correct vertex/color/texcoord pointers
2137 if ( arraysValid ||
2138 tmuState0.vertex_array.changed ||
2139 tmuState0.color_array.changed ||
2140 tmuState0.texture_coord_array.changed || tmuState0.normal_array.changed )
2142 glEsImpl->glClientActiveTexture( GL_TEXTURE0 );
2144 if ( arraysValid || tmuState0.vertex_array.changed )
2146 if ( tmuState0.vertex_array.enabled )
2148 glEsImpl->glEnableClientState( GL_VERTEX_ARRAY );
2150 else
2152 glEsImpl->glDisableClientState( GL_VERTEX_ARRAY );
2154 glEsImpl->glVertexPointer( tmuState0.vertex_array.size,
2155 tmuState0.vertex_array.type,
2156 tmuState0.vertex_array.stride,
2157 tmuState0.vertex_array.ptr );
2158 tmuState0.vertex_array.changed = GL_FALSE;
2160 if ( arraysValid || tmuState0.color_array.changed )
2162 if ( tmuState0.color_array.enabled )
2164 glEsImpl->glEnableClientState( GL_COLOR_ARRAY );
2166 else
2168 glEsImpl->glDisableClientState( GL_COLOR_ARRAY );
2170 glEsImpl->glColorPointer( tmuState0.color_array.size,
2171 tmuState0.color_array.type,
2172 tmuState0.color_array.stride,
2173 tmuState0.color_array.ptr );
2174 tmuState0.color_array.changed = GL_FALSE;
2176 if ( arraysValid || tmuState0.normal_array.changed )
2178 if ( tmuState0.normal_array.enabled )
2180 glEsImpl->glEnableClientState( GL_NORMAL_ARRAY );
2182 else
2184 glEsImpl->glDisableClientState( GL_NORMAL_ARRAY );
2186 glEsImpl->glNormalPointer( tmuState0.normal_array.type,
2187 tmuState0.normal_array.stride,
2188 tmuState0.normal_array.ptr );
2189 tmuState0.normal_array.changed = GL_FALSE;
2191 if ( arraysValid || tmuState0.texture_coord_array.changed )
2193 tmuState0.texture_coord_array.changed = GL_FALSE;
2194 if ( tmuState0.texture_coord_array.enabled )
2196 glEsImpl->glEnableClientState( GL_TEXTURE_COORD_ARRAY );
2198 else
2200 glEsImpl->glDisableClientState( GL_TEXTURE_COORD_ARRAY );
2202 glEsImpl->glTexCoordPointer( tmuState0.texture_coord_array.size,
2203 tmuState0.texture_coord_array.type,
2204 tmuState0.texture_coord_array.stride,
2205 tmuState0.texture_coord_array.ptr );
2208 if ( arraysValid || tmuState1.texture_coord_array.changed )
2210 tmuState1.texture_coord_array.changed = GL_FALSE;
2211 glEsImpl->glClientActiveTexture( GL_TEXTURE1 );
2212 if ( tmuState1.texture_coord_array.enabled )
2214 glEsImpl->glEnableClientState( GL_TEXTURE_COORD_ARRAY );
2216 else
2218 glEsImpl->glDisableClientState( GL_TEXTURE_COORD_ARRAY );
2220 glEsImpl->glTexCoordPointer( tmuState1.texture_coord_array.size,
2221 tmuState1.texture_coord_array.type,
2222 tmuState1.texture_coord_array.stride,
2223 tmuState1.texture_coord_array.ptr );
2226 arraysValid = GL_FALSE;
2227 glEsImpl->glDrawArrays( mode, first, count );
2229 /*void glNormalPointer(GLenum type, GLsizei stride, const void *ptr)
2231 glEsImpl->glNormalPointer( type, stride, ptr );
2232 }*/
2234 void glCopyTexSubImage2D( GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height )
2236 FlushOnStateChange( );
2237 glEsImpl->glCopyTexSubImage2D( target, level, xoffset, yoffset, x, y, width, height );
2240 void glGenFramebuffers( GLsizei n, GLuint *framebuffers )
2242 FlushOnStateChange( );
2243 glEsImpl->glGenFramebuffers( n, framebuffers );
2246 void glGenRenderbuffers( GLsizei n, GLuint *renderbuffers )
2248 FlushOnStateChange( );
2249 glEsImpl->glGenRenderbuffers( n, renderbuffers );
2252 void glBindRenderbuffer( GLenum target, GLuint renderbuffer )
2254 FlushOnStateChange( );
2255 glEsImpl->glBindRenderbuffer( target, renderbuffer );
2258 void glBindFramebuffer( GLenum target, GLuint framebuffer )
2260 FlushOnStateChange( );
2261 glEsImpl->glBindFramebuffer( target, framebuffer );
2264 void glFramebufferRenderbuffer( GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer )
2266 FlushOnStateChange( );
2267 glEsImpl->glFramebufferRenderbuffer( target, attachment, renderbuffertarget, renderbuffer );
2270 void glDeleteFramebuffers( GLsizei n, const GLuint *framebuffers )
2272 FlushOnStateChange( );
2273 glEsImpl->glDeleteFramebuffers( n, framebuffers );
2276 void glDeleteRenderbuffers( GLsizei n, const GLuint *renderbuffers )
2278 FlushOnStateChange( );
2279 glEsImpl->glDeleteRenderbuffers( n, renderbuffers );
2281 void glFramebufferTexture2D( GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level )
2283 FlushOnStateChange( );
2284 glEsImpl->glFramebufferTexture2D( target, attachment, textarget, texture, level );
2287 void glRenderbufferStorage( GLenum target, GLenum internalformat, GLsizei width, GLsizei height )
2289 FlushOnStateChange( );
2290 glEsImpl->glRenderbufferStorage( target, internalformat, width, height );
2293 void glBindBufferARB( GLuint target, GLuint index )
2295 static int sindex;
2297 if( index && !sindex && !skipnanogl )
2298 FlushOnStateChange();
2299 glEsImpl->glDisableClientState( GL_COLOR_ARRAY );
2300 glEsImpl->glColor4f( currentVertexAttrib.red/255.0f, currentVertexAttrib.green/255.0f,
2301 currentVertexAttrib.blue/255.0f, currentVertexAttrib.alpha/255.0f );
2303 skipnanogl = (!!index) || vboarray;
2304 glEsImpl->glBindBuffer( target, index );
2305 if( sindex && !index )
2307 arraysValid = GL_FALSE;
2309 sindex = index;
2312 void glGenBuffersARB( GLuint count, GLuint *indexes )
2314 glEsImpl->glGenBuffers( count, indexes );
2317 void glDeleteBuffersARB( GLuint count, GLuint *indexes )
2319 glEsImpl->glDeleteBuffers( count, indexes );
2322 void glBufferDataARB( GLuint target, GLuint size, void *buffer, GLuint type )
2324 glEsImpl->glBufferData( target, size, buffer, type );
2327 void glBufferSubDataARB( GLuint target, GLsizei offset, GLsizei size, void *buffer )
2329 glEsImpl->glBufferSubData( target, offset, size, buffer );