/*
 *  eglSetup.c
 *  eagl clue and OpenGL environment setup routines
 *  All eagl specific knowledge is wrapped here,
 *  other part of the project can be pure OpenGL ES.
 *
 *  Created by pin xue on 4/8/08.
 *  Copyright 2008 http://www.play3d.net. All rights reserved.
 *
 */

#include "eglSetup.h"

#include <math.h>

#ifndef USE_FIXED_POINT
  #define glClearColorx glClearColor
  #define glFrustumx glFrustumf
  #define glTranslatex glTranslatef
#endif

#define NULL 0

static EAGLContext g_context = NULL;
static EAGLSurface g_surface = NULL;

int init_egl(EAGLNativeWindow nativeWindow, int width, int height)
{

        EAGLError ret;
        
        // create surface
        ret = eaglCreateWindowSurface(kEAGLPixelFormat_RGB565_D24, nativeWindow, &g_surface);
        if ( ret != kEAGLErrorSuccess || g_surface == NULL )
        {
            return -1;
        }

        // create context
        ret = eaglCreateContext(NULL, &g_context);
        if ( ret != kEAGLErrorSuccess || g_context == NULL )
        {
            return -2;
        }

        // active context (make current)
        makeCurrent();
        
        resize(width, height);

        default3DEnv();

        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

        return 0;
}

void default3DEnv(void)
{
        glClearColorx(Float2Fixed(0.5f), Float2Fixed(0.5f), Float2Fixed(0.5f), Float2Fixed(1.0f));
          
        glShadeModel(GL_SMOOTH);
        glEnable(GL_CULL_FACE);
        glCullFace(GL_BACK);
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_TEXTURE_2D);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        //glFrustumx( Float2Fixed(-10.0f), Float2Fixed(10.0f), Float2Fixed(-10.0f), Float2Fixed(10.0), Float2Fixed(0.1f), Float2Fixed(100.0f) );

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
}

void resize(int width, int height)
{
        glViewport( 0, 0, width, height );

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();

        float top = tan(45.0f*3.14159f/360.0f) * 0.005f;
        float right = (float)width/(float)height * top;

        glFrustumx( Float2Fixed(-right), Float2Fixed(right), Float2Fixed(-top), Float2Fixed(top), Float2Fixed(0.1f), Float2Fixed(100.0f) );

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glTranslatex( 0, 0, Float2Fixed(-60.0f) ); // fixpoint : hi 16 bits are integer part, low 16 bits are fragmental part.
}


int makeCurrent(void)
{
        EAGLError ret = eaglMakeCurrent(g_surface, g_context);
        return ret != kEAGLErrorSuccess;
}

int swapBuffers(void)
{
        return kEAGLErrorSuccess != eaglSwapBuffers(g_surface);
}

void clean(void)
{
        swapBuffers();

        // deactive context
        // eglMakeCurrent( dpy, NULL, NULL, NULL );
        eaglMakeCurrent(NULL, NULL);

        // destroy context
        if ( g_context != NULL )
            eaglDestroyContext( g_context );

        // destroy suerface
        if ( g_surface != NULL )
            eaglDestroySurface( g_surface );

}

