ecere/gfx/OpenGLDispalyDriver: Fixed setting initial projection matrix; Emscripten...
[sdk] / ecere / src / gfx / drivers / OpenGLDisplayDriver.ec
index 1533928..4c06cf0 100644 (file)
@@ -7,54 +7,54 @@ namespace gfx::drivers;
 #if defined(__unix__) || defined(__APPLE__)
 
 #if !defined(__MINGW32__)
-#define GL_GLEXT_PROTOTYPES
+   #define GL_GLEXT_PROTOTYPES
 #endif
 
+#define pointer _pointer
+
 #ifdef ECERE_MINIGLX
 
 //#include <GL/miniglx.h>
 
 #else
 
-#if defined(__ANDROID__)
-
-#else
-
-#define property _property
-#define new _new
-#define class _class
+   #if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
+
+      #define property _property
+      #define new _new
+      #define class _class
+
+      #define Window    X11Window
+      #define Cursor    X11Cursor
+      #define Font      X11Font
+      #define Display   X11Display
+      #define Time      X11Time
+      #define KeyCode   X11KeyCode
+      #define Picture   X11Picture
+      #define uint _uint
+
+      #include <X11/Xlib.h>
+      #include <X11/Xutil.h>
+      #include <GL/glx.h>
+      #include <X11/extensions/XShm.h>
+      #include <sys/ipc.h>
+      #include <sys/shm.h>
+      #include <X11/extensions/Xrender.h>
+      #include <X11/extensions/shape.h>
+
+      #undef Window
+      #undef Cursor
+      #undef Font
+      #undef Display
+      #undef Time
+      #undef KeyCode
+      #undef Picture
+      #undef uint
+      #undef new
+      #undef property
+      #undef class
 
-#define Window    X11Window
-#define Cursor    X11Cursor
-#define Font      X11Font
-#define Display   X11Display
-#define Time      X11Time
-#define KeyCode   X11KeyCode
-#define Picture   X11Picture
-#define uint _uint
-
-#include <X11/Xlib.h>
-#include <X11/Xutil.h>
-#include <GL/glx.h>
-#include <X11/extensions/XShm.h>
-#include <sys/ipc.h>
-#include <sys/shm.h>
-#include <X11/extensions/Xrender.h>
-#include <X11/extensions/shape.h>
-
-#undef Window
-#undef Cursor
-#undef Font
-#undef Display
-#undef Time
-#undef KeyCode
-#undef Picture
-#undef uint
-#undef new
-#undef property
-#undef class
-
-#endif
+   #endif
 
 #endif
 
@@ -67,38 +67,76 @@ namespace gfx::drivers;
 #if defined(__WIN32__) || defined(__unix__) || defined(__APPLE__)
 
 #if defined(__WIN32__)
-#define WIN32_LEAN_AND_MEAN
-#undef _WIN32_WINNT
-#define _WIN32_WINNT 0x0500
-#define String Sting_
-#include <windows.h>
-#undef String
+   #define WIN32_LEAN_AND_MEAN
+   #undef _WIN32_WINNT
+   #define _WIN32_WINNT 0x0502
+   #define String Sting_
+   #include <windows.h>
+   #undef String
 #endif
 
 #if defined(__ANDROID__)
 
-#include <GLES/gl.h>
-#include <EGL/egl.h>
+   #include <GLES/gl.h>
+   #include <EGL/egl.h>
+
+#elif defined(__EMSCRIPTEN__)
+
+   #define property _property
+   #define uint _uint
+
+   #include <GL/gl.h>
+
+   //#include <GLES/gl.h>
+   //#include <EGL/egl.h>
+
+   //#include <GLES2/gl.h>
+   //#include <EGL/egl.h>
+
+   //#include <GLES2/gl2.h>
+   #include <GL/glfw.h>
+   #include <emscripten/emscripten.h>
+
+   #undef property
+   #undef uint
 
 #else
 
-#include <GL/gl.h>
-#include <GL/glext.h>
+   #include <GL/gl.h>
+   #include <GL/glext.h>
 
 #endif
 
+#if defined(__EMSCRIPTEN__)
+#define EM_MODE
+#endif
+
+#define EM_MODE
+
+#undef pointer
+
 import "Display"
 
 #if defined(__unix__) || defined(__APPLE__)
 
-#ifndef __ANDROID__
-import "XInterface"
-#endif
+   #if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
+   import "XInterface"
+   #endif
 
 #endif
 
 static double nearPlane = 1;
 
+public double glesGetNearPlane()
+{
+   return nearPlane;
+}
+
+public void glesSetNearPlane(double value)
+{
+   nearPlane = value;
+}
+
 #define glLoadMatrix glLoadMatrixd
 #define glMultMatrix glMultMatrixd
 #define glGetMatrix  glGetDoublev
@@ -146,407 +184,475 @@ static double nearPlane = 1;
 
 #if defined(__WIN32__)
 
