国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
《基于MFC的OpenGL編程》Part 2 Setting up OpenGL on Windows


關(guān)鍵字: malloc wxWidgets OpenGL 多態(tài)性 doxygen

《基于MFC的OpenGL編程》Part 2 Setting up OpenGL on Windows。

 本文示例源代碼或素材下載

  WGL – Windows的 OpenGL擴(kuò)展層

  The WGL extension consists of a set of functions (wglCreateContext, wglDeleteContext etc.) and structures (such as PIXELFORMATDESCRIPTOR, GLYPHMETRICSFLOAT) etc. Thus every OpenGL implementation has a platform-specific portion which has to be set up and used according to the particular platform.

  設(shè)備上下文

  The Windows Graphical Device Interface (GDI) is capable of drawing to screen, to memory, to printers or to any other device that provides a GDI interface layer and that can process GDI calls. GDI accomplishes this by a rendering handle to the currently selected device, which is called the device context, or DC.

  繪制上下文

  A rendering context is the OpenGL equivalent of the GDI DC. All OpenGL calls are rendered to the device through a RC. The rendering context maintains OpenGL state variables such as current background color, current color etc. just as the DC maintains GDI state variables such as current pen, current brush etc.

  像素格式

  Pixel formats are the translation layer between OpenGL calls and the rendering operation that Windows performs.

  舉個(gè)例子,若像素格式只支持很少一部分顏色值,則OpenGL在用RGB值(128,120,135)繪制一個(gè)像素時(shí),就可能使用轉(zhuǎn)換后的值(128,128,128)來繪制.

  The pixel format selected essentially describes such things as how colors are displayed, depth of field resolution and what additional capabilities are supported by the rendering context created.

  第一個(gè)基于MFC的OpenGL應(yīng)用程

  開發(fā)環(huán)境:VC6.0

  1, 首先下載需要的GLUT頭文件,DLL和Lib文件,下載鏈接: glutdlls37beta.zip (149 kilobytes),解壓縮后把gltu.h放到"VC98/Include/GL"下,把glut.lib和glut32.lib放到"VC9/Lib" 下,glut32.dll和glut.dll放到你創(chuàng)建的應(yīng)用程序的運(yùn)行目錄下

  2, 創(chuàng)建一個(gè)MFC SDI應(yīng)用程序,在項(xiàng)目屬性中加入所需要鏈接的庫(kù)文件

  1, 在stdafx.h中加入下列語句:

//OpenGLHeaders
#include<gl/gl.h>
#include<gl/glu.h>
#include<gl/glut.h>
#include<gl/glaux.h>

  3,在窗口創(chuàng)建之前我們必須設(shè)置窗口風(fēng)格包含WS_CLIPCHILDREN和 WS_CLIPSIBLINGS,從而避免OpenGL繪制到其他窗口中去。這些應(yīng)該放在PreCreateWindow()中。

BOOLCCY457OpenGLView::PreCreateWindow(CREATESTRUCT&cs)
{
  //TODO:ModifytheWindowclassorstylesherebymodifying
  // theCREATESTRUCTcs
  //AnOpenGLWindowmustbecreatedwiththefollowingflags
  cs.style|=WS_CLIPSIBLINGS|WS_CLIPCHILDREN;
  returnCView::PreCreateWindow(cs);
}

  4,在CCY457OpenGLView.h中加入如下語句:

  HGLRCm_hRC;  //RenderingContext
  CDC*m_pDC;    //DeviceContext
  BOOLInitializeOpenGL();  //InitializeOpenGL
  BOOLSetupPixelFormat();  //SetupthePixelFormat
  voidRenderScene();      //RendertheScene

  5,在OnCreate中我們將通過建立像素格式和繪制上下文來初始化OpenGL. 在InitializeOpenGL()中會(huì)創(chuàng)建一個(gè)設(shè)備上下文(DC),為這個(gè)DC選擇一個(gè)像素格式,創(chuàng)建和這個(gè)DC相關(guān)的繪制上下文(RC),然后選擇這個(gè)RC.這個(gè)函數(shù)會(huì)調(diào)用SetupPixelFormat()來建立像素格式。

