OSG中的HUD
所謂HUD節點,說白了就是無論三維場景中的內容怎么改變,它都能在屏幕上固定位置顯示的節點。
實現要點:
- 關閉光照,不受場景光照影響,所有內容以同一亮度顯示
- 關閉深度測試
- 調整渲染順序,使它的內容最后繪制
- 設定參考貼為絕對型:setReferenceFrame(osg::Transform:ABSOLUTE_RF)
- 使其不受父節點變換的影響:setMatrix(osg::Matrix::identity())
- 使用平行投影,設定虛擬投影窗口的大小,這個窗口的大小決定了后面繪制的圖形和文字的尺度比例
示例代碼:
//
創建
HUD
攝像機
osg
::
ref_ptr
<
osg
::
Camera
>
camera
=
new
osg
::
Camera
;
camera
->
setProjectionMatrix
(
osg
::
Matrix
::
ortho2D
(0, 1000, 0, 1000));
//
表示攝像機里的平面世界有多大
camera
->
setReferenceFrame
(
osg
::
Transform
::
ABSOLUTE_RF
);
camera
->
setViewMatrix
(
osg
::
Matrix
::
identity
());
camera
->
setClearMask
(
GL_DEPTH_BUFFER_BIT
);
camera
->
setAllowEventFocus
(
false
);
camera
->
setRenderOrder
(
osg
::
Camera
::
POST_RENDER
);
//
創建提示對象
m_prompt
=
new
osgText
::
Text
;
QFont
f
(
"
宋體
"
);
osgText
::
Font
*
font
=
new
osgText
::
Font
(
new
osgQt
::
QFontImplementation
(
f
));
m_prompt
->
setFont
(
font
);
m_prompt
->
setCharacterSize
(16);
m_prompt
->
setPosition
(
osg
::
Vec3
(0.0f, 10.0f, 0.0f));
m_prompt
->
setColor
(
osg
::
Vec4
(1, 1, 1, 1));
m_prompt
->
setDataVariance
(
osg
::
Object
::
DYNAMIC
);
m_prompt
->
setText
(
_conv
(
"
坐標
"
),
osgText
::
String
::
ENCODING_UTF8
);
osg
::
ref_ptr
<
osg
::
Geode
>
geode
=
new
osg
::
Geode
;
geode
->
addDrawable
(
m_prompt
);
osg
::
ref_ptr
<
osg
::
StateSet
>
stateSet
=
geode
->
getOrCreateStateSet
();
stateSet
->
setMode
(
GL_LIGHTING
,
osg
::
StateAttribute
::
OFF
);
stateSet
->
setMode
(
GL_DEPTH_TEST
,
osg
::
StateAttribute
::
OFF
);
stateSet
->
setMode
(
GL_BLEND
,
osg
::
StateAttribute
::
ON
);
camera
->
addChild
(
geode
);
// 添加到場景里
viewer->getSceneData()->asGroup()->addChild(camera);