一、 建立工程
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/
所有博客索引:http://www.cnblogs.com/zjutlitao/p/4125085.html
第一节OpenGL环境搭建:http://www.cnblogs.com/zjutlitao/p/3910525.html