osg 文理贴图 HUD相关

#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();
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值