/*********************************************************************
*                                                                    *          
*                              FLY                                   *
*                           --E#Y#E--   			     *
*                         ===MUSCLE===                               *
*                                                                    *
*********************************************************************/
/* Header Files */
#include "cube.h"

/* Cube Map */
GLuint m_cubeMapTex;
char m_cubeMapInitialized;
int m_viewportWidth;
int m_viewportHeight;

/*-------------------------------------------------------------------------*/

/* Resize */
void onResize(int width, int height)
{
        /* Viewport */
        glViewport(0, 0, width, height);
        glScissor(0, 0, width, height);
        
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();

        /* Perspective */
        const GLdouble fieldOfView = 52.0f;
        const GLdouble aspect = (float)(width) / (float)(height);
        const GLdouble zNear = 1.0f;
        const GLdouble zFar = 1000.0f;

	/* Perspective */
	gluPerspective(fieldOfView, aspect, zNear, zFar);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
}

/*-------------------------------------------------------------------------*/

/* Cube Map Rendering */
void beginCubeMapRendering(int texSize)
{
        /* Viewport */
        GLint viewport[4];
        glGetIntegerv(GL_VIEWPORT, viewport);
        m_viewportWidth = viewport[2];
        m_viewportHeight = viewport[3];

        /* Viewport */
        glViewport(0, 0, texSize, texSize);
        glScissor(0, 0, texSize, texSize);
        
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        
        /* Perspective */
        const GLdouble fieldOfView = 90.0f;
        const GLdouble zNear = 0.1f;
        const GLdouble zFar = 100.0f;      
        
        /* Perspective */
	gluPerspective(fieldOfView, 1, zNear, zFar);
        glMatrixMode(GL_MODELVIEW);
}

/*-------------------------------------------------------------------------*/

/* Begin Cupe Map Face */
void beginCubeMapFace(int TEX) 
{
        const GLfloat ROTATION[6][6] = {
          /* at              up */
          { 1.0, 0.0, 0.0,   0.0,-1.0, 0.0 },  /* pos x */
          {-1.0, 0.0, 0.0,   0.0,-1.0, 0.0 },  /* neg x */
          { 0.0, 1.0, 0.0,   0.0, 0.0, 1.0 },  /* pos y */
          { 0.0,-1.0, 0.0,   0.0, 0.0,-1.0 },  /* neg y */
          { 0.0, 0.0, 1.0,   0.0,-1.0, 0.0 },  /* poz z */
          { 0.0, 0.0,-1.0,   0.0,-1.0, 0.0 }   /* neg z */
        };

        glClear(GL_DEPTH_BUFFER_BIT);
        glLoadIdentity();
        gluLookAt(0.0, 0.0, 0.0,
                  ROTATION[TEX][0], ROTATION[TEX][1], ROTATION[TEX][2],
                  ROTATION[TEX][3], ROTATION[TEX][4], ROTATION[TEX][5]);
}

/*-------------------------------------------------------------------------*/

/* Finish Cube Map Face */
void finishCubeMapFace(int TEX, int texSize)
{
        glBindTexture(GL_TEXTURE_CUBE_MAP, m_cubeMapTex);
        if(m_cubeMapInitialized)
        {
                glCopyTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + TEX,
                                0, 0, 0, 0, 0, texSize, texSize);
        } 
        else
        {
                glCopyTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + TEX,
                             0, GL_RGB8, 0, 0, texSize, texSize, 0);
        }
}

/*-------------------------------------------------------------------------*/

/* Cup Map Rendering */
void endCubeMapRendering()
{
        m_cubeMapInitialized = TRUE;
        onResize(m_viewportWidth, m_viewportHeight);
}

/*-------------------------------------------------------------------------*/

