OSG开发笔记(二十九):OSG加载模型文件、加载3DMax三维型文件Demo


若该文为原创文章,未经允许不得转载
本文章博客地址:https://blog.csdn.net/qq21497936/article/details/134064988
各位读者,知识无穷而人力有穷,要么改需求,要么找专业人士,要么自己研究

红胖子(红模仿)的博文大全:开发技术集合(包含Qt实用技术、树莓派、三维、OpenCV、OpenGL、ffmpeg、OSG、单片机、软硬结合等等)持续更新中…

OSG开发专栏(点击传送门)

上一篇:《OSG开发笔记(二十八):OSG模型固定路径动画
下一篇: 持续补充中…



前言

  Osg深入之后需要打开模型文件,这些模型文件是已有的模型文件,加载入osg之后可以在常见中展示模型文件,该节点可以操作,多个逼真的模型的节点就实现了基本的场景构建。


Demo

  Please add image description


说明

  三维模型文件一般是由专业的三维建模人员完成,可以去buy通用模型,但是定制模型的费用就相对不便宜了,几十几百几千几万的都有,三维项目一定要了解清楚前期建模,也有很多是由甲方自行提供,因为只有甲方才能知道模型效果是什么样。


Osg读取文件库:OsgDB库

  osg、osgEarth所有文件都是通过osgDB库来读取,通过Registry来查找文件拓展名对应的osg库,根据一定规则拼接成完成的osg库名并加载,通过ReaderWriter对象来完成节点的读取(ReaderWriter是读写节点的基类,可通过派生此类重写读写方法实现自己的读写格式)。
  Insert image description here

  然后,查看我们的osg库名称:
  Insert image description here

  发现支持的格式还真不少,然后我们进行大致归类:

三维格式

.3dc

  3DC文件是三维建模软件3DCrafter建的三维场景,用于存储和加载3D模型和场景。3DC文件包括场景的几何形状、形状、材质和灯光效果,用于创建3 图像和动画。

.3ds

  3DS(3D Studio)文件是一种用于 3D 图形模型的文件格式,它是 Autodesk 3ds Max 软件的主要文件格式之一。是一种常见的 3D 模型文件格式,用于存储和传输 3D 对象、材质、动画和变形等数据。可以被许多 3D 图形软件读取和编辑。它广泛应用于游戏开发、动画制作、虚拟现实等领域。

.bvh

  BVH文件包含角色的骨骼和肢体关节旋转数据。BVH 是一种通用的人体特征动画文件格式,,广泛地被当今流行的各种动画制作软件支持。通常可从记录人类行为运动的运动捕获硬件获得。

.Ive

  OpenSceneGraph 二进制三维文件。

.lwo

  由LightWave 3D创建的三维对象文件,该程序用于三维建模、动画和渲染;包含描述对象形状和外观的点、多边形和曲面;还可以包含对用于对象纹理的图像文件的引用。

.lws

  LightWave 3D Scene File 文件是最常用的文件类型,带有 LWS 文件扩展名,最初由 NewTek开发LightWave 3D。

.osc

  当前这种OSC格式是一种GIS地理信息文件,是由OpenStreetMap研发,作为一种 OpenStreetMap更改文件使用,此文件流行程度是 50 分(0-100满分)。

.osg(osgb)

  OSG场景数据格式,可以记录场景书结构的所有节点及其属性信息。它属于文本文件类型,不需要专门的编辑器。osgb是osg的二进制格式,osgb是倾斜摄影的格式,osg可以直接读取osgb文件。

.osga

  osgArchive 数据归档器 为众多 OSG 文件以及 IVE 文件打包提供了一个工具 它所生成的 OSGA 文件是整个文件,可以为海量数据的存放提供良好的支持

.md2

  MD2是Quake2中使用的模型文件格式,由于其比较简单,容易实现,所以应用很广,是一种经典的动画模型格式。该文件格式由2部分组成:一部分是文件头,包含了文件ID号、版本号和有关模型的各种数据的起始地址等;另一部分是文件的主体,包含了有关模型的各种数据,如顶点数据、纹理数据、法向量数据等。

.obj

  obj可以指一种3D模型格式的文件,也可以指一种程序编译中间代码文件。obj3D模型文件是一种文本文件,可以直接用写字板打开进行查看和编辑修改,里面不包含动画、材质特性、贴图路径、动力学、粒子等信息,但是可包含纹理信息,而对纹理进行轻量化压缩可以减小文件大小和提高加载性能。。obj目标文件一般是程序编译后的二进制文件,再通过链接器和资源文件链接就成可执行文件了。

