lwo.hpp

Go to the documentation of this file.
00001 #ifndef LWO_H
00002 #define LWO_H
00003 
00004 /* Author: Jussi R�s�en */
00005 
00006 #include <vector>
00007 #include <string>
00008 #include <math.h>
00009 #include <assert.h>
00010 #include <stdio.h>
00011 #include "Vector2.hpp"
00012 #include "Vector3.hpp"
00013 #include "Vector4.hpp"
00014 
00015 class Color
00016 {
00017 public:
00018   unsigned char r;
00019   unsigned char g;
00020   unsigned char b;
00021   unsigned char a;
00022   void set(const Vector3& v)
00023   {
00024     r=(unsigned char) (v.x*255.0f);
00025     g=(unsigned char) (v.y*255.0f);
00026     b=(unsigned char) (v.z*255.0f);
00027     a=255;
00028   }
00029 };
00030 
00031 class File
00032 {
00033 public:
00034   void* load(const char* fname)
00035   {
00036     FILE *fp = fopen(fname,"rb");
00037     if(fp == 0) return 0;
00038     fseek(fp,0,SEEK_END);
00039     int size = ftell(fp);
00040     fseek(fp,0,SEEK_SET);
00041     buf = new char[size];
00042     fread(buf,1,size,fp);
00043     fclose(fp);
00044     return buf;
00045   }
00046   void free(char *buf)
00047   {
00048     delete [] buf;
00049   }
00050 
00051 private:
00052   char *buf;
00053 };
00054 
00055 #ifndef UCHAR
00056 typedef unsigned char UCHAR;
00057 typedef unsigned int UINT;
00058 typedef unsigned short USHORT;
00059 #endif
00060 
00061 #ifndef CHAR
00062 //typedef signed char CHAR;
00063 typedef signed int INT;
00064 #endif
00065 
00066 #ifndef FLT_MAX
00067 #define FLT_MAX HUGE_VAL
00068 #endif
00069 
00070 //normalize_0 on tavallinen normalisointi + jos vektorin pituus on 0, tulos on nollavektori
00071 // operaattori | vektoreiden v�ill�on pistetulo ja * ristitulo
00072 
00073 //=============================================================================
00074 //=============================================================================
00075 
00076 typedef Vector4 M_Vector4;
00077 typedef Vector3 M_Vector3;
00078 typedef Vector2 M_Vector2;
00079 
00080 class LWOReader
00081 {
00082 public:
00083 
00084   struct Vertex
00085   {
00086     void      null()
00087     {
00088       m_position.clear();
00089       m_normal.clear();
00090       m_uv.clear();
00091       m_diffuse.clear();
00092     }
00093     void      add( const Vertex &v )
00094     {
00095       m_position += v.m_position;
00096       m_normal += v.m_normal;
00097       m_uv += v.m_uv;
00098       m_diffuse += v.m_diffuse;
00099     }
00100     void      mad( const Vertex &v, float c )
00101     {
00102       m_position += v.m_position * c;
00103       m_normal += v.m_normal * c;
00104       m_uv += v.m_uv * c;
00105       m_diffuse += v.m_diffuse * c;
00106     }
00107     void      normalize()
00108     {
00109       m_normal.normalize();
00110     }
00111     M_Vector3  m_position;
00112     M_Vector3  m_normal;
00113     int    m_triangleUsing; //30msb polyindex 2lsb edgeindex
00114     M_Vector2  m_uv;
00116     M_Vector4  m_diffuse;
00117   };
00118 
00119   struct Triangle
00120   {
00121     M_Vector3  m_normal;
00122     float   m_area;
00123     int    m_surface;
00124     int    m_v[3];
00125     char   m_dummy;
00126   };
00127 
00128   struct Edge
00129   {
00130     bool operator<( const Edge &e ) const { if( m_v[0] < e.m_v[0] ) return true; else if( m_v[0] > e.m_v[0] ) return false; else return (m_v[1] < e.m_v[1]) ? true : false; }
00131     int    m_v[2];
00132     int    m_t[2];
00133   };
00134 
00136   struct Surface
00137   {
00138     std::string  m_name;
00139     Color   m_color;
00140     int    m_polygons;
00141   };
00142 
00143   LWOReader( const char *fileName );
00144   ~LWOReader();
00145 
00146   Vertex    &getVertex( uint i )    { assert(i<m_vertices.size()); return m_vertices[i]; }
00147   Triangle   &getTriangle( uint i )   { assert(i<m_triangles.size()); return m_triangles[i]; }
00148   Edge    &getEdge( uint i )    { assert(i<m_edges.size()); return m_edges[i]; }
00149   Surface    &getSurface( uint i )   { assert(i<m_surfaces.size()); return m_surfaces[i]; }
00150 
00151   uint     getVertexCount() const   { return m_vertices.size(); }
00152   uint     getTriangleCount() const  { return m_triangles.size(); }
00153   uint     getEdgeCount() const   { return m_edges.size(); }
00154   uint     getSurfaceCount() const   { return m_surfaces.size(); }
00155 
00156   const M_Vector3  &getBoundingBoxMin() const  { return m_boundingBoxMin; }
00157   const M_Vector3  &getBoundingBoxMax() const  { return m_boundingBoxMax; }
00158   float    getBoundingRadius() const  { return m_boundingRadius; }
00159 
00160 private:
00161   std::vector<Vertex>  m_vertices;
00162   std::vector<Triangle> m_triangles;
00163   std::vector<Edge>  m_edges;
00164   std::vector<Surface> m_surfaces;
00165   std::string    m_filename;
00166 
00167   M_Vector3    m_boundingBoxMin;
00168   M_Vector3    m_boundingBoxMax;
00169   float     m_boundingRadius;
00170 
00171   static UCHAR   *m_fileptr;
00172   static UCHAR   *m_nextchunkptr;
00173   static UCHAR   *m_nextsubchunkptr;
00174   static UCHAR   *m_fileend;
00175   static UCHAR   *m_readptr;
00176 
00177   UINT     readID4();
00178   int      readI1();
00179   int      readI2();
00180   int      readI4();
00181   UCHAR     readU1();
00182   USHORT     readU2();
00183   UINT     readU4();
00184   float     readF4();
00185   const char    *readS0();
00186   UINT     readVX();
00187   M_Vector3    readCOL12();
00188   M_Vector3    readVEC12();
00189 
00190   void     loadLW5( );
00191   void     loadLW6( );
00192 };
00193 
00194 //=============================================================================
00195 //=============================================================================
00196 
00197 #endif

Generated on Mon Mar 12 21:09:00 2007 for VEE - The Visual Effects Engine by  doxygen 1.4.6