-#define WGL_SAMPLE_BUFFERS_ARB              0x2041
-#define WGL_SAMPLES_ARB                     0x2042
-
-#define  WGL_WGLEXT_VERSION   1
-#define  WGL_FRONT_COLOR_BUFFER_BIT_ARB   0x00000001
-#define  WGL_BACK_COLOR_BUFFER_BIT_ARB   0x00000002
-#define  WGL_DEPTH_BUFFER_BIT_ARB   0x00000004
-#define  WGL_STENCIL_BUFFER_BIT_ARB   0x00000008
-#define  WGL_NUMBER_PIXEL_FORMATS_ARB   0x2000
-#define  WGL_DRAW_TO_WINDOW_ARB   0x2001
-#define  WGL_DRAW_TO_BITMAP_ARB   0x2002
-#define  WGL_ACCELERATION_ARB   0x2003
-#define  WGL_NEED_PALETTE_ARB   0x2004
-#define  WGL_NEED_SYSTEM_PALETTE_ARB   0x2005
-#define  WGL_SWAP_LAYER_BUFFERS_ARB   0x2006
-#define  WGL_SWAP_METHOD_ARB   0x2007
-#define  WGL_NUMBER_OVERLAYS_ARB   0x2008
-#define  WGL_NUMBER_UNDERLAYS_ARB   0x2009
-#define  WGL_TRANSPARENT_ARB   0x200A
-#define  WGL_TRANSPARENT_RED_VALUE_ARB   0x2037
-#define  WGL_TRANSPARENT_GREEN_VALUE_ARB   0x2038
-#define  WGL_TRANSPARENT_BLUE_VALUE_ARB   0x2039
-#define  WGL_TRANSPARENT_ALPHA_VALUE_ARB   0x203A
-#define  WGL_TRANSPARENT_INDEX_VALUE_ARB   0x203B
-#define  WGL_SHARE_DEPTH_ARB   0x200C
-#define  WGL_SHARE_STENCIL_ARB   0x200D
-#define  WGL_SHARE_ACCUM_ARB   0x200E
-#define  WGL_SUPPORT_GDI_ARB   0x200F
-#define  WGL_SUPPORT_OPENGL_ARB   0x2010
-#define  WGL_DOUBLE_BUFFER_ARB   0x2011
-#define  WGL_STEREO_ARB   0x2012
-#define  WGL_PIXEL_TYPE_ARB   0x2013
-#define  WGL_COLOR_BITS_ARB   0x2014
-#define  WGL_RED_BITS_ARB   0x2015
-#define  WGL_RED_SHIFT_ARB   0x2016
-#define  WGL_GREEN_BITS_ARB   0x2017
-#define  WGL_GREEN_SHIFT_ARB   0x2018
-#define  WGL_BLUE_BITS_ARB   0x2019
-#define  WGL_BLUE_SHIFT_ARB   0x201A
-#define  WGL_ALPHA_BITS_ARB   0x201B
-#define  WGL_ALPHA_SHIFT_ARB   0x201C
-#define  WGL_ACCUM_BITS_ARB   0x201D
-#define  WGL_ACCUM_RED_BITS_ARB   0x201E
-#define  WGL_ACCUM_GREEN_BITS_ARB   0x201F
-#define  WGL_ACCUM_BLUE_BITS_ARB   0x2020
-#define  WGL_ACCUM_ALPHA_BITS_ARB   0x2021
-#define  WGL_DEPTH_BITS_ARB   0x2022
-#define  WGL_STENCIL_BITS_ARB   0x2023
-#define  WGL_AUX_BUFFERS_ARB   0x2024
-#define  WGL_NO_ACCELERATION_ARB   0x2025
-#define  WGL_GENERIC_ACCELERATION_ARB   0x2026
-#define  WGL_FULL_ACCELERATION_ARB   0x2027
-#define  WGL_SWAP_EXCHANGE_ARB   0x2028
-#define  WGL_SWAP_COPY_ARB   0x2029
-#define  WGL_SWAP_UNDEFINED_ARB   0x202A
-#define  WGL_TYPE_RGBA_ARB   0x202B
-#define  WGL_TYPE_COLORINDEX_ARB   0x202C
-#define  ERROR_INVALID_PIXEL_TYPE_ARB   0x2043
-#define  ERROR_INCOMPATIBLE_DEVICE_CONTEXTS_ARB   0x2054
-#define  WGL_DRAW_TO_PBUFFER_ARB   0x202D
-#define  WGL_MAX_PBUFFER_PIXELS_ARB   0x202E
-#define  WGL_MAX_PBUFFER_WIDTH_ARB   0x202F
-#define  WGL_MAX_PBUFFER_HEIGHT_ARB   0x2030
-#define  WGL_PBUFFER_LARGEST_ARB   0x2033
-#define  WGL_PBUFFER_WIDTH_ARB   0x2034
-#define  WGL_PBUFFER_HEIGHT_ARB   0x2035
-#define  WGL_PBUFFER_LOST_ARB   0x2036
-#define  ERROR_INVALID_PIXEL_TYPE_EXT   0x2043
-#define  WGL_NUMBER_PIXEL_FORMATS_EXT   0x2000
-#define  WGL_DRAW_TO_WINDOW_EXT   0x2001
-#define  WGL_DRAW_TO_BITMAP_EXT   0x2002
-#define  WGL_ACCELERATION_EXT   0x2003
-#define  WGL_NEED_PALETTE_EXT   0x2004
-#define  WGL_NEED_SYSTEM_PALETTE_EXT   0x2005
-#define  WGL_SWAP_LAYER_BUFFERS_EXT   0x2006
-#define  WGL_SWAP_METHOD_EXT   0x2007
-#define  WGL_NUMBER_OVERLAYS_EXT   0x2008
-#define  WGL_NUMBER_UNDERLAYS_EXT   0x2009
-#define  WGL_TRANSPARENT_EXT   0x200A
-#define  WGL_TRANSPARENT_VALUE_EXT   0x200B
-#define  WGL_SHARE_DEPTH_EXT   0x200C
-#define  WGL_SHARE_STENCIL_EXT   0x200D
-#define  WGL_SHARE_ACCUM_EXT   0x200E
-#define  WGL_SUPPORT_GDI_EXT   0x200F
-#define  WGL_SUPPORT_OPENGL_EXT   0x2010
-#define  WGL_DOUBLE_BUFFER_EXT   0x2011
-#define  WGL_STEREO_EXT   0x2012
-#define  WGL_PIXEL_TYPE_EXT   0x2013
-#define  WGL_COLOR_BITS_EXT   0x2014
-#define  WGL_RED_BITS_EXT   0x2015
-#define  WGL_RED_SHIFT_EXT   0x2016
-#define  WGL_GREEN_BITS_EXT   0x2017
-#define  WGL_GREEN_SHIFT_EXT   0x2018
-#define  WGL_BLUE_BITS_EXT   0x2019
-#define  WGL_BLUE_SHIFT_EXT   0x201A
-#define  WGL_ALPHA_BITS_EXT   0x201B
-#define  WGL_ALPHA_SHIFT_EXT   0x201C
-#define  WGL_ACCUM_BITS_EXT   0x201D
-#define  WGL_ACCUM_RED_BITS_EXT   0x201E
-#define  WGL_ACCUM_GREEN_BITS_EXT   0x201F
-#define  WGL_ACCUM_BLUE_BITS_EXT   0x2020
-#define  WGL_ACCUM_ALPHA_BITS_EXT   0x2021
-#define  WGL_DEPTH_BITS_EXT   0x2022
-#define  WGL_STENCIL_BITS_EXT   0x2023
-#define  WGL_AUX_BUFFERS_EXT   0x2024
-#define  WGL_NO_ACCELERATION_EXT   0x2025
-#define  WGL_GENERIC_ACCELERATION_EXT   0x2026
-#define  WGL_FULL_ACCELERATION_EXT   0x2027
-#define  WGL_SWAP_EXCHANGE_EXT   0x2028
-#define  WGL_SWAP_COPY_EXT   0x2029
-#define  WGL_SWAP_UNDEFINED_EXT   0x202A
-#define  WGL_TYPE_RGBA_EXT   0x202B
-#define  WGL_TYPE_COLORINDEX_EXT   0x202C
-#define  WGL_DRAW_TO_PBUFFER_EXT   0x202D
-#define  WGL_MAX_PBUFFER_PIXELS_EXT   0x202E
-#define  WGL_MAX_PBUFFER_WIDTH_EXT   0x202F
-#define  WGL_MAX_PBUFFER_HEIGHT_EXT   0x2030
-#define  WGL_OPTIMAL_PBUFFER_WIDTH_EXT   0x2031
-#define  WGL_OPTIMAL_PBUFFER_HEIGHT_EXT   0x2032
-#define  WGL_PBUFFER_LARGEST_EXT   0x2033
-#define  WGL_PBUFFER_WIDTH_EXT   0x2034
-#define  WGL_PBUFFER_HEIGHT_EXT   0x2035
-#define  WGL_DEPTH_FLOAT_EXT   0x2040
-#define  WGL_SAMPLE_BUFFERS_3DFX   0x2060
-#define  WGL_SAMPLES_3DFX   0x2061
-#define  WGL_SAMPLE_BUFFERS_EXT   0x2041
-#define  WGL_SAMPLES_EXT   0x2042
-#define  WGL_GENLOCK_SOURCE_MULTIVIEW_I3D   0x2044
-#define  WGL_GENLOCK_SOURCE_EXTENAL_SYNC_I3D   0x2045
-#define  WGL_GENLOCK_SOURCE_EXTENAL_FIELD_I3D   0x2046
-#define  WGL_GENLOCK_SOURCE_EXTENAL_TTL_I3D   0x2047
-#define  WGL_GENLOCK_SOURCE_DIGITAL_SYNC_I3D   0x2048
-#define  WGL_GENLOCK_SOURCE_DIGITAL_FIELD_I3D   0x2049
-#define  WGL_GENLOCK_SOURCE_EDGE_FALLING_I3D   0x204A
-#define  WGL_GENLOCK_SOURCE_EDGE_RISING_I3D   0x204B
-#define  WGL_GENLOCK_SOURCE_EDGE_BOTH_I3D   0x204C
-#define  WGL_GAMMA_TABLE_SIZE_I3D   0x204E
-#define  WGL_GAMMA_EXCLUDE_DESKTOP_I3D   0x204F
-#define  WGL_DIGITAL_VIDEO_CURSOR_ALPHA_FRAMEBUFFER_I3D   0x2050
-#define  WGL_DIGITAL_VIDEO_CURSOR_ALPHA_VALUE_I3D   0x2051
-#define  WGL_DIGITAL_VIDEO_CURSOR_INCLUDED_I3D   0x2052
-#define  WGL_DIGITAL_VIDEO_GAMMA_CORRECTED_I3D   0x2053
-#define  WGL_ARB_buffer_region   1
-#define  WGL_ARB_extensions_string   1
-#define  WGL_ARB_pixel_format   1
-#define  WGL_ARB_make_current_read   1
-#define  WGL_ARB_pbuffer   1
-#define  WGL_EXT_display_color_table   1
-#define  WGL_EXT_extensions_string   1
-#define  WGL_EXT_make_current_read   1
-#define  WGL_EXT_pbuffer   1
-#define  WGL_EXT_pixel_format   1
-#define  WGL_EXT_swap_control   1
-#define  WGL_WGL_EXT_depth_float   1
-#define  WGL_WGL_3DFX_multisample   1
-#define  WGL_WGL_EXT_multisample   1
-#define  WGL_NV_allocate_memory   1
+   #define WGL_SAMPLE_BUFFERS_ARB              0x2041
+   #define WGL_SAMPLES_ARB                     0x2042
+
+   #define  WGL_WGLEXT_VERSION   1
+   #define  WGL_FRONT_COLOR_BUFFER_BIT_ARB   0x00000001
+   #define  WGL_BACK_COLOR_BUFFER_BIT_ARB   0x00000002
+   #define  WGL_DEPTH_BUFFER_BIT_ARB   0x00000004
+   #define  WGL_STENCIL_BUFFER_BIT_ARB   0x00000008
+   #define  WGL_NUMBER_PIXEL_FORMATS_ARB   0x2000
+   #define  WGL_DRAW_TO_WINDOW_ARB   0x2001
+   #define  WGL_DRAW_TO_BITMAP_ARB   0x2002
+   #define  WGL_ACCELERATION_ARB   0x2003
+   #define  WGL_NEED_PALETTE_ARB   0x2004
+   #define  WGL_NEED_SYSTEM_PALETTE_ARB   0x2005
+   #define  WGL_SWAP_LAYER_BUFFERS_ARB   0x2006
+   #define  WGL_SWAP_METHOD_ARB   0x2007
+   #define  WGL_NUMBER_OVERLAYS_ARB   0x2008
+   #define  WGL_NUMBER_UNDERLAYS_ARB   0x2009
+   #define  WGL_TRANSPARENT_ARB   0x200A
+   #define  WGL_TRANSPARENT_RED_VALUE_ARB   0x2037
+   #define  WGL_TRANSPARENT_GREEN_VALUE_ARB   0x2038
+   #define  WGL_TRANSPARENT_BLUE_VALUE_ARB   0x2039
+   #define  WGL_TRANSPARENT_ALPHA_VALUE_ARB   0x203A
+   #define  WGL_TRANSPARENT_INDEX_VALUE_ARB   0x203B
+   #define  WGL_SHARE_DEPTH_ARB   0x200C
+   #define  WGL_SHARE_STENCIL_ARB   0x200D
+   #define  WGL_SHARE_ACCUM_ARB   0x200E
+   #define  WGL_SUPPORT_GDI_ARB   0x200F
+   #define  WGL_SUPPORT_OPENGL_ARB   0x2010
+   #define  WGL_DOUBLE_BUFFER_ARB   0x2011
+   #define  WGL_STEREO_ARB   0x2012
+   #define  WGL_PIXEL_TYPE_ARB   0x2013
+   #define  WGL_COLOR_BITS_ARB   0x2014
+   #define  WGL_RED_BITS_ARB   0x2015
+   #define  WGL_RED_SHIFT_ARB   0x2016
+   #define  WGL_GREEN_BITS_ARB   0x2017
+   #define  WGL_GREEN_SHIFT_ARB   0x2018
+   #define  WGL_BLUE_BITS_ARB   0x2019
+   #define  WGL_BLUE_SHIFT_ARB   0x201A
+   #define  WGL_ALPHA_BITS_ARB   0x201B
+   #define  WGL_ALPHA_SHIFT_ARB   0x201C
+   #define  WGL_ACCUM_BITS_ARB   0x201D
+   #define  WGL_ACCUM_RED_BITS_ARB   0x201E
+   #define  WGL_ACCUM_GREEN_BITS_ARB   0x201F
+   #define  WGL_ACCUM_BLUE_BITS_ARB   0x2020
+   #define  WGL_ACCUM_ALPHA_BITS_ARB   0x2021
+   #define  WGL_DEPTH_BITS_ARB   0x2022
+   #define  WGL_STENCIL_BITS_ARB   0x2023
+   #define  WGL_AUX_BUFFERS_ARB   0x2024
+   #define  WGL_NO_ACCELERATION_ARB   0x2025
+   #define  WGL_GENERIC_ACCELERATION_ARB   0x2026
+   #define  WGL_FULL_ACCELERATION_ARB   0x2027
+   #define  WGL_SWAP_EXCHANGE_ARB   0x2028
+   #define  WGL_SWAP_COPY_ARB   0x2029
+   #define  WGL_SWAP_UNDEFINED_ARB   0x202A
+   #define  WGL_TYPE_RGBA_ARB   0x202B
+   #define  WGL_TYPE_COLORINDEX_ARB   0x202C
+   #define  ERROR_INVALID_PIXEL_TYPE_ARB   0x2043
+   #define  ERROR_INCOMPATIBLE_DEVICE_CONTEXTS_ARB   0x2054
+   #define  WGL_DRAW_TO_PBUFFER_ARB   0x202D
+   #define  WGL_MAX_PBUFFER_PIXELS_ARB   0x202E
+   #define  WGL_MAX_PBUFFER_WIDTH_ARB   0x202F
+   #define  WGL_MAX_PBUFFER_HEIGHT_ARB   0x2030
+   #define  WGL_PBUFFER_LARGEST_ARB   0x2033
+   #define  WGL_PBUFFER_WIDTH_ARB   0x2034
+   #define  WGL_PBUFFER_HEIGHT_ARB   0x2035
+   #define  WGL_PBUFFER_LOST_ARB   0x2036
+   #define  ERROR_INVALID_PIXEL_TYPE_EXT   0x2043
+   #define  WGL_NUMBER_PIXEL_FORMATS_EXT   0x2000
+   #define  WGL_DRAW_TO_WINDOW_EXT   0x2001
+   #define  WGL_DRAW_TO_BITMAP_EXT   0x2002
+   #define  WGL_ACCELERATION_EXT   0x2003
+   #define  WGL_NEED_PALETTE_EXT   0x2004
+   #define  WGL_NEED_SYSTEM_PALETTE_EXT   0x2005
+   #define  WGL_SWAP_LAYER_BUFFERS_EXT   0x2006
+   #define  WGL_SWAP_METHOD_EXT   0x2007
+   #define  WGL_NUMBER_OVERLAYS_EXT   0x2008
+   #define  WGL_NUMBER_UNDERLAYS_EXT   0x2009
+   #define  WGL_TRANSPARENT_EXT   0x200A
+   #define  WGL_TRANSPARENT_VALUE_EXT   0x200B
+   #define  WGL_SHARE_DEPTH_EXT   0x200C
+   #define  WGL_SHARE_STENCIL_EXT   0x200D
+   #define  WGL_SHARE_ACCUM_EXT   0x200E
+   #define  WGL_SUPPORT_GDI_EXT   0x200F
+   #define  WGL_SUPPORT_OPENGL_EXT   0x2010
+   #define  WGL_DOUBLE_BUFFER_EXT   0x2011
+   #define  WGL_STEREO_EXT   0x2012
+   #define  WGL_PIXEL_TYPE_EXT   0x2013
+   #define  WGL_COLOR_BITS_EXT   0x2014
+   #define  WGL_RED_BITS_EXT   0x2015
+   #define  WGL_RED_SHIFT_EXT   0x2016
+   #define  WGL_GREEN_BITS_EXT   0x2017
+   #define  WGL_GREEN_SHIFT_EXT   0x2018
+   #define  WGL_BLUE_BITS_EXT   0x2019
+   #define  WGL_BLUE_SHIFT_EXT   0x201A
+   #define  WGL_ALPHA_BITS_EXT   0x201B
+   #define  WGL_ALPHA_SHIFT_EXT   0x201C
+   #define  WGL_ACCUM_BITS_EXT   0x201D
+   #define  WGL_ACCUM_RED_BITS_EXT   0x201E
+   #define  WGL_ACCUM_GREEN_BITS_EXT   0x201F
+   #define  WGL_ACCUM_BLUE_BITS_EXT   0x2020
+   #define  WGL_ACCUM_ALPHA_BITS_EXT   0x2021
+   #define  WGL_DEPTH_BITS_EXT   0x2022
+   #define  WGL_STENCIL_BITS_EXT   0x2023
+   #define  WGL_AUX_BUFFERS_EXT   0x2024
+   #define  WGL_NO_ACCELERATION_EXT   0x2025
+   #define  WGL_GENERIC_ACCELERATION_EXT   0x2026
+   #define  WGL_FULL_ACCELERATION_EXT   0x2027
+   #define  WGL_SWAP_EXCHANGE_EXT   0x2028
+   #define  WGL_SWAP_COPY_EXT   0x2029
+   #define  WGL_SWAP_UNDEFINED_EXT   0x202A
+   #define  WGL_TYPE_RGBA_EXT   0x202B
+   #define  WGL_TYPE_COLORINDEX_EXT   0x202C
+   #define  WGL_DRAW_TO_PBUFFER_EXT   0x202D
+   #define  WGL_MAX_PBUFFER_PIXELS_EXT   0x202E
+   #define  WGL_MAX_PBUFFER_WIDTH_EXT   0x202F
+   #define  WGL_MAX_PBUFFER_HEIGHT_EXT   0x2030
+   #define  WGL_OPTIMAL_PBUFFER_WIDTH_EXT   0x2031
+   #define  WGL_OPTIMAL_PBUFFER_HEIGHT_EXT   0x2032
+   #define  WGL_PBUFFER_LARGEST_EXT   0x2033
+   #define  WGL_PBUFFER_WIDTH_EXT   0x2034
+   #define  WGL_PBUFFER_HEIGHT_EXT   0x2035
+   #define  WGL_DEPTH_FLOAT_EXT   0x2040
+   #define  WGL_SAMPLE_BUFFERS_3DFX   0x2060
+   #define  WGL_SAMPLES_3DFX   0x2061
+   #define  WGL_SAMPLE_BUFFERS_EXT   0x2041
+   #define  WGL_SAMPLES_EXT   0x2042
+   #define  WGL_GENLOCK_SOURCE_MULTIVIEW_I3D   0x2044
+   #define  WGL_GENLOCK_SOURCE_EXTENAL_SYNC_I3D   0x2045
+   #define  WGL_GENLOCK_SOURCE_EXTENAL_FIELD_I3D   0x2046
+   #define  WGL_GENLOCK_SOURCE_EXTENAL_TTL_I3D   0x2047
+   #define  WGL_GENLOCK_SOURCE_DIGITAL_SYNC_I3D   0x2048
+   #define  WGL_GENLOCK_SOURCE_DIGITAL_FIELD_I3D   0x2049
+   #define  WGL_GENLOCK_SOURCE_EDGE_FALLING_I3D   0x204A
+   #define  WGL_GENLOCK_SOURCE_EDGE_RISING_I3D   0x204B
+   #define  WGL_GENLOCK_SOURCE_EDGE_BOTH_I3D   0x204C
+   #define  WGL_GAMMA_TABLE_SIZE_I3D   0x204E
+   #define  WGL_GAMMA_EXCLUDE_DESKTOP_I3D   0x204F
+   #define  WGL_DIGITAL_VIDEO_CURSOR_ALPHA_FRAMEBUFFER_I3D   0x2050
+   #define  WGL_DIGITAL_VIDEO_CURSOR_ALPHA_VALUE_I3D   0x2051
+   #define  WGL_DIGITAL_VIDEO_CURSOR_INCLUDED_I3D   0x2052
+   #define  WGL_DIGITAL_VIDEO_GAMMA_CORRECTED_I3D   0x2053
+   #define  WGL_ARB_buffer_region   1
+   #define  WGL_ARB_extensions_string   1
+   #define  WGL_ARB_pixel_format   1
+   #define  WGL_ARB_make_current_read   1
+   #define  WGL_ARB_pbuffer   1
+   #define  WGL_EXT_display_color_table   1
+   #define  WGL_EXT_extensions_string   1
+   #define  WGL_EXT_make_current_read   1
+   #define  WGL_EXT_pbuffer   1
+   #define  WGL_EXT_pixel_format   1
+   #define  WGL_EXT_swap_control   1
+   #define  WGL_WGL_EXT_depth_float   1
+   #define  WGL_WGL_3DFX_multisample   1
+   #define  WGL_WGL_EXT_multisample   1
+   #define  WGL_NV_allocate_memory   1
 
-/*
-typedef void (APIENTRY * PFNGLACTIVETEXTUREARBPROC) (GLenum target);
-typedef void (APIENTRY * PFNGLMULTITEXCOORD2FARBPROC) (GLenum target, GLfloat s, GLfloat t);
-typedef void (APIENTRY * PFNGLCLIENTACTIVETEXTUREARBPROC) (GLenum target);
-typedef void (APIENTRY * PFNGLLOCKARRAYSEXTPROC) (GLint first, GLsizei count);
-typedef void (APIENTRY * PFNGLUNLOCKARRAYSEXTPROC) (void);
-*/
+   /*
+   typedef void (APIENTRY * PFNGLACTIVETEXTUREARBPROC) (GLenum target);
+   typedef void (APIENTRY * PFNGLMULTITEXCOORD2FARBPROC) (GLenum target, GLfloat s, GLfloat t);
+   typedef void (APIENTRY * PFNGLCLIENTACTIVETEXTUREARBPROC) (GLenum target);
+   typedef void (APIENTRY * PFNGLLOCKARRAYSEXTPROC) (GLint first, GLsizei count);
+   typedef void (APIENTRY * PFNGLUNLOCKARRAYSEXTPROC) (void);
+   */
 
