MFC下OpenGL入門_第1頁
MFC下OpenGL入門_第2頁
MFC下OpenGL入門_第3頁
MFC下OpenGL入門_第4頁
MFC下OpenGL入門_第5頁
已閱讀5頁,還剩9頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、MFC下OpenGL入門源文件1, 建一工程文件,我這里命名為first,現(xiàn)在first工程里面我們沒有添加任何東西,所有的東西都是MFC自動幫我們創(chuàng)建的。2, 添加鏈接庫。這一步很關(guān)鍵。打開菜單欄下的項目->屬性->配置屬性->鏈接器->輸入->附加依賴項里加入OpenGL32.lib GLu32.lib GLaux.lib,如圖 3, 加頭文件,在stdafx里面添加opengl的頭文件。如下代碼所示:/-Tramp-添加OpenGL庫頭文件->#include "stdio.h"#i

2、nclude "math.h"#include "glgl.h"#include "glglu.h"#include "glglaux.h"/-<代碼   4, CCY457OpenGLView類的屬性欄,為下述消息加入消息處理函數(shù):WM_CREATE (for OnCreate), WM_DESTROY (for OnDestroy), WM_SIZE (for OnSize), WM_ERASEBACKGROUND (for

3、OnEraseBkground).如下圖:    5, 設(shè)置窗口顯示風(fēng)格。窗口創(chuàng)建之前我們必須設(shè)置窗口風(fēng)格包含WS_CLIPCHILDREN和 WS_CLIPSIBLINGS,從而避免OpenGL繪制到其他窗口中去。這些應(yīng)該放在PreCreateWindow()中。 BOOL CfirstView:PreCreateWindow(CREATESTRUCT& cs) / TODO: 在此處通過修改 / CREATESTRUCT cs

4、0;來修改窗口類或樣式 cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;/Tramp return CView:PreCreateWindow(cs);代碼  6, 在CfirstView.h中加入如下語句:      /*/    /* 設(shè)置的變量是Rendering Context(著色描述表)。每一個OpenGL都被連接到一個著 

5、;   色描述表上。著色描述表將所有的OpenGL調(diào)用命令連接到Device Context(設(shè)備描述表)上。    我將OpenGL的著色描述表定義為hRC 。要讓您的程序能夠繪制窗口的話,還需要創(chuàng)建一個    設(shè)備描述表,也就是第二行的內(nèi)容。Windows的設(shè)備描述表被定義為hDC 。DC將窗口連接到    GDI(Graphics Device Interface圖形設(shè)備接口)。而RC將OpenGL連接到DC   

6、60;                                                  &#

7、160;              */    /*/    HGLRC m_hRC;    /Rendering Context著色描述表    CDC* m_pDC;        /Device

8、 Context設(shè)備描述表    BOOL InitializeOpenGL();    /Initialize OpenGL    BOOL SetupPixelFormat();    /Set up the Pixel Format    void RenderScene();       &

9、#160;    /Render the Scene 7, 在OnCreate中我們將通過建立像素格式和繪制上下文來初始化OpenGL. 在InitializeOpenGL()中會創(chuàng)建一個設(shè)備上下文(DC),為這個DC選擇一個像素格式,創(chuàng)建和這個DC相關(guān)的繪制上下文(RC),然后選擇這個RC.這個函數(shù)會調(diào)用SetupPixelFormat()來建立像素格式。int Clesson1View:OnCreate(LPCREATESTRUCT lpCreateStruct) if (CVie

10、w:OnCreate(lpCreateStruct) = -1)      return -1;  / TODO: 在此添加您專用的創(chuàng)建代碼 InitializeOpenGL();/初始化openGL繪圖  return 0;     /初始化opengl繪制BOOL CfirstView:InitializeOpenGL()    /Get a DC fo

11、r the Client Area    m_pDC = new CClientDC(this);    /Failure to Get DC    if(m_pDC = NULL)            /:MessageBox("Error Obtaining DC");  

12、      return FALSE;        /Failure to set the pixel format    if(!SetupPixelFormat()            return FALSE;     /Create Renderi

13、ng Context m_hRC = :wglCreateContext (m_pDC->GetSafeHdc (); /Failure to Create Rendering Context if(m_hRC = 0)       / MessageBox("Error Creating RC");      return FALSE; &

14、#160;/Make the RC Current if(:wglMakeCurrent (m_pDC->GetSafeHdc (), m_hRC)=FALSE)       / MessageBox("Error making RC Current");      return FALSE;  /Specify Black as the clear color

15、 :glClearColor(0.0f,0.0f,0.0f,0.0f); /Specify the back of the buffer as clear depth :glClearDepth(1.0f); /Enable Depth Testing :glEnable(GL_DEPTH_TEST); return TRUE; /設(shè)置像素格式BOOL CfirstView:SetupPixelFormat() static PIXELFORMATDESCRIPTOR pfd

16、 =       sizeof(PIXELFORMATDESCRIPTOR), / size of this pfd      1,                           &#

17、160;  / version number      PFD_DRAW_TO_WINDOW |            / support window      PFD_SUPPORT_OPENGL |         

18、0;  / support OpenGL      PFD_DOUBLEBUFFER,                / double buffered      PFD_TYPE_RGBA,         

19、         / RGBA type      24,                             / 24-bit color depth &

20、#160;    0, 0, 0, 0, 0, 0,               / color bits ignored      0,                   &#

21、160;          / no alpha buffer      0,                              / shif

22、t bit ignored      0,                              / no accumulation buffer      0, 0, 0, 0, &#

23、160;                   / accum bits ignored      16,                     &#

24、160;       / 16-bit z-buffer      0,                              / no stencil buffer 

25、;     0,                              / no auxiliary buffer      PFD_MAIN_PLANE,   &#

26、160;             / main layer      0,                             

27、; / reserved      0, 0, 0                         / layer masks ignored  int m_nPixelFormat = :ChoosePixelFormat(m_pDC->GetSafe

28、Hdc(), &pfd); if ( m_nPixelFormat = 0 )       return FALSE;  if ( :SetPixelFormat(m_pDC->GetSafeHdc(), m_nPixelFormat, &pfd) = FALSE)       return FALSE;  

29、;return TRUE; 8, 在OnSize()中一般用來設(shè)置視口和視錐,因為這些是和窗口大小相關(guān)的?;静僮靼ㄔO(shè)置視口,選擇投影矩陣,設(shè)置模型視圖矩陣。void CfirstView:OnSize(UINT nType, int cx, int cy) CView:OnSize(nType, cx, cy);  / TODO: 在此處添加消息處理程序代碼 GLdouble aspect_ratio; / wi

30、dth/height ratio  if ( 0 >= cx | 0 >= cy )       return;  / select the full client area :glViewport(0, 0, cx, cy); / compute the aspect ratio / this will keep all dimension scales equal

31、0;aspect_ratio = (GLdouble)cx/(GLdouble)cy; / select the projection matrix and clear it :glMatrixMode(GL_PROJECTION); :glLoadIdentity(); / select the viewing volume :gluPerspective(45.0f, aspect_ratio, .01f, 200.0f);/畫三維 /:gluOrtho2D(-10.0f, 10.0f, -10.0f, 10.

32、0f);    /二維   / switch back to the modelview matrix and clear it :glMatrixMode(GL_MODELVIEW); :glLoadIdentity(); 9, 在繪制場景時,一般包括如下步驟:1)清空緩存。2)繪制場景。3)Flush掉渲染流水線。4)若設(shè)置了雙緩沖,則交換前后臺緩沖區(qū)。void CfirstView:OnDraw(CDC* /*pDC*/) CfirstDoc* pD