intCCY457OpenGLView::OnCreate(LPCREATESTRUCTlpCreateStruct)
{
  if(CView::OnCreate(lpCreateStruct)==-1)
    return-1;
  //InitializeOpenGLHere
  InitializeOpenGL();
  return0;
}
BOOLCCY457OpenGLView::InitializeOpenGL()
{
  //GetaDCfortheClientArea
  m_pDC=newCClientDC(this);
  //FailuretoGetDC
  if(m_pDC==NULL)
  {
    MessageBox("ErrorObtainingDC");
    returnFALSE;
  }
  //Failuretosetthepixelformat
  if(!SetupPixelFormat())
  {
    returnFALSE;
  }
  //CreateRenderingContext
  m_hRC=::wglCreateContext(m_pDC->GetSafeHdc());
  //FailuretoCreateRenderingContext
  if(m_hRC==0)
  {
    MessageBox("ErrorCreatingRC");
    returnFALSE;
  }
  //MaketheRCCurrent
  if(::wglMakeCurrent(m_pDC->GetSafeHdc(),m_hRC)==FALSE)
  {
    MessageBox("ErrormakingRCCurrent");
    returnFALSE;
  }
  //SpecifyBlackastheclearcolor
  ::glClearColor(0.0f,0.0f,0.0f,0.0f);
  //Specifythebackofthebufferascleardepth
  ::glClearDepth(1.0f);
  //EnableDepthTesting
  ::glEnable(GL_DEPTH_TEST);
  returnTRUE;
}
//SetupPixelFormat
/////////////////////////////////////////////////////////////////////////////
BOOLCCY457OpenGLView::SetupPixelFormat()
{
 staticPIXELFORMATDESCRIPTORpfd=
  {
    sizeof(PIXELFORMATDESCRIPTOR), //sizeofthispfd
    1,               //versionnumber
    PFD_DRAW_TO_WINDOW|      //supportwindow
    PFD_SUPPORT_OPENGL|      //supportOpenGL
    PFD_DOUBLEBUFFER,        //doublebuffered
    PFD_TYPE_RGBA,         //RGBAtype
    24,              //24-bitcolordepth
    0,0,0,0,0,0,       //colorbitsignored
    0,               //noalphabuffer
    0,               //shiftbitignored
    0,               //noaccumulationbuffer
    0,0,0,0,          //accumbitsignored
    16,              //16-bitz-buffer
    0,               //nostencilbuffer
    0,               //noauxiliarybuffer
    PFD_MAIN_PLANE,        //mainlayer
    0,               //reserved
    0,0,0            //layermasksignored
  };
  intm_nPixelFormat=::ChoosePixelFormat(m_pDC->GetSafeHdc(),&pfd);
  if(m_nPixelFormat==0)
  {
   returnFALSE;
  }
  if(::SetPixelFormat(m_pDC->GetSafeHdc(),m_nPixelFormat,&pfd)==FALSE)
  {
   returnFALSE;
  }
  returnTRUE;
}

  6,在OnSize()中一般用來設(shè)置視口和視錐,因?yàn)檫@些是和窗口大小相關(guān)的?;静僮靼ㄔO(shè)置視口,選擇投影矩陣,設(shè)置模型視圖矩陣。

voidCCY457OpenGLView::OnSize(UINTnType,intcx,intcy)
{
  CView::OnSize(nType,cx,cy);
  GLdoubleaspect_ratio;//width/heightratio
  
  if(0>=cx||0>=cy)
  {
    return;
  }
  //selectthefullclientarea
  ::glViewport(0,0,cx,cy);
  //computetheaspectratio
  //thiswillkeepalldimensionscalesequal
  aspect_ratio=(GLdouble)cx/(GLdouble)cy;
  //selecttheprojectionmatrixandclearit
  ::glMatrixMode(GL_PROJECTION);
  ::glLoadIdentity();
  //selecttheviewingvolume
  ::gluPerspective(45.0f,aspect_ratio,.01f,200.0f);
  
  //switchbacktothemodelviewmatrixandclearit
  ::glMatrixMode(GL_MODELVIEW);
  ::glLoadIdentity();
}

  7,在繪制場(chǎng)景時(shí),一般包括如下步驟:1)清空緩存。2)繪制場(chǎng)景。3)Flush掉渲染流水線。4)若設(shè)置了雙緩沖,則交換前后臺(tái)緩沖區(qū)。

voidCCY457OpenGLView::OnDraw(CDC*pDC)
{
  CCY457OpenGLDoc*pDoc=GetDocument();
  ASSERT_VALID(pDoc);
  //Clearoutthecolor&depthbuffers
  ::glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  RenderScene();
  //TellOpenGLtoflushitspipeline
  ::glFinish();
  //NowSwapthebuffers
  ::SwapBuffers(m_pDC->GetSafeHdc());
}
voidCCY457OpenGLView::RenderScene()
{//第一個(gè)玩具嘛,先空著,后面慢慢填


}

  8,試試改變窗口的大小,你會(huì)看到很嚴(yán)重的閃爍,并且關(guān)閉程序后會(huì)報(bào)告內(nèi)存泄露,因此我們這就來解決這兩個(gè)問題吧。

  發(fā)生閃爍的原因是Windows先繪制背景,然后再是OpenGL繪制,因?yàn)槲覀円呀?jīng)讓OpenGL負(fù)責(zé)清空背景色,因此我們不需要Windows去清空背景了

BOOLCCY457OpenGLView::OnEraseBkgnd(CDC*pDC)
{
  //TellWindowsnottoerasethebackground
  returnTRUE;
}

  內(nèi)存泄露的原因是我們?cè)赟etupPixelFormat()中使用了new運(yùn)算符來為CClientDC對(duì)象分配內(nèi)存,因此需要顯示delete掉。

voidCCY457OpenGLView::OnDestroy()
{
  CView::OnDestroy();
  //MaketheRCnon-current
  if(::wglMakeCurrent(0,0)==FALSE)
  {
    MessageBox("CouldnotmakeRCnon-current");
  }
  
  //Deletetherenderingcontext
  if(::wglDeleteContext(m_hRC)==FALSE)
  {
    MessageBox("CouldnotdeleteRC");
  }
  //DeletetheDC
  if(m_pDC)
  {
    deletem_pDC;
  }
  //SetittoNULL
  m_pDC=NULL;
}

來源:博客園    作者:Phinecos(洞庭散人)  

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
MFC單文檔程序中搭建OpenGL框架
fedora23 安裝OpenGL
10 《高效學(xué)習(xí)OpenGL》之Hello OpenGl
OpenGL游戲?qū)W習(xí)一:MFC下OpenGL環(huán)境的搭建
vs2012搭建OpenGL環(huán)境 – 小寶個(gè)人筆記
OpenGL繪制長(zhǎng)方體
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服