// -----------------------------------------------------------------------------
// OpenGL Template 
// -----------------------------------------------------------------------------
#include <windows.h>
#include <stdio.h>
#include <gl\glew.h>

LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
bool EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);
HDC hDC; HWND hWnd;

GLfloat OpenGLVersion = 1.0;

// Structure of one unique vertex
struct SVertex
{
	float x, y, z;
	float r, g, b;
};

// Rectangle, four unique vertices
SVertex vertex[] = 
{
	{ -1.0f, -1.0f, 0.0f,  1.0, 0.0, 0.0 },
	{ -1.0f,  1.0f, 0.0f,  0.0, 1.0, 0.0 },
	{  1.0f,  1.0f, 0.0f,  0.0, 0.0, 1.0 },
	{  1.0f, -1.0f, 0.0f,  1.0, 0.0, 1.0 },
};

// Array of 6 indices = 2 triangles
short index[] = { 0, 1, 2, 2, 3, 0 };

// Name of OpenGL VBOs
GLuint VBO_VertexArray, VBO_IndicesArray;

// -----------------------------------------------------------------------------
void DrawGL(void)
{
    glClear (GL_COLOR_BUFFER_BIT);   
    glLoadIdentity();
    glTranslatef(0.0, 0.0, -2.0);
    
    
    if (OpenGLVersion >= 1.1)
    {
		glPushClientAttrib( GL_CLIENT_VERTEX_ARRAY_BIT );
	
			glEnableClientState(GL_VERTEX_ARRAY);
			glEnableClientState(GL_COLOR_ARRAY);
			
			if (OpenGLVersion >= 1.5)
			{
				// Using Vertex Buffer Objects
				glBindBuffer( GL_ARRAY_BUFFER_ARB, VBO_VertexArray );
				glVertexPointer(3, GL_FLOAT, sizeof(struct SVertex), (GLvoid *)NULL);
				glColorPointer(3, GL_FLOAT, sizeof(struct SVertex), (GLvoid *)((float *)NULL + 3));
				
				glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, VBO_IndicesArray );
				glDrawRangeElements( GL_TRIANGLES, 0, 6, 6, GL_UNSIGNED_SHORT, (GLvoid *)NULL);
				
			}
			else
			{
				// Using Vertex Array
				glVertexPointer(3, GL_FLOAT, sizeof(struct SVertex), vertex);
				glColorPointer(3, GL_FLOAT, sizeof(struct SVertex), (float *)vertex + 3);
				
				if (OpenGLVersion >= 1.2)
				{
					glDrawRangeElements( GL_TRIANGLES, 0, 6, 6, GL_UNSIGNED_SHORT, index);
				}
				else
				{
					glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, index);
				}
			}
			
		glPopClientAttrib();
	}
	else
	{
		// The easiest way to draw something, but also the slowest, look at this 
		// huge bunch of code! 
		glPushMatrix ();
	        glBegin (GL_TRIANGLES);
	            glColor3fv ((GLfloat *)&vertex[0].r);   glVertex3fv ((GLfloat *)&vertex[0]); 
	            glColor3fv ((GLfloat *)&vertex[1].r);   glVertex3fv ((GLfloat *)&vertex[1]); 
	            glColor3fv ((GLfloat *)&vertex[2].r);   glVertex3fv ((GLfloat *)&vertex[2]); 
	            
	            glColor3fv ((GLfloat *)&vertex[2].r);   glVertex3fv ((GLfloat *)&vertex[2]); 
	            glColor3fv ((GLfloat *)&vertex[3].r);   glVertex3fv ((GLfloat *)&vertex[3]); 
	            glColor3fv ((GLfloat *)&vertex[0].r);   glVertex3fv ((GLfloat *)&vertex[0]); 
	        glEnd ();
	    glPopMatrix ();
	}
    
    
}

// -----------------------------------------------------------------------------
bool InitGL()
{
	// OpenGL version detecting
    char ver[100];
    OpenGLVersion = atof((char *)glGetString(GL_VERSION));
    sprintf(ver, "OpenGL ver. %.1f", OpenGLVersion);
    SetWindowText(hWnd, ver);
	
    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
	gluPerspective(70.0f,(GLfloat)640/(GLfloat)480,0.2f,400.0f);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

    // Back to the modelview
    glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glShadeModel(GL_SMOOTH);
	
    glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
    
    // Init Glew for OpenGL Extensions
	if (glewInit() != GLEW_OK)
	{
		MessageBox(NULL, "Error with glewInit()!", "Error", MB_OK);
		return false;
	}
    
    if (OpenGLVersion >= 1.5)
    {
		// Create Vertex Objects Buffer
		glGenBuffers( 1, &VBO_VertexArray );
		glBindBuffer( GL_ARRAY_BUFFER, VBO_VertexArray );
		glBufferData( GL_ARRAY_BUFFER, 6 * sizeof(struct SVertex), vertex, GL_STATIC_DRAW );
		
		// Create VBO for Indices
		glGenBuffers( 1, &VBO_IndicesArray );
		glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, VBO_IndicesArray );
		glBufferData( GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(short), index, GL_STATIC_DRAW );
	}
	
    return true;
}

// -----------------------------------------------------------------------------
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
{
    WNDCLASS wc;
    HGLRC hRC;        
    MSG msg;
    BOOL bQuit = FALSE;
    float theta = 0.0f;

    // register window class
    wc.style = CS_OWNDC;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor (NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "GLSample";
    RegisterClass (&wc);

    // create main window
    hWnd = CreateWindow ( "GLSample", "OpenGL Sample", WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
      50, 50, 640, 480, NULL, NULL, hInstance, NULL);

    // enable OpenGL for the window
    if (!EnableOpenGL (hWnd, &hDC, &hRC)) return 0;

    // program main loop
    while (!bQuit)
    {
        if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
        {
            if (msg.message == WM_QUIT)
            {
                bQuit = TRUE;
            }
            else
            {
                TranslateMessage (&msg);
                DispatchMessage (&msg);
            }
        }
        else
        {
            DrawGL();
            SwapBuffers (hDC);
        }
    }


    DisableOpenGL (hWnd, hDC, hRC);
    DestroyWindow (hWnd);
    return msg.wParam;
}

// -----------------------------------------------------------------------------
LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_CREATE:
        return 0;
    case WM_CLOSE:
        PostQuitMessage (0);
        return 0;

    case WM_DESTROY:
        return 0;

    case WM_KEYDOWN:
        switch (wParam)
        {
        case VK_ESCAPE:
            PostQuitMessage(0);
            return 0;
        }
        return 0;
    default:
        return DefWindowProc (hWnd, message, wParam, lParam);
    }
}


// -----------------------------------------------------------------------------
bool EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
{
    PIXELFORMATDESCRIPTOR pfd;
    int iFormat;

    /* get the device context (DC) */
    *hDC = GetDC (hWnd);

    /* set the pixel format for the DC */
    ZeroMemory (&pfd, sizeof (pfd));
    pfd.nSize = sizeof (pfd);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | 
      PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    pfd.cDepthBits = 16;
    pfd.iLayerType = PFD_MAIN_PLANE;
    iFormat = ChoosePixelFormat (*hDC, &pfd);
    SetPixelFormat (*hDC, iFormat, &pfd);

    /* create and enable the render context (RC) */
    *hRC = wglCreateContext( *hDC );
    wglMakeCurrent( *hDC, *hRC );
    
    return InitGL();
}

// -----------------------------------------------------------------------------
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
{
    wglMakeCurrent (NULL, NULL);
    wglDeleteContext (hRC);
    ReleaseDC (hWnd, hDC);
}