.openflight(FLT)

  FLY (OpenFlight) 格式(通常使用 .flt 文件扩展名)是一种用于表示三维场景和模型的文件格式。OpenFlight 最初由Multigen-Paradigm公司开发,用于航空模拟和虚拟环境的创建。后来,这个格式被移交给了Presagis公司继续开发和维护。

.shp

  shp是“空间数据开放”格式的文件。shp全称“ESRI Shapefile”,是美国环境系统研究所公司开发的一种空间数据开放格式,是一种矢量图形格式,该格式文件主要用于描述几何体对象(点,折线与多边形),能够保存几何图形的位置及相关属性。

.stl

  3d打印一般用stl格式的3d文件,3d打印机识别STL格式的文件

dxf

  DXF(Drawing Exchange Format)是一种用于图形交换的文件格式,由AutoDesk公司开发。DXF文件可以包含2D或3D图形数据,可以被多个CAD程序所支持。

图像格式

.bmp

  BMP是英文Bitmap(位图)的简写,它是Windows操作系统中的标准图像文件格式,能够被多种Windows应用程序所支持。

.dds

  DDS是一种图片格式,是DirectDraw Surface的缩写,它是DirectX纹理压缩(DirectX Texture Compression,简称DXTC)的产物。由NVIDIA公司开发。大部分3D游戏引擎都可以使用DDS格式的图片用作贴图,也可以制作法线贴图。通过安装DDS插件后可以在

.jpeg

  JPEG格式是最常用的图像文件格式,后缀名为.jpg或.jpeg。

.tga

  tga是由美国Truevision公司为其显示卡开发的一种图像文件格式,

.pic

  pic是一种图像文件格式,以图像文件格式是记录和存储影像信息的格式。对数字图像进行存储、处理、传播,必须采用一定的图像格式,也就是把图像的像素按照一定的方式进行组织和存储,把图像数据存储成文件就得到图像文件。

.rgb

  .rgb文件是SGI图像文件格式的24位RGB彩色位图图像文件。

.png

  PNG,全称Portable Network Graphics,是一种无损压缩的图像文件格式,广泛应用于互联网上的图像文件。

.pnm

  PNM(Portable Any Map)格式是一系列用于便携式图像的图像文件格式,这些图像是像素值的映射,可以表示灰度或彩色数据。

其他

.dot

  DOT file extension information is a Microsoft Word document template.

.dw

  DW format file is a unique file of Dreamweaver. It usually includes HTML code and web page-related resources (such as images, Flash, JavaScript, CSS, etc.).

.ktx

  KTX (Khronos Texture) is a lightweight texture container for OpenGL®, Vulkan® and other GPU APIs. The KTX file contains all parameters required for texture loading.

.two

  Lua is a concise, lightweight and extensible scripting language. The language is designed to be embedded in applications to provide flexible expansion and customization functions for applications.

.mdl

  MDL file is a file package that contains models, textures, all actions, and scripts.

.osgjs

  Osgjs is a format supported by the osjjs framework. It is actually a text type in json format. To convert the model you built into this format, you must first convert the 3ds format to osg format, and then convert it to json format in osg.

.p3d

  Complete file in the panda3D packaging system; used for packaging applications for distribution; similar to .MF files, but specifically used to save Panda3D applications and no other items; holds everything needed to run the application, including: Python code , models, textures, and compiled .DLL files.

.ply

  PLY is a computer file format, whose full name is Polygon File Format or Stanford Triangle Format.

. throw

  POV file extension is a POV-Ray Raytracing Format developed by The POV-Team file type for the Pov-Ray software program.

.pvr

  PVR file extension comes from POWERVR Texture File. The development of this document was completed by Imagination Technologies. This file belongs to binary format and is a raster-related file.

.rot

  The ˆrot file extension is a Homeworld 2 Graphics File file originally developed for Homeworld 2 by Sierra Entertainment. Annoymous user data statistics infer that ROT files are most popular among users in China and using Windows 10 devices. Most of these users are running the Google Chrome web browser.

.tf

  This format is a file format similar to DOC format. tf is commonly known as rich text format, also called multi-text format, which is commonly used to store document and image information. TF format documents created under different operating systems can be viewed and transferred between other operating systems and application software, which is very convenient.

.txf

  TXF is a font file format used to save tax data such as income and expenses in a standard text-based format. It can be used in font files for Lustia, universe simulation and visualization programs, as well as translation files created by language translation and localization programs.

.txp

  TXP is an editing and browsing format. The simplest example is to store novels on the Internet to mobile devices (such as mobile phones, MP4, etc.). In some cases, the original text must be converted into .TXP

