查看: 1980|回复: 2

从bmp中读取高度图 ,做地形

[复制链接]

该用户从未签到

发表于 2010-6-4 22:42:37 | 显示全部楼层 |阅读模式
///////
osg::image  获取像素的使用
/////////////
#include <osg/AlphaFunc>
#include <osg/Billboard>
#include <osg/BlendFunc>
#include <osg/Depth>
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/Material>
#include <osg/Math>
#include <osg/MatrixTransform>
#include <osg/PolygonOffset>
#include <osg/Projection>
#include <osg/ShapeDrawable>
#include <osg/StateSet>
#include <osg/Switch>
#include <osg/Texture2D>
#include <osg/TexEnv>
#include <osgDB/ReadFile>
#include <osgDB/FileUtils>
#include <osgUtil/LineSegmentIntersector>
#include <osgUtil/IntersectionVisitor>
#include <osgUtil/SmoothingVisitor>
#include <osgText/Text>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/StateSetManipulator>
#include <iostream>
#pragma comment(lib, "osgd.lib")
#pragma comment(lib, "osgDBd.lib")
#pragma comment(lib, "OpenThreadsd.lib")
#pragma comment(lib, "osgAnimationd.lib")
#pragma comment(lib, "osgFXd.lib")
#pragma comment(lib, "osgGAd.lib")
#pragma comment(lib, "osgManipulatord.lib")
#pragma comment(lib, "osgParticled.lib")
#pragma comment(lib, "osgShadowd.lib")
#pragma comment(lib, "osgSimd.lib")
#pragma comment(lib, "osgTerraind.lib")
#pragma comment(lib, "osgTextd.lib")
#pragma comment(lib, "osgUtild.lib")
#pragma comment(lib, "osgViewerd.lib")
#pragma comment(lib, "osgVolumed.lib")
#pragma comment(lib, "osgWidgetd.lib")
osg::Geode* createTerrain(const osg::Vec3& origin)
{
osg::Geode* geode = new osg::Geode();

osg::ref_ptr<osg::Image>  iimage = osgDB::readImageFile("height128.bmp");

unsigned int numColumns = iimage->s();    //列
unsigned int numRows = iimage ->t();     //行
unsigned int r;
unsigned int c;
osg::HeightField* grid = new osg::HeightField;
grid->allocate(numColumns, numRows);
grid->setOrigin(origin);
grid->setXInterval(10.0f);
grid->setYInterval(10.0f);
osg::Vec4 temp;
for(c=0; c<numColumns; ++c)
{
  for(r=0; r<numRows; ++r)
  {
   temp = iimage->getColor(c,r);
   grid->setHeight(c, r, temp.r()*255.0);  //(float)lp[c*numRows+r]
  }
}
geode->addDrawable(new osg::ShapeDrawable(grid));
osg::StateSet* stateset = new osg::StateSet();
osg::Image* image = osgDB::readImageFile("Images/lz.rgb");
if (image)
{
  osg::Texture2D* texture = new osg::Texture2D;
  texture->setImage(image);
  stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
}
osg:olygonMode*  pm = new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK,
              osg::PolygonMode:INE);
//强制使用线框渲染。
stateset->setAttributeAndModes(pm,
        osg::StateAttribute::ON |osg::StateAttribute::OVERRIDE);
geode->setStateSet( stateset );
return geode;
}

int main( int argc, char **argv )
{
// use an ArgumentParser object to manage the program arguments.
osg::ArgumentParser arguments(&argc,argv);
// construct the viewer.
osgViewer::Viewer viewer(arguments);
// add the stats handler
viewer.addEventHandler(new osgViewer::StatsHandler);
// add model to viewer.
osg::Vec3  center(0.0, 0.0, 0.0);
viewer.setSceneData( createTerrain(center));

return viewer.run();
}
地形.JPG

该用户从未签到

发表于 2010-12-3 10:04:37 | 显示全部楼层
1 grid->setHeight(c, r, temp.r()*255.0);  是否只取了红波段?
2 直接运行程序,给一个2000×2000左右的bmp做地形,直接崩溃了,提示大概是访问了已经释放的内存

该用户从未签到

发表于 2010-12-3 17:27:23 | 显示全部楼层
不过我用的是加上了纹理显示
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表