VCRA-SZCZERBACKI-PointBasedRendering/lwo5.cpp

00001 #include "Mesh.h"
00002 
00003 //=======================================================================
00004 //=======================================================================
00005 
00006 void Mesh::loadLW5( )
00007 {
00008         int chunklen,chunkname,subchunkname,subchunklen;
00009         int i,number,vtable[256];
00010         Vector3 p;
00011         float r;
00012         Color color,spec;
00013         UINT8 *chunks, *chunkend,*subchunkend,*vertexPointer = NULL;
00014 
00015         chunks = m_readptr;
00016 
00017         m_boundingBoxMin.set(FLT_MAX,FLT_MAX,FLT_MAX);
00018         m_boundingBoxMax.set(-FLT_MAX,-FLT_MAX,-FLT_MAX);
00019         m_boundingRadius = 0.0f;
00020 
00021 // read file and find out surface,vertex and polygon counts
00022         m_nextchunkptr = chunks;
00023         while(m_nextchunkptr < m_fileend)
00024         {
00025                 m_readptr = m_nextchunkptr;
00026                 chunkname = readID4();
00027                 chunklen = readU4();
00028                 chunkend = m_readptr + chunklen;
00029                 m_nextchunkptr = chunkend;
00030                 if( chunklen & 1 ) m_nextchunkptr++;    //if size is odd, add 1 padding
00031 
00032                 switch(chunkname)
00033                 {
00034                 case 'PNTS':
00035                         vertexPointer = m_readptr;
00036                         m_vertices.resize( chunklen / 12 );
00037                         for(i=0;i<m_vertices.size();i++)
00038                         {
00039                                 p = readVEC12();
00040                                 m_vertices[i].m_position = p;
00041                                 m_vertices[i].m_normal.set(0,0,0);
00042                                 m_vertices[i].m_triangleUsing = -1;
00043                                 if( p.x > m_boundingBoxMax.x ) m_boundingBoxMax.x = p.x;
00044                                 if( p.x < m_boundingBoxMin.x ) m_boundingBoxMin.x = p.x;
00045                                 if( p.y > m_boundingBoxMax.y ) m_boundingBoxMax.y = p.y;
00046                                 if( p.y < m_boundingBoxMin.y ) m_boundingBoxMin.y = p.y;
00047                                 if( p.z > m_boundingBoxMax.z ) m_boundingBoxMax.z = p.z;
00048                                 if( p.z < m_boundingBoxMin.z ) m_boundingBoxMin.z = p.z;
00049 
00050                                 r = dot(p, p);
00051                                 if(r > m_boundingRadius) m_boundingRadius = r;
00052                         }
00053                         break;
00054                 case 'SRFS':
00055                         while(m_readptr < chunkend)
00056                         {
00057                                 static Surface s;
00058                                 s.m_name = readS0();
00059                                 s.m_polygons = 0;
00060                                 m_surfaces.push_back( s );
00061                         }
00062                         break;
00063                 default:
00064                         break;
00065                 }
00066         }
00067 
00068         m_nextchunkptr = chunks;
00069         while(m_nextchunkptr < m_fileend)
00070         {
00071                 m_readptr = m_nextchunkptr;
00072                 chunkname = readID4();
00073                 chunklen = readU4();
00074                 chunkend = m_readptr + chunklen;
00075                 m_nextchunkptr = chunkend;
00076                 if( chunklen & 1 ) m_nextchunkptr++;    //if size is odd, add 1 padding
00077 
00078                 switch(chunkname)
00079                 {
00080                 case 'POLS':
00081                         while(m_readptr < chunkend)
00082                         {
00083                                 int vert = readU2();
00084                                 if(vert >= 3)
00085                                 {
00086                                         for(i=0;i<vert;i++)
00087                                         {
00088                                                 vtable[i] = readU2();
00089                                         }
00090                                 }
00091                                 else
00092                                 {
00093                                         m_readptr += 2*vert;
00094                                 }
00095 
00096                                 int surf = readI2();
00097                                 if(vert >= 3)
00098                                 {
00099                                         static Triangle t;
00100                                         static Vector3 n,e1,e2;
00101                                         t.m_surface = SS::abs(surf) - 1;
00102                                         SS_ASSERT( t.m_surface < m_surfaces.size() );
00103                                         m_surfaces[t.m_surface].m_polygons += vert-2;
00104                                         t.m_n[0] = -1;
00105                                         t.m_n[1] = -1;
00106                                         t.m_n[2] = -1;
00107 
00108                                         e1 = m_vertices[vtable[1]].m_position;
00109                                         e1 -= m_vertices[vtable[0]].m_position;
00110                                         e2 = m_vertices[vtable[vert-1]].m_position;
00111                                         e2 -= m_vertices[vtable[0]].m_position;
00112                                         n = cross(e1,e2);
00113                                         n.normalize();
00114 
00115                                         for(i=0;i<vert-2;i++)
00116                                         {
00117                                                 t.m_v[0] = vtable[0];
00118                                                 t.m_v[1] = vtable[i+1];
00119                                                 t.m_v[2] = vtable[i+2];
00120                                                 t.m_normal = n;
00121                                                 
00122                                                 m_vertices[t.m_v[0]].m_triangleUsing = (m_triangles.size()<<2)|0;
00123                                                 m_vertices[t.m_v[1]].m_triangleUsing = (m_triangles.size()<<2)|1;
00124                                                 m_vertices[t.m_v[2]].m_triangleUsing = (m_triangles.size()<<2)|2;
00125 
00126                                                 e1 = m_vertices[t.m_v[1]].m_position;
00127                                                 e1 -= m_vertices[t.m_v[0]].m_position;
00128                                                 e2 = m_vertices[t.m_v[2]].m_position;
00129                                                 e2 -= m_vertices[t.m_v[0]].m_position;
00130                                                 e1 = cross(e1,e2);
00131                                                 t.m_area = 0.5f * e1.length();
00132                                                 m_triangles.push_back( t );
00133                                         }
00134                                         for(i=0;i<vert;i++)
00135                                         {
00136                                                 m_vertices[vtable[i]].m_normal += n;
00137                                         }
00138                                 }
00139 
00140                                 if(surf < 0)    //surface number, if <0 detail polygons follow
00141                                 {
00142                                         for(i=0;i<readU2();i++) //number of detail polygons
00143                                         {
00144                                                 m_readptr += 2 * readU2(); //number of vertices
00145                                         }
00146                                 }
00147                         }
00148                         break;
00149                 case 'SURF':
00150                         for(number=0;number<m_surfaces.size();number++)
00151                         {
00152                                 if(!strcmp((char *)m_readptr,m_surfaces[number].m_name.c_str())) goto foundName;
00153                         }
00154                         SS_ASSERT(0);           //surface not found
00155                         break;
00156 foundName:
00157                         readS0();
00158 
00159                         m_nextsubchunkptr = m_readptr;
00160                         while(m_nextsubchunkptr < chunkend)
00161                         {
00162                                 m_readptr = m_nextsubchunkptr;
00163                                 subchunkname = readID4();
00164                                 subchunklen = readU2();
00165                                 subchunkend = m_readptr + subchunklen;
00166                                 m_nextsubchunkptr = subchunkend;
00167                                 if( subchunklen & 1 ) m_nextsubchunkptr++;      //if size is odd, add 1 padding
00168 
00169                                 switch(subchunkname)
00170                                 {
00171                                 case 'COLR':
00172                                         m_surfaces[number].m_color.r = readU1();
00173                                         m_surfaces[number].m_color.g = readU1();
00174                                         m_surfaces[number].m_color.b = readU1();
00175                                         m_surfaces[number].m_color.a = 255;
00176                                         break;
00177                                 }
00178                         }
00179                         break;
00180                 default:
00181                         break;
00182                 }
00183         }
00184 
00185         m_boundingRadius = float(sqrt( m_boundingRadius ));
00186         static Vector3 up(0,1,0);
00187         for(i=0;i<m_vertices.size();i++)
00188         {
00189                 m_vertices[i].m_normal.normalize();
00190                 m_vertices[i].m_t1 = cross(up, m_vertices[i].m_normal);
00191                 m_vertices[i].m_t2 = cross(m_vertices[i].m_normal, m_vertices[i].m_t1);
00192     if( (float)SS::fabs(dot(up, m_vertices[i].m_normal)) > 0.999f ) m_vertices[i].m_reliable = false;
00193                 else m_vertices[i].m_reliable = true;
00194         }
00195 }
00196 
00197 //=======================================================================
00198 //=======================================================================

Generated on Thu Feb 15 09:09:20 2007 for Surface Splating by  doxygen 1.5.1-p1