这几天在网上找了很久关于opengl 读取.3ds的源文件.... 找得我心都凉了,其实,当我正打算放弃的时候(因为老师过几周就会交我们如何向opengl中导入文件,所以我可以不用急),在浏览外国的一位编程人员的网站的时候发现了读取的办法....索性做了一个自己读取的文件,不过只是实现了最基本的2样 vertex 和 texCoordinate 的导入.不过因为有课,也用了我一天的时间 下面是那个网站,http://www.spacesimulator.net/tutorials.html 索性分享下,上面有.3ds的读取方法,不过研究3ds的结构是一个非常好玩的过程 下面我把结构copy过来,没人看 我自己看 呵呵~~ his table shows the offset (in bytes) and the length (also in bytes) of each field in a typical chunk:Offset Length 0 2 Chunk identifier 2 4 Chunk length: chunk data + sub-chunks(6+n+m) 6 n Data 6+n m Sub-chunks We can see from the last line in the table exactly how some chunks are dependent on others: each child chunk is in fact contained inside the field "Sub-chunks" of the parent chunk. The following are the most important chunks in a 3ds file. Please note the hierarchy among the various elements: MAIN CHUNK 0x4D4D 3D EDITOR CHUNK 0x3D3D OBJECT BLOCK 0x4000 TRIANGULAR MESH 0x4100 VERTICES LIST 0x4110 FACES DESCRIPTION 0x4120 FACES MATERIAL 0x4130 MAPPING COORDINATES LIST 0x4140 SMOOTHING GROUP LIST 0x4150 LOCAL COORDINATES SYSTEM 0x4160 LIGHT 0x4600 SPOTLIGHT 0x4610 CAMERA 0x4700 MATERIAL BLOCK 0xAFFF MATERIAL NAME 0xA000 AMBIENT COLOR 0xA010 DIFFUSE COLOR 0xA020 SPECULAR COLOR 0xA030 TEXTURE MAP 1 0xA200 BUMP MAP 0xA230 REFLECTION MAP 0xA220 [SUB CHUNKS FOR EACH MAP] MAPPING FILENAME 0xA300 MAPPING PARAMETERS 0xA351 KEYFRAMER CHUNK 0xB000 MESH INFORMATION BLOCK 0xB002 SPOT LIGHT INFORMATION BLOCK 0xB007 FRAMES (START AND END) 0xB008 OBJECT NAME 0xB010 OBJECT PIVOT POINT 0xB013 POSITION TRACK 0xB020 ROTATION TRACK 0xB021 SCALE TRACK 0xB022 HIERARCHY POSITION 0xB030 As mentioned earlier, If we want to read a particular chunk we must always read its parent chunk first. Imagine the 3ds file is a tree and the chunk that we need is a leaf (and we are a little ant on the ground). In order to reach the leaf, we need to start from the trunk and cross any branches that lead to that leaf. For example, if we want to reach the chunk VERTICES LIST, we have to read the MAIN CHUNK first, then the 3D EDITOR CHUNK, the OBJECT BLOCK and finally the TRIANGULAR MESH chunk. The other chunks can safely be skipped. Now let's prune our tree and leave only the branches we are going to use in this tutorial : "vertices", "faces", "mapping coordinates" and their relative parents: MAIN CHUNK 0x4D4D 3D EDITOR CHUNK 0x3D3D OBJECT BLOCK 0x4000 TRIANGULAR MESH 0x4100 VERTICES LIST 0x4110 FACES DESCRIPTION 0x4120 MAPPING COORDINATES LIST 0x4140 Here are the chunks described in detail : 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 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 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)(2 bytes used by 3ds,you can ignore it) MAPPING COORDINATES LIST 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)