.vtf

  VTF (Valve Textures File) Valve texture file is an image format originally created by VALVE and evolved from the TGA format. Its advantage is that it can well reflect and display the channel effects of TGA.

.x

  This is a proprietary format of DirectX.


Custom file plug-in

  Custom file plug-ins mainly customize a plug-in reading and writing class, inherit the osgDB::ReaderWriter class, and then rewrite functions and methods such as readNode according to needs.
  When establishing the plug-in reading and writing class, please note:
  A dll project needs to be created, and the output dll must be osgdb_extension.dll or osgdb_extension. In order to implement plug-in registration in the form of d.dll, a global variable needs to be defined. The method is as follows REGISTER_OSGPLUGIN(VR, ReaderWriterVR). During the initialization process of the global variable, the Registery::addReaderWriter function will be used to automatically register the extension corresponding to the plug-in.
  When using the application, you need to register the plug-in. The method is as follows osgDB::Registry::instance->addFileExtensionAlias(“VR”, “VR”)
   In fact, we do not need to make plug-ins to make engines. It is better to directly convert the 3D model file format.


Test loading model file

  The obj format is most suitable, but it does not come with texture formats and other texture formats, so it also needs to be paired with a texture format.
  Insert image description here

Step 1: Load obj

  The obj file was loaded, but there was no texture. When I opened the obj, I found that a xxx.mtl of mtllib was needed as a texture file, but the model I got did not have it.
  Insert image description here

  mtllib: This prefix specifies the file path of the material library file (*.mtl) used by this OBJ file.

Step 2: Load 3ds files

  The 3ds file was loaded, and it was indeed displayed. The effect was pretty good. I tested the drag-and-zoom function and the effect was perfect and smooth, meeting expectations.
  Insert image description here


key code

Initialize scene code

