ecere/gfx/drivers/OpenGLDisplayDriver: Exporting access to nearPlane
[sdk] / ecere / src / gfx / drivers / OpenGLDisplayDriver.ec
index 3f14600..b70b67e 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
-
-#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>
+   #if !defined(__ANDROID__)
+
+      #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
 
-#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,36 +67,50 @@ 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>
 
 #else
 
-#include <GL/gl.h>
-#include <GL/glext.h>
+   #include <GL/gl.h>
+   #include <GL/glext.h>
 
 #endif
 
+#undef pointer
+
 import "Display"
 
 #if defined(__unix__) || defined(__APPLE__)
 
-#ifndef __ANDROID__
-import "XInterface"
-#endif
+   #ifndef __ANDROID__
+   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
@@ -144,395 +158,426 @@ import "XInterface"
 
 #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 
-
-/*
-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);
-*/
+   #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 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;
-
-#else
+   /*
+   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);
+   */
 
-#if defined(__ANDROID__)
+   /*
+   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;
 
-// 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_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;
+#endif
 
-   EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
-   eglInitialize(display, 0, 0);
-   eglChooseConfig(display, attribs, &config, 1, &numConfigs);
-   eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
+// Our own matrix stack
+static Matrix matrixStack[3][32];
+static int matrixIndex[3];
+static int curStack = 0;
 
-   surface = eglCreateWindowSurface(display, config, window, null);
-   context = eglCreateContext(display, config, null, null);
+#if defined(_GLES)
 
-   if(!eglMakeCurrent(display, surface, surface, context))
-      return false;
+   // OpenGL ES Porting Kit
+#if defined(__ANDROID__)
+   #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 GL_INT                                  0x1404
+   #define GL_UNSIGNED_INT                         0x1405
+   #define GL_DOUBLE                               0x140A
+   #define GL_POLYGON      9
+   #define GL_QUADS        7
+   #define APIENTRY
+#endif
 
-   eglQuerySurface(display, surface, EGL_WIDTH, &w);
-   eglQuerySurface(display, surface, EGL_HEIGHT, &h);
+   #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
 
-   eglDisplay = display;
-   eglContext = context;
-   eglSurface = surface;
-   eglWidth = w;
-   eglHeight = h;
+#if defined(__ANDROID__)
+   //#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
+#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);
+#else
 
-   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-   glEnable(GL_BLEND);
+#define glVertexPointerd(nc, s, p, nv)       glVertexPointer(nc, GL_DOUBLE, s, p)
+#define glDrawElementsi(type, count, start)  glDrawElements(type, count, GL_UNSIGNED_INT, start)
 
-   glMatrixMode(GL_MODELVIEW);
-   glScalef(1.0f, 1.0f, -1.0f);
-   glMatrixMode(GL_PROJECTION);
-   glShadeModel(GL_FLAT);
+#endif
 
-   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);
+#if defined(__ANDROID__)
+   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, 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;
 
-   glViewport(0,0,w,h);
-   glLoadIdentity();
-   glOrtho(0,w,h,0,0.0,1.0);
+      eglQuerySurface(display, surface, EGL_WIDTH, &w);
+      eglQuerySurface(display, surface, EGL_HEIGHT, &h);
 
-   currentVertexBuffer = 0;
-   return true;
-}  
+      eglDisplay = display;
+      eglContext = context;
+      eglSurface = surface;
+      eglWidth = w;
+      eglHeight = h;
 
-static void egl_term_display()
-{
-   if(stippleTexture)
-   {
-      glDeleteTextures(1, (int *)&stippleTexture);
-      stippleTexture = 0;
+      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;
@@ -544,7 +589,7 @@ static GLenum beginMode;
 static unsigned int beginBufferSize, normalBufferSize;
 static int numVertexCoords = 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);
@@ -554,7 +599,7 @@ void glesRecti(int a, int b, int c, int d)
    glEnd();
 }
 
-void glesBegin(GLenum mode)
+public void glesBegin(GLenum mode)
 {
    beginMode = mode;
    beginCount = 0;
@@ -567,7 +612,7 @@ void glesBegin(GLenum mode)
    }
 }
 
-void glesTexCoord2f(float x, float y)
+public void glesTexCoord2f(float x, float y)
 {
    int count = vertexCount;
 
@@ -590,12 +635,12 @@ void glesTexCoord2f(float x, float y)
       vertexPointer[count*(2+numVertexCoords)+1] = vertexPointer[(count-3)*(2+numVertexCoords)+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;
    if(vertexCount + 4 > beginBufferSize)
@@ -619,10 +664,10 @@ void glesVertex2f(float x, float y)
    }
    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;
@@ -654,7 +699,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)
    {
@@ -672,7 +717,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)
    {
@@ -690,32 +735,32 @@ 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 glesColor3f( float r, float g, float b )
 {
    glColor4f(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);
 }
 
-void glesColor4fv(float * a)
+public void glesColor4fv(float * a)
 {
    glColor4f(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;
@@ -731,7 +776,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;
@@ -747,7 +792,151 @@ void glesBufferDatai(int target, int size, void * data, int usage)
    glBufferData(target, numElems*sizeof(unsigned short), shortBDBuffer, usage);
 }
 
-/// *** These might require an implementation to get things working ***
+// *** Our Custom Matrix Stack ***
+
+static void LoadCurMatrix()
+{
+   double * i = matrixStack[curStack][matrixIndex[curStack]].array;
+   float m[16] =
+   {
+      (float)i[0],(float)i[1],(float)i[2],(float)i[3],
+      (float)i[4],(float)i[5],(float)i[6],(float)i[7],
+      (float)i[8],(float)i[9],(float)i[10],(float)i[11],
+      (float)i[12],(float)i[13],(float)i[14],(float)i[15]
+   };
+   glLoadMatrixf(m);
+}
+
+public void glesLoadIdentity()
+{
+   matrixStack[curStack][matrixIndex[curStack]].Identity();
+   LoadCurMatrix();
+}
+
+public void glesPushMatrix()
+{
+   if(matrixIndex[curStack] + 1 < sizeof(matrixStack[0]) / sizeof(Matrix))
+   {
+      matrixIndex[curStack]++;
+      memcpy(matrixStack[curStack][matrixIndex[curStack]].array, matrixStack[curStack][matrixIndex[curStack]-1].array, sizeof(Matrix));
+   }
+}
+
+public void glesPopMatrix()
+{
+   if(matrixIndex[curStack] > 0)
+   {
+      matrixIndex[curStack]--;
+      LoadCurMatrix();
+   }
+}
+
+public void glesLoadMatrixd(double * i)
+{
+   memcpy(matrixStack[curStack][matrixIndex[curStack]].array, i, sizeof(Matrix));
+   LoadCurMatrix();
+}
+
+public void glesOrtho( double l, double r, double b, double t, double n, double f )
+{
+   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
+   } };
+   Matrix res;
+   res.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
+   matrixStack[curStack][matrixIndex[curStack]] = res;
+   LoadCurMatrix();
+}
+
+public void glesFrustum( double l, double r, double b, double t, double n, double f )
+{
+   nearPlane = n;
+   n = 1;
+   l /= nearPlane;
+   r /= nearPlane;
+   b /= nearPlane;
+   t /= nearPlane;
+   f /= nearPlane;
+   {
+      double A = ((r + l) / (r - l));
+      double B = ((t + b) / (t - b));
+      double C = (-(f + n) / (f - n));
+      double D = (-2*f*n/(f-n));
+      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
+      } };
+      Matrix res;
+      res.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
+      matrixStack[curStack][matrixIndex[curStack]] = res;
+      LoadCurMatrix();
+   }
+}
+
+public void glesRotated( double a, double b, double c, double d )
+{
+   Quaternion q;
+   Matrix m, r;
+
+   q.RotationAxis({(float)b,(float)c,(float)-d}, a );
+   m.RotationQuaternion(q);
+   r.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
+   matrixStack[curStack][matrixIndex[curStack]] = r;
+   LoadCurMatrix();
+}
+public void glesScaled( double a, double b, double c )
+{
+   Matrix m, r;
+
+   m.Identity();
+   m.Scale(a,b,c);
+   r.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
+   matrixStack[curStack][matrixIndex[curStack]] = r;
+   LoadCurMatrix();
+}
+
+public void glesTranslated( double a, double b, double c )
+{
+   Matrix m, r;
+
+   m.Identity();
+   m.Translate(a,b,c);
+   r.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
+   matrixStack[curStack][matrixIndex[curStack]] = r;
+   LoadCurMatrix();
+}
+
+public void glesMultMatrixd( double * i )
+{
+   Matrix r;
+   r.Multiply((Matrix *)i, matrixStack[curStack][matrixIndex[curStack]]);
+   matrixStack[curStack][matrixIndex[curStack]] = r;
+   LoadCurMatrix();
+}
+
+public void glesMatrixMode(int mode)
+{
+   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 )
 {
    float m[16] =
@@ -764,7 +953,7 @@ void glesOrtho( double l, double r, double b, double t, double n, double f )
 {
    float matrix[4][4] =
    {
-      { (float)(2 / (r - l)), 0, 0, 0 }, 
+      { (float)(2 / (r - l)), 0, 0, 0 },
       { 0, (float)(2 / (t - b)), 0, 0 },
       { 0, 0, (float)(-2 / (f - n)), 0 },
       { (float)(-(r + l) / (r - l)), (float)(-(t + b) / (t - b)), (float)(-(f + n) / (f - n)), 1 }
@@ -772,6 +961,22 @@ void glesOrtho( double l, double r, double b, double t, double n, double f )
    glMultMatrixf((float *)matrix);
 }
 
+void glesFrustum( double l, double r, double b, double t, double n, double f )
+{
+   float A = (float)((r + l) / (r - l));
+   float B = (float)((t + b) / (t - b));
+   float C = (float)(-(f + n) / (f - n));
+   float D = (float)(-2*f*n/(f-n));
+   float matrix[4][4] =
+   {
+      { (float)(2*n / (r - l)), 0, 0, 0 },
+      { 0, (float)(2*n / (t - b)), 0, 0 },
+      { A, B,             C,-1 },
+      { 0, 0,             D, 0 }
+   };
+   glMultMatrixf((float *)matrix);
+}
+
 void glesRotated( double a, double b, double c, double d ) { glRotatef((float)a, (float)b, (float)c, (float)d); }
 void glesScaled( double a, double b, double c ) { glScalef((float)a, (float)b, (float)c); }
 void glesTranslated( double a, double b, double c ) { glTranslatef((float)a, (float)b, (float)c); }
@@ -787,9 +992,10 @@ void glesMultMatrixd( double * i )
    };
    glMultMatrixf(m);
 }
+*/
 
 // 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)
