osg,纹理贴图,地球、月球、火星

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

}


纹理可以自己上网找随便找几张就行,我就是这么办得。

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值