比较简单的win32 OpenGL 程序

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. //  转载自:http://blog.csdn.net/zhuyingqingfen/article/details/22506111
  2. // GLSAMPLE.CPP  
  3. //  by Blaine Hodge  
  4. //  
  5.   
  6. // Includes  
  7.   
  8. #include <windows.h>  
  9. #include <gl/gl.h>  
  10.   
  11. // Function Declarations  
  12.   
  13. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);  
  14. void EnableOpenGL(HWND hWnd, HDC * hDC, HGLRC * hRC);  
  15. void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC);  
  16.   
  17. // WinMain  
  18.   
  19. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,   
  20.                    LPSTR lpCmdLine, int iCmdShow)  
  21. {  
  22.     WNDCLASS wc;  
  23.     HWND hWnd;  
  24.     HDC hDC;  
  25.     HGLRC hRC;  
  26.     MSG msg;  
  27.     BOOL quit = FALSE;  
  28.     float theta = 0.0f;  
  29.       
  30.     // register window class  
  31.     wc.style = CS_OWNDC;  
  32.     wc.lpfnWndProc = WndProc;  
  33.     wc.cbClsExtra = 0;  
  34.     wc.cbWndExtra = 0;  
  35.     wc.hInstance = hInstance;  
  36.     wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );  
  37.     wc.hCursor = LoadCursor( NULL, IDC_ARROW );  
  38.     wc.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );  
  39.     wc.lpszMenuName = NULL;  
  40.     wc.lpszClassName = "GLSample";  
  41.     RegisterClass( &wc );  
  42.       
  43.     // create main window  
  44.     hWnd = CreateWindow(   
  45.         "GLSample""OpenGL Sample",   
  46.         WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,  
  47.         0, 0, 256, 256,  
  48.         NULL, NULL, hInstance, NULL );  
  49.       
  50.     // enable OpenGL for the window  
  51.     EnableOpenGL( hWnd, &hDC, &hRC );  
  52.       
  53.     // program main loop  
  54.     while ( !quit )  
  55.     {  
  56.           
  57.         // check for messages  
  58.         if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE )  )  
  59.         {  
  60.               
  61.             // handle or dispatch messages  
  62.             if ( msg.message == WM_QUIT )   
  63.             {  
  64.                 quit = TRUE;  
  65.             }   
  66.             else   
  67.             {  
  68.                 TranslateMessage( &msg );  
  69.                 DispatchMessage( &msg );  
  70.             }  
  71.               
  72.         }   
  73.         else   
  74.         {  
  75.               
  76.             // OpenGL animation code goes here  
  77.               
  78.             glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );  
  79.             glClear( GL_COLOR_BUFFER_BIT );  
  80.               
  81.             glPushMatrix();  
  82.             glRotatef( theta, 0.0f, 0.0f, 1.0f );  
  83.             glBegin( GL_TRIANGLES );  
  84.             glColor3f( 1.0f, 0.0f, 0.0f ); glVertex2f( 0.0f, 1.0f );  
  85.             glColor3f( 0.0f, 1.0f, 0.0f ); glVertex2f( 0.87f, -0.5f );  
  86.             glColor3f( 0.0f, 0.0f, 1.0f ); glVertex2f( -0.87f, -0.5f );  
  87.             glEnd();  
  88.             glPopMatrix();  
  89.               
  90.             SwapBuffers( hDC );  
  91.               
  92.             theta += 1.0f;  
  93.               
  94.         }  
  95.           
  96.     }  
  97.       
  98.     // shutdown OpenGL  
  99.     DisableOpenGL( hWnd, hDC, hRC );  
  100.       
  101.     // destroy the window explicitly  
  102.     DestroyWindow( hWnd );  
  103.       
  104.     return msg.wParam;  
  105.       
  106. }  
  107.   
  108. // Window Procedure  
  109.   
  110. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)  
  111. {  
  112.       
  113.     switch (message)  
  114.     {  
  115.           
  116.     case WM_CREATE:  
  117.         return 0;  
  118.           
  119.     case WM_CLOSE:  
  120.         PostQuitMessage( 0 );  
  121.         return 0;  
  122.           
  123.     case WM_DESTROY:  
  124.         return 0;  
  125.           
  126.     case WM_KEYDOWN:  
  127.         switch ( wParam )  
  128.         {  
  129.               
  130.         case VK_ESCAPE:  
  131.             PostQuitMessage(0);  
  132.             return 0;  
  133.               
  134.         }  
  135.         return 0;  
  136.       
  137.     default:  
  138.         return DefWindowProc( hWnd, message, wParam, lParam );  
  139.               
  140.     }  
  141.       
  142. }  
  143.   
  144. // Enable OpenGL  
  145.   
  146. void EnableOpenGL(HWND hWnd, HDC * hDC, HGLRC * hRC)  
  147. {  
  148.     PIXELFORMATDESCRIPTOR pfd;  
  149.     int format;  
  150.       
  151.     // get the device context (DC)  
  152.     *hDC = GetDC( hWnd );  
  153.       
  154.     // set the pixel format for the DC  
  155.     ZeroMemory( &pfd, sizeof( pfd ) );  
  156.     pfd.nSize = sizeof( pfd );  
  157.     pfd.nVersion = 1;  
  158.     pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;  
  159.     pfd.iPixelType = PFD_TYPE_RGBA;  
  160.     pfd.cColorBits = 24;  
  161.     pfd.cDepthBits = 16;  
  162.     pfd.iLayerType = PFD_MAIN_PLANE;  
  163.     format = ChoosePixelFormat( *hDC, &pfd );  
  164.     SetPixelFormat( *hDC, format, &pfd );  
  165.       
  166.     // create and enable the render context (RC)  
  167.     *hRC = wglCreateContext( *hDC );  
  168.     wglMakeCurrent( *hDC, *hRC );  
  169.       
  170. }  
  171.   
  172. // Disable OpenGL  
  173.   
  174. void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC)  
  175. {  
  176.     wglMakeCurrent( NULL, NULL );  
  177.     wglDeleteContext( hRC );  
  178.     ReleaseDC( hWnd, hDC );  
  179. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值