VRUT::OBJParser Class Reference

Class for parsing OBJ files. More...

#include <objparser.h>

List of all members.

Public Member Functions

 OBJParser (wxInputStream *_inputStream, SCENE_ID _sceneID, const wxString _scenePath, Module *_module)
 ~OBJParser ()
 Class destructor.
bool Parse (const wxString &rootUid)

Private Member Functions

bool parseIndices (TextParser &textParser, std::vector< GeometryTriangles::Indice > *indices, GeometryTriangles::TriDescList *triDescList, GeometryTriangles::PRIMITIVE_TYPE primType, std::vector< VECTOR3 > &v, std::vector< GeometryTriangles::TexCoord > &vt, std::vector< VECTOR3 > &vn, std::vector< VECTOR3 > *vertices, std::vector< GeometryTriangles::TexCoord > *texCoords, std::vector< VECTOR3 > *normals)
 Parse indices.
bool parseFile (GeometryNode *node, GeometryTriangles *geometry)
 Parse file.

Private Attributes

wxInputStream * inputStream
 Input stream.
SCENE_ID sceneID
 Scene container ID to be built.
wxString fileName
 Path to scene resources, can be archive filename.
Modulemodule
 Parent module's interface.


Detailed Description

Class for parsing OBJ files.

Definition at line 26 of file objparser.h.


Constructor & Destructor Documentation

OBJParser::OBJParser ( wxInputStream *  _inputStream,
SCENE_ID  _sceneID,
const wxString  _scenePath,
Module _module 
)

Class constructor

Parameters:
[in] _inputStream Stream to read data from
[in] _sceneID Scene ID
[in] _scenePath Path to scene resources (can be an archive filename)
[in] _module Parent module with messaging interface

Definition at line 21 of file objparser.cpp.

00022 : inputStream(_inputStream), sceneID(_sceneID), module(_module)
00023 {
00024        fileName = wxFileName(_scenePath).GetName();
00025 }

OBJParser::~OBJParser (  ) 

Class destructor.

Is called by wxWidgets itself on wxApp end but it doesn't work in dynamic library

Definition at line 28 of file objparser.cpp.

00029 {
00031 }


Member Function Documentation

bool OBJParser::parseIndices ( TextParser textParser,
std::vector< GeometryTriangles::Indice > *  indices,
GeometryTriangles::TriDescList triDescList,
GeometryTriangles::PRIMITIVE_TYPE  primType,
std::vector< VECTOR3 > &  v,
std::vector< GeometryTriangles::TexCoord > &  vt,
std::vector< VECTOR3 > &  vn,
std::vector< VECTOR3 > *  vertices,
std::vector< GeometryTriangles::TexCoord > *  texCoords,
std::vector< VECTOR3 > *  normals 
) [private]

Parse indices.

Definition at line 33 of file objparser.cpp.

00036 {
00037        std::pair<GeometryTriangles::Indice, GeometryTriangles::PRIMITIVE_TYPE> indType((GeometryTriangles::Indice)indices->size(), primType);
00038        triDescList->push_back(indType);
00039        size_t maxV=v.size()+1, maxVT=vt.size()+1, maxVN=vn.size()+1;
00040        //read indices
00041        while (!textParser.NextCharIs('\n'))
00042        {
00043               long i;
00044               if (!textParser.ParseLong(&i, NULL, false))
00045                      break;
00046               if ((i == 0) || (abs(i) > long(maxV)))
00047               {
00048                      LOGERROR(wxString::Format(wxT("<OBJParser>Error parseIndices, v=%i "), i)+textParser.GetWxString("\r\n"));
00049                      return false;
00050               }
00051               vertices->push_back(v[(i>0)?i-1:maxV-i]);
00052               indices->push_back((GeometryTriangles::Indice)(vertices->size()-1));
00053               i=0;
00054               if (textParser.NextCharIs('/', NULL, false))
00055               {
00056                      textParser.GetChar(NULL, false);
00057                      if (!textParser.NextCharIs('/', NULL, false))
00058                      {
00059                             textParser.ParseLong(&i, NULL, false);//read texture index
00060                             if ((i == 0) || (abs(i) > long(maxVT)))
00061                             {
00062                                    LOGERROR(wxString::Format(wxT("<OBJParser>Error parseIndices, vt=%i "), i)+textParser.GetWxString("\r\n"));
00063                                    return false;
00064                             }
00065                             texCoords->push_back(vt[(i>0)?i-1:maxVT-i]);
00066                      }
00067               }
00068               if (i==0)
00069                      texCoords->push_back(GeometryTriangles::TexCoord(0,0));
00070               i=0;
00071               if (textParser.NextCharIs('/', NULL, false))
00072               {
00073                      textParser.GetChar(NULL, false);
00074                      if (!textParser.NextCharIs('/', NULL, false))
00075                      {
00076                             textParser.ParseLong(&i, NULL, false);//read normal index
00077                             if ((i == 0) || (abs(i) > long(maxVN)))
00078                             {
00079                                    LOGERROR(wxString::Format(wxT("<OBJParser>Error parseIndices, vn=%i "), i)+textParser.GetWxString("\r\n"));
00080                                    return false;
00081                             }
00082                             normals->push_back(vn[(i>0)?i-1:maxVN-i]);
00083                      }
00084               }
00085               if (i==0)
00086                      normals->push_back(VECTOR3(0,0,0));
00087        }
00088        return true;
00089 }

