蕙麓

Email:oxionghui@gmail.com

  C++博客 :: 首页 :: 新随笔 :: 联系 ::  :: 管理 ::
OpenGL.h,OpenGL.cpp主要是图形处理相关的操作。main.cpp主程序


 
OpenGL.h: interface for the OpenGL class.
 2 //
 3 //////////////////////////////////////////////////////////////////////
 4 
 5 #if !defined(AFX_OPENGL_H__7078166F_1240_4FB7_8CD9_A3E45C609EF5__INCLUDED_)
 6 #define AFX_OPENGL_H__7078166F_1240_4FB7_8CD9_A3E45C609EF5__INCLUDED_
 7 
 8 #if _MSC_VER > 1000
 9 #pragma once
10 #endif // _MSC_VER > 1000
11 
12 #include <windows.h>
13 #include <gl/gl.h>
14 #include <gl/glu.h>
15 
16 class OpenGL  
17 {
18 private:
19     HDC        hDC;            // 
20     HGLRC    hRC;            // 
21 public:
22     OpenGL();
23     virtual ~OpenGL();
24     BOOL    SetupPixelFormat(HDC hDC);   
25     void    ReSizeGLScene(int Width, int Height);  //初始化视口
26     void    Render();      //渲染
27     void    CleanUp();
28 
29 };
30 
31 #endif // !defined(AFX_OPENGL_H__7078166F_1240_4FB7_8CD9_A3E45C609EF5__INCLUDED_)
32 

OpenGL.cpp   
 1 // OpenGL.cpp: implementation of the OpenGL class.
 2 //
 3 //////////////////////////////////////////////////////////////////////
 4 #include "OpenGL.h"
 5 
 6 //////////////////////////////////////////////////////////////////////
 7 // Construction/Destruction
 8 //////////////////////////////////////////////////////////////////////
 9 
10 OpenGL::OpenGL()
11 {
12 
13 }
14 
15 OpenGL::~OpenGL()
16 {    
17     CleanUp();
18 }
19 
20 BOOL OpenGL::SetupPixelFormat(HDC hDC0)  //设置像素格式
21 {    
22     int nPixelFormat;                                // 象素点格式
23     hDC = hDC0;
24     PIXELFORMATDESCRIPTOR pfd = { 
25         sizeof(PIXELFORMATDESCRIPTOR),    // pfd结构的大小 
26             1,                                          // 版本号 
27             PFD_DRAW_TO_WINDOW |         // 支持在窗口中绘图 
28             PFD_SUPPORT_OPENGL |           // 支持 OpenGL 
29             PFD_DOUBLEBUFFER,                // 支持双缓存模式 
30             PFD_TYPE_RGBA,                    // RGBA 颜色模式 
31             24,                                      // 24 位颜色深度 
32             000000,                     // 忽略颜色位 
33             0,                                        
34             0,                                        
35             0,                                     
36             0000,                           
37             16,                                   // 深度缓存     
38             0,                                      
39             0,                                     
40             PFD_MAIN_PLANE,            
41             0,                                   
42             000                            
43     }; 
44     if (!(nPixelFormat = ChoosePixelFormat(hDC, &pfd))) 
45     {
46         MessageBox(NULL,"没找到合适的显示模式","Error",MB_OK|MB_ICONEXCLAMATION);
47         return FALSE;
48     }
49     SetPixelFormat(hDC,nPixelFormat,&pfd);//设置当前设备的像素点格式
50     hRC = wglCreateContext(hDC);           //获取渲染描述句柄
51     wglMakeCurrent(hDC, hRC);               //激活渲染描述句柄
52     
53     return TRUE;
54 }
55 
56 void OpenGL::ReSizeGLScene(int Width, int Height)
57 {    
58     glViewport(0,0,Width,Height);      //重置当前的视口            
59     glMatrixMode(GL_PROJECTION);   //选择投影矩阵            
60     glLoadIdentity();                      //重置投影矩阵        
61     gluPerspective                         //设置视口的大小                        
62         ( 54.0f,                             
63         (GLfloat)Width/(GLfloat)Height,    
64         0.1f,                                
65         3000.0f                        
66         );
67     
68     glMatrixMode(GL_MODELVIEW);       //选择模型观察矩阵            
69     glLoadIdentity();                         //重置模型观察矩阵
70     
71 }
72 
73 void OpenGL::Render()                                   //OpenGL图形渲染
74 {    
75     glClearColor(0.0f0.0f0.3f1.0f);              // 设置刷新背景色
76     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); // 刷新背景
77     glLoadIdentity();                                    // 重置模型观察矩阵
78     
79     //画矩形
80     glBegin(GL_QUADS);
81     glVertex3f(-1.0f,-1.0f,-5.0f);
82     glVertex3f(1.0f,-1.0f,-5.0f);
83     glVertex3f(1.0f,1.0f,-5.0f);
84     glVertex3f(-1.0f,1.0f,-5.0f);
85     glEnd();
86 
87     SwapBuffers(hDC);                                 // 切换缓冲区
88 }
89 void OpenGL::CleanUp()
90 {    
91     wglMakeCurrent(hDC, NULL);                       //清除OpenGL
92     wglDeleteContext(hRC);                            //清除OpenGL
93 }
94 