-/*
-typedef int (APIENTRY * PFNGLBINDBUFFERARBPROC) (GLenum target, GLuint buffer);
-typedef int (APIENTRY * PFNGLDELETEBUFFERSARBPROC) (GLsizei n, const GLuint *buffers);
-typedef int (APIENTRY * PFNGLGENBUFFERSARBPROC) (GLsizei n, GLuint *buffers);
-typedef int (APIENTRY * PFNGLBUFFERDATAARBPROC) (GLenum target, int size, const GLvoid *data, GLenum usage);
-*/
-typedef int (APIENTRY * PFNWGLCHOOSEPIXELFORMATARBPROC) ();
-typedef void * (APIENTRY * PFNWGLCREATEPBUFFERARBPROC) (HDC hDC, int iPixelFormat, int iWidth, int iHeight, const int *piAttribList);
-typedef HDC (APIENTRY * PFNWGLGETPBUFFERDCARBPROC) (void * hPbuffer);
-typedef int (APIENTRY * PFNWGLRELEASEPBUFFERDCARBPROC) (void * hPbuffer, HDC hDC);
-typedef BOOL (APIENTRY * PFNWGLDESTROYPBUFFERARBPROC) (void * hPbuffer);
-typedef BOOL (APIENTRY * PFNWGLQUERYPBUFFERARBPROC) (void * hPbuffer, int iAttribute, int *piValue);
-
-static PFNGLMAPBUFFERARBPROC glMapBufferARB = null;
-static PFNGLUNMAPBUFFERARBPROC glUnmapBufferARB = null;
-static PFNGLACTIVETEXTUREARBPROC glActiveTextureARB = null;
-static PFNGLMULTITEXCOORD2FARBPROC glMultiTexCoord2fARB = null;
-static PFNGLCLIENTACTIVETEXTUREARBPROC glClientActiveTextureARB = null;
-static PFNGLLOCKARRAYSEXTPROC glLockArraysEXT = null;
-static PFNGLUNLOCKARRAYSEXTPROC glUnlockArraysEXT = null;
-static PFNGLBLENDFUNCSEPARATEPROC glBlendFuncSeparate = null;
-
-static PFNGLGENBUFFERSARBPROC glGenBuffersARB = null;
-static PFNGLBINDBUFFERARBPROC glBindBufferARB = null;
-static PFNGLBUFFERDATAARBPROC glBufferDataARB = null;
-static PFNGLDELETEBUFFERSARBPROC glDeleteBuffersARB = null;
-static PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = null;
-static PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = null;
-static PFNWGLCREATEPBUFFERARBPROC wglCreatePbufferARB = null;
-static PFNWGLGETPBUFFERDCARBPROC wglGetPbufferDCARB = null;
-static PFNWGLQUERYPBUFFERARBPROC wglQueryPbufferARB = null;
-static PFNWGLDESTROYPBUFFERARBPROC wglDestroyPbufferARB = null;
-static PFNWGLRELEASEPBUFFERDCARBPROC wglReleasePbufferDCARB = null;
-static PFNWGLBINDTEXIMAGEARBPROC wglBindTexImageARB = null;
-static PFNWGLRELEASETEXIMAGEARBPROC wglReleaseTexImageARB = null;
-
-#ifdef WGL_WGLEXT_PROTOTYPES
-extern BOOL WINAPI wglSwapIntervalEXT (int);
-extern int WINAPI wglGetSwapIntervalEXT (void);
-#endif /* WGL_WGLEXT_PROTOTYPES */
-typedef BOOL (WINAPI * PFNWGLSWAPINTERVALEXTPROC) (int interval);
-typedef int (WINAPI * PFNWGLGETSWAPINTERVALEXTPROC) (void);
-
-static PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = NULL;
+   /*
+   typedef int (APIENTRY * PFNGLBINDBUFFERARBPROC) (GLenum target, GLuint buffer);
+   typedef int (APIENTRY * PFNGLDELETEBUFFERSARBPROC) (GLsizei n, const GLuint *buffers);
+   typedef int (APIENTRY * PFNGLGENBUFFERSARBPROC) (GLsizei n, GLuint *buffers);
+   typedef int (APIENTRY * PFNGLBUFFERDATAARBPROC) (GLenum target, int size, const GLvoid *data, GLenum usage);
+   */
+   typedef int (APIENTRY * PFNWGLCHOOSEPIXELFORMATARBPROC) ();
+   typedef void * (APIENTRY * PFNWGLCREATEPBUFFERARBPROC) (HDC hDC, int iPixelFormat, int iWidth, int iHeight, const int *piAttribList);
+   typedef HDC (APIENTRY * PFNWGLGETPBUFFERDCARBPROC) (void * hPbuffer);
+   typedef int (APIENTRY * PFNWGLRELEASEPBUFFERDCARBPROC) (void * hPbuffer, HDC hDC);
+   typedef BOOL (APIENTRY * PFNWGLDESTROYPBUFFERARBPROC) (void * hPbuffer);
+   typedef BOOL (APIENTRY * PFNWGLQUERYPBUFFERARBPROC) (void * hPbuffer, int iAttribute, int *piValue);
+   typedef const char * (APIENTRY * PFNWGLGETEXTENSIONSSTRINGARBPROC) (HDC hdc);
+   typedef BOOL (APIENTRY * PFNWGLBINDTEXIMAGEARBPROC) (void * hPbuffer, int iBuffer);
+   typedef BOOL (APIENTRY * PFNWGLRELEASETEXIMAGEARBPROC) (void * hPbuffer, int iBuffer);
+
+   static PFNGLMAPBUFFERARBPROC glMapBufferARB = null;
+   static PFNGLUNMAPBUFFERARBPROC glUnmapBufferARB = null;
+   static PFNGLACTIVETEXTUREARBPROC glActiveTextureARB = null;
+   static PFNGLMULTITEXCOORD2FARBPROC glMultiTexCoord2fARB = null;
+   static PFNGLCLIENTACTIVETEXTUREARBPROC glClientActiveTextureARB = null;
+   static PFNGLLOCKARRAYSEXTPROC glLockArraysEXT = null;
+   static PFNGLUNLOCKARRAYSEXTPROC glUnlockArraysEXT = null;
+   static PFNGLBLENDFUNCSEPARATEPROC glBlendFuncSeparate = null;
+
+   static PFNGLGENBUFFERSARBPROC glGenBuffersARB = null;
+   static PFNGLBINDBUFFERARBPROC glBindBufferARB = null;
+   static PFNGLBUFFERDATAARBPROC glBufferDataARB = null;
+   static PFNGLDELETEBUFFERSARBPROC glDeleteBuffersARB = null;
+   static PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = null;
+   static PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = null;
+   static PFNWGLCREATEPBUFFERARBPROC wglCreatePbufferARB = null;
+   static PFNWGLGETPBUFFERDCARBPROC wglGetPbufferDCARB = null;
+   static PFNWGLQUERYPBUFFERARBPROC wglQueryPbufferARB = null;
+   static PFNWGLDESTROYPBUFFERARBPROC wglDestroyPbufferARB = null;
+   static PFNWGLRELEASEPBUFFERDCARBPROC wglReleasePbufferDCARB = null;
+   static PFNWGLBINDTEXIMAGEARBPROC wglBindTexImageARB = null;
+   static PFNWGLRELEASETEXIMAGEARBPROC wglReleaseTexImageARB = null;
+
+   #define glBufferData glBufferDataARB
+
+   #ifdef WGL_WGLEXT_PROTOTYPES
+   extern BOOL WINAPI wglSwapIntervalEXT (int);
+   extern int WINAPI wglGetSwapIntervalEXT (void);
+   #endif /* WGL_WGLEXT_PROTOTYPES */
+   typedef BOOL (WINAPI * PFNWGLSWAPINTERVALEXTPROC) (int interval);
+   typedef int (WINAPI * PFNWGLGETSWAPINTERVALEXTPROC) (void);
+
+   static PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = NULL;
+
+#elif defined(__ANDROID__)
+
+   #define GL_FRAMEBUFFER           GL_FRAMEBUFFER_OES
+   #define GL_RENDERBUFFER          GL_RENDERBUFFER_OES
+   #define GL_COLOR_ATTACHMENT0     GL_COLOR_ATTACHMENT0_OES
+
+   #define GL_POLYGON_STIPPLE 0xFFFF
+   #define GL_LINE_STIPPLE 0xFFFF
+   #define GL_LINE 0xFFFF
+   #define GL_FILL 0xFFFF
+   #define GL_ALL_ATTRIB_BITS 0xFFFF
+   #define GL_LIGHT_MODEL_LOCAL_VIEWER 0xFFFF
+
+   #define GL_POLYGON      9
+   #define GL_QUADS        7
+
+   //#define GL_QUADS              0
+   #define GL_QUAD_STRIP         0
+   //#define GL_DOUBLE             0
+   //#define GL_UNSIGNED_INT       0
+   //#define GL_FILL               0
+   //#define GL_LINE               0
+   //#define GL_LINE_STIPPLE       0
+   #define GL_BGRA_EXT           0
+   #define GL_UNPACK_ROW_LENGTH  0
+   #define GL_UNPACK_SKIP_PIXELS 0
+   #define GL_UNPACK_SKIP_ROWS   0
+   #define GL_RGBA8              0
+   #define GL_PACK_ROW_LENGTH    0
+   #define GL_PACK_SKIP_ROWS     0
+   #define GL_PACK_SKIP_PIXELS   0
 
-#else
+#endif
 
-#if defined(__ANDROID__)
+#if defined(ECERE_NO3D) || defined(ECERE_VANILLA)
+public union Matrix
+{
+   double array[16];
+   double m[4][4];
+
+   void Identity()
+   {
+      FillBytesBy4(this, 0, sizeof(Matrix) >> 2);
+      m[0][0]=m[1][1]=m[2][2]=m[3][3]=1;
+   }
+
+   void Transpose(Matrix source)
+   {
+      int i,j;
+      for(i=0; i<4; i++)
+         for(j=0; j<4; j++)
+            m[j][i] = source.m[i][j];
+   }
+
+   void Multiply(Matrix a, Matrix b)
+   {
+      // We need a full matrix multiplication for the Projection matrix
+      m[0][0]=a.m[0][0]*b.m[0][0] + a.m[0][1]*b.m[1][0] + a.m[0][2]*b.m[2][0] + a.m[0][3]*b.m[3][0];
+      m[0][1]=a.m[0][0]*b.m[0][1] + a.m[0][1]*b.m[1][1] + a.m[0][2]*b.m[2][1] + a.m[0][3]*b.m[3][1];
+      m[0][2]=a.m[0][0]*b.m[0][2] + a.m[0][1]*b.m[1][2] + a.m[0][2]*b.m[2][2] + a.m[0][3]*b.m[3][2];
+      m[0][3]=a.m[0][0]*b.m[0][3] + a.m[0][1]*b.m[1][3] + a.m[0][2]*b.m[2][3] + a.m[0][3]*b.m[3][3];
+
+      m[1][0]=a.m[1][0]*b.m[0][0] + a.m[1][1]*b.m[1][0] + a.m[1][2]*b.m[2][0] + a.m[1][3]*b.m[3][0];
+      m[1][1]=a.m[1][0]*b.m[0][1] + a.m[1][1]*b.m[1][1] + a.m[1][2]*b.m[2][1] + a.m[1][3]*b.m[3][1];
+      m[1][2]=a.m[1][0]*b.m[0][2] + a.m[1][1]*b.m[1][2] + a.m[1][2]*b.m[2][2] + a.m[1][3]*b.m[3][2];
+      m[1][3]=a.m[1][0]*b.m[0][3] + a.m[1][1]*b.m[1][3] + a.m[1][2]*b.m[2][3] + a.m[1][3]*b.m[3][3];
+
+      m[2][0]=a.m[2][0]*b.m[0][0] + a.m[2][1]*b.m[1][0] + a.m[2][2]*b.m[2][0] + a.m[2][3]*b.m[3][0];
+      m[2][1]=a.m[2][0]*b.m[0][1] + a.m[2][1]*b.m[1][1] + a.m[2][2]*b.m[2][1] + a.m[2][3]*b.m[3][1];
+      m[2][2]=a.m[2][0]*b.m[0][2] + a.m[2][1]*b.m[1][2] + a.m[2][2]*b.m[2][2] + a.m[2][3]*b.m[3][2];
+      m[2][3]=a.m[2][0]*b.m[0][3] + a.m[2][1]*b.m[1][3] + a.m[2][2]*b.m[2][3] + a.m[2][3]*b.m[3][3];
+
+      m[3][0]=a.m[3][0]*b.m[0][0] + a.m[3][1]*b.m[1][0] + a.m[3][2]*b.m[2][0] + a.m[3][3]*b.m[3][0];
+      m[3][1]=a.m[3][0]*b.m[0][1] + a.m[3][1]*b.m[1][1] + a.m[3][2]*b.m[2][1] + a.m[3][3]*b.m[3][1];
+      m[3][2]=a.m[3][0]*b.m[0][2] + a.m[3][1]*b.m[1][2] + a.m[3][2]*b.m[2][2] + a.m[3][3]*b.m[3][2];
+      m[3][3]=a.m[3][0]*b.m[0][3] + a.m[3][1]*b.m[1][3] + a.m[3][2]*b.m[2][3] + a.m[3][3]*b.m[3][3];
+   }
+};
+#endif
 
 // Our own matrix stack
 static Matrix matrixStack[3][32];
 static int matrixIndex[3];
 static int curStack = 0;
 
