Self-made C # version 3DS file parser and display 3DS model with SHARPGL

Source: Internet
Author: User

Self-made C # version 3DS file parser and display 3DS model with SHARPGL

+bit Wei Wei + quietly left a version of the right to say:

It is said that the *.3DS format of 3D model files is very old and outdated format. This document references (Http://www.spacesimulator.net/wiki/index.php?title=Tutorials:3ds_Loader) and (http://www.cnblogs.com/lookof/ archive/2009/03/27/1423695.html), here to express our thanks. This article explains how to write a parser for a 3DS file in C # from scratch, and then use SHARPGL (C # for OpenGL encapsulation) to display the 3DS model. There is a picture of the truth.

Use the 3DS model file and the decal file here. (Spaceship.zip) (spaceshiptexture.bmp)

3DS file Format +bit Wei Wei + quietly left a message in this version of the right to say:

The 3DS file is binary. The basic unit of the 3DS format is called block (chunk). We just read this piece of information. The directory tree is as follows, and the indentation style embodies the parent-child relationship of the block. The 3DS model file, like the XML file, is visible as a tree structure with only 1 root nodes.

1MAIN CHUNK0x4d4d23D EDITOR CHUNK0x3d3d3OBJECT BLOCK0x40004Triangular MESH0x41005VERTICES LIST0x41106FACES DESCRIPTION0x41207FACES MATERIAL0x41308MAPPING coordinates LIST0x41409Smoothing GROUP LIST0x4150TenLOCAL coordinates SYSTEM0x4160 OneLight0x4600 ASPOTLIGHT0x4610 -CAMERA0x4700 -MATERIAL BLOCK0xAFFF theMATERIAL NAME0xa000 -AMBIENT COLOR0xa010 -Diffuse COLOR0xa020 -Specular COLOR0xa030 +TEXTURE MAP1 0xa200 -BUMP MAP0xa230 +REFLECTION MAP0xa220 A [SUB CHUNKS for each MAP] atMAPPING FILENAME0xa300 -MAPPING PARAMETERS0xa351 -Keyframer CHUNK0xb000 -MESH Information BLOCK0xb002 -SPOT Light Information BLOCK0xb007 -FRAMES (START and END)0xb008 inOBJECT NAME0xb010 -OBJECT PIVOT Point0xb013 toPOSITION Track0xb020 +ROTATION Track0xb021 -Scale Track0xb022 theHIERARCHY POSITION0xb030
3DS block Structure+bit Wei Wei + quietly left a version of the right to say:

In fact, the complete list of chunk has thousands of types, and we just need to parse the list of vertex, polygon, and texture Uvs.

The first two bytes of the entire 3DS file must be 0x4d4d, otherwise the file is not a 3DS model file, as an example of the main chunk type identified as 0X4D4D. Then from 3rd to 6th byte is a numeric value of type Uint32 that represents the length of the entire main chunk. Since main chunk is the root node of the entire 3DS file, its length is also the length of the entire 3DS file.

The structure of the Block (Chunk)

The structure of each "chunk" is as follows:

Offset amount

Length

0

2

Block identifiers

2

4

Block Length: Block Data + sub-block content

6

N

Block data

6+n

M

S sub-block

+bit Wei Wei + quietly left a version of the right to say:

Read the idea is: first based on the offset and length to find a block identifier, and then to determine what it is the block, encountered the block we need to further read, if not required, directly skip this piece, read the following block.

Our parser needs vertex, polygon and map UV information, according to the 3DS model file tree structure, you can find the chunk that need to be parsed as follows.

MAIN CHUNK

Identifier

0x4d4d

Length

0 + sub-chunks Length

Chunk father

None

Sub chunks

3D EDITOR CHUNK

Data

None

3D EDITOR CHUNK

Identifier

0x3d3d

Length

0 + sub-chunks Length

Chunk father

MAIN CHUNK

Sub chunks

OBJECT block, MATERIAL block, Keyframer CHUNK

Data

None

OBJECT BLOCK

Identifier

0x4000

Length

Object name length + sub-chunks length

Chunk father

3D EDITOR CHUNK

Sub chunks

Triangular MESH, light, CAMERA

Data

Object Name

Triangular MESH

Identifier

0x4100

Length

0 + sub-chunks Length

Chunk father

OBJECT BLOCK

Sub chunks

VERTICES list, FACES DESCRIPTION, MAPPING coordinates list

Data

None

VERTICES LIST (point data in this)

Identifier

0x4110

Length

Varying + sub-chunks length

Chunk father

Triangular MESH

Sub chunks

None

Data

Vertices number (unsigned short)
Vertices list:x1,y1,z1,x2,y2,z2 etc. (for each vertex:3*float)

FACES DESCRIPTION (polygon data in this)

Identifier

0x4120

Length

Varying + sub-chunks length

Chunk father

Triangular MESH

Sub chunks

FACES MATERIAL

Data

Polygons number (unsigned short)
Polygons LIST:A1,B1,C1,A2,B2,C2 etc. (for each point:3*unsigned short)
Face flag:face options, sides visibility etc. (unsigned short)

MAPPING coordinates LIST (map data in this)

Identifier

0x4140

Length

Varying + sub-chunks length

Chunk father

Triangular MESH

Sub chunks

Smoothing GROUP LIST

Data

Vertices number (unsigned short)
Mapping coordinates list:u1,v1,u2,v2 etc. (for each vertex:2*float)

The enumeration type of chunk is given accordingly

1         enumChunktype2         {3Mainchunk =0x4d4d,4_3deditorchunk =0x3d3d,5Cversion =0x0002,6Keyframerchunk =0xb000,7Materialblock =0xAFFF,8Materialname =0xa000,9Ambientcolor =0xa010,TenDiffusecolor =0xa020, OneSpecularcolor =0xa030, AC_matshininess =0xa040, -Texturemap =0xa200, -Mappingfilename =0xa300, theObjectblock =0x4000, -Triangularmesh =0x4100, -Verticeslist =0x4110, -Facesdescription =0x4120, +Facesmaterial =0x4130, -Mappingcoordinateslist =0x4140 +}
ChunktypeParsing results +bit Wei Wei + quietly left a version of the right information said:

The parse result is an instance of a 3dsFile type that contains several models (called entity). Each entity contains the vertex, polygon, and map UV information that describes the three-dimensional model, which we use SHARPGL to display.

1          foreach(varEntityinch_3dsfile.entities)2          {3 GL. Enable (opengl.gl_texture_2d);4Gl. Bindtextue (opengl.gl_texture_2d, This. Texture. TextureName);5 GL. Begin (SharpGL.Enumerations.BeginMode.Triangles);6                    foreach(varTriangleinchentity.indices)7                    {8                             varPoint1 =Entity.vertices[triangle.vertex1];9                             varUv1 =Entity.texcoords[triangle.vertex1];Ten GL. Texcoord (Uv1. U, Uv1. V); One GL. Vertex (point1. X, Point1. Y, Point1. Z); A                             varPoint2 =Entity.vertices[triangle.vertex2]; -                             varUv2 =Entity.texcoords[triangle.vertex2]; - GL. Texcoord (Uv2. U, Uv2. V); the GL. Vertex (Point2. X, Point2. Y, Point2. Z); -                             varPoint3 =Entity.vertices[triangle.vertex3]; -                             varUv3 =Entity.texcoords[triangle.vertex3]; - GL. Texcoord (Uv3. U, Uv3. V); + GL. Vertex (Point3. X, Point3. Y, Point3. Z); -                    } + GL. End (); A          } at  -  
displaying 3DS models with SHARPGL

One thing to note is that the SHARPGL loading map is reversed, so you have to flip the prepared map up and down in order to use it normally in SHARPGL.

+bit Wei Wei + quietly left a version of the right to say:

It is easier said than done, need the source of the classmate trouble support, with the donation below The two-dimensional code (can also be found here) to my donation of 100 yuan and leave your ID, email and other contact information . You can also find my contact information in the bulletin board, any non-direct source of communication is welcome!

Self-made C # version 3DS file parser and display 3DS model with SHARPGL

Related Article

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.