33、oc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc)      return;  / TODO: 在此處為本機數(shù)據(jù)添加繪制代碼 / Clear out the color & depth buffers :glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); RenderScen

34、e();/繪圖都放在這 / Tell OpenGL to flush its pipeline :glFinish(); / Now Swap the buffers :SwapBuffers( m_pDC->GetSafeHdc() ); 10,              為了使改變窗口大小時嚴(yán)重的閃爍,在OnEraseBkgnd里做一些操作,避免windows自己的窗口刷新閃爍。BOOL

35、 CfirstView:OnEraseBkgnd(CDC* pDC)/ TODO: 在此添加消息處理程序代碼和/或調(diào)用默認(rèn)值 return TRUE;11,              為了避免內(nèi)存泄露,我們要將在SetupPixelFormat()中使用了new運算符來為CClientDC對象分配的內(nèi)存在程序關(guān)閉時delete掉。 void CfirstView:OnDestroy()&

36、#160;   CView:OnDestroy();     / TODO: 在此處添加消息處理程序代碼    /Make the RC non-current    if(:wglMakeCurrent (0,0) = FALSE)            MessageBox(_T("C

37、ould not make RC non-current");         /Delete the rendering context    if(:wglDeleteContext (m_hRC)=FALSE)            MessageBox(_T("Could not delete RC"); &#

38、160;      /Delete the DC    if(m_pDC)            delete m_pDC;        /Set it to NULL    m_pDC = NULL; 12,   &

39、#160;  下面寫主繪圖函數(shù),RenderScene(),在窗口畫了一個正方體、一個四面體。 void CfirstView:RenderScene()    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); / Clear Screen And Depth Buffer    glLoadIdentity();      

40、0;                            / Reset The Current Modelview Matrix    glTranslatef(-1.5f,0.0f,-6.0f);      

41、60;              / Move Left 1.5 Units And Into The Screen 6.0    glRotatef(30,0.0f,1.0f,0.0f);                   &

42、#160;   / Rotate The Triangle On The Y axis ( NEW )    glBegin(GL_TRIANGLES);                              / Start

43、Drawing A Triangle    glColor3f(1.0f,0.0f,0.0f);                      / Red    glVertex3f( 0.0f, 1.0f, 0.0f);       

44、;           / Top Of Triangle (Front)    glColor3f(0.0f,1.0f,0.0f);                      / Green   &

45、#160;glVertex3f(-1.0f,-1.0f, 1.0f);                  / Left Of Triangle (Front)    glColor3f(0.0f,0.0f,1.0f);             

