//读入顶点索引 void ReadVertexIndices(t3DObject pObject, tChunk pPreviousChunk) { int index = 0; //读入该对象中面的数目 pPreviousChunk.bytesRead += fread(ref pObject.numOfFaces, 2, m_FilePointer); //分配所有的储存空间,并初始化结构 pObject.pFaces = new tFace[pObject.numOfFaces]; //遍历对象中所有的面 for (int i = 0; i < pObject.numOfFaces; i++) { pObject.pFaces[i] = new tFace(); for (int j = 0; j < 4; j++) { //读入当前对象的第一个点 pPreviousChunk.bytesRead += fread(ref index, 2, m_FilePointer); if (j < 3) { pObject.pFaces[i].vertIndex[j] = index; } } } }
//读入对象的材质名称 void ReadObjectMaterial(t3DModel pModel, t3DObject pObject, tChunk pPreviousChunk) { String strMaterial = ""; //用来保存对象的材质名称 int[] buffer = new int[50000]; //用来读入不需要的数据 //读入赋予当前对象的材质名称 pPreviousChunk.bytesRead += getStr(ref strMaterial); //遍历所有的纹理 for (int i = 0; i < pModel.numOfMaterials; i++) { //如果读入的纹理与当前纹理名称匹配 if (strMaterial.Equals(pModel.pMaterials[i].strName)) { //设置材质ID pObject.materialID = i; //判断是否是纹理映射,如果strFile是一个长度大于1的字符串,则是纹理 if (pModel.pMaterials[i].strFile.Length > 0) { //设置对象的纹理映射标志 pObject.bHasTexture = true; } break; } else { //如果该对象没有材质,则设置ID为-1 pObject.materialID = -1; } } pPreviousChunk.bytesRead += fread(ref buffer, pPreviousChunk.length - pPreviousChunk.bytesRead, m_FilePointer); }
//读入RGB颜色 void ReadColorChunk(tMaterialInfo pMaterial, tChunk pChunk) { //读入颜色块信息 ReadChunk(m_TempChunk); //读入RGB颜色 m_TempChunk.bytesRead += fread(ref pMaterial.color, m_TempChunk.length - m_TempChunk.bytesRead, m_FilePointer); //增加读入的字节数 pChunk.bytesRead += m_TempChunk.bytesRead; }
//读下一个块 private void ReadChunk(tChunk pChunk) { //pChunk.bytesRead = fread(ref pChunk.ID, 2, this.m_FilePointer); Byte[] id = new Byte[2]; Byte[] length = new Byte[4]; pChunk.bytesRead = (UInt32)this.m_FilePointer.Read(id, 0, 2); pChunk.bytesRead += (UInt32)this.m_FilePointer.Read(length, 0, 4); pChunk.ID = (UInt32)(id[1] * 256 + id[0]); pChunk.length = (UInt32)(((length[3] * 256 + length[2]) * 256 + length[1]) * 256 + length[0]); }
//读入对象的UV坐标 void ReadUVCoordinates(t3DObject pObject, tChunk pPreviousChunk) { //为了读入对象的UV坐标,首先需要读入数量,再读入具体的数据 //读入UV坐标的数量 pPreviousChunk.bytesRead += fread(ref pObject.numTexVertex, 2, m_FilePointer); //初始化保存UV坐标的数组 pObject.pTexVerts = new CVector2[pObject.numTexVertex]; //读入纹理坐标 pPreviousChunk.bytesRead += fread(ref pObject.pTexVerts, pPreviousChunk.length - pPreviousChunk.bytesRead, m_FilePointer); }
//处理所有的文件中的对象信息 void ProcessNextObjectChunk(t3DModel pModel, t3DObject pObject, tChunk pPreviousChunk) { m_CurrentChunk = new tChunk(); //继续读入块的内容直至本子块结束 while (pPreviousChunk.bytesRead < pPreviousChunk.length) { ReadChunk(m_CurrentChunk); if (m_CurrentChunk.ID == FileHead.OBJECT_MESH)//正读入的是一个新块 { //使用递归函数调用,处理该新块 ProcessNextObjectChunk(pModel, pObject, m_CurrentChunk); } else if (m_CurrentChunk.ID == FileHead.OBJECT_VERTICES)//读入的是对象顶点 { ReadVertices(pObject, m_CurrentChunk); } else if (m_CurrentChunk.ID == FileHead.OBJECT_FACES)//读入的是对象的面 { ReadVertexIndices(pObject, m_CurrentChunk); } else if (m_CurrentChunk.ID == FileHead.OBJECT_MATERIAL)//读入的是对象的材质名称 { //该块保存了对象材质的名称,可能是一个颜色,也可能是一个纹理映射。 //同时在该块中也保存了纹理对象所赋予的面 //下面读入对象的材质名称 ReadObjectMaterial(pModel, pObject, m_CurrentChunk); } else if (m_CurrentChunk.ID == FileHead.OBJECT_UV)//读入对象的UV纹理坐标 { ReadUVCoordinates(pObject, m_CurrentChunk); } else { //掠过不需要读入的块 while (m_CurrentChunk.bytesRead != m_CurrentChunk.length) { int[] b = new int[1]; m_CurrentChunk.bytesRead += fread(ref b, 1, m_FilePointer); } } //添加从最后块中读入的字节数 pPreviousChunk.bytesRead += m_CurrentChunk.bytesRead; } //当前快设置为前面的块 m_CurrentChunk = pPreviousChunk; }
//处理所有的材质信息 void ProcessNextMaterialChunk(t3DModel pModel, tChunk pPreviousChunk) { //给当前块分配存储空间 m_CurrentChunk = new tChunk(); //继续读入这些块,直到该子块结束 while (pPreviousChunk.bytesRead < pPreviousChunk.length) { //读入下一块 ReadChunk(m_CurrentChunk); //判断读入的是什么块 if (m_CurrentChunk.ID == FileHead.MATNAME)//材质的名称 { //读入材质的名称 m_CurrentChunk.bytesRead += fread(ref pModel.pMaterials[pModel.numOfMaterials - 1].strName, m_CurrentChunk.length - m_CurrentChunk.bytesRead, m_FilePointer); } else if (m_CurrentChunk.ID == FileHead.MATDIFFUSE)//对象的RGB颜色 { ReadColorChunk(pModel.pMaterials[pModel.numOfMaterials - 1], m_CurrentChunk); } else if (m_CurrentChunk.ID == FileHead.MATMAP)//纹理信息头部 { //进入下一个材质块信息 ProcessNextMaterialChunk(pModel, m_CurrentChunk); } else if (m_CurrentChunk.ID == FileHead.MATMAPFILE) { //读入材质文件名称 pModel.pMaterials[pModel.numOfMaterials - 1].strFile = base_dir; m_CurrentChunk.bytesRead += fread(ref pModel.pMaterials[pModel.numOfMaterials - 1].strFile, m_CurrentChunk.length - m_CurrentChunk.bytesRead, m_FilePointer); } else { //掠过不需要读入的块 while (m_CurrentChunk.bytesRead != m_CurrentChunk.length) { int[] b = new int[1]; m_CurrentChunk.bytesRead += fread(ref b, 1, m_FilePointer); } } //添加从最后块中读入的字节数 pPreviousChunk.bytesRead += m_CurrentChunk.bytesRead; } //当前快设置为前面的块 m_CurrentChunk = pPreviousChunk; }
//读入对象的顶点 void ReadVertices(t3DObject pObject, tChunk pPreviousChunk) { //在读入实际的顶点之前,首先必须确定需要读入多少个顶点。 //读入顶点的数目 pPreviousChunk.bytesRead += fread(ref pObject.numOfVerts, 2, m_FilePointer); //分配顶点的储存空间,然后初始化结构体 pObject.pVerts = new CVector3[pObject.numOfVerts]; //读入顶点序列 pPreviousChunk.bytesRead += fread(ref pObject.pVerts, pPreviousChunk.length - pPreviousChunk.bytesRead, m_FilePointer); //因为3DMax的模型Z轴是指向上的,将y轴和z轴翻转——y轴和z轴交换,再把z轴反向 //遍历所有的顶点 //for (int i = 0; i < pObject.numOfVerts; i++) //{ // float fTempY = pObject.pVerts[i].y; // pObject.pVerts[i].y = pObject.pVerts[i].z; // pObject.pVerts[i].z = -1 * fTempY; //} }
//读出3ds文件的主要部分 void ProcessNextChunk(t3DModel pModel, tChunk pPreviousChunk) { t3DObject newObject = new t3DObject(); int version = 0; m_CurrentChunk = new tChunk(); // 下面每读一个新块,都要判断一下块的ID,如果该块是需要的读入的,则继续进行 // 如果是不需要读入的块,则略过 // 继续读入子块,直到达到预定的长度 while (pPreviousChunk.bytesRead < pPreviousChunk.length) { //读入下一个块 ReadChunk(m_CurrentChunk); //判断ID号 if (m_CurrentChunk.ID == FileHead.VERSION) { m_CurrentChunk.bytesRead += fread(ref version, m_CurrentChunk.length - m_CurrentChunk.bytesRead, m_FilePointer); // 如果文件版本号大于3,给出一个警告信息 if (version > 3) { Debug.WriteLine("Warning: This 3DS file is over version 3 so it may load incorrectly"); } } else if (m_CurrentChunk.ID == FileHead.OBJECTINFO) { //读入下一个块 ReadChunk(m_TempChunk); //获得网络的版本号 m_TempChunk.bytesRead += fread(ref version, m_TempChunk.length - m_TempChunk.bytesRead, m_FilePointer); //增加读入的字节数 m_CurrentChunk.bytesRead += m_TempChunk.bytesRead; //进入下一个块 ProcessNextChunk(pModel, m_CurrentChunk); } else if (m_CurrentChunk.ID == FileHead.MATERIAL)//材质信息 { //材质的数目递增 pModel.numOfMaterials++; //在纹理链表中添加一个空白纹理结构 pModel.pMaterials.Add(new tMaterialInfo()); //进入材质装入函数 ProcessNextMaterialChunk(pModel, m_CurrentChunk); } else if (m_CurrentChunk.ID == FileHead.OBJECT)//对象的名称 { //对象数目递增 pModel.numOfObjects++; //添加一个新的tObject节点到对象的链表中 pModel.pObject.Add(new t3DObject()); //获得并保存对象的名称,然后增加读入的字节数 m_CurrentChunk.bytesRead += getStr(ref pModel.pObject[pModel.numOfObjects - 1].strName); //进入其余对象信息的读入 ProcessNextObjectChunk(pModel, pModel.pObject[pModel.numOfObjects - 1], m_CurrentChunk); } else { // 跳过关键帧块的读入,增加需要读入的字节数 EDITKEYFRAME // 跳过所有忽略的块的内容的读入,增加需要读入的字节数 while (m_CurrentChunk.bytesRead != m_CurrentChunk.length) { int[] b = new int[1]; m_CurrentChunk.bytesRead += fread(ref b, 1, m_FilePointer); } } //添加从最后块中读入的字节数 pPreviousChunk.bytesRead += m_CurrentChunk.bytesRead; } //当前快设置为前面的块 m_CurrentChunk = pPreviousChunk; }