1 import "OpenGLDisplayDriver"
15 #if defined(__EMSCRIPTEN__)
17 #include <GLES2/gl2.h>
20 #if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__) && !defined(__ODROID__)
22 //# include "gl_core_3_3.h"
23 # include "gl_compat_4_4.h" // FIXME: no glPushAttrib() in core profile
25 # include "gl_compat_4_4.h"
31 #undef glEnableClientState
32 #undef glDisableClientState
33 #undef GL_VERTEX_ARRAY
34 #undef GL_NORMAL_ARRAY
35 #undef GL_TEXTURE_COORD_ARRAY
37 #undef glVertexPointer
38 #undef glTexCoordPointer
41 #define glEnableClientState glEnableVertexAttribArray
42 #define glDisableClientState glDisableVertexAttribArray
43 #define GL_VERTEX_ARRAY GLBufferContents::vertex
44 #define GL_NORMAL_ARRAY GLBufferContents::normal
45 #define GL_TEXTURE_COORD_ARRAY GLBufferContents::texCoord
46 #define GL_COLOR_ARRAY GLBufferContents::color
47 #define glVertexPointer(n, t, s, p) glVertexAttribPointer(GLBufferContents::vertex, n, t, GL_FALSE, s, p)
48 #define glTexCoordPointer(n, t, s, p) glVertexAttribPointer(GLBufferContents::texCoord, n, t, GL_FALSE, s, p)
49 #define glColorPointer(n, t, s, p) glVertexAttribPointer(GLBufferContents::color, n, t, GL_FALSE, s, p)
53 #if defined(ES1_1) || defined(ES2) || defined(SHADERS)
90 #undef glColorMaterial
93 #define glRecti glimtkRecti
94 #define glBegin glimtkBegin
95 #define glTexCoord2i glimtkTexCoord2i
96 #define glVertex2i glimtkVertex2i
97 #define glTexCoord2d glimtkTexCoord2d
98 #define glVertex2d glimtkVertex2d
99 #define glTexCoord2f glimtkTexCoord2f
100 #define glVertex2f glimtkVertex2f
101 #define glEnd glimtkEnd
102 #define glColor3f glimtkColor3f
103 #define glColor4ub glimtkColor4ub
104 #define glColor4fv glimtkColor4fv
105 #define glNormal3fv glimtkNormal3fv
106 #define glNormal3f glimtkNormal3f
107 #define glTexCoord2fv glimtkTexCoord2fv
108 #define glVertex3d glimtkVertex3d
109 #define glVertex3dv glimtkVertex3dv
110 #define glVertex3f glimtkVertex3f
111 #define glVertex3fv glimtkVertex3fv
113 #define glLoadMatrixd glmsLoadMatrixd
114 #define glLoadMatrixf glmsLoadMatrixf
115 #define glMultMatrixd glmsMultMatrixd
116 #define glFrustum glmsFrustum
117 #define glOrtho glmsOrtho
118 #define glScaled glmsScaled
119 #define glScalef glmsScaled
120 #define glTranslated glmsTranslated
121 #define glRotated glmsRotated
122 #define glMatrixMode glmsMatrixMode
123 #define glLoadIdentity glmsLoadIdentity
124 #define glPushMatrix glmsPushMatrix
125 #define glPopMatrix glmsPopMatrix
127 #define glLineStipple glesLineStipple
128 #define glColorMaterial glesColorMaterial
129 #define glLightModeli glesLightModeli
135 #define OFFSET(s, m) ((uint)(uintptr) (&((s *) 0)->m))
137 import "textureManager"
140 #define DM_ENABLE_IMAGE_ROTATION (1)
141 #define DM_ENABLE_EXT_COLOR (1)
145 static struct DMDrawVertexFlat
150 #if DM_ENABLE_EXT_COLOR
153 } __attribute__((aligned(16)));
155 static struct DMDrawVertex
160 #if DM_ENABLE_EXT_COLOR
163 } __attribute__((aligned(16)));
174 class DMProgramFlags : uint { bool valid:1; }
180 GLuint fragmentShader;
186 #if DM_ENABLE_EXT_COLOR
190 DMProgramFlags flags;
191 int64 lastUpdateCount;
194 class DMImageFlags : uint16
196 bool empty:1; // Image is empty, do not draw
197 bool blending:1; // Must draw image with blending
200 public struct DMImage
209 // Computed order for sorted rendering, in defineImage()
215 this = { flags = { empty = true } };
218 void defineImage( Texture texture, int offsetx, int offsety, int sizex, int sizey, bool blending, int programIndex, int layerindex )
220 int ordx = offsetx >> 6;
221 int ordy = offsety >> 6;
222 uint32 orderimage = ccMortonNumber32( ordx, ordy ) & ( ( 1 << DM_IMAGE_ORDER_BITS ) - 1 );
226 srcx = (short)offsetx;
227 srcy = (short)offsety;
228 sizex = (short)sizex;
229 sizey = (short)sizey;
230 programIndex = (short)programIndex;
231 flags = { blending = blending };
232 orderMask = (orderimage << DM_IMAGE_ORDER_SHIFT) |
233 (( blending == true ) << DM_BLEND_ORDER_SHIFT) |
234 (programIndex << DM_PROGRAM_ORDER_SHIFT) |
236 (layerindex << DM_LAYER_ORDER_SHIFT);
248 #if DM_ENABLE_IMAGE_ROTATION
252 #if DM_ENABLE_EXT_COLOR
263 define DM_IMAGE_ORDER_BITS = 8;
264 define DM_BLEND_ORDER_BITS = 1;
265 define DM_PROGRAM_ORDER_BITS = 4;
266 define DM_TEXTURE_ORDER_BITS = 10;
267 define DM_LAYER_ORDER_BITS = 4;
268 define DM_BARRIER_ORDER_BITS = 5;
270 define DM_IMAGE_ORDER_SHIFT = 0;
271 define DM_BLEND_ORDER_SHIFT = DM_IMAGE_ORDER_BITS;
272 define DM_PROGRAM_ORDER_SHIFT = DM_IMAGE_ORDER_BITS+DM_BLEND_ORDER_BITS;
273 define DM_TEXTURE_ORDER_SHIFT = DM_IMAGE_ORDER_BITS+DM_BLEND_ORDER_BITS+DM_PROGRAM_ORDER_BITS;
274 define DM_LAYER_ORDER_SHIFT = DM_IMAGE_ORDER_BITS+DM_BLEND_ORDER_BITS+DM_PROGRAM_ORDER_BITS+DM_TEXTURE_ORDER_BITS;
275 define DM_BARRIER_ORDER_SHIFT = DM_IMAGE_ORDER_BITS+DM_BLEND_ORDER_BITS+DM_PROGRAM_ORDER_BITS+DM_TEXTURE_ORDER_BITS+DM_LAYER_ORDER_BITS;
277 define DM_LAYER_COUNT = 1<<DM_LAYER_ORDER_BITS;
278 define DM_PROGRAM_COUNT = 1<<DM_PROGRAM_ORDER_BITS;
280 define DM_CONTEXT_DRAW_BUFFER_COUNT = 64;
281 define DM_CONTEXT_DRAW_BUFFER_VERTEX_ALLOC = 1024;
284 /* Range is from zero to DM_LAYER_COUNT */
305 define DM_LAYER_BOTTOM = DMLayer::DM_LAYER_0_BOTTOM;
306 define DM_LAYER_BELOW = DMLayer::DM_LAYER_3_BELOW;
307 define DM_LAYER_NORMAL = DMLayer::DM_LAYER_6_NORMAL;
308 define DM_LAYER_ABOVE = DMLayer::DM_LAYER_9_ABOVE;
309 define DM_LAYER_TOP = DMLayer::DM_LAYER_15_TOP;
311 define DM_PROGRAM_NORMAL = 0;
312 define DM_PROGRAM_ALPHABLEND = 1;
313 define DM_PROGRAM_ALPHABLEND_INTENSITY = 2;
314 define DM_PROGRAM_ALPHABLEND_INTENSITY_EXTCOLOR = 3;
317 static inline void OpenGLErrorCheck( const char *file, int line )
319 int error = glGetError();
320 if( error != GL_NO_ERROR )
321 printf( "ERROR %d at %s:%d\n", error, file, line );
324 #define ERRORCHECK() OpenGLErrorCheck(__FILE__,__LINE__)
329 // Not using 32767.0 ; overflow when converting to int16 due to floating point crud causes problems
330 #define DM_IMAGE_ROTATION_NORMFACTOR (24576.0f)
332 #define DM_VERTEX_NORMSHIFT (2)
333 #define DM_VERTEX_NORMFACTOR (4.0f)
335 #define DM_TEXCOORD_NORMSHIFT (13)
336 #define DM_TEXCOORD_NORMFACTOR (8192.0f)
338 static GLuint dmCreateShader( GLenum type, const char *shadersource, const char *optionstring )
344 const GLchar *sourcearray[2];
346 shader = glCreateShader( type );
350 if( !( optionstring ) )
352 sourcearray[0] = optionstring;
353 sourcearray[1] = shadersource;
354 glShaderSource( shader, 2, sourcearray, NULL );
355 glCompileShader( shader );
356 glGetShaderiv( shader, GL_COMPILE_STATUS, &status );
357 if( status != GL_TRUE )
359 fprintf( stderr, "ERROR: Failed to compile shader\n" );
360 glGetShaderInfoLog( shader, 8192, &loglength,infolog );
361 fprintf( stderr, "ERROR: \n%s\n\n", infolog );
362 glDeleteShader( shader );
369 static bool dmCreateProgram( DMProgram program, const char *vertexsource, const char *fragmentsource, char *optionstring )
375 program.glProgram = 0;
376 program.vertexShader = 0;
377 program.fragmentShader = 0;
379 program.vertexShader = dmCreateShader( GL_VERTEX_SHADER, vertexsource, optionstring );
380 if( !( program.vertexShader ) )
382 fprintf(stderr, "ERROR: Unable to load vertex shader\n");
385 program.fragmentShader = dmCreateShader( GL_FRAGMENT_SHADER, fragmentsource, optionstring );
386 if( !( program.fragmentShader ) )
388 fprintf(stderr, "ERROR: Unable to load fragment shader\n");
391 program.glProgram = glCreateProgram();
392 if( !( program.glProgram ) )
394 fprintf(stderr, "ERROR: Unable to create program\n");
398 glAttachShader( program.glProgram, program.vertexShader );
399 glAttachShader( program.glProgram, program.fragmentShader );
401 glBindAttribLocation(program.glProgram, GLBufferContents::vertex, "vertex");
402 glBindAttribLocation(program.glProgram, GLBufferContents::texCoord, "texCoord");
403 glBindAttribLocation(program.glProgram, GLBufferContents::color, "color");
404 glBindAttribLocation(program.glProgram, GLBufferContents::normal, "normal");
406 glLinkProgram( program.glProgram );
407 glGetProgramiv( program.glProgram, GL_LINK_STATUS, &status );
408 if( status != GL_TRUE )
410 fprintf( stderr, "ERROR, failed to link shader program\n" );
411 glGetProgramInfoLog( program.glProgram, 8192, &loglength, infolog );
412 fprintf( stderr, "ERROR: \n%s\n\n", infolog );
416 // glUseProgram( program.glProgram );
418 program.matrixloc = glGetUniformLocation( program.glProgram, "uniMatrix" );
419 program.vertexloc = glGetAttribLocation( program.glProgram, "inVertex" );
420 program.texcoord0loc = glGetAttribLocation( program.glProgram, "inTexcoord0" );
421 program.texcoord1loc = glGetAttribLocation( program.glProgram, "inTexcoord1" );
422 program.colorloc = glGetAttribLocation( program.glProgram, "inColor" );
423 #if DM_ENABLE_EXT_COLOR
424 program.extcolorloc = glGetAttribLocation( program.glProgram, "inExtColor" );
426 program.texbaseloc = glGetUniformLocation( program.glProgram, "texBase" );
427 program.flags.valid = true;
432 if( program.fragmentShader )
433 glDeleteShader( program.fragmentShader );
434 if( program.vertexShader )
435 glDeleteShader( program.vertexShader );
436 if( program.glProgram )
437 glDeleteProgram( program.glProgram );
445 const char *dmVertexShaderNormal =
447 "uniform mat4 uniMatrix;\n"
448 "in vec2 inVertex;\n"
449 "in vec2 inTexcoord0;\n"
451 "out vec2 varTexcoord0;\n"
452 "out vec4 varColor;\n"
456 " varTexcoord0 = inTexcoord0 * (1.0/" CC_STRINGIFY(DM_TEXCOORD_NORMFACTOR) ");\n"
457 " varColor = inColor;\n"
458 " gl_Position = uniMatrix * vec4( inVertex, 0.0, 1.0 );\n"
464 const char *dmFragmentShaderNormal =
466 "uniform sampler2D texBase;\n"
467 "in vec2 varTexcoord0;\n"
468 "in vec4 varColor;\n"
471 " gl_FragColor = varColor * texture2D( texBase, varTexcoord0 );\n"
477 const char *dmVertexShaderAlpha =
479 "uniform mat4 uniMatrix;\n"
480 "in vec2 inVertex;\n"
481 "in vec2 inTexcoord0;\n"
483 "out vec2 varTexcoord0;\n"
484 "out vec4 varColor;\n"
488 " varTexcoord0 = inTexcoord0 * (1.0/" CC_STRINGIFY(DM_TEXCOORD_NORMFACTOR) ");\n"
489 " varColor = inColor;\n"
490 " gl_Position = uniMatrix * vec4( inVertex, 0.0, 1.0 );\n"
496 const char *dmFragmentShaderAlpha =
498 "uniform sampler2D texBase;\n"
499 "in vec2 varTexcoord0;\n"
500 "in vec4 varColor;\n"
503 " gl_FragColor = vec4( varColor.rgb, varColor.a * texture2D( texBase, varTexcoord0 ).r );\n"
508 const char *dmVertexShaderAlphaIntensity =
510 "uniform mat4 uniMatrix;\n"
511 "in vec2 inVertex;\n"
512 "in vec2 inTexcoord0;\n"
514 "out vec2 varTexcoord0;\n"
515 "out vec4 varColor;\n"
519 " varTexcoord0 = inTexcoord0 * (1.0/" CC_STRINGIFY(DM_TEXCOORD_NORMFACTOR) ");\n"
520 " varColor = inColor;\n"
521 " gl_Position = uniMatrix * vec4( inVertex, 0.0, 1.0 );\n"
527 const char *dmFragmentShaderAlphaIntensity =
529 "uniform sampler2D texBase;\n"
530 "in vec2 varTexcoord0;\n"
531 "in vec4 varColor;\n"
535 " tex = texture2D( texBase, varTexcoord0 ).rg;\n"
536 " gl_FragColor = vec4( varColor.rgb * tex.g, varColor.a * tex.r );\n"
541 const char *dmVertexShaderAlphaIntensityExtColor =
543 "uniform mat4 uniMatrix;\n"
544 "in vec2 inVertex;\n"
545 "in vec2 inTexcoord0;\n"
547 "in vec4 inExtColor;\n"
548 "out vec2 varTexcoord0;\n"
549 "out vec4 varColor;\n"
550 "out vec4 varExtColor;\n"
554 " varTexcoord0 = inTexcoord0 * (1.0/" CC_STRINGIFY(DM_TEXCOORD_NORMFACTOR) ");\n"
555 " varColor = inColor;\n"
556 " varExtColor = inExtColor;\n"
557 " gl_Position = uniMatrix * vec4( inVertex, 0.0, 1.0 );\n"
563 const char *dmFragmentShaderAlphaIntensityExtColor =
565 "uniform sampler2D texBase;\n"
566 "in vec2 varTexcoord0;\n"
567 "in vec4 varColor;\n"
568 "in vec4 varExtColor;\n"
572 " tex = texture2D( texBase, varTexcoord0 ).rg;\n"
573 " gl_FragColor = vec4( mix( varExtColor.rgb, varColor.rgb, tex.g ), mix( varExtColor.a, varColor.a, tex.g ) * tex.r );\n"
581 static void matrixOrtho( float *m, float left, float right, float bottom, float top, float nearval, float farval )
583 float x = 2.0f / ( right - left );
584 float y = 2.0f / ( top - bottom );
585 float z = -2.0f / ( farval - nearval );
586 float tx = -( right + left ) / ( right - left );
587 float ty = -( top + bottom ) / ( top - bottom );
588 float tz = -( farval + nearval ) / ( farval - nearval );
590 #define M(row,col) m[col*4+row]
591 M(0,0) = x; M(0,1) = 0.0; M(0,2) = 0.0; M(0,3) = tx;
592 M(1,0) = 0.0; M(1,1) = y; M(1,2) = 0.0; M(1,3) = ty;
593 M(2,0) = 0.0; M(2,1) = 0.0; M(2,2) = z; M(2,3) = tz;
594 M(3,0) = 0.0; M(3,1) = 0.0; M(3,2) = 0.0; M(3,3) = 1.0;
601 /* FIXME: Radix sort, not hybrid sort! */
603 static inline int dmSortImagesCmp( DMImageBuffer *draw0, DMImageBuffer *draw1 )
605 return ( ( draw0->orderindex < draw1->orderindex ) ? 0 : 1 );
608 #define HSORT_MAIN dmSortImages
609 #define HSORT_CMP dmSortImagesCmp
610 #define HSORT_TYPE DMImageBuffer
611 #include "cchybridsort.h"
619 // TOFIX: Make this private, have a property
620 public class DrawManagerFlags : uint32 { public: bool prehistoricOpenGL:1; }
622 public class DrawManager
624 DrawManagerFlags flags;
625 DMProgram shaderPrograms[DM_PROGRAM_COUNT];
630 int imageBufferCount;
632 DMImageBuffer *imageBuffer;
633 DMImageBuffer *imageBufferTmp;
635 // Buffers for drawimages() batching
636 DMDrawBuffer drawBuffer[DM_CONTEXT_DRAW_BUFFER_COUNT];
638 int drawBarrierIndex;
639 uint32 orderBarrierMask;
641 // Counter to track program uniforms and such
646 static DMProgram *flushUseProgram( int programIndex )
648 DMProgram *program = &shaderPrograms[ programIndex ];
649 if( !program->flags.valid)
655 glUseProgram( program->glProgram );
656 if( program->lastUpdateCount != this.updateCount )
658 glUniformMatrix4fv( program->matrixloc, 1, GL_FALSE, this.matrix );
659 glUniform1i( program->texbaseloc, 0 );
660 program->lastUpdateCount = this.updateCount;
666 #if !defined(__EMSCRIPTEN__)
667 static void flushRenderDrawBufferArchaic( DMDrawBuffer drawBuffer, DMProgram program, int vertexCount )
669 glEnable( GL_TEXTURE_2D );
670 glBindBuffer( GL_ARRAY_BUFFER, drawBuffer.vbo );
671 glColor3f( 1.0, 1.0, 1.0 );
673 glEnableClientState( GL_VERTEX_ARRAY );
674 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
675 glEnableClientState( GL_COLOR_ARRAY );
677 glVertexPointer( 2, GL_FLOAT, sizeof(DMDrawVertexFlat), (void *)OFFSET(DMDrawVertexFlat,vertex) );
678 glTexCoordPointer( 2, GL_FLOAT, sizeof(DMDrawVertexFlat), (void *)OFFSET(DMDrawVertexFlat,texcoord0) );
679 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(DMDrawVertexFlat), (void *)OFFSET(DMDrawVertexFlat,color) );
681 glDrawArrays( GL_TRIANGLES, 0, vertexCount );
683 glDisableClientState( GL_VERTEX_ARRAY );
684 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
685 glDisableClientState( GL_COLOR_ARRAY );
686 glDisable( GL_TEXTURE_2D );
688 #if DM_FLUSH_EACH_RENDER_DRAW_BUFFER
693 void flushDrawImagesArchaic( )
695 bool flushflag, stateBlend;
696 int index, vertexCount, programIndex;
697 float vx0, vx1, vx2, vx3, vy0, vy1, vy2, vy3;
698 #if DM_ENABLE_IMAGE_ROTATION
699 float angsin, angcos, sizex, sizey;
701 float tx0, tx1, ty0, ty1;
702 DMImageBuffer *imageBuffer;
703 DMImage *image, *bindimage;
704 Texture texture, bindTexture;
705 DMDrawBuffer *drawBuffer;
706 DMDrawVertexFlat *vboVertex;
711 drawBarrierIndex = 0;
712 orderBarrierMask = drawBarrierIndex << DM_BARRIER_ORDER_SHIFT;
715 // Sort by image type and texture, minimize state changes
716 dmSortImages( this.imageBuffer, imageBufferTmp, imageBufferCount, (uint32)( (intptr_t)this.imageBuffer >> 4 ) );
718 // Fill a drawBuffer, write vertex and texcoords
719 drawBuffer = &this.drawBuffer[drawBufferIndex];
720 drawBufferIndex = ( drawBufferIndex + 1 ) % DM_CONTEXT_DRAW_BUFFER_COUNT;
721 glBindBuffer( GL_ARRAY_BUFFER, drawBuffer->vbo );
722 vboVertex = glMapBuffer( GL_ARRAY_BUFFER, GL_WRITE_ONLY );
725 glActiveTexture( GL_TEXTURE0 );
726 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
727 glDisable( GL_BLEND );
729 #if DM_RENDER_IMAGE_DEBUG
730 printf( " Flush %d images\n", (int)imageBufferCount );
738 imageBuffer = this.imageBuffer;
739 for( index = 0 ; index < imageBufferCount ; index++, imageBuffer++ )
741 image = imageBuffer->image;
742 texture = image->texture;
745 if( image != bindimage )
747 if( stateBlend != image->flags.blending )
749 if( texture != bindTexture )
752 if( vertexCount >= ( drawBuffer->vertexAlloc - 6 ) )
759 glUnmapBuffer( GL_ARRAY_BUFFER );
760 // Flush font manager texture updates
762 // Render buffered images
763 flushRenderDrawBufferArchaic( drawBuffer, program, vertexCount );
764 drawBuffer = &this.drawBuffer[drawBufferIndex];
765 drawBufferIndex = ( drawBufferIndex + 1 ) % DM_CONTEXT_DRAW_BUFFER_COUNT;
766 glBindBuffer( GL_ARRAY_BUFFER, drawBuffer->vbo );
767 vboVertex = glMapBuffer( GL_ARRAY_BUFFER, GL_WRITE_ONLY );
771 if( stateBlend != ( image->flags.blending ) )
773 stateBlend = image->flags.blending;
774 ( stateBlend ? glEnable : glDisable )( GL_BLEND );
775 #if DM_RENDER_IMAGE_DEBUG
776 printf( " Switch blending %d\n", stateBlend != false );
779 if( programIndex != image->programIndex )
781 programIndex = image->programIndex;
782 program = flushUseProgram( programIndex );
784 if( texture != bindTexture )
786 bindTexture = texture;
787 glBindTexture( GL_TEXTURE_2D, bindTexture.glTex );
788 #if DM_RENDER_IMAGE_DEBUG
789 printf( " Switch to texture 0x%x\n", (int)texture.orderMask );
795 #if DM_RENDER_IMAGE_DEBUG
796 printf( " Render image at %d %d, order 0x%x, texture %p\n", (int)imageBuffer->offsetx, (int)imageBuffer->offsety, (int)imageBuffer->orderindex, texture );
799 #if DM_ENABLE_IMAGE_ROTATION
800 angsin = (float)imageBuffer->angsin * (1.0f/DM_IMAGE_ROTATION_NORMFACTOR);
801 angcos = (float)imageBuffer->angcos * (1.0f/DM_IMAGE_ROTATION_NORMFACTOR);
802 sizex = (float)imageBuffer->sizex;
803 sizey = (float)imageBuffer->sizey;
804 vx0 = (float)imageBuffer->offsetx * (1.0f/DM_VERTEX_NORMFACTOR);
805 vy0 = (float)imageBuffer->offsety * (1.0f/DM_VERTEX_NORMFACTOR);
806 vx1 = vx0 + ( angcos * sizex );
807 vy1 = vy0 + ( angsin * sizex );
808 vx2 = vx0 - ( angsin * sizey );
809 vy2 = vy0 + ( angcos * sizey );
810 vx3 = vx0 + ( angcos * sizex ) - ( angsin * sizey );
811 vy3 = vy0 + ( angsin * sizex ) + ( angcos * sizey );
813 vx0 = (float)imageBuffer->offsetx * (1.0f/DM_VERTEX_NORMFACTOR);
814 vy0 = (float)imageBuffer->offsety * (1.0f/DM_VERTEX_NORMFACTOR);
815 vx3 = vx0 + (float)( imageBuffer->sizex );
816 vy3 = vy0 + (float)( imageBuffer->sizey );
823 tx0 = (float)( image->srcx ) * texture.widthinv;
824 ty0 = (float)( image->srcy ) * texture.heightinv;
825 tx1 = (float)( image->srcx + image->sizex ) * texture.widthinv;
826 ty1 = (float)( image->srcy + image->sizey ) * texture.heightinv;
829 vboVertex[0].vertex[0] = vx3;
830 vboVertex[0].vertex[1] = vy3;
831 vboVertex[0].texcoord0[0] = tx1;
832 vboVertex[0].texcoord0[1] = ty1;
833 vboVertex[0].color = imageBuffer->color;
834 #if DM_ENABLE_EXT_COLOR
835 vboVertex[0].extcolor = imageBuffer->extcolor;
837 vboVertex[1].vertex[0] = vx1;
838 vboVertex[1].vertex[1] = vy1;
839 vboVertex[1].texcoord0[0] = tx1;
840 vboVertex[1].texcoord0[1] = ty0;
841 vboVertex[1].color = imageBuffer->color;
842 #if DM_ENABLE_EXT_COLOR
843 vboVertex[1].extcolor = imageBuffer->extcolor;
845 vboVertex[2].vertex[0] = vx2;
846 vboVertex[2].vertex[1] = vy2;
847 vboVertex[2].texcoord0[0] = tx0;
848 vboVertex[2].texcoord0[1] = ty1;
849 vboVertex[2].color = imageBuffer->color;
850 #if DM_ENABLE_EXT_COLOR
851 vboVertex[2].extcolor = imageBuffer->extcolor;
853 vboVertex[3].vertex[0] = vx0;
854 vboVertex[3].vertex[1] = vy0;
855 vboVertex[3].texcoord0[0] = tx0;
856 vboVertex[3].texcoord0[1] = ty0;
857 vboVertex[3].color = imageBuffer->color;
858 #if DM_ENABLE_EXT_COLOR
859 vboVertex[3].extcolor = imageBuffer->extcolor;
861 vboVertex[4].vertex[0] = vx2;
862 vboVertex[4].vertex[1] = vy2;
863 vboVertex[4].texcoord0[0] = tx0;
864 vboVertex[4].texcoord0[1] = ty1;
865 vboVertex[4].color = imageBuffer->color;
866 #if DM_ENABLE_EXT_COLOR
867 vboVertex[4].extcolor = imageBuffer->extcolor;
869 vboVertex[5].vertex[0] = vx1;
870 vboVertex[5].vertex[1] = vy1;
871 vboVertex[5].texcoord0[0] = tx1;
872 vboVertex[5].texcoord0[1] = ty0;
873 vboVertex[5].color = imageBuffer->color;
874 #if DM_ENABLE_EXT_COLOR
875 vboVertex[5].extcolor = imageBuffer->extcolor;
882 glUnmapBuffer( GL_ARRAY_BUFFER );
884 // Flush font manager texture updates
887 // Render buffered images
888 flushRenderDrawBufferArchaic( drawBuffer, program, vertexCount );
889 imageBufferCount = 0;
897 void flushRenderDrawBuffer( DMDrawBuffer drawBuffer, DMProgram program, int vertexCount )
899 glBindBuffer( GL_ARRAY_BUFFER, drawBuffer.vbo );
900 if( program.vertexloc != -1 )
902 glEnableVertexAttribArray( program.vertexloc );
903 glVertexAttribPointer( program.vertexloc, 2, GL_SHORT, GL_FALSE, sizeof(DMDrawVertex), (void *)OFFSET(DMDrawVertex,vertex) );
905 if( program.texcoord0loc != -1 )
907 glEnableVertexAttribArray( program.texcoord0loc );
908 glVertexAttribPointer( program.texcoord0loc, 2, GL_SHORT, GL_FALSE, sizeof(DMDrawVertex), (void *)OFFSET(DMDrawVertex,texcoord0) );
910 if( program.colorloc != -1 )
912 glEnableVertexAttribArray( program.colorloc );
913 glVertexAttribPointer( program.colorloc, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(DMDrawVertex), (void *)OFFSET(DMDrawVertex,color) );
916 #if DM_ENABLE_EXT_COLOR
917 if( program.extcolorloc != -1 )
919 glEnableVertexAttribArray( program.extcolorloc );
920 glVertexAttribPointer( program.extcolorloc, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(DMDrawVertex), (void *)OFFSET(DMDrawVertex,extcolor) );
924 glDrawArrays( GL_TRIANGLES, 0, vertexCount );
926 if( program.vertexloc != -1 )
927 glDisableVertexAttribArray( program.vertexloc );
928 if( program.texcoord0loc != -1 )
929 glDisableVertexAttribArray( program.texcoord0loc );
930 if( program.colorloc != -1 )
931 glDisableVertexAttribArray( program.colorloc );
933 #if DM_FLUSH_EACH_RENDER_DRAW_BUFFER
938 void flushDrawImages( )
940 int index, stateblend, vertexcount, flushflag, programIndex;
941 float vx0, vx1, vx2, vx3, vy0, vy1, vy2, vy3;
942 #if DM_ENABLE_IMAGE_ROTATION
943 float angsin, angcos, sizex, sizey;
945 float tx0, tx1, ty0, ty1;
946 DMImageBuffer *imageBuffer;
947 DMImage *image, *bindimage;
948 Texture texture, bindtexture;
949 DMDrawBuffer *drawBuffer;
950 DMDrawVertex *vboVertex;
955 this.drawBarrierIndex = 0;
956 orderBarrierMask = drawBarrierIndex << DM_BARRIER_ORDER_SHIFT;
957 if( imageBufferCount )
959 /* Sort by image type and texture, minimize state changes */
960 dmSortImages( this.imageBuffer, this.imageBufferTmp, imageBufferCount, (uint32)( (uintptr)this.imageBuffer >> 4 ) );
962 /* Fill a drawBuffer, write vertex and texcoords */
963 drawBuffer = &this.drawBuffer[this.drawBufferIndex];
964 this.drawBufferIndex = ( this.drawBufferIndex + 1 ) % DM_CONTEXT_DRAW_BUFFER_COUNT;
965 glBindBuffer( GL_ARRAY_BUFFER, drawBuffer->vbo );
967 #if !defined(__EMSCRIPTEN__)
968 vboVertex = glMapBuffer( GL_ARRAY_BUFFER, GL_WRITE_ONLY );
972 glActiveTexture( GL_TEXTURE0 );
973 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
974 glDisable( GL_BLEND );
976 #if DM_RENDER_IMAGE_DEBUG
977 printf( " Flush %d images\n", (int)imageBufferCount );
985 imageBuffer = this.imageBuffer;
986 for( index = 0 ; index < imageBufferCount ; index++, imageBuffer++ )
988 image = imageBuffer->image;
989 texture = image->texture;
992 if( image != bindimage )
994 if( stateblend != ( image->flags.blending ) )
996 if( texture != bindtexture )
999 if( vertexcount >= ( drawBuffer->vertexAlloc - 6 ) )
1006 #if !defined(__EMSCRIPTEN__)
1007 glUnmapBuffer( GL_ARRAY_BUFFER );
1009 // Flush font manager texture updates
1012 // Render buffered images
1013 flushRenderDrawBuffer( drawBuffer, program, vertexcount );
1014 drawBuffer = &this.drawBuffer[this.drawBufferIndex];
1015 this.drawBufferIndex = ( this.drawBufferIndex + 1 ) % DM_CONTEXT_DRAW_BUFFER_COUNT;
1016 glBindBuffer( GL_ARRAY_BUFFER, drawBuffer->vbo );
1017 #if !defined(__EMSCRIPTEN__)
1018 vboVertex = glMapBuffer( GL_ARRAY_BUFFER, GL_WRITE_ONLY );
1023 if( stateblend != ( image->flags.blending ) )
1025 stateblend = image->flags.blending;
1026 ( stateblend ? glEnable : glDisable )( GL_BLEND );
1027 #if DM_RENDER_IMAGE_DEBUG
1028 printf( " Switch blending %d\n", ( stateblend != 0 ) );
1031 if( programIndex != image->programIndex )
1033 programIndex = image->programIndex;
1034 program = flushUseProgram( programIndex );
1036 if( texture != bindtexture )
1038 bindtexture = texture;
1039 glBindTexture( GL_TEXTURE_2D, bindtexture.glTex );
1040 #if DM_RENDER_IMAGE_DEBUG
1041 printf( " Switch to texture 0x%x\n", (int)texture.orderMask );
1047 #if DM_RENDER_IMAGE_DEBUG
1048 printf( " Render image at %d %d, order 0x%x, texture %p\n", (int)imageBuffer->offsetx, (int)imageBuffer->offsety, (int)imageBuffer->orderindex, texture );
1051 #if DM_ENABLE_IMAGE_ROTATION
1052 /* FIXME TODO: Don't go through float, compute texcoord integers directly */
1053 angsin = (float)imageBuffer->angsin * (1.0f/DM_IMAGE_ROTATION_NORMFACTOR);
1054 angcos = (float)imageBuffer->angcos * (1.0f/DM_IMAGE_ROTATION_NORMFACTOR);
1055 sizex = (float)imageBuffer->sizex;
1056 sizey = (float)imageBuffer->sizey;
1057 vx0 = (float)imageBuffer->offsetx * (1.0f/DM_VERTEX_NORMFACTOR);
1058 vy0 = (float)imageBuffer->offsety * (1.0f/DM_VERTEX_NORMFACTOR);
1059 vx1 = vx0 + ( angcos * sizex );
1060 vy1 = vy0 + ( angsin * sizex );
1061 vx2 = vx0 - ( angsin * sizey );
1062 vy2 = vy0 + ( angcos * sizey );
1063 vx3 = vx0 + ( angcos * sizex ) - ( angsin * sizey );
1064 vy3 = vy0 + ( angsin * sizex ) + ( angcos * sizey );
1066 /* FIXME TODO: Don't go through float, compute texcoord integers directly */
1067 vx0 = (float)imageBuffer->offsetx * (1.0f/DM_VERTEX_NORMFACTOR);
1068 vy0 = (float)imageBuffer->offsety * (1.0f/DM_VERTEX_NORMFACTOR);
1069 vx3 = vx0 + (float)( imageBuffer->sizex );
1070 vy3 = vy0 + (float)( imageBuffer->sizey );
1077 /* FIXME TODO: Don't go through float, compute texcoord integers directly */
1078 tx0 = (float)( image->srcx ) * texture.widthinv;
1079 ty0 = (float)( image->srcy ) * texture.heightinv;
1080 tx1 = (float)( image->srcx + image->sizex ) * texture.widthinv;
1081 ty1 = (float)( image->srcy + image->sizey ) * texture.heightinv;
1083 /* Write data to VBO */
1084 /* TODO: write vertex/texcoord all at once with SSE */
1085 vboVertex[0].vertex[0] = (short)( vx3 * DM_VERTEX_NORMFACTOR );
1086 vboVertex[0].vertex[1] = (short)( vy3 * DM_VERTEX_NORMFACTOR );
1087 vboVertex[0].texcoord0[0] = (short)( tx1 * DM_TEXCOORD_NORMFACTOR );
1088 vboVertex[0].texcoord0[1] = (short)( ty1 * DM_TEXCOORD_NORMFACTOR );
1089 vboVertex[0].color = imageBuffer->color;
1090 vboVertex[0].extcolor = imageBuffer->extcolor;
1091 vboVertex[1].vertex[0] = (short)( vx1 * DM_VERTEX_NORMFACTOR );
1092 vboVertex[1].vertex[1] = (short)( vy1 * DM_VERTEX_NORMFACTOR );
1093 vboVertex[1].texcoord0[0] = (short)( tx1 * DM_TEXCOORD_NORMFACTOR );
1094 vboVertex[1].texcoord0[1] = (short)( ty0 * DM_TEXCOORD_NORMFACTOR );
1095 vboVertex[1].color = imageBuffer->color;
1096 vboVertex[1].extcolor = imageBuffer->extcolor;
1097 vboVertex[2].vertex[0] = (short)( vx2 * DM_VERTEX_NORMFACTOR );
1098 vboVertex[2].vertex[1] = (short)( vy2 * DM_VERTEX_NORMFACTOR );
1099 vboVertex[2].texcoord0[0] = (short)( tx0 * DM_TEXCOORD_NORMFACTOR );
1100 vboVertex[2].texcoord0[1] = (short)( ty1 * DM_TEXCOORD_NORMFACTOR );
1101 vboVertex[2].color = imageBuffer->color;
1102 vboVertex[2].extcolor = imageBuffer->extcolor;
1103 vboVertex[3].vertex[0] = (short)( vx0 * DM_VERTEX_NORMFACTOR );
1104 vboVertex[3].vertex[1] = (short)( vy0 * DM_VERTEX_NORMFACTOR );
1105 vboVertex[3].texcoord0[0] = (short)( tx0 * DM_TEXCOORD_NORMFACTOR );
1106 vboVertex[3].texcoord0[1] = (short)( ty0 * DM_TEXCOORD_NORMFACTOR );
1107 vboVertex[3].color = imageBuffer->color;
1108 vboVertex[3].extcolor = imageBuffer->extcolor;
1109 vboVertex[4].vertex[0] = (short)( vx2 * DM_VERTEX_NORMFACTOR );
1110 vboVertex[4].vertex[1] = (short)( vy2 * DM_VERTEX_NORMFACTOR );
1111 vboVertex[4].texcoord0[0] = (short)( tx0 * DM_TEXCOORD_NORMFACTOR );
1112 vboVertex[4].texcoord0[1] = (short)( ty1 * DM_TEXCOORD_NORMFACTOR );
1113 vboVertex[4].color = imageBuffer->color;
1114 vboVertex[4].extcolor = imageBuffer->extcolor;
1115 vboVertex[5].vertex[0] = (short)( vx1 * DM_VERTEX_NORMFACTOR );
1116 vboVertex[5].vertex[1] = (short)( vy1 * DM_VERTEX_NORMFACTOR );
1117 vboVertex[5].texcoord0[0] = (short)( tx1 * DM_TEXCOORD_NORMFACTOR );
1118 vboVertex[5].texcoord0[1] = (short)( ty0 * DM_TEXCOORD_NORMFACTOR );
1119 vboVertex[5].color = imageBuffer->color;
1120 vboVertex[5].extcolor = imageBuffer->extcolor;
1126 #if !defined(__EMSCRIPTEN__)
1127 glUnmapBuffer( GL_ARRAY_BUFFER );
1129 // Flush font manager texture updates
1131 // Render buffered images
1132 flushRenderDrawBuffer( drawBuffer, program, vertexcount );
1133 imageBufferCount = 0;
1141 virtual void flush();
1143 bool init( DrawManagerFlags flags )
1145 int drawBufferIndex, programIndex;
1146 DMDrawBuffer *drawBuffer;
1149 imageBufferCount = 0;
1150 imageBufferSize = 4096;
1151 imageBuffer = new DMImageBuffer[imageBufferSize];
1152 imageBufferTmp = new DMImageBuffer[imageBufferSize];
1156 if( flags.prehistoricOpenGL )
1157 vertexSize = sizeof(DMDrawVertexFlat);
1161 for( programIndex = 0 ; programIndex < DM_PROGRAM_COUNT ; programIndex++ )
1163 program = &shaderPrograms[ programIndex ];
1165 program->lastUpdateCount = -1;
1167 program = &shaderPrograms[ DM_PROGRAM_NORMAL ];
1168 if( !( dmCreateProgram( program, dmVertexShaderNormal, dmFragmentShaderNormal, 0 ) ) )
1170 program = &shaderPrograms[ DM_PROGRAM_ALPHABLEND ];
1171 if( !( dmCreateProgram( program, dmVertexShaderAlpha, dmFragmentShaderAlpha, 0 ) ) )
1173 program = &shaderPrograms[ DM_PROGRAM_ALPHABLEND_INTENSITY ];
1174 if( !( dmCreateProgram( program, dmVertexShaderAlphaIntensity, dmFragmentShaderAlphaIntensity, 0 ) ) )
1176 program = &shaderPrograms[ DM_PROGRAM_ALPHABLEND_INTENSITY_EXTCOLOR ];
1177 if( !( dmCreateProgram( program, dmVertexShaderAlphaIntensityExtColor, dmFragmentShaderAlphaIntensityExtColor, 0 ) ) )
1179 // glUseProgram( 0 );
1180 vertexSize = sizeof(DMDrawVertex);
1183 for( drawBufferIndex = 0 ; drawBufferIndex < DM_CONTEXT_DRAW_BUFFER_COUNT ; drawBufferIndex++ )
1185 drawBuffer = &this.drawBuffer[drawBufferIndex];
1186 drawBuffer->glType = GL_FLOAT;
1187 drawBuffer->vertexCount = 0;
1188 drawBuffer->vertexAlloc = DM_CONTEXT_DRAW_BUFFER_VERTEX_ALLOC;
1191 glGenBuffers( 1, &drawBuffer->vbo );
1192 glBindBuffer( GL_ARRAY_BUFFER, drawBuffer->vbo );
1193 glBufferData( GL_ARRAY_BUFFER, drawBuffer->vertexAlloc * vertexSize, 0, GL_DYNAMIC_DRAW );
1195 drawBuffer->vertexBuffer = new byte[drawBuffer->vertexAlloc * vertexSize];
1208 for( i = 0 ; i < DM_CONTEXT_DRAW_BUFFER_COUNT ; i++ )
1210 DMDrawBuffer *db = &drawBuffer[i];
1212 glDeleteBuffers( 1, &db->vbo );
1213 delete db->vertexBuffer;
1216 // TODO: Destroy the shaders!
1218 delete imageBufferTmp;
1221 void ready( int viewportwidth, int viewportheight )
1225 if(!flags.prehistoricOpenGL && !prevProgram)
1226 glGetIntegerv(GL_CURRENT_PROGRAM, (GLint *)&prevProgram);
1227 // while(glGetError());
1231 // Save OpenGL state
1232 // FIXME: no glPushAttrib() in core profile
1233 #if !defined(__EMSCRIPTEN__)
1234 glPushClientAttrib( GL_CLIENT_ALL_ATTRIB_BITS );
1235 glPushAttrib( GL_ALL_ATTRIB_BITS );
1238 // Prepare rendering pass
1239 matrixOrtho( matrix, 0.0, (float)viewportwidth, (float)viewportheight, 0.0, -1.0f, 1.0 );
1240 norminv = 1.0f / DM_VERTEX_NORMFACTOR;
1241 for( mindex = 0 ; mindex < 12 ; mindex += 4 )
1243 matrix[mindex+0] *= norminv;
1244 matrix[mindex+1] *= norminv;
1245 matrix[mindex+2] *= norminv;
1247 drawBarrierIndex = 0;
1248 orderBarrierMask = drawBarrierIndex << DM_BARRIER_ORDER_SHIFT;
1249 orderBarrierMask = 0;
1253 if(flags.prehistoricOpenGL)
1255 glMatrixMode(MatrixMode::projection);
1256 glLoadMatrixf(matrix);
1258 glMatrixMode(MatrixMode::modelView);
1264 void drawImage( DMImage image, int offsetx, int offsety, int sizex, int sizey, uint32 color )
1266 DMImageBuffer *imageBuffer;
1268 if( image.flags.empty || ( sizex <= 0 ) || ( sizey <= 0 ) )
1271 if( imageBufferCount >= imageBufferSize )
1273 imageBufferSize <<= 1;
1274 this.imageBuffer = renew this.imageBuffer DMImageBuffer[imageBufferSize];
1275 imageBufferTmp = renew imageBufferTmp DMImageBuffer[imageBufferSize];
1278 imageBuffer = &this.imageBuffer[ imageBufferCount ];
1279 imageBuffer->image = image;
1280 imageBuffer->offsetx = (short)(offsetx << DM_VERTEX_NORMSHIFT);
1281 imageBuffer->offsety = (short)(offsety << DM_VERTEX_NORMSHIFT);
1282 imageBuffer->sizex = (short)sizex;
1283 imageBuffer->sizey = (short)sizey;
1284 #if DM_ENABLE_IMAGE_ROTATION
1285 imageBuffer->angsin = 0;
1286 imageBuffer->angcos = (short)DM_IMAGE_ROTATION_NORMFACTOR;
1288 imageBuffer->color = color;
1289 imageBuffer->orderindex = image.orderMask | orderBarrierMask;
1291 #if DM_RENDER_IMAGE_DEBUG
1292 printf( " Queue image at %d %d, order 0x%x\n", (int)imageBuffer->offsetx, (int)imageBuffer->offsety, (int)imageBuffer->orderindex );
1298 void drawImageExtColor( DMImage image, int offsetx, int offsety, int sizex, int sizey, uint32 color, uint32 extcolor )
1300 DMImageBuffer *imageBuffer;
1302 if( ( image.flags.empty ) || ( sizex <= 0 ) || ( sizey <= 0 ) )
1305 if( imageBufferCount >= imageBufferSize )
1307 imageBufferSize <<= 1;
1308 this.imageBuffer = renew this.imageBuffer DMImageBuffer[imageBufferSize];
1309 imageBufferTmp = renew imageBufferTmp DMImageBuffer[imageBufferSize];
1312 imageBuffer = &this.imageBuffer[ imageBufferCount ];
1313 imageBuffer->image = image;
1314 imageBuffer->offsetx = (short)(offsetx << DM_VERTEX_NORMSHIFT);
1315 imageBuffer->offsety = (short)(offsety << DM_VERTEX_NORMSHIFT);
1316 imageBuffer->sizex = (short)sizex;
1317 imageBuffer->sizey = (short)sizey;
1318 #if DM_ENABLE_IMAGE_ROTATION
1319 imageBuffer->angsin = 0;
1320 imageBuffer->angcos = (short)DM_IMAGE_ROTATION_NORMFACTOR;
1322 imageBuffer->color = color;
1323 #if DM_ENABLE_EXT_COLOR
1324 imageBuffer->extcolor = extcolor;
1326 imageBuffer->orderindex = image.orderMask | orderBarrierMask;
1328 #if DM_RENDER_IMAGE_DEBUG
1329 printf( " Queue image at %d %d, order 0x%x\n", (int)imageBuffer->offsetx, (int)imageBuffer->offsety, (int)imageBuffer->orderindex );
1332 this.imageBufferCount++;
1335 void drawImageFloat( DMImage image, float offsetx, float offsety, float sizex, float sizey, float angsin, float angcos, uint32 color )
1337 DMImageBuffer *imageBuffer;
1339 if( image.flags.empty || sizex <= 0 || sizey <= 0 )
1342 if( imageBufferCount >= imageBufferSize )
1344 imageBufferSize <<= 1;
1345 this.imageBuffer = renew this.imageBuffer DMImageBuffer[imageBufferSize];
1346 imageBufferTmp = renew imageBufferTmp DMImageBuffer[imageBufferSize];
1349 imageBuffer = &this.imageBuffer[ imageBufferCount ];
1350 imageBuffer->image = image;
1351 imageBuffer->offsetx = (short)roundf(offsetx * DM_VERTEX_NORMFACTOR);
1352 imageBuffer->offsety = (short)roundf(offsety * DM_VERTEX_NORMFACTOR);
1353 imageBuffer->sizex = (short)sizex;
1354 imageBuffer->sizey = (short)sizey;
1355 #if DM_ENABLE_IMAGE_ROTATION
1356 imageBuffer->angsin = (short)roundf( angsin * DM_IMAGE_ROTATION_NORMFACTOR );
1357 imageBuffer->angcos = (short)roundf( angcos * DM_IMAGE_ROTATION_NORMFACTOR );
1359 imageBuffer->color = color;
1360 imageBuffer->orderindex = image.orderMask | orderBarrierMask;
1362 #if DM_RENDER_IMAGE_DEBUG
1363 printf( " Queue image at %d %d, order 0x%x\n", (int)imageBuffer->offsetx, (int)imageBuffer->offsety, (int)imageBuffer->orderindex );
1369 void drawImageFloatExtColor( DMImage image, float offsetx, float offsety, float sizex, float sizey, float angsin, float angcos, uint32 color, uint32 extcolor )
1371 DMImageBuffer *imageBuffer;
1373 if( ( image.flags.empty ) || ( sizex <= 0 ) || ( sizey <= 0 ) )
1376 if( this.imageBufferCount >= this.imageBufferSize )
1378 imageBufferSize <<= 1;
1379 this.imageBuffer = renew this.imageBuffer DMImageBuffer[imageBufferSize];
1380 imageBufferTmp = renew imageBufferTmp DMImageBuffer[imageBufferSize];
1383 imageBuffer = &this.imageBuffer[ imageBufferCount ];
1384 imageBuffer->image = image;
1385 imageBuffer->offsetx = (short)roundf( offsetx * DM_VERTEX_NORMFACTOR );
1386 imageBuffer->offsety = (short)roundf( offsety * DM_VERTEX_NORMFACTOR );
1387 imageBuffer->sizex = (short)sizex;
1388 imageBuffer->sizey = (short)sizey;
1389 #if DM_ENABLE_IMAGE_ROTATION
1390 imageBuffer->angsin = (short)roundf( angsin * DM_IMAGE_ROTATION_NORMFACTOR );
1391 imageBuffer->angcos = (short)roundf( angcos * DM_IMAGE_ROTATION_NORMFACTOR );
1393 imageBuffer->color = color;
1394 #if DM_ENABLE_EXT_COLOR
1395 imageBuffer->extcolor = extcolor;
1397 imageBuffer->orderindex = image.orderMask | orderBarrierMask;
1399 #if DM_RENDER_IMAGE_DEBUG
1400 printf( " Queue image at %d %d, order 0x%x\n", (int)imageBuffer->offsetx, (int)imageBuffer->offsety, (int)imageBuffer->orderindex );
1403 this.imageBufferCount++;
1408 #if !defined(__EMSCRIPTEN__)
1409 if( flags.prehistoricOpenGL )
1410 flushDrawImagesArchaic( );
1416 glBindBuffer( GL_ARRAY_BUFFER, 0 );
1417 glabCurArrayBuffer = 0;
1418 if( !flags.prehistoricOpenGL )
1419 glUseProgram( prevProgram );
1420 // Restore OpenGL state
1421 // FIXME: no glPushAttrib() in core profile
1422 #if !defined(__EMSCRIPTEN__)
1424 glPopClientAttrib();
1431 if( drawBarrierIndex >= ( 1 << DM_BARRIER_ORDER_BITS ) )
1433 orderBarrierMask = drawBarrierIndex << DM_BARRIER_ORDER_SHIFT;