我要发帖 回复

新手上路

1

主题

12

积分

0

专家分

:

私信
发表时间 : 2010-3-25 10:46:49 | 浏览 : 2331    评论 : 1
小弟初学osg遇到一问题,请各位大侠出手相助,小弟不胜感激~~
我用书上的示例合成了一个程序,我想让视图三中的摄影机作为姿态摄影机,在飞机的侧上方显示飞机的实时姿态,但摄影机不是不随动,就是不显示,应该如何修改?源代码如下:
#include<osgViewer/Viewer>
#include<osgViewer/ViewerEventHandlers>
#include<osgViewer/CompositeViewer>

#include<osg/Node>
#include<osg/Geode>
#include<osg/Geometry>
#include<osg/Group>
#include<osg/Math>
#include<osg/AnimationPath>
#include<osg/PositionAttitudeTransform>
#include<osg/MatrixTransform>
#include<osg/Camera>

#include<osgGA/TrackballManipulator>

#include<osgDB/ReadFile>
#include<osgDB/WriteFile>

#include<osgUtil/Optimizer>

#include<iostream>


osg::ref_ptr<osg::AnimationPath>createAnimationPath(osg::Vec3&center,float radius,float looptime)
{
        osg::ref_ptr<osg::AnimationPath>animationPath=new osg::AnimationPath();
        animationPath->setLoopMode(osg::AnimationPath::LOOP);

        int numPoint=60;

        float yaw=0.0f;
        float yaw_delta=2.0f*osg::PI/((float)(numPoint-1.0f));
        float roll=osg::inDegrees(45.0f);

        float time=0.0f;
        float time_delta=looptime/((float)numPoint);
       
        for(int i=0;i<numPoint;i++)
        {
                osg::Vec3 position(center+osg::Vec3(sinf(yaw)*radius,cosf(yaw)*radius,0.0f));
                osg::Quat rotation(osg::Quat(roll,osg::Vec3(0.0,1.0,0.0))*osg::Quat(-(yaw+osg::inDegrees(90.0f)),osg::Vec3(0.0,0.0,1.0)));

                animationPath->insert(time,osg::AnimationPath::ControlPoint(position,rotation));
                yaw+=yaw_delta;
                time+=time_delta;
        }
        return animationPath.get();
}

