Modern design assimp 3D model opengl to load the library

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/leon_zeng0/article/details/88860645

This article describes the downloading and compiling assimp libraries, to prepare our complex 3D display with opengl. Description section in front of the model library reference  https://learnopengl-cn.github.io/  .

All the scenes so far, we have been abusing our friend boxes, but over time even our best friends will be bored. In the daily graphics program, they usually use a very complicated model and fun, they want to look good more than a static box. However, different objects and boxes, we are not able to manually define complex shapes such pairs such as a house, car or humanoid characters all vertices, normals and texture coordinates. What we want is these models (Model) import (Import) to program them. 3D models are usually by artists in Blender , 3DS Max or Maya elaborate such a tool.

These so-called 3D modeling tool (3D Modeling Tool) allows the artist to create complex shapes, and use means called UV mapping (uv-mapping) to apply a decal. These tools will automatically generate all the vertex coordinates, vertex normals and texture coordinates when exporting to the model file. This way even under the artists do not understand the technical details of the graphics, can also have a powerful set of tools to build a high-quality model. All technical details are hidden in the exported model file. However, as the graphics developers, we need to understand the details of these technologies.

So, our job is to parse these models exported file and extract all useful information, they were stored in the format of OpenGL can understand. A very common problem is that there are a variety of file formats models, each of which will be in their own way to export the model data. Such Wavefront .obj the model format such that the model contains only the data and material information, such as the color model and the diffuse reflection / specular light map. The XML-based Collada file format is very rich, containing the model, lighting, a variety of materials, animation data, video camera, full scene information, and so on. Wavefront .obj format is generally considered to be a model for easily parsed format. At least to look at the proposals on Wavefront wiki page on how information is packaged file format. This should make you realize that the basic structure of the model file.

All in all different kinds of file formats there are many, among them usually do not have a common structure. So if we want to import models from these file formats, we must go to write an import filter for each file format to be imported. Fortunately, just have a library devoted to this issue.

Model load library

A very popular model import library is Assimp , it is the Open Asset Import Library abbreviation (open resource import bank). Assimp can be introduced into a wide variety of models of different file formats (format and can be derived moiety), which keeps all the model data loaded in the generic data structure Assimp. When Assimp finished loading model, we will be able to extract all the data we need from the data structure Assimp in. Since the data structure Assimp remains the same, no matter what kind of import file format, it can be that we abstract from these different file formats out, we need to use to access the data in the same way.

When using a model Assimp introduced, it will usually loaded into a model of the entire scene (Scene) object that contains all the imported data model / scene. Assimp scene will load a series of nodes (the Node), each node contains the index data stored in the object scene, each node can have any number of child nodes. (Simplified) model data structure follows Assimp

 

  • And materials and grids (the Mesh), as all of the scene / model data contained in the Scene object. Scene object also contains a reference to a scene root node.
  • Scene Root Node (root) may contain child node (and, like the other nodes), it will have an index number of the scene objects pointing to the array mMeshes mesh data stored. SCENE mMeshes array stored real Mesh object stored in the array of nodes only mMeshes scene index grid array.
  • A Mesh object itself contains all relevant data required for rendering, such as vertex positions, normals, texture coordinates, surface (Face) and the material of the object.
  • A grid comprising a plurality of surfaces. Face represent the rendering of the object element (Primitive) (triangles, squares, dot). A plane including the indices of the vertices of primitives composition. Since the vertex and index are separated using a buffer to render index is very simple (see Hello, triangle ).
  • Finally, a grid also includes a Material object that contains a number of functions allows us to obtain the material properties of the object, such as the color and texture map (such as diffuse and specular light map).

Therefore, the first thing we need to do is an object loaded into the Scene object, traversing a node, acquires the corresponding Mesh object (we recursively search every sub-node of the node), and process each Mesh object to obtain the vertices data, index, and its material properties. The end result is a series of grid data, we will include them in one Modelobject.

Construction of Assimp

1: Download the source code assimp

Can go to   http://assimp.org/index.php/downloads  select the version you want to download, I chose the latest, guide me to   https://github.com/assimp/assimp/releases/tag/v4.1.0 /

2: Installation boost

assimp is dependent boost library, if the library does not boost function can only compile a limited version.

boost official link: www.boost.org

On my system visual studio 2010,2017, then which version should I use boost, can, I will now download the latest release of 1.69.

assimp did not use the boost needed to compile parts, it is boost only need to download the unzip it.

3: assimp view dependent directx sdk

If you need to compile assimp view, you need to install directx sdk.

You can get from here to download the SDK.

Click the exe file to install it. After installation, restart at least if you do not need to log off, directx sdk environment configuration will work.

4: Install cmake

cmake official link: www.cmake.org

Run cmake

 Select assimp source directory, the directory compiled binary files can first set up, can then enter, I copy the source directory, add the bin or bin7 behind. Then Add Entry, add BOOST_ROOT variable, type the election STRING, value added boost of directory (this step can also do, compile the assimp may be limited functionality). After the addition effect is as follows:

Point Config, the first time point will ask you to choose which version of the compiler.

Once again points Config, and then point Generate, produces good works. Then Open project, compile and link (build).

It takes some time, for me, visual studio 2010 compiler always results:

========== Rebuild All: 5 succeeded, 4 failed, 6 skipped ==========

That is unsuccessful.

But visual studio 2017 is a success. Since there are successful on the use to say.

5: assimp use

  • Use the default configuration built Assimp is a dynamic library (Dynamic Library), so we need to include the generated assimp.dll binary files and programs. You can simply copy the DLL to the same directory as the executable file in our program.
  • After Assimp compiled, generated and DLL files located lib library code / Debug or code / Release folder.
  • Then copy the compiled LIB and DLL files to the appropriate directory project and link them in the solution. And remember to Assimp header files are also copied to your include directory (the header file can be downloaded from the Assimp include find directory). I copied assimp directory under the original include directory after unzip it into my include directory, while the assimp directory under the include directory compiled binaries in the config.h also copied.

Well these preparations, you can import a variety of 3D files with assimp library.

6: c ++ application simple example

Establishing a  Assimp :: Importer  instance of the class, or do set, then calls L  Assimp :: Importer :: the ReadFile () . File can be read and processed data, processed data on  aiScene  the object's class pointer. You can extract the required data. importer of all resource management itself. Its disappearance, it also released all of his resources. So the easiest way is to establish a local example, after you use the results, so naturally he goes out of scope range.

Examples of C ++ is as follows:

#include <assimp/Importer.hpp>      // C++ importer interface
#include <assimp/scene.h>           // Output data structure
#include <assimp/postprocess.h>     // Post processing flags
bool DoTheImportThing( const std::string& pFile)
{
  // Create an instance of the Importer class
  Assimp::Importer importer;
  // And have it read the given file with some example postprocessing
  // Usually - if speed is not the most important aspect for you - you'll 
  // propably to request more postprocessing than we do in this example.
  const aiScene* scene = importer.ReadFile( pFile, 
        aiProcess_CalcTangentSpace       | 
        aiProcess_Triangulate            |
        aiProcess_JoinIdenticalVertices  |
        aiProcess_SortByPType);
  
  // If the import failed, report it
  if( !scene)
  {
    DoTheErrorLogging( importer.GetErrorString());
    return false;
  }
  // Now we can access the file's contents. 
  DoTheSceneProcessing( scene);
  // We're done. Everything will be cleaned up by the importer destructor
  return true;
}

 

Guess you like

Origin blog.csdn.net/leon_zeng0/article/details/88860645