----要 學 習OpenGL 編 程, 希 望 讀 者 具 備 基 本 的 圖 形 知 識。 本 文 使 用 基 于Visual C + + 消 息 驅 動 編 程, 對 于 沒 學 過 VC 的 讀 者 也 有 一 定 的 幫 助。 我 們 的 第 一 個 程 序 將 明 建 立 一 個 視 窗 程 序 顯 示OpenGL 圖 形 的 最 小 需 求。 為 成 這 一 任 務 我 們 將 分 如 下5 步 來 進 行:
----1 設 置 窗 口 像 素 的 格 式;2 建 立RC;3 使 RC 設 為 當 前;4 創 建 視 口 和 矩 陣 模 型;5 畫 一 個 立 方 體 和 一 個 茶 壺。
----現 在 你 可 以 打 開 你 的Visual C + +, 建 立 一 個 單 文 檔 的 項 目。 首 先 我 們 在 該 項 目 中 加 進 所 有 必 需 的OpenGL 文 件 和 庫, 在 菜 單 中 選 擇Build Settings, 然 后 點 擊LINK 按 鈕( 或 者 按 Ctrl +Tab 鍵 來 移 動 到 那 兒)。 在Object/Library 欄 中 鍵 入Opengl32.lib glu32.lib glaux.lib, 并 確 定。 打 開 文 件stdafx.h 插 入 如 下 行:
#include < gl\gl.h > #include < gl\glu.h > #include < gl\glaux.h >
----OpenGL 僅 能 在 具 有WS_CLIPCHILDREN 和 WS_CLIPSIBLINGS 類 型 的 窗 口 顯 示 圖 形, 我 們 需 要 編 輯OnPreCreate 函 數, 指 定 一 下 窗 口 類 型。
BOOL COPView::PreCreateWindow(CREATESTRUCT & cs) {cs.style |= (WS_CLIPCHILDREN | WS_CLIPSIBLINGS); return CView::PreCreateWindow(cs); }
----下 面 我 們 要 定 義 窗 口 的 像 素 格 式, 這 一 點 對 建 立RC 很 重 要。 首 先 我 們 需 要 建 立 一 個 受 保 護 的 成 員 函 數 BOOL SetWindowPixelFormat(HDC hDC)。 如 下 所 示:
BOOL COPView::SetWindowPixelFormat(HDC hDC) { PIXELFORMATDESCRIPTOR pixelDesc; pixelDesc.nSize = sizeof(PIXELFORMATDESCRIP ? TOR); pixelDesc.nVersion = 1; pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW | PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL | PFD_SUPPORT_GDI |PFD_STEREO_DONTCARE; pixelDesc.iPixelType = PFD_TYPE_RGBA; pixelDesc.cColorBits = 32; pixelDesc.cRedBits = 8; pixelDesc.cRedShift = 16; pixelDesc.cGreenBits = 8; pixelDesc.cGreenShift = 8; pixelDesc.cBlueBits = 8; pixelDesc.cBlueShift = 0; pixelDesc.cAlphaBits = 0; pixelDesc.cAlphaShift = 0; pixelDesc.cAclearcase/" target="_blank" >ccumBits= 64; pixelDesc.cAccumRedBits = 16; pixelDesc.cAccumGreenBits = 16; pixelDesc.cAccumBlueBits = 16; pixelDesc.cAccumAlphaBits= 0; pixelDesc.cDepthBits = 32; pixelDesc.cStencilBits= 8; pixelDesc.cAuxBuffers = 0; pixelDesc.iLayerType= PFD_MAIN_PLANE; pixelDesc.bReserved = 0; pixelDesc.dwLayerMask= 0; pixelDesc.dwVisibleMask= 0; pixelDesc.dwDamageMask= 0; m_GLPixelIndex = ChoosePixelFormat( hDC, &pixelDesc); if (m_GLPixelIndex==0) // Let's choose a default index. { m_GLPixelIndex = 1; if (DescribePixelFormat(hDC, m_GLPixelIndex, sizeof (PIXELFORMATDESCRIPTOR), &pixelDesc)==0) { return FALSE; } } if (SetPixelFormat( hDC, m_GLPixelIndex, &pixelDesc)==FALSE) { return FALSE; } return TRUE; }
----加 入 一 個 成 員 變 量 到 視 類 中:
int m_GLPixelIndex; // protected
----最 后, 在ClassWizard 中 加 入 函 數OnCreate 來 響 應 消 息WM_CREATE, 函 數 如 下:
int COPView::OnCreate (LPCREATESTRUCT lpCreateStruct) { if (CView::OnCreate(lpCreateStruct) == -1) return -1; HWND hWnd = GetSafeHwnd(); HDC hDC = ::GetDC(hWnd); if (SetWindowPixelFormat(hDC)==FALSE) return 0; if (CreateViewGLContext(hDC)==FALSE) return 0; return 0; }
----下 面 需 要 作 的 步 驟 就 是 建 立RC, 并 置 為 當 前RC。
----在 視 類 中 加 入 保 護 函 數 CreateViewGLContext(HDC hDC) 和 變 量HGLRC m_hGLContext:
BOOL COPView::CreateViewGLContext(HDC hDC) { m_hGLContext = wglCreateContext(hDC); if (m_hGLContext == NULL) { return FALSE; } if (wglMakeCurrent(hDC, m_hGLContext)==FALSE) { return FALSE; } return TRUE; } 加 入 函 數OnDestroy 來 響 應WM_DESTROY: void COPView::OnDestroy() { if(wglGetCurrentContext()!=NULL) { wglMakeCurrent(NULL, NULL) ; } if (m_hGLContext!=NULL) { wglDeleteContext(m_hGLContext); m_hGLContext = NULL; } CView::OnDestroy(); }
----最 后, 編 輯 一 下COPView 類 構 造 函 數:
COPView::COPView() { m_hGLContext = NULL; m_GLPixelIndex = 0; }
----現 在, 我 們 就 可 以 進 行OpenGL 畫 圖 了, 雖 然 它 看 起 來 仍 然 像 一 個 典 型 的MFC 程 序。
----現 在 我 們 進 行 下 一 步 建 立 視 點 和 矩 陣 模 型, 用ClassWizard 在 視 類 中 加 入 函 數OnSize 響 應WM_SIZE。 如 下 所 示:
void COPView::OnSize(UINT nType, int cx, int cy) { CView::OnSize(nType, cx, cy); GLsizei width, height; GLdouble aspect; width = cx; height = cy; if (cy==0) aspect = (GLdouble)width; else aspect = (GLdouble)width/(GLdouble)height; glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45, aspect, 1, 10.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } 加 入 函 數:void COPView::OnPaint() { CPaintDC dc(this); // device context for painting COPDoc * pDoc = GetDocument(); pDoc ->RenderScene(); }
----在 文 檔 類 中 加 入 公 共 函 數RenderScene():
void COPDoc::RenderScene(void) { glClear(GL_COLOR_BUFFER_BIT); glFlush(); }
----現 在 程 序 運 行 后 僅 是 黑 黑 的 屏 幕, 我 們 需 要 加 進 些 東 西。
----在 文 檔 類 中 加 一 個 枚 舉 變 量 GLDisplayListNames:
enum GLDisplayListNames { ArmPart1,ArmPart2 };
----為 將 來 建 立 顯 示 列 表 用。 編 輯 函 數 OnNewDocument(), 編 碼 如 下:
BOOL COPDoc::OnNewDocument() if (!CDocument::OnNewDocument()) return FALSE; glNewList(ArmPart1, GL_COMPILE); GLfloat RedSurface[] = { 1.0f, 0.0f, 0.0f, 1.0f}; GLfloat GreenSurface[] = { 0.0f, 1.0f, 0.0f, 1.0f}; GLfloat BlueSurface[] = { 0.0f, 0.0f, 1.0f, 1.0f}; GLfloat LightAmbient[] = { 0.1f, 0.1f, 0.1f, 0.1f }; GLfloat LightDiffuse[] = { 0.7f, 0.7f, 0.7f, 0.7f }; GLfloat LightSpecular[] = { 0.0f, 0.0f, 0.0f, 0.1f }; GLfloat LightPosition[] = { 5.0f, 5.0f, 5.0f, 0.0f }; glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient); glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, LightSpecular); glLightfv(GL_LIGHT0, GL_POSITION, LightPosition); glEnable(GL_LIGHT0); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, RedSurface); glBegin(GL_POLYGON); glNormal3d( 1.0, 0.0, 0.0); glVertex3d( 1.0, 1.0, 1.0); glVertex3d( 1.0, -1.0, 1.0); glVertex3d( 1.0, -1.0, -1.0); glVertex3d ( 1.0, 1.0, -1.0); // 畫 第 一 個 面 glEnd(); glBegin(GL_POLYGON); glNormal3d( -1.0, 0.0, 0.0); // 此 處 同 上 畫 第 二 個 面。 立 方 體 的 中 心 為 坐 標 原 點。 glEnd(); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, GreenSurface); // 此 處 同 上 畫 第 三、 四 個 面, 注 意 平 面 法 向 和 坐 標。 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, BlueSurface); // 此 處 同 上 畫 第 五、 六 個 面。 glEndList(); glNewList(ArmPart2, GL_COMPILE); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, GreenSurface); auxSolidTeapot(1.0);// 用 輔 助 庫 函 數 畫 茶 壺。 glEndList(); return TRUE; }
----下 面 就 看 怎 樣 顯 示 它 了。 編 輯RenderScene 函 數:
void COPDoc::RenderScene(void) { double m_angle1=60.0; double m_angle2=30.0; glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); glTranslated(3.0, 0.0, -8.0); glRotated( m_angle1, 0, 0, 1); glRotated( m_angle2, 0, 1, 0); glCallList(ArmPart1); glPopMatrix(); glPushMatrix(); glTranslated(0.0, 0.0, -8.0); glCallList(ArmPart2); glPopMatrix(); glFlush(); }