int main()
{
        osg::ref_ptr<osg::Group>root=new osg::Group();
        osg::ref_ptr<osg::Node>ss=osgDB::readNodeFile("cessna.osg");
        osg::ref_ptr<osg::Node>node=osgDB::readNodeFile("lz.osg");

        const osg::BoundingSphere&nk=node->getBound();

        const osg::BoundingSphere&bs=ss->getBound();
        osg::Vec3 position=bs.center()+osg::Vec3(0.0f,0.0f,200.0f);
        float size=100.0f/bs.radius()*0.3f;

        osg::ref_ptr<osg::AnimationPath>animationPath=new osg::AnimationPath();
        animationPath=createAnimationPath(position,200.0f,10.0f);

        std::string fileName("animation.path");
        std::ofstream out(fileName.c_str());
        animationPath->write(out);

        osg::ref_ptr<osg::MatrixTransform>mt=new osg::MatrixTransform();
        mt->setDataVariance(osg::Object::STATIC);
        mt->setMatrix(osg::Matrix::translate(-bs.center())*osg::Matrix::scale(size,size,size)*osg::Matrix::rotate(osg::inDegrees(-180.0f),(0.0f),0.0f,1.0f));
        mt->addChild(ss.get());
       
        osg::ref_ptr<osg::PositionAttitudeTransform>pat=new osg::PositionAttitudeTransform();
        pat->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0f,1.0f));
        pat->addChild(mt);
        const osg::BoundingSphere&ps=pat->getBound();

        root->addChild(pat.get());
        root->addChild(node.get());

        osgUtil::Optimizer op;
        op.optimize(root.get());
        op.optimize(pat.get());


        osg::ref_ptr<osgViewer::CompositeViewer>viewer=new osgViewer::CompositeViewer();

        osg::ref_ptr<osg::GraphicsContext::Traits>ts=new osg::GraphicsContext::Traits();
        ts->x=100;
        ts->y=100;
        ts->width=900;
        ts->height=700;
        ts->windowDecoration=true;
        ts->doubleBuffer=true;
        ts->sharedContext=0;

        osg::ref_ptr<osg::GraphicsContext>gc=osg::GraphicsContext::createGraphicsContext(ts.get());

        if(gc->valid())
        {
                osg::notify(osg::INFO)<<"GraphicsWindow has been createa successfully."<<std::endl;
                gc->setClearColor(osg::Vec4f(0.2f,0.2f,0.6f,1.0f));
                gc->setClearMask(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
        }
        else
        {
                osg::notify(osg::INFO)<<"GraphicsWindow has not been createa successfully."<<std::endl;
        }

//视图一
        {
                osg::ref_ptr<osgViewer::View>view=new osgViewer::View;
                viewer->addView(view.get());

                view->setSceneData(root.get());

                double fovy,aspectRatio,zNear,zFar;
                view->getCamera()->getProjectionMatrixAsPerspective(fovy,aspectRatio,zNear,zFar);
                double newAspectRatio=double(ts->width*2/3)/double(ts->height);
                double aspectRatioChange=newAspectRatio/aspectRatio;
                if(aspectRatioChange!=1.0)
                {
                        view->getCamera()->getProjectionMatrix()*=osg::Matrix::scale(1./aspectRatioChange,1.0,1.0);
                }

                view->getCamera()->setViewport(new osg::Viewport(0,0,ts->width*2/3,ts->height));
                view->getCamera()->setGraphicsContext(gc.get());

                view->setCameraManipulator(new osgGA::TrackballManipulator);

                view->addEventHandler(new osgViewer::StatsHandler);
                view->addEventHandler(new osgViewer::WindowSizeHandler);
                view->addEventHandler(new osgViewer::ThreadingHandler);
                view->addEventHandler(new osgViewer::RecordCameraPathHandler);
        }
//视图二
        {
                osg::ref_ptr<osgViewer::View>view=new osgViewer::View;
                viewer->addView(view.get());
                osg::ref_ptr<osg::Camera>camera=new osg::Camera();
                camera->setViewMatrixAsLookAt(nk.center()+osg::Vec3f(400.0,0.0,0.0),nk.center(),osg::Vec3f(0.0,0.0,-1.0));
                camera->addChild(root.get());

                view->setSceneData(camera);

                double fovy,aspectRatio,zNear,zFar;
                view->getCamera()->getProjectionMatrixAsPerspective(fovy,aspectRatio,zNear,zFar);
                double newAspectRatio=double(ts->width/3)/double(ts->height/2);
                double aspectRatioChange=newAspectRatio/aspectRatio;
                if(aspectRatioChange!=1.0)
                {
                        view->getCamera()->getProjectionMatrix()*=osg::Matrix::scale(1./aspectRatioChange,1.0,1.0);
                }

                view->getCamera()->setViewport(new osg::Viewport(ts->width*2/3,ts->height/2,ts->width/3,ts->height/2));
                view->getCamera()->setGraphicsContext(gc.get());

                view->setCameraManipulator(new osgGA::TrackballManipulator);

        }
//视图三
        {
                osg::ref_ptr<osgViewer::View>view=new osgViewer::View;
                viewer->addView(view.get());
                osg::ref_ptr<osg::Camera>camera=new osg::Camera();
                camera->setViewMatrixAsLookAt(bs.center()+osg::Vec3f(0.0,0.0,2*bs.radius()),bs.center(),osg::Vec3f(0.0,1.0,0.0));
                camera->setProjectionMatrixAsOrtho(-bs.radius(),bs.radius(),-bs.radius(),bs.radius(),2*bs.radius(),300);
                camera->addChild(root.get());

                view->setSceneData(camera);

                double fovy,aspectRatio,zNear,zFar;
                view->getCamera()->getProjectionMatrixAsPerspective(fovy,aspectRatio,zNear,zFar);
                double newAspectRatio=double(ts->width/3)/double(ts->height/2);
                double aspectRatioChange=newAspectRatio/aspectRatio;
                if(aspectRatioChange!=1.0)
                {
                        view->getCamera()->getProjectionMatrix()*=osg::Matrix::scale(1./aspectRatioChange,1.0,1.0);
                }

                view->getCamera()->setViewport(new osg::Viewport(ts->width*2/3,0,ts->width/3,ts->height/2));
                view->getCamera()->setGraphicsContext(gc.get());

                view->setCameraManipulator(new osgGA::TrackballManipulator);

        }
        viewer->realize();
        viewer->run();

        return 0;
}

最近VR访客

FlySky 评论于2010-4-1 09:18:31
camera->setViewMatrixAsLookAt(bs.center()+osg::Vec3f(0.0,0.0,2*bs.radius()),bs.center(),osg::Vec3f(0.0,1.0,0.0));

view->setCameraManipulator(new osgGA::TrackballManipulator);

使用了默认的操作器,同时设置相机位置是无效的,,建议你通过更新回调来处理~~~····

OSG中国官方网站:http://www.osgChina.org
OSG中国官方讨论区:http://bbs.osgChina.org

OSG专业群-OSG地形研究者 49668412
      OSG地形专业研究 45763709

手机版|VR开发网 统计 津ICP备18009691号
网安备12019202000257

GMT+8, 2021-2-25 06:51 AM

返回顶部