model.c

00001 /*
00002 
00003   Velocity  v1.5
00004   Copyright 2006, 2007 Gabriel Anderson
00005 
00006   This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019 
00020 */
00021 
00022 //---------------------------------------------------------------------------------//
00023 // Includes                                                                        //
00024 //---------------------------------------------------------------------------------//
00025 
00026 #include "vlib.h"
00027 
00028 //---------------------------------------------------------------------------------//
00029 // Model                                                                           //
00030 //---------------------------------------------------------------------------------//
00031 
00032 static float __attribute__ ( ( aligned ( 16 ) ) ) bbox[8][3] =
00033         {
00034                 {
00035                         -1.0f, -1.0f, -1.0f
00036                 },
00037                 { -1.0f, -1.0f,  1.0f },
00038                 { -1.0f,  1.0f, -1.0f },
00039                 { -1.0f,  1.0f,  1.0f },
00040                 {  1.0f, -1.0f, -1.0f },
00041                 {  1.0f, -1.0f,  1.0f },
00042                 {  1.0f,  1.0f, -1.0f },
00043                 {  1.0f,  1.0f,  1.0f }
00044         };
00045 
00046 static void ReadStr ( FILE *f, char *string )
00047 {
00048         do {
00049                 fgets ( string, 255, f );
00050         } while ( ( string[0] == '/' ) || ( string[0] == '\n' ) );
00051 }
00052 
00053 static void compute_normals ( Model *mdl )
00054 {}
00055 
00056 static void load_obj ( const char *filename, Model *mdl )
00057 {
00058         FILE *fp;
00059         if ( ( fp = fopen ( filename, "rb" ) ) == NULL )
00060                 return;
00061 
00062         char strLine[255]= {0};
00063         char ch = 0;
00064 
00065         while (!feof(fp)) {
00066                 float x = 0.0f, y = 0.0f, z = 0.0f;
00067 
00068                 // Get the beginning character of the current line in the .obj file
00069                 ch = fgetc(fp);
00070 
00071                 switch (ch) {
00072                 case 'v':
00073 
00074                         // If we just read in a face line, then we have gone to another object,
00075                         // so we need to save the last object's data before moving onto the next one.
00076                         if (m_bJustReadAFace) {
00077                                 // Save the last object's info into our model structure
00078                                 FillInObjectInfo(pModel);
00079                         }
00080 
00081                         // Decipher this line to see if it's a vertex ("v"), normal ("vn"), or UV coordinate ("vt")
00082                         ReadVertexInfo();
00083                         break;
00084 
00085                 case 'f':
00086       // Read in as a face
00087                         ReadFaceInfo();
00088                         break;
00089 
00090                 case '\n':
00091                         // Newline, do nothing
00092                         break;
00093 
00094                 default:
00095                         // Read past
00096                         fgets(strLine, 100, m_FilePointer);
00097                         break;
00098                 }
00099         }
00100 
00101         // Now that we are done reading in the file, we have need to save the last object read.
00102         FillInObjectInfo(pModel);
00103         fclose ( fp );
00104 }
00105 
00106 Model *load_model ( const char *filename )
00107 {
00108         Model *mdl = ( Model* ) malloc ( sizeof ( Model ) );
00109         const char *suffix = strrchr ( filename, '.' );
00110 
00111         //if ( strcmp ( suffix, ".3ds" ) == 0 ) {}
00112         if ( strcmp ( suffix, ".obj" ) == 0 )
00113                 load_obj ( filename, mdl );
00114         //else if ( strcmp ( suffix, ".md2" ) == 0 )
00115         //else if ( strcmp ( suffix, ".blend" ) == 0 ) {}
00116         //else if ( strcmp ( suffix, ".maya" ) == 0 ) {}
00117         /*else { // This is our .mdl file. It's only temp until I can get some of the larger, more complex files finished
00118                 float x, y, z, u, v;
00119                 int numOfVertices = 0;
00120                 char oneLine[255];
00121                 // Read one line
00122                 ReadStr ( fp, oneLine );
00123                 // Find the number of triangles
00124                 sscanf ( oneLine, "verts %i\n", &mdl->vertTotal );
00125 
00126                 // Allocate memory for the vertices
00127                 mdl->verts = ( TP_Vertex_3D* ) malloc ( mdl->vertTotal * sizeof ( TP_Vertex_3D ) ); //  + 0x40000000;
00128 
00129                 // Load the geometry line by line
00130                 int loop;
00131                 for ( loop = 0; loop < mdl->vertTotal / 3; loop++ ) {
00132                         int vert;
00133                         for ( vert = 0; vert < 3; vert++ ) {
00134                                 ReadStr ( fp, oneLine );
00135                                 sscanf ( oneLine, "%f %f %f %f %f", &x, &y, &z, &u, &v );
00136                                 mdl->verts[numOfVertices].x = x;
00137                                 mdl->verts[numOfVertices].y = y;
00138                                 mdl->verts[numOfVertices].z = z;
00139                                 mdl->verts[numOfVertices].u = u;
00140                                 mdl->verts[numOfVertices].v = v;
00141                                 numOfVertices++;
00142                         }
00143                 }
00144         }*/
00145 
00146         sceKernelDcacheWritebackAll();
00147 
00148         /*sceGuStart(GU_CALL, t_displayList);
00149 
00150         sceGuBeginObject(GU_TEXTURE_32BITF|GU_VERTEX_32BITF|GU_TRANSFORM_3D, mdl->vertTotal, 0, mdl->verts);
00151         sceGuDrawArray(GU_TRIANGLES, GU_TEXTURE_32BITF|GU_VERTEX_32BITF|GU_TRANSFORM_3D, mdl->vertTotal, 0, mdl->verts);
00152         sceGuEndObject();
00153 
00154         sceGuFinish();
00155         sceGuSync(0,0);*/
00156 
00157         compute_normals ( mdl );
00158 
00159         mdl->posX = mdl->posY = mdl->posZ = 0.0f;
00160         mdl->sizeX = mdl->sizeY = mdl->sizeZ = 1.0f;
00161         mdl->rotX = mdl->rotY = mdl->rotZ = 0.0f;
00162 
00163         return mdl;
00164 }
00165 
00166 void unload_model ( Model *mdl )
00167 {
00168         free ( mdl->displayList );
00169         free ( mdl->verts );
00170         free ( mdl );
00171 }
00172 
00173 void draw_model ( Model *mdl, Image *img )
00174 {
00175         // Reset matrices
00176         vfpu_identity_m ( &m_projection_model );
00177 
00178         // Modify the matrices
00179         vfpu_translate_m ( &m_projection_model, mdl->posX, mdl->posY, mdl->posZ );
00180         vfpu_rotateXYZ_m ( &m_projection_model, mdl->rotX, mdl->rotY, mdl->rotZ );
00181         vfpu_scale_m ( &m_projection_model, mdl->sizeX, mdl->sizeY, mdl->sizeZ );
00182 
00183         // Set the new matrices for use
00184         sceGuSetMatrix ( GU_PROJECTION, &m_projection );
00185         sceGuSetMatrix ( GU_VIEW, &m_projection_view );
00186         sceGuSetMatrix ( GU_MODEL, &m_projection_model );
00187 
00188         sceGuTexImage ( 0, img->realWidth, img->realHeight, img->realWidth, ( void* ) img->data );
00189 
00190         sceGuBeginObject ( GU_VERTEX_32BITF, 8, 0, bbox );
00191         sceGuDrawArray ( GU_TRIANGLES, GU_TEXTURE_32BITF | GU_VERTEX_32BITF | GU_TRANSFORM_3D, mdl->vertTotal, 0, mdl->verts );
00192         sceGuEndObject();
00193 
00194         //sceGuCallList(mdl->displayList);
00195 }
00196 
00197 void set_fog ( float near, float far, unsigned int color )
00198 {
00199         if ( drawingStarted ) {
00200                 sceGuEnable ( GU_FOG );
00201                 sceGuFog ( near, far, color );
00202                 return;
00203         }
00204 
00205         sceGuStart ( GU_DIRECT, DList );
00206         sceGuEnable ( GU_FOG );
00207         sceGuFog ( near, far, color );
00208         sceGuFinish();
00209         sceGuSync ( 0, 0 );
00210 
00211 }
00212 
00213 void disable_fog ( void )
00214 {
00215         if ( drawingStarted ) {
00216                 sceGuDisable ( GU_FOG );
00217                 return;
00218         }
00219 
00220         sceGuStart ( GU_DIRECT, DList );
00221         sceGuDisable ( GU_FOG );
00222         sceGuFinish();
00223         sceGuSync ( 0, 0 );
00224 }
00225 
00226 //---------------------------------------------------------------------------------//
00227 // Camera                                                                          //
00228 //---------------------------------------------------------------------------------//
00229 
00230 void set_camera_3rd_person ( float posX, float posY, float posZ, float rotX, float rotY, float zoom )
00231 {
00232         // Reset
00233         vfpu_identity_m ( &m_projection_view );
00234         vfpu_identity_m ( &m_projection_model );
00235 
00236         // Setup
00237         ScePspFVector3 eye = ( ScePspFVector3 ) {
00238                                      posX - ( zoom * vfpu_sinf ( rotY ) * vfpu_cosf ( rotX ) ),
00239                                      posY + ( zoom * vfpu_sinf ( rotX ) ),
00240                                      posZ + ( zoom * vfpu_cosf ( rotY ) * vfpu_cosf ( rotX ) )
00241                              };
00242 
00243         ScePspFVector3 center = ( ScePspFVector3 ) {
00244                                         posX, posY, posZ
00245                                 };
00246         ScePspFVector3 up = ( ScePspFVector3 ) {
00247                                     0.0f, 1.0f, 0.0f
00248                             };
00249 
00250         // Set
00251         vfpu_lookAt_m ( &m_projection_view, &eye, &center, &up );
00252 }

Generated on Tue Mar 20 23:01:06 2007 for vLib by  doxygen 1.4.7