在OPENGL中读取3DS模型文件.doc_第1页
在OPENGL中读取3DS模型文件.doc_第2页
在OPENGL中读取3DS模型文件.doc_第3页
在OPENGL中读取3DS模型文件.doc_第4页
在OPENGL中读取3DS模型文件.doc_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

在OpenGL中读取3DS模型文件这里要解决的一个关键问题是: 如何在OpenGL中读取3DS模型文件。在这部分介绍了3DS文件的存储格式,给出了OpenGL与3DS的接口编程,这对三维模型的创建提供了极大的方便,还综合运用其他的技术和算法来完成三维动画。6.1 3ds文件格式简介3DS文件由许多块组成,每个块首先描述其信息类别,即该块是如何组成的。块的信息类别用ID来标识,块还包含了下一个块的相对位置信息。因此,即使不理解一个块的含义,也可以很容易地跳过它,因为该块中指出了下一个块的相对于该块起始位置的偏移字节数。与许多文件格式一样,3DS二进制文件中的数据也是按低位在前、高位在后的方式组织的,例如,2个十六进制字节,4A 5C组成的整型数,表明5C是高位字节,4A是低位字节;对于长整型数,如:4A 5C 3B 8F表明 5C4A是低位字,而 8F3B是高位字。下面描述块的具体定义。块的前两项信息分别是:块的ID和块的长度(下一个块相对于该块的字节偏移量),块的ID是一个整型数,而块的长度是一个长整型数。每个块实际上是一个层次结构,不同类型的块,其层次结构也不相同。3DS文件中有一个基本块,其ID是4D4D,每一个3DS文件的开头都是由这样一个块构成。基本块内的块称为主块。为了对块的层次结构有一个初步的认识,下面给出一个图表来说明不同类型(ID)的块及其各自在文件中的位置。如下图所示给每一个块都起了一个名字,这样更便于理解或直接转换为源程序。图6.1 3DS文件结构图颜色块是文件中自始至终都能见到的一种块类型,颜色块总共有3种类型,它们是COL_ RGB,COL_ TRU和COL_ UNK。6.2 存储3ds文件的数据结构本节将对程序中所用到的一些重要数据结构进行简单说明,以便使读者更进一步了解3ds文件所存储的数据。1t3Dmodel:struct t3DModel int numOfObjects;/ object的数量(总数)int numOfMaterials;/ materials 的数量(总数)vector pMaterials; / 存贮materials 信息的列表vector pObject;/ 存贮object信息的列表;2tMaterialInfo:struct tMaterialInfochar strName255;/ 材质的名字char strFile255; / 材质的文件名(真正存在harddisk的bmp)BYTE color3;/ object 的颜色(RGB) (0255)Int texureId;/ 材质的ID33DObject:struct t3DObject int numOfVerts;/ object 的顶点数int numOfFaces;/ object 的面数int numTexVertex;/ 材质坐标的个数int materialID;/ 材质ID ,正是材质数组中的索引bool bHasTexture;/有材质就为真char strName255;/ object的名字CVector3 *pVerts;/ object 的顶点,将会指向一个数组CVector3 *pNormals;/ object 的法线(只是一个近似的法线)将会指向一个数组CVector3 *pFaceNorm;/ 面的法线,将会指向一个数组CVector3 *pFaceVert1;/ 面的顶点x,将会指向一个数组CVector3 *pFaceVert2;/ 面的顶点y,将会指向一个数组CVector3 *pFaceVert3;/ 面的顶点z,将会指向一个数组CVector2 *pTexVerts;/ 材质的坐标(二维)将会指向一个数组tFace *pFaces;/ 面的结构(在后面有注解)将会指向一个数组;其中:struct tFaceint vertIndex3;/ 组成面三角形顶点的索引(整型)int coordIndex3;/ 组成面三角形材质的索引(整型);6.3 3D动画的实现读取3DS文件的全部操作封装在了CLoad3DS类中,有关CLoad3DS类的具体定义所附光盘中的源程序3ds.cpp and 3ds.h。/ IMPORT 3DS */This is called by the client to open the .3ds file, read it, then clean up/ *bool CLoad3DS:Import3DS(t3DModel *pModel, char *strFileName)char strMessage255 = 0;/ Open the 3DS filem_FilePointer = fopen(strFileName, rb);/ Make sure we have a valid file pointer (we found the file)if(!m_FilePointer) sprintf(strMessage, Unable to find the file: %s!, strFileName);MessageBox(NULL, strMessage, Error, MB_OK);return false;/ Once we have the file open, we need to read the very first data chunk/ to see if its a 3DS file. That way we dont read an invalid file./ If it is a 3DS file, then the first chunk ID will be equal to PRIMARY (some hex num)/ Read the first chuck of the file to see if its a 3DS fileReadChunk(m_CurrentChunk);/ Make sure this is a 3DS fileif (m_CurrentChunk-ID != PRIMARY)sprintf(strMessage, Unable to load PRIMARY chuck from file: %s!, strFileName);MessageBox(NULL, strMessage, Error, MB_OK);return false;/ Now we actually start reading in the data. ProcessNextChunk() is recursive/ Begin loading objects, by calling this recursive functionProcessNextChunk(pModel, m_CurrentChunk);/ After we have read the whole 3DS file, we want to calculate our own vertex normals.ComputeNormals(pModel);/ Clean up after everythingCleanUp();return true;See光盘中的源程序StepinGlView.cpp/ StepinGlView.cpp : implementation of the CStepinGlView class/ #include main.h/ This includes our header file#include 3ds.h/ Include the 3DS header file. #define FILE_NAME moon.3ds/ This is the 3D file we will load.UINT g_TextureMAX_TEXTURES = 0;/ This holds the texture info, referenced by an IDCLoad3DS g_Load3ds;/ This is 3DS class. This should go in a good model class.t3DModel g_3DModel;/ This holds the 3D Model info that we load inint g_ViewMode = GL_TRIANGLES;/ We want the default drawing mode to be normalbool g_bLighting = true;/ Turn lighting on initiallyfloat g_RotateX = 0.0f;/ This is the current value at which the model is rotatedfloat g_RotationSpeed = 0.8f;/ This is the speed that our model rotates. (-speed rotates left) / CStepinGlView message handlersvoid CStepinGlView:Init() PIXELFORMATDESCRIPTOR pfd; int n; HGLRChrc; m_pDC = new CClientDC(this); ASSERT(m_pDC != NULL); if (!bSetupPixelFormat() return; n =:GetPixelFormat(m_pDC-GetSafeHdc(); :DescribePixelFormat(m_pDC-GetSafeHdc(), n, sizeof(pfd), &pfd); hrc = wglCreateContext(m_pDC-GetSafeHdc(); wglMakeCurrent(m_pDC-GetSafeHdc(), hrc); GetClientRect(&m_oldRect); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST);/glMatrixMode(GL_MODELVIEW);/glLoadIdentity();g_Load3ds.Import3DS(&g_3DModel, FILE_NAME);/ Load our .3DS file into our model structure/ Depending on how many textures we found, load each one (Assuming .BMP)/ If you want to load other files than bitmaps, you will need to adjust CreateTexture()./ Below, we go through all of the materials and check if they have a texture map to load./ Otherwise, the material just holds the color information and we dont need to load a texture./ Go through all the materialsfor(int i = 0; i 0)/ Use the name of the texture file to load the bitmap, with a texture ID (i)./ We pass in our global texture array, the name of the texture, and an ID to reference it.CreateTexture(g_Texture, g_3DModel.pMaterialsi.strFile, i);/ Set the texture ID for this materialg_3DModel.pMaterialsi.texureId = i;glLightfv(GL_LIGHT0,GL_AMBIENT,m_lightAmb);glLightfv(GL_LIGHT0,GL_DIFFUSE,m_lightDif);glLightfv(GL_LIGHT0,GL_SPECULAR,m_lightSpe);glLightfv(GL_LIGHT0,GL_POSITION,m_lightPos);glEnable(GL_LIGHT0);/ Turn on a light with defaults setglEnable(GL_LIGHTING);/ Turn on lightingglEnable(GL_COLOR_MATERIAL);/ Allow colorvoid CStepinGlView:DrawScene()glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glMatrixMode(GL_MODELVIEW);glLoadIdentity(); glTranslatef(m_posx,m_posy,-5.0f); glRotatef(m_fAngleY, 0.0f, 1.0f, 0.0f); glRotatef(m_fAngleZ, 0.0f, 0.0f, 1.0f);for(int i = 0; i g_3DModel.numOfObjects; i+)/ Make sure we have valid objects just in case. (size() is in the vector class)if(g_3DModel.pObject.size() bHasTexture) / Turn on texture mapping and turn off colorglEnable(GL_TEXTURE_2D);/ Reset the color to normal againglColor3ub(255, 255, 255);/ Bind the texture map to the object by its materialIDglBindTexture(GL_TEXTURE_2D, g_TexturepObject-materialID); else / Turn off texture mapping and turn on colorglDisable(GL_TEXTURE_2D);/ Reset the color to normal againglColor3ub(255, 255, 255);/ This determines if we are in wireframe or normal modeglBegin(g_ViewMode);/ Begin drawing with our selected mode (triangles or lines)/ Go through all of the faces (polygons) of the object and draw themfor(int j = 0; j numOfFaces; j+)/ Go through each corner of the triangle and draw it.for(int whichVertex = 0; whichVertex pFacesj.vertIndexwhichVertex;/ Give OpenGL the normal for this vertex.glNormal3f(pObject-pNormals index .x, pObject-pNormals index .y, pObject-pNormals index .z);/ If the object has a texture associated with it, give it a texture coordinate.if(pObject-bHasTexture) / Make sure there was a UVW map applied to the object or else it wont have tex coords.if(pObject-pTexVerts) glTexCoord2f(pObject-pTexVerts index .x, pObject-pTexVerts index .y); else / Make

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论