-// OpenGL ES Porting Kit
-
-#define glBindFramebuffer        glBindFramebufferOES
-#define glBindRenderbuffer       glBindRenderbufferOES
-#define GL_FRAMEBUFFER           GL_FRAMEBUFFER_OES
-#define GL_RENDERBUFFER          GL_RENDERBUFFER_OES
-#define glFramebufferTexture2D   glFramebufferTexture2DOES
-#define GL_COLOR_ATTACHMENT0     GL_COLOR_ATTACHMENT0_OES
-#define glGenFramebuffers        glGenFramebuffersOES
-#define glGenRenderbuffers       glGenRenderbuffersOES
-#define glDeleteFramebuffers     glDeleteFramebuffersOES
-#define glDeleteRenderbuffers    glDeleteRenderbuffersOES
-
-#define GL_POLYGON_STIPPLE 0xFFFF
-#define GL_LINE_STIPPLE 0xFFFF
-#define GL_LINE 0xFFFF
-#define GL_FILL 0xFFFF
-#define GL_ALL_ATTRIB_BITS 0xFFFF
-#define GL_LIGHT_MODEL_LOCAL_VIEWER 0xFFFF
-#define glDrawElementsi(type, count, start)  glDrawElements(type, count, GL_UNSIGNED_SHORT, start)
-
-#define GL_UNSIGNED_INT                         0x1405
-#define GL_DOUBLE                               0x140A
-
-#define GL_POLYGON      9
-#define GL_QUADS        7
-
-#define glBufferDatai         glesBufferDatai
-#define glBufferDatad         glesBufferDatad
-#define glVertexPointeri      glesVertexPointeri
-#define glVertexPointerd      glesVertexPointerd
-
-#define glRecti               glesRecti
-#define glBegin               glesBegin
-#define glTexCoord2i          glesTexCoord2i
-#define glVertex2i            glesVertex2i
-#define glTexCoord2d          glesTexCoord2d
-#define glVertex2d            glesVertex2d
-#define glTexCoord2f          glesTexCoord2f
-#define glVertex2f            glesVertex2f
-#define glEnd                 glesEnd
-#define glColor3f             glesColor3f
-#define glColor4ub            glesColor4ub
-#define glColor4fv            glesColor4fv
-#define glLineStipple         glesLineStipple
-#define glNormal3fv           glesNormal3fv
-#define glTexCoord2fv         glesTexCoord2fv
-#define glColorMaterial       glesColorMaterial
-
-#define glLoadMatrixd         glesLoadMatrixd
-#define glMultMatrixd         glesMultMatrixd
-#define glFrustum             glesFrustum
-#define glOrtho               glesOrtho
-#define glScaled              glesScaled
-#define glTranslated          glesTranslated
-#define glRotated             glesRotated
-#define glVertex3d            glesVertex3d
-#define glVertex3f            glesVertex3f
-#define glVertex3fv           glesVertex3fv
-#define glLightModeli         glesLightModeli
-
-#define APIENTRY
-//#define GL_QUADS              0
-#define GL_QUAD_STRIP         0
-//#define GL_DOUBLE             0
-//#define GL_UNSIGNED_INT       0
-//#define GL_FILL               0
-//#define GL_LINE               0
-//#define GL_LINE_STIPPLE       0
-#define GL_BGRA_EXT           0
-#define GL_UNPACK_ROW_LENGTH  0
-#define GL_UNPACK_SKIP_PIXELS 0
-#define GL_UNPACK_SKIP_ROWS   0
-#define GL_RGBA8              0
-#define GL_PACK_ROW_LENGTH    0
-#define GL_PACK_SKIP_ROWS     0
-#define GL_PACK_SKIP_PIXELS   0
-
-static EGLDisplay eglDisplay;
-static EGLSurface eglSurface;
-static EGLContext eglContext;
-static int eglWidth, eglHeight;
-
-static bool egl_init_display(ANativeWindow* window)
-{
-   const EGLint attribs[] =
-   {
-      EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
-      EGL_BLUE_SIZE, 8,
-      EGL_GREEN_SIZE, 8,
-      EGL_RED_SIZE, 8,
-      EGL_DEPTH_SIZE, 24,
-      /*EGL_SAMPLE_BUFFERS, 1,
-      EGL_SAMPLES, 0, //2,*/
-      EGL_NONE
-   };
-   EGLint w, h, dummy, format;
-   EGLint numConfigs;
-   EGLConfig config;
-   EGLSurface surface;
-   EGLContext context;
+#if defined(_GLES)
 
-   EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
-   eglInitialize(display, 0, 0);
-   eglChooseConfig(display, attribs, &config, 1, &numConfigs);
-   eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
+   // OpenGL ES Porting Kit
+#if defined(__ANDROID__)
+   #define glBindFramebuffer        glBindFramebufferOES
+   #define glBindRenderbuffer       glBindRenderbufferOES
+   #define glFramebufferTexture2D   glFramebufferTexture2DOES
+   #define glGenFramebuffers        glGenFramebuffersOES
+   #define glGenRenderbuffers       glGenRenderbuffersOES
+   #define glDeleteFramebuffers     glDeleteFramebuffersOES
+   #define glDeleteRenderbuffers    glDeleteRenderbuffersOES
+
+   #define GL_INT                                  0x1404
+   #define GL_UNSIGNED_INT                         0x1405
+   #define GL_DOUBLE                               0x140A
+   #define APIENTRY
+#endif
 
-   surface = eglCreateWindowSurface(display, config, window, null);
-   context = eglCreateContext(display, config, null, null);
+   #define glDrawElementsi(type, count, start)  glDrawElements(type, count, GL_UNSIGNED_SHORT, start)
+
+   #define glBufferDatai         glesBufferDatai
+   #define glBufferDatad         glesBufferDatad
+   #define glVertexPointeri      glesVertexPointeri
+   #define glVertexPointerd      glesVertexPointerd
+
+   #define glRecti               glesRecti
+   #define glBegin               glesBegin
+   #define glTexCoord2i          glesTexCoord2i
+   #define glVertex2i            glesVertex2i
+   #define glTexCoord2d          glesTexCoord2d
+   #define glVertex2d            glesVertex2d
+   #define glTexCoord2f          glesTexCoord2f
+   #define glVertex2f            glesVertex2f
+   #define glEnd                 glesEnd
+   #define glColor3f             glesColor3f
+   #define glColor4ub            glesColor4ub
+   #define glColor4fv            glesColor4fv
+   #define glLineStipple         glesLineStipple
+   #define glNormal3fv           glesNormal3fv
+   #define glTexCoord2fv         glesTexCoord2fv
+   #define glColorMaterial       glesColorMaterial
+
+   #define glLoadMatrixd         glesLoadMatrixd
+   #define glMultMatrixd         glesMultMatrixd
+   #define glFrustum             glesFrustum
+   #define glOrtho               glesOrtho
+   #define glScaled              glesScaled
+   #define glTranslated          glesTranslated
+   #define glRotated             glesRotated
+   #define glVertex3d            glesVertex3d
+   #define glVertex3dv           glesVertex3dv
+   #define glVertex3f            glesVertex3f
+   #define glVertex3fv           glesVertex3fv
+   #define glLightModeli         glesLightModeli
 
-   if(!eglMakeCurrent(display, surface, surface, context))
-      return false;
+#else
 
-   eglQuerySurface(display, surface, EGL_WIDTH, &w);
-   eglQuerySurface(display, surface, EGL_HEIGHT, &h);
+#define glVertexPointerd(nc, s, p, nv)       glVertexPointer(nc, GL_DOUBLE, s, p)
+#define glDrawElementsi(type, count, start)  glDrawElements(type, count, GL_UNSIGNED_INT, start)
 
-   eglDisplay = display;
-   eglContext = context;
-   eglSurface = surface;
-   eglWidth = w;
-   eglHeight = h;
+#endif
 
-   glEnableClientState(GL_VERTEX_ARRAY);
-   /*
-   // Initialize GL state.
-   glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
-   glEnable(GL_CULL_FACE);
-   glShadeModel(GL_SMOOTH);
-   glDisable(GL_DEPTH_TEST);
-   */
-   glDisable(GL_CULL_FACE);
-   glDisable(GL_DEPTH_TEST);
-
-   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-   glEnable(GL_BLEND);
-
-   matrixStack[0][0].Identity();
-   matrixStack[1][0].Identity();
-   matrixStack[2][0].Identity();
-
-   glesMatrixMode(GL_MODELVIEW);
-   glScaled(1.0, 1.0, -1.0);
-   glesMatrixMode(GL_PROJECTION);
-   glShadeModel(GL_FLAT);
-
-   glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
-   glFogi(GL_FOG_MODE, GL_EXP);
-   glFogf(GL_FOG_DENSITY, 0);
-   glEnable(GL_NORMALIZE);
-   glDepthFunc(GL_LESS);
-   glClearDepth(1.0);
-   glDisable(GL_MULTISAMPLE_ARB);
-
-   glViewport(0,0,w,h);
-   glesLoadIdentity();
-   glOrtho(0,w,h,0,0.0,1.0);
-
-   currentVertexBuffer = 0;
-   return true;
-}
+#if defined(__ANDROID__)
+   static EGLDisplay eglDisplay;
+   static EGLSurface eglSurface;
+   static EGLContext eglContext;
+   static int eglWidth, eglHeight;
 
-static void egl_term_display()
-{
-   if(stippleTexture)
+   static bool egl_init_display(ANativeWindow* window)
    {
-      glDeleteTextures(1, (int *)&stippleTexture);
-      stippleTexture = 0;
+      const EGLint attribs[] =
+      {
+         EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
+         EGL_BLUE_SIZE, 8,
+         EGL_GREEN_SIZE, 8,
+         EGL_RED_SIZE, 8,
+         EGL_DEPTH_SIZE, 16, //24,
+         /*EGL_SAMPLE_BUFFERS, 1,
+         EGL_SAMPLES, 0, //2,*/
+         EGL_NONE
+      };
+      EGLint w, h, format;
+      EGLint numConfigs;
+      EGLConfig config;
+      EGLSurface surface;
+      EGLContext context;
+
+      EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
+      eglInitialize(display, 0, 0);
+      eglChooseConfig(display, attribs, &config, 1, &numConfigs);
+      eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
+
+      surface = eglCreateWindowSurface(display, config, window, null);
+      context = eglCreateContext(display, config, null, null);
+
+      if(!eglMakeCurrent(display, surface, surface, context))
+         return false;
+
+      eglQuerySurface(display, surface, EGL_WIDTH, &w);
+      eglQuerySurface(display, surface, EGL_HEIGHT, &h);
+
+      eglDisplay = display;
+      eglContext = context;
+      eglSurface = surface;
+      eglWidth = w;
+      eglHeight = h;
+
+      glEnableClientState(GL_VERTEX_ARRAY);
+      /*
+      // Initialize GL state.
+      glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
+      glEnable(GL_CULL_FACE);
+      glShadeModel(GL_SMOOTH);
+      glDisable(GL_DEPTH_TEST);
+      */
+      glDisable(GL_CULL_FACE);
+      glDisable(GL_DEPTH_TEST);
+
+      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+      glEnable(GL_BLEND);
+
+      matrixStack[0][0].Identity();
+      matrixStack[1][0].Identity();
+      matrixStack[2][0].Identity();
+
+      glesMatrixMode(GL_MODELVIEW);
+      glScaled(1.0, 1.0, -1.0);
+      glesMatrixMode(GL_PROJECTION);
+      glShadeModel(GL_FLAT);
+
+      glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
+      glFogi(GL_FOG_MODE, GL_EXP);
+      glFogf(GL_FOG_DENSITY, 0);
+      glEnable(GL_NORMALIZE);
+      glDepthFunc(GL_LESS);
+      glClearDepth(1.0);
+      glDisable(GL_MULTISAMPLE_ARB);
+
+      glViewport(0,0,w,h);
+      glesLoadIdentity();
+      glOrtho(0,w,h,0,0.0,1.0);
+
+      currentVertexBuffer = 0;
+      return true;
    }
-   if(eglDisplay != EGL_NO_DISPLAY)
+
+   static void egl_term_display()
    {
-      eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
-      if(eglContext != EGL_NO_CONTEXT)
-         eglDestroyContext(eglDisplay, eglContext);
-      if(eglSurface != EGL_NO_SURFACE)
-         eglDestroySurface(eglDisplay, eglSurface);
-      eglTerminate(eglDisplay);
+      if(stippleTexture)
+      {
+         glDeleteTextures(1, &stippleTexture);
+         stippleTexture = 0;
+      }
+      if(eglDisplay != EGL_NO_DISPLAY)
+      {
+         eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
+         if(eglContext != EGL_NO_CONTEXT)
+            eglDestroyContext(eglDisplay, eglContext);
+         if(eglSurface != EGL_NO_SURFACE)
+            eglDestroySurface(eglDisplay, eglSurface);
+         eglTerminate(eglDisplay);
+      }
+      eglDisplay = EGL_NO_DISPLAY;
+      eglContext = EGL_NO_CONTEXT;
+      eglSurface = EGL_NO_SURFACE;
    }
-   eglDisplay = EGL_NO_DISPLAY;
-   eglContext = EGL_NO_CONTEXT;
-   eglSurface = EGL_NO_SURFACE;
-}
+
+#endif
 
 // OpenGL Immediate Mode Porting Kit
 static int beginCount;
@@ -554,11 +660,14 @@ static int vertexCount;
 static int normalCount;
 static float *vertexPointer;
 static float *normalPointer;
-static GLenum beginMode;
-static unsigned int beginBufferSize, normalBufferSize;
+static GLenum beginMode = -1;
+static uint beginBufferSize, normalBufferSize;
 static int numVertexCoords = 2;
+static bool vertexColorValues = false;
+static int vertexStride = 4;
+static int vertexOffset = 2;
 
-void glesRecti(int a, int b, int c, int d)
+public void glesRecti(int a, int b, int c, int d)
 {
    glBegin(GL_QUADS);
    glVertex2i(a, b);
@@ -568,83 +677,96 @@ void glesRecti(int a, int b, int c, int d)
    glEnd();
 }
 
-void glesBegin(GLenum mode)
+public void glesBegin(GLenum mode)
 {
    beginMode = mode;
    beginCount = 0;
    vertexCount = 0;
+   vertexColorValues = false;
+   vertexOffset = 2;
+   vertexStride = 4;
+   numVertexCoords = 2;
+
    if(!vertexPointer)
    {
       normalBufferSize = beginBufferSize = 1024;  // default number of vertices
-      vertexPointer = new float[beginBufferSize * 5];
+      vertexPointer = new float[beginBufferSize * vertexStride];
       normalPointer = new float[normalBufferSize * 3];
    }
 }
 
-void glesTexCoord2f(float x, float y)
+public void glesTexCoord2f(float x, float y)
 {
    int count = vertexCount;
 
    if(vertexCount + numVertexCoords > beginBufferSize)
    {
       beginBufferSize = beginBufferSize + beginBufferSize/2;
-      vertexPointer = renew vertexPointer float[beginBufferSize * 5];
+      vertexPointer = renew vertexPointer float[beginBufferSize * vertexStride];
    }
 
-   vertexPointer[count*(2+numVertexCoords)  ] = x;
-   vertexPointer[count*(2+numVertexCoords)+1] = y;
+   vertexPointer[count*vertexStride  ] = x;
+   vertexPointer[count*vertexStride+1] = y;
    count++;
 
    if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
    {
-      vertexPointer[count*(2+numVertexCoords)  ] = vertexPointer[(count-4)*(2+numVertexCoords)];
-      vertexPointer[count*(2+numVertexCoords)+1] = vertexPointer[(count-4)*(2+numVertexCoords)+1];
+      vertexPointer[count*vertexStride  ] = vertexPointer[(count-4)*vertexStride];
+      vertexPointer[count*vertexStride+1] = vertexPointer[(count-4)*vertexStride+1];
       count++;
-      vertexPointer[count*(2+numVertexCoords)  ] = vertexPointer[(count-3)*(2+numVertexCoords)];
-      vertexPointer[count*(2+numVertexCoords)+1] = vertexPointer[(count-3)*(2+numVertexCoords)+1];
+      vertexPointer[count*vertexStride  ] = vertexPointer[(count-3)*vertexStride];
+      vertexPointer[count*vertexStride+1] = vertexPointer[(count-3)*vertexStride+1];
       count++;
    }
 }
-void glesTexCoord2i(int x, int y)       { glesTexCoord2f((float)x, (float)y); }
-void glesTexCoord2d(double x, double y) { glesTexCoord2f((float)x, (float)y); }
-void glesTexCoord2fv(float * a)         { glesTexCoord2f(a[0], a[1]); }
+public void glesTexCoord2i(int x, int y)       { glesTexCoord2f((float)x, (float)y); }
+public void glesTexCoord2d(double x, double y) { glesTexCoord2f((float)x, (float)y); }
+public void glesTexCoord2fv(float * a)         { glesTexCoord2f(a[0], a[1]); }
 
-void glesVertex2f(float x, float y)
+public void glesVertex2f(float x, float y)
 {
    numVertexCoords = 2;
+   vertexStride = vertexOffset + numVertexCoords;
+
    if(vertexCount + 4 > beginBufferSize)
    {
       beginBufferSize = beginBufferSize + beginBufferSize/2;
-      vertexPointer = renew vertexPointer float[beginBufferSize * 5];
+      vertexPointer = renew vertexPointer float[beginBufferSize * vertexStride];
    }
 
-   vertexPointer[vertexCount*4+2] = x;
-   vertexPointer[vertexCount*4+3] = y;
+   vertexPointer[vertexCount*vertexStride+vertexOffset] = x;
+   vertexPointer[vertexCount*vertexStride+vertexOffset + 1] = y;
    vertexCount++;
 
    if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
    {
-      vertexPointer[vertexCount*4+2] = vertexPointer[(vertexCount-4)*4+2];
-      vertexPointer[vertexCount*4+3] = vertexPointer[(vertexCount-4)*4+3];
+      vertexPointer[vertexCount*vertexStride+vertexOffset] = vertexPointer[(vertexCount-4)*vertexStride+vertexOffset];
+      vertexPointer[vertexCount*vertexStride+vertexOffset + 1] = vertexPointer[(vertexCount-4)*vertexStride+vertexOffset + 1];
       vertexCount++;
-      vertexPointer[vertexCount*4+2] = vertexPointer[(vertexCount-3)*4+2];
-      vertexPointer[vertexCount*4+3] = vertexPointer[(vertexCount-3)*4+3];
+      vertexPointer[vertexCount*vertexStride+vertexOffset] = vertexPointer[(vertexCount-3)*vertexStride+vertexOffset];
+      vertexPointer[vertexCount*vertexStride+vertexOffset + 1] = vertexPointer[(vertexCount-3)*vertexStride+vertexOffset + 1];
       vertexCount++;
    }
    beginCount++;
 }
-void glesVertex2i(int x, int y)         { glesVertex2f((float)x, (float)y); }
-void glesVertex2d(double x, double y)   { glesVertex2f((float)x, (float)y); }
+public void glesVertex2i(int x, int y)         { glesVertex2f((float)x, (float)y); }
+public void glesVertex2d(double x, double y)   { glesVertex2f((float)x, (float)y); }
 
