• lbbe
    了解作者
  • C/C++
    开发工具
  • 1KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 0
    下载次数
  • 2019-07-04 14:54
    上传日期
可以实现osg的图片朝向屏幕显示,固定大小
OsgIconAuto.zip
  • OsgIconAuto.h
    377B
  • OsgIconAuto.cpp
    4.7KB
内容介绍
 #include "OsgIconAuto.h" #include "Common.h" #include <osg/Geode> #include <osg/Geometry> #include <osg/Billboard> #include <osg/Texture2D> #include <osgDB/ReadFile> OsgIconAuto::OsgIconAuto(const osg::Vec3 &bottomcenter, std::string &imgurl,float imgWidth): osg::AutoTransform(), _imgWidth(imgWidth) { osg::ref_ptr<osg::Image> img = osgDB::readImageFile(imgurl); if(img.valid()) { double scale = 2.0/ img->s();//固定宽度2米 double s = scale * img->s(); double t = scale * img->t(); //osg::Geode * geode = new osg::Geode(); osg::Billboard * geode = new osg::Billboard(); geode->setMode(osg::Billboard::POINT_ROT_EYE); geode->addDrawable( createSquare(osg::Vec3(0,0,0),(float)s,(float)t)); geode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON); geode->getOrCreateStateSet()->setRenderingHint( osg::StateSet::TRANSPARENT_BIN); const char vertexShader[] = "#version " GLSL_VERSION_STR "\n" GLSL_DEFAULT_PRECISION_FLOAT "\n" "varying vec2 TexCoord0; \n" "void main() { \n" " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; \n" " TexCoord0 = gl_MultiTexCoord0.xy; \n" "} \n"; const char fragmentShader[] = "#version " GLSL_VERSION_STR "\n" GLSL_DEFAULT_PRECISION_FLOAT "\n" "varying vec2 TexCoord0; \n" "uniform sampler2D mytexture; \n" "void main() { \n" " gl_FragColor = texture2D(mytexture,TexCoord0); \n" "} \n"; osg::ref_ptr<osg::Shader> tvshader = new osg::Shader(osg::Shader::VERTEX, vertexShader); osg::ref_ptr<osg::Shader> tfshader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShader); osg::ref_ptr<osg::Program> tprog = new osg::Program; tprog->addShader(tvshader); tprog->addShader(tfshader); //set Texture osg::Texture2D* tex = new osg::Texture2D(img); tex->setDataVariance(osg::Object::DYNAMIC); geode->getOrCreateStateSet()->setTextureAttributeAndModes(0, tex); osg::ref_ptr<osg::Uniform> tmyTexture = new osg::Uniform("mytexture", 0); geode->getOrCreateStateSet()->addUniform(tmyTexture); geode->getOrCreateStateSet()->setAttributeAndModes(tprog, osg::StateAttribute::ON | osg::StateAttribute::PROTECTED); geode->getOrCreateStateSet()->setAttribute(tprog); // this->addChild(geode); this->setAutoScaleToScreen(true); //当相机位置与at距离介于minScale和maxScale之间时,at会自动的进行缩放,以维持固定的大小,但是如果相机和at之间距离超出该范围,该放大放大,该缩小缩小 this->setMinimumScale(0.0000001); this->setMaximumScale(10000); this->setPosition(bottomcenter); } } osg::Drawable *OsgIconAuto::createSquare(const osg::Vec3 &bottomcenter, float width, float height) { // set up the Geometry. osg::Geometry* geom = new osg::Geometry; float widthhalf = width*0.5; osg::Vec3Array* coords = new osg::Vec3Array(4); (*coords)[0] = bottomcenter+osg::Vec3(-widthhalf,0.0f,height); (*coords)[1] = bottomcenter+osg::Vec3(widthhalf,0.0f,height); (*coords)[2] = bottomcenter+osg::Vec3(widthhalf,0.0f,0.0f); (*coords)[3] = bottomcenter+osg::Vec3(-widthhalf,0.0f,0.0f); geom->setVertexArray(coords); osg::Vec3Array* norms = new osg::Vec3Array(1); (*norms)[0] = osg::Vec3(0.0f,-1.0f,0.0f); //(*norms)[0].normalize(); geom->setNormalArray(norms, osg::Array::BIND_OVERALL); osg::Vec2Array* tcoords = new osg::Vec2Array(4); (*tcoords)[0].set(0.0f,1.0f); (*tcoords)[1].set(1.0f,1.0f); (*tcoords)[2].set(1.0f,0.0f); (*tcoords)[3].set(0.0f,0.0f); geom->setTexCoordArray(0,tcoords); geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4)); return geom; }
评论
    相关推荐
    • 2.rar
      这是OSG入门指南源码之二,内容有布告板示例,自动变换节点等12项。
    • osg.rar
      基于OSG和粒子系统的雪效仿真,OSG学习资料
    • Billboard.rar
      基于vs2012的OSG入门程序源码,主要实现布告板示例功能。
    • E_3_1_Billboard.rar
      一个osg布告板示例,显示树木,程序中会使用到如何绘制几何体,节点以及纹理贴图
    • 3D机房.zip
      不多说,上干货 ! 风靡的3D机房具体实现,目前主要提供有以下管理功能:设备资产管理、机房空间统计、机房功耗统计、机房载重统计、场景可视化温度云、机房高级搜索、机房设备空间搜索、机房动环监控、机房链路管理。
    • 720度万能全景开发包flash+AS3+3d+源文件+源代码.rar
      flash全景开源代码,只要换下图片就是自己的作品!! flash全景开源代码,flash3D学习案例,pv3d很好的例子
    • Gait-Tracking-With-x-IMU-master.zip
      x-IMU matlab zupt惯性室内定位算法
    • 上汽软件大赛提交作品.rar
      小车在制定道路上自动驾驶,自动识别车道线等标识。
    • 倒车入库.rar
      主要用来仿真侧方位停车过程中的运动轨迹,为自动泊车做理论基础。
    • 雨流计数法.rar
      可通过matlab 实现雨滴计数法,可直接调用