Oh my goodness, how long has it been since I wrote the first part of this article? What on hell was I buzzing around for during this period which seemed years to me? This plug-in is nearly becoming a legacy for me. Now it’s time to bring it back and finish the remaining parts. And I will also upload the whole project’s source code and related docs later.
First part of this article: http://www.bennychen.cn/?p=640
Predefine mesh file format
After we’ve got some basic concepts about 3DS Max, now it’s time to define our own mesh file format. It’s very hard to directly define a format from the scratch. Researching some already typical and mature file formats is a good starting point. Recommended by my tutor, I chose .md2 & .md3 formats as my learning material which are famous for being used in the Quake series game. Based on several days’ research on them, I defined my own mesh file format, which is sort of like .md2 & .md3 but also differs from them at some point.
I did not just define one format. They are a suite of formats, see the following table.
File Type
|
File Format
|
Basic mesh file
|
*.CSM (Crowd Simulation Model)
|
Texture
|
*.jpg, *.bmp, ……
|
Animation
|
*.AM
|
Animation Configuration
|
*.CFG
|
Mounting Configuration
|
*.CSM_PACK
|
There are 5 parts in total. But some of them are not necessary. Only one file is essential – the basic mesh file, which is named as CSM( Crowd Simulation Model ), because at that time I programmed this plug-in mainly for a crowd simulation project. The texture part is the textures file used by the mesh. If a mesh includes animation, the AM file( short for animation ) and animation configuration file(.CFG) are also needed. My format also supports mesh mounting, so if a mesh is mounted by several sub-meshes, .CSM_PACK is used to store the mount info.
These are some of the features of my format.
- Support multi-texture
- Support skinned animation
- Decouple mesh and animation
- Support animation configuration
- Support mesh mounting
And the 2 figures below are the inside structure of CSM and AM. If you want to know more detail of them, later I will upload the docs.
I stored my animation data in .AM file, bone by bone, animation by animation, and frame by frame.
Export static mesh data
OK, so far, all the preparation work is done. We can finally start the programming process.
For the last 2 steps, I will not present very detailed info here. I will only talk on the difficulties needed to overcome when I programmed them. All the programming job is strictly based on the format we defined last step. For more detail, please turn to my source code.
Firstly we need to handle the static mesh data. In this step, some difficulties are.
1. Transformation between different types of coordinate system.
Basically 3DS Max uses right-hand coordinate system. But some common rendering system( i.e. DirectX ) uses left-hand coordinate system. They have totally different coordinate systems, see figure below. So, the coordinate data needs to be transformed before outputting. See the figure below, the left is a typical rendering system’s coordinate system, and the right is of 3DS Max.
About how to transform, you can refer to this paper – 《一种不同坐标系之间的变换矩阵的转换方法》by 杨卫东&刘玉树.
2. Handle mesh with multiple textures.
This is a tricky problem. We know that a single vertex could be shared by different faces, so if these faces are attached with different textures, the vertex should have multiple UV coordinates. This leads to the fact that we need to split the vertex into multiple vertices, so that we can ensure one vertex only maps to one UV coordinate. And the whole mesh needs to be reorganized.
From my CSM format definition, you may have found that a mesh is comprised of multiple submeshes. Actually the submeshes are split based on textures, that is, vertices using the same texture are categorized into one submesh. So if an original vertex is using 2 textures, this vertex is firstly split into 2 vertices and will exist under 2 submeshes respectively.
3. Vertex normal
This is another painful issue. About this topic, I once wrote an article in my blog before. Turn to it here.
Export skinned animation
Now, we have come to the most complex, most sticky part of the whole work. I can still remember the time, the pain and the effort when I tried to tackle this problem. Those memories are so unforgettable. But they all deserve.
Why I did it so painfully was that, firstly I was still not familiar with the mechanism of bone animation at that time, secondly the mechanism of 3DS Max skeletal system is hard to figure out, whether its bone system or biped system, lastly, decouple the vertex data and the animation data is another challenge.
About the computational process of bone animation, thanks to this blog, it is the key article that helped me accomplish the whole work.
http://www.cnblogs.com/neoragex2002/archive/2007/09/13/Bone_Animation.html
Screenshots
The figure below is my exporter plug-in UI. The upper part is used to include animations, which will later generate .CFG file. Ant the lower part is used to configure mesh mounting, which will later generate .CSM_PACK file.
And here is an example of exporting. On the back is the 3ds max software interface, rendering a mesh with or without some animation. And on the front is the mesh viewer developed by me, which renders the same mesh with the same animation.
This is another example, the Tiananmen Gate.
Recent Comments