-void glesEnd(void)
+public void glesEnd(void)
 {
    int mode = beginMode;
    if(mode == GL_QUADS)        mode = GL_TRIANGLES;
    else if(mode == GL_POLYGON) mode = GL_TRIANGLE_FAN;
+
    GLSelectVBO(0);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
-   glTexCoordPointer(numVertexCoords, GL_FLOAT, (numVertexCoords+2)*sizeof(float),vertexPointer);
-   glVertexPointer  (numVertexCoords, GL_FLOAT, (numVertexCoords+2)*sizeof(float),vertexPointer+2);
+   glTexCoordPointer(2, GL_FLOAT,  vertexStride * sizeof(float), vertexPointer);
+   if(vertexColorValues)
+   {
+      glEnableClientState(GL_COLOR_ARRAY);
+      glColorPointer(4, GL_FLOAT, vertexStride * sizeof(float), vertexPointer + 2);
+   }
+   glVertexPointer  (numVertexCoords, GL_FLOAT, (vertexStride)*sizeof(float),vertexPointer+vertexOffset);
    if(normalCount && normalCount == vertexCount)
    {
       glEnableClientState(GL_NORMAL_ARRAY);
@@ -654,8 +776,13 @@ void glesEnd(void)
    glDrawArrays(mode, 0, vertexCount);
    if(normalCount)
       glDisableClientState(GL_NORMAL_ARRAY);
+   if(vertexColorValues)
+      glDisableClientState(GL_COLOR_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    normalCount = 0;
+   vertexColorValues = false;
+   numVertexCoords = 2;
+   beginMode = -1;
 }
 
 // Vertex Pointer
@@ -668,7 +795,7 @@ static unsigned int shortVPSize = 0, floatVPSize = 0;
 static unsigned short *shortBDBuffer = null;
 static unsigned int shortBDSize = 0/*, floatVPSize = 0*/;
 
-void glesVertexPointeri(int numCoords, int stride, int *pointer, int numVertices)
+public void glesVertexPointeri(int numCoords, int stride, int *pointer, int numVertices)
 {
    if(pointer)
    {
@@ -686,7 +813,7 @@ void glesVertexPointeri(int numCoords, int stride, int *pointer, int numVertices
       glVertexPointer(numCoords, GL_SHORT, stride, 0);
 }
 
-void glesVertexPointerd(int numCoords, int stride, double *pointer, int numVertices)
+public void glesVertexPointerd(int numCoords, int stride, double *pointer, int numVertices)
 {
    if(pointer)
    {
@@ -704,32 +831,72 @@ void glesVertexPointerd(int numCoords, int stride, double *pointer, int numVerti
       glVertexPointer(numCoords, GL_FLOAT, stride, 0);
 }
 
-void glesTexReuseIntVP(int numCoords)
+public void glesTexReuseIntVP(int numCoords)
 {
    glTexCoordPointer(numCoords, GL_SHORT, 0, floatVPBuffer);
 }
 
-void glesTexReuseDoubleVP(int numCoords)
+public void glesTexReuseDoubleVP(int numCoords)
 {
    glTexCoordPointer(numCoords, GL_FLOAT, 0, floatVPBuffer);
 }
 
-void glesColor3f( float r, float g, float b )
+public void glesColor4f(float r, float g, float b, float a)
 {
-   glColor4f(r, g, b, 1.0f);
+   if(beginMode != (GLenum)-1)
+   {
+      int count = vertexCount;
+
+      vertexColorValues = true;
+      vertexOffset = 6;
+      vertexStride = vertexOffset + numVertexCoords;
+
+      if(vertexCount + vertexStride > beginBufferSize)
+      {
+         beginBufferSize = beginBufferSize + beginBufferSize/2;
+         vertexPointer = renew vertexPointer float[beginBufferSize * vertexStride];
+      }
+
+      vertexPointer[count*vertexStride + 2] = r;
+      vertexPointer[count*vertexStride + 3] = g;
+      vertexPointer[count*vertexStride + 4] = b;
+      vertexPointer[count*vertexStride + 5] = a;
+      count++;
+
+      if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
+      {
+         vertexPointer[count*vertexStride + 2] = vertexPointer[(count-4) * vertexStride + 2];
+         vertexPointer[count*vertexStride + 3] = vertexPointer[(count-4) * vertexStride + 3];
+         vertexPointer[count*vertexStride + 4] = vertexPointer[(count-4) * vertexStride + 4];
+         vertexPointer[count*vertexStride + 5] = vertexPointer[(count-4) * vertexStride + 5];
+         count++;
+         vertexPointer[count*vertexStride + 2] = vertexPointer[(count-3) * vertexStride + 2];
+         vertexPointer[count*vertexStride + 3] = vertexPointer[(count-3) * vertexStride + 3];
+         vertexPointer[count*vertexStride + 4] = vertexPointer[(count-3) * vertexStride + 4];
+         vertexPointer[count*vertexStride + 5] = vertexPointer[(count-3) * vertexStride + 5];
+         count++;
+      }
+   }
+   else
+      glColor4f(r, g, b, a);
+}
+
+public void glesColor3f( float r, float g, float b )
+{
+   glesColor4f(r, g, b, 1.0f);
 }
 
-void glesColor4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
+public void glesColor4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
 {
-   glColor4f(r/255.0f, g/255.0f, b/255.0f, a/255.0f);
+   glesColor4f(r/255.0f, g/255.0f, b/255.0f, a/255.0f);
 }
 
-void glesColor4fv(float * a)
+public void glesColor4fv(float * a)
 {
-   glColor4f(a[0], a[1], a[2], a[3]);
+   glesColor4f(a[0], a[1], a[2], a[3]);
 }
 
-void glesBufferDatad(int target, int size, void * data, int usage)
+public void glesBufferDatad(int target, int size, void * data, int usage)
 {
    int numElems = size/sizeof(double);
    double * dblPtr = (double *)data;
@@ -745,7 +912,7 @@ void glesBufferDatad(int target, int size, void * data, int usage)
    glBufferData(target, numElems*sizeof(float), floatVPBuffer, usage);
 }
 
-void glesBufferDatai(int target, int size, void * data, int usage)
+public void glesBufferDatai(int target, int size, void * data, int usage)
 {
    int numElems = size/sizeof(unsigned int);
    unsigned int * pointer = (unsigned int *)data;
@@ -776,13 +943,13 @@ static void LoadCurMatrix()
    glLoadMatrixf(m);
 }
 
-void glesLoadIdentity()
+public void glesLoadIdentity()
 {
    matrixStack[curStack][matrixIndex[curStack]].Identity();
    LoadCurMatrix();
 }
 
-void glesPushMatrix()
+public void glesPushMatrix()
 {
    if(matrixIndex[curStack] + 1 < sizeof(matrixStack[0]) / sizeof(Matrix))
    {
@@ -791,7 +958,7 @@ void glesPushMatrix()
    }
 }
 
-void glesPopMatrix()
+public void glesPopMatrix()
 {
    if(matrixIndex[curStack] > 0)
    {
@@ -800,27 +967,28 @@ void glesPopMatrix()
    }
 }
 
-void glesLoadMatrixd(double * i)
+public void glesLoadMatrixd(double * i)
 {
    memcpy(matrixStack[curStack][matrixIndex[curStack]].array, i, sizeof(Matrix));
    LoadCurMatrix();
 }
 
-void glesOrtho( double l, double r, double b, double t, double n, double f )
+public void glesOrtho( double l, double r, double b, double t, double n, double f )
 {
-   Matrix m =
+   Matrix m
    { {
       (2 / (r - l)), 0, 0, 0,
       0, (2 / (t - b)), 0, 0,
       0, 0, (-2 / (f - n)), 0,
       (-(r + l) / (r - l)), (-(t + b) / (t - b)), (-(f + n) / (f - n)), 1
-   } }, res;
+   } };
+   Matrix res;
    res.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
    matrixStack[curStack][matrixIndex[curStack]] = res;
    LoadCurMatrix();
 }
 
-void glesFrustum( double l, double r, double b, double t, double n, double f )
+public void glesFrustum( double l, double r, double b, double t, double n, double f )
 {
    nearPlane = n;
    n = 1;
@@ -834,29 +1002,33 @@ void glesFrustum( double l, double r, double b, double t, double n, double f )
       double B = ((t + b) / (t - b));
       double C = (-(f + n) / (f - n));
       double D = (-2*f*n/(f-n));
-      Matrix m =
+      Matrix m
       { {
          (2.0*n / (r - l)), 0, 0, 0,
          0, (2.0*n / (t - b)), 0, 0,
          A, B,             C,-1,
          0, 0,             D, 0
-      } }, res;
+      } };
+      Matrix res;
       res.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
       matrixStack[curStack][matrixIndex[curStack]] = res;
       LoadCurMatrix();
    }
 }
 
-void glesRotated( double a, double b, double c, double d )
+#if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
+public void glesRotated( double a, double b, double c, double d )
 {
-   Matrix m;
    Quaternion q;
-   q.RotationAxis({(float)b,(float)-c,(float)d}, a );
+   Matrix m, r;
+
+   q.RotationAxis({(float)b,(float)c,(float)-d}, a );
    m.RotationQuaternion(q);
-   matrixStack[curStack][matrixIndex[curStack]].Rotate(q);
+   r.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
+   matrixStack[curStack][matrixIndex[curStack]] = r;
    LoadCurMatrix();
 }
-void glesScaled( double a, double b, double c )
+public void glesScaled( double a, double b, double c )
 {
    Matrix m, r;
 
@@ -867,7 +1039,7 @@ void glesScaled( double a, double b, double c )
    LoadCurMatrix();
 }
 
-void glesTranslated( double a, double b, double c )
+public void glesTranslated( double a, double b, double c )
 {
    Matrix m, r;
 
@@ -878,25 +1050,30 @@ void glesTranslated( double a, double b, double c )
    LoadCurMatrix();
 }
 
-void glesMultMatrixd( double * i )
+public void glesMultMatrixd( double * i )
 {
    Matrix r;
    r.Multiply((Matrix *)i, matrixStack[curStack][matrixIndex[curStack]]);
    matrixStack[curStack][matrixIndex[curStack]] = r;
    LoadCurMatrix();
 }
+#endif
 
-void glesMatrixMode(int mode)
+public void glesMatrixMode(int mode)
 {
-   curStack = mode == GL_MODELVIEW ? 0 : mode == GL_PROJECTION ? 1 : 2;
+   curStack = (mode == GL_MODELVIEW) ? 0 : (mode == GL_PROJECTION) ? 1 : 2;
    glMatrixMode(mode);
 }
 
+#if defined(_GLES)
+
 #define glPushMatrix          glesPushMatrix
 #define glPopMatrix           glesPopMatrix
 #define glLoadIdentity        glesLoadIdentity
 #define glMatrixMode          glesMatrixMode
 
+#endif
+
 /* Using the built-in matrix stack
 void glesLoadMatrixd( double * i )
 {
@@ -956,38 +1133,41 @@ void glesMultMatrixd( double * i )
 */
 
 // Need to do these...
-void glesVertex3f( float x, float y, float z )
+public void glesVertex3f( float x, float y, float z )
 {
    numVertexCoords = 3;
-   if(vertexCount + 4 > beginBufferSize)
+   vertexStride = vertexOffset + numVertexCoords;
+
+   if(vertexCount + vertexStride > beginBufferSize)
    {
       beginBufferSize = beginBufferSize + beginBufferSize/2;
-      vertexPointer = renew vertexPointer float[beginBufferSize * 5];
+      vertexPointer = renew vertexPointer float[beginBufferSize * vertexStride];
    }
 
-   vertexPointer[vertexCount*5+2] = x;
-   vertexPointer[vertexCount*5+3] = y;
-   vertexPointer[vertexCount*5+4] = z;
+   vertexPointer[vertexCount*vertexStride+vertexOffset] = x;
+   vertexPointer[vertexCount*vertexStride+vertexOffset+1] = y;
+   vertexPointer[vertexCount*vertexStride+vertexOffset+2] = z;
    vertexCount++;
 
    if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
    {
-      vertexPointer[vertexCount*5+2] = vertexPointer[(vertexCount-4)*5+2];
-      vertexPointer[vertexCount*5+3] = vertexPointer[(vertexCount-4)*5+3];
-      vertexPointer[vertexCount*5+4] = vertexPointer[(vertexCount-4)*5+4];
+      vertexPointer[vertexCount*vertexStride+vertexOffset] = vertexPointer[(vertexCount-4)*vertexStride+vertexOffset];
+      vertexPointer[vertexCount*vertexStride+vertexOffset+1] = vertexPointer[(vertexCount-4)*vertexStride+vertexOffset+1];
+      vertexPointer[vertexCount*vertexStride+vertexOffset+2] = vertexPointer[(vertexCount-4)*vertexStride+vertexOffset+2];
       vertexCount++;
-      vertexPointer[vertexCount*5+2] = vertexPointer[(vertexCount-3)*5+2];
-      vertexPointer[vertexCount*5+3] = vertexPointer[(vertexCount-3)*5+3];
-      vertexPointer[vertexCount*5+4] = vertexPointer[(vertexCount-3)*5+4];
+      vertexPointer[vertexCount*vertexStride+vertexOffset] = vertexPointer[(vertexCount-3)*vertexStride+vertexOffset];
+      vertexPointer[vertexCount*vertexStride+vertexOffset+1] = vertexPointer[(vertexCount-3)*vertexStride+vertexOffset+1];
+      vertexPointer[vertexCount*vertexStride+vertexOffset+2] = vertexPointer[(vertexCount-3)*vertexStride+vertexOffset+2];
       vertexCount++;
    }
    beginCount++;
 }
 
-void glesVertex3d( double x, double y, double z )  { glesVertex3f((float)x, (float)y, (float)z); }
-void glesVertex3fv( float* coords )                { glesVertex3f(coords[0], coords[1], coords[2]); }
+public void glesVertex3d( double x, double y, double z )  { glesVertex3f((float)x, (float)y, (float)z); }
+public void glesVertex3fv( float* coords )                { glesVertex3f(coords[0], coords[1], coords[2]); }
+public void glesVertex3dv( double* coords )               { glesVertex3f((float)coords[0], (float)coords[1], (float)coords[2]); }
 
-void glesNormal3f(float x, float y, float z)
+public void glesNormal3f(float x, float y, float z)
 {
    normalCount = vertexCount;
    if(vertexCount + 4 > normalBufferSize)
@@ -1013,17 +1193,18 @@ void glesNormal3f(float x, float y, float z)
       normalCount++;
    }
 }
-void glesNormal3fd(double x, double y, double z)         { glesNormal3f((float)x, (float)y, (float)z); }
-void glesNormal3fv(float * coords)                       { glesNormal3f(coords[0], coords[1], coords[2]); }
+public void glesNormal3fd(double x, double y, double z)         { glesNormal3f((float)x, (float)y, (float)z); }
+public void glesNormal3fv(float * coords)                       { glesNormal3f(coords[0], coords[1], coords[2]); }
 
-void glesColorMaterial(int a, int b)
+public void glesColorMaterial(int a, int b)
 {
    PrintLn("glColorMaterial stub");
 }
 
-void glesTerminate()
+public void glesTerminate()
 {
    delete vertexPointer;
+   delete normalPointer;
    beginBufferSize = 0;
 
    delete floatVPBuffer;
@@ -1036,13 +1217,14 @@ void glesTerminate()
    shortBDSize = 0;
 }
 
