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
在三維場景中變換的過程與用相機拍照的過程很相似,如下圖所示,拍照的過程大概如下:
Figure 1.1 The Camera Analogy (From the Red Book)
v 放好三角架并使相機對準(zhǔn)要拍照的場景(viewing transformation);
v 布置好場景到理想的位置,以便拍照(modeling transformation);
v 選擇鏡頭或調(diào)整變焦(projection transformation);
v 確定最終的照片要有多大尺寸,例如你可能需要放大一些的照片(viewport transformation);
OpenCASCADE在6.8.0版本中引入了一個Graphic3d_Camera類來對視圖的變換進(jìn)行管理,通過對camera的調(diào)整,方便對場景中的模型/視圖(Model/View)進(jìn)行變換。
Figure 1.2 Camera in the Scene
如下圖所示,對象的空間坐標(biāo)經(jīng)過一系列的變換,最終得到顯示屏幕上的二維坐標(biāo)。
Figure 1.3 Stages of Vertex Transformation (From the Red Book)
為了指定viewing, modeling和projection變換,可以構(gòu)造一個4x4的矩陣與頂點的齊次坐標(biāo)相乘來實現(xiàn)這些變換。
2.Coordinate Transforms
在計算機圖形學(xué)中,造型MODELING就是定義一個可渲染物體的數(shù)字表示。對于OpenGL而言,通常就是用多邊形來近似表示一個物體。用多邊形表示物體至少需要每個多邊形頂點的坐標(biāo)和連接關(guān)系的信息。附加的信息包含頂點顏色、法向量或貼圖坐標(biāo)等。
三維對象的屬性如頂點位置及面的法向量,為了造型的方便,通常定義在對象空間OBJECT SPACE中,即在局部坐標(biāo)系中建模。對象空間通常也叫作模型空間MODEL SPACE或造型坐標(biāo)系統(tǒng)MODELING COORDINATE SYSTEM。
在一個包含大量三維對象的場景中,每一個對象都是在其對象空間中建模的,這是我們就需要一個統(tǒng)一的坐標(biāo)系統(tǒng)COMMON COORDINATE SYSTEM,通常稱之為世界空間WORLD SPACE或世界坐標(biāo)系WORLD COORDINATE SYSTEM,或絕對坐標(biāo)系。
定義了世界坐標(biāo)系統(tǒng)后,場景中所有模型對象必須從其局部坐標(biāo)系變換到世界坐標(biāo)系中來。從對象空間變換到世界空間的變換稱之為模型變換MODELING TRANSFORMATION。若模型對象造型坐標(biāo)系中的單位是feet,但是世界坐標(biāo)系中是inches,對象坐標(biāo)必須縮小12倍以得到世界坐標(biāo)系。若對象在其對象空間中朝前的,但是在世界場景中需要其朝后,那么需要對模型對象進(jìn)行一個旋轉(zhuǎn)變換。當(dāng)要將模型對象放到場景中的合適位置時,那么就需要進(jìn)行一個移動變換。所有這些單個變換也可以合成一個矩陣,即模型變換矩陣MODEL TRANSFORMATION MATRIX,就是表示將模型對象從對象坐標(biāo)系變換到世界坐標(biāo)系中。
當(dāng)場景確定好后,視圖viewing parameters的參數(shù)就需要確定了。視圖的參數(shù)之一就是視點vantage point(eye or camera position),即在哪里對模型進(jìn)行觀察。視圖的參數(shù)還包括焦點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)。視圖的參數(shù)定義了視圖變換VIEWING TRANSFORMATION,它們也可以合成一個矩陣,稱作視圖矩陣VIEWING MATRIX。一個坐標(biāo)乘以這個矩陣就可以將其從世界空間變換到觀察空間EYE SPACE,也稱為EYE COORDINATE SYSTEM。由定義可知,此坐標(biāo)系的原點位于視點處。
盡管有些3D圖形API允許分別指定模型變換矩陣modeling matrix和視圖變換矩陣viewing matrix,但是在OpenGL中將這兩者合成為一個矩陣,稱為模型/視圖矩陣MODELVIEW MATRIX。這個矩陣定義了坐標(biāo)從對象空間變換到觀察空間的變換。
Figure 2.1 Coordinate spaces and transforms in OpenGL(From OpenGL Shading Language)
在OpenGL中,可以調(diào)用函數(shù)glMatrixMode來選擇modelview matrix或其他的矩陣,然后可以調(diào)用函數(shù)glLoadIdentity將當(dāng)前矩陣設(shè)置為單位矩陣,或調(diào)用函數(shù)glLoadMatrix將當(dāng)前矩陣替換為任意一個矩陣。替換時你需要清楚地知道你要進(jìn)行的變換操作,否則容易產(chǎn)生完全不可理解的視圖。還可以調(diào)用函數(shù)glMultMatrix來將當(dāng)前矩陣與任意矩陣相乘。可以通過調(diào)用函數(shù)gluLookAt來設(shè)置modelview變換矩陣。
當(dāng)坐標(biāo)變換到觀察空間EYE SPACE后,下一步需要定義一個觀察體VIEWING VOLUME,即這個區(qū)域內(nèi)的三維場景將出現(xiàn)在最終的圖像上。將模型對象放進(jìn)觀察體即裁剪空間CLIP SPACE(也稱作裁剪坐標(biāo)系CLIPPING COORDINATE SYSTEM)的變換稱為投影變換PROJECTION TRANSFORMATION。在OpenGL中,可以通過glMatrixModel來選擇需要設(shè)置的變換矩陣。也可以調(diào)用函數(shù)glOrtho、glFrustum、gluPerspective來設(shè)置。
通過投影變換將觀察空間內(nèi)的三維模型投影到了二維空間,下一步變換就是頂點坐標(biāo)的perspective divide。這個操作將觀察體內(nèi)所有坐標(biāo)點的各個分量除以齊次坐標(biāo)分量w。得到的x, y, z的取值范圍都是[-1, 1],而齊次坐標(biāo)值w為1,所以就不再需要w了。換句話說所有可見的圖元graphics primitives都變換到一個在點(-1,-1,-1)和點(1,1,1)之間的立方體區(qū)域,這就是規(guī)范化設(shè)備坐標(biāo)空間NORMALIZED DEVICE COORDINATE SPACE,他是個允許將觀察區(qū)域viewing area映射到任意尺寸及深度的視口的中間空間。
窗口顯示的像素點pixel不能指向-1到1的浮點數(shù),通常指向窗口坐標(biāo)系WINDOW COORDINATE SYSTEM中的坐標(biāo),x取值范圍從0到窗口寬度減1,y取值范圍從0到窗口高度減1,因此還需要進(jìn)行一步變換處理。視口變換指定了從規(guī)范設(shè)備坐標(biāo)系到窗口坐標(biāo)系的變換VIEWPORT TRANSFORMATION。在OpenGL通過函數(shù)glViewport來指定視口變換。
以上內(nèi)容主要來自《OpenGL Shading Language》一書中的第一章“Review of OpenGL Basics-1.9 Coordinate Transforms”,詳細(xì)內(nèi)容可參考原書。也可利用下面的程序來直觀地學(xué)習(xí)這些變換及投影的知識:
Figure 2.2 OpenGL Tutors-Projection (by Nate Robins)
3.Camera of OpenCASCADE
OpenCASCADE6.8.0版本中引入一個類Graphic3d_Camera來簡化對三維視圖的變換操作,即可以對視圖進(jìn)行投影和模型視圖的相關(guān)設(shè)置。如通過設(shè)置視點Eye position和中心點可以計算得到觀察矩陣,類似gluLookAt的功能,相關(guān)代碼如下所示:
// =======================================================================
// 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
}
通過調(diào)用這個函數(shù)得到將世界坐標(biāo)系變換到觀察坐標(biāo)系的變換矩陣。其實現(xiàn)步驟如下:
v 平移觀察坐標(biāo)原點到世界坐標(biāo)系原點;
v 進(jìn)行旋轉(zhuǎn),分別讓觀察坐標(biāo)系的Vx, Vy, Vz軸對應(yīng)到世界坐標(biāo)的Wx,Wy,Wz軸。
OpenCASCADE中的實現(xiàn)代碼如下所示:
// =======================================================================
// 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 (0, 0) = theAxialScale.x();
anAxialScaleMx.ChangeValue (1, 1) = theAxialScale.y();
anAxialScaleMx.ChangeValue (2, 2) = theAxialScale.z();
theOutMx.Multiply (anAxialScaleMx);
}
當(dāng)指定投影變換類型后,可以得到投影變換矩陣,可以指定的類型如下:
//! 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
};
當(dāng)指定為Projection_Orthographic時即為正投影變換。使用從坐標(biāo)位置到觀察平面的正投影變換,任意一點(x, y, z)的投影位置是(x, y)。因此在建立觀察體的范圍后,該矩形平行管道內(nèi)部的坐標(biāo)描述即為投影坐標(biāo),它們不需要另外的投影處理就可直接映射到規(guī)范化觀察體normalized view volume。有些圖形軟件包使用單位立方體作為規(guī)范化觀察體,其x,y,z坐標(biāo)規(guī)范成0到1之間,另外的規(guī)范化變換方法使用坐標(biāo)范圍從-1到1的對稱立方體。
由于屏幕坐標(biāo)系經(jīng)常指定為左手系,因此規(guī)范化觀察體也常指定為左手系統(tǒng)。正投影觀察體的規(guī)范化變換矩陣是:詳細(xì)推導(dǎo)請參考《Computer Graphics with OpenGL》。

