osgrobot例子详解

本文通过osg库实现了一个机器人手臂的3D模型,详细解释了如何创建关节和连杆,通过键盘事件控制各关节旋转。每个关节由MatrixTransform表示,通过矩阵变换实现旋转和平移,模拟机器人运动。示例代码展示了如何构建和操纵机器人关节,以及添加轴线辅助视觉理解。

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

osgEarth 的 121 个案例详解 osgEarth 的 121 个案例详解 ...........................................................................................................1 1. aeqd.earth.................................................................................................................................4 2. annotation.earth .......................................................................................................................5 3. annotation_dateline.earth........................................................................................................6 4. annotation_dateline_projected.earth ......................................................................................8 5. annotation_flat.earth................................................................................................................8 6. arcgisonline.earth .....................................................................................................................9 7. bing.earth................................................................................................................................10 8. boston.earth............................................................................................................................11 9. boston_buildings.earth ...........................................................................................................12 10. boston_projected.earth ..................................................................................................13 11. boston_tfs.earth..............................................................................................................14 12. boston-gpu.earth ............................................................................................................15 13. bumpmap.earth ..............................................................................................................16 14. clouds.earth ....................................................................................................................17 15. colorramp.earth ..............................................................................................................18 16. contourmap.earth ...........................................................................................................19 17. datum_override.earth.....................................................................................................20 18. day_night_mp.earth........................................................................................................21 19. day_night_rex.earth........................................................................................................21 20. detail_texture.earth ........................................................................................................21 21. errors.earth .....................................................................................................................22 22. fade_elevation.earth.......................................................................................................22 23. feature_clip_plane.earth.................................................................................................23 24. feature_country_boundaries.earth.................................................................................24 25. feature_custom_filters.earth ..........................................................................................25 26. feature_draped_lines.earth ............................................................................................26 27. feature_draped_polygons.earth .....................................................................................27 28. feature_elevation.earth ..................................................................................................28 29. feature_extrude.earth.....................................................................................................29 30. feature_geom.earth ........................................................................................................30 31. feature_gpx.earth............................................................................................................31 32. feature_inline_geometry.earth.......................................................................................32 33. feature_labels.earth........................................................................................................33 34. feature_labels_script.earth.............................................................................................35 35. feature_levels_and_selectors.earth................................................................................35 36. feature_model_scatter.earth ..........................................................................................36 37. feature_models.earth .....................................................................................................37 38. feature_occlusion_culling.earth......................................................................................38osgEarth 编辑器 SXEarth www.sxsim.com 2 39. feature_offset_polygons.earth .......................................................................................38 40. feature_overlay.earth......................................................................................................39 41. feature_poles.earth.........................................................................................................40 42. feature_population_cylinders.earth ...............................................................................40 43. feature_raster.earth ........................................................................................................41 44. feature_rasterize.earth ...................................................................................................41 45. feature_rasterize_2.earth ...............................................................................................42 46. feature_scripted_styling.earth........................................................................................43 47. feature_scripted_styling_2.earth....................................................................................43 48. feature_scripted_styling_3.earth....................................................................................43 49. feature_style_selector.earth ...........................................................................................44 50. feature_tfs.earth .............................................................................................................45 51. feature_tfs_scripting.earth .............................................................................................46 52. feature_wfs.earth............................................................................................................47 53. fractal_elevation.earth....................................................................................................47 54. gdal_multiple_files.earth ................................................................................................47 55. gdal_tiff.earth..................................................................................................................48 56. geomshader.earth ...........................................................................................................49 57. glsl.earth..........................................................................................................................50 58. glsl_filter.earth ................................................................................................................51 59. graticules.earth ...............................................................................................................52 60. hires-inset.earth..............................................................................................................53 61. intersect_filter.earth .......................................................................................................54 62. land_cover_mixed.earth .................................................................................................55 63. layer_opacity.earth .........................................................................................................55 64. ldb.earth..........................................................................................................................56 65. mapbox.earth..................................................................................................................56 66. mask.earth ......................................................................................................................57 67. mb_tiles.earth.................................................................................................................58 68. mercator_to_plate_carre.earth ......................................................................................59 69. mgrs_graticule.earth.......................................................................................................60 70. min_max_level.earth ......................................................................................................60 71. min_max_range.earth.....................................................................................................61 72. min_max_range_rex.earth..............................................................................................62 73. min_max_resolutions.earth............................................................................................62 74. multiple_heightfields.earth.............................................................................................64 75. night.earth.......................................................................................................................65 76. nodata.earth ...................................................................................................................65 77. noise.earth ......................................................................................................................68 78. normalmap.earth ............................................................................................................68 79. ocean.earth .....................................................................................................................69 80. ocean_no_elevation.earth ..............................................................................................69 81. openstreetmap.earth......................................................................................................69 82. openstreetmap_buildings.earth .....................................................................................70osgEarth 编辑器 SXEarth www.sxsim.com 3 83. openstreetmap_flat.earth...............................................................................................70 84. openstreetmap_full.earth...............................................................................................70 85. openweathermap_clouds.earth......................................................................................71 86. openweathermap_precipitation.earth ...........................................................................71 87. openweathermap_pressure.earth ..................................................................................71 88. photosphere1.earth ........................................................................................................71 89. photosphere2.earth ........................................................................................................72 90. readymap.earth...............................................................................................................73 91. readymap_flat.earth .......................................................................................................73 92. readymap_include.earth.................................................................................................74 93. readymap_template.earth..............................................................................................74 94. readymap-elevation-only.earth.......................................................................................74 95. readymap-osm.earth ......................................................................................................75 96. readymap-priority.earth..................................................................................................75 97. readymap-rex.earth ........................................................................................................75 98. roads.earth......................................................................................................................76 99. roads-flattened.earth......................................................................................................76 100. roads-test.earth...............................................................................................................76 101. scene_clamping.earth.....................................................................................................76 102. silverlining.earth..............................................................................................................78 103. simple_model.earth........................................................................................................78 104. skyview1.earth ................................................................................................................79 105. skyview2.earth ................................................................................................................80 106. splat.earth .......................................................................................................................81 107. splat-blended-with-imagery.earth ..................................................................................81 108. splat-with-mask-layer.earth.............................................................................................81 109. splat-with-multiple-zones.earth......................................................................................82 110. splat-with-rasterized-land-cover.earth............................................................................82 111. stamen_toner.earth ........................................................................................................82 112. stamen_watercolor.earth................................................................................................82 113. state_plane.earth............................................................................................................82 114. tess_screen_space.earth.................................................................................................82 115. tess-coastlines.earth .......................................................................................................82 116. tess-terrain.earth ............................................................................................................83 117. triton.earth......................................................................................................................83 118. triton_drop_shader.earth................................................................................................83 119. utm.earth ........................................................................................................................83 120. utm_graticule.earth ........................................................................................................83 121. vertical_datum.earth ......................................................................................................83 122. wms_nexrad.earth ..........................................................................................................84 123. wms-t_nexrad_animated.earth ......................................................................................84 124. 编辑问题总结....................................................................................错误!未定义书签。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值