-static int stippleTexture;
+static GLuint stippleTexture;
+#if defined(_GLES) || defined(EM_MODE)
 static bool stippleEnabled;
+#endif
 
-void glesLineStipple( int i, unsigned short j )
+public void glesLineStipple( int i, unsigned short j )
 {
    uint texture[1*16];
-   int c;
    int x;
    for(x = 0; x < 16; x++)
    {
@@ -1066,12 +1248,15 @@ void glesLineStipple( int i, unsigned short j )
    glMatrixMode(GL_PROJECTION);
 }
 
-void glesLightModeli( unsigned int pname, int param )
+public void glesLightModeli( unsigned int pname, int param )
 {
+#if !defined(EM_MODE)
    if(pname == GL_LIGHT_MODEL_TWO_SIDE)
       glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, param);
+#endif
 }
 
+#ifdef __ANDROID__
 void glClearDepth( double depth ) { glClearDepthf((float)depth); }
 void glFogi( unsigned int pname, int param ) { }
 void glPolygonMode( unsigned int i, unsigned int j ) { }
@@ -1093,34 +1278,15 @@ void glRasterPos2d(double a, double b) { }
 void glPixelZoom(float a, float b) { }
 void glDrawPixels(int a, int b, int c, int d, void * e) { }
 
-#else
-
-/* Non OpenGL ES friendly stuff
-#undef GL_UNSIGNED_INT
-#undef GL_DOUBLE
-#undef GL_INT
-//#undef GL_POLYGON
-//#undef GL_QUADS
-#undef GL_QUAD_STRIP
-#undef GL_POLYGON_STIPPLE
-#undef GL_LINE_STIPPLE
-#undef GL_LINE
-#undef GL_FILL
-#undef GL_ALL_ATTRIB_BITS
-#undef GL_LIGHT_MODEL_LOCAL_VIEWER
-*/
-
 #endif
 
-#if !defined(__APPLE__)
+#if !defined(__APPLE__) && !defined(__WIN32__)
 void (APIENTRY * glBindBufferARB) (GLenum target, GLuint buffer);
 void (APIENTRY * glGenBuffersARB) (GLsizei n, GLuint *buffers);
 void (APIENTRY * glDeleteBuffersARB) (GLsizei n, const GLuint *buffers);
 void (APIENTRY * glBufferDataARB) (GLenum target, int size, const GLvoid *data, GLenum usage);
 #endif
 
-#endif
-
 static int currentVertexBuffer;
 
 bool GLSelectVBO(uint vbo)
@@ -1136,50 +1302,101 @@ bool GLSelectVBO(uint vbo)
 
 void GLGenBuffers(int count, uint * buffer)
 {
-#ifdef __ANDROID__
+#if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
    glGenBuffers(count, buffer);
 #else
+#if defined(__WIN32__)
    if(glGenBuffersARB)
+#endif
       glGenBuffersARB(count, buffer);
 #endif
 }
 
-void GLDeleteBuffers(int count, uint * buffer)
+void GLDeleteBuffers(int count, GLuint * buffer)
 {
-#ifdef __ANDROID__
+#if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
    glDeleteBuffers(count, buffer);
 #else
+#if defined(__WIN32__)
    if(glDeleteBuffersARB)
+#endif
       glDeleteBuffersARB(count, buffer);
 #endif
 }
 
 void GLBindBuffer(int target, uint buffer)
 {
-#ifdef __ANDROID__
+#if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
    glBindBuffer(target, buffer);
 #else
+#if defined(__WIN32__)
    if(glBindBufferARB)
+#endif
       glBindBufferARB(target, buffer);
 #endif
+   currentVertexBuffer = buffer;
 }
 
-void GLBufferData(int type, GLenum target, int size, const GLvoid *data, GLenum usage)
+public void GLVertexPointer(int numCoords, int glType, int stride, void *ptr, int numVertices)
 {
-#ifdef __ANDROID__
+#ifdef _GLES
+   if(glType == GL_DOUBLE)
+      glesVertexPointerd(numCoords, stride, ptr, numVertices);
+   else if(glType == GL_INT)
+      glesVertexPointeri(numCoords, stride, ptr, numVertices);
+   else
+#endif
+      glVertexPointer(numCoords, glType, stride, ptr);
+}
+
+public void GLBufferData(int type, GLenum target, int size, const GLvoid *data, GLenum usage)
+{
+#ifdef _GLES
    if(type == GL_DOUBLE)
-      glesBufferDatad(target, size, data, usage);
+      glesBufferDatad(target, size, (void *)data, usage);
    else if(type == GL_UNSIGNED_INT)
-      glesBufferDatai(target, size, data, usage);
+      glesBufferDatai(target, size, (void *)data, usage);
    else
+#endif
+
+#if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
       glBufferData(target, size, data, usage);
 #else
+
+#if defined(__WIN32__)
    if(glBufferDataARB)
+#endif
       glBufferDataARB(target, size, data, usage);
-
 #endif
 }
 
+#if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
+static int primitiveTypes[RenderPrimitiveType] =
+{
+   GL_POINTS, GL_LINES, GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUADS, GL_QUAD_STRIP, GL_LINE_STRIP
+};
+#endif
+
+
+// Non OpenGL ES friendly stuff
+
+#if defined(_GLES)
+
+//#undef GL_UNSIGNED_INT
+//#undef GL_DOUBLE
+#undef GL_INT
+//#undef GL_POLYGON
+//#undef GL_QUADS
+#undef GL_QUAD_STRIP
+#undef GL_POLYGON_STIPPLE
+#undef GL_LINE_STIPPLE
+#undef GL_LINE
+#undef GL_FILL
+#undef GL_ALL_ATTRIB_BITS
+#undef GL_LIGHT_MODEL_LOCAL_VIEWER
+
+#endif
+
 static int displayWidth, displayHeight;
 
 #define GL_CLAMP_TO_EDGE 0x812F
@@ -1202,7 +1419,7 @@ class OGLDisplay : struct
    int imageBuffers[2];
    byte * pboMemory1, * pboMemory2;
    */
-#else
+#elif !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
    GLXContext glContext;
 
    Pixmap pixmap;
@@ -1236,7 +1453,7 @@ class OGLSystem : struct
    HDC hdc;
    HGLRC glrc;
    HWND hwnd;
-#else
+#elif !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
    XVisualInfo * visualInfo;
    GLXContext glContext;
    GLXDrawable glxDrawable;
@@ -1249,33 +1466,27 @@ class OGLSurface : struct
    bool opaqueText;
    int xOffset;
    bool writingText;
+   bool writingOutline;
 
    float foreground[4], background[4], bitmapMult[4];
 } OGLSurface;
 
 class OGLMesh : struct
 {
-   int vertices;
-   int normals;
-   int texCoords;
-   int texCoords2;
-   int colors;
+   uint vertices;
+   uint normals;
+   uint texCoords;
+   uint texCoords2;
+   uint colors;
 };
 
 class OGLIndices : struct
 {
    uint16 * indices;
-   int buffer;
-   int nIndices;
+   uint buffer;
+   uint nIndices;
 };
 
-#if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
-static int primitiveTypes[RenderPrimitiveType] =
-{
-   GL_POINTS, GL_LINES, GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUADS, GL_QUAD_STRIP, GL_LINE_STRIP
-};
-#endif
-
 int current;
 void * previous;
 
@@ -1285,6 +1496,7 @@ class OpenGLDisplayDriver : DisplayDriver
 
    bool LockSystem(DisplaySystem displaySystem)
    {
+#if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
       OGLSystem oglSystem = displaySystem.driverData;
       if(useSingleGLContext) return true;
    #if defined(__WIN32__)
@@ -1292,11 +1504,10 @@ class OpenGLDisplayDriver : DisplayDriver
    #elif defined(__unix__) || defined(__APPLE__)
       //if(previous) return true;
       // printf("Making SYSTEM current\n");
-#if !defined(__ANDROID__)
       glXMakeCurrent(xGlobalDisplay, (GLXDrawable)oglSystem.glxDrawable, oglSystem.glContext);
-#endif
       //previous = oglSystem.glContext;
    #endif
+#endif
       return true;
    }
 
@@ -1307,7 +1518,7 @@ class OpenGLDisplayDriver : DisplayDriver
       wglMakeCurrent(null, null);
    #elif defined(__unix__) || defined(__APPLE__)
       // printf("Making NULL current\n");
-      #if defined(__ANDROID__)
+      #if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
       #else
       glXMakeCurrent(xGlobalDisplay, None, null);
       #endif
@@ -1317,20 +1528,17 @@ class OpenGLDisplayDriver : DisplayDriver
 
    bool Lock(Display display)
    {
+#if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
       OGLDisplay oglDisplay = display.driverData;
-      OGLSystem oglSystem = display.displaySystem.driverData;
-
       if(useSingleGLContext) return true;
    #if defined(__WIN32__)
       wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
    #elif defined(__unix__) || defined(__APPLE__)
       // if(previous) glXMakeCurrent(xGlobalDisplay, None, null);
       // printf("   Making DISPLAY current\n");
-      #if defined(__ANDROID__)
-      #else
       glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
-      #endif
    #endif
+#endif
       return true;
    }
 
@@ -1368,7 +1576,7 @@ class OpenGLDisplayDriver : DisplayDriver
          if(oglDisplay.memBitmap) DeleteObject(oglDisplay.memBitmap);
 
    #elif defined(__unix__) || defined(__APPLE__)
-      #if defined(__ANDROID__)
+      #if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
       #else
          if(oglDisplay.shapePixmap)
             XFreePixmap(xGlobalDisplay, oglDisplay.shapePixmap);
