Reading and displaying 3DS files in OpenGL

Source: Internet
Author: User

Reading and displaying 3DS files in OpenGL

After learning blender, I really want to load the model I designed into my own program. Therefore, I considered the common 3DS file format. This time I tried to load the 3DS file into my own OpenGL program and display it.


Demo program: here

Source code: here

First, I found a lot of examples of loading the 3DS format out of the box and found that only one program can run, so I took the source code of this program for research. At the same time, I checked some foreign format instructions, combined with the data organization in outliner in blender, and started to understand the 3DS format organization method.

3DS files are binary files and are in the Autodesk private format. Many compatible software reads data based on known cracked content. Therefore, different software may have different reading effects, experts recommend that you only export the mesh model to 3DS because the light source and camera cannot be saved.

The 3DS file contains a structure called "chunk", which is used to describe various objects in 3D scenarios. In fact, the block mentioned here is similar to the block in the wav file, and both contain header and content ). The block header in 3DS contains the ID (two bytes) and length (four bytes). The length is the whole length of the block. The content length of a block is the length of the entire block minus 6 bytes.

For example, the ID of the primary block is 0x4d4d. We can do this: read two bytes of the block ID and then read the length of the Four-byte block (set to length ), in this way, we know that we need to read at most (length-
6) bytes.

In 3DS, the data organization structure is tree-like, and each node in the tree is a block structure. Because of the similarity between different layers of the tree, the tree traversal knowledge is based on the data structure, we can write deep-priority search algorithms and use recursive algorithms.

The leaf node in the 3DS tree usually has a specific resolution method. For example, if we read the vertex (ID is 0x4110), we need to first read the number of vertices in two bytes N, and then read the XYZ coordinates of each vertex N times.

The coordinate system in the 3DS file is different from that in OpenGL and direct3d. the XY axis determines the horizontal plane, and the Z axis is vertical. At the early stage of resolution, I used to replace the XYZ values of all its vertices by switching and de-inverse, but a model matrix needs to be resolved later, if you only replace the coordinate axes instead of the model matrix, the parsed model may be misplaced. This can be very troublesome. Even if you can achieve the goal by modifying the first 3 × 3 parts of the model matrix, I still think it would be better to convert the coordinate system of the model to the left-hand Coordinate System of direct3d or the right-hand Coordinate System of OpenGL During modeling, and avoid unnecessary conversions.

After parsing the 3DS file, you need to display it. Unfortunately, the 3DS exported by my blender currently does not contain lights and cameras, so the effect is not as well edited as in the blender software. In addition, the default elements in 3DS are triangles. That is to say, polygon elements are subdivided into triangles for storage during export. Therefore, in OpenGL, specify glbegin (gl_triangles
.

The following are the images I edited in blender:


Is the running result of the program.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.