一、准备
数据文件:地形数据map.data,模型数据car.ms3d.
float *terrainHeights; float intervalX = 20; float intervalY = 20; float terrainGridLength; //y方向 float terrainGridWidth; //x方向 double COLOR[8][3] = { { 0, 0, 0 }, { 0, 38, 255 }, { 38, 127, 0 }, { 127, 160, 0 }, { 255, 255, 0 }, { 255, 128, 0 }, { 255, 0, 0 }, { 255, 255, 255 } }; void GetColor(double height, double &r, double &g, double &b); bool LoadHeightMap(QString filePath); //加载地图 bool LoadCarModel(QString filePath); //加载模型
二、效果
m_viewer = new osgViewer::Viewer(); m_viewer->setUpViewerAsEmbeddedInWindow(0, 0, 800, 600); //osg::ref_ptr<osg::Group> root = new osg::Group; m_GroupRoot = new osg::Group; osg::ref_ptr<osg::Group> lightRoot = new osg::Group(); osg::ref_ptr<osg::StateSet> lightState = new osg::StateSet(); lightState = lightRoot->getOrCreateStateSet(); lightState->setMode(GL_LIGHTING, osg::StateAttribute::ON); lightState->setMode(GL_LIGHT0, osg::StateAttribute::ON); osg::ref_ptr<osg::Multisample> pms = new osg::Multisample; lightState->setAttributeAndModes(pms, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); LoadMapModel("G:/map.pnt"); LoadCarModel("G:/car.ms3d"); lightRoot->addChild(m_CarMT); lightRoot->addChild(m_GeodeMap); lightRoot->addChild(createLight()); m_viewer->setSceneData(lightRoot); m_viewer->setCameraManipulator(new osgGA::TrackballManipulator); osg::ref_ptr<osg::Camera> camera = m_viewer->getCamera(); camera->setEventCallback(new MoveCarEventCallback()); m_viewer->realize();
三、待总结
1.小错误不断,最可恨的是,设置移动的时候,用了三角函数,然后我忘记了要改成弧度值,当车子转角为a°的时候,我依旧使用cos(a)或者sin(a),结果各种神之位移。还是记录下来吧delt = cos(a/180*PI);
2.按键响应,事件使用Qt的QKeyEvent,但是需要传一个当前窗口的keypress();键盘的事件很容易啊,但是鼠标的没有搞明白。
3.碰撞检测,还没有做,慢慢学习吧。
4.骨骼动画,还没有做,慢慢学习吧。