[OpenGL] 2、企业版VC6.0自带的Win32-OpenGL工程浅析
2014-12-07 23:45
411 查看
[b]一、 建立工程[/b]
O(∩_∩)O~上一节介绍了一种非常容易的OpenGL的搭建方法,这一节将就上一节介绍的VC6.0企业版自带的OpenGL Win32 Application建立一个模板工程,并分析这个模板工程,便于今后我们在此基础上进行修改~
PS: 如果有报错,请仔细读上一节的介绍哦~好像Win8不行的!
二、 框架逻辑分析
O(∩_∩)O~工程建好之后主要的就是上图中用红框框起来的文件
2-1、 OpenGL1.cpp分析
其中OpenGL1.cpp就是Win32应用程序的主逻辑框架啦~由下面OpenGL1.h中主要代码可以看出该文件和Win32原框架很像,主要由初始化窗口、消息回调、退出等组成:
①InitInstance负责产生并显示窗口;
②ExitInstance()什么都没有;
③OnCommand()消息回调,这里只有一个IDM_EXIT消息发送WM_CLOSE;
④WindowProc()窗口消息回调函数及相关窗口消息都在此处理;
剩下几个注释很明白~
2-2、 OpenGL1.cpp分析
其中OpenGL1.cpp就是来处理OpenGL的相关东西,从下面的OpenGL1.h可以看出:
其大致由初始化函数、绘制函数、计时器函数、绘制函数和结束函数组成,这里特别说明计时器函数是用来定时更新一些变量来使3D图呈现运动的效果的,这个在MFC\C#\Win32中经常见到,和安卓游戏开发中logic()函数效果差不多~今后我们无特殊情况基本上都是在这几个函数中修改来呈现各种效果的~
三、本例程细节
对于本模板带的绘制例子,这里大致分析下:
>_<" 首先看void CMainWnd::InitScene()函数主要是设置灯光和材质(现在先别管具体函数是啥,大致知道是这个就行)
>_<" 其次要看CMainWnd::DrawScene()函数,这个函数在每个时间点被调用来绘制OpenGL3D图。这里主要清空缓存、加载光源、加载镜头、绘制正方体(这里正方体封装到GLCube(-r, -r, -r, r, r, r);函数中。
>_<" 下面是GLCuBe函数,用来绘制正方体的:由参数知道其输入正方体的左上角和右下角,然后根据这两个点按照顺序绘制正方体在三维空间中的6个面,这样就组成了一个空间中的正方形了。
四、效果及相关链接
博主主页:http://www.cnblogs.com/zjutlitao/
所有博客索引:/article/5083891.html
第一节OpenGL环境搭建:/article/5083853.html
O(∩_∩)O~上一节介绍了一种非常容易的OpenGL的搭建方法,这一节将就上一节介绍的VC6.0企业版自带的OpenGL Win32 Application建立一个模板工程,并分析这个模板工程,便于今后我们在此基础上进行修改~
PS: 如果有报错,请仔细读上一节的介绍哦~好像Win8不行的!
二、 框架逻辑分析
O(∩_∩)O~工程建好之后主要的就是上图中用红框框起来的文件
2-1、 OpenGL1.cpp分析
其中OpenGL1.cpp就是Win32应用程序的主逻辑框架啦~由下面OpenGL1.h中主要代码可以看出该文件和Win32原框架很像,主要由初始化窗口、消息回调、退出等组成:
①InitInstance负责产生并显示窗口;
②ExitInstance()什么都没有;
③OnCommand()消息回调,这里只有一个IDM_EXIT消息发送WM_CLOSE;
④WindowProc()窗口消息回调函数及相关窗口消息都在此处理;
剩下几个注释很明白~
///////////////////////////////////////////////////////////////////////////// // CApp // Application class class CApp { protected: HINSTANCE m_hInst; CMainWnd* m_pMainWnd; public: CApp(); ~CApp(); int Run(); // Starts the message pump void OnIdle(); // Called when there are no messages in the message queue. // Sets current OpenGL RC and notifies the main window class that another frame // is to be drawn. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow, LPSTR lpCmdLine); // Called when the application starts. Creates and shows the main window. void ExitInstance(); // Called when the application exits. BOOL OnCommand(int nCmdID, int nEvent);// Handles WM_COMMAND messages LRESULT WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); // Handles messages sent to the main window void OnPaint(HDC hDC); // Handles WM_PAINT messages. Redraws the OpenGL scene. void OnDestroy(); // Called when the main window is destroyed. Shuts down OpenGL void OnCreate(); // Called when the main window has been created. Initializes OpenGL. void OnSize(int cx, int cy);// Called when the main window is resized. Resizes the OpenGL };
2-2、 OpenGL1.cpp分析
其中OpenGL1.cpp就是来处理OpenGL的相关东西,从下面的OpenGL1.h可以看出:
其大致由初始化函数、绘制函数、计时器函数、绘制函数和结束函数组成,这里特别说明计时器函数是用来定时更新一些变量来使3D图呈现运动的效果的,这个在MFC\C#\Win32中经常见到,和安卓游戏开发中logic()函数效果差不多~今后我们无特殊情况基本上都是在这几个函数中修改来呈现各种效果的~
class CMainWnd { public: CMainWnd(); virtual ~CMainWnd(); HWND m_hWnd; HGLRC m_hRC; // Handle to RC GLfloat m_fAngle; // Rotation angle of the cube void DrawScene(); void KillScene(); void InitScene(); void Tick(BOOL &bRedrawScene); };
三、本例程细节
对于本模板带的绘制例子,这里大致分析下:
>_<" 首先看void CMainWnd::InitScene()函数主要是设置灯光和材质(现在先别管具体函数是啥,大致知道是这个就行)
// // InitScene() // Called when the OpenGL RC has been created. Sets the initial state for // the OpenGL scene. // void CMainWnd::InitScene() { glClearColor(0.000f, 0.000f, 0.000f, 1.0f); //Background color // TODO: Replace the following sample code with your initialization code. // Activate lighting and a light source glEnable(GL_LIGHT0); glEnable(GL_LIGHTING); glEnable(GL_DEPTH_TEST); // Define material parameters static GLfloat glfMatAmbient[] = {0.000f, 0.450f, 1.000f, 1.0f}; static GLfloat glfMatDiffuse[] = {0.000f, 0.000f, 0.580f, 1.0f}; static GLfloat glfMatSpecular[]= {1.000f, 1.000f, 1.000f, 1.0f}; static GLfloat glfMatEmission[]= {0.000f, 0.000f, 0.000f, 1.0f}; static GLfloat fShininess = 128.000f; // Set material parameters glMaterialfv(GL_FRONT, GL_AMBIENT, glfMatAmbient); glMaterialfv(GL_FRONT, GL_DIFFUSE, glfMatDiffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, glfMatSpecular); glMaterialfv(GL_FRONT, GL_EMISSION, glfMatEmission); glMaterialf(GL_FRONT, GL_SHININESS, fShininess); }
>_<" 其次要看CMainWnd::DrawScene()函数,这个函数在每个时间点被调用来绘制OpenGL3D图。这里主要清空缓存、加载光源、加载镜头、绘制正方体(这里正方体封装到GLCube(-r, -r, -r, r, r, r);函数中。
// // DrawScene() // Called each time the OpenGL scene has to be drawn. // void CMainWnd::DrawScene() { // TODO: Replace the following sample code with your code to draw the scene. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear buffers glLoadIdentity(); // Load identity matrix // Add a light source GLfloat glfLight[] = {-4.0f, 4.0f, 4.0f, 0.0f}; glLightfv(GL_LIGHT0, GL_POSITION, glfLight); // Position and rotate the camera glTranslatef(0.0f, 0.0f, -5.0f); glRotatef(30.0f, 1.0f, 0.0f, 0.0f); glRotatef(m_fAngle, 0.0f, 1.0f, 0.0f); // Draw a cube static GLfloat r = .7f; GLCube(-r, -r, -r, r, r, r); glFlush(); }
>_<" 下面是GLCuBe函数,用来绘制正方体的:由参数知道其输入正方体的左上角和右下角,然后根据这两个点按照顺序绘制正方体在三维空间中的6个面,这样就组成了一个空间中的正方形了。
// // GLCube() // Renders a cube. // void GLCube(GLfloat x1, GLfloat y1, GLfloat z1, GLfloat x2, GLfloat y2, GLfloat z2) { glBegin(GL_POLYGON); glNormal3f(0.0f, 0.0f, 1.0f); glVertex3f(x2, y2, z2); glVertex3f(x1, y2, z2); glVertex3f(x1, y1, z2); glVertex3f(x2, y1, z2); glEnd(); glBegin(GL_POLYGON); glNormal3f(0.0f, 0.0f, -1.0f); glVertex3f(x2, y2, z1); glVertex3f(x2, y1, z1); glVertex3f(x1, y1, z1); glVertex3f(x1, y2, z1); glEnd(); glBegin(GL_POLYGON); glNormal3f(-1.0f, 0.0f, 0.0f); glVertex3f(x1, y2, z2); glVertex3f(x1, y2, z1); glVertex3f(x1, y1, z1); glVertex3f(x1, y1, z2); glEnd(); glBegin(GL_POLYGON); glNormal3f(1.0f, 0.0f, 0.0f); glVertex3f(x2, y2, z2); glVertex3f(x2, y1, z2); glVertex3f(x2, y1, z1); glVertex3f(x2, y2, z1); glEnd(); glBegin(GL_POLYGON); glNormal3f(0.0f, 1.0f, 0.0f); glVertex3f(x1, y2, z1); glVertex3f(x1, y2, z2); glVertex3f(x2, y2, z2); glVertex3f(x2, y2, z1); glEnd(); glBegin(GL_POLYGON); glNormal3f(0.0f, -1.0f, 0.0f); glVertex3f(x1, y1, z1); glVertex3f(x2, y1, z1); glVertex3f(x2, y1, z2); glVertex3f(x1, y1, z2); glEnd(); }
四、效果及相关链接
博主主页:http://www.cnblogs.com/zjutlitao/
所有博客索引:/article/5083891.html
第一节OpenGL环境搭建:/article/5083853.html
相关文章推荐
- opengl 使用 vc6.0中win32控制台工程去掉控制台窗口
- vs2010建立win32 application 编译错误(用vc6.0建立此工程正常运行)解决方法
- 用VC6.0编写Win32程序加载位图(含工程源代码)
- 新建ios 及win32 opengl工程
- 一个服务程序框架,并在vc6.0win32控制台工程中嵌入manifest使用uac
- vs2010建立win32 application 编译错误(用vc6.0建立此工程正常运行)解决方法
- VC6.0如何创建一个基本的Win32 DLL工程
- VC6.0 环境下Win32 Application工程添加资源
- VC6.0使用Greta的工程在VS2005中编译出现的问题:C2079,LNK1123,CVT1100等等
- cocos2dx win32工程如何自定义资源路径,即Resources的路径
- VC6.0无法打开文件和无法向工程添加文件的解决办法
- VC6与OFFICE2007的冲突,导至VC6.0中的打开文件和添加文件到工程会导致异常或点了没反应
- Opengl and Win32
- 如何更改工程类型console/win32(转)
- VC6.0修改工程名字方法
- 用VC6.0与OpenGL实现游戏场景设计
- VC6.0 OpenGL环境配置及编程基础
- 给新建的Cocos2d-x 3.0rc0 的Win32工程添加CocoStudio库
- 用VC6.0编译驱动程序的工程设置方法
- VS2015--win32工程配置的一些想法之在 Visual Studio 2015 中进行调试的同时分析性能