3ds文件是3D Max的一种二进制存储格式,用来存储3D模型场景。关于这种格式官方的资料很少,从二进制角度解析文件很困难,幸好有个开源的项目已经提供了解析的库。
[lib3ds]http://code.google.com/p/lib3ds/
-
- 用ANSI-C编写
- 支持的框架:
-
- GNU build tools (autoconf, automake, libtool)
- UNIX
- Mac OS X
- Microsoft Visual C++ 8.0
- 支持小端和大端字节序的cpu
- 载入和保存:
-
- Atmosphere settings
- 背景设置 (Background settings)
- 阴影贴图设置 (Shadow map settings)
- 视口设置 (Viewport setting)
- 材质 (Materials)
- 摄像机 (Cameras)
- 灯光 (Lights)
- 网格 (Meshes)
- 层次结构 (Hierarchy)
- 关键祯 (Keyframes)
- 计算动画(关键祯)数据
- 简单容易操作的数据结构
-
- 矢量数学模块
- 四元数数学模块
- 矩阵数学模块
- 与OpenGL无缝集成
(1) 坐标系
在3ds文件中使用的坐标系是左手坐标系,从左到右,x递增;从下到上,z递增;从近到远,y递增。
在lib3ds库中,经过解析,把3ds文件中的数据转换成了OpenGL采用的右手坐标系,从左到右,x递增,从下到上y递增,从远到近,z递增。
![](https://i-blog.csdnimg.cn/blog_migrate/823b8759ef19a16f1dea6bedb10ef18a.png)
(2) 数学模块
矢量数学函数
1 extern LIB3DSAPI void lib3ds_vector_make(
2 float c[3],
3 float x,
4 float y,
5 float z);
6
7 /**
8 Sets all components of a vector to zero.
9 \param c
10 The Pointer to the vector.
11 */
12 extern LIB3DSAPI void lib3ds_vector_zero(
13 float c[3]);
14
15 /**
16 Copies all components of a vector to another vector.
17 \param dst
18 [out] The destination vector.
19 \param src
20 [in] The source vector.
21 */
22 extern LIB3DSAPI void lib3ds_vector_copy(
23 float dst[3],
24 float src[3]);
25
26 /**
27 Negates all components of a vector.
28 \param c
29 The Pointer to the vector.
30 */
31 extern LIB3DSAPI void lib3ds_vector_neg(
32 float c[3]);
33
34 extern LIB3DSAPI void lib3ds_vector_make(float c[3], float x, float y, float z);
35 extern LIB3DSAPI void lib3ds_vector_zero(float c[3]);
36 extern LIB3DSAPI void lib3ds_vector_add(float c[3], float a[3], float b[3]);
37 extern LIB3DSAPI void lib3ds_vector_sub(float c[3], float a[3], float b[3]);
38 extern LIB3DSAPI void lib3ds_vector_scalar_mul(float c[3], float a[3], float k);
39 extern LIB3DSAPI void lib3ds_vector_cross(float c[3], float a[3], float b[3]);
40 extern LIB3DSAPI float lib3ds_vector_dot(float a[3], float b[3]);
41 extern LIB3DSAPI float lib3ds_vector_length(float c[3]);
42 extern LIB3DSAPI void lib3ds_vector_normalize(float c[3]);
43 extern LIB3DSAPI void lib3ds_vector_normal(float n[3], float a[3], float b[3], float c[3]);
44 extern LIB3DSAPI void lib3ds_vector_min(float c[3], float a[3]);
45 extern LIB3DSAPI void lib3ds_vector_max(float c[3], float a[3]);
46 extern LIB3DSAPI void lib3ds_vector_transform(float c[3], float m[4][4], float a[3]);
47
48 extern LIB3DSAPI void lib3ds_quat_identity(float c[4]);
49 extern LIB3DSAPI void lib3ds_quat_copy(float dest[4], float src[4]);
50 extern LIB3DSAPI void lib3ds_quat_axis_angle(float c[4], float axis[3], float angle);
51 extern LIB3DSAPI void lib3ds_quat_neg(float c[4]);
52 extern LIB3DSAPI void lib3ds_quat_cnj(float c[4]);
53 extern LIB3DSAPI void lib3ds_quat_mul(float c[4], float a[4], float b[4]);
54 extern LIB3DSAPI void lib3ds_quat_scalar(float c[4], float k);
55 extern LIB3DSAPI void lib3ds_quat_normalize(float c[4]);
56 extern LIB3DSAPI void lib3ds_quat_inv(float c[4]);
57 extern LIB3DSAPI float lib3ds_quat_dot(float a[4], float b[4]);
58 extern LIB3DSAPI float lib3ds_quat_norm(float c[4]);
59 extern LIB3DSAPI void lib3ds_quat_ln(float c[4]);
60 extern LIB3DSAPI void lib3ds_quat_ln_dif(float c[4], float a[4], float b[4]);
61 extern LIB3DSAPI void lib3ds_quat_exp(float c[4]);
62 extern LIB3DSAPI void lib3ds_quat_slerp(float c[4], float a[4], float b[4], float t);
63 extern LIB3DSAPI void lib3ds_quat_squad(float c[4], float a[4], float p[4], float q[4], float b[4], float t);
64 extern LIB3DSAPI void lib3ds_quat_tangent(float c[4], float p[4], float q[4], float n[4]);
四元数数学函数
1 extern LIB3DSAPI void lib3ds_quat_identity(float c[4]);
2 extern LIB3DSAPI void lib3ds_quat_copy(float dest[4], float src[4]);
3 extern LIB3DSAPI void lib3ds_quat_axis_angle(float c[4], float axis[3], float angle);
4 extern LIB3DSAPI void lib3ds_quat_neg(float c[4]);
5 extern LIB3DSAPI void lib3ds_quat_cnj(float c[4]);
6 extern LIB3DSAPI void lib3ds_quat_mul(float c[4], float a[4], float b[4]);
7 extern LIB3DSAPI void lib3ds_quat_scalar(float c[4], float k);
8 extern LIB3DSAPI void lib3ds_quat_normalize(float c[4]);
9 extern LIB3DSAPI void lib3ds_quat_inv(float c[4]);
10 extern LIB3DSAPI float lib3ds_quat_dot(float a[4], float b[4]);
11 extern LIB3DSAPI float lib3ds_quat_norm(float c[4]);
12 extern LIB3DSAPI void lib3ds_quat_ln(float c[4]);
13 extern LIB3DSAPI void lib3ds_quat_ln_dif(float c[4], float a[4], float b[4]);
14 extern LIB3DSAPI void lib3ds_quat_exp(float c[4]);
15 extern LIB3DSAPI void lib3ds_quat_slerp(float c[4], float a[4], float b[4], float t);
16 extern LIB3DSAPI void lib3ds_quat_squad(float c[4], float a[4], float p[4], float q[4], float b[4], float t);
17 extern LIB3DSAPI void lib3ds_quat_tangent(float c[4], float p[4], float q[4], float n[4]);
矩阵数学函数
1 extern LIB3DSAPI void lib3ds_matrix_zero(float m[4][4]);
2 extern LIB3DSAPI void lib3ds_matrix_identity(float m[4][4]);
3 extern LIB3DSAPI void lib3ds_matrix_copy(float dest[4][4], float src[4][4]);
4 extern LIB3DSAPI void lib3ds_matrix_neg(float m[4][4]);
5 extern LIB3DSAPI void lib3ds_matrix_transpose(float m[4][4]);
6 extern LIB3DSAPI void lib3ds_matrix_add(float m[4][4], float a[4][4], float b[4][4]);
7 extern LIB3DSAPI void lib3ds_matrix_sub(float m[4][4], float a[4][4], float b[4][4]);
8 extern LIB3DSAPI void lib3ds_matrix_mult(float m[4][4], float a[4][4], float b[4][4]);
9 extern LIB3DSAPI void lib3ds_matrix_scalar(float m[4][4], float k);
10 extern LIB3DSAPI float lib3ds_matrix_det(float m[4][4]);
11 extern LIB3DSAPI int lib3ds_matrix_inv(float m[4][4]);
12 extern LIB3DSAPI void lib3ds_matrix_translate(float m[4][4], float x, float y, float z);
13 extern LIB3DSAPI void lib3ds_matrix_scale(float m[4][4], float x, float y, float z);
14 extern LIB3DSAPI void lib3ds_matrix_rotate_quat(float m[4][4], float q[4]);
15 extern LIB3DSAPI void lib3ds_matrix_rotate(float m[4][4], float angle, float ax, float ay, float az);
16 extern LIB3DSAPI void lib3ds_matrix_camera(float m[4][4], float pos[3], float tgt[3], float roll);
关于3D数学基础知识,大家可以参考下这本书---《3D数学基础:图形与游戏开发》。
(3) 3DS文件数据结构
Lib3dsFile
1 typedef struct Lib3dsFile {
2 unsigned user_id;
3 void* user_ptr;
4 unsigned mesh_version;
5 unsigned keyf_revision;
6 char name[12+1];
7 float master_scale;
8 float construction_plane[3];
9 float ambient[3];
10 Lib3dsShadow shadow;
11 Lib3dsBackground background;
12 Lib3dsAtmosphere atmosphere;
13 Lib3dsViewport viewport;
14 Lib3dsViewport viewport_keyf;
15 int frames;
16 int segment_from;
17 int segment_to;
18 int current_frame;
19 int materials_size;
20 int nmaterials;
21 Lib3dsMaterial** materials;
22 int cameras_size;
23 int ncameras;
24 Lib3dsCamera** cameras;
25 int lights_size;
26 int nlights;
27 Lib3dsLight** lights;
28 int meshes_size;
29 int nmeshes;
30 Lib3dsMesh** meshes;
31 Lib3dsNode* nodes;
32 } Lib3dsFile;
33
34 extern LIB3DSAPI Lib3dsFile* lib3ds_file_open(const char *filename);
35 extern LIB3DSAPI int lib3ds_file_save(Lib3dsFile *file, const char *filename);
36 extern LIB3DSAPI Lib3dsFile* lib3ds_file_new();
37 extern LIB3DSAPI void lib3ds_file_free(Lib3dsFile *file);
通过lib3ds_file_open函数打开3ds文件后,Lib3dsFile结构包含了解析完成的所有3d数据。
3ds文件是3D Max的一种二进制存储格式,用来存储3D模型场景。关于这种格式官方的资料很少,从二进制角度解析文件很困难,幸好有个开源的项目已经提供了解析的库。
[lib3ds]http://code.google.com/p/lib3ds/
-
- 用ANSI-C编写
- 支持的框架:
-
- GNU build tools (autoconf, automake, libtool)
- UNIX
- Mac OS X
- Microsoft Visual C++ 8.0
- 支持小端和大端字节序的cpu
- 载入和保存:
-
- Atmosphere settings
- 背景设置 (Background settings)
- 阴影贴图设置 (Shadow map settings)
- 视口设置 (Viewport setting)
- 材质 (Materials)
- 摄像机 (Cameras)
- 灯光 (Lights)
- 网格 (Meshes)
- 层次结构 (Hierarchy)
- 关键祯 (Keyframes)
- 计算动画(关键祯)数据
- 简单容易操作的数据结构
-
- 矢量数学模块
- 四元数数学模块
- 矩阵数学模块
- 与OpenGL无缝集成
(1) 坐标系
在3ds文件中使用的坐标系是左手坐标系,从左到右,x递增;从下到上,z递增;从近到远,y递增。
在lib3ds库中,经过解析,把3ds文件中的数据转换成了OpenGL采用的右手坐标系,从左到右,x递增,从下到上y递增,从远到近,z递增。
(2) 数学模块
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
1 extern LIB3DSAPI void lib3ds_vector_make( 2 float c[3], 3 float x, 4 float y, 5 float z); 6 7 /** 8 Sets all components of a vector to zero. 9 \param c 10 The Pointer to the vector. 11 */ 12 extern LIB3DSAPI void lib3ds_vector_zero( 13 float c[3]); 14 15 /** 16 Copies all components of a vector to another vector. 17 \param dst 18 [out] The destination vector. 19 \param src 20 [in] The source vector. 21 */ 22 extern LIB3DSAPI void lib3ds_vector_copy( 23 float dst[3], 24 float src[3]); 25 26 /** 27 Negates all components of a vector. 28 \param c 29 The Pointer to the vector. 30 */ 31 extern LIB3DSAPI void lib3ds_vector_neg( 32 float c[3]); 33 34 extern LIB3DSAPI void lib3ds_vector_make(float c[3], float x, float y, float z); 35 extern LIB3DSAPI void lib3ds_vector_zero(float c[3]); 36 extern LIB3DSAPI void lib3ds_vector_add(float c[3], float a[3], float b[3]); 37 extern LIB3DSAPI void lib3ds_vector_sub(float c[3], float a[3], float b[3]); 38 extern LIB3DSAPI void lib3ds_vector_scalar_mul(float c[3], float a[3], float k); 39 extern LIB3DSAPI void lib3ds_vector_cross(float c[3], float a[3], float b[3]); 40 extern LIB3DSAPI float lib3ds_vector_dot(float a[3], float b[3]); 41 extern LIB3DSAPI float lib3ds_vector_length(float c[3]); 42 extern LIB3DSAPI void lib3ds_vector_normalize(float c[3]); 43 extern LIB3DSAPI void lib3ds_vector_normal(float n[3], float a[3], float b[3], float c[3]); 44 extern LIB3DSAPI void lib3ds_vector_min(float c[3], float a[3]); 45 extern LIB3DSAPI void lib3ds_vector_max(float c[3], float a[3]); 46 extern LIB3DSAPI void lib3ds_vector_transform(float c[3], float m[4][4], float a[3]); 47 48 extern LIB3DSAPI void lib3ds_quat_identity(float c[4]); 49 extern LIB3DSAPI void lib3ds_quat_copy(float dest[4], float src[4]); 50 extern LIB3DSAPI void lib3ds_quat_axis_angle(float c[4], float axis[3], float angle); 51 extern LIB3DSAPI void lib3ds_quat_neg(float c[4]); 52 extern LIB3DSAPI void lib3ds_quat_cnj(float c[4]); 53 extern LIB3DSAPI void lib3ds_quat_mul(float c[4], float a[4], float b[4]); 54 extern LIB3DSAPI void lib3ds_quat_scalar(float c[4], float k); 55 extern LIB3DSAPI void lib3ds_quat_normalize(float c[4]); 56 extern LIB3DSAPI void lib3ds_quat_inv(float c[4]); 57 extern LIB3DSAPI float lib3ds_quat_dot(float a[4], float b[4]); 58 extern LIB3DSAPI float lib3ds_quat_norm(float c[4]); 59 extern LIB3DSAPI void lib3ds_quat_ln(float c[4]); 60 extern LIB3DSAPI void lib3ds_quat_ln_dif(float c[4], float a[4], float b[4]); 61 extern LIB3DSAPI void lib3ds_quat_exp(float c[4]); 62 extern LIB3DSAPI void lib3ds_quat_slerp(float c[4], float a[4], float b[4], float t); 63 extern LIB3DSAPI void lib3ds_quat_squad(float c[4], float a[4], float p[4], float q[4], float b[4], float t); 64 extern LIB3DSAPI void lib3ds_quat_tangent(float c[4], float p[4], float q[4], float n[4]);
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
1 extern LIB3DSAPI void lib3ds_quat_identity(float c[4]); 2 extern LIB3DSAPI void lib3ds_quat_copy(float dest[4], float src[4]); 3 extern LIB3DSAPI void lib3ds_quat_axis_angle(float c[4], float axis[3], float angle); 4 extern LIB3DSAPI void lib3ds_quat_neg(float c[4]); 5 extern LIB3DSAPI void lib3ds_quat_cnj(float c[4]); 6 extern LIB3DSAPI void lib3ds_quat_mul(float c[4], float a[4], float b[4]); 7 extern LIB3DSAPI void lib3ds_quat_scalar(float c[4], float k); 8 extern LIB3DSAPI void lib3ds_quat_normalize(float c[4]); 9 extern LIB3DSAPI void lib3ds_quat_inv(float c[4]); 10 extern LIB3DSAPI float lib3ds_quat_dot(float a[4], float b[4]); 11 extern LIB3DSAPI float lib3ds_quat_norm(float c[4]); 12 extern LIB3DSAPI void lib3ds_quat_ln(float c[4]); 13 extern LIB3DSAPI void lib3ds_quat_ln_dif(float c[4], float a[4], float b[4]); 14 extern LIB3DSAPI void lib3ds_quat_exp(float c[4]); 15 extern LIB3DSAPI void lib3ds_quat_slerp(float c[4], float a[4], float b[4], float t); 16 extern LIB3DSAPI void lib3ds_quat_squad(float c[4], float a[4], float p[4], float q[4], float b[4], float t); 17 extern LIB3DSAPI void lib3ds_quat_tangent(float c[4], float p[4], float q[4], float n[4]);
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
1 extern LIB3DSAPI void lib3ds_matrix_zero(float m[4][4]); 2 extern LIB3DSAPI void lib3ds_matrix_identity(float m[4][4]); 3 extern LIB3DSAPI void lib3ds_matrix_copy(float dest[4][4], float src[4][4]); 4 extern LIB3DSAPI void lib3ds_matrix_neg(float m[4][4]); 5 extern LIB3DSAPI void lib3ds_matrix_transpose(float m[4][4]); 6 extern LIB3DSAPI void lib3ds_matrix_add(float m[4][4], float a[4][4], float b[4][4]); 7 extern LIB3DSAPI void lib3ds_matrix_sub(float m[4][4], float a[4][4], float b[4][4]); 8 extern LIB3DSAPI void lib3ds_matrix_mult(float m[4][4], float a[4][4], float b[4][4]); 9 extern LIB3DSAPI void lib3ds_matrix_scalar(float m[4][4], float k); 10 extern LIB3DSAPI float lib3ds_matrix_det(float m[4][4]); 11 extern LIB3DSAPI int lib3ds_matrix_inv(float m[4][4]); 12 extern LIB3DSAPI void lib3ds_matrix_translate(float m[4][4], float x, float y, float z); 13 extern LIB3DSAPI void lib3ds_matrix_scale(float m[4][4], float x, float y, float z); 14 extern LIB3DSAPI void lib3ds_matrix_rotate_quat(float m[4][4], float q[4]); 15 extern LIB3DSAPI void lib3ds_matrix_rotate(float m[4][4], float angle, float ax, float ay, float az); 16 extern LIB3DSAPI void lib3ds_matrix_camera(float m[4][4], float pos[3], float tgt[3], float roll);
关于3D数学基础知识,大家可以参考下这本书---《3D数学基础:图形与游戏开发》。
(3) 3DS文件数据结构
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
1 typedef struct Lib3dsFile { 2 unsigned user_id; 3 void* user_ptr; 4 unsigned mesh_version; 5 unsigned keyf_revision; 6 char name[12+1]; 7 float master_scale; 8 float construction_plane[3]; 9 float ambient[3]; 10 Lib3dsShadow shadow; 11 Lib3dsBackground background; 12 Lib3dsAtmosphere atmosphere; 13 Lib3dsViewport viewport; 14 Lib3dsViewport viewport_keyf; 15 int frames; 16 int segment_from; 17 int segment_to; 18 int current_frame; 19 int materials_size; 20 int nmaterials; 21 Lib3dsMaterial** materials; 22 int cameras_size; 23 int ncameras; 24 Lib3dsCamera** cameras; 25 int lights_size; 26 int nlights; 27 Lib3dsLight** lights; 28 int meshes_size; 29 int nmeshes; 30 Lib3dsMesh** meshes; 31 Lib3dsNode* nodes; 32 } Lib3dsFile; 33 34 extern LIB3DSAPI Lib3dsFile* lib3ds_file_open(const char *filename); 35 extern LIB3DSAPI int lib3ds_file_save(Lib3dsFile *file, const char *filename); 36 extern LIB3DSAPI Lib3dsFile* lib3ds_file_new(); 37 extern LIB3DSAPI void lib3ds_file_free(Lib3dsFile *file);
通过lib3ds_file_open函数打开3ds文件后,Lib3dsFile结构包含了解析完成的所有3d数据。