[OpenGL] 2、企业版VC6.0自带的Win32-OpenGL工程浅析

 

 

一、 建立工程

O(∩_∩)O~上一节介绍了一种非常容易的OpenGL的搭建方法,这一节将就上一节介绍的VC6.0企业版自带的OpenGL Win32 Application建立一个模板工程,并分析这个模板工程,便于今后我们在此基础上进行修改~

[OpenGL] 2、企业版VC6.0自带的Win32-OpenGL工程浅析

PS: 如果有报错,请仔细读上一节的介绍哦~好像Win8不行的!

[OpenGL] 2、企业版VC6.0自带的Win32-OpenGL工程浅析


 

二、 框架逻辑分析

O(∩_∩)O~工程建好之后主要的就是上图中用红框框起来的文件

 

  2-1、 OpenGL1.cpp分析

  其中OpenGL1.cpp就是Win32应用程序的主逻辑框架啦~由下面OpenGL1.h中主要代码可以看出该文件和Win32原框架很像,主要由初始化窗口、消息回调、退出等组成:

①InitInstance负责产生并显示窗口;

②ExitInstance()什么都没有;

③OnCommand()消息回调,这里只有一个IDM_EXIT消息发送WM_CLOSE;

④WindowProc()窗口消息回调函数及相关窗口消息都在此处理;

   剩下几个注释很明白~

 1 /////////////////////////////////////////////////////////////////////////////
 2 // CApp
 3 // Application class
 4 
 5 class CApp
 6 {
 7 protected:
 8     HINSTANCE m_hInst;
 9     CMainWnd* m_pMainWnd;
10 
11 public:
12     CApp();
13     ~CApp();
14 
15     int Run();                // Starts the message pump
16     void OnIdle();            // Called when there are no messages in the message queue. 
17                               // Sets current OpenGL RC and notifies the main window class that another frame 
18                               // is to be drawn. 
19     BOOL InitInstance(HINSTANCE hInstance, int nCmdShow, LPSTR lpCmdLine);    
20                               // Called when the application starts. Creates and shows the main window.
21     void ExitInstance();      // Called when the application exits.
22     BOOL OnCommand(int nCmdID, int nEvent);// Handles WM_COMMAND messages
23     LRESULT WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
24                               // Handles messages sent to the main window
25     void OnPaint(HDC hDC);    // Handles WM_PAINT messages. Redraws the OpenGL scene.
26     void OnDestroy();         // Called when the main window is destroyed. Shuts down OpenGL
27     void OnCreate();          // Called when the main window has been created. Initializes OpenGL.
28     void OnSize(int cx, int cy);// Called when the main window is resized. Resizes the OpenGL
29 };

 

  2-2、 OpenGL1.cpp分析

   其中OpenGL1.cpp就是来处理OpenGL的相关东西,从下面的OpenGL1.h可以看出:

其大致由初始化函数、绘制函数、计时器函数、绘制函数和结束函数组成,这里特别说明计时器函数是用来定时更新一些变量来使3D图呈现运动的效果的,这个在MFC\C#\Win32中经常见到,和安卓游戏开发中logic()函数效果差不多~今后我们无特殊情况基本上都是在这几个函数中修改来呈现各种效果的~

 1 class CMainWnd  
 2 {
 3 public:
 4     CMainWnd();
 5     virtual ~CMainWnd();
 6     
 7     HWND m_hWnd;
 8     HGLRC m_hRC;          // Handle to RC
 9     GLfloat m_fAngle; // Rotation angle of the cube
10 
11     void DrawScene();
12     void KillScene();
13     void InitScene();    
14     void Tick(BOOL &bRedrawScene);
15 };

 

三、本例程细节

对于本模板带的绘制例子,这里大致分析下:

>_<" 首先看void CMainWnd::InitScene()函数主要是设置灯光和材质(现在先别管具体函数是啥,大致知道是这个就行)

 1 //
 2 // InitScene()
 3 // Called when the OpenGL RC has been created. Sets the initial state for
 4 // the OpenGL scene.
 5 //
 6 void CMainWnd::InitScene()
 7 {
 8     glClearColor(0.000f, 0.000f, 0.000f, 1.0f); //Background color
 9 
10     // TODO: Replace the following sample code with your initialization code.
11 
12     // Activate lighting and a light source
13   glEnable(GL_LIGHT0);
14     glEnable(GL_LIGHTING);
15   glEnable(GL_DEPTH_TEST);
16 
17     // Define material parameters
18     static GLfloat glfMatAmbient[] = {0.000f, 0.450f, 1.000f, 1.0f};
19     static GLfloat glfMatDiffuse[] = {0.000f, 0.000f, 0.580f, 1.0f};
20     static GLfloat glfMatSpecular[]= {1.000f, 1.000f, 1.000f, 1.0f};
21     static GLfloat glfMatEmission[]= {0.000f, 0.000f, 0.000f, 1.0f};
22     static GLfloat fShininess = 128.000f;
23 
24     // Set material parameters
25     glMaterialfv(GL_FRONT, GL_AMBIENT,  glfMatAmbient);
26     glMaterialfv(GL_FRONT, GL_DIFFUSE,  glfMatDiffuse);
27     glMaterialfv(GL_FRONT, GL_SPECULAR, glfMatSpecular);
28     glMaterialfv(GL_FRONT, GL_EMISSION, glfMatEmission);
29     glMaterialf(GL_FRONT, GL_SHININESS, fShininess);
30 }

