• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Shuffy

            不斷的學(xué)習(xí),不斷的思考,才能不斷的進(jìn)步.Let's do better together!
            posts - 102, comments - 43, trackbacks - 0, articles - 19

            源代碼下載:OpenGL_ch2.rar 

            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.

            舉個例子,若像素格式只支持很少一部分顏色值,則OpenGL在用RGB(128,120,135)繪制一個像素時,就可能使用轉(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.

            第一個基于MFCOpenGL應(yīng)用程

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

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

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

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

            //OpenGL Headers

            #include 
            <gl/gl.h>

            #include 
            <gl/glu.h>

            #include 
            <gl/glut.h>

            #include 
            <gl/glaux.h>

            2, 打開ClassWizard,選擇CCY457OpenGLView類,為下述消息加入消息處理函數(shù):WM_CREATE (for OnCreate), WM_DESTROY (for OnDestroy), WM_SIZE (for OnSize), WM_ERASEBACKGROUND (for OnEraseBkground).

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

            BOOL CCY457OpenGLView::PreCreateWindow(CREATESTRUCT& cs)
            {
                
            // TODO: Modify the Window class or styles here by modifying
                
            //  the CREATESTRUCT cs
                
            //An OpenGL Window must be created with the following flags
                cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
                
            return CView::PreCreateWindow(cs);
            }

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

                HGLRC m_hRC;    //Rendering Context
                CDC* m_pDC;        //Device Context
                BOOL InitializeOpenGL();    //Initialize OpenGL
                BOOL SetupPixelFormat();    //Set up the Pixel Format
                void RenderScene();            //Render the Scene

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

            int CCY457OpenGLView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
            {
                
            if (CView::OnCreate(lpCreateStruct) == -1)
                    
            return -1;
                
            //Initialize OpenGL Here
                InitializeOpenGL();
                
            return 0;
            }

            BOOL CCY457OpenGLView::InitializeOpenGL()
            {
                
            //Get a DC for the Client Area
                m_pDC = new CClientDC(this);
                
            //Failure to Get DC
                if(m_pDC == NULL)
                {
                    MessageBox(
            "Error Obtaining DC");
                    
            return FALSE;
                }
                
            //Failure to set the pixel format
                if(!SetupPixelFormat())
                {
                    
            return FALSE;
                }
                
            //Create Rendering Context
                m_hRC = ::wglCreateContext (m_pDC->GetSafeHdc ());
                
            //Failure to Create Rendering Context
                if(m_hRC == 0)
                {
                    MessageBox(
            "Error Creating RC");
                    
            return FALSE;
                }
                
            //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
                ::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;
            }
            //Setup Pixel Format
            /////////////////////////////////////////////////////////////////////////////
            BOOL CCY457OpenGLView::SetupPixelFormat()
            {
              
            static PIXELFORMATDESCRIPTOR pfd = 
                {
                    
            sizeof(PIXELFORMATDESCRIPTOR),  // size of this pfd
                    1,                              // version number
                    PFD_DRAW_TO_WINDOW |            // support window
                    PFD_SUPPORT_OPENGL |            // support OpenGL
                    PFD_DOUBLEBUFFER,                // double buffered
                    PFD_TYPE_RGBA,                  // RGBA type
                    24,                             // 24-bit color depth
                    000000,               // color bits ignored
                    0,                              // no alpha buffer
                    0,                              // shift bit ignored
                    0,                              // no accumulation buffer
                    0000,                     // accum bits ignored
                    16,                             // 16-bit z-buffer
                    0,                              // no stencil buffer
                    0,                              // no auxiliary buffer
                    PFD_MAIN_PLANE,                 // main layer
                    0,                              // reserved
                    000                         // layer masks ignored
                };
                
            int m_nPixelFormat = ::ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd);
                
            if ( m_nPixelFormat == 0 )
                {
                   
            return FALSE;
                }
                
            if ( ::SetPixelFormat(m_pDC->GetSafeHdc(), m_nPixelFormat, &pfd) == FALSE)
                {
                   
            return FALSE;
                }
                
            return TRUE;
            }

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

            void CCY457OpenGLView::OnSize(UINT nType, int cx, int cy) 
            {
                CView::OnSize(nType, cx, cy);
                GLdouble aspect_ratio; 
            // width/height ratio
                
                
            if ( 0 >= cx || 0 >= cy )
                {
                    
            return;
                }
                
            // select the full client area
                ::glViewport(00, cx, cy);
                
            // compute the aspect ratio
                
            // this will keep all dimension scales equal
                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);
                
                
            // switch back to the modelview matrix and clear it
                ::glMatrixMode(GL_MODELVIEW);
                ::glLoadIdentity();
            }

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

            void CCY457OpenGLView::OnDraw(CDC* pDC)
            {
                CCY457OpenGLDoc
            * pDoc = GetDocument();
                ASSERT_VALID(pDoc);
                
            // Clear out the color & depth buffers
                ::glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
                RenderScene();
                
            // Tell OpenGL to flush its pipeline
                ::glFinish();
                
            // Now Swap the buffers
                ::SwapBuffers( m_pDC->GetSafeHdc() );
            }
            void CCY457OpenGLView::RenderScene ()
            {
            //第一個玩具嘛,先空著,后面慢慢填
            }

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

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

            BOOL CCY457OpenGLView::OnEraseBkgnd(CDC* pDC) 
            {
                
            //Tell Windows not to erase the background
                return TRUE;
            }

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

            void CCY457OpenGLView::OnDestroy() 
            {
                CView::OnDestroy();
                
            //Make the RC non-current
                if(::wglMakeCurrent (0,0== FALSE)
                {
                    MessageBox(
            "Could not make RC non-current");
                }
                
                
            //Delete the rendering context
                if(::wglDeleteContext (m_hRC)==FALSE)
                {
                    MessageBox(
            "Could not delete RC");
                }
                
            //Delete the DC
                if(m_pDC)
                {
                    delete m_pDC;
                }
                
            //Set it to NULL
                m_pDC = NULL;
            }

             

            作者:洞庭散人

            出處:http://phinecos.cnblogs.com/    

            本文版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責(zé)任的權(quán)利。
            原文鏈接:http://www.cnblogs.com/phinecos/archive/2008/11/04/1326687.html
            久久精品一区二区影院| 久久综合色区| 亚洲伊人久久精品影院| 欧美牲交A欧牲交aⅴ久久| 国产精品99精品久久免费| 久久精品一区二区三区不卡| 免费观看久久精彩视频| 久久99精品久久久久久秒播| 一本久久知道综合久久| 久久国产色AV免费观看| 亚洲国产成人精品女人久久久 | 久久精品国产亚洲AV蜜臀色欲 | 一本大道加勒比久久综合| 久久综合视频网| 91精品日韩人妻无码久久不卡| 久久久久人妻一区精品| 久久精品无码专区免费青青| 日韩影院久久| 伊人热人久久中文字幕| 亚洲精品乱码久久久久久蜜桃图片| 中文字幕久久亚洲一区| 日批日出水久久亚洲精品tv| 精品免费久久久久久久| 麻豆久久久9性大片| 色综合久久中文综合网| 精品熟女少妇av免费久久| 久久久久波多野结衣高潮| 久久久久久久久久免免费精品 | 久久国产精品二国产精品| 精品综合久久久久久888蜜芽| 久久婷婷五月综合97色直播| 国产精品嫩草影院久久| 国产精品亚洲美女久久久| MM131亚洲国产美女久久| 97精品依人久久久大香线蕉97| 午夜精品久久久久久久无码| 日本精品久久久久久久久免费| 久久97久久97精品免视看秋霞| 国产午夜福利精品久久| 国产精品内射久久久久欢欢| 精品99久久aaa一级毛片|