main.cpp
  1 #include "OpenGL.h"
  2 
  3 OpenGL* m_OpenGL;
  4 HDC        hDC;         //窗口的设备环境句柄 
  5 HWND    hWnd = NULL; //窗口句柄 
  6 int        Width = 800// 窗口宽
  7 int        Height = 600;// 窗口高
  8 int        bits  = 24;     // 颜色深度
  9 
 10 void GameLoop()
 11 {   
 12     MSG msg;    //消息结构
 13     BOOL fMessage;
 14     PeekMessage(&msg, NULL, 0U0U, PM_REMOVE);
 15     while(msg.message != WM_QUIT)    // 
 16     {   
 17         fMessage = PeekMessage(&msg, NULL, 0U0U, PM_REMOVE);
 18         if(fMessage)                      //
 19         { 
 20             TranslateMessage(&msg);
 21             DispatchMessage(&msg);
 22         }
 23         else  
 24             m_OpenGL->Render();    //无消息
 25     }
 26 }
 27 
 28 LRESULT WINAPI MsgProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam )// 消息响应处理
 29 {    
 30     switch(message)
 31     {
 32     case WM_CREATE:                        // 建立窗口
 33         hDC = GetDC(hWnd);                // 获取当前窗口的设备句柄
 34         m_OpenGL->SetupPixelFormat(hDC);// 调用显示模式安装功能
 35         break;
 36 
 37     case WM_CLOSE:                        // 关闭窗口
 38         m_OpenGL->CleanUp();            // 结束处理
 39         PostQuitMessage(0);
 40         break;
 41 
 42     case WM_SIZE:                         // 窗口尺寸变化
 43         Height = HIWORD(lParam);      // 窗口的高
 44         Width  = LOWORD(lParam);     // 窗口的宽
 45         if (Height == 0)                   // 防止被零除
 46             Height = 1;                   
 47         m_OpenGL->ReSizeGLScene(Width,Height);  //重置窗口
 48         break;
 49 
 50     case WM_DESTROY:                    // 退出消息
 51         PostQuitMessage(0);
 52         break;
 53         
 54     case WM_KEYUP:                        // 按ESC退出
 55         switch (wParam)
 56         {
 57         case VK_ESCAPE:
 58             m_OpenGL->CleanUp();    // 结束处理
 59             PostQuitMessage(0);
 60         
 61             break;
 62         } 
 63         default:        
 64             break;
 65     }
 66     return (DefWindowProc(hWnd, message, wParam, lParam));
 67 }
 68 
 69 INT WINAPI WinMain(HINSTANCE hInst,HINSTANCE,LPSTR,INT )// WinMain程序入口
 70 {
 71     // 注册窗口类
 72     bool fullScreen = TRUE;      //全屏标志,默认全屏
 73     DWORD    dwExStyle;        // Window 扩展风格
 74     DWORD    dwStyle;           // Window 窗口风格
 75     RECT    windowRect;        // 窗口尺寸
 76     int        nX = 0,nY = 0;
 77     if (MessageBox(NULL,"使用全屏模式吗?""选择显示模式",
 78         MB_YESNO|MB_ICONQUESTION|MB_SYSTEMMODAL) == IDNO)//选择窗口模式
 79     {
 80         fullScreen = false;
 81     }                                    
 82     if (fullScreen)                                  // 选择全屏模式
 83     {
 84         DEVMODE dmScreenSettings;         // 设备模式
 85         memset(&dmScreenSettings,0,sizeof(dmScreenSettings));    // 确保内存分配
 86         dmScreenSettings.dmSize = sizeof(dmScreenSettings);        // Devmode 结构的大小
 87         dmScreenSettings.dmPelsWidth = Width;          // 屏幕宽
 88         dmScreenSettings.dmPelsHeight = Height;        // 屏幕高
 89         dmScreenSettings.dmBitsPerPel = 16;             // 色彩深度
 90         dmScreenSettings.dmDisplayFrequency = 75;   // 
 91         dmScreenSettings.dmFields = DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT|DM_DISPLAYFREQUENCY;
 92         if (ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
 93         {
 94             fullScreen = FALSE;
 95         }
 96         dwExStyle = WS_EX_APPWINDOW; // Window 扩展风格
 97         dwStyle = WS_POPUP;                // Window 窗口风格
 98         ShowCursor(FALSE);                   // 隐藏鼠标
 99     }
100     else
101     {
102         dwExStyle = WS_EX_APPWINDOW|WS_EX_WINDOWEDGE;    // 使窗口具有3D外观
103         dwStyle = WS_OVERLAPPEDWINDOW;                // 使用标准窗口    
105         int wid = GetSystemMetrics(SM_CXSCREEN);        // 获取当前屏幕宽
106         int hei = GetSystemMetrics(SM_CYSCREEN);        // 获取当前屏幕高
107         nX = (wid-Width)/2;nY = (hei-Height)/2;            // 计算窗口居中用
108     }
109     //-------------------------------------------------------------------
110     AdjustWindowRectEx(&windowRect,dwStyle,FALSE,dwExStyle);//调整窗口尺寸
111     
112     char className[] = "TApplicationGL";
113     
114     //窗口类型结构
115     WNDCLASSEX wc = { sizeof(WNDCLASSEX),
116         CS_HREDRAW | CS_VREDRAW | CS_OWNDC,//
117         MsgProc, 
118         0L
119         0L
120         GetModuleHandle(NULL),
121         NULL, 
122         NULL, 
123         NULL, 
124         NULL,
125         className,
126         NULL };   
127     
128     RegisterClassEx( &wc ); //注册该窗口类型
129 
130     m_OpenGL = new OpenGL();//
131     hWnd = CreateWindowEx(NULL,
132                           className,
133                           "OpenGL",
134                           dwStyle|WS_CLIPCHILDREN|WS_CLIPSIBLINGS,
135                           nX,
136                           nY,
137                           Width, 
138                           Height,
139                           NULL,
140                           NULL,
141                           hInst,
142                           NULL);   
143     ShowWindow(hWnd, SW_SHOW);  // 显示窗口
144     UpdateWindow(hWnd);               // 刷新窗口
145     GameLoop();                            // 进入消息循环
146     return 0
147 }

注:OpenGL类处理图形相关的操作,main.cpp管理主窗口,处理消息

结果如下:
posted on 2012-05-22 11:54 蕙麓 阅读(588) 评论(2)  编辑 收藏 引用 所属分类: 计算机图像学(OpenGL)

评论

# re: OpenGL程序框架 2012-06-10 13:08 eryar
可以使用OpenSceneGraph,
比直接用OpenGL要方便点...  回复  更多评论
  

# re: OpenGL程序框架 2012-06-17 17:14 蕙麓
嗯,我可以试试看。Thank you @eryar
  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理