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

            eryar

            PipeCAD - Plant Piping Design Software.
            RvmTranslator - Translate AVEVA RVM to OBJ, glTF, etc.
            posts - 603, comments - 590, trackbacks - 0, articles - 0

            OpenCASCADE View Manipulator

            Posted on 2014-12-20 19:52 eryar 閱讀(4477) 評論(2)  編輯 收藏 引用 所屬分類: 2.OpenCASCADE

            OpenCASCADE View Manipulator

            eryar@163.com

            Abstract. When you finish modeling objects in the scene, you must want to use some operations to view the scene objects, such as Pan, Zoom and Rotate the scene. Pan and Zoom is easy to understand, rotate the 3D scene according to 2D point in the viewport is a little complicated. There are many methods to rotate the 3D scene, but the Arcball Controller is intuitive for the user and any viewport can be described. You can rotate your model at will just by using the mouse.

            Key Words. OpenCASCADE Camera, View, ArcBall, Rotate

            1. Introduction

            當用OpenGL建立了一個模型場景后,就需要有便捷的操作來觀察場景中的物體。場景的觀察即注重于一個從三維世界轉(zhuǎn)換到二維屏幕的過程。假設場景的觀察者使用一臺相機來記錄世界的變化,那么相機的移動、角度偏轉(zhuǎn)、焦距變化都會改變底片上顯現(xiàn)的內(nèi)容,也就是觀察這個世界的方式,這涉及到三維人機的交互。

            三維用戶交互是一種與三維環(huán)境本身特性相匹配的交互動作,可使用戶在虛擬場景中獲得身臨其境的直觀感受。三維世界的交互技術(shù)相當于一種“控制-顯示”的映射,用戶設備例如鼠標、鍵盤等向系統(tǒng)輸入控制信息,然后系統(tǒng)向用戶輸出執(zhí)行結(jié)果。所以首先要對硬件設備的輸入信息進行處理,然后就是根據(jù)這些信息來改變場景數(shù)據(jù)。

            三維交互涉及的任務很多,包括三維場景對象的選擇和編輯、三維世界中的導航漫游,乃至時下流行的三維交互建模等。本文主要介紹如何通過改變像機參數(shù)來對場景進行瀏覽,如對場景的平移、縮放和旋轉(zhuǎn)操作。

            wps_clip_image-22681

            Figure 1.1 OpenCASCADE Viewer 

            2.Translate View

            場景的移動就是改變觀察相機的位置,相對容易理解,在OpenCASCADE的類V3d_View中也是這樣實現(xiàn)的,代碼如下所示:

            // ==================================================================
            // function : Translate
            // purpose  : Internal
            // ==================================================================
            void V3d_View::Translate (const Handle(Graphic3d_Camera)& theCamera,
                                      
            const Standard_Real theDXv,
                                      
            const Standard_Real theDYv) const
            {
              
            const gp_Pnt& aCenter = theCamera->Center();
              
            const gp_Dir& aDir = theCamera->Direction();
              
            const gp_Dir& anUp = theCamera->Up();

              gp_Ax3 aCameraCS (aCenter, aDir.Reversed(), aDir 
            ^ anUp);
              gp_Vec aCameraPanXv 
            = gp_Vec (aCameraCS.XDirection()) * theDXv;
              gp_Vec aCameraPanYv 
            = gp_Vec (aCameraCS.YDirection()) * theDYv;
              gp_Vec aCameraPan 
            = aCameraPanXv + aCameraPanYv;

              gp_Trsf aPanTrsf;
              aPanTrsf.SetTranslation (aCameraPan);

              theCamera
            ->Transform (aPanTrsf);
            }

            由上述代碼可知,根據(jù)兩次鼠標位置計算出需要移動的偏移量來對相機進行移動變換。根據(jù)鼠標第一次按下及移動過程中的坐標點來計算偏移量。計算偏移量時,需要注意坐標系的統(tǒng)一,即要么都在視口坐標系,要么都在世界坐標系中。如下代碼是將鼠標點變換到世界坐標系中進行移動:

            void ArcballController::Translate(const gp_Pnt2d &thePoint)
            {
              gp_Pnt aCurrentPoint 
            = Convert2World(thePoint);
              
                gp_Trsf aTrsf;
              aTrsf.SetTranslation(aCurrentPoint, mPreviousPoint);
              
                mCamera
            ->Transform(aTrsf);
            }

            對相機參數(shù)進行修改后,需要更新場景數(shù)據(jù)。移動場景只涉及到MODELVIEW變換,所以需要刷新模型視圖MODELVIEW變換矩陣數(shù)據(jù)并重繪場景,相關(guān)代碼如下所示:

            // model/view transformation for pan and rotate.
            glMatrixMode(GL_MODELVIEW);
            glLoadIdentity();
            glLoadMatrixf(theArcballController.GetOrientationMatrix());

            其中theArcballController的這個函數(shù)是調(diào)用了Graphic3d_Camera的函數(shù)來設置模型視圖變換矩陣。經(jīng)過測試,移動效果還可以,如下圖所示為將一個Teapot從屏幕左上角移動到了右下角:

            wps_clip_image-8405

            Figure 2.1 Translate the Scene

            3.Zoom View

            對于透視投影而言,靠模型越近,看到模型就越大,因為透視投影的特點就是近大遠小。而對平行投影而言,這種規(guī)律就不適用了。其實二者都可以統(tǒng)一到通過調(diào)整視口大小來對場景模型進行縮放。同樣的模型,當投影到較大的視口中時,模型的投影得到的二維圖形也會較大;當投影到較小的視口中時,模型的投影得到的二維圖形也會較小。這樣就達到對場景進行縮放的目的了。其中OpenCASCADE中的實現(xiàn)是通過設置Graphic3d_Camera的Scale來實現(xiàn)的,代碼如下圖所示:

            //===================================================================
            //function : SetZoom
            //purpose  :
            //===================================================================
            void V3d_View::SetZoom(const Standard_Real Coef,const Standard_Boolean Start)
            {
              V3d_BadValue_Raise_if( Coef 
            <= 0.,"V3d_View::SetZoom, bad coefficient");
              
            if (Start)
              {
                myCamStartOpEye    
            = myCamera->Eye();
                myCamStartOpCenter 
            = myCamera->Center();
              }

              Standard_Real aViewWidth  
            = myCamera->ViewDimensions().X();
              Standard_Real aViewHeight 
            = myCamera->ViewDimensions().Y();

              
            // ensure that zoom will not be too small or too big
              Standard_Real coef = Coef;
              
            if (aViewWidth < coef * Precision::Confusion())
              {
                coef 
            = aViewWidth / Precision::Confusion();
              }
              
            else if (aViewWidth > coef * 1e12)
              {
                coef 
            = aViewWidth / 1e12;
              }
              
            if (aViewHeight < coef * Precision::Confusion())
              {
                coef 
            = aViewHeight / Precision::Confusion();
              }
              
            else if (aViewHeight > coef * 1e12)
              {
                coef 
            = aViewHeight / 1e12;
              }

              myCamera
            ->SetEye (myCamStartOpEye);
              myCamera
            ->SetCenter (myCamStartOpCenter);
              myCamera
            ->SetScale (myCamera->Scale() / Coef);
              View()
            ->AutoZFit();

              ImmediateUpdate();
            }

            根據(jù)鼠標點計算出縮改系數(shù),通過myCamera->SetScale()來達到對場景進行縮放的目的。場景縮放操作涉及到需要更新OpenGL的投影矩陣數(shù)據(jù),代碼如下所示:

            // projection transformation for zoom.
            glMatrixMode(GL_PROJECTION);
            glLoadIdentity();
            glLoadMatrixf(theArcballController.GetProjectionMatrix());

            wps_clip_image-31482

            Figure 3.1 Zoom the scene

            4.Rotate View

            通過鼠標在二維屏幕上來旋轉(zhuǎn)三維的場景有幾種方法,如下圖所示:

            wps_clip_image-1268

            Figure 4.1 3D Rotation(http://www.cabiatl.com/mricro/obsolete/graphics/3d.html)

            方法一是通過Euler Angles來實現(xiàn),好處是用戶比較容易理解Euler角,如yaw, pitch和roll,如下圖所示:

            wps_clip_image-19192

            Figure 4.2 Euler Angles: Yaw, Pitch and Roll

            缺點就是因為死鎖問題(gimbal lock)導致不能指定一些視圖,當出現(xiàn)死鎖問題時,操作就顯得不直觀了。

            比較直觀的方法就是ArcBall方式了,使用這種方法可以以任意方向來查看場景中的模型。有個網(wǎng)頁版的實現(xiàn),可以去體驗一下:

            http://www.math.tamu.edu/~romwell/arcball_js/index.html

            wps_clip_image-1409

            Figure 4.3 Arcball in Javascript

            ArcBall的原理是將二維屏幕上鼠標點轉(zhuǎn)換到球面上,拖動鼠標就是在轉(zhuǎn)動這個球。根據(jù)映射到球面的兩個點,通過矢量的點乘及叉乘得到旋轉(zhuǎn)角度及旋轉(zhuǎn)軸。通過這種方式可以將二維的鼠標位置映射到三維的場景來實現(xiàn)對場景觀察的直觀操作。

            OpenCASCADE中場景的旋轉(zhuǎn)方式是通過先遍歷場景中的模型計算出重心點,再繞三個坐標軸來旋轉(zhuǎn),代碼如下所示:

            //=============================================================================
            //function : Rotate
            //purpose  :
            //=============================================================================
            void V3d_View::Rotate(const Standard_Real ax, const Standard_Real ay, const Standard_Real az,
                                  
            const Standard_Real X, const Standard_Real Y, const Standard_Real Z, const Standard_Boolean Start)
            {

              Standard_Real Ax 
            = ax ;
              Standard_Real Ay 
            = ay ;
              Standard_Real Az 
            = az ;

              
            if( Ax > 0. ) while ( Ax > DEUXPI ) Ax -= DEUXPI ;
              
            else if( Ax < 0. ) while ( Ax < -DEUXPI ) Ax += DEUXPI ;
              
            if( Ay > 0. ) while ( Ay > DEUXPI ) Ay -= DEUXPI ;
              
            else if( Ay < 0. ) while ( Ay < -DEUXPI ) Ay += DEUXPI ;
              
            if( Az > 0. ) while ( Az > DEUXPI ) Az -= DEUXPI ;
              
            else if( Az < 0. ) while ( Az < -DEUXPI ) Az += DEUXPI ;

              
            if (Start)
              {
                myGravityReferencePoint.SetCoord (X, Y, Z);
                myCamStartOpUp 
            = myCamera->Up();
                myCamStartOpEye 
            = myCamera->Eye();
                myCamStartOpCenter 
            = myCamera->Center();
              }

              
            const Graphic3d_Vertex& aVref = myGravityReferencePoint;

              myCamera
            ->SetUp (myCamStartOpUp);
              myCamera
            ->SetEye (myCamStartOpEye);
              myCamera
            ->SetCenter (myCamStartOpCenter);

              
            // rotate camera around 3 initial axes
              gp_Pnt aRCenter (aVref.X(), aVref.Y(), aVref.Z());

              gp_Dir aZAxis (myCamera
            ->Direction().Reversed());
              gp_Dir aYAxis (myCamera
            ->Up());
              gp_Dir aXAxis (aYAxis.Crossed (aZAxis)); 

              gp_Trsf aRot[
            3], aTrsf;
              aRot[
            0].SetRotation (gp_Ax1 (aRCenter, aYAxis), -Ax);
              aRot[
            1].SetRotation (gp_Ax1 (aRCenter, aXAxis), Ay);
              aRot[
            2].SetRotation (gp_Ax1 (aRCenter, aZAxis), Az);
              aTrsf.Multiply (aRot[
            0]);
              aTrsf.Multiply (aRot[
            1]);
              aTrsf.Multiply (aRot[
            2]);

              myCamera
            ->Transform (aTrsf);

              View()
            ->AutoZFit();

              ImmediateUpdate();
            }

            5.Conclusion

            當實現(xiàn)三維場景的建模后,最激動人心的應該是對場景及場景中模型的控制。通過交互操作使用戶方便地觀察場景的模型,或直觀地編輯場景中的模型。所以交互也是三維軟件中的重要功能,且是給用戶最直接的感覺的操作。

            因為交互操作涉及到鼠標鍵盤消息的處理,所以首先要設計好對這些消息的處理方式,在OpenSceneGraph中使用了適配器的方式來實現(xiàn)跨平臺的消息處理,使用戶通過繼承的方式來實現(xiàn)對消息的處理。這種方式使程序的可擴展性及代碼的可讀性更好,OpenCASCADE中的消息的處理還是比較直接的,沒有什么封裝。

            本文主要介紹了如何實現(xiàn)對場景的控制,如移動、縮放及旋轉(zhuǎn)操作,這些功能的實現(xiàn)需要對OpenGL的渲染管線有一定的了解。在理解了對視圖/場景的控制后,為進一步理解對場景中的模型的控制打下基礎(chǔ),如選擇Picking,拖拽Drag等操作。最后給出一個基于OpenCASCADE的類Graphic3d_Camera及GLUT實現(xiàn)的場景變換操作,功能不是很完善,僅供參考。若有好的意見,歡迎反饋。

            6. References

            1. Brad Smith. ArcBall. http://rainwarrior.ca/dragon/arcball.html

            2. WikiBooks. Modern OpenGL Tutorial Arcball. 

            http://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_Arcball

            3. sgCore demo code. http://www.shnenglu.com/eryar/archive/2013/06/30/201411.html

            4. Virtual Trackballs Revisited. http://image.diku.dk/research/trackballs/index.html

            5. http://oviliazhang.diandian.com/post/2012-05-19/40027878859

            6. 王銳,錢學雷. OpenSceneGraph三維渲染引擎設計與實踐. 清華大學出版社. 2009

            PDF Version and Source code: OpenCASCADE View Manipulator

            Feedback

            # re: OpenCASCADE View Manipulator  回復  更多評論   

            2015-06-19 20:07 by 佚名
            博主您好,我想請教一下,如何改變view3d的z向顯示范圍,目前的情況是z向范圍不夠,自己通過opengl函數(shù)在屏幕邊部繪制點能顯示出來,但一旋轉(zhuǎn)到屏幕中間,就被屏蔽看不見了。

            # re: OpenCASCADE View Manipulator  回復  更多評論   

            2015-06-19 20:26 by eryar
            @佚名
            Hi ,
            你好!

            你可以調(diào)用Camera的SetZRange()或ZFitAll()試試看。

            Best Regards,
            Shing Liu
            无码AV中文字幕久久专区| 久久国产成人午夜AV影院| 久久无码AV中文出轨人妻| 久久久久久久91精品免费观看| 久久久久久久波多野结衣高潮| 国产精品99久久99久久久| 久久无码人妻精品一区二区三区 | 久久久久久无码国产精品中文字幕| 久久久久国色AV免费观看| 狠狠精品久久久无码中文字幕| 国产精品欧美久久久天天影视| 欧美精品一区二区久久| 久久狠狠色狠狠色综合| 狠狠色婷婷久久一区二区| 久久久久亚洲AV无码专区桃色| 久久久久99精品成人片试看 | 久久久久亚洲AV成人网人人网站| 久久久久99精品成人片欧美| 国产精品99久久久久久宅男小说| 国产高潮国产高潮久久久91 | 久久久久亚洲精品无码蜜桃 | 久久99国产精品二区不卡| 无码人妻久久一区二区三区蜜桃| 国产精品免费看久久久香蕉| 97久久久久人妻精品专区| 欧美黑人激情性久久| 精品久久久无码21p发布| 欧美成a人片免费看久久| 欧美粉嫩小泬久久久久久久| 狠狠色综合久久久久尤物| 精品久久综合1区2区3区激情| 精品久久久久久| 国内精品久久久久影院网站 | yy6080久久| 日本五月天婷久久网站| 久久精品国产亚洲AV久| 久久久久久精品免费免费自慰 | 亚洲AV乱码久久精品蜜桃| 久久精品中文字幕大胸| 亚洲中文字幕久久精品无码APP | 久久精品国产亚洲av麻豆图片|