#include "include/GL/gl.h"

#include <stdlib.h> 
#include <proto/intuition.h> 
#include <proto/exec.h> 
#include <proto/dos.h>
#include <stdio.h>

#include <proto/ogles2.h>


GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0};  /* Red diffuse light. */
GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0};  /* Infinite light location. */
GLfloat n[6][3] = {  /* Normals for the 6 faces of a cube. */
  {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
  {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0} };
GLint faces[6][4] = {  /* Vertex indices for the 6 faces of a cube. */
  {0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
  {4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3} };
GLfloat v[8][3];  /* Will be filled in with X,Y,Z vertexes. */

GLfloat angle = 0.0;


void
drawBox(void)
{
  int i;
  
  static float colors[6][3] =
  {
  	{1.0, 0.0, 0.0},
  	{0.0, 1.0, 0.0},
  	{0.0, 0.0, 1.0},
  	{1.0, 0.0, 1.0},
  	{0.0, 1.0, 1.0},
  	{1.0, 1.0, 1.0}
  };

  for (i = 0; i < 6; i++) {

	glEnableClientState(GL_VERTEX_ARRAY);
    glNormal3fv(&n[i][0]);
    glVertexPointer(3, GL_FLOAT, 0, v);
    glColor3fv(&colors[i][0]);
	
    glDrawElements(GL_QUADS, 4, GL_UNSIGNED_INT, faces[i]);
  }
}

void
display(void)
{
  glClearColor(0.4, 0.0, 0.0, 1.0);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  glPushMatrix();
   glRotatef(angle, 1.0, 0.0, 0.0);
  drawBox();
  glPopMatrix();
}


void
init(void)
{
  /* Setup cube vertex data. */
  v[0][0] = v[1][0] = v[2][0] = v[3][0] = -1;
  v[4][0] = v[5][0] = v[6][0] = v[7][0] = 1;
  v[0][1] = v[1][1] = v[4][1] = v[5][1] = -1;
  v[2][1] = v[3][1] = v[6][1] = v[7][1] = 1;
  v[0][2] = v[3][2] = v[4][2] = v[7][2] = 1;
  v[1][2] = v[2][2] = v[5][2] = v[6][2] = -1;

  /* Enable a single OpenGL light. */
  glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  glEnable(GL_LIGHT0);
  glEnable(GL_LIGHTING);

	glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
	glEnable(GL_COLOR_MATERIAL);
	
  /* Use depth buffering for hidden surface elimination. */
  glEnable(GL_DEPTH_TEST);

  /* Setup the view of the cube. */
  glMatrixMode(GL_PROJECTION);
  
  //gluPerspective( /* field of view in degree */ 40.0,
   // /* aspect ratio */ 1.0,
   // /* Z near */ 1.0, /* Z far */ 10.0);
	
  glMatrixMode(GL_MODELVIEW);
    
  //gluLookAt(0.0, 0.0, 5.0,  /* eye is at (0,0,5) */
   // 0.0, 0.0, 0.0,      /* center is at (0,0,0) */
   // 0.0, 1.0, 0.);      /* up is in positive Y direction */

  /* Adjust cube position to be asthetic angle. */  
  glTranslatef(0.0, 0.0, -1.0);
  glRotatef(60.0, 1.0, 0.0, 0.0);
  glRotatef(-20, 0.0, 0.0, 1.0);
}

void 
idle(void)
{ 	
    angle += 0.1;
    if (angle > 360.0)
       angle = 0.0;
  
}



int main(int argc, char **argv)
{

		const char titleStrBase[] = "Hello OpenGLES2";
		struct Window *win=IIntuition->OpenWindowTags(NULL,
								WA_Title,				titleStrBase,
								WA_Activate,			TRUE,
								WA_RMBTrap,				TRUE,
								WA_DragBar,				TRUE,
								WA_DepthGadget,			TRUE,
								WA_SimpleRefresh,		TRUE,
								WA_CloseGadget,			TRUE,
								WA_IDCMP,				IDCMP_REFRESHWINDOW | IDCMP_NEWSIZE | 
														IDCMP_CLOSEWINDOW | IDCMP_RAWKEY,
								WA_InnerWidth,			640,
								WA_InnerHeight,			480,
								WA_MinWidth,			100,
								WA_MinHeight,			100,
								WA_MaxWidth,			2048,
								WA_MaxHeight,			2048,
								WA_BackFill, 			LAYERS_NOBACKFILL,
								TAG_DONE);
		
		void *ogles_context=IOGLES2->aglCreateContextTags(0,
				OGLES2_CCT_WINDOW,(ULONG)win,
				OGLES2_CCT_DEPTH,16,
				OGLES2_CCT_STENCIL,8,
				OGLES2_CCT_VSYNC,0,
				OGLES2_CCT_SINGLE_GET_ERROR_MODE,1,
			TAG_DONE);


	if (ogles_context)
	{
		IOGLES2->aglMakeCurrent(ogles_context);

		glViewport(0,0, 640, 480);

		init();

		BOOL bRun = TRUE;
		
		if (!IIntuition->ModifyIDCMP(win, IDCMP_CLOSEWINDOW))
			bRun = FALSE;

		while (bRun)
		{
			struct IntuiMessage *imsg;

			display();
			idle();
			
			glFlush();
			IOGLES2->aglSwapBuffers();
			

			while ((imsg = (struct IntuiMessage *)IExec->GetMsg(win->UserPort)))
			{
				if (imsg->Class == IDCMP_CLOSEWINDOW)
					bRun = FALSE;
				IExec->ReplyMsg((struct Message *)imsg);
			}
		}
		
		IIntuition->CloseWindow(win);
    } 
     
	
	return 0;
}
