/* * Author: Leng_que * Date: 2009/08/29 * E-mail: leng_que@yahoo.com.cn * Description: Draw a teapot by OpenGL */ #include <windows.h> #pragma comment(linker, "/subsystem:windows") #include "gl/glaux.h" #pragma comment(lib,"Glaux.lib") #pragma comment(lib,"Opengl32.lib") int cx,cy; HGLRC m_hGLRC; HDC hDC; static PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR), // Size of this structure 1, // Version of this structure PFD_DRAW_TO_WINDOW | // Draw to Window (not to bitmap) PFD_SUPPORT_OPENGL | // Support OpenGL calls in window PFD_DOUBLEBUFFER, // Double buffered mode PFD_TYPE_RGBA, // RGBA Color mode 32, // Want 32 bit color 0,0,0,0,0,0, // Not used to select mode 0,0, // Not used to select mode 0,0,0,0,0, // Not used to select mode 16, // Size of depth buffer 0, // Not used to select mode 0, // Not used to select mode 0, // Not used to select mode 0, // Not used to select mode 0,0,0 // Not used to select mode }; LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM ); int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ) { WNDCLASS wndclass; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.hCursor = LoadCursor(NULL,IDC_ARROW); wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION); wndclass.hInstance = hInstance; wndclass.lpfnWndProc = WndProc; wndclass.lpszClassName = "leng_que"; wndclass.lpszMenuName = NULL; wndclass.style = CS_HREDRAW | CS_VREDRAW; RegisterClass( &wndclass ); HWND hwnd = CreateWindow ( wndclass.lpszClassName, "OpenGL", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL ); ShowWindow(hwnd,nShowCmd); UpdateWindow(hwnd); MSG msg; while ( GetMessage( &msg, NULL, 0, 0 ) ) { TranslateMessage(&msg); DispatchMessage(&msg); } UnregisterClass( wndclass.lpszClassName, wndclass.hInstance ); return 0; } LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam) { switch(message) { case WM_CREATE: hDC = GetDC(hwnd); SetPixelFormat( hDC, ChoosePixelFormat(hDC, &pfd), &pfd ); m_hGLRC = wglCreateContext( hDC ); break; case WM_PAINT: wglMakeCurrent(hDC,m_hGLRC); glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT); glPushMatrix(); glColor3f(0,255,0); auxWireTeapot(0.5); glPopMatrix(); glFinish(); wglMakeCurrent(NULL,NULL); SwapBuffers(hDC); break; case WM_SIZE: cx = lParam&0x0000ffff; cy = (lParam&0xffff0000)/0x10000; wglMakeCurrent(hDC,m_hGLRC); glViewport(0,0,cx,cy); wglMakeCurrent(NULL,NULL); break; case WM_LBUTTONDOWN: break; case WM_RBUTTONDOWN: break; case WM_DESTROY: if ( wglGetCurrentContext() != NULL ) wglMakeCurrent(NULL,NULL); if ( m_hGLRC != NULL ) wglDeleteContext(m_hGLRC); PostQuitMessage(0); break; } return DefWindowProc(hwnd,message,wParam,lParam); }