|
楼主 |
发表于 2014-5-10 21:04:45
|
显示全部楼层
在a.cpp文件中,构造函数如果xrot= osg:I_2;漫游正常(漫游交互按键注释中有),但如果xrot= 0;即便用F11和F12改变xrot的值并在控制台显示出xrot的值,也不能显示这个场景。
这是a.h的代码:
/*
程序功能:
·············利用键盘交互实现场景漫游··········
F1 沿着当前方向前行
F2 沿着当前方向后行
F3 沿着当前方向左移
F4 沿着当前方向右移
F5 向上平移
F6 向下平移
F7 沿着Z轴逆时针旋转
F8 沿着Z轴顺时针旋转
F9 沿着Y轴逆时针旋转
F10 沿着Y轴顺时针旋转
F11 沿着X轴逆时针旋转
F12 沿着X轴顺时针旋转
*/
#include <iostream>
#include <osgGA/GUIEventHandler>
#include <osgGA/CameraManipulator>
#include <osgGA/TrackballManipulator>
#include <osgViewer/Viewer>
class WPSCameraManipulator : public osgGA::CameraManipulator
{
public:
WPSCameraManipulator(void);
~WPSCameraManipulator(void);
private:
osg::Matrixd translateMatrix;
osg::Matrixd rotateMatrix;
osg::Vec3 pos;
osg::Vec3 angle;
float xtrans, ytrans, ztrans, xrot, yrot, zrot;
float steplength, stepangle;//分别设置相机移动的位移基准长度,旋转基准角度。
public:
// 虚函数
virtual void setByMatrix(const osg::Matrixd& matrix);
// 虚函数
virtual void setByInverseMatrix(const osg::Matrixd& matrix);
virtual osg::Matrixd getMatrix(void) const;
// 得到逆矩阵
virtual osg::Matrixd getInverseMatrix(void)const;
// 主要事件控制器
virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us);
void refresh(void);
};
这是a.cpp的代码:
#include "a.h"
#include <osg/Node>
#include <osgGA/GUIEventHandler>
#include <iostream>
WPSCameraManipulator::WPSCameraManipulator()
{
xtrans = 0;
ytrans = 0;
ztrans= 0;
//xrot= 0;
xrot= osg::PI_2;
yrot= 0;
zrot= 0;
steplength = 0.5;
stepangle = osg::PI/20;
pos.set(xtrans, ytrans, ztrans);
angle.set(xrot, yrot, zrot);
translateMatrix.makeIdentity();
rotateMatrix.makeIdentity();
translateMatrix = translateMatrix * osg::Matrixd::translate(pos);
rotateMatrix.makeRotate(angle._v[0],osg::Vec3(1.0f, 0.0f, 0.0f),
angle._v[1],osg::Vec3(0.0f, 1.0f, 0.0f),
angle._v[2],osg::Vec3(0.0f, 0.0f, 1.0f));
}
//------------------------//------------------------------
WPSCameraManipulator::~WPSCameraManipulator()
{
}
//------------------------//------------------------------
void WPSCameraManipulator::setByMatrix(const osg::Matrixd& matrix)
{
}
//------------------------//------------------------------
void WPSCameraManipulator::setByInverseMatrix(const osg::Matrixd &matrix)
{
}
//------------------------//------------------------------
osg::Matrixd WPSCameraManipulator::getMatrix(void) const
{
return rotateMatrix * translateMatrix;
}
//------------------------//------------------------------
osg::Matrixd WPSCameraManipulator::getInverseMatrix (void) const
{
return osg::Matrixd::inverse(rotateMatrix * translateMatrix);
}
// 主要事件控制器
bool WPSCameraManipulator::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us)
{
switch(ea.getEventType())
{
case(osgGA::GUIEventAdapter::KEYDOWN):
{
if(ea.getKey()==0xFFBE)//F1
{
//!!!这里我解释一下,zrot是绕着Z轴的角度,初始时是0,也就是面向X轴正向,
//但osg的视图现实中,默认是面向Y轴正向,所以加了osg::PI_2使得面向x转为面向y
xtrans += steplength * cosf(zrot+osg::PI_2);
ytrans += steplength * sinf(zrot+osg::PI_2);
refresh();
}
if(ea.getKey()==0xFFBF)
{
xtrans -= steplength * cosf(zrot+osg::PI_2);
ytrans -= steplength * sinf(zrot+osg::PI_2);
refresh();
}
if(ea.getKey()==0xFFC0)//F3
{
xtrans -= steplength * sinf(zrot+osg::PI_2);
ytrans += steplength * cosf(zrot+osg::PI_2);
refresh();
}
if(ea.getKey()==0xFFC1)
{
xtrans += steplength * sinf(zrot+osg::PI_2);
ytrans -= steplength * cosf(zrot+osg::PI_2);
refresh();
}
if(ea.getKey()==0xFFC2)//F5
{
ztrans += steplength;
refresh();
}
if(ea.getKey()==0xFFC3)
{
ztrans -= steplength;
refresh();
}
//------------------------//------------------------------
if(ea.getKey()==0xFFC4)//F7
{
zrot += stepangle;
refresh();
}
if(ea.getKey()==0xFFC5)//F8
{
zrot -= stepangle;
refresh();
}
if(ea.getKey()==0xFFC6)
{
yrot += stepangle;
refresh();
}
if(ea.getKey()==0xFFC7)
{
yrot -= stepangle;
refresh();
}
if(ea.getKey()==0xFFC8)
{
xrot += stepangle;
refresh();
std::cout<<"绕着X轴的角度是"<<xrot<<std::endl;
}
if(ea.getKey()==0xFFC9)
{
xrot -= stepangle;
refresh();
std::cout<<"绕着X轴的角度是"<<xrot<<std::endl;
}
}
break;
case(osgGA::GUIEventAdapter:OUBLECLICK):
{
}
break;
default:break;
}
return false;
}
void WPSCameraManipulator::refresh()
{
pos.set(xtrans, ytrans, ztrans);
angle.set(xrot, yrot, zrot);
translateMatrix.makeTranslate(pos);
rotateMatrix.makeRotate(angle._v[0],osg::Vec3(1.0f, 0.0f, 0.0f),
angle._v[1],osg::Vec3(0.0f, 1.0f, 0.0f),
angle._v[2],osg::Vec3(0.0f, 0.0f, 1.0f));
}
主函数就比较简单了:
#include <osg/Node>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osgGA/TrackballManipulator>
#include <osg/MatrixTransform>
#include <iostream>
#include "a.h"
void main()
{
osgViewer::Viewer viewer;
osg::ref_ptr<osg::Group> root = new osg::Group();
osg::ref_ptr<osg::Node> buildingnode = osgDB::readNodeFile("ceep.ive");
root->addChild(buildingnode.get());
viewer.setSceneData(root.get());
WPSCameraManipulator* myCameramanipulator = new WPSCameraManipulator;
viewer.setCameraManipulator(myCameramanipulator);
viewer.setUpViewInWindow(700,500,500,500);
viewer.realize();
viewer.run();
}
这里的文件 ceep.ive可以再FreeSouth的群文件下到。二十多M附件放不下。
cenfer哥帮看看是什么情况? |
-
-
a.cpp
3.71 KB, 下载次数: 1, 下载积分: 威望 1
漫游类实现
-
-
a.h
1.26 KB, 下载次数: 1, 下载积分: 威望 1
漫游类的定义
-
-
wps.cpp
641 Bytes, 下载次数: 0, 下载积分: 威望 1
主函数
|