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

因此在osg/State 中 在使用applyModelViewMatrix() osg/State 是OpenGL狀態(tài)機(jī)的封裝.是與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)進(jìn)行載入矩陣的.因此它的轉(zhuǎn)置剛好適應(yīng)了OpenGL列優(yōu)先的模式.
你只需牢記 在OSG當(dāng)中變換的步驟則是左乘操作(全局坐標(biāo)系)..即左邊的是先執(zhí)行變換的.
談完如上的那些之后.我們現(xiàn)在來看MatrixTransform 和PositionAttitudeTransform.
參考坐標(biāo)系有三種
RELATIVE_RF, //全局 相對 ()
ABSOLUTE_RF, //局部 絕對 ()
ABSOLUTE_RF_INHERIT_VIEWPOINT //基于視點一個局部坐標(biāo)系.很少用到.
MatrixTransform 故名 矩陣變換節(jié)點.在位于它的節(jié)點之下的節(jié)點都將按照這它的矩陣變換來進(jìn)行模型變換操作.因此 MatrixTransform的主要功能之一就是提供模型變換操作..你只要根據(jù)你所需要的設(shè)置其模型矩陣就行.
即執(zhí)行 setMatrix() 因此.查看計算當(dāng)前World矩陣的方法就可以很清晰的明白其最后結(jié)果就是:
如果是相對的.那么 先執(zhí)行自身的變換,在執(zhí)行父節(jié)點的變換操作.這類似OpenGL中后寫的變換是先執(zhí)行的一樣(全局坐標(biāo)系考慮)
否則是絕對的,那么當(dāng)前矩陣就是自身矩陣

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


{
if (_referenceFrame==RELATIVE_RF)

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

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

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的一個特例.,操作順序我就不在詳談了.因為它的功能就是為了實現(xiàn)姿態(tài)和位置.還有縮放等.
因此 setPivotPoint 將使得之后的變換將會基于此平移操作執(zhí)行.
那么實現(xiàn)一個例子來觀察他們的效果: 下面的例子則是
<1> 使用PositionAttitudeTransform 設(shè)置左邊的牛. 位置位于(-10,0,0) 姿態(tài)為繞z軸旋轉(zhuǎn)90度.
<2> 使用MatrixTransform 設(shè)置右邊的牛, 先執(zhí)行平移操作(10,0,0) 在繞z軸旋轉(zhuǎn)60度 因此使得這只牛偏屏幕內(nèi)部一點.(你畫個坐標(biāo)軸 按全局思路演示 則就知道牛在上面位置了.!)

例子
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
米游 閱讀(6859)
評論(0) 編輯 收藏 引用 所屬分類:
OpenGL/OSG