#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <math.h>
#include <stdio.h>

LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
void EnableOpenGL(HWND hwnd, HDC*, HGLRC*);
void DisableOpenGL(HWND, HDC, HGLRC);
int state, stage, ang;
bool movement, lines_only;

const int N = 10;

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    WNDCLASSEX wcex;
    HWND hwnd;
    HDC hDC;
    HGLRC hRC;
    MSG msg;
    BOOL bQuit = FALSE;

    /* register window class */
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style = CS_OWNDC;
    wcex.lpfnWndProc = WindowProc;
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = hInstance;
    wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    wcex.lpszMenuName = NULL;
    wcex.lpszClassName = "GLSample";
    wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);;


    if (!RegisterClassEx(&wcex))
        return 0;

    /* create main window */
    hwnd = CreateWindowEx(0,
                          "GLSample",
                          "Simple Geometry",
                          WS_POPUP,
                          CW_USEDEFAULT,
                          CW_USEDEFAULT,
                          256,
                          256,
                          NULL,
                          NULL,
                          hInstance,
                          NULL);

    ShowWindow(hwnd, SW_MAXIMIZE);

    /* enable OpenGL for the window */
    EnableOpenGL(hwnd, &hDC, &hRC);

    /* program main loop */
    while (!bQuit)
    {
        /* check for messages */
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            /* handle or dispatch messages */
            if (msg.message == WM_QUIT)
            {
                bQuit = TRUE;
            }
            else
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }
        else
        {
            /* OpenGL animation code goes here */
            glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
            glClear(GL_COLOR_BUFFER_BIT);

            glPushMatrix();
            glEnable(GL_POINT_SMOOTH);

            glPointSize(10);
            glLineWidth(10);

            //glEnable(GL_LIGHTING);

            //GLfloat color[] = {0, 0, 1, 0.5};
            //glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
            //glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color);
            //glEnable(GL_LIGHT0);

            if (lines_only)
                glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
            else
                glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

            if (movement)
            {
                ang += 1;
                glRotatef(ang, 1, 1, 1);
            }

            if (stage == 0) {
                switch (state) {
                    case 0:
                    break;

                    case 1:
                        glBegin(GL_LINES);
                    break;

                    case 2:
                        glBegin(GL_LINE_STRIP);
                    break;

                    case 3:
                        glBegin(GL_LINE_LOOP);
                    break;

                    case 4:
                        glBegin(GL_TRIANGLES);
                    break;

                    case 5:
                        glBegin(GL_TRIANGLE_STRIP);
                    break;

                    case 6:
                        glBegin(GL_TRIANGLE_FAN);
                    break;

                    case 7:
                        glBegin(GL_QUADS);
                    break;

                    case 8:
                        glBegin(GL_QUAD_STRIP);
                    break;

                    case 9:
                        glBegin(GL_POLYGON);
                    break;
                }

                for (int i = 0; i < 12; i++)
                    glVertex2f(0.8 * sin(i / 6.0 * M_PI), 0.8 * cos(i / 6.0 * M_PI));
                glEnd();

                glBegin(GL_POINTS);
                for (int i = 0; i < 12; i++)
                    glVertex2f(0.8 * sin(i / 6.0 * M_PI), 0.8 * cos(i / 6.0 * M_PI));
                glEnd();
            }

            if (stage == 1)
            {
                switch (state)
                {
                    case 0:
                        glBegin(GL_TRIANGLE_FAN);
                            glVertex3f(0, 0, 0);
                            for (int i = 0; i <= 20; i++)
                            {
                                float angle = (i % 20) / 10.0 * M_PI;
                                glVertex3f(0.8 * cos(angle), 0.8 * sin(angle), 0);
                            }
                        glEnd();
                    break;

                    case 1:
                        for (int j = 0; j < 2; j++)
                        {
                            glBegin(GL_TRIANGLE_FAN);
                            glVertex3f(0, 0, j / 2.0);
                            for (int i = 0; i <= 20; i++)
                            {
                                float angle = (i % 20) / 10.0 * M_PI;
                                glVertex3f(0.8 * cos(angle), 0.8 * sin(angle), 0);
                            }
                            glEnd();
                        }
                    break;

                    case 2:
                        for (int j = 0; j < 2; j++)
                        {
                            glBegin(GL_TRIANGLE_FAN);
                            glVertex3f(0, 0, j - 0.5);
                            for (int i = 0; i <= 20; i++)
                            {
                                float angle = (i % 20) / 10.0 * M_PI;
                                glVertex3f(0.8 * cos(angle), 0.8 * sin(angle), j - 0.5);
                            }
                            glEnd();
                        }
                        glBegin(GL_QUAD_STRIP);
                            for (int i = 0; i <= 20; i++)
                            {
                                float angle = (i % 20) / 10.0 * M_PI;
                                glVertex3f(0.8 * cos(angle), 0.8 * sin(angle), -0.5);
                                glVertex3f(0.8 * cos(angle), 0.8 * sin(angle), 0.5);
                            }
                        glEnd();
                    break;
                }
            }

            if (stage == 2)
            {

                switch (state)
                {
                    case 0:
                        glBegin(GL_QUAD_STRIP);
                        for (int i = 0; i < 20; i++)
                        {
                            float ang1 = i / 20.0 * M_PI - M_PI / 2;
                            float ang2 = (i + 1) / 20.0 * M_PI - M_PI / 2;
                            for (int j = 0; j <= 20; j++)
                            {
                                float ang3 = (j % 20) / 10.0 * M_PI;
                                glVertex3f(0.8 * sin(ang1) * cos(ang3),
                                            0.8 * sin(ang1) * sin(ang3),
                                            0.8 * cos(ang1));
                                glVertex3f(0.8 * sin(ang2) * cos(ang3),
                                            0.8 * sin(ang2) * sin(ang3),
                                            0.8 * cos(ang2));
                            }
                        }
                        glEnd();
                    break;

                    case 1:
                        glBegin(GL_QUAD_STRIP);
                        for (int i = 0; i < 20; i++)
                        {
                            float ang1 = i / 20.0 * M_PI - M_PI / 2;
                            float ang2 = (i + 1) / 20.0 * M_PI - M_PI / 2;
                            for (int j = 0; j <= 20; j++)
                            {
                                float ang3 = (j % 20) / 10.0 * M_PI;
                                glVertex3f(0.8 * sin(ang1) * cos(ang3),
                                            0.8 * sin(ang1) * sin(ang3),
                                            0.8 * cos(ang1));
                                glVertex3f(0.8 * sin(ang2) * cos(ang3),
                                            0.8 * sin(ang2) * sin(ang3),
                                            0.8 * cos(ang2));
                            }
                        }
                        glEnd();

                        glBegin(GL_QUAD_STRIP);
                        for (int i = 0; i < 20; i++)
                        {
                            float ang1 = i / 20.0 * M_PI - M_PI / 2;
                            float ang2 = (i + 1) / 20.0 * M_PI - M_PI / 2;
                            for (int j = 0; j <= 20; j++)
                            {
                                float ang3 = (j % 20) / 10.0 * M_PI;
                                glVertex3f(0.8 * sin(ang1) * cos(ang3),
                                            0.8 * sin(ang1) * sin(ang3),
                                            -0.8 * cos(ang1));
                                glVertex3f(0.8 * sin(ang2) * cos(ang3),
                                            0.8 * sin(ang2) * sin(ang3),
                                            -0.8 * cos(ang2));
                            }
                        }
                        glEnd();
                    break;

                    case 2:
                        glBegin(GL_QUAD_STRIP);
                        for (int i = 0; i < 20; i++)
                        {
                            float ang1 = i / 10.0 * M_PI;
                            float ang2 = (i + 1) / 10.0 * M_PI;

                            for (int j = 0; j <= 20; j++)
                            {
                                float ang3 = (j % 20) / 10.0 * M_PI;
                                glVertex3f((0.4 + 0.2 * cos(ang1)) * cos(ang3),
                                           (0.4 + 0.2 * cos(ang1)) * sin(ang3),
                                            0.2 * sin(ang1));
                                glVertex3f((0.4 + 0.2 * cos(ang2)) * cos(ang3),
                                           (0.4 + 0.2 * cos(ang2)) * sin(ang3),
                                            0.2 * sin(ang2));
                            }
                        }
                        glEnd();
                    break;
                }
            }
            if (stage == 3) {
                GLUquadricObj *quadObj;
                quadObj = gluNewQuadric();
                //glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
                float front_color[] = {1, 1, 0, 0.5};
                float back_color[] = {1, 1, 0, 0.5};
                glMaterialfv(GL_FRONT, GL_DIFFUSE, front_color);
                glMaterialfv(GL_BACK, GL_DIFFUSE, back_color);
                //glPushMatrix();
                if (lines_only)
                    gluQuadricDrawStyle(quadObj, GLU_LINE);
                else
                    gluQuadricDrawStyle(quadObj, GLU_FILL);

                switch (state)
                {
                    case 0:
                        gluCylinder(quadObj, 0.6, 0.5, 0.7, 10, 10);
                    break;

                    case 1:
                        gluSphere(quadObj, 0.5, 10, 10);
                    break;

                    case 2:
                        gluDisk(quadObj, 0.5, 1.0, 10, 10);
                    break;
                }



                gluDeleteQuadric(quadObj);
            }

            glPopMatrix();

            //glDisable(GL_LIGHTING);

            SwapBuffers(hDC);

            Sleep (1);
        }
    }

    /* shutdown OpenGL */
    DisableOpenGL(hwnd, hDC, hRC);

    /* destroy the window explicitly */
    DestroyWindow(hwnd);

    return msg.wParam;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
        case WM_CLOSE:
            PostQuitMessage(0);
        break;

        case WM_DESTROY:
            return 0;

        case WM_KEYDOWN:
        {
            switch (wParam)
            {
                case VK_ESCAPE:
                    PostQuitMessage(0);
                break;

                case VK_RIGHT:
                    state = (state + 1) % N;
                break;

                case VK_LEFT:
                    state--;
                    if (state < 0) state = N - 1;
                break;

                case VK_UP:
                    stage++;
                    state = 0;
                break;

                case VK_DOWN:
                    stage--;
                    state = 0;
                break;

                case VK_RETURN:
                    movement = !movement;
                    if (movement) ang = 0;
                break;

                case VK_SPACE:
                    lines_only = !lines_only;
                break;
            }
        }
        break;

        default:
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }

    return 0;
}

void 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);

    state = stage = 0;
    movement = lines_only = false;
}

void DisableOpenGL (HWND hwnd, HDC hDC, HGLRC hRC)
{
    wglMakeCurrent(NULL, NULL);
    wglDeleteContext(hRC);
    ReleaseDC(hwnd, hDC);
}

