OSG几何变换实验三

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

天津理工大学
计算机科学与技术学院
实验报告
2015 至2016 学年第二学期
源代码:
#include <osg/Group>
#include <osg/ShapeDrawable>
#include <osgViewer/ViewerEventHandlers> #include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osgUtil/LineSegmentIntersector> #include <osgGA/GUIEventHandler>
#include <osg/Matrixd>
#include <osg/MatrixTransform>
#include <osgGA/AnimationPathManipulator>
#include <osg/AnimationPath>
osg::ref_ptr<osg::MatrixTransform> root_Tr = new osg::MatrixTransform; osg::ref_ptr<osg::MatrixTransform> head_Tr = new osg::MatrixTransform; osg::ref_ptr<osg::MatrixTransform> body_Tr = new osg::MatrixTransform; osg::ref_ptr<osg::MatrixTransform> left_arm_Tr =new
osg::MatrixTransform;
osg::ref_ptr<osg::MatrixTransform> right_arm_Tr =new
osg::MatrixTransform;
osg::ref_ptr<osg::MatrixTransform> left_leg_Tr =new
osg::MatrixTransform;
osg::ref_ptr<osg::MatrixTransform> right_leg_Tr =new
osg::MatrixTransform;
class UseEventHandler:public osgGA::GUIEventHandler
{
private:
float angle1;
float angle;
float move;
float scale;
int singal;
public :
UseEventHandler(){
angle1=0;
angle=0;
move=1;
scale=1;
singal=0;
}
static void rotate(float angle,osg::MatrixTransform *joint)
{
osg::Matrix zRot;
zRot.makeRotate(angle, 1.0,0.0,0.0);
joint->setMatrix(zRot*joint->getMatrix());
}
virtual bool handle (const osgGA::GUIEventAdapter&
ea,osgGA::GUIActionAdapter& aa)
{
switch(ea.getEventType())
{
case osgGA::GUIEventAdapter::KEYDOWN:
{
angle=osg::PI_2/3;
if(ea.getKey()=='w')
{
if(singal==0)
{
left_leg_Tr->setMatrix(osg::Matrix::translate(1.5,0,-8)*osg::Matr ix::rotate(angle,osg::Vec3(-1,0,0))*osg::Matrix::translate(-1.5,0,8)* osg::Matrix::translate(0,-move,0));
}
else if(singal==1)
{
body_Tr->setMatrix(osg::Matrix::translate(0,-move,0));
head_Tr->setMatrix(osg::Matrix::translate(0,-move,0));
left_arm_Tr->setMatrix(osg::Matrix::translate(0,-move,0));
right_arm_Tr->setMatrix(osg::Matrix::translate(0,-move,0));
left_leg_Tr->setMatrix(osg::Matrix::translate(1.5,0,-8)*osg::Matr ix::rotate(angle,osg::Vec3(1,0,0))*osg::Matrix::translate(-1.5,0,8));
left_leg_Tr->setMatrix(osg::Matrix::translate(0,-move,0));
right_leg_Tr->setMatrix(osg::Matrix::translate(0,-move,0));
}
singal++;
singal%=2;
move++;
}
else if(ea.getKey()=='s')
{
if(singal==0)
{
right_leg_Tr->setMatrix(osg::Matrix::translate(-1.5,0,-8)*osg::Ma trix::rotate(angle,osg::Vec3(1,0,0))*osg::Matrix::translate(1.5,0,8)* osg::Matrix::translate(0,-move,0));
}
else if(singal==1)
{
body_Tr->setMatrix(osg::Matrix::translate(0,-move,0));
head_Tr->setMatrix(osg::Matrix::translate(0,-move,0));
left_arm_Tr->setMatrix(osg::Matrix::translate(0,-move,0));
right_arm_Tr->setMatrix(osg::Matrix::translate(0,-move,0));
left_leg_Tr->setMatrix(osg::Matrix::translate(-1.5,0,-8)*osg::Mat rix::rotate(angle,osg::Vec3(-1,0,0))*osg::Matrix::translate(1.5,0,8)) ;
left_leg_Tr->setMatrix(osg::Matrix::translate(0,-move,0));
right_leg_Tr->setMatrix(osg::Matrix::translate(0,-move,0));
}
singal++;
singal%=2;
move--;
}else if(ea.getKey()=='a')
{
angle1+=osg::PI_2;
root_Tr->setMatrix(osg::Matrix::rotate(angle1,osg::Vec3(0,0,1)));
}else if(ea.getKey()=='d')
{
angle1+=osg::PI_2;
root_Tr->setMatrix(osg::Matrix::rotate(angle1,osg::Vec3(0,0,-1))) ;
}
return true ;
}
break;
default:
break;
}
return false;
}
osg::Group * createPerson()
{
osg::ref_ptr<osg::Node> node = new osg::Node;
osg::ref_ptr<osg::Group> group = new osg::Group;
group->addChild(root_Tr.get());
osg::ref_ptr<osg::Geode> head = new osg::Geode;
head_Tr->addChild(head.get());
osg::ref_ptr<osg::Geode> body = new osg::Geode;
body_Tr->addChild(body.get());
osg::ref_ptr<osg::Geode> left_arm = new osg::Geode; //左胳膊
left_arm_Tr->addChild(left_arm.get());
osg::ref_ptr<osg::Geode> right_arm = new osg::Geode; //右胳膊right_arm_Tr->addChild(right_arm.get());
osg::ref_ptr<osg::Geode> left_leg = new osg::Geode; //左腿
left_leg_Tr->addChild(left_leg.get());
osg::ref_ptr<osg::Geode> right_leg = new osg::Geode; //右腿
right_leg_Tr->addChild(right_leg.get());
root_Tr->addChild(head_Tr.get());
root_Tr->addChild(body_Tr.get());
root_Tr->addChild(left_arm_Tr.get());
root_Tr->addChild(right_arm_Tr.get());
root_Tr->addChild(left_leg_Tr.get());
root_Tr->addChild(right_leg_Tr.get());
osg::ref_ptr<osg::Sphere> head_sp = new
osg::Sphere(osg::Vec3(0,0.0,20),2);
osg::ref_ptr<osg::ShapeDrawable> head_ShapeDrawable = new osg::ShapeDrawable(head_sp.get());
head->addDrawable(head_ShapeDrawable.get());
osg::ref_ptr<osg::Sphere> eye1_sp = new
osg::Sphere(osg::Vec3(-0.7,-1.7,21),0.5);
osg::ref_ptr<osg::ShapeDrawable> eye1_ShapeDrawable = new osg::ShapeDrawable(eye1_sp.get());
head->addDrawable(eye1_ShapeDrawable.get());
osg::ref_ptr<osg::Sphere> eye2_sp = new
osg::Sphere(osg::Vec3(0.7,-1.7,21),0.5);
osg::ref_ptr<osg::ShapeDrawable> eye2_ShapeDrawable = new osg::ShapeDrawable(eye2_sp.get());
head->addDrawable(eye2_ShapeDrawable.get());
osg::ref_ptr<osg::Sphere> ear1_sp = new
osg::Sphere(osg::Vec3(0,-1.7,20),0.5);
osg::ref_ptr<osg::ShapeDrawable> ear1_ShapeDrawable = new osg::ShapeDrawable(ear1_sp.get());
head->addDrawable(ear1_ShapeDrawable.get());
osg::ref_ptr<osg::Cylinder> head_Cylinder = new
osg::Cylinder(osg::Vec3(0,0,18), 1, 2);
osg::ref_ptr<osg::ShapeDrawable> cylinderDrawable = new osg::ShapeDrawable(head_Cylinder.get());
head->addDrawable(cylinderDrawable);
osg::ref_ptr<osg::ShapeDrawable> body_Shape =
new osg::ShapeDrawable( new osg::Box(osg::Vec3(0.0f, 0.0f, 13.0f), 6.0f, 3.0f, 8.0f));
body->addDrawable(body_Shape.get());
osg::ref_ptr<osg::Cylinder> left_arm_shape = new
osg::Cylinder(osg::Vec3(-4,0,14), 1, 6);
osg::ref_ptr<osg::ShapeDrawable> left_arm_Drawable = new osg::ShapeDrawable(left_arm_shape.get());
left_arm->addDrawable(left_arm_Drawable);
osg::ref_ptr<osg::Cylinder> right_arm_shape = new
osg::Cylinder(osg::Vec3(4,0,14), 1, 6);
osg::ref_ptr<osg::ShapeDrawable> right_arm_Drawable = new osg::ShapeDrawable(right_arm_shape.get());
计算机科学与技术学院right_arm->addDrawable(right_arm_Drawable);
osg::ref_ptr<osg::Cylinder> left_leg_shape = new
osg::Cylinder(osg::Vec3(-1.5,0,6), 1, 8);
osg::ref_ptr<osg::ShapeDrawable> left_leg_Drawable = new
osg::ShapeDrawable(left_leg_shape.get());
left_leg->addDrawable(left_leg_Drawable);
osg::ref_ptr<osg::Cylinder> right_leg_shape = new
osg::Cylinder(osg::Vec3(1.5,0,6), 1, 8);
osg::ref_ptr<osg::ShapeDrawable> right_leg_Drawable = new
osg::ShapeDrawable(right_leg_shape.get());
right_leg->addDrawable(right_leg_Drawable);
root_Tr->setMatrix(osg::Matrix::rotate(osg::PI_2,osg::Vec3(0,0,1) ));
return group.release();
}
int main0(int argc,char **argv)
{
osgViewer::Viewer viewer;
viewer.setSceneData(createPerson());
viewer.addEventHandler(new UseEventHandler());
return viewer.run();
}
11。

相关文档
最新文档