對應(yīng)OpenCASCADE中實現(xiàn)代碼如下所示:
// =======================================================================
// 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 (0, 0) = Elem_t (2.0) / (theRight - theLeft);
theOutMx.ChangeValue (0, 1) = Elem_t (0.0);
theOutMx.ChangeValue (0, 2) = Elem_t (0.0);
theOutMx.ChangeValue (0, 3) = - (theRight + theLeft) / (theRight - theLeft);
// row 1
theOutMx.ChangeValue (1, 0) = Elem_t (0.0);
theOutMx.ChangeValue (1, 1) = Elem_t (2.0) / (theTop - theBottom);
theOutMx.ChangeValue (1, 2) = Elem_t (0.0);
theOutMx.ChangeValue (1, 3) = - (theTop + theBottom) / (theTop - theBottom);
// row 2
theOutMx.ChangeValue (2, 0) = Elem_t (0.0);
theOutMx.ChangeValue (2, 1) = Elem_t (0.0);
theOutMx.ChangeValue (2, 2) = Elem_t (-2.0) / (theFar - theNear);
theOutMx.ChangeValue (2, 3) = - (theFar + theNear) / (theFar - theNear);
// row 3
theOutMx.ChangeValue (3, 0) = Elem_t (0.0);
theOutMx.ChangeValue (3, 1) = Elem_t (0.0);
theOutMx.ChangeValue (3, 2) = Elem_t (0.0);
theOutMx.ChangeValue (3, 3) = Elem_t (1.0);
}
平行投影主要用于設(shè)計制圖CAD,而透視投影更有真實感覺,即近大遠(yuǎn)小,所以在一些三維瀏覽或仿真程序中主要使用透視投影。當(dāng)指定投影類型為Projection_Perspective時,即指定為場景的投影變換為透視變換。一般的透視投影規(guī)范化變換矩陣如下所示:詳細(xì)推導(dǎo)請參考《Computer Graphics with OpenGL》。
OpenCASCADE中相關(guān)的實現(xiàn)代碼如下所示:
// =======================================================================
// 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 (0, 0) = (Elem_t (2.0) * theNear) / (theRight - theLeft);
theOutMx.ChangeValue (1, 0) = Elem_t (0.0);
theOutMx.ChangeValue (2, 0) = Elem_t (0.0);
theOutMx.ChangeValue (3, 0) = Elem_t (0.0);
// column 1
theOutMx.ChangeValue (0, 1) = Elem_t (0.0);
theOutMx.ChangeValue (1, 1) = (Elem_t (2.0) * theNear) / (theTop - theBottom);
theOutMx.ChangeValue (2, 1) = Elem_t (0.0);
theOutMx.ChangeValue (3, 1) = Elem_t (0.0);
// column 2
theOutMx.ChangeValue (0, 2) = (theRight + theLeft) / (theRight - theLeft);
theOutMx.ChangeValue (1, 2) = (theTop + theBottom) / (theTop - theBottom);
theOutMx.ChangeValue (2, 2) = -(theFar + theNear) / (theFar - theNear);
theOutMx.ChangeValue (3, 2) = Elem_t (-1.0);
// column 3
theOutMx.ChangeValue (0, 3) = Elem_t (0.0);
theOutMx.ChangeValue (1, 3) = Elem_t (0.0);
theOutMx.ChangeValue (2, 3) = -(Elem_t (2.0) * theFar * theNear) / (theFar - theNear);
theOutMx.ChangeValue (3, 3) = Elem_t (0.0);
}
其中投影方式Projection_Stereo為立體投影,在網(wǎng)上搜了一下,這種投影可以應(yīng)用到3D影片的播放。目前來看,相關(guān)應(yīng)用還是很火爆的,可以用來在電視中播放立體影片。更多介紹可參考:
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實用函數(shù)工具包(OpenGL Utility Toolkit, GLUT)提供了與任意屏幕窗口系統(tǒng)進(jìn)行交互的函數(shù)庫,可以用來快速演示OpenGL中的相關(guān)概念。其中設(shè)置投影變換矩陣的方法如下代碼所示:
// projection transformation.
glMatrixMode(GL_PROJECTION);
glLoadMatrixd(theCamera->ProjectionMatrix().GetData());
設(shè)置模型/視圖變換矩陣代碼如下所示:
glMatrixMode(GL_MODELVIEW);
glLoadMatrixd(theCamera->OrientationMatrix().GetData());
glutPostRedisplay();
通過鍵盤按鍵o和p用來在平行投影和透視投影之間進(jìn)行切換,切換效果如下圖所示:
Figure 4.1 Orthographic and Perspective projection test
如上圖所示,透視投影比平行投影更具立體感,但是在工程設(shè)計CAD或機械制圖CAD軟件中,平行投影更有利于設(shè)計人員使用。當(dāng)處于平行投影模式時,視點的遠(yuǎn)近對模型在視圖中的顯示沒有影響;當(dāng)處于透視投影模式時,移動視點對視圖是有影響的:當(dāng)視點離模型較近時,模型在視圖中顯示較大,反之較小。
按下鼠標(biāo)中鍵可以用來測試四元數(shù)Quaternion插值來對場景繞Y軸進(jìn)行旋轉(zhuǎn),其實主要用來測試OpenCASCADE的quaternion插值類的插值效果。代碼及效果圖如下所示:
static gp_Quaternion aStartQuat(gp_Vec(0.0, 1.0, 0.0), 0.0);
static gp_Quaternion aEndQuat(gp_Vec(0.0, 1.0, 0.0), M_PI);
static gp_QuaternionSLerp aSlerp(aStartQuat, aEndQuat);
gp_Quaternion aResultQuat;
aSlerp.Interpolate(T, aResultQuat);
Figure 4.2 Test Quaternion SLERP
程序源代碼見文后附件,可下載編譯后自己修改一些參數(shù)來測試效果。
5.Conclusion
OpenGL中的坐標(biāo)變換涉及到對象空間,模型空間,世界空間,觀察空間及相關(guān)的坐標(biāo)變換,而所有的變換操作都可以統(tǒng)一為一個4X4的齊次矩陣的數(shù)學(xué)運算,如矩陣加法、矩陣乘法、求逆矩陣等。
OpenCASCADE用面向?qū)ο蟮乃枷敕庋b了Grpahic3d_Camera用來方便對視圖的變換控制,通過設(shè)置視點及投影類型等相關(guān)參數(shù),即可得到相關(guān)的變換矩陣,進(jìn)而可對模型/視圖方便地進(jìn)行變換。利用Camera的類,可以方便對視圖進(jìn)行縮放、旋轉(zhuǎn)等變換操作。相關(guān)實現(xiàn)可參考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. 孫家廣等. 計算機圖形學(xué). 清華大學(xué)出版社. 2000