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

            Posted on 2014-12-03 21:30 eryar 閱讀(3977) 評論(0)  編輯 收藏 引用 所屬分類: 2.OpenCASCADE

            OpenCASCADE Camera

            eryar@163.com

            Abstract. OpenCASCADE introduce a new class Graphic3d_Camera for the Visualization module. The camera class provides object-oriented approach to setting up projection and orientation properties of 3D view. The paper use GLUT to test the function of the new Camera class.

            Key Words. OpenCASCADE, Camera, OpenGL, GLUT, Transformations

            1. Introduction

            在三維場景中變換的過程與用相機拍照的過程很相似,如下圖所示,拍照的過程大概如下:

            wps_clip_image-29450

            Figure 1.1 The Camera Analogy (From the Red Book)

            v 放好三角架并使相機對準要拍照的場景(viewing transformation);

            v 布置好場景到理想的位置,以便拍照(modeling transformation);

            v 選擇鏡頭或調整變焦(projection transformation);

            v 確定最終的照片要有多大尺寸,例如你可能需要放大一些的照片(viewport transformation);

            OpenCASCADE在6.8.0版本中引入了一個Graphic3d_Camera類來對視圖的變換進行管理,通過對camera的調整,方便對場景中的模型/視圖(Model/View)進行變換。

            wps_clip_image-6395

            Figure 1.2 Camera in the Scene

            如下圖所示,對象的空間坐標經過一系列的變換,最終得到顯示屏幕上的二維坐標。

            wps_clip_image-31265

            Figure 1.3 Stages of Vertex Transformation (From the Red Book)

            為了指定viewing, modeling和projection變換,可以構造一個4x4的矩陣與頂點的齊次坐標相乘來實現這些變換。

            2.Coordinate Transforms

            在計算機圖形學中,造型MODELING就是定義一個可渲染物體的數字表示。對于OpenGL而言,通常就是用多邊形來近似表示一個物體。用多邊形表示物體至少需要每個多邊形頂點的坐標和連接關系的信息。附加的信息包含頂點顏色、法向量或貼圖坐標等。

            三維對象的屬性如頂點位置及面的法向量,為了造型的方便,通常定義在對象空間OBJECT SPACE中,即在局部坐標系中建模。對象空間通常也叫作模型空間MODEL SPACE或造型坐標系統MODELING COORDINATE SYSTEM。

            在一個包含大量三維對象的場景中,每一個對象都是在其對象空間中建模的,這是我們就需要一個統一的坐標系統COMMON COORDINATE SYSTEM,通常稱之為世界空間WORLD SPACE或世界坐標系WORLD COORDINATE SYSTEM,或絕對坐標系。

            定義了世界坐標系統后,場景中所有模型對象必須從其局部坐標系變換到世界坐標系中來。從對象空間變換到世界空間的變換稱之為模型變換MODELING TRANSFORMATION。若模型對象造型坐標系中的單位是feet,但是世界坐標系中是inches,對象坐標必須縮小12倍以得到世界坐標系。若對象在其對象空間中朝前的,但是在世界場景中需要其朝后,那么需要對模型對象進行一個旋轉變換。當要將模型對象放到場景中的合適位置時,那么就需要進行一個移動變換。所有這些單個變換也可以合成一個矩陣,即模型變換矩陣MODEL TRANSFORMATION MATRIX,就是表示將模型對象從對象坐標系變換到世界坐標系中。

            當場景確定好后,視圖viewing parameters的參數就需要確定了。視圖的參數之一就是視點vantage point(eye or camera position),即在哪里對模型進行觀察。視圖的參數還包括焦點focus point(also called the lookat point or the direction in which the camera is pointed)和向上的方向(the camera may be held sideways or upside down)。視圖的參數定義了視圖變換VIEWING TRANSFORMATION,它們也可以合成一個矩陣,稱作視圖矩陣VIEWING MATRIX。一個坐標乘以這個矩陣就可以將其從世界空間變換到觀察空間EYE SPACE,也稱為EYE COORDINATE SYSTEM。由定義可知,此坐標系的原點位于視點處。

            盡管有些3D圖形API允許分別指定模型變換矩陣modeling matrix和視圖變換矩陣viewing matrix,但是在OpenGL中將這兩者合成為一個矩陣,稱為模型/視圖矩陣MODELVIEW MATRIX。這個矩陣定義了坐標從對象空間變換到觀察空間的變換。

            wps_clip_image-5810

            Figure 2.1 Coordinate spaces and transforms in OpenGL(From OpenGL Shading Language)

            在OpenGL中,可以調用函數glMatrixMode來選擇modelview matrix或其他的矩陣,然后可以調用函數glLoadIdentity將當前矩陣設置為單位矩陣,或調用函數glLoadMatrix將當前矩陣替換為任意一個矩陣。替換時你需要清楚地知道你要進行的變換操作,否則容易產生完全不可理解的視圖。還可以調用函數glMultMatrix來將當前矩陣與任意矩陣相乘。可以通過調用函數gluLookAt來設置modelview變換矩陣。

            當坐標變換到觀察空間EYE SPACE后,下一步需要定義一個觀察體VIEWING VOLUME,即這個區域內的三維場景將出現在最終的圖像上。將模型對象放進觀察體即裁剪空間CLIP SPACE(也稱作裁剪坐標系CLIPPING COORDINATE SYSTEM)的變換稱為投影變換PROJECTION TRANSFORMATION。在OpenGL中,可以通過glMatrixModel來選擇需要設置的變換矩陣。也可以調用函數glOrtho、glFrustum、gluPerspective來設置。

            通過投影變換將觀察空間內的三維模型投影到了二維空間,下一步變換就是頂點坐標的perspective divide。這個操作將觀察體內所有坐標點的各個分量除以齊次坐標分量w。得到的x, y, z的取值范圍都是[-1, 1],而齊次坐標值w為1,所以就不再需要w了。換句話說所有可見的圖元graphics primitives都變換到一個在點(-1,-1,-1)和點(1,1,1)之間的立方體區域,這就是規范化設備坐標空間NORMALIZED DEVICE COORDINATE SPACE,他是個允許將觀察區域viewing area映射到任意尺寸及深度的視口的中間空間。

            窗口顯示的像素點pixel不能指向-1到1的浮點數,通常指向窗口坐標系WINDOW COORDINATE SYSTEM中的坐標,x取值范圍從0到窗口寬度減1,y取值范圍從0到窗口高度減1,因此還需要進行一步變換處理。視口變換指定了從規范設備坐標系到窗口坐標系的變換VIEWPORT TRANSFORMATION。在OpenGL通過函數glViewport來指定視口變換。

            以上內容主要來自《OpenGL Shading Language》一書中的第一章“Review of OpenGL Basics-1.9 Coordinate Transforms”,詳細內容可參考原書。也可利用下面的程序來直觀地學習這些變換及投影的知識:

            wps_clip_image-5668

            Figure 2.2 OpenGL Tutors-Projection (by Nate Robins)

            3.Camera of OpenCASCADE

            OpenCASCADE6.8.0版本中引入一個類Graphic3d_Camera來簡化對三維視圖的變換操作,即可以對視圖進行投影和模型視圖的相關設置。如通過設置視點Eye position和中心點可以計算得到觀察矩陣,類似gluLookAt的功能,相關代碼如下所示:

            // =======================================================================
            // function : UpdateOrientation
            // purpose  :
            // =======================================================================
            template <typename Elem_t>
            Graphic3d_Camera::TransformMatrices
            <Elem_t>&
              Graphic3d_Camera::UpdateOrientation (TransformMatrices
            <Elem_t>& theMatrices) const
            {
              
            if (theMatrices.IsOrientationValid())
              {
                
            return theMatrices; // for inline accessors
              }
              theMatrices.InitOrientation();

              NCollection_Vec3
            <Elem_t> anEye (static_cast<Elem_t> (myEye.X()),
                                              static_cast
            <Elem_t> (myEye.Y()),
                                              static_cast
            <Elem_t> (myEye.Z()));

              NCollection_Vec3
            <Elem_t> aCenter (static_cast<Elem_t> (myCenter.X()),
                                                static_cast
            <Elem_t> (myCenter.Y()),
                                                static_cast
            <Elem_t> (myCenter.Z()));

              NCollection_Vec3
            <Elem_t> anUp (static_cast<Elem_t> (myUp.X()),
                                             static_cast
            <Elem_t> (myUp.Y()),
                                             static_cast
            <Elem_t> (myUp.Z()));

              NCollection_Vec3
            <Elem_t> anAxialScale (static_cast<Elem_t> (myAxialScale.X()),
                                                     static_cast
            <Elem_t> (myAxialScale.Y()),
                                                     static_cast
            <Elem_t> (myAxialScale.Z()));

              LookOrientation (anEye, aCenter, anUp, anAxialScale, 
            *theMatrices.Orientation);

              
            return theMatrices; // for inline accessors
            }

            通過調用這個函數得到將世界坐標系變換到觀察坐標系的變換矩陣。其實現步驟如下:

            v 平移觀察坐標原點到世界坐標系原點;

            v 進行旋轉,分別讓觀察坐標系的Vx, Vy, Vz軸對應到世界坐標的Wx,Wy,Wz軸。

            OpenCASCADE中的實現代碼如下所示:

            // =======================================================================
            // function : LookOrientation
            // purpose  :
            // =======================================================================
            template <typename Elem_t>
            void Graphic3d_Camera::LookOrientation (const NCollection_Vec3<Elem_t>& theEye,
                                                  
            const NCollection_Vec3<Elem_t>& theLookAt,
                                                  
            const NCollection_Vec3<Elem_t>& theUpDir,
                                              
            const NCollection_Vec3<Elem_t>& theAxialScale,
                                                    NCollection_Mat4
            <Elem_t>& theOutMx)
            {
              NCollection_Vec3
            <Elem_t> aForward = theLookAt - theEye;
              aForward.Normalize();

              
            // side = forward x up
              NCollection_Vec3<Elem_t> aSide = NCollection_Vec3<Elem_t>::Cross (aForward, theUpDir);
              aSide.Normalize();

              
            // recompute up as: up = side x forward
              NCollection_Vec3<Elem_t> anUp = NCollection_Vec3<Elem_t>::Cross (aSide, aForward);

              NCollection_Mat4
            <Elem_t> aLookMx;
              aLookMx.SetRow (
            0, aSide);
              aLookMx.SetRow (
            1, anUp);
              aLookMx.SetRow (
            2-aForward);

              theOutMx.InitIdentity();
              theOutMx.Multiply (aLookMx);
              theOutMx.Translate (
            -theEye);

              NCollection_Mat4
            <Elem_t> anAxialScaleMx;
              anAxialScaleMx.ChangeValue (
            00= theAxialScale.x();
              anAxialScaleMx.ChangeValue (
            11= theAxialScale.y();
              anAxialScaleMx.ChangeValue (
            22= theAxialScale.z();

              theOutMx.Multiply (anAxialScaleMx);
            }

            當指定投影變換類型后,可以得到投影變換矩陣,可以指定的類型如下:

              //! Enumerates supported monographic projections.
              
            //! - Projection_Orthographic : orthographic projection.
              
            //! - Projection_Perspective  : perspective projection.
              
            //! - Projection_Stere        : stereographic projection.
              
            //! - Projection_MonoLeftEye  : mono projection for stereo left eye.
              
            //! - Projection_MonoRightEye : mono projection for stereo right eye.
              enum Projection
              {
                Projection_Orthographic,
                Projection_Perspective,
                Projection_Stereo,
                Projection_MonoLeftEye,
                Projection_MonoRightEye
              };

            當指定為Projection_Orthographic時即為正投影變換。使用從坐標位置到觀察平面的正投影變換,任意一點(x, y, z)的投影位置是(x, y)。因此在建立觀察體的范圍后,該矩形平行管道內部的坐標描述即為投影坐標,它們不需要另外的投影處理就可直接映射到規范化觀察體normalized view volume。有些圖形軟件包使用單位立方體作為規范化觀察體,其x,y,z坐標規范成0到1之間,另外的規范化變換方法使用坐標范圍從-1到1的對稱立方體。

            由于屏幕坐標系經常指定為左手系,因此規范化觀察體也常指定為左手系統。正投影觀察體的規范化變換矩陣是:詳細推導請參考《Computer Graphics with OpenGL》。

            wps_clip_image-6349

            對應OpenCASCADE中實現代碼如下所示:

            // =======================================================================
            // function : OrthoProj
            // purpose  :
            // =======================================================================
            template <typename Elem_t>
            void Graphic3d_Camera::OrthoProj (const Elem_t theLeft,
                                              
            const Elem_t theRight,
                                              
            const Elem_t theBottom,
                                              
            const Elem_t theTop,
                                              
            const Elem_t theNear,
                                              
            const Elem_t theFar,
                                              NCollection_Mat4
            <Elem_t>& theOutMx)
            {
              
            // row 0
              theOutMx.ChangeValue (00= Elem_t (2.0/ (theRight - theLeft);
              theOutMx.ChangeValue (
            01= Elem_t (0.0);
              theOutMx.ChangeValue (
            02= Elem_t (0.0);
              theOutMx.ChangeValue (
            03= - (theRight + theLeft) / (theRight - theLeft);

              
            // row 1
              theOutMx.ChangeValue (10= Elem_t (0.0);
              theOutMx.ChangeValue (
            11= Elem_t (2.0/ (theTop - theBottom);
              theOutMx.ChangeValue (
            12= Elem_t (0.0);
              theOutMx.ChangeValue (
            13= - (theTop + theBottom) / (theTop - theBottom);

              
            // row 2
              theOutMx.ChangeValue (20= Elem_t (0.0);
              theOutMx.ChangeValue (
            21= Elem_t (0.0);
              theOutMx.ChangeValue (
            22= Elem_t (-2.0/ (theFar - theNear);
              theOutMx.ChangeValue (
            23= - (theFar + theNear) / (theFar - theNear);

              
            // row 3
              theOutMx.ChangeValue (30= Elem_t (0.0);
              theOutMx.ChangeValue (
            31= Elem_t (0.0);
              theOutMx.ChangeValue (
            32= Elem_t (0.0);
              theOutMx.ChangeValue (
            33= Elem_t (1.0);
            }

            平行投影主要用于設計制圖CAD,而透視投影更有真實感覺,即近大遠小,所以在一些三維瀏覽或仿真程序中主要使用透視投影。當指定投影類型為Projection_Perspective時,即指定為場景的投影變換為透視變換。一般的透視投影規范化變換矩陣如下所示:詳細推導請參考《Computer Graphics with OpenGL》。

            wps_clip_image-19588

            OpenCASCADE中相關的實現代碼如下所示:

            // =======================================================================
            // function : PerspectiveProj
            // purpose  :
            // =======================================================================
            template <typename Elem_t>
            void Graphic3d_Camera::PerspectiveProj (const Elem_t theLeft,
                                                    
            const Elem_t theRight,
                                                    
            const Elem_t theBottom,
                                                    
            const Elem_t theTop,
                                                    
            const Elem_t theNear,
                                                    
            const Elem_t theFar,
                                                    NCollection_Mat4
            <Elem_t>& theOutMx)
            {
              
            // column 0
              theOutMx.ChangeValue (00= (Elem_t (2.0* theNear) / (theRight - theLeft);
              theOutMx.ChangeValue (
            10= Elem_t (0.0);
              theOutMx.ChangeValue (
            20= Elem_t (0.0);
              theOutMx.ChangeValue (
            30= Elem_t (0.0);

              
            // column 1
              theOutMx.ChangeValue (01= Elem_t (0.0);
              theOutMx.ChangeValue (
            11= (Elem_t (2.0* theNear) / (theTop - theBottom);
              theOutMx.ChangeValue (
            21= Elem_t (0.0);
              theOutMx.ChangeValue (
            31= Elem_t (0.0);

              
            // column 2
              theOutMx.ChangeValue (02= (theRight + theLeft) / (theRight - theLeft);
              theOutMx.ChangeValue (
            12= (theTop + theBottom) / (theTop - theBottom);
              theOutMx.ChangeValue (
            22= -(theFar + theNear) / (theFar - theNear);
              theOutMx.ChangeValue (
            32= Elem_t (-1.0);

              
            // column 3
              theOutMx.ChangeValue (03= Elem_t (0.0);
              theOutMx.ChangeValue (
            13= Elem_t (0.0);
              theOutMx.ChangeValue (
            23= -(Elem_t (2.0* theFar * theNear) / (theFar - theNear);
              theOutMx.ChangeValue (
            33= Elem_t (0.0);
            }

            其中投影方式Projection_Stereo為立體投影,在網上搜了一下,這種投影可以應用到3D影片的播放。目前來看,相關應用還是很火爆的,可以用來在電視中播放立體影片。更多介紹可參考:

            http://www.nvidia.com/content/gtc-2010/pdfs/2010_gtc2010.pdf

            http://www.gali-3d.com/archive/articles/StereoOpenGL/StereoscopicOpenGLTutorial.php

            4.Test the Camera in GLUT

            OpenGL實用函數工具包(OpenGL Utility Toolkit, GLUT)提供了與任意屏幕窗口系統進行交互的函數庫,可以用來快速演示OpenGL中的相關概念。其中設置投影變換矩陣的方法如下代碼所示:

            // projection transformation.
            glMatrixMode(GL_PROJECTION);
            glLoadMatrixd(theCamera
            ->ProjectionMatrix().GetData());

            設置模型/視圖變換矩陣代碼如下所示:

            glMatrixMode(GL_MODELVIEW);
            glLoadMatrixd(theCamera
            ->OrientationMatrix().GetData());
            glutPostRedisplay();

            通過鍵盤按鍵o和p用來在平行投影和透視投影之間進行切換,切換效果如下圖所示:

            wps_clip_image-574

            Figure 4.1 Orthographic and Perspective projection test

            如上圖所示,透視投影比平行投影更具立體感,但是在工程設計CAD或機械制圖CAD軟件中,平行投影更有利于設計人員使用。當處于平行投影模式時,視點的遠近對模型在視圖中的顯示沒有影響;當處于透視投影模式時,移動視點對視圖是有影響的:當視點離模型較近時,模型在視圖中顯示較大,反之較小。

            按下鼠標中鍵可以用來測試四元數Quaternion插值來對場景繞Y軸進行旋轉,其實主要用來測試OpenCASCADE的quaternion插值類的插值效果。代碼及效果圖如下所示:

                static gp_Quaternion aStartQuat(gp_Vec(0.01.00.0), 0.0);
                
            static gp_Quaternion aEndQuat(gp_Vec(0.01.00.0), M_PI);
                
            static gp_QuaternionSLerp aSlerp(aStartQuat, aEndQuat);

                gp_Quaternion aResultQuat;

                aSlerp.Interpolate(T, aResultQuat);

            wps_clip_image-8867

            Figure 4.2 Test Quaternion SLERP

            程序源代碼見文后附件,可下載編譯后自己修改一些參數來測試效果。

            5.Conclusion

            OpenGL中的坐標變換涉及到對象空間,模型空間,世界空間,觀察空間及相關的坐標變換,而所有的變換操作都可以統一為一個4X4的齊次矩陣的數學運算,如矩陣加法、矩陣乘法、求逆矩陣等。

            OpenCASCADE用面向對象的思想封裝了Grpahic3d_Camera用來方便對視圖的變換控制,通過設置視點及投影類型等相關參數,即可得到相關的變換矩陣,進而可對模型/視圖方便地進行變換。利用Camera的類,可以方便對視圖進行縮放、旋轉等變換操作。相關實現可參考OpenCASCADE中視圖類中的代碼。

            最后,還測試了一下gp_Quaternion插值的算法。

            6. References

            1. Dave Shreiner. OpenGL Programming Guide(7th Edition). Addison-Wesley. 2010

            2. Randi J. Rost. OpenGL Shading Language(2nd Edition). Addison-Wesley. 2006

            3. Donald Hearn, M. Pauline Baker. Computer Graphics with OpenGL. 2004

            4. Nate Robins. OpenGL Tutors. www.cs.utah.edu/~narobins/opengl.html

            5. Song Ho Ahn. OpenGL Transformation. http://www.songho.ca/opengl/gl_transform.html

            6. 孫家廣等. 計算機圖形學. 清華大學出版社. 2000

             

            PDF Version and Source Code: OpenCASCADE Camera

            99久久精品免费看国产一区二区三区 | 久久综合九色综合欧美就去吻| 国产美女久久久| 久久久久久噜噜精品免费直播| 久久国产精品无| 久久精品国产半推半就| 无夜精品久久久久久| 国产69精品久久久久777| 狠狠人妻久久久久久综合| 久久婷婷五月综合色奶水99啪| 99久久精品毛片免费播放| 久久免费视频1| 国产精品99久久不卡| 亚洲国产精品一区二区久久hs| 精品久久国产一区二区三区香蕉| 欧美日韩精品久久免费| 国产亚洲成人久久| 精品999久久久久久中文字幕| 亚洲欧美久久久久9999| 四虎国产精品免费久久久| 久久ww精品w免费人成| 久久亚洲中文字幕精品一区| segui久久国产精品| 精品久久久久久| 国内精品久久久久伊人av| 久久精品国产免费观看三人同眠| 日韩中文久久| 色99久久久久高潮综合影院 | 久久久久亚洲AV成人网人人网站| 久久久久18| 性高朝久久久久久久久久| 久久男人中文字幕资源站| 久久婷婷五月综合色99啪ak| 国产精品一区二区久久精品无码 | 国产视频久久| 久久毛片免费看一区二区三区| 国产99久久九九精品无码| 久久精品国产99久久香蕉| 久久高清一级毛片| 中文精品99久久国产| 精品久久久无码人妻中文字幕|