@@ -817,10 +1023,11 @@ void glesVertex3f( float x, float y, float z )
    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)
@@ -846,17 +1053,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;
@@ -869,13 +1077,14 @@ void glesTerminate()
    shortBDSize = 0;
 }
 
-static int stippleTexture;
+static GLuint stippleTexture;
+#if defined(_GLES)
 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++)
    {
@@ -899,28 +1108,13 @@ void glesLineStipple( int i, unsigned short j )
    glMatrixMode(GL_PROJECTION);
 }
 
-void glesFrustum( double l, double r, double b, double t, double n, double f )
-{
-   float A = (float)((r + l) / (r - l));
-   float B = (float)((t + b) / (t - b));
-   float C = (float)(-(f + n) / (f - n));
-   float D = (float)(-2*f*n/(f-n));
-   float matrix[4][4] =
-   {
-      { (float)(2*n / (r - l)), 0, 0, 0 },
-      { 0, (float)(2*n / (t - b)), 0, 0 },
-      { A, B,             C,-1 },
-      { 0, 0,             D, 0 }
-   };
-   glMultMatrixf((float *)matrix);
-}
-
-void glesLightModeli( unsigned int pname, int param )
+public void glesLightModeli( unsigned int pname, int param )
 {
    if(pname == GL_LIGHT_MODEL_TWO_SIDE)
       glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, param);
 }
 
+#ifdef __ANDROID__
 void glClearDepth( double depth ) { glClearDepthf((float)depth); }
 void glFogi( unsigned int pname, int param ) { }
 void glPolygonMode( unsigned int i, unsigned int j ) { }
@@ -942,34 +1136,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)
@@ -983,19 +1158,108 @@ bool GLSelectVBO(uint vbo)
    return false;
 }
 
+void GLGenBuffers(int count, uint * buffer)
+{
+#ifdef __ANDROID__
+   glGenBuffers(count, buffer);
+#else
+#if defined(__WIN32__)
+   if(glGenBuffersARB)
+#endif
+      glGenBuffersARB(count, buffer);
+#endif
+}
+
+void GLDeleteBuffers(int count, GLuint * buffer)
+{
+#ifdef __ANDROID__
+   glDeleteBuffers(count, buffer);
+#else
+#if defined(__WIN32__)
+   if(glDeleteBuffersARB)
+#endif
+      glDeleteBuffersARB(count, buffer);
+#endif
+}
+
 void GLBindBuffer(int target, uint buffer)
 {
 #ifdef __ANDROID__
    glBindBuffer(target, buffer);
 #else
-   glBindBufferARB(target, buffer);
+#if defined(__WIN32__)
+   if(glBindBufferARB)
+#endif
+      glBindBufferARB(target, buffer);
+#endif
+}
+
+public void GLVertexPointer(int numCoords, int glType, int stride, void *ptr, int numVertices)
+{
+#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, (void *)data, usage);
+   else if(type == GL_UNSIGNED_INT)
+      glesBufferDatai(target, size, (void *)data, usage);
+   else
+#endif
+
+#ifdef __ANDROID__
+      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
 
+static bool vboAvailable;
+
 static bool useSingleGLContext = false;
 class OGLDisplay : struct
 {
@@ -1012,7 +1276,7 @@ class OGLDisplay : struct
    int imageBuffers[2];
    byte * pboMemory1, * pboMemory2;
    */
-#else
+#elif !defined(__ANDROID__)
    GLXContext glContext;
 
    Pixmap pixmap;
@@ -1037,18 +1301,20 @@ class OGLDisplay : struct
 
 class OGLSystem : struct
 {
+   int maxTextureSize;
+   bool loadingFont;
+   bool pow2textures;
 #if defined(__WIN32__)
    PIXELFORMATDESCRIPTOR pfd;
    int format;
    HDC hdc;
    HGLRC glrc;
    HWND hwnd;
-#else
+#elif !defined(__ANDROID__)
    XVisualInfo * visualInfo;
    GLXContext glContext;
    GLXDrawable glxDrawable;
 #endif
-   bool loadingFont;
 };
 
 class OGLSurface : struct
@@ -1063,27 +1329,20 @@ class OGLSurface : struct
 
 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;
 
@@ -1093,6 +1352,7 @@ class OpenGLDisplayDriver : DisplayDriver
 
    bool LockSystem(DisplaySystem displaySystem)
    {
+#if !defined(__ANDROID__)
       OGLSystem oglSystem = displaySystem.driverData;
       if(useSingleGLContext) return true;
    #if defined(__WIN32__)
@@ -1100,11 +1360,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;
    }
 
@@ -1125,20 +1384,17 @@ class OpenGLDisplayDriver : DisplayDriver
 
    bool Lock(Display display)
    {
+#if !defined(__ANDROID__)
       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, (int)display.window, oglDisplay.glContext);
-      #endif
+      glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
    #endif
+#endif
       return true;
    }
 
@@ -1160,20 +1416,20 @@ class OpenGLDisplayDriver : DisplayDriver
    #if defined(__WIN32__)
          wglMakeCurrent( null, null );
 
-         if(oglDisplay.glrc) 
+         if(oglDisplay.glrc)
             wglDeleteContext(oglDisplay.glrc);
-      
+
          if(oglDisplay.hdc && oglDisplay.pBuffer)
             wglReleasePbufferDCARB(oglDisplay.pBuffer, oglDisplay.hdc);
 
          if(oglDisplay.pBuffer)
             wglDestroyPbufferARB(oglDisplay.pBuffer);
 
-         if(oglDisplay.hdc) 
+         if(oglDisplay.hdc)
             ReleaseDC(display.window, oglDisplay.hdc);
 
          if(oglDisplay.memDC) DeleteDC(oglDisplay.memDC);
-         if(oglDisplay.memBitmap) DeleteObject(oglDisplay.memBitmap); 
+         if(oglDisplay.memBitmap) DeleteObject(oglDisplay.memBitmap);
 
    #elif defined(__unix__) || defined(__APPLE__)
       #if defined(__ANDROID__)
@@ -1200,14 +1456,14 @@ class OpenGLDisplayDriver : DisplayDriver
                if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
                   shmdt(oglDisplay.shminfoShape.shmaddr);
                shmctl(oglDisplay.shminfoShape.shmid, IPC_RMID, 0);
-            }                  
+            }
             XDestroyImage(oglDisplay.shapeImage);
             oglDisplay.shapeImage = None;
          }
 
          glXMakeCurrent(xGlobalDisplay, None, null);