@@ -1411,7 +1619,7 @@ class OpenGLDisplayDriver : DisplayDriver
 
    void ::CheckExtensions(OGLSystem oglSystem)
    {
-      char * extensions = glGetString(GL_EXTENSIONS);
+      const char * extensions = (const char *)glGetString(GL_EXTENSIONS);
       if(extensions)
          oglSystem.pow2textures = strstr(extensions, "GL_ARB_texture_non_power_of_two") ? false : true;
       glGetIntegerv(GL_MAX_TEXTURE_SIZE, &oglSystem.maxTextureSize);
@@ -1550,6 +1758,20 @@ class OpenGLDisplayDriver : DisplayDriver
          egl_init_display(guiApp.desktop.windowHandle);
          CheckExtensions(oglSystem);
          result = true;
+      #elif defined(__EMSCRIPTEN__)
+         if(glfwInit() == GL_TRUE)
+         {
+            const int width = 640, height = 480;
+            if(glfwOpenWindow(width, height, 8, 8, 8, 8, 16, 0, GLFW_WINDOW) == GL_TRUE)
+            {
+               //glfwSwapBuffers();
+               result = true;
+            }
+            else
+               printf("glfwOpenWindow() failed\n"); //glfwTerminate();
+         }
+         else
+            printf("glfwInit() failed\n"); //glfwTerminate();
       #else
       {
          X11Window root = RootWindow( xGlobalDisplay, DefaultScreen( xGlobalDisplay ) );
@@ -1614,6 +1836,8 @@ class OpenGLDisplayDriver : DisplayDriver
    #elif defined(__unix__) || defined(__APPLE__)
       #if defined(__ANDROID__)
          egl_term_display();
+      #elif defined(__EMSCRIPTEN__)
+         glfwTerminate();
       #else
       if(oglSystem.visualInfo)
       {
@@ -1638,7 +1862,9 @@ class OpenGLDisplayDriver : DisplayDriver
    {
       bool result = false;
       OGLDisplay oglDisplay = display.driverData;
+#if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
       OGLSystem oglSystem = display.displaySystem.driverData;
+#endif
       if(!oglDisplay)
          oglDisplay = display.driverData = OGLDisplay { };
       //printf("Inside CreateDisplay\n");
@@ -1659,7 +1885,7 @@ class OpenGLDisplayDriver : DisplayDriver
          else
             ReleaseDC(display.window, oglDisplay.hdc);
    #elif defined(__unix__) || defined(__APPLE__)
-      #if defined(__ANDROID__)
+      #if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
       #else
          XVisualInfo * visualInfo = ((XWindowData)display.windowDriverData).visual;
          /*
@@ -1703,12 +1929,18 @@ class OpenGLDisplayDriver : DisplayDriver
 #endif
       if(result)
       {
-#if !defined(__OLDX__)
+#if defined(__WIN32__)
          if(glBlendFuncSeparate)
             glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
          else
-#endif
             glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+#else
+#if !defined(__OLDX__)
+          glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
+#else
+         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+#endif
+#endif
          glEnable(GL_BLEND);
 
          glMatrixMode(GL_MODELVIEW);
@@ -1719,7 +1951,9 @@ class OpenGLDisplayDriver : DisplayDriver
          glShadeModel(GL_FLAT);
 
          // glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, true);
+#if !defined(EM_MODE)
          glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
+#endif
          glFogi(GL_FOG_MODE, GL_EXP);
          glFogf(GL_FOG_DENSITY, 0);
          glEnable(GL_NORMALIZE);
@@ -1736,7 +1970,7 @@ class OpenGLDisplayDriver : DisplayDriver
    #if defined(__WIN32__)
          wglMakeCurrent(null, null);
    #elif defined(__unix__) || defined(__APPLE__)
-      #if defined(__ANDROID__)
+      #if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
          result = true;
       #else
          glXMakeCurrent(xGlobalDisplay, None, null);
@@ -1750,12 +1984,12 @@ class OpenGLDisplayDriver : DisplayDriver
    bool DisplaySize(Display display, int width, int height)
    {
       OGLDisplay oglDisplay = display.driverData;
-      OGLSystem oglSystem = display.displaySystem.driverData;
 
       bool result = false;
 
       //printf("Inside DisplaySize\n");
 #if defined(__WIN32__) || defined(USEPBUFFER)
+      OGLSystem oglSystem = display.displaySystem.driverData;
       if(display.alphaBlend)
       {
 #if defined(__WIN32__)
@@ -1913,7 +2147,7 @@ class OpenGLDisplayDriver : DisplayDriver
             ReleaseDC(display.window, hdc);
          }
 #elif defined(__unix__) || defined(__APPLE__)
-      #if defined(__ANDROID__)
+      #if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
          result = true;
       #else
        int attrib[] =
@@ -2077,7 +2311,7 @@ class OpenGLDisplayDriver : DisplayDriver
 #if defined(__WIN32__)
          wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
 #elif defined(__unix__) || defined(__APPLE__)
-      #if defined(__ANDROID__)
+      #if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
          width = eglWidth;
          height = eglHeight;
       #else
@@ -2107,7 +2341,11 @@ class OpenGLDisplayDriver : DisplayDriver
       {
          oglDisplay.flipBufW = width;
          oglDisplay.flipBufH = height;
+#ifdef _GLES
+         result = true;
+#else
          oglDisplay.flippingBuffer = renew oglDisplay.flippingBuffer ColorAlpha [width * height];
+#endif
       }
       if(oglDisplay.flippingBuffer || !width || !height)
          result = true;
@@ -2145,7 +2383,9 @@ class OpenGLDisplayDriver : DisplayDriver
 
    void Update(Display display, Box updateBox)
    {
+#if defined(__WIN32__) || defined(USEPBUFFER)
       OGLDisplay oglDisplay = display.driverData;
+#endif
       //Logf("DisplayScreen\n");
 
       glFlush();
@@ -2209,7 +2449,7 @@ class OpenGLDisplayDriver : DisplayDriver
 
             ReleaseDC(0, hdc);
 #elif defined(__unix__) || defined(__APPLE__)
-      #if defined(__ANDROID__)
+      #if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
       #else
             XTransform transform =
             {
@@ -2241,6 +2481,8 @@ class OpenGLDisplayDriver : DisplayDriver
 #elif defined(__unix__) || defined(__APPLE__)
       #if defined(__ANDROID__)
          eglSwapBuffers(eglDisplay, eglSurface);
+      #elif defined(__EMSCRIPTEN__)
+         glfwSwapBuffers();
       #else
          glXSwapBuffers(xGlobalDisplay, (GLXDrawable)display.window);
       #endif
@@ -2253,7 +2495,8 @@ class OpenGLDisplayDriver : DisplayDriver
    {
       if(bitmap.driverData)
       {
-         glDeleteTextures(1, (int *)&bitmap.driverData);
+         GLuint tex = (GLuint)(uintptr)bitmap.driverData;
+         glDeleteTextures(1, &tex);
          bitmap.driverData = 0;
       }
       bitmap.driver = ((subclass(DisplayDriver))class(LFBDisplayDriver));
@@ -2264,7 +2507,7 @@ class OpenGLDisplayDriver : DisplayDriver
       OGLSystem oglSystem = displaySystem.driverData;
       bool result = false;
       Bitmap mipMap { };
-      int glBitmap = -1;
+      GLuint glBitmap = 0;
 
       uint w = width, h = height;
       if(oglSystem.pow2textures)
@@ -2295,7 +2538,7 @@ class OpenGLDisplayDriver : DisplayDriver
 
       delete mipMap;
 
-      bitmap.driverData = (void *)glBitmap;
+      bitmap.driverData = (void *)(uintptr)glBitmap;
       bitmap.driver = displaySystem.driver;
       bitmap.width = w;
       bitmap.height = h;
@@ -2308,13 +2551,19 @@ class OpenGLDisplayDriver : DisplayDriver
    {
       bool result = false;
       OGLSystem oglSystem = displaySystem.driverData;
+      Bitmap convBitmap = bitmap;
+      if(bitmap.keepData)
+      {
+         convBitmap = { };
+         convBitmap.Copy(bitmap);
+      }
 
       // Pre process the bitmap... First make it 32 bit
-      if(/*bitmap.pixelFormat == pixelFormatRGBA || */bitmap.Convert(null, pixelFormat888, null))
+      if(/*bitmap.pixelFormat == pixelFormatRGBA || */convBitmap.Convert(null, pixelFormat888, null))
       {
          int c, level;
          uint w = bitmap.width, h = bitmap.height;
-         int glBitmap = -1;
+         GLuint glBitmap = 0;
          if(oglSystem.pow2textures)
          {
             w = pow2i(w);
@@ -2336,19 +2585,18 @@ class OpenGLDisplayDriver : DisplayDriver
             {
                // ((ColorRGBA *)bitmap.picture)[c] = ((ColorAlpha *)bitmap.picture)[c];
                // TODO:
-               ColorAlpha color = ((ColorAlpha *)bitmap.picture)[c];
-               ((ColorRGBA *)bitmap.picture)[c] = ColorRGBA { color.color.r, color.color.g, color.color.b, color.a };
+               ColorAlpha color = ((ColorAlpha *)convBitmap.picture)[c];
+               ((ColorRGBA *)convBitmap.picture)[c] = ColorRGBA { color.color.r, color.color.g, color.color.b, color.a };
             }
          }
-         bitmap.pixelFormat = pixelFormat888;
+         // convBitmap.pixelFormat = pixelFormat888;
 
          glGetError();
          glGenTextures(1, &glBitmap);
-         if(glBitmap == -1)
+         if(glBitmap == 0)
          {
-            int error = glGetError();
+            //int error = glGetError();
             return false;
-            //Print("");
          }
 
          glBindTexture(GL_TEXTURE_2D, glBitmap);
@@ -2374,10 +2622,10 @@ class OpenGLDisplayDriver : DisplayDriver
             if(bitmap.width != w || bitmap.height != h)
             {
                mipMap = Bitmap { };
-               if(mipMap.Allocate(null, w, h, w, bitmap.pixelFormat, false))
+               if(mipMap.Allocate(null, w, h, w, convBitmap.pixelFormat, false))
                {
                   Surface mipSurface = mipMap.GetSurface(0,0,null);
-                  mipSurface.Filter(bitmap, 0,0,0,0, w, h, bitmap.width, bitmap.height);
+                  mipSurface.Filter(convBitmap, 0,0,0,0, w, h, convBitmap.width, convBitmap.height);
                   delete mipSurface;
                }
                else
@@ -2387,7 +2635,7 @@ class OpenGLDisplayDriver : DisplayDriver
                }
             }
             else
-               mipMap = bitmap;
+               mipMap = convBitmap;
 
             if(result)
             {
@@ -2406,14 +2654,13 @@ class OpenGLDisplayDriver : DisplayDriver
                   result = false;
                }
             }
-            if(mipMap != bitmap)
+            if(mipMap != convBitmap)
                delete mipMap;
             if(!mipMaps) break;
          }
 
-         if(!bitmap.keepData)
-            bitmap.driver.FreeBitmap(bitmap.displaySystem, bitmap);
-         bitmap.driverData = (void *)glBitmap;
+         convBitmap.driver.FreeBitmap(convBitmap.displaySystem, convBitmap);
+         bitmap.driverData = (void *)(uintptr)glBitmap;
          bitmap.driver = displaySystem.driver;
 
          if(!result)
@@ -2618,7 +2865,7 @@ class OpenGLDisplayDriver : DisplayDriver
 
       glColor4fv(oglSurface.foreground);
       glBegin(GL_LINES);
-#ifdef __ANDROID__
+#if defined(_GLES) || defined(EM_MODE)
       if(stippleEnabled)
       {
          glTexCoord2f(0.5f, 0);
@@ -2651,7 +2898,7 @@ class OpenGLDisplayDriver : DisplayDriver
       //Logf("Rectangle\n");
 
       glColor4fv(oglSurface.foreground);
-#ifdef __ANDROID__
+#if defined(_GLES) || defined(EM_MODE)
       if(stippleEnabled)
       {
          glBegin(GL_LINES);
@@ -2700,9 +2947,18 @@ class OpenGLDisplayDriver : DisplayDriver
       //Logf("Area\n");
 
       glColor4fv(oglSurface.background);
+
+#ifdef EM_MODE
+      glBegin(GL_QUADS);
+      glVertex2f(x1+surface.offset.x, y1+surface.offset.y);
+      glVertex2f(x1+surface.offset.x, y2+surface.offset.y+1);
+      glVertex2f(x2+surface.offset.x+1, y2+surface.offset.y+1);
+      glVertex2f(x2+surface.offset.x+1, y1+surface.offset.y);
+      glEnd();
+#else
       glRecti(x1+surface.offset.x, y1+surface.offset.y,
               x2+surface.offset.x + 1, y2+surface.offset.y + 1);
-
+#endif
       /*
       glRectf(x1+surface.offset.x, y1+surface.offset.y,
               x2+surface.offset.x + 1, y2+surface.offset.y + 1);
@@ -2753,7 +3009,7 @@ class OpenGLDisplayDriver : DisplayDriver
       else if(oglSurface.xOffset)
          glTranslated(oglSurface.xOffset / 64.0/*-0.375*/, 0.0, 0.0);
 
-      glBindTexture(GL_TEXTURE_2D, (uint)bitmap.driverData);
+      glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)bitmap.driverData);
       glBegin(GL_QUADS);
 
       if(h < 0)
@@ -2820,7 +3076,7 @@ class OpenGLDisplayDriver : DisplayDriver
 #endif
 
       glEnable(GL_TEXTURE_2D);
-      glBindTexture(GL_TEXTURE_2D, (uint)bitmap.driverData);
+      glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)bitmap.driverData);
 
       glColor4fv(oglSurface.bitmapMult);
 
@@ -2874,8 +3130,9 @@ class OpenGLDisplayDriver : DisplayDriver
 
    void StretchDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
    {
+#if !defined(EM_MODE)
       float s2dw,s2dh,d2sw,d2sh;
-      bool flipX = false, flipY = false;
+      //bool flipX = false, flipY = false;
 
       //Logf("StretchDI\n");
 
@@ -2883,13 +3140,13 @@ class OpenGLDisplayDriver : DisplayDriver
       {
          w = Abs(w);
          sw = Abs(sw);
-         flipX = true;
+         //flipX = true;
       }
       if(Sgn(h) != Sgn(sh))
       {
          h = Abs(h);
          sh = Abs(sh);
-         flipY = true;
+         //flipY = true;
       }
 
       s2dw=(float)w / sw;
@@ -2970,10 +3227,12 @@ class OpenGLDisplayDriver : DisplayDriver
          glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
          glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
       }
+#endif
    }
 
    void BlitDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
    {
+#if !defined(EM_MODE)
       //Logf("BlitDI\n");
 
       //Clip against the edges of the source
@@ -3034,6 +3293,7 @@ class OpenGLDisplayDriver : DisplayDriver
          glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
          glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
       }
+#endif
    }
 
    void FilterDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
@@ -3046,7 +3306,7 @@ class OpenGLDisplayDriver : DisplayDriver
       ((subclass(DisplayDriver))class(LFBDisplayDriver)).UnloadFont(displaySystem, font);
    }
 
-   Font LoadFont(DisplaySystem displaySystem, char * faceName, float size, FontFlags flags)
+   Font LoadFont(DisplaySystem displaySystem, const char * faceName, float size, FontFlags flags)
    {
       Font font;
       OGLSystem oglSystem = displaySystem.driverData;
@@ -3055,12 +3315,12 @@ class OpenGLDisplayDriver : DisplayDriver
       return font;
    }
 
-   void FontExtent(DisplaySystem displaySystem, Font font, char * text, int len, int * width, int * height)
+   void FontExtent(DisplaySystem displaySystem, Font font, const char * text, int len, int * width, int * height)
    {
       ((subclass(DisplayDriver))class(LFBDisplayDriver)).FontExtent(displaySystem, font, text, len, width, height);
    }
 
-   void WriteText(Display display, Surface surface, int x, int y, char * text, int len)
+   void WriteText(Display display, Surface surface, int x, int y, const char * text, int len)
    {
       OGLSurface oglSurface = surface.driverData;
       OGLSystem oglSystem = display.displaySystem.driverData;
@@ -3080,6 +3340,18 @@ class OpenGLDisplayDriver : DisplayDriver
       oglSurface.writingText = true;
 
       glEnable(GL_TEXTURE_2D);
+
+      if(surface.outline.size)
+      {
+         ColorAlpha outlineColor = surface.outline.color;
+         glColor4ub(outlineColor.color.r, outlineColor.color.g, outlineColor.color.b, outlineColor.a);
+         //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+         //glEnable(GL_BLEND);
+
+         oglSurface.writingOutline = true;
+         ((subclass(DisplayDriver))class(LFBDisplayDriver)).WriteText(display, surface, x, y, text, len);
+         oglSurface.writingOutline = false;
+      }
       glColor4fv(oglSurface.foreground);
 
       ((subclass(DisplayDriver))class(LFBDisplayDriver)).WriteText(display, surface, x, y, text, len);
@@ -3102,7 +3374,7 @@ class OpenGLDisplayDriver : DisplayDriver
       oglSurface.opaqueText = opaque;
    }
 
-   void TextExtent(Display display, Surface surface, char * text, int len, int * width, int * height)
+   void TextExtent(Display display, Surface surface, const char * text, int len, int * width, int * height)
    {
       OGLSurface oglSurface = surface.driverData;
       OGLSystem oglSystem = display.displaySystem.driverData;
@@ -3122,7 +3394,7 @@ class OpenGLDisplayDriver : DisplayDriver
 
       if(stipple)
       {
-#if defined(__ANDROID__)
+#if defined(_GLES) || defined(EM_MODE)
          stippleEnabled = true;
          glesLineStipple(1, (uint16)stipple);
 #else
@@ -3132,7 +3404,7 @@ class OpenGLDisplayDriver : DisplayDriver
       }
       else
       {
-#if defined(__ANDROID__)
+#if defined(_GLES) || defined(EM_MODE)
          stippleEnabled = false;
          glMatrixMode(GL_TEXTURE);
          glLoadIdentity();
@@ -3158,7 +3430,9 @@ class OpenGLDisplayDriver : DisplayDriver
                glDisable(GL_MULTISAMPLE_ARB);
             break;
          case fillMode:
+#if !defined(_GLES)
             glPolygonMode(GL_FRONT_AND_BACK, ((FillModeValue)value == solid) ? GL_FILL : GL_LINE);
+#endif
             break;
          case depthTest:
             if(value) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
@@ -3174,16 +3448,17 @@ class OpenGLDisplayDriver : DisplayDriver
             break;
          }
          case fogDensity:
-            value *= nearPlane;
-            glFogf(GL_FOG_DENSITY, *(float *)(void *)&value);
+            glFogf(GL_FOG_DENSITY, (float)(RenderStateFloat { ui = value }.f * nearPlane));
             break;
          case blend:
             if(value) glEnable(GL_BLEND); else glDisable(GL_BLEND);
             break;
          case ambient:
          {
+#if !defined(EM_MODE)
             float ambient[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
             glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
+#endif
             break;
          }
          case alphaWrite:
@@ -3203,6 +3478,7 @@ class OpenGLDisplayDriver : DisplayDriver
 
    void SetLight(Display display, int id, Light light)
    {
+#if !defined(EM_MODE)
       //Logf("SetLight\n");
 
       if(light != null)
@@ -3349,11 +3625,13 @@ class OpenGLDisplayDriver : DisplayDriver
             position[1] = direction.y;
             position[2] = direction.z;
 
+            PrintLn("position");
             glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
          }
       }
       else
          glDisable(GL_LIGHT0 + id);
+#endif
    }
 
    void SetCamera(Display display, Surface surface, Camera camera)
@@ -3379,14 +3657,17 @@ class OpenGLDisplayDriver : DisplayDriver
 
          // *** Projection Matrix ***
          if(!display.display3D.camera)
+         {
+            glMatrixMode(GL_PROJECTION);
             glPushMatrix();
+         }
          else
             glMatrixMode(GL_PROJECTION);
          if(display.display3D.collectingHits)
          {
             float pickX = display.display3D.pickX + surface.offset.x;
             float pickY = display.height - (display.display3D.pickY + surface.offset.y) - 1;
-            Matrix pickMatrix =
+            Matrix pickMatrix
             {
                {
                   w / display.display3D.pickWidth, 0, 0, 0,
@@ -3425,8 +3706,10 @@ class OpenGLDisplayDriver : DisplayDriver
          // ...
 
          glEnable(GL_DEPTH_TEST);
+#if !defined(EM_MODE)
          glEnable(GL_LIGHTING);
          glShadeModel(GL_SMOOTH);
+#endif
          glDepthMask((byte)bool::true);
          oglDisplay.depthWrite = true;
 
@@ -3442,7 +3725,9 @@ class OpenGLDisplayDriver : DisplayDriver
          glDisable(GL_LIGHTING);
          glDisable(GL_FOG);
          glDisable(GL_TEXTURE_2D);
+#if !defined(EM_MODE)
          glShadeModel(GL_FLAT);
+#endif
          glEnable(GL_BLEND);
          glDisable(GL_MULTISAMPLE_ARB);
 
@@ -3464,12 +3749,16 @@ class OpenGLDisplayDriver : DisplayDriver
       // Basic Properties
       if(material.flags.doubleSided)
       {
+#if !defined(EM_MODE)
          glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, !material.flags.singleSideLight);
+#endif
          glDisable(GL_CULL_FACE);
       }
       else
       {
+#if !defined(EM_MODE)
          glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, bool::false);
+#endif
          glEnable(GL_CULL_FACE);
       }
 
@@ -3480,11 +3769,11 @@ class OpenGLDisplayDriver : DisplayDriver
          glEnable(GL_FOG);
 
       // Maps
-      if(material.baseMap && mesh.texCoords)
+      if(material.baseMap && (mesh.texCoords || mesh.flags.texCoords1))
       {
          Bitmap map = material.baseMap;
          glEnable(GL_TEXTURE_2D);
-         glBindTexture(GL_TEXTURE_2D, (uint)map.driverData);
+         glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)map.driverData);
 
          glMatrixMode(GL_TEXTURE);
          glLoadIdentity();
