继续上次的QT与osg相结合,在原先的基础上创建简单的几何图元。
#include <QApplication>
#include <osgViewer/Viewer>
#include <osgGA/TrackballManipulator>
#include <osgQt/GraphicsWindowQt>
#include <osg/ShapeDrawable>
#include <osg/MatrixTransform>
#include <osgManipulator/TabBoxDragger>
#include <osgGA/StateSetManipulator>
#include <osgViewer/ViewerEventHandlers>
#include <osgDB/ReadFile>
#include <osg/Material>
#include <osg/Texture2D>
#include <osg/Vec3>
#include <osg/Vec4>
#include <osg/ShapeDrawable>
#include <osg/MatrixTransform>
osg::ref_ptr<osg::Geode> CreateBox() {
osg::ref_ptr<osg::Geode> geode= new osg::Geode;
osg::ref_ptr<osg::TessellationHints> hints= new osg::TessellationHints;
osg::ref_ptr<osg::ShapeDrawable> shape= new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(5.0,5.0,5.0),10.0));
osg::ref_ptr<osg::Material> material= new osg::Material;
osg::ref_ptr<osg::Texture2D> texture= new osg::Texture2D;
osg::ref_ptr<osg::Image> image= new osg::Image;
hints->setDetailRatio(0.5);
//设置颜色
shape->setColor(osg::Vec4(0.1,0.5,0.5,0.5));
//设置材质
material->setAmbient(osg::Material::FRONT_AND_BACK,osg::Vec4(0.5,0.5,0.5,0.5));
material->setDiffuse(osg::Material::FRONT_AND_BACK,osg::Vec4(0.5,0.5,0.5,0.5));
material->setSpecular(osg::Material::FRONT_AND_BACK,osg::Vec4(0.5,0.5,0.5,0.5));
material->setShininess(osg::Material::FRONT_AND_BACK,0.6);
shape->getOrCreateStateSet()->setAttributeAndModes(material.get(),osg::StateAttribute::ON);
//设置纹理
image= osgDB::readImageFile("Images/land_shallow_topo_2048.jpg");
if(image->valid())
texture->setImage(image);
shape->getOrCreateStateSet()->setTextureAttributeAndModes(0,texture.get(),osg::StateAttribute::ON);
geode->addDrawable(shape);
return geode;
}
osg::ref_ptr<osg::Geode> CreateTan(){
osg::ref_ptr<osg::Geode> geode= new osg::Geode;
osg::ref_ptr<osg::Geometry> geometry= new osg::Geometry;
osg::ref_ptr<osg::Vec3Array> vects= new osg::Vec3Array;
osg::ref_ptr<osg::Vec4Array> colors= new osg::Vec4Array;
osg::ref_ptr<osg::Vec3Array> norm= new osg::Vec3Array;
//设置定点
vects->push_back(osg::Vec3(5.0,0.0,0.0));
vects->push_back(osg::Vec3(0.0,5.0,0.0));
vects->push_back(osg::Vec3(0.0,0.0,5.0));
geometry->setVertexArray(vects);
//设置颜色
colors->push_back(osg::Vec4(0.5,0.5,0.5,0.5));
colors->push_back(osg::Vec4(0.5,0.5,0.5,0.5));
colors->push_back(osg::Vec4(0.5,0.5,0.5,0.5));
geometry->setColorArray(colors);
//设置绑定方式
geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES,0,3));
//设置法线
norm->push_back(osg::Vec3(2.5,2.5,2.5));
geometry->setNormalArray(norm);
geometry->setNormalBinding(osg::Geometry::BIND_OVERALL);
geode->addDrawable(geometry);
return geode;
}
int main( int argc, char** argv )
{
QApplication app(argc, argv);
osgQt::initQtWindowingSystem();
osg::Group* sceneroot = new osg::Group;
osg::ref_ptr< osg::MatrixTransform> max= new osg::MatrixTransform;
//平移变换
max->setMatrix(osg::Matrix::translate(10.0,0.0,0.0));
max->addChild(CreateTan());
sceneroot->addChild(CreateBox());
sceneroot->addChild(max);
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
viewer->addEventHandler(new osgViewer::StatsHandler);
viewer->setSceneData( sceneroot );
viewer->setCameraManipulator( new osgGA::TrackballManipulator );
osgQt::setViewer( viewer.get() );
osgQt::GLWidget* glw = new osgQt::GLWidget;
osgQt::GraphicsWindowQt* graphicswin = new osgQt::GraphicsWindowQt(glw);
viewer->getCamera()->setViewport( new osg::Viewport(0, 0, glw->width(), glw->height() ) );
viewer->getCamera()->setGraphicsContext( graphicswin );
glw->show();
return app.exec();
}效果图如下: