/*
 *  @ Project : hello eagl ++
 *  @ File Name : eglSetup.c
 *  @ Date : 2008-9-8
 *  @ Author : kongfu.yang
 *  @ Description : eagl clue and OpenGL environment setup routines
 *      All eagl specific knowledge is wrapped here,
 *      other part of the project can be pure OpenGL ES.
 *
 *  @ Company : http://www.play3d.net
 *  @ Copyright : 2008-9, Example source license. 
 *        By keep this header comment, 
 *        you may use this source file in your project for non-commercial or commercial purpose.
 *
 */
  

#include "eglSetup.h"

#include <math.h>

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

#define NULL 0

int init_egl(int width, int height)
{
	   // it is not happy and easy to adopt EAGLContext calls to CPP,
	   // so that the initializing of the context is inside the view.

		// 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.
}

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 );
 */

}

