|
我设置了操作器,但是运行后根本看不到奶牛模型。
TravleManipulator.h
#pragma once
#include <osgGA/CameraManipulator>
#include <osgViewer/Viewer>
class TravleManipulator : public osgGA::CameraManipulator
{
public:
TravleManipulator();
virtual void setByMatrix(const osg::Matrixd& matrix){};
virtual osg::Matrixd getMatrix() const;
virtual void setByInverseMatrix(const osg::Matrixd& matrix){};
virtual osg::Matrixd getInverseMatrix() const ;
virtual bool handle(osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa);
//virtual ~TravleManipulator();
bool changePostion (osg::Vec3 delta);
private:
osg::Vec3 m_vPostion;
osg::Vec3 m_vRotation;
int m_vStep;
float m_vRotateStep;
};
TravleManipulator.cpp实现如下,实现最简单的wsad的动作响应
#include "TravleManipulator.h"
#include <iostream>
TravleManipulator::TravleManipulator()
{
this->m_vPostion= osg::Vec3(20,0,0);
this->m_vRotation = osg::Vec3(osg:I,0,0);
this->m_vStep=5;
this->m_vRotateStep=0;
}
osg::Matrixd TravleManipulator::getMatrix(void) const
{
osg::Matrixd mat;
mat.makeTranslate(this->m_vPostion);
return osg::Matrixd::rotate(this->m_vRotation[0],osg::X_AXIS,this->m_vRotation[1],osg::Y_AXIS,this->m_vRotation[2],
osg::Z_AXIS)*mat;
}
osg::Matrixd TravleManipulator::getInverseMatrix() const
{
osg::Matrixd mat;
mat.makeTranslate(this->m_vPostion);
return osg::Matrixd::inverse(osg::Matrixd::rotate(this->m_vRotation[0],osg::X_AXIS,this->m_vRotation[1],osg::Y_AXIS,this->m_vRotation[2],
osg::Z_AXIS)*mat);
}
bool TravleManipulator::changePostion(osg::Vec3 vec)
{
this->m_vPostion+=vec;
return true;
}
bool TravleManipulator::handle(osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa)
{
osg::ref_ptr<osgViewer::Viewer> v = dynamic_cast<osgViewer::Viewer*>(&aa);
switch(ea.getEventType())
{
case osgGA::GUIEventAdapter::KEYDOWN:
{
if(ea.getKey()=='w'||ea.getKey()=='W')
{
this->changePostion(osg::Vec3(0.0,this->m_vStep,0.0));
std::cout<<"W"<<std::endl;
}
else if(ea.getKey()=='s'||ea.getKey()=='S')
this->changePostion(osg::Vec3(0.0,-this->m_vStep,0.0));
else if(ea.getKey()=='a'||ea.getKey()=='A')
this->changePostion(osg::Vec3(this->m_vStep,0.0,0.0));
else if(ea.getKey()=='d'||ea.getKey()=='D')
this->changePostion(osg::Vec3(this->m_vStep,0.0,0.0));
}
break;
}
return false;
}
main.cpp如下
#include <osg/Group>
#include <osgDB/ReadFile>
#include <osgUtil/PrintVisitor>
#include <osgViewer/ViewerEventHandlers>
#include <osgViewer/Viewer>
#include <osgGA\CameraManipulator>
#include <osgGA\TrackballManipulator>
#include <osgGA/StateSetManipulator>
#include <iostream>
#include <fstream>
#include <sstream>
#include <time.h>
int main( int argc, char** argv )
{
osg::ref_ptr<osg::Group> root = new osg::Group;
root->addChild(osgDB::readNodeFile("cow.osg"));
osgViewer::Viewer viewer;
viewer.setSceneData( root.get() );
//viewer.addEventHandler(new osgViewer::StatsHandler());
osg::ref_ptr<TravleManipulator> kk = new TravleManipulator();
viewer.setCameraManipulator(kk);
//std::cout<<"end "<<t2-t1<<" "<<std::endl;
return viewer.run();
}
运行起来后,屏幕中间看不到奶牛模型,不知道为啥 |
|