@@ -3506,6 +3795,9 @@ class OpenGLDisplayDriver : DisplayDriver
       else
          glDisable(GL_TEXTURE_2D);
 
+#ifdef EM_MODE
+      glColor4f(material.diffuse.r, material.diffuse.g, material.diffuse.b, material.opacity);
+#else
       if(mesh.flags.colors)
       {
          glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
@@ -3533,6 +3825,7 @@ class OpenGLDisplayDriver : DisplayDriver
       }
 
       glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &material.power);
+#endif
    }
 
    void FreeMesh(DisplaySystem displaySystem, Mesh mesh)
@@ -3594,7 +3887,7 @@ class OpenGLDisplayDriver : DisplayDriver
       }
    }
 
-   bool AllocateMesh(DisplaySystem displaySystem, Mesh mesh)
+   bool AllocateMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags, int nVertices)
    {
       bool result = false;
 
@@ -3603,26 +3896,85 @@ class OpenGLDisplayDriver : DisplayDriver
       if(mesh.data)
       {
          OGLMesh oglMesh = mesh.data;
-
-         if(mesh.flags.vertices && !oglMesh.vertices && !mesh.vertices)
-         {
-            mesh.vertices = mesh.flags.doubleVertices ? (Vector3Df *)new Vector3D[mesh.nVertices] : new Vector3Df[mesh.nVertices];
-            GLGenBuffers(1, &oglMesh.vertices);
-         }
-         if(mesh.flags.normals && !oglMesh.normals && !mesh.normals)
-         {
-            GLGenBuffers( 1, &oglMesh.normals);
-            mesh.normals = mesh.flags.doubleNormals ? (Vector3Df *)new Vector3D[mesh.nVertices] : new Vector3Df[mesh.nVertices];
-         }
-         if(mesh.flags.texCoords1 && !oglMesh.texCoords && !mesh.texCoords)
+         if(mesh.nVertices == nVertices)
          {
-            GLGenBuffers( 1, &oglMesh.texCoords);
-            mesh.texCoords = new Pointf[mesh.nVertices];
+            // Same number of vertices, adding features (Leaves the other features pointers alone)
+            if(mesh.flags != flags)
+            {
+               if(!mesh.flags.vertices && flags.vertices)
+               {
+                  if(flags.doubleVertices)
+                  {
+                     mesh.vertices = (Vector3Df *)new Vector3D[nVertices];
+                  }
+                  else
+                     mesh.vertices = new Vector3Df[nVertices];
+                  if(!oglMesh.vertices)
+                     GLGenBuffers(1, &oglMesh.vertices);
+               }
+               if(!mesh.flags.normals && flags.normals)
+               {
+                  if(flags.doubleNormals)
+                  {
+                     mesh.normals = (Vector3Df *)new Vector3D[nVertices];
+                  }
+                  else
+                     mesh.normals = new Vector3Df[nVertices];
+                  if(!oglMesh.normals)
+                     GLGenBuffers( 1, &oglMesh.normals);
+               }
+               if(!mesh.flags.texCoords1 && flags.texCoords1)
+               {
+                  mesh.texCoords = new Pointf[nVertices];
+                  if(!oglMesh.texCoords)
+                     GLGenBuffers( 1, &oglMesh.texCoords);
+               }
+               if(!mesh.flags.colors && flags.colors)
+               {
+                  mesh.colors = new ColorRGBAf[nVertices];
+                  if(!oglMesh.colors)
+                     GLGenBuffers( 1, &oglMesh.colors);
+               }
+            }
          }
-         if(mesh.flags.colors && !oglMesh.colors && !mesh.colors)
+         else
          {
-            GLGenBuffers( 1, &oglMesh.colors);
-            mesh.colors = new ColorRGBAf[mesh.nVertices];
+            // New number of vertices, reallocate all current and new features
+            flags |= mesh.flags;
+            if(flags.vertices)
+            {
+               if(flags.doubleVertices)
+               {
+                  mesh.vertices = (Vector3Df *)renew mesh.vertices Vector3D[nVertices];
+               }
+               else
+                  mesh.vertices = renew mesh.vertices Vector3Df[nVertices];
+               if(!oglMesh.vertices)
+                  GLGenBuffers(1, &oglMesh.vertices);
+            }
+            if(flags.normals)
+            {
+               if(flags.doubleNormals)
+               {
+                  mesh.normals = (Vector3Df *)renew mesh.normals Vector3D[nVertices];
+               }
+               else
+                  mesh.normals = renew mesh.normals Vector3Df[nVertices];
+               if(!oglMesh.normals)
+                  GLGenBuffers( 1, &oglMesh.normals);
+            }
+            if(flags.texCoords1)
+            {
+               mesh.texCoords = renew mesh.texCoords Pointf[nVertices];
+               if(!oglMesh.texCoords)
+                  GLGenBuffers( 1, &oglMesh.texCoords);
+            }
+            if(flags.colors)
+            {
+               mesh.colors = renew mesh.colors ColorRGBAf[nVertices];
+               if(!oglMesh.colors)
+                  GLGenBuffers( 1, &oglMesh.colors);
+            }
          }
          result = true;
       }
@@ -3636,25 +3988,25 @@ class OpenGLDisplayDriver : DisplayDriver
 
       if(vboAvailable)
       {
-         if(!(flags.vertices) || oglMesh.vertices)
+         if(flags.vertices && oglMesh.vertices)
          {
             GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.vertices);
             GLBufferData( mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT, GL_ARRAY_BUFFER_ARB, mesh.nVertices * (mesh.flags.doubleVertices ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.vertices, GL_STATIC_DRAW_ARB );
          }
 
-         if(!(flags.normals) || oglMesh.normals)
+         if(flags.normals && oglMesh.normals)
          {
             GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.normals);
             GLBufferData( mesh.flags.doubleNormals ? GL_DOUBLE : GL_FLOAT, GL_ARRAY_BUFFER_ARB, mesh.nVertices * (mesh.flags.doubleNormals ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.normals, GL_STATIC_DRAW_ARB );
          }
 
-         if(!(flags.texCoords1) || oglMesh.texCoords)
+         if(flags.texCoords1 && oglMesh.texCoords)
          {
             GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.texCoords);
             GLBufferData( GL_FLOAT, GL_ARRAY_BUFFER_ARB, mesh.nVertices * sizeof(Pointf), mesh.texCoords, GL_STATIC_DRAW_ARB );
          }
 
-         if(!(flags.colors) || oglMesh.colors)
+         if(flags.colors && oglMesh.colors)
          {
             GLBindBuffer( GL_ARRAY_BUFFER_ARB, oglMesh.colors);
             GLBufferData( GL_FLOAT, GL_ARRAY_BUFFER_ARB, mesh.nVertices * sizeof(ColorRGBAf), mesh.colors, GL_STATIC_DRAW_ARB );
@@ -3715,13 +4067,17 @@ class OpenGLDisplayDriver : DisplayDriver
    {
       //Logf("SelectMesh\n");
 
-#if !defined( __ANDROID__) && !defined(__APPLE__)
-      if(display.display3D.mesh && glUnlockArraysEXT)
-         glUnlockArraysEXT();
+#if !defined( __ANDROID__) && !defined(__APPLE__) && !defined(__EMSCRIPTEN__)
+
+#if defined(__WIN32__)
+      if(glUnlockArraysEXT)
+#endif
+         if(!vboAvailable && display.display3D.mesh)
+            glUnlockArraysEXT();
+
 #endif
       if(mesh)
       {
-         OGLDisplay oglDisplay = display.driverData;
          OGLMesh oglMesh = mesh.data;
 
          // *** Vertex Stream ***
@@ -3729,20 +4085,23 @@ class OpenGLDisplayDriver : DisplayDriver
          if(!display.display3D.collectingHits && oglMesh)
          {
             GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.vertices );
-            glVertexPointer(3, mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT, 0, vboAvailable ? null : mesh.vertices);
+            if(mesh.flags.doubleVertices)
+               glVertexPointerd(3, 0, (double *)(vboAvailable ? null : mesh.vertices), mesh.nVertices);
+            else
+               glVertexPointer(3, GL_FLOAT, 0, vboAvailable ? null : mesh.vertices);
 
             // *** Normals Stream ***
-            if(mesh.normals)
+            if(mesh.normals || mesh.flags.normals)
             {
                glEnableClientState(GL_NORMAL_ARRAY);
                GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.normals);
-               glNormalPointer(mesh.flags.doubleNormals ? GL_DOUBLE : GL_FLOAT, 0, vboAvailable ? null : mesh.normals);
+               glNormalPointer(/*mesh.flags.doubleNormals ? GL_DOUBLE : */GL_FLOAT, 0, vboAvailable ? null : mesh.normals);
             }
             else
                glDisableClientState(GL_NORMAL_ARRAY);
 
             // *** Texture Coordinates Stream ***
-            if(mesh.texCoords)
+            if(mesh.texCoords || mesh.flags.texCoords1)
             {
                glEnableClientState(GL_TEXTURE_COORD_ARRAY);
                GLBindBuffer( GL_ARRAY_BUFFER_ARB, oglMesh.texCoords);
@@ -3752,7 +4111,7 @@ class OpenGLDisplayDriver : DisplayDriver
                glDisableClientState(GL_TEXTURE_COORD_ARRAY);
 
             // *** Color Stream ***
-            if(mesh.colors)
+            if(mesh.colors || mesh.flags.colors)
             {
                glEnableClientState(GL_COLOR_ARRAY);
                GLBindBuffer( GL_ARRAY_BUFFER_ARB, oglMesh.colors);
@@ -3765,22 +4124,25 @@ class OpenGLDisplayDriver : DisplayDriver
          else
          {
             GLBindBuffer( GL_ARRAY_BUFFER_ARB, 0);
-            glVertexPointer(3,mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT,0,mesh.vertices);
-            if(mesh.normals && !display.display3D.collectingHits)
+            if(mesh.flags.doubleVertices)
+               glVertexPointerd(3, 0, (double *)mesh.vertices, mesh.nVertices);
+            else
+               glVertexPointer(3, GL_FLOAT, 0, mesh.vertices);
+            if((mesh.normals || mesh.flags.normals) && !display.display3D.collectingHits)
             {
                glEnableClientState(GL_NORMAL_ARRAY);
-               glNormalPointer(mesh.flags.doubleNormals ? GL_DOUBLE : GL_FLOAT, 0, mesh.normals);
+               glNormalPointer(/*mesh.flags.doubleNormals ? GL_DOUBLE : */GL_FLOAT, 0, mesh.normals);
             }
             else
                glDisableClientState(GL_NORMAL_ARRAY);
-            if(mesh.texCoords && !display.display3D.collectingHits)
+            if((mesh.texCoords || mesh.flags.texCoords1) && !display.display3D.collectingHits)
             {
                glEnableClientState(GL_TEXTURE_COORD_ARRAY);
                glTexCoordPointer(2, GL_FLOAT, 0, mesh.texCoords);
             }
             else
                glDisableClientState(GL_TEXTURE_COORD_ARRAY);
-            if(mesh.colors && !display.display3D.collectingHits)
+            if((mesh.colors || mesh.flags.colors) && !display.display3D.collectingHits)
             {
                glEnableClientState(GL_COLOR_ARRAY);
                glColorPointer(4, GL_FLOAT, 0, mesh.colors);
@@ -3789,8 +4151,14 @@ class OpenGLDisplayDriver : DisplayDriver
                glDisableClientState(GL_COLOR_ARRAY);
          }
 
-#if !defined(__ANDROID__) && !defined(__APPLE__)
-         if(glLockArraysEXT) glLockArraysEXT(0, mesh.nVertices);
+#if !defined(__ANDROID__) && !defined(__APPLE__) && !defined(__EMSCRIPTEN__)
+
+#if defined(__WIN32__)
+         if(glLockArraysEXT)
+#endif
+            if(!vboAvailable)
+               glLockArraysEXT(0, mesh.nVertices);
+
 #endif
       }
       else
@@ -3799,7 +4167,6 @@ class OpenGLDisplayDriver : DisplayDriver
 
    void DrawPrimitives(Display display, PrimitiveSingle * primitive, Mesh mesh)
    {
-      OGLDisplay oglDisplay = display.driverData;
       //Logf("DrawPrimitives\n");
 
       if(primitive->type.vertexRange)
@@ -3809,7 +4176,7 @@ class OpenGLDisplayDriver : DisplayDriver
          //    *** Hoping the data won't be uploaded at all (Won't really work if another group of the mesh is using the mesh ) ***
          // HACK TO SPEED THINGS UP...
 #ifndef __ANDROID__
-         GLBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
+         /*GLBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
          if(primitive->nIndices < (mesh.nVertices >> 2) && !primitive->type.indices32bit)
          {
             int c;
@@ -3829,7 +4196,7 @@ class OpenGLDisplayDriver : DisplayDriver
             }
             glEnd();
          }
-         else
+         else*/
 #endif
          {
             OGLIndices oglIndices = primitive->data;
@@ -3837,13 +4204,21 @@ class OpenGLDisplayDriver : DisplayDriver
             if(!display.display3D.collectingHits && vboAvailable && oglIndices)
             {
                GLBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, oglIndices.buffer);
-               glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
-                  primitive->type.indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0);
+               if(primitive->type.indices32bit)
+                  glDrawElementsi(primitiveTypes[primitive->type.primitiveType], primitive->nIndices, 0);
+               else
+                  glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices, GL_UNSIGNED_SHORT, 0);
                GLBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
             }
             else
-               glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
-                  primitive->type.indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, oglIndices ? oglIndices.indices : primitive->indices);
+            {
+               if(primitive->type.indices32bit)
+                  glDrawElementsi(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
+                     oglIndices ? oglIndices.indices : primitive->indices);
+               else
+                  glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
+                     GL_UNSIGNED_SHORT, oglIndices ? oglIndices.indices : primitive->indices);
+            }
          }
       }
    }
@@ -3895,15 +4270,22 @@ public void UseSingleGLContext(bool useSingle)
    useSingleGLContext = useSingle;
 }
 
-default dllexport void * __attribute__((stdcall)) IS_GLGetContext(DisplaySystem displaySystem)
+default dllexport void *
+#if defined(__WIN32__)
+__attribute__((stdcall))
+#endif
+IS_GLGetContext(DisplaySystem displaySystem)
 {
-   void * context = null;
    if(displaySystem)
    {
-      OGLSystem system = displaySystem.driverData;
 #if defined(__WIN32__)
+      OGLSystem system = displaySystem.driverData;
       return system.glrc;
+#elif defined(__ANDROID__)
+      return eglContext;
+#elif defined(__EMSCRIPTEN__)
 #else
+      OGLSystem system = displaySystem.driverData;
       return system.glContext;
 #endif
    }