#includetotal.h#includewindows.h#includemath.h#includefstream#includevector#includecrtdbg.h#includestdio.h#includestdlib.h#includetime.h#includeinclude\opengl\gl.h#includeinclude\opengl\glu.h#includeinclude\opengl\glaux.h#includeinclude\opengl\glut.h#includeinclude\opengl\glext.h#includeinclude\opengl\wglext.h#defineGLUT_DISABLE_ATEXIT_HACK#defineFILE_NAMEfeiji.3DS//指定对应的FILENAME#defineSCREEN_WIDTH800#defineSCREEN_HEIGHT600#defineSCREEN_DEPTH16#defineMAX_TEXTURES100//最大的纹理数目#definePRIMARY0x4D4D//基本块(PrimaryChunk),位于文件的开始//主块(MainChunks)#defineOBJECTINFO0x3D3D//网格对象的版本号#defineVERSION0x0002//.3ds文件的版本#defineEDITKEYFRAME0xB000//所有关键帧信息的头部//对象的次级定义(包括对象的材质和对象)#defineMATERIAL0xAFFF//保存纹理信息#defineOBJECT0x4000//保存对象的面、顶点等信息//材质的次级定义#defineMATNAME0xA000//保存材质名称#defineMATDIFFUSE0xA020//对象/材质的颜色#defineMATMAP0xA200//新材质的头部#defineMATMAPFILE0xA300//保存纹理的文件名#defineOBJECT_MESH0x4100//新的网格对象//OBJECT_MESH的次级定义#defineOBJECT_VERTICES0x4110//对象顶点#defineOBJECT_FACES0x4120//对象的面#defineOBJECT_MATERIAL0x4130//对象的材质#defineOBJECT_UV0x4140//对象的UV纹理坐标intg_ViewMode=GL_TRIANGLES;boolg_bLighting=true;floatg_Rotatex=0;floatg_Rotatey=0;floatg_Rotatez=0;floatg_x=0;floatg_y=0;floatg_z=0;//。。。。。。。。。。。。。。。。。。。。定义需要导入的数据floatRotatex;floatRotatey;floatRotatez;floatx;floaty;floatz;doublegl_x;doublegl_y;doublegl_z;doublegl_pitch;doublegl_roll;doublegl_yaw;doublegl_wcpitch;doublegl_wcroll;doublegl_wcyaw;usingnamespacestd;UINTg_Texture[MAX_TEXTURES]={0};HWNDg_hWnd;RECTg_rRect;HDCg_hDC;HGLRCg_hRC;HINSTANCEg_hInstance;HINSTANCEhInstance;MMRESULTidtimer_Opengl;HWNDCreateMyWindow(LPSTRstrWindowName,intwidth,intheight,DWORDdwStyle,HINSTANCEhInstance);//生成用户窗口LRESULTCALLBACKWinProc(HWNDgl_hwnd,UINTmessage,WPARAMwParam,LPARAMlParam);//消息响应voidInit(HWNDgl_hWnd);//初始化整个程序voidInitializeOpenGL(intwidth,intheight);//初始化OpenGLboolbSetupPixelFormat(HDChdc);//设置像素格式voidSizeOpenGLScreen(intwidth,intheight);//初始化投影变换intMainLoop();//主循环voidRenderScene(doublegl_x,doublegl_y,doublegl_z,doublegl_roll,doublegl_pitch,doublegl_yaw);voidDeInit();//释放程序占用的内存空间voidmyDisplaycube();//绘制外框架voidglprintf();//输出文字//----------------定义结构变量-----------------------------------------------------------------------------//定义3D点的类,用于保存模型中的顶点classCVector3{public:floatx,y,z;};//定义2D点类,用于保存模型的UV纹理坐标classCVector2{public:floatx,y;};//面的结构定义structtFace{intvertIndex[3];//顶点索引intcoordIndex[3];//纹理坐标索引};//材质信息结构体structtMaterialInfo{charstrName[255];//纹理名称charstrFile[255];//如果存在纹理映射,则表示纹理文件名称BYTEcolor[3];//对象的RGB颜色inttexureId;//纹理IDfloatuTile;//u重复floatvTile;//v重复floatuOffset;//u纹理偏移floatvOffset;//v纹理偏移};//对象信息结构体structt3DObject{intnumOfVerts;//模型中顶点的数目intnumOfFaces;//模型中面的数目intnumTexVertex;//模型中纹理坐标的数目intmaterialID;//纹理IDcharstrName[255];//对象的名称CVector3*pVerts;//对象的顶点CVector3*pNormals;//对象的法向量CVector2*pTexVerts;//纹理UV坐标tFace*pFaces;//对象的面信息};//模型信息结构体structt3DModel{intnumOfObjects;//模型中对象的数目intnumOfMaterials;//模型中材质的数目vectortMaterialInfopMaterials;//材质链表信息vectort3DObjectpObject;//模型中对象链表信息};structtIndices{unsignedshorta,b,c,bVisible;};//保存块信息的结构structtChunk{unsignedshortintID;//块的IDunsignedintlength;//块的长度unsignedintbytesRead;//需要读的块数据的字节数};//CLoad3DS类处理所有的装入代码classCLoad3DS{public://可以被该类中的函数、子类的函数、其友元函数访问,也可以由该类的对象访问CLoad3DS();//初始化数据成员//装入3ds文件到模型结构中boolImport3DS(t3DModel*pModel,char*strFileName);//只能由该类中的函数、其友元函数访问,不能被任何其他访问,该类的对象也不能访问//读一个字符串intGetString(char*);//读下一个块voidReadChunk(tChunk*);//读下一个块voidProcessNextChunk(t3DModel*pModel,tChunk*);//读下一个对象块voidProcessNextObjectChunk(t3DModel*pModel,t3DObject*pObject,tChunk*);//读下一个材质块voidProcessNextMaterialChunk(t3DModel*pModel,tChunk*);//读对象颜色的RGB值voidReadColorChunk(tMaterialInfo*pMaterial,tChunk*pChunk);//读对象的顶点voidReadVertices(t3DObject*pObject,tChunk*);//读对象的面信息voidReadVertexIndices(t3DObject*pObject,tChunk*);//读对象的纹理坐标voidReadUVCoordinates(t3DObject*pObject,tChunk*);//读赋予对象的材质名称voidReadObjectMaterial(t3DModel*pModel,t3DObject*pObject,tChunk*pPreviousChunk);//计算对象顶点的法向量voidComputeNormals(t3DModel*pModel);//关闭文件,释放内存空间voidCleanUp();//文件指针FILE*m_FilePointer;tChunk*m_CurrentChunk;//当前块tChunk*m_TempChunk;//下一个块};CLoad3DSg_Load3ds;t3DModelg_3DModel;//.........................程序函数............................................................................intOpenGL_Main()//入口程序{gl_hWnd=CreateMyWindow(飞机动态飞行模拟,SCREEN_WIDTH,SCREEN_HEIGHT,0,hInstance);if(gl_hWnd==NULL)returntrue;Init(gl_hWnd);//初始化整个程序MainLoop();return0;}//创建窗口HWNDCreateMyWindow(LPSTRstrWindowName,intwidth,intheight,DWORDdwStyle,HINSTANCEhInstance){HWNDgl_hWnd;WNDCLASSwndclass;/*wndclass用来保存我们的窗口类的结构。窗口类结构中保存着我们的窗口信息。通过改变类的不同字段我们可以改变窗口的外观和行为。每个窗口都属于一个窗口类。当您创建窗口时,您必须为窗口注册类。*/memset(&wndclass,0,sizeof(WNDCLASS));wndclass.style=CS_HREDRAW|CS_VREDRAW;//设置窗口风格wndclass.lpfnWndProc=WinProc;//WndProc处理消息其实就是键盘响应wndclass.hInstance=hInstance;//设置实例wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);//装入缺省图标要使用windows预定义的图标,这时hInstance必须设置成NULLwndclass.hCursor=LoadCursor(NULL,IDC_ARROW);//装入鼠标指针wndclass.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);//设置opengl的背景色wndclass.lpszClassN