-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
-
-#if defined(__ANDROID__)
-
-// Our own matrix stack
-static Matrix matrixStack[3][32];
-static int matrixIndex[3];
-static int curStack = 0;
-
-// OpenGL ES Porting Kit
-
-#define glBindFramebuffer glBindFramebufferOES
-#define glBindRenderbuffer glBindRenderbufferOES
-#define GL_FRAMEBUFFER GL_FRAMEBUFFER_OES
-#define GL_RENDERBUFFER GL_RENDERBUFFER_OES
-#define glFramebufferTexture2D glFramebufferTexture2DOES
-#define GL_COLOR_ATTACHMENT0 GL_COLOR_ATTACHMENT0_OES
-#define glGenFramebuffers glGenFramebuffersOES
-#define glGenRenderbuffers glGenRenderbuffersOES
-#define glDeleteFramebuffers glDeleteFramebuffersOES
-#define glDeleteRenderbuffers glDeleteRenderbuffersOES
-
-#define GL_POLYGON_STIPPLE 0xFFFF
-#define GL_LINE_STIPPLE 0xFFFF
-#define GL_LINE 0xFFFF
-#define GL_FILL 0xFFFF
-#define GL_ALL_ATTRIB_BITS 0xFFFF
-#define GL_LIGHT_MODEL_LOCAL_VIEWER 0xFFFF
-#define glDrawElementsi(type, count, start) glDrawElements(type, count, GL_UNSIGNED_SHORT, start)
-
-#define GL_UNSIGNED_INT 0x1405
-#define GL_DOUBLE 0x140A
-
-#define GL_POLYGON 9
-#define GL_QUADS 7
-
-#define glBufferDatai glesBufferDatai
-#define glBufferDatad glesBufferDatad
-#define glVertexPointeri glesVertexPointeri
-#define glVertexPointerd glesVertexPointerd
-
-#define glRecti glesRecti
-#define glBegin glesBegin
-#define glTexCoord2i glesTexCoord2i
-#define glVertex2i glesVertex2i
-#define glTexCoord2d glesTexCoord2d
-#define glVertex2d glesVertex2d
-#define glTexCoord2f glesTexCoord2f
-#define glVertex2f glesVertex2f
-#define glEnd glesEnd
-#define glColor3f glesColor3f
-#define glColor4ub glesColor4ub
-#define glColor4fv glesColor4fv
-#define glLineStipple glesLineStipple
-#define glNormal3fv glesNormal3fv
-#define glTexCoord2fv glesTexCoord2fv
-#define glColorMaterial glesColorMaterial
-
-#define glLoadMatrixd glesLoadMatrixd
-#define glMultMatrixd glesMultMatrixd
-#define glFrustum glesFrustum
-#define glOrtho glesOrtho
-#define glScaled glesScaled
-#define glTranslated glesTranslated
-#define glRotated glesRotated
-#define glVertex3d glesVertex3d
-#define glVertex3f glesVertex3f
-#define glVertex3fv glesVertex3fv
-#define glLightModeli glesLightModeli
-
-#define APIENTRY
-//#define GL_QUADS 0
-#define GL_QUAD_STRIP 0
-//#define GL_DOUBLE 0
-//#define GL_UNSIGNED_INT 0
-//#define GL_FILL 0
-//#define GL_LINE 0
-//#define GL_LINE_STIPPLE 0
-#define GL_BGRA_EXT 0
-#define GL_UNPACK_ROW_LENGTH 0
-#define GL_UNPACK_SKIP_PIXELS 0
-#define GL_UNPACK_SKIP_ROWS 0
-#define GL_RGBA8 0
-#define GL_PACK_ROW_LENGTH 0
-#define GL_PACK_SKIP_ROWS 0
-#define GL_PACK_SKIP_PIXELS 0
-
-static EGLDisplay eglDisplay;
-static EGLSurface eglSurface;
-static EGLContext eglContext;
-static int eglWidth, eglHeight;
-
-static bool egl_init_display(ANativeWindow* window)
-{
- const EGLint attribs[] =
- {
- EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
- EGL_BLUE_SIZE, 8,
- EGL_GREEN_SIZE, 8,
- EGL_RED_SIZE, 8,
- EGL_DEPTH_SIZE, 24,
- /*EGL_SAMPLE_BUFFERS, 1,
- EGL_SAMPLES, 0, //2,*/
- EGL_NONE
- };
- EGLint w, h, dummy, format;
- EGLint numConfigs;
- EGLConfig config;
- EGLSurface surface;
- EGLContext context;
-
- EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
- eglInitialize(display, 0, 0);
- eglChooseConfig(display, attribs, &config, 1, &numConfigs);
- eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
-
- surface = eglCreateWindowSurface(display, config, window, null);
- context = eglCreateContext(display, config, null, null);
-
- if(!eglMakeCurrent(display, surface, surface, context))
- return false;
-
- eglQuerySurface(display, surface, EGL_WIDTH, &w);
- eglQuerySurface(display, surface, EGL_HEIGHT, &h);
-
- eglDisplay = display;
- eglContext = context;
- eglSurface = surface;
- eglWidth = w;
- eglHeight = h;
-
- glEnableClientState(GL_VERTEX_ARRAY);
- /*
- // Initialize GL state.
- glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
- glEnable(GL_CULL_FACE);
- glShadeModel(GL_SMOOTH);
- glDisable(GL_DEPTH_TEST);
- */
- glDisable(GL_CULL_FACE);
- glDisable(GL_DEPTH_TEST);
-
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- glEnable(GL_BLEND);
-
- matrixStack[0][0].Identity();
- matrixStack[1][0].Identity();
- matrixStack[2][0].Identity();
-
- glesMatrixMode(GL_MODELVIEW);
- glScaled(1.0, 1.0, -1.0);
- glesMatrixMode(GL_PROJECTION);
- glShadeModel(GL_FLAT);
-
- glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
- glFogi(GL_FOG_MODE, GL_EXP);
- glFogf(GL_FOG_DENSITY, 0);
- glEnable(GL_NORMALIZE);
- glDepthFunc(GL_LESS);
- glClearDepth(1.0);
- glDisable(GL_MULTISAMPLE_ARB);
-
- glViewport(0,0,w,h);
- glesLoadIdentity();
- glOrtho(0,w,h,0,0.0,1.0);
-
- currentVertexBuffer = 0;
- return true;
-}
-
-static void egl_term_display()
-{
- if(stippleTexture)
- {
- glDeleteTextures(1, (int *)&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;
-}
-
-// OpenGL Immediate Mode Porting Kit
-static int beginCount;
-static int vertexCount;
-static int normalCount;
-static float *vertexPointer;
-static float *normalPointer;
-static GLenum beginMode;
-static unsigned int beginBufferSize, normalBufferSize;
-static int numVertexCoords = 2;
-
-void glesRecti(int a, int b, int c, int d)
-{
- glBegin(GL_QUADS);
- glVertex2i(a, b);
- glVertex2i(a, d);
- glVertex2i(c, d);
- glVertex2i(c, b);
- glEnd();
-}
-
-void glesBegin(GLenum mode)
-{
- beginMode = mode;
- beginCount = 0;
- vertexCount = 0;
- if(!vertexPointer)
- {
- normalBufferSize = beginBufferSize = 1024; // default number of vertices
- vertexPointer = new float[beginBufferSize * 5];
- normalPointer = new float[normalBufferSize * 3];
- }
-}
-
-void glesTexCoord2f(float x, float y)
-{
- int count = vertexCount;
-
- if(vertexCount + numVertexCoords > beginBufferSize)
- {
- beginBufferSize = beginBufferSize + beginBufferSize/2;
- vertexPointer = renew vertexPointer float[beginBufferSize * 5];
- }
-
- vertexPointer[count*(2+numVertexCoords) ] = x;
- vertexPointer[count*(2+numVertexCoords)+1] = y;
- count++;
-
- if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
- {
- vertexPointer[count*(2+numVertexCoords) ] = vertexPointer[(count-4)*(2+numVertexCoords)];
- vertexPointer[count*(2+numVertexCoords)+1] = vertexPointer[(count-4)*(2+numVertexCoords)+1];
- count++;
- vertexPointer[count*(2+numVertexCoords) ] = vertexPointer[(count-3)*(2+numVertexCoords)];
- 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]); }
-
-void glesVertex2f(float x, float y)
-{
- numVertexCoords = 2;
- if(vertexCount + 4 > beginBufferSize)
- {
- beginBufferSize = beginBufferSize + beginBufferSize/2;
- vertexPointer = renew vertexPointer float[beginBufferSize * 5];
- }
-
- vertexPointer[vertexCount*4+2] = x;
- vertexPointer[vertexCount*4+3] = y;
- vertexCount++;
-
- if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
- {
- vertexPointer[vertexCount*4+2] = vertexPointer[(vertexCount-4)*4+2];
- vertexPointer[vertexCount*4+3] = vertexPointer[(vertexCount-4)*4+3];
- vertexCount++;
- vertexPointer[vertexCount*4+2] = vertexPointer[(vertexCount-3)*4+2];
- vertexPointer[vertexCount*4+3] = vertexPointer[(vertexCount-3)*4+3];
- vertexCount++;
- }
- beginCount++;
-}
-void glesVertex2i(int x, int y) { glesVertex2f((float)x, (float)y); }
-void glesVertex2d(double x, double y) { glesVertex2f((float)x, (float)y); }
-
-void glesEnd(void)
-{
- int mode = beginMode;
- if(mode == GL_QUADS) mode = GL_TRIANGLES;
- else if(mode == GL_POLYGON) mode = GL_TRIANGLE_FAN;
- GLSelectVBO(0);
- glEnableClientState(GL_TEXTURE_COORD_ARRAY);
- glTexCoordPointer(numVertexCoords, GL_FLOAT, (numVertexCoords+2)*sizeof(float),vertexPointer);
- glVertexPointer (numVertexCoords, GL_FLOAT, (numVertexCoords+2)*sizeof(float),vertexPointer+2);
- if(normalCount && normalCount == vertexCount)
- {
- glEnableClientState(GL_NORMAL_ARRAY);
- glNormalPointer (GL_FLOAT, 3*sizeof(float),normalPointer);
- }
-
- glDrawArrays(mode, 0, vertexCount);
- if(normalCount)
- glDisableClientState(GL_NORMAL_ARRAY);
- glDisableClientState(GL_TEXTURE_COORD_ARRAY);
- normalCount = 0;
-}
-
-// Vertex Pointer
-static float *floatVPBuffer = null;
-static short *shortVPBuffer = null;
-static unsigned int shortVPSize = 0, floatVPSize = 0;
-
-// Buffer Data
-//static float *floatVPBuffer = null; // For floats we reuse floatVPBuffer
-static unsigned short *shortBDBuffer = null;
-static unsigned int shortBDSize = 0/*, floatVPSize = 0*/;
-
-void glesVertexPointeri(int numCoords, int stride, int *pointer, int numVertices)
-{
- if(pointer)
- {
- int i;
- if(numVertices*numCoords > shortVPSize)
- {
- shortVPSize = numVertices*numCoords;
- shortVPBuffer = renew shortVPBuffer short[shortVPSize];
- }
- for(i = 0; i < numVertices*numCoords; i++)
- shortVPBuffer[i] = (short)pointer[i];
- glVertexPointer(numCoords, GL_SHORT, stride, shortVPBuffer);
- }
- else
- glVertexPointer(numCoords, GL_SHORT, stride, 0);
-}
-
-void glesVertexPointerd(int numCoords, int stride, double *pointer, int numVertices)
-{
- if(pointer)
- {
- int i;
- if(numVertices*numCoords > floatVPSize)
- {
- floatVPSize = numVertices*numCoords;
- floatVPBuffer = renew floatVPBuffer float[floatVPSize];
- }
- for(i = 0; i < numVertices*numCoords; i++)
- floatVPBuffer[i] = (float)pointer[i];
- glVertexPointer(numCoords, GL_FLOAT, stride, floatVPBuffer);
- }
- else
- glVertexPointer(numCoords, GL_FLOAT, stride, 0);
-}
-
-void glesTexReuseIntVP(int numCoords)
-{
- glTexCoordPointer(numCoords, GL_SHORT, 0, floatVPBuffer);
-}
-
-void glesTexReuseDoubleVP(int numCoords)
-{
- glTexCoordPointer(numCoords, GL_FLOAT, 0, floatVPBuffer);
-}
-
-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)
-{
- glColor4f(r/255.0f, g/255.0f, b/255.0f, a/255.0f);
-}
-
-void glesColor4fv(float * a)
-{
- glColor4f(a[0], a[1], a[2], a[3]);
-}
-
-void glesBufferDatad(int target, int size, void * data, int usage)
-{
- int numElems = size/sizeof(double);
- double * dblPtr = (double *)data;
- int i;
- if (numElems > floatVPSize)
- {
- floatVPSize = numElems;
- floatVPBuffer = renew floatVPBuffer float[floatVPSize];
- }
- for (i=0; i< numElems; i++)
- floatVPBuffer[i] = (float)dblPtr[i];
-
- glBufferData(target, numElems*sizeof(float), floatVPBuffer, usage);
-}
-
-void glesBufferDatai(int target, int size, void * data, int usage)
-{
- int numElems = size/sizeof(unsigned int);
- unsigned int * pointer = (unsigned int *)data;
- int i;
- if (numElems > shortBDSize)
- {
- shortBDSize = numElems;
- shortBDBuffer = renew shortBDBuffer uint16[shortBDSize];
- }
- for (i=0; i< numElems; i++)
- shortBDBuffer[i] = (unsigned short)pointer[i];
-
- glBufferData(target, numElems*sizeof(unsigned short), shortBDBuffer, usage);
-}
-
-// *** 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);
-}
-
-void glesLoadIdentity()
-{
- matrixStack[curStack][matrixIndex[curStack]].Identity();
- LoadCurMatrix();
-}
-
-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));
- }
-}
-
-void glesPopMatrix()
-{
- if(matrixIndex[curStack] > 0)
- {
- matrixIndex[curStack]--;
- LoadCurMatrix();
- }
-}
-
-void glesLoadMatrixd(double * i)
-{
- memcpy(matrixStack[curStack][matrixIndex[curStack]].array, i, sizeof(Matrix));
- LoadCurMatrix();
-}
-
-void glesOrtho( double l, double r, double b, double t, double n, double f )
-{
- Matrix m =
- { {
- (2 / (r - l)), 0, 0, 0,
- 0, (2 / (t - b)), 0, 0,
- 0, 0, (-2 / (f - n)), 0,
- (-(r + l) / (r - l)), (-(t + b) / (t - b)), (-(f + n) / (f - n)), 1
- } }, res;
- res.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
- matrixStack[curStack][matrixIndex[curStack]] = res;
- LoadCurMatrix();
-}
-
-void glesFrustum( double l, double r, double b, double t, double n, double f )
-{
- 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
- } }, res;
- res.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
- matrixStack[curStack][matrixIndex[curStack]] = res;
- LoadCurMatrix();
- }
-}
-
-void glesRotated( double a, double b, double c, double d )
-{
- Matrix m;
- Quaternion q;
- q.RotationAxis({(float)b,(float)-c,(float)d}, a );
- m.RotationQuaternion(q);
- matrixStack[curStack][matrixIndex[curStack]].Rotate(q);
- LoadCurMatrix();
-}
-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();
-}
-
-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();
-}
-
-void glesMultMatrixd( double * i )
-{
- Matrix r;
- r.Multiply((Matrix *)i, matrixStack[curStack][matrixIndex[curStack]]);
- matrixStack[curStack][matrixIndex[curStack]] = r;
- LoadCurMatrix();
-}
-
-void glesMatrixMode(int mode)
-{
- curStack = mode == GL_MODELVIEW ? 0 : mode == GL_PROJECTION ? 1 : 2;
- glMatrixMode(mode);
-}