查看: 1136|回复: 1

请教个最基本的CameraManipulator的问题

[复制链接]

该用户从未签到

发表于 2015-4-1 22:28:59 | 显示全部楼层 |阅读模式
我设置了操作器,但是运行后根本看不到奶牛模型。
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();
       
}



运行起来后,屏幕中间看不到奶牛模型,不知道为啥

该用户从未签到

发表于 2015-5-2 17:52:04 | 显示全部楼层
有可能是视口的参数没设置好,导致看到了物体内部
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表