|
代码如下- osg::Vec3 v1(0.0,0.0,0.0);
- osg::Vec3 v2(1.0,0.0,0.0);
- osg::Vec3 v3(0.0,1.0,0.0);
- osg::Vec3 v4(0.0,0.0,1.0);
- osg::ref_ptr<osg::Geometry> t1 = new osg::Geometry;
- osg::ref_ptr<osg::Geometry> t2 = new osg::Geometry;
- osg::ref_ptr<osg::Geometry> t3 = new osg::Geometry;
- osg::ref_ptr<osg::Geometry> t4 = new osg::Geometry;
- osg::ref_ptr<osg::Vec3Array> tv1 = new osg::Vec3Array;
- osg::ref_ptr<osg::Vec3Array> tv2 = new osg::Vec3Array;
- osg::ref_ptr<osg::Vec3Array> tv3 = new osg::Vec3Array;
- osg::ref_ptr<osg::Vec3Array> tv4 = new osg::Vec3Array;
- tv1->push_back(v1);tv1->push_back(v2);tv1->push_back(v3);
- tv2->push_back(v1);tv2->push_back(v3);tv2->push_back(v4);
- tv3->push_back(v1);tv3->push_back(v2);tv3->push_back(v4);
- tv4->push_back(v2);tv4->push_back(v3);tv4->push_back(v4);
- t1->setVertexArray(tv1.get());t2->setVertexArray(tv2.get());
- t3->setVertexArray(tv3.get());t4->setVertexArray(tv4.get());
-
- t1->addPrimitiveSet(new osg::DrawArrays(osg::DrawArrays::TRIANGLES,0,3));
- t2->addPrimitiveSet(new osg::DrawArrays(osg::DrawArrays::TRIANGLES,0,3));
- t3->addPrimitiveSet(new osg::DrawArrays(osg::DrawArrays::TRIANGLES,0,3));
- t4->addPrimitiveSet(new osg::DrawArrays(osg::DrawArrays::TRIANGLES,0,3));
- osg::ref_ptr<osg::Geode> trian = new osg::Geode;
- trian->addDrawable(t1);trian->addDrawable(t2);
- trian->addDrawable(t3);trian->addDrawable(t4);
- osgUtil::SmoothingVisitor visitor;
- visitor.smooth(*t1);visitor.smooth(*t2);
- visitor.smooth(*t3);visitor.smooth(*t4);
- osg::ref_ptr<osg::PolygonMode> line = new osg::PolygonMode;
-
- osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
- traits->windowDecoration = true;
- traits->x = 100;
- traits->y = 100;
- traits->width = 800;
- traits->height = 600;
- traits->doubleBuffer = true;
- traits->sharedContext = 0;
- osg::DisplaySettings* ds = osg::DisplaySettings::instance();
- traits->alpha = ds->getMinimumNumAccumAlphaBits();
- traits->stencil = ds->getMinimumNumStencilBits();
- traits->sampleBuffers = ds->getMultiSamples();
- traits->samples = ds->getNumMultiSamples();
-
- osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
-
- gc->setClearColor(osg::Vec4(0.2,0.5,0.0,1.0));
- gc->setClearMask(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
- osg::ref_ptr<osg::Camera> camera = new osg::Camera;
- osg::Vec3 eye(0.0,2.0,2.0);
- osg::Vec3 center(0,-0.5,-0.5);
- osg::Vec3 up(0.0,1.0,0.0);
- osgViewer::Viewer viewer;
- viewer.setSceneData(trian.get());
-
-
- camera->setViewMatrixAsLookAt(eye,center,up);
- camera->setProjectionMatrixAsPerspective(90.0,2.0,4.0,10.0);
-
- camera->setGraphicsContext(gc.get());
- camera->setViewport(new osg::Viewport(10,10,700,500));
- camera->setClearColor(osg::Vec4(0.1,0.1,0.0,1.0));
- CameraEventCallBack* ecb = new CameraEventCallBack;
- //camera->setEventCallback(ecb);
- camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF);
- viewer.setCamera(camera.get());
-
-
-
- viewer.setSceneData(trian.get());
- viewer.setCamera(camera);
-
- return viewer.run();
复制代码 使用上面的代码之后不能显示场景图形,显示效果如图:
显示效果
即使调整窗口和视图的大小也还是不能显示
当去除- camera->setGraphicsContext(gc.get());
复制代码 这句代码后是可以全屏显示四面体的。望指教,谢谢。 |
|