bool OBJParser::parseFile ( GeometryNode node,
GeometryTriangles geometry 
) [private]

Parse file.

prepare

parse

Definition at line 91 of file objparser.cpp.

00092 {
00093        TextParser textParser(inputStream, 10000, 1000);
00094 //     newName = textParser.GetWxString("\r\n");
00096        std::vector<VECTOR3> * vertices = &(geometry->vertices);
00097        std::vector<VECTOR3> * normals = &(geometry->normals);
00098        std::vector<GeometryTriangles::TexCoord> * texCoords = &(geometry->texCoords);
00099        std::vector<GeometryTriangles::Indice> * indices = &(geometry->indices);
00100 
00101        std::vector<VECTOR3> v;
00102        std::vector<GeometryTriangles::TexCoord> vt;
00103        std::vector<VECTOR3> vn;
00104 
00105 
00107        const char *keywords[]={"v", "vn", "vt", "f", "l", "p", "g", "s", "mg", "o", "lod", "usemtl", "mtllib", "shadow_obj", 
00108               "vp", "cstype", "deg", "bmat", "step", "curv", "curv2", "surf", "parm", "trim", "hole", "scrv", "sp", "end", "con", "bevel", "c_interp", "d_interp", "trace_obj", "ctech", "stech", ""};
00109 /*
00110 o       parameter space vertices (vp)
00111           Free-form curve/surface attributes
00112 o       rational or non-rational forms of curve or surface type:
00113           basis matrix, Bezier, B-spline, Cardinal, Taylor (cstype)
00114 o       degree (deg)
00115 o       basis matrix (bmat)
00116 o       step size (step)
00117 
00118 Elements
00119 
00120 o       curve (curv)
00121 o       2D curve (curv2)
00122 o       surface (surf)
00123 
00124 Free-form curve/surface body statements
00125 
00126 o       parameter values (parm)
00127 o       outer trimming loop (trim)
00128 o       inner trimming loop (hole)
00129 o       special curve (scrv)
00130 o       special point (sp)
00131 o       end statement (end)
00132 
00133 Connectivity between free-form surfaces
00134 
00135 o       connect (con)
00136 
00137 Display/render attributes
00138 
00139 o       bevel interpolation (bevel)
00140 o       color interpolation (c_interp)
00141 o       dissolve interpolation (d_interp)
00142 o       ray tracing (trace_obj)
00143 o       curve approximation technique (ctech)
00144 o       surface approximation technique (stech)
00145 */
00146        unsigned int index = 0;
00147        while (!textParser.IsEmpty())
00148        {
00149               switch (textParser.KeywordsSwitch(keywords))
00150               {
00151               case 0://v - geometric vertices
00152                      {
00153                             VECTOR3 vert;
00154                             if (textParser.ParseVector(vert._v, 3, true) != 3)
00155                             {
00156                                    LOGERROR(wxT("<OBJParser>Error parsing 'v'."));
00157                                    return false;
00158                             }
00159                             v.push_back(vert);
00160                             break;
00161                      }
00162               case 1://vn - vertex normals
00163                      {
00164                             VECTOR3 norm;
00165                             if (textParser.ParseVector(norm._v, 3, true) != 3)
00166                             {
00167                                    LOGERROR(wxT("<OBJParser>Error parsing 'vn'."));
00168                                    return false;
00169                             }
00170                             vn.push_back(norm);
00171                             break;
00172                      }
00173               case 2://vt - texture vertices
00174                      {
00175                             GeometryTriangles::TexCoord texCoord;
00176                             if (textParser.ParseVector(texCoord._v, 2, true) != 2)
00177                             {
00178                                    LOGERROR(wxT("<OBJParser>Error parsing 'vt'."));
00179                                    return false;
00180                             }
00181                             vt.push_back(texCoord);
00182                             break;
00183                      }
00184               case 3://f - face
00185                      if (!parseIndices(textParser, indices, &(geometry->triDescList), GeometryTriangles::POLYGON, v, vt, vn, vertices, texCoords, normals))
00186                      {
00187                             LOGERROR(wxT("<OBJParser>Error parsing 'f'."));
00188                             return false;
00189                      }
00190                      break;
00191               case 4://l - line
00192                      if (!parseIndices(textParser, indices, &(geometry->triDescList), GeometryTriangles::LINES, v, vt, vn, vertices, texCoords, normals))
00193                      {
00194                             LOGERROR(wxT("<OBJParser>Error parsing 'l'."));
00195                             return false;
00196                      }
00197                      break;
00198               case 5://p - point
00199                      if (!parseIndices(textParser, indices, &(geometry->triDescList), GeometryTriangles::POINTS, v, vt, vn, vertices, texCoords, normals))
00200                      {
00201                             LOGERROR(wxT("<OBJParser>Error parsing 'p'."));
00202                             return false;
00203                      }
00204                      break;
00205               case 6://g - group name
00206                      break;
00207               case 7://s - smoothing group
00208                      break;
00209               case 8://mg - merging group
00210                      break;
00211               case 9://o - object name
00212                      {
00213                             wxString nodeName=textParser.GetWxLine();
00214                             wxCommandEvent ev = Event::GET_EVT_SCENE_NODE_NAME_SET(sceneID, node->GetUid(), nodeName);
00215                             module->PostToKernel(ev);
00216                             break;
00217                      }
00218               case 10://lod - level of detail
00219                      break;
00220               case 11://usemtl - material name
00221                      textParser.GetWxLine();
00222                      break;
00223               case 12://mtllib - material library
00224                      break;
00225               case 13://shadow_obj - shadow casting
00226                      break;
00227               default:
00228                      LOGERROR(wxT("<OBJParser>Unexpected token: '" + textParser.GetWxLine() + wxT("'")));
00229                      return false;
00230               }
00231        }
00232        return true;
00233 }

