一、要导入的obj文件
二、导入代码
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
void main()
{
osgViewer::Viewer viewer;
{
/// 自定义OSG显示窗口大小
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
traits->x = 40;
traits->y = 40;
traits->width = 600;
traits->height = 480;
traits->windowDecoration = true;
traits->doubleBuffer = true;
traits->sharedContext = 0;
osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
osg::ref_ptr<osg::Camera> camera = new osg::Camera;
camera->setGraphicsContext(gc.get());
camera->setViewport(new osg::Viewport(0, 0, traits->width, traits->height));
GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
camera->setDrawBuffer(buffer);
camera->setReadBuffer(buffer);
// add this slave camera to the viewer, with a shift left of the projection matrix
viewer.addSlave(camera.get());
}
osgDB::Options* a = new osgDB::Options(std::string("noTriStripPolygons"));
osg::ref_ptr<osg::Node> node1 = osgDB::readNodeFile("E:\\vs2022\\obj\\xx.obj", a);
osg::ref_ptr<osg::Node> node2 = osgDB::readNodeFile("E:\\vs2022\\obj\\xx.obj", a);
osg::ref_ptr<osg::Node> node3 = osgDB::readNodeFile("E:\\vs2022\\obj\\xx.obj", a);
osg::ref_ptr<osg::Node> node4 = osgDB::readNodeFile("E:\\vs2022\\obj\\xx.obj");
osg::ref_ptr<osg::Node> node5 = osgDB::readNodeFile("E:\\vs2022\\obj\\xx.obj");
osg::ref_ptr<osg::Node> node6 = osgDB::readNodeFile("E:\\vs2022\\obj\\xx.obj");
osg::Group* root = new osg::Group;
root->addChild(node1);
root->addChild(node2);
root->addChild(node3);
root->addChild(node4);
root->addChild(node5);
root->addChild(node6);
viewer.setSceneData(root);
viewer.realize();
viewer.run();
}
三、效果图
四、在多个obj文件的情况下平移
osgDB::Options* a = new osgDB::Options(std::string("noTriStripPolygons"));
osg::ref_ptr<osg::Node> node1 = osgDB::readNodeFile("E:\\vs2022\\obj\\xxx.obj", a);
osg::MatrixTransform* t = new osg::MatrixTransform;
t->setMatrix(osg::Matrix::translate(osg::Vec3d(1, 0, 0)));
t->addChild(node1);
osg::Group* root = new osg::Group;
root->addChild(node1);
root->addChild(t);
viewer.setSceneData(root);
viewer.realize();
viewer.run();
-
效果图