PS.部分代码是参考网上的其他程序,原理是参考《OpenSceneGraph3.0 三维视景仿真技术开发详解》以及王锐大大的书。
这是一个期末大作业,很low的大作业。按老师的要求要实现纹理,光照,交互等,还好可以用OSG,用OpenGL的话不知道要写多少代码。
这个代码主要就是实现了球体的贴图,添加了一个光照,还有通过键盘来操控球体的功能。
代码格式啊,命令风格啊,代码结构组织问题比较大,就这样了吧,,以后再慢慢学习。
1、common.h 写一些都会用到的头文件,以及包含lib文件的命令,这样的话其他需要使用这些头文件的和lib库的只用包含这个common.h就可以了
#pragma once
#include <iostream>
#include <osg/LightModel>
#ifdef _DEBUG
#pragma comment(lib,"osgDBd.lib")
#pragma comment(lib,"osgViewerd.lib")
#pragma comment(lib,"osgd.lib")
#pragma comment(lib,"osgGAd.lib")
#pragma comment(lib,"OpenThreadsd.lib")
#else
#pragma comment(lib,"osgDB.lib")
#pragma comment(lib,"osgViewer.lib")
#pragma comment(lib,"osg.lib")
#pragma comment(lib,"osgGA.lib")
#pragma comment(lib,"OpenThreads.lib")
#endif
2、moveEarth.h 控制球体移动的类的声明
#pragma once
#include "common.h"
class moveEarth :public osgGA::GUIEventHandler
{
public:
moveEarth();
~moveEarth();
moveEarth(osg::Vec3 position, double radius);
virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa);
osg::Geode *sphere;
osg::MatrixTransform *mt = new osg::MatrixTransform;
};
包含一个自定义的构造函数,通过传入一个Vec3类型的position表示球体的位置,还有radius表示球体的半径,来构造一个球体。
moveEarth::handle()通过键盘控制球体移动的事件。
这个类包含两个成员变量,一个是Geode类型的sphere,一个是MatrixTransform类型的mt。
sphere用于保存构造的球体,mt是一个控制矩阵类节点,加入这个mt的子节点都要经过这个矩阵的处理。
3、moveEarth.cpp 实现
#include "stdafx.h"
#include"moveEarth.h"
moveEarth::moveEarth(osg::Vec3 position, double radius)
{
sphere = new osg::Geode;
osg::ref_ptr<osg::Sphere> sphereShape = new osg::Sphere(position, radius);
osg::ref_ptr<osg::ShapeDrawable> sphereShapeDrawable = new osg::ShapeDrawable(sphereShape.get());
sphere -> addDraw