>_<" 其次要看CMainWnd::DrawScene()函数,这个函数在每个时间点被调用来绘制OpenGL3D图。这里主要清空缓存、加载光源、加载镜头、绘制正方体(这里正方体封装到GLCube(-r, -r, -r, r, r, r);函数中。

 1 //
 2 // DrawScene()
 3 // Called each time the OpenGL scene has to be drawn.
 4 //
 5 void CMainWnd::DrawScene()
 6 {
 7     // TODO: Replace the following sample code with your code to draw the scene.
 8 
 9   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear buffers
10   glLoadIdentity(); // Load identity matrix
11 
12     // Add a light source
13     GLfloat glfLight[] = {-4.0f, 4.0f, 4.0f, 0.0f};
14     glLightfv(GL_LIGHT0, GL_POSITION, glfLight);
15  
16     // Position and rotate the camera
17   glTranslatef(0.0f, 0.0f, -5.0f);    
18     glRotatef(30.0f, 1.0f, 0.0f, 0.0f);
19     glRotatef(m_fAngle, 0.0f, 1.0f, 0.0f);
20 
21     // Draw a cube
22     static GLfloat r = .7f;
23     GLCube(-r, -r, -r, r, r, r);
24 
25   glFlush();
26 }

>_<" 下面是GLCuBe函数,用来绘制正方体的:由参数知道其输入正方体的左上角和右下角,然后根据这两个点按照顺序绘制正方体在三维空间中的6个面,这样就组成了一个空间中的正方形了。

 1 //
 2 // GLCube()
 3 // Renders a cube.
 4 //
 5 void GLCube(GLfloat x1, GLfloat y1, GLfloat z1, GLfloat x2, GLfloat y2, GLfloat z2)
 6 {
 7     glBegin(GL_POLYGON);
 8     glNormal3f(0.0f, 0.0f, 1.0f);
 9     glVertex3f(x2, y2, z2);
10     glVertex3f(x1, y2, z2);
11     glVertex3f(x1, y1, z2);
12     glVertex3f(x2, y1, z2);
13     glEnd();
14 
15     glBegin(GL_POLYGON);
16     glNormal3f(0.0f, 0.0f, -1.0f);
17     glVertex3f(x2, y2, z1);
18     glVertex3f(x2, y1, z1);
19     glVertex3f(x1, y1, z1);
20     glVertex3f(x1, y2, z1);
21     glEnd();
22 
23     glBegin(GL_POLYGON);
24     glNormal3f(-1.0f, 0.0f, 0.0f);
25     glVertex3f(x1, y2, z2);
26     glVertex3f(x1, y2, z1);
27     glVertex3f(x1, y1, z1);
28     glVertex3f(x1, y1, z2);
29     glEnd();
30 
31     glBegin(GL_POLYGON);
32     glNormal3f(1.0f, 0.0f, 0.0f);
33     glVertex3f(x2, y2, z2);
34     glVertex3f(x2, y1, z2);
35     glVertex3f(x2, y1, z1);
36     glVertex3f(x2, y2, z1);
37     glEnd();
38 
39     glBegin(GL_POLYGON);
40     glNormal3f(0.0f, 1.0f, 0.0f);
41     glVertex3f(x1, y2, z1);
42     glVertex3f(x1, y2, z2);
43     glVertex3f(x2, y2, z2);
44     glVertex3f(x2, y2, z1);
45     glEnd();
46 
47     glBegin(GL_POLYGON);
48     glNormal3f(0.0f, -1.0f, 0.0f);
49     glVertex3f(x1, y1, z1);
50     glVertex3f(x2, y1, z1);
51     glVertex3f(x2, y1, z2);
52     glVertex3f(x1, y1, z2);
53     glEnd();
54 }

四、效果及相关链接

[OpenGL] 2、企业版VC6.0自带的Win32-OpenGL工程浅析

 

博主主页:http://www.cnblogs.com/zjutlitao/

所有博客索引:http://www.cnblogs.com/zjutlitao/p/4125085.html

第一节OpenGL环境搭建:http://www.cnblogs.com/zjutlitao/p/3910525.html

 

[OpenGL] 2、企业版VC6.0自带的Win32-OpenGL工程浅析

上一篇:[转]C# 中的委托和事件


下一篇:天气预报服务网站