class KeyboardEventHandler : public osgGA::GUIEventHandler
{
public:
KeyboardEventHandler()
{}
static void rotate(float angle,osg::MatrixTransform *joint)//旋转某个矩阵(在以前的基础上不断累积)
{
osg::Matrix zRot;
zRot.makeRotate(angle, 0.0,0.0,1.0);
joint->setMatrix(zRot*joint->getMatrix());
}
virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
{
switch (ea.getEventType())
{
case(osgGA::GUIEventAdapter::KEYDOWN):
{
switch (ea.getKey())
{
case 'q':
rotate(osg::PI/180, joint1);//正值,是逆时针(右手定则)
return true;
case 'a':
rotate(-osg::PI/180, joint1);
return true;
......
}
}
default:
break;
}
//return false to allow mouse manipulation
return false;
}
};
osg::Group* createShapes()
{
osg::Group* group = new osg::Group();
osg::MatrixTransform* transform =new osg::MatrixTransform();
group->addChild(transform);
//每个关节都有一个MatrixTransform,且都是在前面的基础上添加的( addChild()),以便进行累积,即大臂动了,小臂也随之动
//且每次Z轴都是指向下一个节点方向,以便能直接translate( 0.0, 0.0, height);
joint1 = buildJoint1(transform);
joint2 = buildJoint2(joint1);
osg::MatrixTransform* tube2= buildTube2(joint2);//后面用前面的,承前启后,前面的变化后,后面的也变化
joint3 = buildJoint3(tube2);
joint4 = buildJoint4(joint3);
osg::MatrixTransform *tube5 = buildTube5(joint4);
joint5 = buildJoint5(tube5);
joint6= buildJoint6(joint5);
joint6->addChild( buildEndEffector());
return group;
}
osg::MatrixTransform* buildJoint1(osg::MatrixTransform* previousJoint)
{
osg::MatrixTransform* xTransform =new osg::MatrixTransform();
previousJoint->addChild(xTransform);
float radius = 6.7640f;
float height = 45.0f;
osg::Geode* joint = new osg::Geode();
xTransform->addChild(joint);//xTransform表示当前位置,添加Geode后在当前处画几何体
joint->addDrawable(new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(0.0f,0.0f,height/2),radius,height),hints));
osg::MatrixTransform *zTransform = new osg::MatrixTransform();
xTransform->addChild(zTransform);
osg::Matrix zTrans=osg::Matrix::translate(0.0, 0.0, height);//原点在上(世界坐标系),坐标系Z上X右
osg::Matrix zRot= osg::Matrix::rotate(jointAngle1, 0.0, 0.0, 1.0);//jointAngle1=0,旋转无效
zTransform->setMatrix(zTrans*zRot);
return zTransform;//zTransform为下一个关节点的位置
}
osg::MatrixTransform* buildJoint2(osg::MatrixTransform* previousJoint)
{
if (showAxis)
{
createAxis(previousJoint);
}
double height = 17.6;
double radius = 4.45633;
osg::MatrixTransform* xTransform = new osg::MatrixTransform();
previousJoint->addChild(xTransform);
osg::Matrix xRot = osg::Matrix::rotate(osg::PI_2, 1.0, 0.0, 0.0);//原点在上,坐标系Z前X右
xTransform->setMatrix(xRot);
osg::Geode* joint = new osg::Geode();
joint->addDrawable(new osg::ShapeDrawable(new osg::Capsule(osg::Vec3(0.0f,0.0f,height/2),radius,height),hints));
xTransform->addChild(joint);
osg::MatrixTransform* zTransform = new osg::MatrixTransform();
osg::Matrix zTrans = osg::Matrix::translate( 0.0, 0.0, height);
osg::Matrix zRot = osg::Matrix::rotate(osg::PI_2+jointAngle2, 0.0,0.0,1.0);
//先zRot:原点在上(世界坐标系),坐标系Z前X上.再zTrans:原点在上前(世界坐标系),坐标系Z前X上
zTransform->setMatrix(zTrans*zRot);
xTransform->addChild(zTransform);
return zTransform;
}
osg::MatrixTransform* buildTube2(osg::MatrixTransform* previousJoint)
{
if (showAxis)
{
createAxis(previousJoint);
}
double height = 17.6;
double radius = 4.45633;
osg::MatrixTransform* xTransform = new osg::MatrixTransform();
previousJoint->addChild(xTransform);
osg::Matrix xRot = osg::Matrix::rotate(osg::PI_2, 1.0,0.0,0.0);//原点在上前(世界坐标系),坐标系Z右X上
xTransform->setMatrix(xRot);
osg::Geode* tube3 = new osg::Geode();
xTransform->addChild(tube3);
tube3->addDrawable(new osg::ShapeDrawable(new osg::Capsule(osg::Vec3(0.0f,0.0f,height/2),radius,height),hints));
osg::MatrixTransform* zTransform = new osg::MatrixTransform();
xTransform->addChild(zTransform);
osg::Matrix zTrans = osg::Matrix::translate(0,0,height);
zTransform->se