osg::ref_ptr<osg::Node> OsgWidget::getModel3DMaxFile()
{
    
    
    // 隐藏整个demo全局的按钮面板
    ui->groupBox_pannel->setVisible(false);
    ui->label_cursor->setVisible(false);
    ui->label_cursor_2->setVisible(false);
    ui->label_msg->setVisible(false);
    ui->label_state->setVisible(false);

    // 正式开始
    osg::ref_ptr<osg::Group> pGroup = new osg::Group;
    osg::ref_ptr<osg::Camera> pCamera = 0;
    osg::ref_ptr<osg::Geode> pGeode = 0;
    osg::ref_ptr<osg::Node> pNode = 0;

    // 添加模型
    {
    
    
//        osgDB::Options* a = new osgDB::Options(std::string("noTriStripPolygons"));
//        osg::ref_ptr<osg::Node> node1 = osgDB::readNodeFile("D:/qtProject/osgDemo/modelData/grass.obj", a);
//        osg::ref_ptr<osg::Node> node1 = osgDB::readNodeFile("D:/qtProject/osgDemo/modelData/grass.obj");
        pNode = osgDB::readNodeFile("D:/qtProject/osgDemo/modelData/grass.3ds");
    }
    // 创建背景相机(相机需要配合几何题图,可以理解为相机是最后得,相机中有一个全屏的图,这个图是个元素节点,这样图节点在相机中就成为了背景图)
    {
    
    
        // 步骤一:创建相机
        pCamera = new osg::Camera();
        // 步骤二:设置矩阵  显示得界面边坐标      左    右  下    上
        pCamera->setProjectionMatrixAsOrtho2D(0, 1920, 0, 1080);
        // 步骤三:设置视图矩阵
        pCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
        // 步骤四:不受父类矩阵影响
        pCamera->setViewMatrix(osg::Matrix::identity());
        // 步骤五:清除深度缓存
        pCamera->setClearMask(GL_DEPTH_BUFFER_BIT);
        // 步骤六:设置为不接受事件,让其得不到焦点
        pCamera->setAllowEventFocus(false);
        // 步骤七:设置渲染顺序
//        pCamera->setRenderOrder(osg::Camera::PRE_RENDER);       // 不显示了
        pCamera->setRenderOrder(osg::Camera::NESTED_RENDER);    // 显示为背景HUD
//        pCamera->setRenderOrder(osg::Camera::POST_RENDER);      // 显示为前景HUD
        // 步骤八:关闭光照,通过osg::StateSet设置
        pGeode = new osg::Geode();
        osg::ref_ptr<osg::StateSet> pStateSet = pGeode->getOrCreateStateSet();
        pStateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
        // 步骤九:关闭深度测试
        pStateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
    }

    // 创建铺满窗口的方形(窗口形状),并贴背景纹理图
    {
    
    
        // 步骤一:创建几何信息对象
        osg::ref_ptr<osg::Geometry> pGeometry = new osg::Geometry;
        // 步骤二:绑定顶点
        osg::ref_ptr<osg::Vec3Array> pVec3Array = new osg::Vec3Array;
        pGeometry->setVertexArray(pVec3Array);
#if 0
        pVec3Array->push_back(osg::Vec3( 300.0, 100.0, 0.0));
        pVec3Array->push_back(osg::Vec3( 600.0, 100.0, 0.0));
        pVec3Array->push_back(osg::Vec3( 600.0, 200.0, 0.0));
        pVec3Array->push_back(osg::Vec3( 300.0, 200.0, 0.0));
#else
        pVec3Array->push_back(osg::Vec3(   0,    0, 0));
        pVec3Array->push_back(osg::Vec3(1920,    0, 0));
        pVec3Array->push_back(osg::Vec3(1920, 1080, 0));
        pVec3Array->push_back(osg::Vec3(   0, 1080, 0));
#endif
        // 步骤三:读取图片(用于贴纹理)
        osg::ref_ptr<osg::Image> pImage = new osg::Image;
        pImage = osgDB::readImageFile(QString("D:/qtProject/osgDemo/osgDemo/modules/osgWidget/image/background2.png").toStdString());
        if(!pImage->valid())
        {
    
    
            LOG_WARN(QString("Failed to open image file: %1").arg(QString::fromStdString(pImage->getFileName())));
            return 0;

        }
        // 步骤四:创建纹理
        osg::ref_ptr<osg::Texture2D> pTexture2D = new osg::Texture2D();
        pTexture2D->setImage(pImage);
        pTexture2D->setUnRefImageDataAfterApply(true);
        // 步骤五:绑定纹理映射顶点
        osg::ref_ptr<osg::Vec2Array> pVec2Array = new osg::Vec2Array();
        pGeometry->setTexCoordArray(0, pVec2Array.get());
        pVec2Array->push_back(osg::Vec2(0.0, 0.0));
        pVec2Array->push_back(osg::Vec2(1.0, 0.0));
        pVec2Array->push_back(osg::Vec2(1.0, 1.0));
        pVec2Array->push_back(osg::Vec2(0.0, 1.0));
        // 步骤六:设置纹理环境
//        osg::ref_ptr<osg::TexEnv> pTexEnv=new osg::TexEnv;
//        pTexEnv->setMode(osg::TexEnv::Mode::BLEND);
//        pTexEnv->setColor(osg::Vec4(1.0,0.0,0.0,1.0));
        // 步骤七:关联纹理状态(包括纹理和纹理环境),通过osg::StateSet
        osg::ref_ptr<osg::StateSet> pStateSet = pGeometry->getOrCreateStateSet();
        pStateSet->setTextureAttributeAndModes(0, pTexture2D.get(), osg::StateAttribute::ON);
//        pStateSet->setTextureAttributeAndModes(0, pTexEnv.get(), osg::StateAttribute::ON);
        // 步骤八:为唯一的法线创建一个数组    法线: normal
        osg::ref_ptr<osg::Vec3Array> pVec3ArrayNormal = new osg::Vec3Array;
        pGeometry->setNormalArray(pVec3ArrayNormal.get());
        pGeometry->setNormalBinding(osg::Geometry::BIND_OVERALL);
        pVec3ArrayNormal->push_back(osg::Vec3(0.0, 1.0, 0.0));
        // 步骤九:由保存的数据绘制四个顶点的多边形
        pGeometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, 4));
        // 步骤十:将图形添加进几何节点
        pGeode->addDrawable(pGeometry.get());

    }
    pCamera->addChild(pGeode);  // 几何背景贴图为底图
    pGroup->addChild(pCamera);
    pGroup->addChild(pNode);    // 插入草地
//    pGroup->addChild(pGeode);   // 几何背景又插入一次中间作为元素
    // 设置漫游器,并禁止拽拖自动旋转
    osg::ref_ptr<osgGA::TrackballManipulator> pManipulator = new osgGA::TrackballManipulator;
    pManipulator->setAllowThrow(false);

    return pGroup.get();
} 

Project template: corresponding version number v1.31.0

  (There are a few version numbers in the middle that are for the prophecy project, we will come back to versions 26~30 later)
  Insert image description here


Previous article: "OSG Development Notes (Twenty-Eight): OSG Model Fixed Path Animation"
Next article: Continuously adding...


Blog address of this article:https://blog.csdn.net/qq21497936/article/details/134064988

Guess you like

Origin blog.csdn.net/qq21497936/article/details/134064988