Created
November 21, 2012 22:05
-
-
Save neuro-sys/4128171 to your computer and use it in GitHub Desktop.
stuff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <windows.h> | |
| #include <stdio.h> | |
| #include <math.h> | |
| #include <gl/gl.h> | |
| #include <gl/glu.h> | |
| #define WIN32_LEAN_AND_MEAN | |
| #define WCLASSNAME "HEDE" | |
| #define W 640 | |
| #define H 480 | |
| #define PI (3.141592653589793) | |
| #define A2R(A) A* PI / 180 | |
| RECT rect; | |
| HANDLE hndc; | |
| HWND hwnd; | |
| HDC hdc; | |
| HGLRC hglrc; | |
| int i; | |
| int fps; | |
| double eye[3]; | |
| double cam[3] = {0, 0, 1}; | |
| GLfloat triangle_vertices[] = { 0, 1, 0, -1,-1, 0, 1, -1, 0 }; | |
| GLfloat triangle_normals[] = { 0, 0, 1, 0, 0, 1, 0, 0, 1 }; | |
| GLubyte triangle_indices[] = { 0, 1, 2 }; | |
| void draw_triangle(void) | |
| { | |
| glEnableClientState(GL_NORMAL_ARRAY); | |
| glEnableClientState(GL_VERTEX_ARRAY); | |
| glNormalPointer(GL_FLOAT, 0, triangle_normals); | |
| glVertexPointer(3, GL_FLOAT, 0, triangle_vertices); | |
| glPushMatrix(); | |
| glTranslatef(0, 0, -4); | |
| glRotatef(i, 1, 0, 1); | |
| glRotatef(i, 0, 1, 0); | |
| glRotatef(i, 0, 0, 1); | |
| //glDrawArrays(GL_TRIANGLES, 0, 3); | |
| glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_BYTE, triangle_indices); | |
| glPopMatrix(); | |
| glDisableClientState(GL_NORMAL_ARRAY); | |
| glDisableClientState(GL_VERTEX_ARRAY); | |
| } | |
| void render(void) | |
| { | |
| glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
| glMatrixMode(GL_MODELVIEW); | |
| glLoadIdentity(); | |
| gluLookAt(cam[0], cam[1], cam[2], | |
| eye[0], eye[1], eye[2], | |
| 0, 1, 0); | |
| draw_triangle(); | |
| SwapBuffers(hdc); | |
| if (++i >= 360) i = 0; | |
| } | |
| void initLights(void) | |
| { | |
| // set up light colors (ambient, diffuse, specular) | |
| GLfloat lightKa[] = {.2f, .2f, .2f, 1.0f}; // ambient light | |
| GLfloat lightKd[] = {.7f, .7f, .7f, 1.0f}; // diffuse light | |
| GLfloat lightKs[] = {1, 1, 1, 1}; // specular light | |
| glLightfv(GL_LIGHT0, GL_AMBIENT, lightKa); | |
| glLightfv(GL_LIGHT0, GL_DIFFUSE, lightKd); | |
| glLightfv(GL_LIGHT0, GL_SPECULAR, lightKs); | |
| // position the light | |
| float lightPos[4] = {0, 0, 20, 1}; // positional light | |
| glLightfv(GL_LIGHT0, GL_POSITION, lightPos); | |
| glEnable(GL_LIGHT0); // MUST enable each light source after configuration | |
| } | |
| void initGL(void) | |
| { | |
| PIXELFORMATDESCRIPTOR pfd; | |
| ZeroMemory(&pfd, sizeof(pfd)); | |
| pfd.nSize = sizeof(pfd); | |
| pfd.nVersion = 1; | |
| pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER | PFD_TYPE_RGBA; | |
| hdc = GetDC(hwnd); | |
| SetPixelFormat(hdc, ChoosePixelFormat(hdc, &pfd), &pfd); | |
| hglrc = wglCreateContext(hdc); | |
| wglMakeCurrent(hdc, hglrc); | |
| //glShadeModel(GL_SMOOTH); | |
| glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); | |
| glPolygonMode(GL_FRONT_AND_BACK, GL_FLAT); | |
| glEnable(GL_CULL_FACE); | |
| glEnable(GL_DEPTH_TEST); | |
| glEnable(GL_LIGHTING); | |
| glEnable(GL_TEXTURE_2D); | |
| glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); | |
| glEnable(GL_COLOR_MATERIAL); | |
| glClearColor(0, 0, 0, 0); // background color | |
| glClearStencil(0); // clear stencil buffer | |
| glClearDepth(1.0f); // 0 is near, 1 is far | |
| glDepthFunc(GL_LEQUAL); | |
| initLights(); | |
| glMatrixMode(GL_VIEWPORT); | |
| glLoadIdentity; | |
| glViewport(0, 0, W, H); | |
| glMatrixMode(GL_PROJECTION); | |
| glLoadIdentity(); | |
| gluPerspective(65, (float) W/H, 1, 1000); | |
| glMatrixMode(GL_MODELVIEW); | |
| glLoadIdentity(); | |
| } | |
| void destroyGL(void) | |
| { | |
| wglMakeCurrent(NULL, NULL); | |
| wglDeleteContext(hglrc); | |
| ReleaseDC(hwnd, hdc); | |
| } | |
| LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) | |
| { | |
| switch(msg) { | |
| case WM_CREATE: | |
| break; | |
| case WM_PAINT: | |
| return 0; | |
| case WM_MOUSEMOVE: | |
| return 0; | |
| case WM_CHAR: | |
| switch(wparam) { | |
| case 27: | |
| PostQuitMessage(0); | |
| break; | |
| } | |
| return 0; | |
| case WM_DESTROY: | |
| PostQuitMessage(0); | |
| return 0; | |
| } | |
| return DefWindowProc(hwnd, msg, wparam, lparam); | |
| } | |
| int frame_count; | |
| long then; | |
| int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) | |
| { | |
| WNDCLASS wc; | |
| MSG msg; | |
| ZeroMemory(&wc, sizeof(WNDCLASS)); | |
| wc.style = CS_OWNDC | WS_OVERLAPPED; | |
| wc.hbrBackground = (HBRUSH) (COLOR_WINDOW); | |
| wc.hCursor = LoadCursor(NULL, IDC_ARROW); | |
| wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); | |
| wc.lpszClassName = WCLASSNAME; | |
| wc.hInstance = hInstance; | |
| wc.lpfnWndProc = WndProc; | |
| if (!(RegisterClass(&wc))) | |
| return 0; | |
| hwnd = CreateWindow(WCLASSNAME, "meep", WS_OVERLAPPED | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, | |
| W, H, HWND_DESKTOP, NULL, hInstance, NULL); | |
| if (!hwnd) | |
| return 0; | |
| initGL(); | |
| AllocConsole(); | |
| hndc = GetStdHandle(STD_OUTPUT_HANDLE); | |
| ShowWindow(hwnd, SW_SHOW); | |
| WriteConsole(hndc, "hello", strlen("hello"), NULL, NULL); | |
| GetWindowRect(hwnd, &rect); | |
| then = GetTickCount() + 1000; | |
| while (1) | |
| { | |
| long t1, t2; | |
| t1 = GetTickCount(); | |
| if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { | |
| if (msg.message == WM_QUIT) | |
| break; | |
| TranslateMessage(&msg); | |
| DispatchMessage(&msg); | |
| } | |
| render(); | |
| t2 = GetTickCount() - t1; | |
| frame_count++; | |
| if (GetTickCount() > then) { | |
| then = GetTickCount() + 1000; | |
| fps = frame_count; | |
| frame_count = 0; | |
| } | |
| #define FPS 100. | |
| if (t2 <= 1000 / FPS) Sleep(1000 / FPS - t2); | |
| } | |
| destroyGL(); | |
| return 0; | |
| } | |
| int WINAPI StubWinMainCRTStartup(void) | |
| { | |
| ExitProcess(WinMain(GetModuleHandle(NULL), NULL, NULL, 0)); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment