查看: 1075|回复: 0

关于相机的若干问题

[复制链接]

该用户从未签到

发表于 2015-7-29 21:14:06 | 显示全部楼层 |阅读模式
最近在学习osg,其中一个比较重要的环节是camera,为什么我下面的代码物体没有被渲染出来。
#include "../Common/Common.h"
#include <Windows.h>

#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/stateSetManipulator>
#include <osgGA/GUIEventHandler>

#ifdef _DEBUG
#pragma comment(lib,"../Debug/Common.lib")
#else
#pragma comment(lib,"../Release/Common.lib")
#endif

osg::ref_ptr<osg::Geode> createQuads()
{
        osg::ref_ptr<osg::Geode> geod = new osg::Geode;
        osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;

        osg::ref_ptr<osg::Vec3Array> vertexs = new osg::Vec3Array;
        vertexs->push_back(osg::Vec3(-0.5,-0.5,0.0));
        vertexs->push_back(osg::Vec3(0.5,-0.5,0.0));
        vertexs->push_back(osg::Vec3(0.5,0.5,0.0));
        vertexs->push_back(osg::Vec3(-0.5,0.5,0.0));
        vertexs->push_back(osg::Vec3(-0.5,0.0,-0.5));
        vertexs->push_back(osg::Vec3(0.5,0.0,-0.5));
        vertexs->push_back(osg::Vec3(0.5,0.0,0.5));
        vertexs->push_back(osg::Vec3(-0.5,0.0,0.5));
        geom->setVertexArray(vertexs);

        osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
        colors->push_back(osg::Vec4(1.0,0.0,0.0,1.0));
        colors->push_back(osg::Vec4(0.0,1.0,0.0,1.0));
        colors->push_back(osg::Vec4(0.0,0.0,1.0,1.0));
        colors->push_back(osg::Vec4(0.5,0.5,0.5,1.0));
        colors->push_back(osg::Vec4(1.0,0.0,0.0,1.0));
        colors->push_back(osg::Vec4(0.0,1.0,0.0,1.0));
        colors->push_back(osg::Vec4(0.0,0.0,1.0,1.0));
        colors->push_back(osg::Vec4(0.5,0.5,0.5,1.0));
        geom->setColorArray(colors);
        geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);

        geod->addDrawable(geom);
        geom->addPrimitiveSet(new osg:rawArrays(osg:rimitiveSet:UADS,0,8));
       

        return geod;
}

int main()
{
        osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
        osg::ref_ptr<osg::Group> gp = new osg::Group;
        osg::ref_ptr<osg::Node> ndgld = osgDB::readNodeFile("glider.osg");
        osg::ref_ptr<osg::Node> ndcow = osgDB::readNodeFile("cow.osg");

        //if( camera0 == NULL )
        //        std::cout<<"master camera is NULL"<<std::endl;

        gp->addChild(createQuads());
       
        viewer->addEventHandler(new osgViewer::WindowSizeHandler);

        viewer->getCamera()->setViewport(0.0,0.0,400,300);
        viewer->getCamera()->setViewMatrixAsLookAt(osg::Vec3(0.0,0.0,100.0),osg::Vec3(0.0,0.0,0.0),osg::Vec3(0.0,1.0,0.0));
        viewer->getCamera()->setProjectionMatrixAsOrtho(-4.0,4.0,-3.0,3.0,0,200.0);
        viewer->getCamera()->setClearColor(osg::Vec4(0.5,0.5,0.5,1.0));
        viewer->getCamera()->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


       
        viewer->setSceneData(gp);
        viewer->setCameraManipulator(NULL);
       
        //viewer->run();
        while( !(viewer->done()) ){
                viewer->frame();
        }

        return 0;
}

我这里有几个问题:
1、相机可以作为一个节点,当其作为节点使用的时候,设置它的投影矩阵、视图变换矩阵还有没有效果?
2、每个相机都会有若干渲染buffer,比如frame_buffer、frame_buffer_object等,这些buffer是不是都是有效的,在我实例化一个相机的时候,我的理解是这些buffer都会创建,但是只有在相机选入gc后,其frame_buffer才会映射到显示窗口上,不知道对不对?
3、viewer如果直接运行run是会帮我们创建CameraManipulitor这个东西,如果这个东西被创建,是不是我们主相机的一些矩阵的设置就会无效,我试了一下确实是这样,不晓得原理是什么?
4、viewer中的主相机是什么意思,一个viewer是不是只有一个主相机,那setCamer就是设置这个主相机,那addSlave(,true)是不是也是设置这个主相机,他们两个的作用是不是一致的?
5、viewer当中的主相机和从相机是不是共享frame_buffer?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

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

联系我们

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