bool OBJParser::Parse ( const wxString &  rootUid  ) 

Parse OBJ stream and build scene graph

Parameters:
[in] rootUid Uid of node that will be root of imported scene
Returns:
true if successful

Add new geometry node

Create new mesh

Assign mesh

Definition at line 235 of file objparser.cpp.

00236 {
00237        wxCommandEvent evName = Event::GET_EVT_SCENE_NODE_NAME_SET(sceneID, rootUid, fileName);
00238        module->PostToKernel(evName);
00239 
00240        //prepare material
00241        Material * material = new Material(wxT("OBJDefault"));
00242        material->diffuse = VECTOR4(0.5, 0.5, 0.5, 1);
00243        material->specular = VECTOR4(0.5, 0.5, 0.5, 1);
00244        material->flags = Material::DOUBLE_SIDED;
00245        wxCommandEvent evMat = Event::GET_EVT_SCENE_MATERIAL_ADD(sceneID, material);
00246        module->PostToKernel(evMat);
00247 
00248        //prepare geometry
00250        wxString nodeUid = fileName + wxT("_node");
00251        GeometryNode * node = new GeometryNode(nodeUid, nodeUid);
00252        wxCommandEvent evNode = Event::GET_EVT_SCENE_NODE_INSERT(sceneID, node, fileName);
00253        module->PostToKernel(evNode);
00254        wxCommandEvent evMatSet = Event::GET_EVT_SCENE_NODE_MATERIAL_SET(sceneID, nodeUid, material->name);
00255        module->PostToKernel(evMatSet);
00256 
00258        GeometryTriangles *geometry = new GeometryTriangles(nodeUid);
00259 
00260        //parse
00261        if (!parseFile(node, geometry))
00262        {
00263               LOGERROR(wxT("<OBJParser>Scene not loaded properly"));
00264               return false;
00265        }
00266        else
00267        {
00269               wxCommandEvent evGeom = Event::GET_EVT_SCENE_GEOMETRY_ADD(sceneID, geometry);
00270               module->PostToKernel(evGeom);
00271               wxCommandEvent evGeomSet = Event::GET_EVT_SCENE_NODE_GEOMETRY_SET(sceneID, nodeUid, geometry->GetName());
00272               module->PostToKernel(evGeomSet);
00273        }
00274        return true;
00275 }


Member Data Documentation

wxInputStream* VRUT::OBJParser::inputStream [private]

Input stream.

Definition at line 30 of file objparser.h.

Scene container ID to be built.

Definition at line 32 of file objparser.h.

wxString VRUT::OBJParser::fileName [private]

Path to scene resources, can be archive filename.

Definition at line 34 of file objparser.h.

Parent module's interface.

Definition at line 36 of file objparser.h.


The documentation for this class was generated from the following files:

Generated on Tue Mar 10 14:41:40 2009 for VRUT by  doxygen 1.5.5