Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

ALoad.cpp

Go to the documentation of this file.
00001 /*
00002 CS Senior Project 2003
00003 Team : Leftfield
00004 Project : ModernWarfare
00005 Members :
00006 - Russ Christensen              <rchriste@cs.utah.edu>
00007 - Todd Smith                    <tcsmith@cs.utah.edu>
00008 - Usit Duongsaa                 <duongsaa@cs.utah.edu>
00009 Copyright 2003 Russ Christensen, Usit Duongsaa, and Todd Smith. All rights reserved.
00010 
00011 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
00012 
00013 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
00014 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 
00015 THIS SOFTWARE IS PROVIDED BY RUSS CHRISTENSEN, USIT DUONGSAA, AND TODD SMITH ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RUSS, USIT, TODD OR OTHER CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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         // read it vertex pos, texture, normal arrays
00047         while(true)
00048         {
00049                 f >> cmd;
00050                 if( cmd=="#" )                          // just a comment line.  ignore it              
00051                 {
00052                         getline( f, cmd );
00053                 }
00054                 else if( cmd=="v" )                     // vertex position
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" )            // texture coordinate
00063                 {
00064                         f >> f1 >> f2 >> f3;
00065                         vec_uv.push_back( Vec2D(f1,1.0f-f2) );
00066                 }
00067                 else if( cmd=="vn" )            // normal
00068                 {
00069                         f >> f1 >> f2 >> f3;
00070                         vec_normal.push_back( Vec3D(f1,f2,f3) );
00071                 }
00072                 else if( cmd=="g"  )            // end of vertex info list
00073                 {
00074                         getline( f, cmd );
00075                         break;
00076                 }
00077         }
00078 
00079         // read in triangle indices, create&map tuples
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                 // read the 3 corners of a triangle
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);            // -1 because .obj is 1-based
00097                         iter = tuplesMap.find(tuple);
00098                         if( iter==tuplesMap.end() )                             // if this vertex is new, add it
00099                         {
00100                                 tuplesMap[tuple] = (int) vertices.size();
00101                                 indices.push_back( (USHORT)vertices.size() );
00102                                 vertices.push_back(tuple);                      
00103                         }
00104                         else                                                                    // but if it's already there, use existing vertex number
00105                                 indices.push_back( iter->second );
00106                 }
00107 
00108         }
00109 
00110         // concat new data to entire Mesh VB&IB
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++ )         // add all vertices
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++ )          // add all indices
00123         {
00124                 AS.mesh_raw_ib.push_back( indices[i]+vertexOffset );
00125         }
00126 
00127         // add MeshEntry
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 //------------------------------------------------------------------------------------

Generated on Wed Apr 23 05:50:13 2003 for Modern Warfare by doxygen1.3-rc2