// // Generic OpenGl app // #include #include #include #include #include #include // Windows globals and defines char szAppName[]="Generic"; HWND ghWnd; HDC ghDC; HGLRC ghRC; // opengl rendering context #define WIDTH 800 #define HEIGHT 600 LONG WINAPI MainWndProc (HWND, UINT, WPARAM, LPARAM); BOOL bSetupPixelFormat(HDC); GLvoid initializeGL(GLsizei, GLsizei); GLvoid drawScene(HWND hWnd); static GLint box; static GLint pyramid; static GLint dodec; GLvoid MakeBox(GLvoid); //void MakeRectagon(void); GLvoid MakePyramid(GLvoid); GLvoid MakeDodec(GLvoid); GLdouble color[3] = { 0.6, 0.0, 0.0 }; POINT angle = { 30, 60 }; GLfloat position[3] = { -0.5, 0.0, -5.0 }; // // // int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; WNDCLASS wndclass; // Register frame class wndclass.style = 0; wndclass.lpfnWndProc = (WNDPROC)MainWndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon (hInstance, szAppName); wndclass.hCursor = LoadCursor(NULL,IDC_ARROW); wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW+1); wndclass.lpszMenuName = szAppName; wndclass.lpszClassName = szAppName; if (!RegisterClass (&wndclass) ) return FALSE; // Create frame ghWnd = CreateWindow(szAppName, "Generic", WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, // opengl requires the clips CW_USEDEFAULT, CW_USEDEFAULT, WIDTH, HEIGHT, NULL, NULL, hInstance, NULL); // Make sure window created if (!ghWnd) return FALSE; // Show and update main window ShowWindow (ghWnd, nCmdShow); UpdateWindow (ghWnd); while (GetMessage(&msg, NULL, 0, 0) ) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } // main window procedure LONG WINAPI MainWndProc ( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { LONG lRet = 1; RECT rect; PAINTSTRUCT ps; // static BOOL Right = TRUE; switch (uMsg) { case WM_CREATE: ghDC = GetDC(hWnd); if (!bSetupPixelFormat(ghDC)) PostQuitMessage(0); ghRC = wglCreateContext(ghDC); wglMakeCurrent(ghDC, ghRC); GetClientRect(hWnd, &rect); initializeGL(rect.right, rect.bottom); break; case WM_ERASEBKGND: return TRUE; break; case WM_KEYDOWN: switch (wParam) { case VK_F1: color[0]= color[0] +.01; InvalidateRect(hWnd, NULL, TRUE); break; case VK_F2: color[1]= color[1] +.01; InvalidateRect(hWnd, NULL, TRUE); break; case VK_F3: color[2]= color[2] +.01; InvalidateRect(hWnd, NULL, TRUE); break; case VK_F4: color[0] = 0; color[1] = 0; color[2] = 0; InvalidateRect(hWnd, NULL, TRUE); break; case VK_F5: if (angle.x == 360) { angle.x = 0; } else { angle.x = angle.x+10; } InvalidateRect(hWnd, NULL, TRUE); break; case VK_F6: if (angle.y == 360) { angle.y = 0; } else { angle.y = angle.y + 10; } InvalidateRect(hWnd, NULL, TRUE); break; case VK_NUMPAD8: position[1] = position[1] + 0.1; InvalidateRect(hWnd, NULL, TRUE); break; case VK_NUMPAD2: position[1] = position[1] - 0.1; InvalidateRect(hWnd, NULL, TRUE); break; case VK_NUMPAD6: position[0] = position[0] + 0.1; InvalidateRect(hWnd, NULL, TRUE); break; case VK_NUMPAD4: position[0] = position[0] - 0.1; InvalidateRect(hWnd, NULL, TRUE); break; case VK_NUMPAD9: position[2] = position[2] + 0.1; InvalidateRect(hWnd, NULL, TRUE); break; case VK_NUMPAD1: position[2] = position[2] - 0.1; InvalidateRect(hWnd, NULL, TRUE); break; case VK_ESCAPE: PostQuitMessage(0); break; default: break; } break; case WM_PAINT: BeginPaint(hWnd, &ps); drawScene(hWnd); EndPaint(hWnd, &ps); break; case WM_DESTROY: if (ghRC) wglDeleteContext(ghRC); if (ghDC) ReleaseDC(ghWnd,ghDC); PostQuitMessage (0); break; default: lRet = DefWindowProc (hWnd, uMsg, wParam, lParam); break; } return lRet; } BOOL bSetupPixelFormat(HDC hdc) { PIXELFORMATDESCRIPTOR *ppfd; int pixelformat; PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR), 1, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, PFD_TYPE_RGBA, 24, 0,0,0,0,0,0, 0, 0, 0, 0,0,0,0, 32, 0, 0, PFD_MAIN_PLANE, 0, 0,0,0 }; pfd.cColorBits = GetDeviceCaps(ghDC, BITSPIXEL); ppfd = &pfd; pixelformat = ChoosePixelFormat(hdc, ppfd); if ( (pixelformat = ChoosePixelFormat(hdc, ppfd)) == 0) { MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK); return FALSE; } if (SetPixelFormat(hdc, pixelformat, ppfd) == FALSE) { MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK); return FALSE; } return TRUE; } GLvoid initializeGL(GLsizei width, GLsizei height) { // // Set up the mapping of 3-space to screen space // glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING) ; glEnable(GL_LIGHT0) ; glClearColor(0.0f, 0.0f, 0.0f, 0.0f) ; GLdouble gldAspect = (GLdouble) width/ (GLdouble) height; glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(30.0, gldAspect, 1.0, 10.0); glViewport(0, 0, width, height); // Set the material color to follow the current color glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE) ; glEnable(GL_COLOR_MATERIAL) ; // preset to modelview for drawing scene glMatrixMode(GL_MODELVIEW); glLoadIdentity(); MakeBox(); MakePyramid(); MakeDodec(); } GLvoid drawScene(HWND hWnd) { // Set up some colors static GLdouble red[3] = {0.8, 0.0, 0.0 } ; static GLdouble green[3] = {0.0, 0.75, 0.0} ; static GLdouble purple[3] = {1.0, 0.14, 0.6667} ; static GLdouble blue[3] = {0.0, 0.0, 0.8 }; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // // Draw the box. // // Change the current color to green. glColor3dv(color) ; glLoadIdentity(); glTranslated(position[0], position[1], position[2]); glRotated(angle.x, 0.0, 0.1, 0.0); glRotated(angle.y, 1.0, 0.0, 0.0); glCallList(box); //Swap Buffers SwapBuffers(ghDC) ; } GLvoid MakeBox(GLvoid) { GLdouble size = 0.5 ; // // Build a display list for the cube. // box = glGenLists(1); glNewList(box, GL_COMPILE) ; // // Draw the six faces of the cube. // glBegin(GL_POLYGON); // right glNormal3d( 0.0, 0.0, size); glVertex3d( size, size, size); glVertex3d(-size, size, size); glVertex3d(-size, -size, size); glVertex3d( size, -size, size); glEnd(); glBegin(GL_POLYGON); //left glNormal3d( 0.0, 0.0, -size); glVertex3d( size, size, -size); glVertex3d( size, -size, -size); glVertex3d(-size, -size, -size); glVertex3d(-size, size, -size); glEnd(); glBegin(GL_POLYGON); //Front glNormal3d(-size, 0.0, 0.0); glVertex3d(-size, size, size); glVertex3d(-size, size, -size); glVertex3d(-size, -size, -size); glVertex3d(-size, -size, size); glEnd(); glBegin(GL_POLYGON); //Back glNormal3d(size, 0.0, 0.0); glVertex3d(size, size, size); glVertex3d(size, -size, size); glVertex3d(size, -size, -size); glVertex3d(size, size, -size); glEnd(); glBegin(GL_POLYGON); //Top glNormal3d( 0.0, size, 0.0); glVertex3d(-size, size, -size); glVertex3d(-size, size, size); glVertex3d( size, size, size); glVertex3d( size, size, -size); glEnd(); glBegin(GL_POLYGON); //Bottom glNormal3d( 0.0, -size, 0.0); glVertex3d(-size, -size, -size); glVertex3d( size, -size, -size); glVertex3d( size, -size, size); glVertex3d(-size, -size, size); glEnd(); glEndList() ; } GLvoid MakePyramid(GLvoid) { GLdouble size = 0.5 ; pyramid = glGenLists(1); // // Build a display list for the pyramid. // glNewList(pyramid, GL_COMPILE) ; // // Draw the pyramid // glBegin(GL_POLYGON); glNormal3d(-size,-size,-size) ; glVertex3d( size,-size,-size) ; //2 glVertex3d(-size,-size, size) ; //3 glVertex3d(-size, size,-size) ; //4 glEnd() ; glBegin(GL_POLYGON); glNormal3d(-size, size, size) ; glVertex3d( size, size, size) ; //1 glVertex3d(-size, size,-size) ; //4 glVertex3d(-size,-size, size) ; //3 glEnd() ; glBegin(GL_POLYGON); glNormal3d( size, size,-size) ; glVertex3d( size, size, size) ; //1 glVertex3d( size,-size,-size) ; //2 glVertex3d(-size, size,-size) ; //4 glEnd() ; glBegin(GL_POLYGON); glNormal3d( size,-size, size) ; glVertex3d( size, size, size) ; //1 glVertex3d(-size,-size, size) ; //3 glVertex3d( size,-size,-size) ; //2 glEnd() ; glEndList() ; } GLvoid MakeDodec(GLvoid) { GLdouble dsize = 0.35 ; // Size for Dodecadedron // // Build display list for Dodecahedron // GLdouble t = ((sqrt(5) - 1.0) / 2.0) ; GLdouble tt = t*t ; t *= dsize ; tt *= dsize ; dodec = glGenLists(1); glNewList(dodec, GL_COMPILE) ; glBegin(GL_POLYGON); // Face 0 glNormal3d(0, dsize, t) ; glVertex3d( t, t, t) ; // Vertex 0 glVertex3d( tt,dsize, 0) ; // Vertex 7 glVertex3d(-tt,dsize, 0) ; // Vertex 8 glVertex3d( -t, t, t) ; // Vertex 15 glVertex3d( 0, tt,dsize) ; // Vertex 4 glEnd() ; glBegin(GL_POLYGON); // Face 1 glNormal3d(0, dsize, -t) ; glVertex3d( 0, tt,-dsize) ; // Vertex 10 glVertex3d( -t, t, -t) ; // Vertex 9 glVertex3d(-tt,dsize, 0) ; // Vertex 8 glVertex3d( tt,dsize, 0) ; // Vertex 7 glVertex3d( t, t, -t) ; // Vertex 6 glEnd() ; glBegin(GL_POLYGON); // Face 2 glNormal3d(dsize, t, 0) ; glVertex3d( t, t, t) ; // Vertex 0 glVertex3d(dsize, 0, tt) ; // Vertex 1 glVertex3d(dsize, 0, -tt) ; // Vertex 5 glVertex3d( t, t, -t) ; // Vertex 6 glVertex3d( tt,dsize, 0) ; // Vertex 7 glEnd() ; glBegin(GL_POLYGON); // Face 3 glNormal3d(dsize, -t, 0) ; glVertex3d(dsize, 0, tt) ; // Vertex 1 glVertex3d( t, -t, t) ; // Vertex 2 glVertex3d( tt,-dsize, 0) ; // Vertex 12 glVertex3d( t, -t, -t) ; // Vertex 11 glVertex3d(dsize, 0, -tt) ; // Vertex 5 glEnd() ; glBegin(GL_POLYGON); // Face 4 glNormal3d(0, -dsize, -t) ; glVertex3d( t, -t, -t) ; // Vertex 11 glVertex3d( tt,-dsize, 0) ; // Vertex 12 glVertex3d(-tt,-dsize, 0) ; // Vertex 13 glVertex3d( -t, -t, -t) ; // Vertex 16 glVertex3d( 0, -tt,-dsize) ; // Vertex 17 glEnd() ; glBegin(GL_POLYGON); // Face 5 glNormal3d(0, -dsize, t) ; glVertex3d( -t, -t, t) ; // Vertex 18 glVertex3d(-tt,-dsize, 0) ; // Vertex 13 glVertex3d( tt,-dsize, 0) ; // Vertex 12 glVertex3d( t, -t, t) ; // Vertex 2 glVertex3d( 0, -tt,dsize) ; // Vertex 3 glEnd() ; glBegin(GL_POLYGON); // Face 6 glNormal3d(t, 0, dsize) ; glVertex3d( 0, tt,dsize) ; // Vertex 4 glVertex3d( 0, -tt,dsize) ; // Vertex 3 glVertex3d( t, -t, t) ; // Vertex 2 glVertex3d(dsize, 0, tt) ; // Vertex 1 glVertex3d( t, t, t) ; // Vertex 0 glEnd() ; glBegin(GL_POLYGON); // Face 7 glNormal3d(-t, 0, dsize) ; glVertex3d( 0, -tt,dsize) ; // Vertex 3 glVertex3d( 0, tt,dsize) ; // Vertex 4 glVertex3d( -t, t, t) ; // Vertex 15 glVertex3d(-dsize, 0, tt) ; // Vertex 14 glVertex3d( -t, -t, t) ; // Vertex 18 glEnd() ; glBegin(GL_POLYGON); // Face 8 glNormal3d(t, 0, -dsize) ; glVertex3d( 0, -tt,-dsize) ; // Vertex 17 glVertex3d( 0, tt,-dsize) ; // Vertex 10 glVertex3d( t, t, -t) ; // Vertex 6 glVertex3d(dsize, 0, -tt) ; // Vertex 5 glVertex3d( t, -t, -t) ; // Vertex 11 glEnd() ; glBegin(GL_POLYGON); // Face 9 glNormal3d(-t, 0, -dsize) ; glVertex3d( -t, t, -t) ; // Vertex 9 glVertex3d( 0, tt,-dsize) ; // Vertex 10 glVertex3d( 0, -tt,-dsize) ; // Vertex 17 glVertex3d( -t, -t, -t) ; // Vertex 16 glVertex3d(-dsize, 0, -tt) ; // Vertex 19 glEnd() ; glBegin(GL_POLYGON); // Face 10 glNormal3d(-dsize, t, 0) ; glVertex3d( -tt,dsize, 0) ; // Vertex 8 glVertex3d( -t, t, -t) ; // Vertex 9 glVertex3d(-dsize, 0, -tt) ; // Vertex 19 glVertex3d(-dsize, 0, tt) ; // Vertex 14 glVertex3d( -t, t, t) ; // Vertex 15 glEnd() ; glBegin(GL_POLYGON); // Face 11 glNormal3d(-dsize, -t, 0) ; glVertex3d(-dsize, 0, -tt) ; // Vertex 19 glVertex3d( -t, -t, -t) ; // Vertex 16 glVertex3d( -tt,-dsize, 0) ; // Vertex 13 glVertex3d( -t, -t, t) ; // Vertex 18 glVertex3d(-dsize, 0, tt) ; // Vertex 14 glEnd() ; glEndList() ; }