-      
-         if(oglDisplay.glContext) 
+
+         if(oglDisplay.glContext)
             glXDestroyContext(xGlobalDisplay, oglDisplay.glContext);
       #endif
    #endif
@@ -1217,6 +1473,14 @@ class OpenGLDisplayDriver : DisplayDriver
       }
    }
 
+   void ::CheckExtensions(OGLSystem oglSystem)
+   {
+      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);
+   }
+
    bool CreateDisplaySystem(DisplaySystem displaySystem)
    {
       bool result = false;
@@ -1228,7 +1492,7 @@ class OpenGLDisplayDriver : DisplayDriver
       oglSystem.hdc = GetDC(oglSystem.hwnd);
       if(oglSystem.hdc)
       {
-          
+
          oglSystem.pfd.nSize = (short)sizeof(oglSystem.pfd);
          oglSystem.pfd.nVersion = 1;
          oglSystem.pfd.dwFlags = PFD_DRAW_TO_WINDOW /*PFD_DRAW_TO_BITMAP*/ | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
@@ -1275,6 +1539,8 @@ class OpenGLDisplayDriver : DisplayDriver
 
                wglSwapIntervalEXT = (void *)wglGetProcAddress("wglSwapIntervalEXT");
 
+               vboAvailable = glBindBufferARB != null;
+
                // eSystem_LoggingMode(LOG_MSGBOX, null);
 
                if(wglChoosePixelFormatARB)
@@ -1284,7 +1550,7 @@ class OpenGLDisplayDriver : DisplayDriver
                    int numFormats;
                    float fAttributes[] = {0,0};
                    int iAttributes[] =
-                  { 
+                  {
                      WGL_DRAW_TO_WINDOW_ARB,GL_TRUE,
                            WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
                            WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
@@ -1334,6 +1600,8 @@ class OpenGLDisplayDriver : DisplayDriver
 
                result = true;
 
+               CheckExtensions(oglSystem);
+
                wglMakeCurrent(null, null);
 
                //eSystem_DumpErrors(true);
@@ -1341,8 +1609,10 @@ class OpenGLDisplayDriver : DisplayDriver
          }
       }
    #elif defined(__unix__) || defined(__APPLE__)
+      vboAvailable = true;
       #if defined(__ANDROID__)
          egl_init_display(guiApp.desktop.windowHandle);
+         CheckExtensions(oglSystem);
          result = true;
       #else
       {
@@ -1377,6 +1647,7 @@ class OpenGLDisplayDriver : DisplayDriver
          {
             glXMakeCurrent(xGlobalDisplay, oglSystem.glxDrawable, oglSystem.glContext);
             // Setup Extensions
+            CheckExtensions(oglSystem);
             glXMakeCurrent(xGlobalDisplay, None, null);
             result = true;
          }
@@ -1397,10 +1668,10 @@ class OpenGLDisplayDriver : DisplayDriver
    #if defined(__WIN32__)
       wglMakeCurrent( null, null );
 
-      if(oglSystem.glrc) 
+      if(oglSystem.glrc)
          wglDeleteContext(oglSystem.glrc);
-      
-      if(oglSystem.hdc) 
+
+      if(oglSystem.hdc)
          ReleaseDC(oglSystem.hwnd, oglSystem.hdc);
       DestroyWindow(oglSystem.hwnd);
 
@@ -1431,7 +1702,9 @@ class OpenGLDisplayDriver : DisplayDriver
    {
       bool result = false;
       OGLDisplay oglDisplay = display.driverData;
+#if !defined(__ANDROID__)
       OGLSystem oglSystem = display.displaySystem.driverData;
+#endif
       if(!oglDisplay)
          oglDisplay = display.driverData = OGLDisplay { };
       //printf("Inside CreateDisplay\n");
@@ -1484,7 +1757,7 @@ class OpenGLDisplayDriver : DisplayDriver
          if(oglDisplay.glContext)
          {
             //printf("CreateDisplay Got a Context\n");
-            glXMakeCurrent(xGlobalDisplay, (int)display.window, oglDisplay.glContext);
+            glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
             result = true;
          }
       #endif
@@ -1496,16 +1769,22 @@ 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);
-         glScalef(1.0f, 1.0f, -1.0f);
+         glScaled(1.0, 1.0, -1.0);
          // glTranslatef(0.375f, 0.375f, 0.0f);
          // glTranslatef(-0.625f, -0.625f, 0.0f);
          glMatrixMode(GL_PROJECTION);
@@ -1543,12 +1822,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__)
@@ -1556,7 +1835,7 @@ class OpenGLDisplayDriver : DisplayDriver
          {
             /*WGL_TEXTURE_FORMAT_ARB, WGL_TEXTURE_RGBA_ARB,
             WGL_TEXTURE_TARGET_ARB, WGL_TEXTURE_2D_ARB, */0
-         }; 
+         };
          int pixelFormat = 0;
          if(wglChoosePixelFormatARB)
          {
@@ -1564,7 +1843,7 @@ class OpenGLDisplayDriver : DisplayDriver
             int numFormats;
             float fAttributes[] = {0,0};
             int iAttributes[] =
-            { 
+            {
                //WGL_DRAW_TO_BITMAP_ARB, GL_TRUE,
                WGL_DRAW_TO_PBUFFER_ARB,GL_TRUE,
                    WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
@@ -1578,7 +1857,7 @@ class OpenGLDisplayDriver : DisplayDriver
                    WGL_SAMPLES_ARB, 4,                                         // Check For 4x Multisampling
                    0,0
             };
-            
+
             //Log("Found wglChoosePixelFormatARB\n");
 
             valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
@@ -1613,7 +1892,7 @@ class OpenGLDisplayDriver : DisplayDriver
             {
                wglMakeCurrent(null, null);
             }
-         }  
+         }
 
          wglMakeCurrent( null, null );
          wglMakeCurrent( oglDisplay.hdc, oglDisplay.glrc );
@@ -1624,8 +1903,8 @@ class OpenGLDisplayDriver : DisplayDriver
 
          if(!useSingleGLContext)
             wglMakeCurrent( null, null );
-           
-         if(oglDisplay.glrc) 
+
+         if(oglDisplay.glrc)
             wglDeleteContext(oglDisplay.glrc);
 
          oglDisplay.pBuffer = wglCreatePbufferARB(oglSystem.hdc, pixelFormat, width, height, attributes);
@@ -1639,8 +1918,8 @@ class OpenGLDisplayDriver : DisplayDriver
             wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
 
             //wglQueryPbufferARB(pBuffer, WGL_PBUFFER_WIDTH_ARB, &width);
-            //wglQueryPbufferARB(pBuffer, WGL_PBUFFER_HEIGHT_ARB, &height); 
-            
+            //wglQueryPbufferARB(pBuffer, WGL_PBUFFER_HEIGHT_ARB, &height);
+
             // glDeleteBuffersARB(2, oglDisplay.imageBuffers);
 
             if((info = (BITMAPINFO *)new0 byte[sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256]))
@@ -1770,7 +2049,7 @@ class OpenGLDisplayDriver : DisplayDriver
                   if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
                      shmdt(oglDisplay.shminfoShape.shmaddr);
                   shmctl(oglDisplay.shminfoShape.shmid, IPC_RMID, 0);
-               }                  
+               }
                XDestroyImage(oglDisplay.shapeImage);
                oglDisplay.shapeImage = None;
             }
@@ -1795,14 +2074,14 @@ class OpenGLDisplayDriver : DisplayDriver
                if(oglDisplay.glContext)
                {
                   glXMakeCurrent(xGlobalDisplay, None, null);
-                  glXMakeCurrent(xGlobalDisplay, (int)display.window, oglDisplay.glContext);
+                  glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
 
                   // Initialize Shared Memory Pixmap
-                  oglDisplay.image = XShmCreateImage(xGlobalDisplay, DefaultVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 32, 
+                  oglDisplay.image = XShmCreateImage(xGlobalDisplay, DefaultVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 32,
                      ZPixmap, null, &oglDisplay.shminfo, width, height);
                   if(oglDisplay.image)
                   {
-                     oglDisplay.shminfo.shmid = shmget(IPC_PRIVATE, 
+                     oglDisplay.shminfo.shmid = shmget(IPC_PRIVATE,
                         oglDisplay.image->bytes_per_line * oglDisplay.image->height, IPC_CREAT|0777);
                      if(oglDisplay.shminfo.shmid != -1)
                      {
@@ -1812,15 +2091,15 @@ class OpenGLDisplayDriver : DisplayDriver
                            oglDisplay.shminfo.readOnly = False;
                            if(XShmAttach(xGlobalDisplay, &oglDisplay.shminfo))
                            {
-                              oglDisplay.pixmap = XShmCreatePixmap(xGlobalDisplay, (X11Window)display.window, oglDisplay.shminfo.shmaddr, 
+                              oglDisplay.pixmap = XShmCreatePixmap(xGlobalDisplay, (X11Window)display.window, oglDisplay.shminfo.shmaddr,
                                  &oglDisplay.shminfo, width, height, 32);
 
                               // Initialize Shared Memory Shape Pixmap
-                              oglDisplay.shapeImage = XShmCreateImage(xGlobalDisplay, DefaultVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 1, 
+                              oglDisplay.shapeImage = XShmCreateImage(xGlobalDisplay, DefaultVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 1,
                                  ZPixmap, null, &oglDisplay.shminfoShape, width, height);
                               if(oglDisplay.shapeImage)
                               {
-                                 oglDisplay.shminfoShape.shmid = shmget(IPC_PRIVATE, 
+                                 oglDisplay.shminfoShape.shmid = shmget(IPC_PRIVATE,
                                     oglDisplay.shapeImage->bytes_per_line * oglDisplay.shapeImage->height, IPC_CREAT|0777);
                                  if(oglDisplay.shminfoShape.shmid != -1)
                                  {
@@ -1830,7 +2109,7 @@ class OpenGLDisplayDriver : DisplayDriver
                                        oglDisplay.shminfoShape.readOnly = False;
                                        if(XShmAttach(xGlobalDisplay, &oglDisplay.shminfoShape))
                                        {
-                                          oglDisplay.shapePixmap = XShmCreatePixmap(xGlobalDisplay, (X11Window)display.window, oglDisplay.shminfoShape.shmaddr, 
+                                          oglDisplay.shapePixmap = XShmCreatePixmap(xGlobalDisplay, (X11Window)display.window, oglDisplay.shminfoShape.shmaddr,
                                              &oglDisplay.shminfoShape, width, height, 1);
                                           //oglDisplay.shapePixmap = XCreatePixmap(xGlobalDisplay, (X11Window)display.window, width, height, 1);
 
@@ -1844,9 +2123,9 @@ class OpenGLDisplayDriver : DisplayDriver
                                              #endif
                                              oglDisplay.pixmapPicture = XRenderCreatePicture(xGlobalDisplay, oglDisplay.pixmap, format, CPRepeat, &attributes);
                                              oglDisplay.windowPicture = XRenderCreatePicture(xGlobalDisplay, (X11Window)display.window, format, 0, &attributes);
-                                             oglDisplay.shapePicture = XRenderCreatePicture(xGlobalDisplay, oglDisplay.shapePixmap, 
+                                             oglDisplay.shapePicture = XRenderCreatePicture(xGlobalDisplay, oglDisplay.shapePixmap,
                                                 XRenderFindStandardFormat(xGlobalDisplay, PictStandardA1), 0, &attributes);
-                                          }  
+                                          }
 
                                           oglDisplay.picture = oglDisplay.shminfo.shmaddr;
                                           oglDisplay.stride = oglDisplay.image->bytes_per_line / 4;
@@ -1874,7 +2153,7 @@ class OpenGLDisplayDriver : DisplayDriver
          width = eglWidth;
          height = eglHeight;
       #else
-         glXMakeCurrent(xGlobalDisplay, (int)display.window, oglDisplay.glContext);
+         glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
       #endif
 #endif
       }
@@ -1887,8 +2166,8 @@ class OpenGLDisplayDriver : DisplayDriver
       }
       if(!result)
          return false;
-         
-      result = false;           
+
+      result = false;
 
       glViewport(0,0,width,height);
       glLoadIdentity();
@@ -1900,11 +2179,15 @@ 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;
-         
+
       return result;
    }
 
@@ -1938,7 +2221,9 @@ class OpenGLDisplayDriver : DisplayDriver
 
    void Update(Display display, Box updateBox)
    {
+#if defined(__WIN32__) || defined(USEPBUFFER)
       OGLDisplay oglDisplay = display.driverData;
+#endif
       //Logf("DisplayScreen\n");
 
       glFlush();
@@ -1971,15 +2256,15 @@ class OpenGLDisplayDriver : DisplayDriver
             // outstanding DMA transfers into the buffer to finish.
             glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
             oglDisplay.pboMemory1 = (byte *)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
-           
+
             // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
             // oglDisplay.pboMemory2 = (byte *)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB,GL_READ_ONLY);
 
-           
+
             memcpy(oglDisplay.picture, oglDisplay.pboMemory1, display.width * display.height * 4);
             //memcpy(oglDisplay.picture + display.width * display.height * 4 / 2, oglDisplay.pboMemory2, display.width * display.height * 4/ 2);
             */
-            
+
             UpdateLayeredWindow(display.window, hdc, &point, &size, oglDisplay.memDC, &srcPoint, 0, &blend, ULW_ALPHA);
             /*
 
@@ -1989,7 +2274,7 @@ class OpenGLDisplayDriver : DisplayDriver
 
             // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
             // glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
-           
+
             // Bind two different buffer objects and start the glReadPixels
             // asynchronously. Each call will return directly after
             // starting the DMA transfer.
@@ -2004,7 +2289,7 @@ class OpenGLDisplayDriver : DisplayDriver
 #elif defined(__unix__) || defined(__APPLE__)
       #if defined(__ANDROID__)
       #else
-            XTransform transform = 
+            XTransform transform =
             {
                {
                   { (int)(1.0f * (1<<16)), (int)(0.0f * (1<<16)),  (int)(0 * (1 << 16)) },
@@ -2024,18 +2309,18 @@ class OpenGLDisplayDriver : DisplayDriver
      #endif
 #endif
          }
-      } 
+      }
       else
 #endif
       {
 #if defined(__WIN32__)
-         //wglSwapLayerBuffers(oglDisplay.hdc,WGL_SWAP_MAIN_PLANE); 
+         //wglSwapLayerBuffers(oglDisplay.hdc,WGL_SWAP_MAIN_PLANE);
          SwapBuffers(oglDisplay.hdc);
 #elif defined(__unix__) || defined(__APPLE__)
       #if defined(__ANDROID__)
          eglSwapBuffers(eglDisplay, eglSurface);
       #else
-         glXSwapBuffers(xGlobalDisplay, (int)display.window);
+         glXSwapBuffers(xGlobalDisplay, (GLXDrawable)display.window);
       #endif
 #endif
       }
@@ -2044,21 +2329,30 @@ class OpenGLDisplayDriver : DisplayDriver
 
    void FreeBitmap(DisplaySystem displaySystem, Bitmap bitmap)
    {
-      glDeleteTextures(1, (int *)&bitmap.driverData);
-      bitmap.driverData = 0;
-
+      if(bitmap.driverData)
+      {
+         GLuint tex = (GLuint)(uintptr)bitmap.driverData;
+         glDeleteTextures(1, &tex);
+         bitmap.driverData = 0;
+      }
       bitmap.driver = ((subclass(DisplayDriver))class(LFBDisplayDriver));
    }
 
    bool AllocateBitmap(DisplaySystem displaySystem, Bitmap bitmap, int width, int height, int stride, PixelFormat format, bool allocatePalette)
    {
+      OGLSystem oglSystem = displaySystem.driverData;
       bool result = false;
       Bitmap mipMap { };
-      int glBitmap = -1;
-      
-      uint w = pow2i(Min(width, 1024)), h = pow2i(Min(height, 1024));
-      //uint w = pow2i(Min(width, 2048)), h = pow2i(Min(height, 2048));
-      //uint w = pow2i(Min(width, 512)), h = pow2i(Min(height, 512));
+      GLuint glBitmap = 0;
+
+      uint w = width, h = height;
+      if(oglSystem.pow2textures)
+      {
+         w = pow2i(w);
+         h = pow2i(h);
+      }
+      w = Min(w, oglSystem.maxTextureSize);
+      h = Min(h, oglSystem.maxTextureSize);
 
       glGenTextures(1, &glBitmap);
       glBindTexture(GL_TEXTURE_2D, glBitmap);
@@ -2080,7 +2374,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;
@@ -2098,9 +2392,21 @@ class OpenGLDisplayDriver : DisplayDriver
       if(/*bitmap.pixelFormat == pixelFormatRGBA || */bitmap.Convert(null, pixelFormat888, null))
       {
          int c, level;
-         uint w = pow2i(Min(bitmap.width, 1024)), h = pow2i(Min(bitmap.height, 1024));
-         //uint w = pow2i(Min(bitmap.width, 512)), h = pow2i(Min(bitmap.height, 512));
-         int glBitmap = -1;
+         uint w = bitmap.width, h = bitmap.height;
+         GLuint glBitmap = 0;
+         if(oglSystem.pow2textures)
+         {
+            w = pow2i(w);
+            h = pow2i(h);
+         }
+         w = Min(w, oglSystem.maxTextureSize);
+         h = Min(h, oglSystem.maxTextureSize);
+
+         if(mipMaps)
+         {
+            while(w * 2 < h) w *= 2;
+            while(h * 2 < w) h *= 2;
+         }
 
          // Switch ARGB to RGBA
          //if(bitmap.format != pixelFormatRGBA)
@@ -2108,7 +2414,7 @@ class OpenGLDisplayDriver : DisplayDriver
             for(c=0; c<bitmap.size; c++)
             {
                // ((ColorRGBA *)bitmap.picture)[c] = ((ColorAlpha *)bitmap.picture)[c];
-               // TODO: 
+               // TODO:
                ColorAlpha color = ((ColorAlpha *)bitmap.picture)[c];
                ((ColorRGBA *)bitmap.picture)[c] = ColorRGBA { color.color.r, color.color.g, color.color.b, color.a };
             }
@@ -2117,18 +2423,18 @@ class OpenGLDisplayDriver : DisplayDriver
 
          glGetError();
          glGenTextures(1, &glBitmap);
-         if(glBitmap == -1)
+         if(glBitmap == 0)
          {
-            int error = glGetError();
+            //int error = glGetError();
             return false;
-            //Print("");
          }
 
          glBindTexture(GL_TEXTURE_2D, glBitmap);
          glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
 
-         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
-         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mipMaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
+         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mipMaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
+         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+
          //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 
          //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
@@ -2141,9 +2447,11 @@ class OpenGLDisplayDriver : DisplayDriver
 
          result = true;
 
-         for(level = 0; result && (w > 1 || h > 1); level++, w >>= 1, h >>= 1)
+         for(level = 0; result && (w >= 1 || h >= 1); level++, w >>= 1, h >>= 1)
          {
             Bitmap mipMap;
+            if(!w) w = 1;
+            if(!h) h = 1;
             if(bitmap.width != w || bitmap.height != h)
             {
                mipMap = Bitmap { };
@@ -2159,7 +2467,7 @@ class OpenGLDisplayDriver : DisplayDriver
                   delete mipMap;
                }
             }
-            else 
+            else
                mipMap = bitmap;
 
             if(result)
@@ -2179,14 +2487,14 @@ class OpenGLDisplayDriver : DisplayDriver
                   result = false;
                }
             }
-            if(mipMap != bitmap) 
+            if(mipMap != bitmap)
                delete mipMap;
             if(!mipMaps) break;
          }
 
          if(!bitmap.keepData)
             bitmap.driver.FreeBitmap(bitmap.displaySystem, bitmap);
-         bitmap.driverData = (void *)glBitmap;
+         bitmap.driverData = (void *)(uintptr)glBitmap;
          bitmap.driver = displaySystem.driver;
 
          if(!result)
@@ -2280,7 +2588,7 @@ class OpenGLDisplayDriver : DisplayDriver
       bool result = false;
       OGLDisplay oglDisplay = display.driverData;
       ColorAlpha * flippingBuffer = oglDisplay.flippingBuffer;
-      
+
       if(oglDisplay.flippingBuffer)
       {
          if(bitmap.pixelFormat != pixelFormat888 || bitmap.width < w || bitmap.height < h)
@@ -2360,15 +2668,28 @@ class OpenGLDisplayDriver : DisplayDriver
       glBegin(GL_POINTS);
       // glVertex2i(x+surface.offset.x, y+surface.offset.y);
       glVertex2f(x+surface.offset.x + 0.5f, y+surface.offset.y + 0.5f);
-       
+
       glEnd();
    }
 
-   void DrawLine(Display display, Surface surface, int x1, int y1, int x2, int y2)
+   void DrawLine(Display display, Surface surface, int _x1, int _y1, int _x2, int _y2)
    {
       OGLSurface oglSurface = surface.driverData;
-      if(x1 == x2) { y2++; y1--; }
-      else if(y1 == y2) { x2++; x1--; }
+      float x1 = _x1, x2 = _x2, y1 = _y1, y2 = _y2;
+      if(_x1 == _x2)
+      {
+         if(_y2 >= _y1)
+            y2 += 1;
+         else
+            y1 += 1;
+      }
+      else if(_y1 == _y2)
+      {
+         if(_x2 >= _x1)
+            x2 += 1;
+         else
+            x1 += 1;
+      }
       x1 += surface.offset.x;
       y1 += surface.offset.y;
       x2 += surface.offset.x;
@@ -2378,7 +2699,7 @@ class OpenGLDisplayDriver : DisplayDriver
 
       glColor4fv(oglSurface.foreground);
       glBegin(GL_LINES);
-#ifdef __ANDROID__
+#ifdef _GLES
       if(stippleEnabled)
       {
          glTexCoord2f(0.5f, 0);
@@ -2396,7 +2717,7 @@ class OpenGLDisplayDriver : DisplayDriver
          glVertex2f(x1 + 0.5f, y1 + 0.5f);
          glVertex2f(x2 + 0.5f, y2 + 0.5f);
       }
-      
+
       glEnd();
    }
 
@@ -2411,7 +2732,7 @@ class OpenGLDisplayDriver : DisplayDriver
       //Logf("Rectangle\n");
 
       glColor4fv(oglSurface.foreground);
-#ifdef __ANDROID__
+#ifdef _GLES
       if(stippleEnabled)
       {
          glBegin(GL_LINES);
@@ -2462,7 +2783,7 @@ class OpenGLDisplayDriver : DisplayDriver
       glColor4fv(oglSurface.background);
       glRecti(x1+surface.offset.x, y1+surface.offset.y,
               x2+surface.offset.x + 1, y2+surface.offset.y + 1);
-              
+
       /*
       glRectf(x1+surface.offset.x, y1+surface.offset.y,
               x2+surface.offset.x + 1, y2+surface.offset.y + 1);
@@ -2511,9 +2832,9 @@ class OpenGLDisplayDriver : DisplayDriver
          glColor4fv(oglSurface.bitmapMult);
       }
       else if(oglSurface.xOffset)
-         glTranslatef(oglSurface.xOffset / 64.0f/*-0.375f*/, 0.0f, 0.0f);
-         
-      glBindTexture(GL_TEXTURE_2D, (uint)bitmap.driverData);
+         glTranslated(oglSurface.xOffset / 64.0/*-0.375*/, 0.0, 0.0);
+
+      glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)bitmap.driverData);
       glBegin(GL_QUADS);
 
       if(h < 0)
@@ -2555,10 +2876,10 @@ class OpenGLDisplayDriver : DisplayDriver
       {
          glDisable(GL_TEXTURE_2D);
 
-         //glTranslatef(0.375f, 0.375f, 0.0f);
+         //glTranslate(0.375, 0.375, 0.0);
       }
       else if(oglSurface.xOffset)
-         glTranslatef(-oglSurface.xOffset / 64.0f/*+0.375f*/, 0.0f, 0.0f);
+         glTranslated(-oglSurface.xOffset / 64.0/*+0.375*/, 0.0, 0.0);
 
 #if !defined(__OLDX__)
          /*if(glBlendFuncSeparate && !oglSurface.writingText)
@@ -2570,7 +2891,7 @@ class OpenGLDisplayDriver : DisplayDriver
    {
       OGLSurface oglSurface = surface.driverData;
 
-      //glTranslatef(-0.375f, -0.375f, 0.0f);
+      //glTranslate(-0.375, -0.375, 0.0);
 
       //Logf("Stretch\n");
 
@@ -2580,7 +2901,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);
 
@@ -2590,13 +2911,13 @@ class OpenGLDisplayDriver : DisplayDriver
       {
          glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
          glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
-      
+
          glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
          glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
-      
+
          glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
          glVertex2i(dx+w+surface.offset.x, dy-h+surface.offset.y);
-      
+
          glTexCoord2f((float)(sx) / bitmap.width, (float)(sy)/ bitmap.height);
          glVertex2i(dx+surface.offset.x, dy-h+surface.offset.y);
       }
@@ -2604,13 +2925,13 @@ class OpenGLDisplayDriver : DisplayDriver
       {
          glTexCoord2f((float)(sx) / bitmap.width, (float)(sy)/ bitmap.height);
          glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
-      
+
          glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
          glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
-      
+
          glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
          glVertex2i(dx+w+surface.offset.x, dy+h+surface.offset.y);
-      
+
          glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
          glVertex2i(dx+surface.offset.x, dy+h+surface.offset.y);
       }
@@ -2619,7 +2940,7 @@ class OpenGLDisplayDriver : DisplayDriver
 
       glDisable(GL_TEXTURE_2D);
 
-      //glTranslatef(0.375f, 0.375f, 0.0f);
+      //glTranslate(0.375, 0.375, 0.0);
 #if !defined(__OLDX__)
       /*if(glBlendFuncSeparate)
          glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
@@ -2635,7 +2956,7 @@ 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)
    {
       float s2dw,s2dh,d2sw,d2sh;
-      bool flipX = false, flipY = false;
+      //bool flipX = false, flipY = false;
 
       //Logf("StretchDI\n");
 
@@ -2643,13 +2964,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;
@@ -2686,7 +3007,7 @@ class OpenGLDisplayDriver : DisplayDriver
       //Clip against the edges of the surfaceination
       if(dx<surface.box.left)
       {
-         //if(!flip) 
+         //if(!flip)
          sx+=(int)((surface.box.left-dx)*d2sw);
          sw-=(int)((surface.box.left-dx)*d2sw);
          w-=surface.box.left-dx;
@@ -2727,6 +3048,8 @@ class OpenGLDisplayDriver : DisplayDriver
          glDrawPixels(sw,sh,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
          glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
          glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
+         glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
+         glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
       }
    }
 
@@ -2754,7 +3077,7 @@ class OpenGLDisplayDriver : DisplayDriver
       //Clip against the edges of the surfaceination
       if(dx<surface.box.left)
       {
-         //if(!flip) 
+         //if(!flip)
          sx+=surface.box.left-dx;
          w-=surface.box.left-dx;
          dx=surface.box.left;
@@ -2789,6 +3112,8 @@ class OpenGLDisplayDriver : DisplayDriver
          glDrawPixels(w,h,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
          glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
          glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
+         glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
+         glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
       }
    }
 
@@ -2802,7 +3127,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;
@@ -2811,18 +3136,18 @@ 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;
       oglSystem.loadingFont = true;
 
-      //glTranslatef(-0.375f, -0.375f, 0.0f);
+      //glTranslated(-0.375, -0.375, 0.0);
 
       //Logf("Blit\n");
 
@@ -2844,7 +3169,7 @@ class OpenGLDisplayDriver : DisplayDriver
 
       glDisable(GL_TEXTURE_2D);
 
-      //glTranslatef(0.375f, 0.375f, 0.0f);
+      //glTranslated(0.375, 0.375, 0.0);
    }
 
    void TextFont(Display display, Surface surface, Font font)
@@ -2858,7 +3183,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;
@@ -2878,7 +3203,7 @@ class OpenGLDisplayDriver : DisplayDriver
 
       if(stipple)
       {
-#if defined(__ANDROID__)
+#if defined(_GLES)
          stippleEnabled = true;
          glesLineStipple(1, (uint16)stipple);
 #else
@@ -2888,7 +3213,7 @@ class OpenGLDisplayDriver : DisplayDriver
       }
       else
       {
-#if defined(__ANDROID__)
+#if defined(_GLES)
          stippleEnabled = false;
          glMatrixMode(GL_TEXTURE);
          glLoadIdentity();
@@ -2914,14 +3239,16 @@ 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);
             break;
          case depthWrite:
             if(value) glDepthMask((byte)bool::true); else glDepthMask((byte)bool::false);
-            oglDisplay.depthWrite = value;
+            oglDisplay.depthWrite = (bool)value;
             break;
          case fogColor:
          {
@@ -2930,7 +3257,7 @@ class OpenGLDisplayDriver : DisplayDriver
             break;
          }
          case fogDensity:
-            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);
@@ -2979,7 +3306,7 @@ class OpenGLDisplayDriver : DisplayDriver
          color[1] = light.diffuse.g * light.multiplier;
          color[2] = light.diffuse.b * light.multiplier;
          glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, color);
-         
+
          color[0] = light.ambient.r * light.multiplier;
          color[1] = light.ambient.g * light.multiplier;
          color[2] = light.ambient.b * light.multiplier;
@@ -2988,7 +3315,7 @@ class OpenGLDisplayDriver : DisplayDriver
          color[1] = light.specular.g * light.multiplier;
          color[2] = light.specular.b * light.multiplier;
          glLightfv(GL_LIGHT0 + id, GL_SPECULAR,color);
-         
+
          if(lightObject)
          {
             Vector3D positionVector;
@@ -3026,7 +3353,7 @@ class OpenGLDisplayDriver : DisplayDriver
             position[0] = (float)positionVector.x;
             position[1] = (float)positionVector.y;
             position[2] = (float)positionVector.z;
-            
+
             glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
 
             /*
@@ -3040,7 +3367,7 @@ class OpenGLDisplayDriver : DisplayDriver
             glEnd();
             glEnable(GL_DEPTH_TEST);
             glEnable(GL_LIGHTING);
-                     
+
 
             // Display Target
             if(lightObject.flags.root || !lightObject.parent)
@@ -3050,7 +3377,7 @@ class OpenGLDisplayDriver : DisplayDriver
             }
             else
             {
-               positionVector.MultMatrix(light.target.transform.position, 
+               positionVector.MultMatrix(light.target.transform.position,
                   lightObject.light.target.parent.matrix);
                positionVector.Subtract(positionVector, display.camera.cPosition);
             }
@@ -3092,7 +3419,7 @@ class OpenGLDisplayDriver : DisplayDriver
          }
          else
          {
-            
+
             Vector3Df vector { 0,0,-1 };
             Vector3Df direction;
             Matrix mat;
@@ -3141,7 +3468,7 @@ class OpenGLDisplayDriver : DisplayDriver
          {
             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,
@@ -3170,7 +3497,8 @@ class OpenGLDisplayDriver : DisplayDriver
             glPushMatrix();
 
          glLoadIdentity();
-         glScalef(1.0f, 1.0f, -1.0f);
+
+         glScaled(1.0/nearPlane, 1.0/nearPlane, -1.0/nearPlane);
 
          // *** View Matrix ***
          glMultMatrixd(camera.viewMatrix.array);
@@ -3208,8 +3536,7 @@ class OpenGLDisplayDriver : DisplayDriver
          glPopMatrix();
       }
 
-      if(glBindBufferARB)
-         glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
+      GLBindBuffer(GL_ARRAY_BUFFER_ARB, 0);
    }
 
    void ApplyMaterial(Display display, Material material, Mesh mesh)
@@ -3235,11 +3562,17 @@ 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();
+         if(material.uScale && material.vScale)
+            glScalef(material.uScale, material.vScale, 1);
+         glMatrixMode(GL_MODELVIEW);
 
          if(material.flags.tile)
          {
@@ -3272,7 +3605,7 @@ class OpenGLDisplayDriver : DisplayDriver
             glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color);
          }
       }
-      {   
+      {
          float color[4] = { material.specular.r, material.specular.g, material.specular.b, 0 };
          glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, color);
       }
@@ -3280,7 +3613,7 @@ class OpenGLDisplayDriver : DisplayDriver
          float color[4] = { material.emissive.r, material.emissive.g, material.emissive.b, 0 };
          glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, color);
       }
-      
+
       glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &material.power);
    }
 
@@ -3293,7 +3626,7 @@ class OpenGLDisplayDriver : DisplayDriver
          {
             if(oglMesh.vertices)
             {
-               glDeleteBuffersARB(1, &oglMesh.vertices);
+               GLDeleteBuffers(1, &oglMesh.vertices);
                oglMesh.vertices = 0;
             }
             delete mesh.vertices;
@@ -3302,7 +3635,7 @@ class OpenGLDisplayDriver : DisplayDriver
          {
             if(oglMesh.normals)
             {
-               glDeleteBuffersARB(1, &oglMesh.normals);
+               GLDeleteBuffers(1, &oglMesh.normals);
                oglMesh.normals = 0;
             }
             delete mesh.normals;
@@ -3311,7 +3644,7 @@ class OpenGLDisplayDriver : DisplayDriver
          {
             if(oglMesh.texCoords)
             {
-               glDeleteBuffersARB(1, &oglMesh.texCoords);
+               GLDeleteBuffers(1, &oglMesh.texCoords);
                oglMesh.texCoords = 0;
             }
             delete mesh.texCoords;
@@ -3320,7 +3653,7 @@ class OpenGLDisplayDriver : DisplayDriver
          {
             if(oglMesh.texCoords2)
             {
-               glDeleteBuffersARB(1, &oglMesh.texCoords2);
+               GLDeleteBuffers(1, &oglMesh.texCoords2);
                oglMesh.texCoords2 = 0;
             }
             /*
@@ -3331,7 +3664,7 @@ class OpenGLDisplayDriver : DisplayDriver
          {
             if(oglMesh.colors)
             {
-               glDeleteBuffersARB(1, &oglMesh.colors);
+               GLDeleteBuffers(1, &oglMesh.colors);
                oglMesh.colors = 0;
             }
          }
@@ -3343,7 +3676,7 @@ class OpenGLDisplayDriver : DisplayDriver
       }
    }
 
-   bool AllocateMesh(DisplaySystem displaySystem, Mesh mesh)
+   bool AllocateMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags, int nVertices)
    {
       bool result = false;
 
@@ -3352,30 +3685,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];
-            if(glGenBuffersARB)
-               glGenBuffersARB(1, &oglMesh.vertices);
-         }
-         if(mesh.flags.normals && !oglMesh.normals && !mesh.normals)
+         if(mesh.nVertices == nVertices)
          {
-            if(glGenBuffersARB)
-               glGenBuffersARB( 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(glGenBuffersARB)
-               glGenBuffersARB( 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
          {
-            if(glGenBuffersARB)
-               glGenBuffersARB( 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;
       }
@@ -3386,34 +3774,34 @@ class OpenGLDisplayDriver : DisplayDriver
    {
       OGLMesh oglMesh = mesh.data;
       if(!flags) flags = mesh.flags;
-      
-      if(glGenBuffersARB)
+
+      if(vboAvailable)
       {
-         if(!(flags.vertices) || oglMesh.vertices)
+         if(flags.vertices && oglMesh.vertices)
          {
-            glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.vertices);
-            glBufferDataARB( GL_ARRAY_BUFFER_ARB, mesh.nVertices * (mesh.flags.doubleVertices ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.vertices, GL_STATIC_DRAW_ARB );
+            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)
          {
-            glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.normals);
-            glBufferDataARB( GL_ARRAY_BUFFER_ARB, mesh.nVertices * (mesh.flags.doubleNormals ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.normals, GL_STATIC_DRAW_ARB );
+            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)
          {
-            glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.texCoords);
-            glBufferDataARB( GL_ARRAY_BUFFER_ARB, mesh.nVertices * sizeof(Pointf), mesh.texCoords, GL_STATIC_DRAW_ARB );
+            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)
          {
-            glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.colors);
-            glBufferDataARB( GL_ARRAY_BUFFER_ARB, mesh.nVertices * sizeof(ColorRGBAf), mesh.colors, GL_STATIC_DRAW_ARB );
+            GLBindBuffer( GL_ARRAY_BUFFER_ARB, oglMesh.colors);
+            GLBufferData( GL_FLOAT, GL_ARRAY_BUFFER_ARB, mesh.nVertices * sizeof(ColorRGBAf), mesh.colors, GL_STATIC_DRAW_ARB );
          }
 
-         glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0);
+         GLBindBuffer( GL_ARRAY_BUFFER_ARB, 0);
       }
    }
 
@@ -3428,8 +3816,8 @@ class OpenGLDisplayDriver : DisplayDriver
    {
       if(oglIndices)
       {
-         if(oglIndices.buffer) 
-            glDeleteBuffersARB(1, &oglIndices.buffer);
+         if(oglIndices.buffer)
+            GLDeleteBuffers(1, &oglIndices.buffer);
          delete oglIndices.indices;
          delete oglIndices;
       }
@@ -3441,8 +3829,7 @@ class OpenGLDisplayDriver : DisplayDriver
       if(oglIndices)
       {
          oglIndices.indices = (void *)(indices32bit ? new uint32[nIndices] : new uint16[nIndices]);
-         if(glGenBuffersARB)
-            glGenBuffersARB( 1, &oglIndices.buffer);
+         GLGenBuffers( 1, &oglIndices.buffer);
          oglIndices.nIndices = nIndices;
       }
       return oglIndices;
@@ -3450,18 +3837,18 @@ class OpenGLDisplayDriver : DisplayDriver
 
    void UnlockIndices(DisplaySystem displaySystem, OGLIndices oglIndices, bool indices32bit, int nIndices)
    {
-      if(glGenBuffersARB)
+      if(vboAvailable)
       {
-         glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, oglIndices.buffer);
-         glBufferDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB, nIndices * (indices32bit ? sizeof(uint32) : sizeof(uint16)), 
+         GLBindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB, oglIndices.buffer);
+         GLBufferData( indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, GL_ELEMENT_ARRAY_BUFFER_ARB, nIndices * (indices32bit ? sizeof(uint32) : sizeof(uint16)),
             oglIndices.indices, GL_STATIC_DRAW_ARB);
-         glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
+         GLBindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
       }
    }
 
    uint16 * LockIndices(DisplaySystem displaySystem, OGLIndices oglIndices)
    {
-      
+
       return oglIndices.indices;
    }
 
@@ -3470,51 +3857,54 @@ class OpenGLDisplayDriver : DisplayDriver
       //Logf("SelectMesh\n");
 
 #if !defined( __ANDROID__) && !defined(__APPLE__)
-      if(display.display3D.mesh && glUnlockArraysEXT)   
-         glUnlockArraysEXT();
+
+#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 ***
          glEnableClientState(GL_VERTEX_ARRAY);
          if(!display.display3D.collectingHits && oglMesh)
          {
-            if(glBindBufferARB)
-               glBindBufferARB(GL_ARRAY_BUFFER_ARB, oglMesh.vertices );
-            glVertexPointer(3, mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT, 0, glBindBufferARB ? null : mesh.vertices);
+            GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.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);
-               if(glBindBufferARB)
-                  glBindBufferARB(GL_ARRAY_BUFFER_ARB, oglMesh.normals);
-               glNormalPointer(mesh.flags.doubleNormals ? GL_DOUBLE : GL_FLOAT, 0, glBindBufferARB ? null : mesh.normals);
+               GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.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);
-               if(glBindBufferARB)
-                  glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.texCoords);
-               glTexCoordPointer(2, GL_FLOAT, 0, glBindBufferARB ? null : mesh.texCoords);
+               GLBindBuffer( GL_ARRAY_BUFFER_ARB, oglMesh.texCoords);
+               glTexCoordPointer(2, GL_FLOAT, 0, vboAvailable ? null : mesh.texCoords);
             }
             else
                glDisableClientState(GL_TEXTURE_COORD_ARRAY);
 
             // *** Color Stream ***
-            if(mesh.colors)
+            if(mesh.colors || mesh.flags.colors)
             {
                glEnableClientState(GL_COLOR_ARRAY);
-               if(glBindBufferARB)
-                  glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.colors);
-               glColorPointer(4, GL_FLOAT, 0, glBindBufferARB ? null : mesh.colors);
+               GLBindBuffer( GL_ARRAY_BUFFER_ARB, oglMesh.colors);
+               glColorPointer(4, GL_FLOAT, 0, vboAvailable ? null : mesh.colors);
             }
             else
                glDisableClientState(GL_COLOR_ARRAY);
@@ -3522,24 +3912,26 @@ class OpenGLDisplayDriver : DisplayDriver
          }
          else
          {
-            if(glBindBufferARB)
-               glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0);
-            glVertexPointer(3,mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT,0,mesh.vertices);
-            if(mesh.normals && !display.display3D.collectingHits)
+            GLBindBuffer( GL_ARRAY_BUFFER_ARB, 0);
+            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);
@@ -3549,24 +3941,31 @@ class OpenGLDisplayDriver : DisplayDriver
          }
 
 #if !defined(__ANDROID__) && !defined(__APPLE__)
-         if(glLockArraysEXT) glLockArraysEXT(0, mesh.nVertices);
+
+#if defined(__WIN32__)
+         if(glLockArraysEXT)
+#endif
+            if(!vboAvailable)
+               glLockArraysEXT(0, mesh.nVertices);
+
 #endif
       }
-      else if(glBindBufferARB)
-         glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
+      else
+         GLBindBuffer(GL_ARRAY_BUFFER_ARB, 0);
    }
 
    void DrawPrimitives(Display display, PrimitiveSingle * primitive, Mesh mesh)
    {
-      OGLDisplay oglDisplay = display.driverData;
       //Logf("DrawPrimitives\n");
 
       if(primitive->type.vertexRange)
          glDrawArrays(primitiveTypes[primitive->type.primitiveType], primitive->first, primitive->nVertices);
       else
       {
-         //    *** Hoping the data won't be uploaded at all (Won't really work if another group of the mesh is using the mesh ) *** 
+         //    *** 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);
          if(primitive->nIndices < (mesh.nVertices >> 2) && !primitive->type.indices32bit)
          {
             int c;
@@ -3577,7 +3976,7 @@ class OpenGLDisplayDriver : DisplayDriver
                MeshFeatures flags = mesh.flags;
                for(c = 0; c<primitive->nIndices; c++)
                {
-                  short index = ((short *) oglIndices.indices)[c];
+                  uint16 index = ((uint16 *) oglIndices.indices)[c];
                   if(flags.normals) glNormal3fv((float *)&mesh.normals[index]);
                   if(flags.texCoords1) glTexCoord2fv((float *)&mesh.texCoords[index]);
                   if(flags.colors) glColor4fv((float *)&mesh.colors[index]);
@@ -3586,23 +3985,29 @@ class OpenGLDisplayDriver : DisplayDriver
             }
             glEnd();
          }
-         else
+         else*/
+#endif
          {
             OGLIndices oglIndices = primitive->data;
 
-            if(!display.display3D.collectingHits && glBindBufferARB && oglIndices)
+            if(!display.display3D.collectingHits && vboAvailable && oglIndices)
             {
-               glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, oglIndices.buffer);
-               glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices, 
-                  primitive->type.indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0);
-               glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
+               GLBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, oglIndices.buffer);
+               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 if(oglIndices)
-               glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices, 
-                  primitive->type.indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, oglIndices.indices);
             else
-               glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices, 
-                  primitive->type.indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 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);
+            }
          }
       }
    }
@@ -3625,7 +4030,7 @@ class OpenGLDisplayDriver : DisplayDriver
       if(viewSpace)
       {
          glLoadIdentity();
-         glScalef(1.0f, 1.0f, -1.0f);
+         glScaled(1.0/nearPlane, 1.0/nearPlane, -1.0/nearPlane);
       }
       else if(camera)
       {
@@ -3654,4 +4059,25 @@ public void UseSingleGLContext(bool useSingle)
    useSingleGLContext = useSingle;
 }
 
+default dllexport void *
+#if defined(__WIN32__)
+__attribute__((stdcall))
+#endif
+IS_GLGetContext(DisplaySystem displaySystem)
+{
+   if(displaySystem)
+   {
+#if defined(__WIN32__)
+      OGLSystem system = displaySystem.driverData;
+      return system.glrc;
+#elif !defined(__ANDROID__)
+      OGLSystem system = displaySystem.driverData;
+      return system.glContext;
+#else
+      return eglContext;
+#endif
+   }
+   return null;
+}
+
 #endif