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.
29 #include "glesinterface.h"
31 #define GL_TEXTURE0_ARB 0x84C0
32 #define GL_TEXTURE1_ARB 0x84C1
38 GLboolean clip_planei
;
39 GLboolean color_logic_op
;
40 GLboolean color_material
;
47 GLboolean line_smooth
;
48 GLboolean matrix_palette_oes
;
49 GLboolean multisample
;
51 GLboolean point_smooth
;
52 GLboolean point_sprite_oes
;
53 GLboolean polygon_offset_fill
;
54 GLboolean rescale_normal
;
55 GLboolean sample_alpha_to_coverage
;
56 GLboolean sample_alpha_to_one
;
57 GLboolean sample_coverage
;
58 GLboolean scissor_test
;
59 GLboolean stencil_test
;
61 GLclampf depth_range_near
;
62 GLclampf depth_range_far
;
71 static struct nanoState nanoglState
;
73 static struct nanoState nanoglInitState
=
141 struct booleanstate texture_2d
;
142 struct floatstate texture_env_mode
;
143 struct uintstate boundtexture
;
144 struct ptrstate vertex_array
;
145 struct ptrstate color_array
;
146 struct ptrstate texture_coord_array
;
149 static struct nanotmuState tmuState0
;
150 static struct nanotmuState tmuState1
;
152 static struct nanotmuState tmuInitState
=
154 {GL_FALSE
, GL_FALSE
},
155 {GL_MODULATE
,GL_FALSE
},
156 {0x7fffffff,GL_FALSE
},
157 {4,GL_FLOAT
,0, NULL
, GL_FALSE
, GL_FALSE
},
158 {4,GL_FLOAT
,0, NULL
, GL_FALSE
, GL_FALSE
},
159 {4,GL_FLOAT
,0, NULL
, GL_FALSE
, GL_FALSE
},
162 static struct nanotmuState
* activetmuState
= &tmuState0
;
164 extern "C++" GlESInterface
* glEsImpl
;
166 static GLenum wrapperPrimitiveMode
= GL_QUADS
;
167 GLboolean useTexCoordArray
= GL_FALSE
;
168 static GLenum activetmu
= GL_TEXTURE0
;
169 static GLenum clientactivetmu
= GL_TEXTURE0
;
171 #if defined(__MULTITEXTURE_SUPPORT__)
172 GLboolean useMultiTexCoordArray
= GL_FALSE
;
176 #if !defined (__WINS__)
177 //#define __FORCEINLINE __forceinline
178 #define __FORCEINLINE inline
180 #define __FORCEINLINE
183 static GLboolean delayedttmuchange
= GL_FALSE
;
184 static GLenum delayedtmutarget
= GL_TEXTURE0
;
191 #if !defined(__MULTITEXTURE_SUPPORT__)
201 #if defined(__MULTITEXTURE_SUPPORT__)
207 static VertexAttrib vertexattribs
[8000];
209 static GLushort indexArray
[30000];
211 static GLuint vertexCount
= 0;
212 static GLuint indexCount
= 0;
213 static GLuint vertexMark
= 0;
214 static int indexbase
= 0;
216 static VertexAttrib
* ptrVertexAttribArray
= NULL
;
217 static VertexAttrib
* ptrVertexAttribArrayMark
= NULL
;
219 static VertexAttrib currentVertexAttrib
;
220 #if defined(__MULTITEXTURE_SUPPORT__)
221 static VertexAttrib currentVertexAttribInit
= {0.0f
,0.0f
,0.0f
,
227 static VertexAttrib currentVertexAttribInit
= {0.0f
,0.0f
,0.0f
,0.0f
,
232 static GLushort
* ptrIndexArray
= NULL
;
234 static GLboolean arraysValid
= GL_FALSE
;
238 ptrVertexAttribArray
= vertexattribs
;
239 ptrVertexAttribArrayMark
= ptrVertexAttribArray
;
240 ptrIndexArray
= indexArray
;
242 memcpy(&nanoglState
, &nanoglInitState
, sizeof(struct nanoState
));
243 memcpy(&tmuState0
,&tmuInitState
,sizeof(struct nanotmuState
));
244 memcpy(&tmuState1
,&tmuInitState
,sizeof(struct nanotmuState
));
245 memcpy(¤tVertexAttrib
,¤tVertexAttribInit
,sizeof(struct VertexAttrib
));
247 activetmuState
= &tmuState0
;
248 wrapperPrimitiveMode
= GL_QUADS
;
249 useTexCoordArray
= GL_FALSE
;
250 activetmu
= GL_TEXTURE0
;
251 clientactivetmu
= GL_TEXTURE0
;
252 delayedttmuchange
= GL_FALSE
;
253 delayedtmutarget
= GL_TEXTURE0
;
258 arraysValid
= GL_FALSE
;
262 void FlushOnStateChange()
264 if (delayedttmuchange
)
266 delayedttmuchange
= GL_FALSE
;
267 glEsImpl
->glActiveTexture(delayedtmutarget
);
275 glEsImpl
->glClientActiveTexture(GL_TEXTURE0
);
276 glEsImpl
->glVertexPointer(3, GL_FLOAT
, sizeof(VertexAttrib
), &vertexattribs
[0].x
);
277 glEsImpl
->glColorPointer(4, GL_UNSIGNED_BYTE
, sizeof(VertexAttrib
), &vertexattribs
[0].red
);
278 glEsImpl
->glTexCoordPointer(2, GL_FLOAT
, sizeof(VertexAttrib
), &vertexattribs
[0].s
);
279 glEsImpl
->glEnableClientState(GL_VERTEX_ARRAY
);
280 glEsImpl
->glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
281 glEsImpl
->glEnableClientState(GL_COLOR_ARRAY
);
282 #if defined(__MULTITEXTURE_SUPPORT__)
283 glEsImpl
->glClientActiveTexture(GL_TEXTURE1
);
284 glEsImpl
->glTexCoordPointer(2, GL_FLOAT
, sizeof(VertexAttrib
), &vertexattribs
[0].s_multi
);
285 glEsImpl
->glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
286 glEsImpl
->glClientActiveTexture(GL_TEXTURE0
);
288 arraysValid
= GL_TRUE
;
291 glEsImpl
->glDrawElements( GL_TRIANGLES
,vertexCount
,GL_UNSIGNED_SHORT
, indexArray
);
294 #if defined(__MULTITEXTURE_SUPPORT__)
295 useMultiTexCoordArray
= GL_FALSE
;
299 ptrVertexAttribArray
= vertexattribs
;
300 ptrVertexAttribArrayMark
= ptrVertexAttribArray
;
301 ptrIndexArray
= indexArray
;
302 useTexCoordArray
= GL_FALSE
;
305 void glBegin(GLenum mode
)
307 wrapperPrimitiveMode
= mode
;
308 vertexMark
= vertexCount
;
309 ptrVertexAttribArrayMark
= ptrVertexAttribArray
;
310 indexbase
= indexCount
;
315 vertexCount
+=((unsigned char*)ptrVertexAttribArray
-(unsigned char*)ptrVertexAttribArrayMark
)/sizeof(VertexAttrib
);
320 switch (wrapperPrimitiveMode
)
324 *ptrIndexArray
++ = indexCount
;
325 *ptrIndexArray
++ = indexCount
+1;
326 *ptrIndexArray
++ = indexCount
+2;
327 *ptrIndexArray
++ = indexCount
;
328 *ptrIndexArray
++ = indexCount
+2;
329 *ptrIndexArray
++ = indexCount
+3;
336 int vcount
= (vertexCount
-vertexMark
)/3;
337 for (int count
= 0; count
< vcount
; count
++)
339 *ptrIndexArray
++ = indexCount
;
340 *ptrIndexArray
++ = indexCount
+1;
341 *ptrIndexArray
++ = indexCount
+2;
346 case GL_TRIANGLE_STRIP
:
348 *ptrIndexArray
++ = indexCount
;
349 *ptrIndexArray
++ = indexCount
+1;
350 *ptrIndexArray
++ = indexCount
+2;
352 int vcount
= ((vertexCount
-vertexMark
)-3);
353 if (vcount
&& ((long)ptrIndexArray
& 0x02))
355 *ptrIndexArray
++ = indexCount
-1; // 2
356 *ptrIndexArray
++ = indexCount
-2; // 1
357 *ptrIndexArray
++ = indexCount
; // 3
362 unsigned int* longptr
= (unsigned int*) ptrIndexArray
;
364 for (int count
= 0; count
< vcount
; count
++)
366 *(longptr
++) = (indexCount
-2) | ((indexCount
-1)<<16);
367 *(longptr
++) = (indexCount
) | ((indexCount
)<<16);
368 *(longptr
++) = (indexCount
-1) | ((indexCount
+1)<<16);
371 ptrIndexArray
= (unsigned short*)(longptr
);
374 *ptrIndexArray
++ = indexCount
-2; // 2
375 *ptrIndexArray
++ = indexCount
-1; // 1
376 *ptrIndexArray
++ = indexCount
; // 3
385 unsigned int* longptr
= (unsigned int*) ptrIndexArray
;
387 for (int count
= 0; count
< vcount
; count
++)
389 *(longptr
++) = (indexCount
-1) | ((indexCount
-2)<<16);
390 *(longptr
++) = (indexCount
) | ((indexCount
-1)<<16);
391 *(longptr
++) = (indexCount
) | ((indexCount
+1)<<16);
395 ptrIndexArray
= (unsigned short*)(longptr
);
399 *ptrIndexArray
++ = indexCount
-1; // 2
400 *ptrIndexArray
++ = indexCount
-2; // 1
401 *ptrIndexArray
++ = indexCount
; // 3
405 vertexCount
+=(vertexCount
-vertexMark
-3)*2;
409 case GL_TRIANGLE_FAN
:
411 *ptrIndexArray
++ = indexCount
++;
412 *ptrIndexArray
++ = indexCount
++;
413 *ptrIndexArray
++ = indexCount
++;
414 int vcount
= ((vertexCount
-vertexMark
)-3);
415 for (int count
= 0; count
< vcount
; count
++)
417 *ptrIndexArray
++ = indexbase
;
418 *ptrIndexArray
++ = indexCount
-1;
419 *ptrIndexArray
++ = indexCount
++;
430 void glEnable (GLenum cap
)
432 GLboolean statechanged
= GL_FALSE
;
437 if (!nanoglState
.alpha_test
)
439 nanoglState
.alpha_test
= GL_TRUE
;
440 statechanged
= GL_TRUE
;
446 if (!nanoglState
.blend
)
448 nanoglState
.blend
= GL_TRUE
;
449 statechanged
= GL_TRUE
;
453 //case GL_CLIP_PLANEi
454 case GL_COLOR_LOGIC_OP
:
456 if (!nanoglState
.color_logic_op
)
458 nanoglState
.color_logic_op
= GL_TRUE
;
459 statechanged
= GL_TRUE
;
463 case GL_COLOR_MATERIAL
:
465 if (!nanoglState
.color_material
)
467 nanoglState
.color_material
= GL_TRUE
;
468 statechanged
= GL_TRUE
;
474 if (!nanoglState
.cull_face
)
476 nanoglState
.cull_face
= GL_TRUE
;
477 statechanged
= GL_TRUE
;
483 if (!nanoglState
.depth_test
)
485 nanoglState
.depth_test
= GL_TRUE
;
486 statechanged
= GL_TRUE
;
492 if (!nanoglState
.dither
)
494 nanoglState
.dither
= GL_TRUE
;
495 statechanged
= GL_TRUE
;
502 if (!nanoglState
.fog
)
504 nanoglState
.fog
= GL_TRUE
;
505 statechanged
= GL_TRUE
;
511 if (!nanoglState
.lighting
)
513 nanoglState
.lighting
= GL_TRUE
;
514 statechanged
= GL_TRUE
;
520 if (!nanoglState
.line_smooth
)
522 nanoglState
.line_smooth
= GL_TRUE
;
523 statechanged
= GL_TRUE
;
527 /* case GL_MATRIX_PALETTE_OES:
529 if (!nanoglState.matrix_palette_oes)
531 nanoglState.matrix_palette_oes = GL_TRUE;
532 statechanged = GL_TRUE;
538 if (!nanoglState
.multisample
)
540 nanoglState
.multisample
= GL_TRUE
;
541 statechanged
= GL_TRUE
;
547 if (!nanoglState
.normalize
)
549 nanoglState
.normalize
= GL_TRUE
;
550 statechanged
= GL_TRUE
;
554 /* case GL_POINT_SPRITE_OES:
556 if (!nanoglState.point_sprite_oes)
558 nanoglState.point_sprite_oes = GL_TRUE;
559 statechanged = GL_TRUE;
563 case GL_POLYGON_OFFSET_FILL
:
565 if (!nanoglState
.polygon_offset_fill
)
567 nanoglState
.polygon_offset_fill
= GL_TRUE
;
568 statechanged
= GL_TRUE
;
572 case GL_RESCALE_NORMAL
:
574 if (!nanoglState
.rescale_normal
)
576 nanoglState
.rescale_normal
= GL_TRUE
;
577 statechanged
= GL_TRUE
;
581 case GL_SAMPLE_ALPHA_TO_COVERAGE
:
583 if (!nanoglState
.sample_alpha_to_coverage
)
585 nanoglState
.sample_alpha_to_coverage
= GL_TRUE
;
586 statechanged
= GL_TRUE
;
590 case GL_SAMPLE_ALPHA_TO_ONE
:
592 if (!nanoglState
.sample_alpha_to_one
)
594 nanoglState
.sample_alpha_to_one
= GL_TRUE
;
595 statechanged
= GL_TRUE
;
599 case GL_SAMPLE_COVERAGE
:
601 if (!nanoglState
.sample_coverage
)
603 nanoglState
.sample_coverage
= GL_TRUE
;
604 statechanged
= GL_TRUE
;
608 case GL_SCISSOR_TEST
:
610 if (!nanoglState
.scissor_test
)
612 nanoglState
.scissor_test
= GL_TRUE
;
613 statechanged
= GL_TRUE
;
617 case GL_STENCIL_TEST
:
620 /* if (!nanoglState.stencil_test)
622 nanoglState.stencil_test = GL_TRUE;
623 statechanged = GL_TRUE;
629 if (!activetmuState
->texture_2d
.value
)
631 FlushOnStateChange();
632 glEsImpl
->glEnable(cap
);
633 activetmuState
->texture_2d
.value
= GL_TRUE
;
644 FlushOnStateChange();
645 glEsImpl
->glEnable(cap
);
649 void glDisable (GLenum cap
)
651 GLboolean statechanged
= GL_FALSE
;
656 if (nanoglState
.alpha_test
)
658 nanoglState
.alpha_test
= GL_FALSE
;
659 statechanged
= GL_TRUE
;
665 if (nanoglState
.blend
)
667 nanoglState
.blend
= GL_FALSE
;
668 statechanged
= GL_TRUE
;
672 //case GL_CLIP_PLANEi
673 case GL_COLOR_LOGIC_OP
:
675 if (nanoglState
.color_logic_op
)
677 nanoglState
.color_logic_op
= GL_FALSE
;
678 statechanged
= GL_TRUE
;
682 case GL_COLOR_MATERIAL
:
684 if (nanoglState
.color_material
)
686 nanoglState
.color_material
= GL_FALSE
;
687 statechanged
= GL_TRUE
;
693 if (nanoglState
.cull_face
)
695 nanoglState
.cull_face
= GL_FALSE
;
696 statechanged
= GL_TRUE
;
702 if (nanoglState
.depth_test
)
704 nanoglState
.depth_test
= GL_FALSE
;
705 statechanged
= GL_TRUE
;
711 if (nanoglState
.dither
)
713 nanoglState
.dither
= GL_FALSE
;
714 statechanged
= GL_TRUE
;
723 nanoglState
.fog
= GL_FALSE
;
724 statechanged
= GL_TRUE
;
730 if (nanoglState
.lighting
)
732 nanoglState
.lighting
= GL_FALSE
;
733 statechanged
= GL_TRUE
;
739 if (nanoglState
.line_smooth
)
741 nanoglState
.line_smooth
= GL_FALSE
;
742 statechanged
= GL_TRUE
;
746 /* case GL_MATRIX_PALETTE_OES:
748 if (nanoglState.matrix_palette_oes)
750 nanoglState.matrix_palette_oes = GL_FALSE;
751 statechanged = GL_TRUE;
757 if (nanoglState
.multisample
)
759 nanoglState
.multisample
= GL_FALSE
;
760 statechanged
= GL_TRUE
;
766 if (nanoglState
.normalize
)
768 nanoglState
.normalize
= GL_FALSE
;
769 statechanged
= GL_TRUE
;
773 /* case GL_POINT_SPRITE_OES:
775 if (nanoglState.point_sprite_oes)
777 nanoglState.point_sprite_oes = GL_FALSE;
778 statechanged = GL_TRUE;
782 case GL_POLYGON_OFFSET_FILL
:
784 if (nanoglState
.polygon_offset_fill
)
786 nanoglState
.polygon_offset_fill
= GL_FALSE
;
787 statechanged
= GL_TRUE
;
791 case GL_RESCALE_NORMAL
:
793 if (nanoglState
.rescale_normal
)
795 nanoglState
.rescale_normal
= GL_FALSE
;
796 statechanged
= GL_TRUE
;
800 case GL_SAMPLE_ALPHA_TO_COVERAGE
:
802 if (nanoglState
.sample_alpha_to_coverage
)
804 nanoglState
.sample_alpha_to_coverage
= GL_FALSE
;
805 statechanged
= GL_TRUE
;
809 case GL_SAMPLE_ALPHA_TO_ONE
:
811 if (nanoglState
.sample_alpha_to_one
)
813 nanoglState
.sample_alpha_to_one
= GL_FALSE
;
814 statechanged
= GL_TRUE
;
818 case GL_SAMPLE_COVERAGE
:
820 if (nanoglState
.sample_coverage
)
822 nanoglState
.sample_coverage
= GL_FALSE
;
823 statechanged
= GL_TRUE
;
827 case GL_SCISSOR_TEST
:
829 if (nanoglState
.scissor_test
)
831 nanoglState
.scissor_test
= GL_FALSE
;
832 statechanged
= GL_TRUE
;
836 case GL_STENCIL_TEST
:
839 /* if (nanoglState.stencil_test)
841 nanoglState.stencil_test = GL_FALSE;
842 statechanged = GL_TRUE;
848 if (activetmuState
->texture_2d
.value
)
850 FlushOnStateChange();
851 glEsImpl
->glDisable(cap
);
852 activetmuState
->texture_2d
.value
= GL_FALSE
;
865 FlushOnStateChange();
866 glEsImpl
->glDisable(cap
);
870 void glVertex2f(GLfloat x
, GLfloat y
)
872 glVertex3f(x
,y
,0.0f
);
876 __FORCEINLINE
unsigned int ClampTo255(float value
)
878 unsigned int retval
= (unsigned int)(value
);
886 void glColor3f( GLfloat red
, GLfloat green
, GLfloat blue
)
888 currentVertexAttrib
.red
= (unsigned char)ClampTo255(red
*255.0f
);
889 currentVertexAttrib
.green
= (unsigned char)ClampTo255(green
*255.0f
);
890 currentVertexAttrib
.blue
= (unsigned char)ClampTo255(blue
*255.0f
);
891 currentVertexAttrib
.alpha
= 255;
895 void glTexCoord2fv( const GLfloat
*v
)
897 memcpy(¤tVertexAttrib
.s
, v
, 2*sizeof(float));
901 void glTexCoord2f(GLfloat s
, GLfloat t
)
903 currentVertexAttrib
.s
= s
;
904 currentVertexAttrib
.t
= t
;
907 void glViewport (GLint x
, GLint y
, GLsizei width
, GLsizei height
)
909 FlushOnStateChange();
910 glEsImpl
->glViewport(x
,y
,width
,height
);
913 void glLoadIdentity (void)
915 FlushOnStateChange();
916 glEsImpl
->glLoadIdentity();
919 void glColor4f (GLfloat red
, GLfloat green
, GLfloat blue
, GLfloat alpha
)
921 currentVertexAttrib
.red
= (unsigned char)ClampTo255(red
*255.0f
);
922 currentVertexAttrib
.green
= (unsigned char)ClampTo255(green
*255.0f
);
923 currentVertexAttrib
.blue
= (unsigned char)ClampTo255(blue
*255.0f
);
924 currentVertexAttrib
.alpha
= (unsigned char)ClampTo255(alpha
*255.0f
);
927 void glOrtho (GLfloat left
, GLfloat right
, GLfloat bottom
, GLfloat top
, GLfloat zNear
, GLfloat zFar
)
929 FlushOnStateChange();
930 glEsImpl
->glOrthof(left
,right
,bottom
,top
, zNear
,zFar
);
933 void glMatrixMode (GLenum mode
)
935 if (nanoglState
.matrixmode
== mode
)
939 nanoglState
.matrixmode
= mode
;
940 FlushOnStateChange();
941 glEsImpl
->glMatrixMode(mode
);
944 void glTexParameterf (GLenum target
, GLenum pname
, GLfloat param
)
946 if (pname
== GL_TEXTURE_BORDER_COLOR
)
948 return; // not supported by opengl es
950 if ( (pname
== GL_TEXTURE_WRAP_S
||
951 pname
== GL_TEXTURE_WRAP_T
) &&
957 FlushOnStateChange();
958 glEsImpl
->glTexParameterf(target
, pname
,param
);
961 void glTexParameterfv( GLenum target
, GLenum pname
, const GLfloat
*params
)
963 glTexParameterf(target
, pname
, params
[0]);
966 void glTexImage2D (GLenum target
, GLint level
, GLint internalformat
, GLsizei width
, GLsizei height
, GLint border
, GLenum format
, GLenum type
, const GLvoid
*pixels
)
968 FlushOnStateChange();
969 internalformat
= format
;
970 glEsImpl
->glTexImage2D(target
, level
, internalformat
, width
, height
,border
,format
,type
,pixels
);
973 void glDrawBuffer(GLenum
/*mode*/)
977 void glTranslatef (GLfloat x
, GLfloat y
, GLfloat z
)
979 FlushOnStateChange();
980 glEsImpl
->glTranslatef(x
,y
,z
);
983 void glRotatef (GLfloat angle
, GLfloat x
, GLfloat y
, GLfloat z
)
985 FlushOnStateChange();
986 glEsImpl
->glRotatef(angle
, x
, y
, z
);
989 void glScalef (GLfloat x
, GLfloat y
, GLfloat z
)
991 FlushOnStateChange();
992 glEsImpl
->glScalef(x
,y
,z
);
995 void glDepthRange(GLclampf zNear
, GLclampf zFar
)
997 if ((nanoglState
.depth_range_near
== zNear
) &&(nanoglState
.depth_range_far
== zFar
))
1003 nanoglState
.depth_range_near
= zNear
;
1004 nanoglState
.depth_range_far
= zFar
;
1006 FlushOnStateChange();
1007 glEsImpl
->glDepthRangef(zNear
, zFar
);
1010 void glDepthFunc (GLenum func
)
1012 if (nanoglState
.depth_func
== func
)
1018 nanoglState
.depth_func
= func
;
1020 FlushOnStateChange();
1021 glEsImpl
->glDepthFunc(func
);
1024 void glFinish (void)
1026 FlushOnStateChange();
1027 glEsImpl
->glFinish();
1030 void glGetFloatv (GLenum pname
, GLfloat
*params
)
1032 FlushOnStateChange();
1033 glEsImpl
->glGetFloatv(pname
, params
);
1036 void glCullFace (GLenum mode
)
1038 if (nanoglState
.cullface
== mode
)
1044 nanoglState
.cullface
= mode
;
1046 FlushOnStateChange();
1047 glEsImpl
->glCullFace(mode
);
1050 void glFrustum (GLfloat left
, GLfloat right
, GLfloat bottom
, GLfloat top
, GLfloat zNear
, GLfloat zFar
)
1052 FlushOnStateChange();
1053 glEsImpl
->glFrustumf(left
,right
,bottom
,top
,zNear
,zFar
);
1056 void glClear (GLbitfield mask
)
1058 FlushOnStateChange();
1059 glEsImpl
->glClear(mask
);
1063 void glVertex3f( GLfloat x
, GLfloat y
, GLfloat z
)
1065 GLfloat
* vert
= (GLfloat
*)ptrVertexAttribArray
++;
1069 #if defined(__MULTITEXTURE_SUPPORT__)
1070 memcpy(vert
, ¤tVertexAttrib
.red
, 5*sizeof(GLfloat
));
1072 memcpy(vert
+1, ¤tVertexAttrib
.red
, 3*sizeof(GLfloat
));
1077 void glColor4fv( const GLfloat
*v
)
1079 currentVertexAttrib
.red
= (unsigned char)ClampTo255(v
[0]*255.0f
);
1080 currentVertexAttrib
.green
= (unsigned char)ClampTo255(v
[1]*255.0f
);
1081 currentVertexAttrib
.blue
= (unsigned char)ClampTo255(v
[2]*255.0f
);
1082 currentVertexAttrib
.alpha
= (unsigned char)ClampTo255(v
[3]*255.0f
);
1085 void glColor3ubv( const GLubyte
* v
)
1087 currentVertexAttrib
.red
= v
[0];
1088 currentVertexAttrib
.green
= v
[1];
1089 currentVertexAttrib
.blue
= v
[2];
1090 currentVertexAttrib
.alpha
= 255;
1093 void glColor4ubv( const GLubyte
*v
)
1095 //*((unsigned int*)(¤tVertexAttrib.red)) = *((unsigned int*)(v));
1096 currentVertexAttrib
.red
= v
[0];
1097 currentVertexAttrib
.green
= v
[1];
1098 currentVertexAttrib
.blue
= v
[2];
1099 currentVertexAttrib
.alpha
= v
[3];
1102 void glColor3fv( const GLfloat
*v
)
1104 currentVertexAttrib
.red
= (unsigned char)ClampTo255(v
[0]*255.0f
);
1105 currentVertexAttrib
.green
= (unsigned char)ClampTo255(v
[1]*255.0f
);
1106 currentVertexAttrib
.blue
= (unsigned char)ClampTo255(v
[2]*255.0f
);
1107 currentVertexAttrib
.alpha
= 255;
1110 //-- nicknekit: xash3d funcs --
1112 void glColor4ub( GLubyte red
, GLubyte green
, GLubyte blue
, GLubyte alpha
)
1114 currentVertexAttrib
.red
= red
;
1115 currentVertexAttrib
.green
= green
;
1116 currentVertexAttrib
.blue
= blue
;
1117 currentVertexAttrib
.alpha
= alpha
;
1120 void glColor3ub( GLubyte red
, GLubyte green
, GLubyte blue
)
1122 currentVertexAttrib
.red
= red
;
1123 currentVertexAttrib
.green
= green
;
1124 currentVertexAttrib
.blue
= blue
;
1125 currentVertexAttrib
.alpha
= 255;
1128 void glNormal3fv( const GLfloat
*v
)
1130 FlushOnStateChange();
1131 glEsImpl
->glNormal3f(v
[0],v
[1],v
[2]);
1134 void glCopyTexImage2D( GLenum target
, GLint level
, GLenum internalformat
, GLint x
, GLint y
, GLsizei width
, GLsizei height
, GLint border
)
1136 FlushOnStateChange();
1137 glEsImpl
->glCopyTexImage2D(target
, level
, internalformat
, x
, y
, width
, height
, border
);
1140 void glTexImage1D(GLenum target
, GLint level
, GLint internalformat
, GLsizei width
, GLint border
, GLenum format
, GLenum type
, const GLvoid
*pixels
)
1142 glTexImage2D(GL_TEXTURE_2D
, level
, internalformat
, width
, 1, border
, format
, type
, pixels
);
1145 void glTexImage3D(GLenum target
, GLint level
, GLint internalformat
, GLsizei width
, GLsizei height
, GLsizei depth
, GLint border
, GLenum format
, GLenum type
, const GLvoid
*pixels
)
1147 glTexImage2D(GL_TEXTURE_2D
, level
, internalformat
, width
, height
, border
, format
, type
, pixels
);
1150 void glTexSubImage1D( GLenum target
, GLint level
, GLint xoffset
, GLsizei width
, GLenum format
, GLenum type
, const GLvoid
*pixels
)
1152 glTexSubImage2D(target
,level
,xoffset
,0,width
,1,format
,type
,pixels
);
1155 void glTexSubImage3D( GLenum target
, GLint level
,
1156 GLint xoffset
, GLint yoffset
,
1157 GLint zoffset
, GLsizei width
,
1158 GLsizei height
, GLsizei depth
,
1160 GLenum type
, const GLvoid
*pixels
)
1162 glTexSubImage2D(target
,level
,xoffset
,yoffset
,width
,height
,format
,type
,pixels
);
1167 GLboolean
glIsTexture(GLuint texture
)
1169 FlushOnStateChange();
1170 return glEsImpl
->glIsTexture(texture
);
1174 void glTexGeni( GLenum coord
, GLenum pname
, GLint param
)
1176 //for mirrors? not needed for original hl?
1179 void glTexGenfv( GLenum coord
, GLenum pname
, const GLfloat
*params
)
1181 //for mirrors? not needed for original hl?
1186 void glHint (GLenum target
, GLenum mode
)
1188 FlushOnStateChange();
1189 glEsImpl
->glHint(target
, mode
);
1192 void glBlendFunc (GLenum sfactor
, GLenum dfactor
)
1194 if ((nanoglState
.sfactor
== sfactor
) && (nanoglState
.dfactor
== dfactor
))
1198 nanoglState
.sfactor
= sfactor
;
1199 nanoglState
.dfactor
= dfactor
;
1200 FlushOnStateChange();
1201 glEsImpl
->glBlendFunc(sfactor
, dfactor
);
1204 void glPopMatrix (void)
1206 FlushOnStateChange();
1207 glEsImpl
->glPopMatrix();
1210 void glShadeModel (GLenum mode
)
1212 if (nanoglState
.shademodel
== mode
)
1216 nanoglState
.shademodel
= mode
;
1217 FlushOnStateChange();
1218 glEsImpl
->glShadeModel(mode
);
1221 void glPushMatrix (void)
1223 FlushOnStateChange();
1224 glEsImpl
->glPushMatrix();
1227 void glTexEnvf (GLenum target
, GLenum pname
, GLfloat param
)
1229 if (target
== GL_TEXTURE_ENV
)
1231 if (pname
== GL_TEXTURE_ENV_MODE
)
1233 if (param
== activetmuState
->texture_env_mode
.value
)
1239 FlushOnStateChange();
1240 glEsImpl
->glTexEnvf(target
, pname
, param
);
1241 activetmuState
->texture_env_mode
.value
= param
;
1246 FlushOnStateChange();
1247 glEsImpl
->glTexEnvf(target
, pname
, param
);
1250 void glVertex3fv( const GLfloat
*v
)
1252 GLfloat
* vert
= (GLfloat
*)ptrVertexAttribArray
++;
1253 memcpy(vert
, v
, 3*sizeof(GLfloat
));
1254 #if defined(__MULTITEXTURE_SUPPORT__)
1255 memcpy(vert
+3, ¤tVertexAttrib
.red
, 5*sizeof(GLfloat
));
1257 memcpy(vert
+4, ¤tVertexAttrib
.red
, 3*sizeof(GLfloat
));
1262 void glDepthMask (GLboolean flag
)
1264 if (nanoglState
.depthmask
== flag
)
1268 nanoglState
.depthmask
= flag
;
1269 FlushOnStateChange();
1270 glEsImpl
->glDepthMask(flag
);
1273 void glBindTexture (GLenum target
, GLuint texture
)
1275 if (activetmuState
->boundtexture
.value
== texture
)
1279 FlushOnStateChange();
1280 activetmuState
->boundtexture
.value
= texture
;
1281 glEsImpl
->glBindTexture(target
, texture
);
1285 void glGetIntegerv (GLenum pname
, GLint
*params
)
1287 FlushOnStateChange();
1288 glEsImpl
->glGetIntegerv(pname
, params
);
1291 GLubyte nano_extensions_string
[4096];
1292 const GLubyte
* glGetString (GLenum name
)
1295 if (name
== GL_EXTENSIONS
)
1297 #if defined(__MULTITEXTURE_SUPPORT__)
1298 sprintf((char*)nano_extensions_string
,"%s %s",glEsImpl
->glGetString(name
),"GL_ARB_multitexture EXT_texture_env_add");
1300 sprintf((char*)nano_extensions_string
,"%s %s",glEsImpl
->glGetString(name
),"EXT_texture_env_add");
1302 return nano_extensions_string
;
1304 return glEsImpl
->glGetString(name
);
1307 void glAlphaFunc (GLenum func
, GLclampf ref
)
1309 FlushOnStateChange();
1310 glEsImpl
->glAlphaFunc(func
,ref
);
1315 FlushOnStateChange();
1316 glEsImpl
->glFlush();
1319 void glReadPixels (GLint x
, GLint y
, GLsizei width
, GLsizei height
, GLenum format
, GLenum type
, GLvoid
*pixels
)
1321 if (format
== GL_DEPTH_COMPONENT
)
1323 // OpenglEs 1.1 does not support reading depth buffer without an extension
1324 memset(pixels
, 0xff,4);
1327 FlushOnStateChange();
1328 glEsImpl
->glReadPixels(x
,y
,width
,height
,format
,type
,pixels
);
1331 void glReadBuffer( GLenum
/*mode*/ )
1336 void glLoadMatrixf (const GLfloat
*m
)
1338 FlushOnStateChange();
1339 glEsImpl
->glLoadMatrixf(m
);
1342 void glTexSubImage2D (GLenum target
, GLint level
, GLint xoffset
, GLint yoffset
, GLsizei width
, GLsizei height
, GLenum format
, GLenum type
, const GLvoid
*pixels
)
1344 FlushOnStateChange();
1345 glEsImpl
->glTexSubImage2D(target
,level
,xoffset
,yoffset
,width
,height
,format
,type
,pixels
);
1348 void glClearColor (GLclampf red
, GLclampf green
, GLclampf blue
, GLclampf alpha
)
1350 FlushOnStateChange();
1351 glEsImpl
->glClearColor(red
,green
,blue
,alpha
);
1354 GLenum
glGetError (void)
1356 FlushOnStateChange();
1357 return GL_NO_ERROR
;//glEsImpl->glGetError();
1360 void glActiveTexture (GLenum texture
)
1362 if (activetmu
== texture
)
1366 if (delayedttmuchange
)
1368 delayedttmuchange
= GL_FALSE
;
1372 delayedttmuchange
= GL_TRUE
;
1373 delayedtmutarget
= texture
;
1375 if (texture
== GL_TEXTURE0
)
1377 activetmuState
= &tmuState0
;
1381 activetmuState
= &tmuState1
;
1383 activetmu
= texture
;
1386 void glClientActiveTexture (GLenum texture
)
1388 clientactivetmu
= texture
;
1391 void glPolygonMode( GLenum face
, GLenum mode
)
1395 void glDeleteTextures( GLsizei n
, const GLuint
*textures
)
1397 FlushOnStateChange();
1398 glEsImpl
->glDeleteTextures(n
,textures
);
1401 void glClearDepth( GLclampf depth
)
1403 FlushOnStateChange();
1404 glEsImpl
->glClearDepthf( depth
);
1407 void glClipPlane( GLenum plane
, const GLdouble
*equation
)
1409 FlushOnStateChange();
1411 array
[0] = (GLfloat
)(equation
[0]);
1412 array
[1] = (GLfloat
)(equation
[1]);
1413 array
[2] = (GLfloat
)(equation
[2]);
1414 array
[3] = (GLfloat
)(equation
[3]);
1415 glEsImpl
->glClipPlanef( plane
, array
);
1418 void glScissor( GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1420 FlushOnStateChange();
1421 glEsImpl
->glScissor( x
, y
, width
,height
);
1424 void glPointSize( GLfloat size
)
1426 FlushOnStateChange();
1427 glEsImpl
->glPointSize( size
);
1430 void glArrayElement(GLint i
) {}
1431 void glLineWidth(GLfloat width
) {}
1432 void glCallList( GLuint list
) {}
1433 void glColorMask( GLboolean red
, GLboolean green
, GLboolean blue
, GLboolean alpha
) {}
1434 void glStencilFunc( GLenum func
, GLint ref
, GLuint mask
) {}
1435 void glStencilOp( GLenum fail
, GLenum zfail
, GLenum zpass
) {}
1437 struct ptrstate vertex_array
;
1438 struct ptrstate color_array
;
1439 struct ptrstate texture_coord_array
;
1441 void glDrawElements( GLenum mode
, GLsizei count
, GLenum type
, const GLvoid
*indices
)
1443 // ensure that all primitives specified between glBegin/glEnd pairs
1444 // are rendered first, and that we have correct tmu in use..
1445 FlushOnStateChange();
1446 // setup correct vertex/color/texcoord pointers
1448 tmuState0
.vertex_array
.changed
||
1449 tmuState0
.color_array
.changed
||
1450 tmuState0
.texture_coord_array
.changed
)
1452 glEsImpl
->glClientActiveTexture(GL_TEXTURE0
);
1454 if (arraysValid
|| tmuState0
.vertex_array
.changed
)
1456 if (tmuState0
.vertex_array
.enabled
)
1458 glEsImpl
->glEnableClientState(GL_VERTEX_ARRAY
);
1462 glEsImpl
->glDisableClientState(GL_VERTEX_ARRAY
);
1464 glEsImpl
->glVertexPointer(tmuState0
.vertex_array
.size
,
1465 tmuState0
.vertex_array
.type
,
1466 tmuState0
.vertex_array
.stride
,
1467 tmuState0
.vertex_array
.ptr
);
1468 tmuState0
.vertex_array
.changed
= GL_FALSE
;
1470 if (arraysValid
|| tmuState0
.color_array
.changed
)
1472 if (tmuState0
.color_array
.enabled
)
1474 glEsImpl
->glEnableClientState(GL_COLOR_ARRAY
);
1478 glEsImpl
->glDisableClientState(GL_COLOR_ARRAY
);
1480 glEsImpl
->glColorPointer(tmuState0
.color_array
.size
,
1481 tmuState0
.color_array
.type
,
1482 tmuState0
.color_array
.stride
,
1483 tmuState0
.color_array
.ptr
);
1484 tmuState0
.color_array
.changed
= GL_FALSE
;
1486 if (arraysValid
|| tmuState0
.texture_coord_array
.changed
)
1488 tmuState0
.texture_coord_array
.changed
= GL_FALSE
;
1489 if (tmuState0
.texture_coord_array
.enabled
)
1491 glEsImpl
->glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
1495 glEsImpl
->glDisableClientState(GL_TEXTURE_COORD_ARRAY
);
1497 glEsImpl
->glTexCoordPointer(tmuState0
.texture_coord_array
.size
,
1498 tmuState0
.texture_coord_array
.type
,
1499 tmuState0
.texture_coord_array
.stride
,
1500 tmuState0
.texture_coord_array
.ptr
);
1503 if (arraysValid
|| tmuState1
.texture_coord_array
.changed
)
1505 tmuState1
.texture_coord_array
.changed
= GL_FALSE
;
1506 glEsImpl
->glClientActiveTexture(GL_TEXTURE1
);
1507 if (tmuState1
.texture_coord_array
.enabled
)
1509 glEsImpl
->glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
1513 glEsImpl
->glDisableClientState(GL_TEXTURE_COORD_ARRAY
);
1515 glEsImpl
->glTexCoordPointer(tmuState1
.texture_coord_array
.size
,
1516 tmuState1
.texture_coord_array
.type
,
1517 tmuState1
.texture_coord_array
.stride
,
1518 tmuState1
.texture_coord_array
.ptr
);
1521 arraysValid
= GL_FALSE
;
1522 glEsImpl
->glDrawElements(mode
, count
, type
, indices
);
1525 void glEnableClientState(GLenum array
)
1527 struct nanotmuState
* clientstate
= NULL
;
1528 if (clientactivetmu
== GL_TEXTURE0
)
1530 clientstate
= &tmuState0
;
1532 else if (clientactivetmu
== GL_TEXTURE1
)
1534 clientstate
= &tmuState1
;
1542 case GL_VERTEX_ARRAY
:
1543 if (clientstate
->vertex_array
.enabled
)
1547 clientstate
->vertex_array
.enabled
= GL_TRUE
;
1548 clientstate
->vertex_array
.changed
= GL_TRUE
;
1550 case GL_COLOR_ARRAY
:
1551 if (clientstate
->color_array
.enabled
)
1555 clientstate
->color_array
.enabled
= GL_TRUE
;
1556 clientstate
->color_array
.changed
= GL_TRUE
;
1559 case GL_TEXTURE_COORD_ARRAY
:
1560 if (clientstate
->texture_coord_array
.enabled
)
1564 clientstate
->texture_coord_array
.enabled
= GL_TRUE
;
1565 clientstate
->texture_coord_array
.changed
= GL_TRUE
;
1571 void glDisableClientState(GLenum array
)
1573 struct nanotmuState
* clientstate
= NULL
;
1574 if (clientactivetmu
== GL_TEXTURE0
)
1576 clientstate
= &tmuState0
;
1578 else if (clientactivetmu
== GL_TEXTURE1
)
1580 clientstate
= &tmuState1
;
1588 case GL_VERTEX_ARRAY
:
1589 if (!clientstate
->vertex_array
.enabled
)
1593 clientstate
->vertex_array
.enabled
= GL_FALSE
;
1594 clientstate
->vertex_array
.changed
= GL_TRUE
;
1596 case GL_COLOR_ARRAY
:
1597 if (!clientstate
->color_array
.enabled
)
1601 clientstate
->color_array
.enabled
= GL_FALSE
;
1602 clientstate
->color_array
.changed
= GL_TRUE
;
1605 case GL_TEXTURE_COORD_ARRAY
:
1606 if (!clientstate
->texture_coord_array
.enabled
)
1610 clientstate
->texture_coord_array
.enabled
= GL_FALSE
;
1611 clientstate
->texture_coord_array
.changed
= GL_TRUE
;
1617 void glVertexPointer( GLint size
, GLenum type
,GLsizei stride
, const GLvoid
*pointer
)
1619 if (tmuState0
.vertex_array
.size
== size
&&
1620 tmuState0
.vertex_array
.stride
== stride
&&
1621 tmuState0
.vertex_array
.type
== type
&&
1622 tmuState0
.vertex_array
.ptr
== pointer
)
1626 tmuState0
.vertex_array
.size
= size
;
1627 tmuState0
.vertex_array
.stride
= stride
;
1628 tmuState0
.vertex_array
.type
= type
;
1629 tmuState0
.vertex_array
.ptr
= (GLvoid
*)pointer
;
1630 tmuState0
.vertex_array
.changed
= GL_TRUE
;
1632 void glTexCoordPointer( GLint size
, GLenum type
, GLsizei stride
, const GLvoid
*pointer
)
1634 struct nanotmuState
* clientstate
= NULL
;
1635 if (clientactivetmu
== GL_TEXTURE0
)
1637 clientstate
= &tmuState0
;
1639 else if (clientactivetmu
== GL_TEXTURE1
)
1641 clientstate
= &tmuState1
;
1643 if (clientstate
->texture_coord_array
.size
== size
&&
1644 clientstate
->texture_coord_array
.stride
== stride
&&
1645 clientstate
->texture_coord_array
.type
== type
&&
1646 clientstate
->texture_coord_array
.ptr
== pointer
)
1650 clientstate
->texture_coord_array
.size
= size
;
1651 clientstate
->texture_coord_array
.stride
= stride
;
1652 clientstate
->texture_coord_array
.type
= type
;
1653 clientstate
->texture_coord_array
.ptr
= (GLvoid
*)pointer
;
1654 clientstate
->texture_coord_array
.changed
= GL_TRUE
;
1656 void glColorPointer( GLint size
, GLenum type
, GLsizei stride
, const GLvoid
*pointer
)
1658 if (tmuState0
.color_array
.size
== size
&&
1659 tmuState0
.color_array
.stride
== stride
&&
1660 tmuState0
.color_array
.type
== type
&&
1661 tmuState0
.color_array
.ptr
== pointer
)
1665 tmuState0
.color_array
.size
= size
;
1666 tmuState0
.color_array
.stride
= stride
;
1667 tmuState0
.color_array
.type
= type
;
1668 tmuState0
.color_array
.ptr
= (GLvoid
*)pointer
;
1669 tmuState0
.color_array
.changed
= GL_TRUE
;
1671 void glPolygonOffset( GLfloat factor
, GLfloat units
)
1673 FlushOnStateChange();
1674 glEsImpl
->glPolygonOffset(factor
, units
);
1676 void glStencilMask( GLuint mask
) {}
1677 void glClearStencil( GLint s
) {}
1679 #if defined(__MULTITEXTURE_SUPPORT__)
1681 extern "C" void glMultiTexCoord2fARB( GLenum target
, GLfloat s
, GLfloat t
);
1683 void glMultiTexCoord2fARB( GLenum target
, GLfloat s
, GLfloat t
)
1685 if (target
== GL_TEXTURE0
)
1691 currentVertexAttrib
.s_multi
= s
;
1692 currentVertexAttrib
.t_multi
= t
;
1698 void glDrawArrays( GLenum mode
, int first
, int count
)
1700 FlushOnStateChange();
1701 glEsImpl
->glDrawArrays(mode
, first
, count
);
1703 void glMultMatrixf (const GLfloat
*m
)
1705 FlushOnStateChange();
1706 glEsImpl
->glMultMatrixf(m
);
1709 void glPixelStorei (GLenum pname
, GLint param
)
1711 FlushOnStateChange();
1712 glEsImpl
->glPixelStorei(pname
, param
);
1716 void glFogf (GLenum pname
, GLfloat param
)
1718 FlushOnStateChange();
1719 glEsImpl
->glFogf(pname
, param
);
1722 void glFogfv (GLenum pname
, const GLfloat
*params
)
1724 FlushOnStateChange();
1725 glEsImpl
->glFogfv(pname
, params
);
1728 void glGetTexParameteriv (GLenum target
, GLenum pname
, GLint
*params
)
1730 FlushOnStateChange();
1731 glEsImpl
->glGetTexParameteriv(target
, pname
, params
);
1735 // This gives: called unimplemented OpenGL ES API (Android)
1736 void glTexParameteri (GLenum target
, GLenum pname
, GLint param
)
1738 if (pname
== GL_TEXTURE_BORDER_COLOR
) {
1739 return; // not supported by opengl es
1741 if ( (pname
== GL_TEXTURE_WRAP_S
||
1742 pname
== GL_TEXTURE_WRAP_T
) &&
1743 param
== GL_CLAMP
) {
1747 FlushOnStateChange();
1748 glEsImpl
->glTexParameteri(target
, pname
, param
);
1751 void glTexParameterx (GLenum target
, GLenum pname
, GLfixed param
)
1753 if (pname
== GL_TEXTURE_BORDER_COLOR
) {
1754 return; // not supported by opengl es
1756 if ( (pname
== GL_TEXTURE_WRAP_S
||
1757 pname
== GL_TEXTURE_WRAP_T
) &&
1758 param
== GL_CLAMP
) {
1761 FlushOnStateChange();
1762 glEsImpl
->glTexParameterx(target
, pname
, param
);
1765 void glGenTextures (GLsizei n
, GLuint
*textures
)
1767 FlushOnStateChange();
1768 glEsImpl
->glGenTextures(n
, textures
);
1771 void glFrontFace (GLenum mode
)
1773 FlushOnStateChange();
1774 glEsImpl
->glFrontFace(mode
);