1 #include <atlbase.h> 2 #include <atlwin.h> 3 #include <gl/glew.h> 4 #include <gl/GL.h> 5 #pragma comment(lib, "opengl32.lib") 6 extern CComModule _Module; 7 8 CComModule _Module; 9 10 class MyWindow : public CWindowImpl<MyWindow, CWindow, CFrameWinTraits> 11 { 12 public: 13 MyWindow() 14 : m_hDC(0) 15 , m_hGLRC(0) 16 { 17 Create(NULL, rcDefault); 18 } 19 public: 20 DECLARE_WND_CLASS(_T("MyWindow")); 21 BEGIN_MSG_MAP(MyWindow) 22 MESSAGE_HANDLER(WM_CREATE, OnCreate) 23 MESSAGE_HANDLER(WM_SIZE, OnSize) 24 MESSAGE_HANDLER(WM_PAINT, OnPaint) 25 MESSAGE_HANDLER(WM_ERASEBKGND, OnEraseBkgnd) 26 MESSAGE_HANDLER(WM_DESTROY, OnDestroy) 27 END_MSG_MAP() 28 public: 29 LRESULT OnCreate(UINT msg, WPARAM wParam, LPARAM lParam, BOOL & bHandled) 30 { 31 m_hDC = GetDC(); 32 33 PIXELFORMATDESCRIPTOR pfd = { 34 sizeof(PIXELFORMATDESCRIPTOR), /* size */ 35 1, /* version */ 36 PFD_SUPPORT_OPENGL | 37 PFD_DRAW_TO_WINDOW | 38 PFD_DOUBLEBUFFER, /* support double-buffering */ 39 PFD_TYPE_RGBA, /* color type */ 40 16, /* prefered color depth */ 41 0, 0, 0, 0, 0, 0, /* color bits (ignored) */ 42 0, /* no alpha buffer */ 43 0, /* alpha bits (ignored) */ 44 0, /* no accumulation buffer */ 45 0, 0, 0, 0, /* accum bits (ignored) */ 46 16, /* depth buffer */ 47 0, /* no stencil buffer */ 48 0, /* no auxiliary buffers */ 49 PFD_MAIN_PLANE, /* main layer */ 50 0, /* reserved */ 51 0, 0, 0, /* no layer, visible, damage masks */ 52 }; 53 int pixelFormat = ChoosePixelFormat(m_hDC, &pfd); 54 SetPixelFormat(m_hDC, pixelFormat, &pfd); 55 m_hGLRC = wglCreateContext(m_hDC); 56 if (!m_hGLRC) 57 { 58 PostQuitMessage(0); 59 } 60 wglMakeCurrent(m_hDC, m_hGLRC); 61 glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 62 return true; 63 } 64 65 LRESULT OnSize(UINT msg, WPARAM wParam, LPARAM lParam, BOOL & bHandled) 66 { 67 return true; 68 } 69 70 LRESULT OnPaint(UINT msg, WPARAM wParam, LPARAM lParam, BOOL & bHandled) 71 { 72 if (m_hGLRC) 73 { 74 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 75 } 76 if (m_hDC) 77 SwapBuffers(m_hDC); 78 return true; 79 } 80 81 LRESULT OnEraseBkgnd(UINT msg, WPARAM wParam, LPARAM lParam, BOOL & bHandled) 82 { 83 84 return false; 85 } 86 87 LRESULT OnDestroy(UINT msg, WPARAM wParam, LPARAM lParam, BOOL & bHandled) 88 { 89 wglMakeCurrent(NULL, NULL); 90 if (m_hGLRC) 91 wglDeleteContext(m_hGLRC); 92 if (m_hDC) 93 ReleaseDC(m_hDC); 94 m_hDC = 0; 95 m_hGLRC = 0; 96 PostQuitMessage(0); 97 return true; 98 } 99 public: 100 void Show() 101 { 102 ShowWindow(SW_SHOW); 103 UpdateWindow(); 104 } 105 private: 106 HDC m_hDC; 107 HGLRC m_hGLRC; 108 }; 109 110 111 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 112 { 113 _Module.Init(NULL, hInstance); 114 MyWindow win; 115 win.Show(); 116 MSG msg; 117 while (GetMessage(&msg, NULL, 0, 0)) 118 { 119 TranslateMessage(&msg); 120 DispatchMessage(&msg); 121 } 122 _Module.Term(); 123 return 0; 124 }
原文:http://www.cnblogs.com/nmgxbc/p/4860158.html