• <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

            《基于MFC的OpenGL編程》Part 15 Selection

            Posted on 2010-03-31 20:50 Shuffy 閱讀(570) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): MFC,OpenGL

            Selection

            Selection is a powerful feature of OpenGL that allows you click at some position of the OpenGL window using the mouse and determine which of your objects lie beneath it. The act of selecting a specific object is called Picking. With OpenGL's selection feature, we can specify a viewing volume and determine which objects fall within that viewing volume. A powerful utility function, gluPickMatrix, produces a matrix which can be used to create a smaller viewing volume placed beneath the mouse cursor. Then we use selection to test this viewing volume to see which objects are contained by it.

            Selection is actually a rendering mode, but in this mode no pisels are actually copied onto the frame buffer. Instead, primitives drawn within the viewing volume produce hit records in a selection buffer. We must set up this selection buffer in advance and name the primitives or groups of primitives so that they can be identified in the selection buffer.We can then parse the buffer to determine which objects intersected the viewing volume.

            Naming Primitives

            We have to name a group of primitives such as one describing a cube or a cylinder etc in order to identify them. These names are nothing but integers such as for display list names. The names list is maintained on the named stack. After we initialize the name stack we can push names on the stack or simply replace the name currently on the top of the stack. When a hit occurs during selection, all the names on the stack are copied onto the selection buffer.

            1,CCY457OpenGLView類(lèi)中加入一個(gè)變量,用來(lái)表示寬高比

                  GLdouble m_aspectRatio; // width/height ratio

            2OnSize函數(shù)修改如下:

            void CCY457OpenGLView::OnSize(UINT nType, int cx, int cy)
            {
                  CView::OnSize(nType, cx, cy);
                  
            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
                  m_aspectRatio = (GLdouble)cx/(GLdouble)cy;
                  
            // select the projection matrix and clear it
                ::glMatrixMode(GL_PROJECTION);
                ::glLoadIdentity();
                  
            // select the viewing volume
                  ::gluPerspective(45.0f, m_aspectRatio, .01f, 200.0f);
                  
            //::gluOrtho2D(-10.0f, 10.0f, -10.0f, 10.0f);   
                  
            // switch back to the modelview matrix and clear it
                ::glMatrixMode(GL_MODELVIEW);
                ::glLoadIdentity();
            }

            3,繪制函數(shù)修改如下:

            void CCY457OpenGLView::RenderScene ()
            {
            //繪制函數(shù)
                  glTranslatef(0.0f,0.0f,-5.0f);
                  glRotatef(m_xRot,
            1.0f,0.0f,0.0f);
                  glRotatef(m_yRot,
            0.0f,1.0f,0.0f);
                  glInitNames();
                  glPushName(
            0);
                  glPushMatrix();
                       glTranslatef(
            -2.0f,0.0f,0.0f);
                       glLoadName(
            1);
                       glutSolidSphere(
            1.0f,20,20);
                  glPopMatrix();
                  glPushMatrix();
                       glLoadName(
            2);
                       glTranslatef(
            2.0f,0.0f,0.0f);
                       glutSolidCube(
            1.0f);
                  glPopMatrix();
            }

            4,加入對(duì)鼠標(biāo)左鍵選擇物體的處理

            void CCY457OpenGLView::ProcessSelection (CPoint point)
            {
                  
            int xPos = point.x ;
                  
            int yPos = point.y ;
                  GLuint selectBuff[
            64];
                  GLint hits, viewport[
            4];
                  glSelectBuffer(
            64, selectBuff);
                  glGetIntegerv(GL_VIEWPORT, viewport);
                  glMatrixMode(GL_PROJECTION);
                  glPushMatrix();
                  glRenderMode(GL_SELECT);
                  glLoadIdentity();
                  gluPickMatrix(xPos, viewport[
            3- yPos, 2,2, viewport);
                  gluPerspective(
            45.0f, m_aspectRatio, .01f, 200.0f);
                  RenderScene();
                  hits 
            = glRenderMode(GL_RENDER);
                  
            if(hits==1)
                       ProcessObject(selectBuff);
                  glMatrixMode(GL_PROJECTION);
                  glPopMatrix();
                  glMatrixMode(GL_MODELVIEW);
            }
            void CCY457OpenGLView::ProcessObject (GLuint* pSelectBuff)
            {
                  
            int id = pSelectBuff[3];
                  
            if(id==1)
                       MessageBox(
            "You clicked on Sphere");
                  
            if(id==2)
                       MessageBox(
            "You clicked on Cube");
            }
            void CCY457OpenGLView::OnLButtonDown(UINT nFlags, CPoint point)
            {
                  ProcessSelection(point);
                  CView::OnLButtonDown(nFlags, point);
            }

            作者:洞庭散人

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

            本文版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁(yè)面明顯位置給出原文連接,否則保留追究法律責(zé)任的權(quán)利。
            原文鏈接:http://www.cnblogs.com/phinecos/archive/2008/11/07/1328861.html
            精品免费tv久久久久久久| 久久久久se色偷偷亚洲精品av| 久久久久这里只有精品| 99久久国产亚洲综合精品| 999久久久无码国产精品| 久久一区二区免费播放| 中文字幕无码久久精品青草| 久久国产成人精品麻豆| 久久久久久毛片免费看| 久久久久久久久无码精品亚洲日韩 | 久久无码人妻精品一区二区三区| 偷窥少妇久久久久久久久| 99久久无码一区人妻a黑| 精品无码久久久久国产动漫3d| 国产欧美久久一区二区| 久久亚洲精品无码aⅴ大香| 国产叼嘿久久精品久久| 久久久久免费精品国产| 精品国产91久久久久久久a| 久久国产乱子精品免费女| 国产精品一区二区久久精品涩爱| 伊人久久免费视频| 久久国产精品免费一区| 91精品国产乱码久久久久久 | 99热都是精品久久久久久| 无码人妻久久一区二区三区免费| 亚洲中文字幕伊人久久无码 | 久久99精品久久久久久秒播| 久久精品国产亚洲av麻豆色欲| 日本加勒比久久精品| 久久精品国产亚洲5555| 99久久精品免费看国产一区二区三区| 综合久久国产九一剧情麻豆| 久久水蜜桃亚洲av无码精品麻豆 | 日韩电影久久久被窝网| 狠狠久久综合伊人不卡| 久久高清一级毛片| 亚洲精品国精品久久99热| 中文成人无码精品久久久不卡| 久久人人爽人人爽人人片av麻烦| 久久大香萑太香蕉av|