一顆場景樹,遠不止只有葉子節點Geode 因此.在osg中有著一些各種各樣的組節點.osg::Group.還有許多從osg::Group 繼承的其他各種各樣的組節點.類太多了.
附類繼承圖地址 我目前只是嘗試使用了MatrixTransform PositionAttitudeTransform Group Projection Switch 等各種功能的組節點. 在介紹MatrixTransform 和 PositionAttitudeTransform 節點前.我想先介紹下osg當中矩陣的一些相關知識.
其實,無需關心osg矩陣是如何實現的.但是要記住的是osg中采用的左乘操作,我們平時里討論的操作如旋轉平移等.
OpenGL的: newpos = R* T * oldpos //先執行平移 后執行 旋轉 (全局坐標系)
osg當中: newpos =oldpos * T *R //先執行平移 后執行旋轉 (全局坐標系)
因為在osg當中矩陣執行的操作則是行矩陣操作.因此為了跟OpenGL的列矩陣做同步 而不在定義的時候就相當于做了一個轉置操作.即

因此在osg/State 中 在使用applyModelViewMatrix() osg/State 是OpenGL狀態機的封裝.是與OpenGL交互的類

Matrix
inline void applyModelViewMatrix(const osg::RefMatrix* matrix)

{
if (_modelView!=matrix)

{
if (matrix)

{
_modelView=matrix;
glLoadMatrix(matrix->ptr());
}
else

{
_modelView=_identity;
glLoadIdentity();
}
}
}
它是直接使用glLoadMatrix(matrix)進行載入矩陣的.因此它的轉置剛好適應了OpenGL列優先的模式.
你只需牢記 在OSG當中變換的步驟則是左乘操作(全局坐標系)..即左邊的是先執行變換的.
談完如上的那些之后.我們現在來看MatrixTransform 和PositionAttitudeTransform.
參考坐標系有三種
RELATIVE_RF, //全局 相對 ()
ABSOLUTE_RF, //局部 絕對 ()
ABSOLUTE_RF_INHERIT_VIEWPOINT //基于視點一個局部坐標系.很少用到.
MatrixTransform 故名 矩陣變換節點.在位于它的節點之下的節點都將按照這它的矩陣變換來進行模型變換操作.因此 MatrixTransform的主要功能之一就是提供模型變換操作..你只要根據你所需要的設置其模型矩陣就行.
即執行 setMatrix() 因此.查看計算當前World矩陣的方法就可以很清晰的明白其最后結果就是:
如果是相對的.那么 先執行自身的變換,在執行父節點的變換操作.這類似OpenGL中后寫的變換是先執行的一樣(全局坐標系考慮)
否則是絕對的,那么當前矩陣就是自身矩陣

MatrixTransform
bool MatrixTransform::computeLocalToWorldMatrix(Matrix& matrix,NodeVisitor*) const


{
if (_referenceFrame==RELATIVE_RF)

{
matrix.preMult(_matrix);
}
else // absolute

{
matrix = _matrix;
}
return true;
}
PositionAttibuteTransform 就是位置姿態節點..即我們只可以調整該節點的所有孩子的位置以及姿態.,我們也先來看一下它的矩陣變換:

Positon
bool PositionAttitudeTransform::computeLocalToWorldMatrix(Matrix& matrix,NodeVisitor*) const


{
if (_referenceFrame==RELATIVE_RF)

{
matrix.preMultTranslate(_position);
matrix.preMultRotate(_attitude);
matrix.preMultScale(_scale);
matrix.preMultTranslate(-_pivotPoint);
}
else // absolute

{
matrix.makeRotate(_attitude);
matrix.postMultTranslate(_position);
matrix.preMultScale(_scale);
matrix.preMultTranslate(-_pivotPoint);
}
return true;
}
PositionAttibuteTransform 的矩陣變換順序的固定的.可以說是MatrixTransform的一個特例.,操作順序我就不在詳談了.因為它的功能就是為了實現姿態和位置.還有縮放等.
因此 setPivotPoint 將使得之后的變換將會基于此平移操作執行.
那么實現一個例子來觀察他們的效果: 下面的例子則是
<1> 使用PositionAttitudeTransform 設置左邊的牛. 位置位于(-10,0,0) 姿態為繞z軸旋轉90度.
<2> 使用MatrixTransform 設置右邊的牛, 先執行平移操作(10,0,0) 在繞z軸旋轉60度 因此使得這只牛偏屏幕內部一點.(你畫個坐標軸 按全局思路演示 則就知道牛在上面位置了.!)

例子
osg::Node* createTrans()


{

/**//** declare a root node*/
osg::Group* root = new osg::Group;

/**//** declare a Position Node*/
osg::PositionAttitudeTransform* posCow = new osg::PositionAttitudeTransform;
root->addChild(posCow);

/**//** declare a Matrix Node*/
osg::MatrixTransform* matrixCow = new osg::MatrixTransform;
root->addChild(matrixCow);

osg::Node* cow = osgDB::readNodeFile("cow.osg");


/**//**
When use Position Node and the ReferenceFrame is RELATIVE_RF
the matrix is Compute Trans(-pivot) * scale * Rotate * Trans(Pos)
here the pivot and scale is default,so it means that make rotate firstly.
*/
posCow->addChild(cow);
osg::Quat quat;
quat.makeRotate(osg::PI_2,osg::Vec3(0.0,0.0,1.0));
posCow->setAttitude(quat);
posCow->setPosition(osg::Vec3(-10,0.0,0.0));


/**//**
when use Matrix Node you can set the matrix what you want.
here , it make trans firstly and then make rotate.
*/
matrixCow->addChild(cow);
quat.makeRotate(osg::DegreesToRadians(60.0),osg::Vec3(0.0,0.0,1.0));
matrixCow->setMatrix(osg::Matrixd::translate(osg::Vec3(10.0,0.0,0.0))*osg::Matrixd::rotate(quat));

return root;
}
例子下載地址 :
源代碼下載
posted on 2009-08-24 17:29
米游 閱讀(6857)
評論(0) 編輯 收藏 引用 所屬分類:
OpenGL/OSG