createSphere.h
#include <osg/Shape>
#include <osg/ShapeDrawable>
#include <osg/Geode>
osg::ref_ptr<osg::Geode> createSphere(float radius)
{
osg::ref_ptr<osg::Sphere> sphereShape = new osg::Sphere(osg::Vec3(2.5,0.0,0.0),radius);
osg::ref_ptr<osg::ShapeDrawable> sphereShapeDrawable = new osg::ShapeDrawable(sphereShape.get());
osg::ref_ptr<osg::Geode> sphereGeode = new osg::Geode;
sphereGeode->addDrawable(sphereShapeDrawable.get());
return sphereGeode;
}
main.cpp
#include "createSphere.h"
#include <osg/Group>
#include <osgDB/readfile>
#include <osgViewer/Viewer>
#include <osg/TexEnv>
#include <osg/Texture2D>
#include <osg/PositionAttitudeTransform>
#include <iostream>
#ifdef _DEBUG
#pragma comment(lib,"osgDBd.lib")
#pragma comment(lib,"osgViewerd.lib")
#pragma comment(lib,"osgd.lib")
#else
#pragma comment(lib,"osgDB.lib")
#pragma comment(lib,"osgViewer.lib")
#pragma comment(lib,"osg.lib")
#endif
int main()
{
//Declear the root node
osg::ref_ptr<osg::Group> root = new osg::Group;
//Declear the viewer instance
osgViewer::Viewer myViewer;
//Declear texture
osg::ref_ptr<osg::Texture2D> fogTexture = new osg::Texture2D;
fogTexture->setDataVariance(osg::Object::DYNAMIC);
osg::ref_ptr<osg::Image> smokeImage = osgDB::readImageFile("land_shallow_topo_2048.jpg");
if (!smokeImage.get())
{
std::cout<<"open file is failed!"<<std::endl;
return -1;
}
fogTexture->setImage(smokeImage.get());
osg::ref_ptr<osg::Geode> sphereOne = createSphere(2.0);
//Create a TexEnv instance for binding the StateSet
osg::ref_ptr<osg::TexEnv> sphereOneTexEnv = new osg::TexEnv;
sphereOneTexEnv->setMode(osg::TexEnv::DECAL);
osg::ref_ptr<osg::StateSet> sphereOneStateSet = new osg::StateSet;
sphereOneStateSet->setAttribute(sphereOneTexEnv.get());
sphereOneStateSet->setAttributeAndModes(fogTexture.get());
sphereOne->setStateSet(sphereOneStateSet.get());
//the second sphere
osg::ref_ptr<osg::Geode> sphereTwo = createSphere(0.5);
osg::ref_ptr<osg::PositionAttitudeTransform> sphereTwoPositionTransform = new osg::PositionAttitudeTransform;
sphereTwoPositionTransform->setPosition(osg::Vec3(5.0,0.0,0.0));
sphereTwoPositionTransform->addChild(sphereTwo.get());
osg::ref_ptr<osg::Texture2D> moonTexture = new osg::Texture2D;
osg::ref_ptr<osg::Image> moonImage = osgDB::readImageFile("moon.jpg");
if (!moonImage.get())
{
std::cout<<"moon file open error"<<std::endl;
return -1;
}
moonTexture->setImage(moonImage.get());
osg::ref_ptr<osg::TexEnv> texEnvSphereTwo = new osg::TexEnv;
texEnvSphereTwo->setMode(osg::TexEnv::BLEND);
osg::ref_ptr<osg::StateSet> sphereTwoStateSet = new osg::StateSet;
sphereTwoStateSet->setAttribute(texEnvSphereTwo.get());
sphereTwoStateSet->setAttributeAndModes(moonTexture.get());
sphereTwoStateSet->setMode(GL_FOG,osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE);
sphereTwo->setStateSet(sphereTwoStateSet.get());
//the third sphere
osg::ref_ptr<osg::Geode> sphereThree = createSphere(1.6);
osg::ref_ptr<osg::PositionAttitudeTransform> sphereThreePositionTrans = new osg::PositionAttitudeTransform;
sphereThreePositionTrans->setPosition(osg::Vec3(30,0,0));
sphereThreePositionTrans->addChild(sphereThree.get());
osg::ref_ptr<osg::Texture2D> marsTexture = new osg::Texture2D;
marsTexture->setDataVariance(osg::Object::DYNAMIC);
osg::ref_ptr<osg::Image> marsImage = osgDB::readImageFile("mars.jpg");
if (!marsImage.get())
{
std::cout<<"moon file open error"<<std::endl;
return -1;
}
marsTexture->setImage(marsImage.get());
osg::ref_ptr<osg::StateSet> sphereThreeStateSet = new osg::StateSet;
osg::ref_ptr<osg::TexEnv> sphereThreeTexEnv = new osg::TexEnv;
sphereThreeTexEnv->setMode(osg::TexEnv::DECAL);
sphereThreeStateSet->setAttributeAndModes(marsTexture.get());
sphereThreeStateSet->setAttribute(sphereThreeTexEnv.get());
sphereThreeStateSet->setMode(GL_FOG,osg::StateAttribute::OFF);
sphereThree->setStateSet(sphereThreeStateSet.get());
root->setStateSet(sphereOneStateSet.get());
root->addChild(sphereOne.get());
root->addChild(sphereTwoPositionTransform.get());
root->addChild(sphereThreePositionTrans.get());
myViewer.setSceneData(root.get());
myViewer.realize();
myViewer.run();
}
纹理可以自己上网找随便找几张就行,我就是这么办得。