OSG学习笔记12-期末大作业(包含交互、纹理、光照)

这篇博客记录了一项使用OpenSceneGraph(OSG)完成的期末大作业,主要内容包括球体纹理贴图、光照效果以及键盘交互控制。作者通过Sphere的ShapeDrawable实现纹理绘制,使用MatrixTransform进行矩阵变换以实现球体的移动。同时,通过osg::Light和LightSource添加灯光并处理键盘事件以调整灯光位置。代码结构和风格有待改进。
摘要由CSDN通过智能技术生成

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
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值