#include <osg/Group>
#include <osg/Camera>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osgText/Text>
#include <osg/Camera>
#include <osg/Image>
#include <osg/Geometry>
osg::Camera*createHUD(){
osg::ref_ptr<osg::Camera>camera=new osg::Camera;
//观察矩阵一直是单位矩阵--保证是二维
camera->setViewMatrix(osg::Matrix::identity());
//最后渲染
camera->setRenderOrder(osg::Camera::POST_RENDER);
//在其他相机的基础上渲染
camera->setClearMask(GL_DEPTH_BUFFER_BIT);
//不接受事件
camera->setAllowEventFocus(false);
//不受其他节点影响--设置参考侦
camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
//设置透视矩阵
camera->setProjectionMatrixAsOrtho2D(0, 1366, 0, 900);
//End Camera
osg::ref_ptr<osg::Geode>geode=new osg::Geode;
geode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
osg::ref_ptr<osgText::Text>text=new osgText::Text;
geode->addDrawable(text);
text->setText("Hello HUD");
// x,y,z,xy代表离左下角的位置,x向左,y向上,z类似深度
text->setPosition(osg::Vec3(1.0,100.4,-0.1));
osg::ref_ptr<osg::Geometry>gm=new osg::Geometry;
geode->addDrawable(gm);
//压入顶点
osg::Vec3Array*vertex=new osg::Vec3Array;
vertex->push_back(osg::Vec3(0,0,-0.2));
vertex->push_back(osg::Vec3(100,0,-0.2));
vertex->push_back(osg::Vec3(100,100,-0.2));
vertex->push_back(osg::Vec3(0,100,-0.2));
gm->setVertexArray(vertex);
//法线--其实没必要,因为灯光都关了
osg::Vec3Array*norm=new osg::Vec3Array;
norm->push_back(osg::Vec3(0,0,1));
gm->setNormalArray(norm);
gm->setNormalBinding(osg::Geometry::BIND_OVERALL);
//设置文理
osg::Vec2Array*coord=new osg::Vec2Array;
coord->push_back(osg::Vec2(0.0,0.0));
coord->push_back(osg::Vec2(1.0,0.0));
coord->push_back(osg::Vec2(1.0,1.0));
coord->push_back(osg::Vec2(0.0,1.0));
gm->setTexCoordArray(0, coord);
gm->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4));
//贴纹理
osg::ref_ptr<osg::Image>image=osgDB::readImageFile("Cubemap_snow/posz.jpg");
osg::Texture2D *t2d=new osg::Texture2D;
t2d->setImage(0, image);
gm->getOrCreateStateSet()->setTextureAttributeAndModes(0, t2d,osg::StateAttribute::ON);
gm->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
camera->addChild(geode.get());
return camera.release();
}
int main(int argc, char *argv[])
{
osg::ref_ptr<osgViewer::Viewer>viewer=new osgViewer::Viewer;
osg::ref_ptr<osg::Group>root=new osg::Group;
root->addChild(osgDB::readNodeFile("cow.osg"));
root->addChild(createHUD());
viewer->setSceneData(root.get());
return viewer->run();
}
osg 文理贴图 HUD相关
最新推荐文章于 2023-05-26 15:23:04 发布