#include <windows.h>
#include <stdio.h>
#include <GL/gl.h>
#include <GL/glu.h>


static GLfloat vertices[48] = {
    0.0f, 0.0f, 0.0f,  -1.0f, -1.0f, -1.0f,
    0.0f, 0.0f, 1.0f,  -1.0f, -1.0f, +1.0f,
    0.0f, 1.0f, 0.0f,  -1.0f, +1.0f, -1.0f,
    0.0f, 1.0f, 1.0f,  -1.0f, +1.0f, +1.0f,
    1.0f, 0.0f, 0.0f,  +1.0f, -1.0f, -1.0f,
    1.0f, 0.0f, 1.0f,  +1.0f, -1.0f, +1.0f,
    1.0f, 1.0f, 0.0f,  +1.0f, +1.0f, -1.0f,
    1.0f, 1.0f, 1.0f,  +1.0f, +1.0f, +1.0f,
};

static GLuint indices[36] = {
    0, 2, 6,   0, 6, 4,
    4, 6, 7,   4, 7, 5,
    5, 7, 3,   5, 3, 1,
    1, 3, 2,   1, 2, 0,
    2, 3, 7,   2, 7, 6,
    1, 0, 4,   1, 4, 5,
};


static void InvertCube(void) {
    // modify the cube model in-place so that all vertices are at the opposite
    // corner of the cube; adjust the indices accordingly, too
    int i, temp;
    GLfloat *pf = vertices;
    for (i = 0;  i < 8;  ++i) {
        pf[0] = 1.0 - pf[0];
        pf[1] = 1.0 - pf[1];
        pf[2] = 1.0 - pf[2];
        pf[3] = -pf[3];
        pf[4] = -pf[4];
        pf[5] = -pf[5];
        pf += 6;
    }
    for (i = 1;  i < 36;  i += 3) {
        temp = indices[i];
        indices[i] = indices[i+1];
        indices[i+1] = temp;
    }
}


static WNDCLASS wc = {
    CS_OWNDC,       // style
    DefWindowProc,  // lpfnWndProc
    0,              // cbClsExtra
    0,              // cbWndExtra
    0,              // hInstance
    0,              // hIcon
    0,              // hCursor
    0,              // hbrBackground
    0,              // lpszMenuName
    "nvbug"         // lpszClassName
};

static PIXELFORMATDESCRIPTOR pfd = {
    sizeof(PIXELFORMATDESCRIPTOR), // nSize
    1,              // nVersion
    PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER, // dwFlags
    PFD_TYPE_RGBA,  // iPixelType
    32,             // cColorBits
    0,              // cRedBits
    0,              // cRedShift
    0,              // cGreenBits
    0,              // cGreenShift
    0,              // cBlueBits
    0,              // cBlueShift
    0,              // cAlphaBits
    0,              // cAlphaShift
    0,              // cAccumBits
    0,              // cAccumRedBits
    0,              // cAccumGreenBits
    0,              // cAccumBlueBits
    0,              // cAccumAlphaBits
    32,             // cDepthBits
    8,              // cStencilBits
    0,              // cAuxBuffers
    PFD_MAIN_PLANE, // iLayerType
    0,              // bReserved
    0,              // dwLayerMask
    0,              // dwVisibleMask
    0,              // dwDamageMask
};


static LRESULT CALLBACK HandleMessage(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch(uMsg) {
        case WM_CLOSE:
        case WM_QUIT:
        case WM_DESTROY:
            ExitProcess(0);
        default:
            break;
    }
    return DefWindowProc(hwnd,uMsg,wParam,lParam);
}



int main(int argc, char *argv[]) {
    HWND hWnd;
    HDC hDC;
    HGLRC hRC;
    MSG msg;
    HKEY reg;
    BYTE cpu_name[256] = "(unknown)";
    DWORD count = 255;
    double t;
    GLuint list;

    // create a simple window
    wc.lpfnWndProc = HandleMessage;
    wc.hCursor = LoadCursor(0, IDC_ARROW);
    RegisterClass(&wc);
    hWnd = CreateWindowEx(
        WS_EX_APPWINDOW | WS_EX_WINDOWEDGE,
        "nvbug", "nVidia driver bug?",
        WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
        0, 0,
        512 + GetSystemMetrics(SM_CXFIXEDFRAME) * 2,
        256 + GetSystemMetrics(SM_CYFIXEDFRAME) + GetSystemMetrics(SM_CYCAPTION),
        NULL, NULL, GetModuleHandle(0), NULL);
    hDC = GetDC(hWnd);
    SetPixelFormat(hDC, ChoosePixelFormat(hDC, &pfd), &pfd);
    hRC = wglCreateContext(hDC);
    wglMakeCurrent(hDC, hRC);
    ShowWindow(hWnd, SW_SHOW);

    // startup baner / system information
    printf("nVidia driver bug check\n=======================\n\n"
"Normally, the two cubes should look exactly the same. However, on some nVidia\n"
"card/driver combinations on multi-threaded or multi-core CPUs, they look\n"
"different, which is a bug in their OpenGL implementation, IMHO.\n\n"
"Please send an e-mail to <martin.fiedler@gmx.net> with the result (same or\n"
"different), your graphics driver version, and the following information:\n\n");
    printf("OpenGL vendor: %s\n", glGetString(GL_VENDOR));
    printf("OpenGL renderer: %s\n", glGetString(GL_RENDERER));
    printf("OpenGL version: %s\n", glGetString(GL_VERSION));
    RegOpenKeyEx(HKEY_LOCAL_MACHINE,
        "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
        0, KEY_QUERY_VALUE, &reg);
    cpu_name[255] = '\0';
    RegQueryValueEx(reg, "ProcessorNameString", NULL, NULL, cpu_name, &count);
    printf("CPU name: %s\n", cpu_name);
    RegCloseKey(reg);

    // initialize basic OpenGL stuff
    glClearColor(0.2, 0.3, 0.4, 0.5);
    glMatrixMode(GL_PROJECTION);
    gluPerspective(45.0, 1.0, 0.1, 10.0);
    glMatrixMode(GL_MODELVIEW);
    gluLookAt(0.0, 0.0, -5.0,  0.0, 0.0, 0.0,  0.0, 1.0, 0.0);
    glPolygonMode(GL_BACK, GL_LINE);
    glEnable(GL_DEPTH_TEST);
    list = glGenLists(2);

    // compile the original model into a display list
    glNewList(list, GL_COMPILE);
    glInterleavedArrays(GL_C3F_V3F, 0, (const GLvoid*) vertices);
    glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, (const GLvoid*) indices);
    glEndList();

    // modify the cube a few times in-memory
    for (count = 111111;  count;  --count)
        InvertCube();

    // compile the modified model into a display list
    glNewList(list + 1, GL_COMPILE);
    glInterleavedArrays(GL_C3F_V3F, 0, (const GLvoid*) vertices);
    glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, (const GLvoid*) indices);
    glEndList();

    // main loop
    while (1) {
        while (PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

        // draw the two cubes
        t = 0.001 * GetTickCount();
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glPushMatrix();
        glRotated(t * 23.0, 1.0, 0.0, 0.0);
        glRotated(t * 37.0, 0.0, 1.0, 0.0);
        glRotated(t * 43.0, 0.0, 0.0, 1.0);
        glViewport(0, 0, 256, 256);
        glCallList(list);
        glViewport(256, 0, 256, 256);
        glCallList(list + 1);
        glPopMatrix();
        SwapBuffers(hDC);
    }
    return 0;
}



