|
这是什么问题照着视频讲解的一样代码结果偏差太大
请问这是环境配置的有恩题吗,运行源代码如下:#include"../Common/Common.h"
#include<osgViewer\Viewer>
#include<osgDB\ReadFile>
#include<osgDB\Registry>
#include<osg\Geode>
#include<osg\Geometry>
#include<osg\Camera>
#include<osg\Texture2D>
#include<iostream>
#include<osg\Image>
void CreateTexture(osgViewer::Viewer *viewer)
{
if(!viewer)
{
std::cout <<"Viewer is NULL"<<std::endl;
return ;
}
//
/*osg::GraphicsContext ::WindowingSystemInterface *wsi=osg::GraphicsContext ::getWindowingSystemInterface ();
if(!wsi)
{
std::cout <<"windowing error"<<std::endl;
return ;
}
unsigned int width,height;
wsi->getScreenResolution (osg::GraphicsContext ::ScreenIdentifier (0),width,height);*/
osg::ref_ptr<osg::GraphicsContext::Traits>traits=new osg::GraphicsContext ::Traits ;
traits->x=0;
traits->y=0;
traits->width=800;
traits->height =600;
traits->windowDecoration=false;
traits->doubleBuffer =true;
traits->sharedContext =0;
osg::ref_ptr<osg::GraphicsContext >gc=osg::GraphicsContext ::createGraphicsContext (traits);
if(!gc.valid())
{
std::cout<<"error"<<std::endl;
return ;
}
osg::ref_ptr<osg::Camera>master=new osg::Camera;
master->setGraphicsContext (gc);
master->setViewport (0,0,800,600);
viewer->addSlave(master.get());
osg::ref_ptr <osg::Camera>rttCamera=new osg::Camera;
rttCamera->setRenderOrder (osg::Camera :RE_RENDER);
rttCamera->setGraphicsContext (gc);
rttCamera->setViewport (0,0,800,600);
rttCamera->addChild (osgDB::readNodeFile ("ceep.ive"));
viewer->addSlave (rttCamera,false);
osg::ref_ptr<osg::Group>gp=new osg::Group;
osg::ref_ptr <osg::Geode>gnode=new osg::Geode;
gp->addChild (gnode);
osg::ref_ptr<osg::Geometry >gm=new osg::Geometry ;
gnode->addDrawable (gm);//加入几何体
//压入顶点
osg::ref_ptr<osg::Vec3Array >vertex=new osg::Vec3Array ;
vertex->push_back (osg::Vec3(-400,100,-300));
vertex->push_back (osg::Vec3(400,100,-300));
vertex->push_back (osg::Vec3(400,100,300));
vertex->push_back (osg::Vec3(-400,100,300));
gm->setVertexArray (vertex);
//压入法线
osg::ref_ptr<osg::Vec3Array >norm1=new osg::Vec3Array ;
norm1->push_back (osg::Vec3(0,-1.0,0));
gm->setNormalArray (norm1);
gm->setNormalBinding (osg::Geometry ::BIND_OVERALL );//设置法线的绑定方式
//纹理坐标
osg::ref_ptr<osg::Vec2Array >coord=new osg::Vec2Array ;
coord->push_back (osg::Vec2(0.0,0.0));
coord->push_back (osg::Vec2(1.0,0.0));
coord->push_back (osg::Vec2(1.0,1.0));
coord->push_back (osg::Vec2(0.0,1.0));
gm->setTexCoordArray (0,coord);
gm->addPrimitiveSet (new osg:rawArrays (osg::PrimitiveSet :UADS,0,4));
gm->getOrCreateStateSet ()->setMode (GL_LIGHTING,osg::StateAttribute ::OFF );//灯光关掉
//gm->getOrCreateStateSet ()->setMode (GL_BLEND,osg::StateAttribute ::ON );//打开透明
//创建一个2D的纹理
osg::ref_ptr<osg::Texture2D >t2d=new osg::Texture2D ;
t2d->setInternalFormat (GL_RGBA);
//viewer->getCamera ()->attach (osg::Camera::COLOR_BUFFER,t2d);
rttCamera->attach (osg::Camera::COLOR_BUFFER ,t2d);
//读取纹理
/*osg::ref_ptr<osg::Image>image=osgDB::readImageFile ("Images/lz.rgb");
if(!image)
{
std::cout <<"Could not read this image"<<std::endl;
return ;
}
t2d->setImage (0,image);*/
gm->getOrCreateStateSet ()->setTextureAttributeAndModes (0,t2d,osg::StateAttribute ::ON);
gp->addChild (osgDB::readNodeFile ("ceep.ive"));
viewer->setSceneData (gp);
return ;
}
int main()
{
osg::ref_ptr<osgViewer::Viewer>viewer=new osgViewer::Viewer;
CreateTexture(viewer);
viewer->run();
return viewer->run();
}
|
|