gpt4 book ai didi

c++ - 如何在 openscenegraph 中旋转相机(在模型中穿行)?

转载 作者:太空狗 更新时间:2023-10-29 23:15:41 25 4
gpt4 key购买 nike

我已经在我的场景中加载了 3D 模型,我想在世界中穿行。我实现了向前、向后、向左、向右、向上和向下,但我需要在该摄像头位置围绕 x、y、z 旋转我的摄像头,但我无法获得请帮助我??

#include <osgViewer/ViewerEventHandlers>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>

#include <osg/Camera>



#include <math.h>



osg::Matrixd md ;
osg::Matrixd originalmd ;
osg::Quat qua;
float angle=0;

osgViewer::Viewer viewer;
osg::Vec3f eye=osg::Vec3f(1.0,-200.0,2.0);
osg::Vec3f centre=osg::Vec3f(0.0,0.0,0.0);
osg::Vec3f up=osg::Vec3f(0.0,0.0,1.0);

osg::Vec3f originaleye=osg::Vec3f(0.0,-200.0,0.0);
osg::Vec3f originalcentre=osg::Vec3f(0.0,0.0,0.0);
osg::Vec3f originalup=osg::Vec3f(0.0,0.0,1.0);

int ConstantVariation=5;


class PickHandler : public osgGA::GUIEventHandler
{
public:

PickHandler() {}

~PickHandler() {}

bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa);


protected:

};

bool PickHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa)
{
switch(ea.getEventType())
{
case(osgGA::GUIEventAdapter::PUSH):
{
osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
//if (view) pick(view,ea);
return false;
}
case(osgGA::GUIEventAdapter::KEYDOWN):
{
if (ea.getKey()=='c')
{
osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
osg::ref_ptr<osgGA::GUIEventAdapter> event = new osgGA::GUIEventAdapter(ea);
event->setX((ea.getXmin()+ea.getXmax())*0.5);
event->setY((ea.getYmin()+ea.getYmax())*0.5);
// if (view) pick(view,*event);
}
if (ea.getKey()== osgGA::GUIEventAdapter::KEY_Up)
{
std::cout<<"Forward"<<std::endl;

md=viewer.getCamera()->getViewMatrix();
osg::Vec3d trans=md.getTrans();
std::cout<<"translation="<<trans.x()<<","<<trans.y()<<","<<trans.z()<<std::endl;

md.getLookAt(eye,centre,up);

trans.z()=trans.z()+5;

md.setTrans(trans);
}
if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Down)
{
std::cout<<"BackWard"<<std::endl;


md=viewer.getCamera()->getViewMatrix();
osg::Vec3d trans=md.getTrans();
std::cout<<"translation="<<trans.x()<<","<<trans.y()<<","<<trans.z()<<std::endl;

md.getLookAt(eye,centre,up);
trans.z()=trans.z()-5;

md.setTrans(trans);

}
if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Left)
{

std::cout<<"LEFT Side"<<std::endl;

md=viewer.getCamera()->getViewMatrix();
osg::Vec3d trans=md.getTrans();
std::cout<<"translation="<<trans.x()<<","<<trans.y()<<","<<trans.z()<<std::endl;

md.getLookAt(eye,centre,up);

trans.x()=trans.x()+5;

md.setTrans(trans);
}

if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Right)
{

std::cout<<"Right Side"<<std::endl;

md=viewer.getCamera()->getViewMatrix();
osg::Vec3d trans = md.getTrans();
std::cout<<"translation="<<trans.x()<<","<<trans.y()<<","<<trans.z()<<std::endl;

trans.x()=trans.x()-5;
md.setTrans(trans);


}
if(ea.getKey()==osgGA::GUIEventAdapter::KEY_Page_Up)
{
std::cout<<"UP"<<std::endl;

md=viewer.getCamera()->getViewMatrix();
osg::Vec3d trans = md.getTrans();
std::cout<<"translation="<<trans.x()<<","<<trans.y()<<","<<trans.z()<<std::endl;

trans.y()=trans.y()-5;
md.setTrans(trans);


}

if(ea.getKey()==osgGA::GUIEventAdapter::KEY_Page_Down)
{
std::cout<<"Down"<<std::endl;

md=viewer.getCamera()->getViewMatrix();
osg::Vec3d trans = md.getTrans();
std::cout<<"translation="<<trans.x()<<","<<trans.y()<<","<<trans.z()<<std::endl;

trans.y()=trans.y()+5;
md.setTrans(trans);


}

if(ea.getKey()==osgGA::GUIEventAdapter::KEY_8)
{
std::cout<<"Rotation"<<std::endl;
md=viewer.getCamera()->getViewMatrix();
osg::Vec3d trans=md.getTrans();
double a=trans.x();
double b=trans.y();
double c=trans.z();
std::cout<<"translation="<<trans.x()<<","<<trans.y()<<","<<trans.z()<<std::endl;
md.setTrans(osg::Vec3d(0,0,0));
std::cout<<"translation="<<trans.x()<<","<<trans.y()<<","<<trans.z()<<std::endl;

angle=0.077;
//osg::Quat qa(angle,osg::Vec3d(0.0,0.0,1.0));

md.getLookAt(eye,centre,up);
osg::Quat qa(angle,up);
md.setRotate(qa);
up=md.getRotate()*trans;

md.setRotate(qa);
trans.x()=a;
trans.y()=b;
trans.z()=c;
md.setTrans(trans);

std::cout<<"translation="<<trans.x()<<","<<trans.y()<<","<<trans.z()<<std::endl;
std::cout<<" End Rotation"<<std::endl;
}


if(ea.getKey()==osgGA::GUIEventAdapter::KEY_Space)
{
md=originalmd;

}
return false;
}
default:
return false;
}
}



int main( int argc, char **argv )
{



osg::ref_ptr<osg::Group> scene = new osg::Group;

scene->addChild(osgDB::readNodeFile("cessna.osg"));

viewer.addEventHandler(new osgViewer::WindowSizeHandler);
viewer.addEventHandler(new PickHandler());

viewer.setSceneData(scene.get());

const osg::BoundingSphere& bs = scene->getBound();


viewer.getCamera()->setViewMatrixAsLookAt(bs.center()+osg::Vec3(0.0f, -(3*bs.radius()),0.0f), bs.center(), osg::Z_AXIS) ;

md=viewer.getCamera()->getViewMatrix();
originalmd=md;



viewer.realize();

while(!viewer.done())
{
viewer.getCamera()->setViewMatrix(md);
viewer.frame();
}


}

编辑:-我怀疑我是否以正确的方式执行任务,即我需要移动相机但上面的代码翻译和旋转对象以感觉相机旋转......许多网站说在 opengl 中没有相机链接(https://www.opengl.org/archives/resources/faq/technical/viewing.htm) .

最佳答案

就像翻译一样,您使用 getTrans() 获取现有翻译并向其添加增量或减量,类似地对于旋转也使用 getRotate() 获得现有旋转然后给它加一个小的增量或减量,不要每次都直接设置相同的值。

关于c++ - 如何在 openscenegraph 中旋转相机(在模型中穿行)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28104362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com