00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00022 #include "AEngine.h"
00023 #include "AUtil.h"
00024 #include "ASharedFunc.h"
00025
00026
00027 AFont load_font( string fontName, string filename )
00028 {
00029 AS.res_fonts.push_back( util_loadFont(fontName,filename,18) );
00030 return (int)AS.res_fonts.size()-1;
00031 }
00032
00033 AMesh load_mesh( string filename )
00034 {
00035 ifstream f(filename.c_str());
00036 if(!f) throw Error("Can't read mesh file : "+filename );
00037
00038 vector<Vec3D> vec_pos, vec_normal;
00039 vector<Vec2D> vec_uv;
00040 string cmd;
00041 FLOAT f1, f2, f3;
00042 FLOAT minX, maxX, minY, maxY, minZ, maxZ;
00043 minX = minY = minZ = 100000.0f;
00044 maxX = maxY = maxZ = -100000.0f;
00045
00046
00047 while(true)
00048 {
00049 f >> cmd;
00050 if( cmd=="#" )
00051 {
00052 getline( f, cmd );
00053 }
00054 else if( cmd=="v" )
00055 {
00056 f >> f1 >> f2 >> f3;
00057 vec_pos.push_back( Vec3D(f1,f2,f3) );
00058 if(f1<minX) minX=f1; else if(f1>maxX) maxX=f1;
00059 if(f2<minY) minY=f2; else if(f2>maxY) maxY=f2;
00060 if(f3<minZ) minZ=f3; else if(f3>maxZ) maxZ=f3;
00061 }
00062 else if( cmd=="vt" )
00063 {
00064 f >> f1 >> f2 >> f3;
00065 vec_uv.push_back( Vec2D(f1,1.0f-f2) );
00066 }
00067 else if( cmd=="vn" )
00068 {
00069 f >> f1 >> f2 >> f3;
00070 vec_normal.push_back( Vec3D(f1,f2,f3) );
00071 }
00072 else if( cmd=="g" )
00073 {
00074 getline( f, cmd );
00075 break;
00076 }
00077 }
00078
00079
00080 map< Int3Tuple, int > tuplesMap;
00081 vector<Int3Tuple> vertices;
00082 vector<USHORT> indices;
00083 map<Int3Tuple,int>::iterator iter;
00084 char skip;
00085 int posIndex, uvIndex, normalIndex;
00086
00087 while(true)
00088 {
00089 f >> cmd;
00090 if( cmd!="f" ) break;
00091
00092
00093 for( int corner=0; corner<3; corner++ )
00094 {
00095 f >> posIndex >> skip >> uvIndex >> skip >> normalIndex;
00096 Int3Tuple tuple(posIndex-1,uvIndex-1,normalIndex-1);
00097 iter = tuplesMap.find(tuple);
00098 if( iter==tuplesMap.end() )
00099 {
00100 tuplesMap[tuple] = (int) vertices.size();
00101 indices.push_back( (USHORT)vertices.size() );
00102 vertices.push_back(tuple);
00103 }
00104 else
00105 indices.push_back( iter->second );
00106 }
00107
00108 }
00109
00110
00111 int vertexOffset = (int) AS.mesh_raw_vb.size();
00112 int indexOffset = (int) AS.mesh_raw_ib.size();
00113 MeshVertex v;
00114 for( UINT i=0; i<vertices.size(); i++ )
00115 {
00116 v.pos = vec_pos[ vertices[i].a ];
00117 v.uv = vec_uv[ vertices[i].b ];
00118 v.normal = vec_normal[ vertices[i].c ];
00119 v.cindex = 0;
00120 AS.mesh_raw_vb.push_back(v);
00121 }
00122 for( UINT i=0; i<indices.size(); i++ )
00123 {
00124 AS.mesh_raw_ib.push_back( indices[i]+vertexOffset );
00125 }
00126
00127
00128 MeshEntry entry;
00129 entry.IBstart = indexOffset;
00130 entry.TriCount = (UINT) indices.size() / 3;
00131 entry.VBcount = (UINT) vertices.size();
00132 entry.VBmin = vertexOffset;
00133 entry.size = Vec3D( maxX-minX, maxY-minY, maxZ-minZ );
00134 AS.res_meshes.push_back( entry );
00135
00136 return (int)AS.res_meshes.size() - 1;
00137 }
00138
00139 ATexture load_texture( string filename )
00140 {
00141 LPDIRECT3DTEXTURE8 pTexture;
00142 HRESULT hr = D3DXCreateTextureFromFile( AS.pDevice, filename.c_str(), &pTexture );
00143 if( FAILED(hr) ) throw Error("Cannot Load Texture : "+filename);
00144 AS.res_textures.push_back(pTexture);
00145 return (int)AS.res_textures.size()-1;
00146 }
00147
00148 ATerrain load_terrain( string filename, FLOAT sizeX, FLOAT sizeY, FLOAT sizeZ, FLOAT offsetZ )
00149 {
00150 return terrain_create(filename,sizeX,sizeY,sizeZ,offsetZ);
00151 }
00152