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

LightwaveObjects.cpp

Go to the documentation of this file.
00001 
00002 /*
00003     TSGL - Teddy Space Game Library
00004     Copyright (C) 2002 Timo Suoranta
00005     tksuoran@cc.helsinki.fi
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2.1 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021     $Id: LightwaveObjects.cpp,v 1.1 2002/01/08 20:47:05 tksuoran Exp $
00022 */
00023 
00024 
00025 #include "Teddy/SpaceGame/ObjectManager.h"
00026 #include "Teddy/Graphics/View.h"
00027 #include "Teddy/Imports/LWMesh.h"
00028 #include "Teddy/Models/ModelInstance.h"
00029 #include "Teddy/Scenes/Scene.h"
00030 #include "Teddy/SysSupport/FileScan.h"
00031 #include "Teddy/SysSupport/Messages.h"
00032 #include <cstdio>
00033 using namespace Teddy::Graphics;
00034 using namespace Teddy::Imports;
00035 using namespace Teddy::Models;
00036 using namespace Teddy::Scenes;
00037 
00038 
00039 namespace Teddy     {
00040 namespace SpaceGame {
00041 
00042 
00043 #define SPACE 5
00044 
00045 
00051 void ObjectManager::loadLWO(){
00052     lwo_debug_msg( "UI::loadLWO..." );
00053 
00054     DoubleVector v;
00055     double       cursor = -400.0;
00056     int          i;
00057 
00058     FileScan lwos( "lwo/*.lwo" );
00059     int num = lwos.get_files().size();
00060 
00061     if( num >0 ){
00062         //con << ": Found " << num << " lightwave objects" << endl;
00063         //view->display();
00064     }else{
00065         //con << ": No lightwave files found" << endl;
00066         //view->display();
00067         return;
00068     }
00069 
00070     list<char*>::iterator f_it = lwos.get_files().begin();
00071     char *fname;
00072 
00073     //  -----  Read In Files -----
00074     double  line_z[1000];
00075     double  line_x[1000];
00076     Mesh   *lwom  [1000];
00077 
00078     int per_line      = (int)(::sqrt( num ));
00079     int lines         = num/per_line+1;
00080     int count         = 0;
00081     int cur_line      = 0;
00082     int items_on_line = 0;
00083 
00084     //  Initialize each line to width 0
00085     for( i=0; i<lines; i++ ){
00086         line_z[i] = 0;
00087         line_x[i] = 0;
00088     }
00089     for( i=0; i<num; i++ ){
00090         lwom[i] = NULL;
00091     }
00092 
00093     double total_z      = 0;
00094     double prev_ob_size = 0;
00095 
00096     //  Read in all files and slice them to lines
00097     while( f_it != lwos.get_files().end() ){
00098         double ob_rad;
00099         fname = new char[ strlen((*f_it))+5 ];
00100 #       if defined( _MSC_VER )
00101 
00102         sprintf( fname, "lwo/%s",  *f_it );
00103 
00104 #       else
00105 
00106         sprintf( fname, "%s",  *f_it );
00107 
00108 #       endif
00109 
00110         lwom[count] = new LWMesh( fname, 0 /*LWFILE_OPTION_SKIP_MATERIAL_M*/ );
00111         if( lwom[count] != NULL ){
00112             ob_rad = lwom[count]->getClipRadius();
00113         }else{
00114             ob_rad = 0;
00115         }
00116         count++;
00117         items_on_line++;
00118 
00119         //  Line full enough?
00120         if( items_on_line > per_line ){
00121             line_z[cur_line] += SPACE;  //  Add some space
00122             total_z += line_z[cur_line];
00123             cur_line++;
00124             items_on_line = 0;
00125         }
00126 
00127         //  Make sure line is wide enough
00128         line_x[cur_line] += ob_rad + SPACE;  //  Add some space
00129         if( line_z[cur_line] < ob_rad*2 ){
00130             line_z[cur_line] = ob_rad*2;
00131         }
00132         // con << "Processed file " << fname << endl; view->display();
00133         //con << ".";
00134         //view->display();
00135         f_it++;
00136     }
00137     total_z += line_z[cur_line];
00138 
00139     //  Place all lines so that:
00140     //   - Each line has own Z, centered on X
00141     //   - All lines together are centered in Z
00142     cur_line        = 0;
00143     items_on_line   = 0;
00144     double z_offset = -total_z / 2;
00145     double x_offset = -line_x[cur_line] / 2;
00146 
00147     z_offset -= line_z[cur_line]/2;
00148 
00149     for( i=0; i<num; i++ ){
00150         if( lwom[i] == NULL ){
00151             lwo_debug_msg( "NULL Mesh\n" );
00152             continue;
00153         }
00154         ModelInstance *mi = new ModelInstance(
00155             lwom[i]->getName(),
00156             lwom[i]
00157         );
00158         x_offset += lwom[i]->getClipRadius()*1.1f + SPACE;
00159         mi->setPosition( x_offset, 0, z_offset );
00160 
00161 /*      switch( i %3 ){
00162         case 0: mi->tick_rotation = DoubleVector( 0.17f, 0.15f, 0.13f ); break;
00163         case 1: mi->tick_rotation = DoubleVector(-0.13f,-0.17f,-0.15f ); break;
00164         case 2: mi->tick_rotation = DoubleVector( 0.15f, 0.13f, 0.17f ); break;
00165         default: break;
00166         }*/
00167 
00168         scene->addInstance( mi );
00169 
00170         x_offset += lwom[i]->getClipRadius()*1.1f + SPACE;
00171         items_on_line++;
00172         if( items_on_line > per_line ){
00173             z_offset += line_z[cur_line]/2;
00174             cur_line++;
00175             z_offset += line_z[cur_line]/2;
00176             items_on_line = 0;
00177             x_offset = -line_x[cur_line] / 2;
00178         }
00179 
00180     }
00181 
00182 //  con << endl;
00183 //  view->display();
00184 }
00185 
00186 
00187 };  //  namespace SpaceGame
00188 };  //  namespace Teddy
00189