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