46、60;        / Blue    glVertex3f( 1.0f,-1.0f, 1.0f);                  / Right Of Triangle (Front)    glColor3f(1.0f,0.0f,0.0f);

47、0;                     / Red    glVertex3f( 0.0f, 1.0f, 0.0f);                  / Top

48、 Of Triangle (Right)    glColor3f(0.0f,0.0f,1.0f);                      / Blue    glVertex3f( 1.0f,-1.0f, 1.0f);      &#

49、160;           / Left Of Triangle (Right)    glColor3f(0.0f,1.0f,0.0f);                      / Green  

50、60; glVertex3f( 1.0f,-1.0f, -1.0f);                 / Right Of Triangle (Right)    glColor3f(1.0f,0.0f,0.0f);             

51、60;        / Red    glVertex3f( 0.0f, 1.0f, 0.0f);                  / Top Of Triangle (Back)    glColor3f(0.0f,1.0f,0.0f); &#

52、160;                    / Green    glVertex3f( 1.0f,-1.0f, -1.0f);                 / Left Of Tr

53、iangle (Back)    glColor3f(0.0f,0.0f,1.0f);                      / Blue    glVertex3f(-1.0f,-1.0f, -1.0f);       &#

54、160;         / Right Of Triangle (Back)    glColor3f(1.0f,0.0f,0.0f);                      / Red    glVer

55、tex3f( 0.0f, 1.0f, 0.0f);                  / Top Of Triangle (Left)    glColor3f(0.0f,0.0f,1.0f);               

56、60;      / Blue    glVertex3f(-1.0f,-1.0f,-1.0f);                  / Left Of Triangle (Left)    glColor3f(0.0f,1.0f,0.0f);   

57、                   / Green    glVertex3f(-1.0f,-1.0f, 1.0f);                  / Right Of Triang

58、le (Left)    glEnd();                                           &

59、#160;/ Done Drawing The Pyramid     glLoadIdentity();                                   / Reset The

60、Current Modelview Matrix    glTranslatef(1.5f,0.0f,-7.0f);                      / Move Right 1.5 Units And Into The Screen 7.0    glRotatef(25,1.0f,1.

61、0f,1.0f);                   / Rotate The Quad On The X axis ( NEW )    glBegin(GL_QUADS);                

62、                  / Draw A Quad    glColor3f(0.0f,1.0f,0.0f);                     

63、60;/ Set The Color To Green    glVertex3f( 1.0f, 1.0f,-1.0f);                  / Top Right Of The Quad (Top)    glVertex3f(-1.0f, 1.0f,-1.0f);    

64、0;             / Top Left Of The Quad (Top)    glVertex3f(-1.0f, 1.0f, 1.0f);                  / Bottom Left Of The Quad (Top

65、)    glVertex3f( 1.0f, 1.0f, 1.0f);                  / Bottom Right Of The Quad (Top)    glColor3f(1.0f,0.5f,0.0f);         &

66、#160;            / Set The Color To Orange    glVertex3f( 1.0f,-1.0f, 1.0f);                  / Top Right Of The Quad (Bottom)

67、0;   glVertex3f(-1.0f,-1.0f, 1.0f);                  / Top Left Of The Quad (Bottom)    glVertex3f(-1.0f,-1.0f,-1.0f);         

68、60;        / Bottom Left Of The Quad (Bottom)    glVertex3f( 1.0f,-1.0f,-1.0f);                  / Bottom Right Of The Quad (Bottom)   

69、 glColor3f(1.0f,0.0f,0.0f);                      / Set The Color To Red    glVertex3f( 1.0f, 1.0f, 1.0f);          

70、        / Top Right Of The Quad (Front)    glVertex3f(-1.0f, 1.0f, 1.0f);                  / Top Left Of The Quad (Front)    glVer

71、tex3f(-1.0f,-1.0f, 1.0f);                  / Bottom Left Of The Quad (Front)    glVertex3f( 1.0f,-1.0f, 1.0f);             &#

72、160;    / Bottom Right Of The Quad (Front)    glColor3f(1.0f,1.0f,0.0f);                      / Set The Color To Yellow    glVerte

73、x3f( 1.0f,-1.0f,-1.0f);                  / Top Right Of The Quad (Back)    glVertex3f(-1.0f,-1.0f,-1.0f);              &

74、#160;   / Top Left Of The Quad (Back)    glVertex3f(-1.0f, 1.0f,-1.0f);                  / Bottom Left Of The Quad (Back)    glVertex3f( 1.0f, 1.0f,-1.0f);

75、                  / Bottom Right Of The Quad (Back)    glColor3f(0.0f,0.0f,1.0f);                  &

76、#160;   / Set The Color To Blue    glVertex3f(-1.0f, 1.0f, 1.0f);                  / Top Right Of The Quad (Left)    glVertex3f(-1.0f, 1.0f,-1.0f); &#

77、160;                / Top Left Of The Quad (Left)    glVertex3f(-1.0f,-1.0f,-1.0f);                  / Bottom Left Of The Quad (Left)    glVertex3f(-1

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論