NGL  6.5
The NCCA Graphics Library
HoudiniGeo.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 Jon Macey
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 #include "HoudiniGeo.h"
18 #include <boost/lexical_cast.hpp>
19 //----------------------------------------------------------------------------------------------------------------------
22 //----------------------------------------------------------------------------------------------------------------------
23 
24 namespace ngl
25 {
26 
27 //----------------------------------------------------------------------------------------------------------------------
29  const std::string& _fName,
30  bool _calcBB
31  )
32 {
33  // Open the stream and parse
34  std::ifstream fileIn;
35  fileIn.open(_fName.c_str());
36  if (!fileIn.is_open())
37  {
38  std::cout <<"File : "<<_fName<<" Not founds "<<std::endl;
39  return false;
40  }
41 // first we check the header to see if we have a valid file
42 // if only OBJ's did this!!
43 
44  loadHeader(fileIn);
45  // check to see if we need to load any attribs
46  if(m_nPointAttrib !=0)
47  {
49  }
50  loadPoints(fileIn);
51  // set the center and BBox
52  this->calcDimensions();
53 
54  fileIn.close();
55  return true;
56 }
57 
58 //----------------------------------------------------------------------------------------------------------------------
60  const std::string& _fName
61  )
62 {
63 m_vbo=0;
64 m_loaded=load(_fName);
65 }
66 
67 //----------------------------------------------------------------------------------------------------------------------
69  const std::string& _fName,
70  const std::string& _texName
71  )
72 {
73 m_vbo=0;
74 m_loaded=load(_fName);
75 loadTexture(_texName);
76 
77 }
78 
79 //----------------------------------------------------------------------------------------------------------------------
87  std::ifstream& _stream
88  )
89 {
90  // set the token index to large value so we can check if they are set
91  m_normalTokenIndex=0xffff;
92  m_uvTokenIndex=0x0ffff;
93  std::string lineBuffer;
94  // first check if valid
95  getline(_stream,lineBuffer,'\n');
96  // now tokenize the line
97  boost::char_separator<char> sep(" \t\r\n");
98  tokenizer tokens(lineBuffer, sep);
99  if(*tokens.begin() != "PGEOMETRY")
100  {
101  std::cerr<<"Not a valid Houdini geo file first line should be PGEOMETRY version"<<std::endl;
102  return false;
103  }
104  // load in the number of points and prims
105  getline(_stream,lineBuffer,'\n');
106  tokenizer pointPrimTokens(lineBuffer,sep);
107  // ok we will take some liberties here and assume that the file is not corrupted
108  // as this is really a dirty pipeline tool and not a solid parser
109  // Point/Prim Counts: NPoints # NPrims #
110  // first skip the NPoints
111  tokenizer::iterator firstWord = pointPrimTokens.begin();
112  ++firstWord;
113  // now we have the num points
114  m_nVerts=boost::lexical_cast<int>(*firstWord);
115  // now the prims
116  ++firstWord;
117  // skip the NPrims token
118  ++firstWord;
119 
120  // now we have the num points
121  m_nFaces =boost::lexical_cast<int>(*firstWord);
122  std::cerr<<"num points "<<m_nVerts<<" prims "<<m_nFaces<<std::endl;
123  m_verts.resize(m_nVerts);
124  m_face.resize(m_nFaces);
125 
126  // skip the next line as we don't support it
127  getline(_stream,lineBuffer,'\n');
128  // now we need to read the rest of the header and find what attribs if any are attached
129  // to the Points and the vertex values
130  // At present we are only interested in the Texture and Normal attribs as that is all that
131  // is supported in the AbstractBase class
132  getline(_stream,lineBuffer,'\n');
133  tokenizer attribTokens(lineBuffer,sep);
134  // the basic format is as follows NPointAttrib 1 NVertexAttrib 1 NPrimAttrib 0 NAttrib 0
135  // first skip the NPointAttrib
136  std::cerr<<lineBuffer<<std::endl;
137  firstWord = attribTokens.begin();
138  ++firstWord;
139  // now we have the num points
140  m_nPointAttrib=boost::lexical_cast<int>(*firstWord);
141  // now the prims
142  ++firstWord;
143  // skip the NPrims token
144  ++firstWord;
145  m_nVertAttrib=boost::lexical_cast<int>(*firstWord);
146  std::cerr<<"points Attrib "<<m_nPointAttrib<<" Vert Attrib "<<m_nVertAttrib<<std::endl;
147  std::cerr<<"Finished Reading Header"<<std::endl;
148  return true;
149 }
150 //----------------------------------------------------------------------------------------------------------------------
152  std::ifstream& _stream
153  )
154 {
162 
164 
167 
169 
176  boost::char_separator<char> sep(" \t\r\n()");
177  tokenizer::iterator firstWord;
178  tokenizer::iterator lastWord;
179 
180  std::string lineBuffer;
181  for (unsigned int i=0; i<m_nVerts; ++i)
182  {
183  getline(_stream,lineBuffer,'\n');
184  //std::cerr<<lineBuffer<<std::endl;
185  tokenizer pointTokens(lineBuffer,sep);
186  firstWord=pointTokens.begin();
187  lastWord=pointTokens.end();
188  // grab the point data
189  float x=boost::lexical_cast<float>(*firstWord++);
190  float y=boost::lexical_cast<float>(*firstWord++);
191  float z=boost::lexical_cast<float>(*firstWord++);
192  float w=boost::lexical_cast<float>(*firstWord++);
193  m_verts[i].set(x,y,z,w);
194  // now we see if we have any other data to load and grab that as well.
195  // this can be difficult as we can have either vert data or prim data in the
196  // file lets just grab all the data and put it into a vector to make indexing
197  // easier
198  std::vector <float> values;
199  while(firstWord !=lastWord)
200  {
201  values.push_back(boost::lexical_cast<float>(*firstWord++));
202  }
203  // now lets load the normals if they exist
204  //if()
205  {
206 
207  }
208  }
209  }
210 
211 
212 
213 //----------------------------------------------------------------------------------------------------------------------
215  std::ifstream& _stream
216  )
217 {
218  std::string lineBuffer;
219  std::cerr<<"Loading Attrib Dictionary num attribs"<< m_nPointAttrib<<std::endl;
220  getline(_stream,lineBuffer,'\n');
221  // the first line of this dictionary should be PointAttrib so check and bomb if wrong
222  if(lineBuffer != "PointAttrib")
223  {
224  std::cerr<< "file not in correct format line should read PointAttrib\n";
225  return;
226  }
227  // our seperator for the tokens including () for the attribs
228  boost::char_separator<char> sep(" \t\r\n");
229  tokenizer::iterator firstWord;
230  tokenizer::iterator sizeToken;
231 
232  int relIndex=0;
233  for(int i=0; i<m_nPointAttrib; ++i)
234  {
235  getline(_stream,lineBuffer,'\n');
236  std::cerr<<"Found "<<lineBuffer<<std::endl;
237  tokenizer attribTokens(lineBuffer,sep);
238  // first we are going to grab the 2nd element of the dictionary so we can see
239  // what size the token is, we then use it to get the relative offset (begining) of
240  // the data in the line for the point attrib parsing
241  sizeToken=attribTokens.begin();
242  ++sizeToken;
243  int size=boost::lexical_cast<int>(*sizeToken);
244  // relIndex tells us which attribute is the one we want, we basically inc this everytime
245  // we find an attrib and if it's one we want we know.
246  // imagine we have Cd Alpha N uv then N == 2 uv == 3 so we know we can skip the others
247  relIndex++;
248  std::cerr<<"relIndex "<<relIndex<<std::endl;
249  firstWord=attribTokens.begin();
250 
251  // see if we have a normal
252  if(*firstWord == "N")
253  {
254  // increment sizeToken to get the data type
255  ++sizeToken;
256  m_dictionary["N"]= new HouDictionaryEntry("N",size,*sizeToken,relIndex);
257  m_dictionary["N"]->print();
258 
259  }
260  else if(*firstWord =="uv")
261  {
262  ++sizeToken;
263  m_dictionary["uv"]= new HouDictionaryEntry("uv",size,*sizeToken,relIndex);
264  m_dictionary["uv"]->print();
265 
266  }
267 
268  }
269 
270 }
271 //----------------------------------------------------------------------------------------------------------------------
273 {
275 #warning add new code to do this
276  /*
277  glBegin(GL_POINTS);
278  glPointSize(4);
279  for(unsigned int i=0; i<m_nVerts; ++i)
280  {
281  m_verts[i].vertex();
282 
283  }
284  glEnd();
285 */
286 }
287 //----------------------------------------------------------------------------------------------------------------------
288 
289 //----------------------------------------------------------------------------------------------------------------------
290 
291 //----------------------------------------------------------------------------------------------------------------------
292 
293 //----------------------------------------------------------------------------------------------------------------------
294 
295 //----------------------------------------------------------------------------------------------------------------------
296 
297 } // end of ngl namespace
void loadPoints(std::ifstream &_stream)
a method to load the points from the geo file
Definition: HoudiniGeo.cpp:151
GLdouble GLdouble z
Definition: glew.h:1562
bool load(const std::string &_fName, bool _calcBB=true)
the main loader for the geo we will have other methods called from this
Definition: HoudiniGeo.cpp:28
a class to hold the dictionary entries for the houdini geo header this will be put into a map for eas...
Definition: HoudiniGeo.h:38
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1255
std::vector< Vec3 > m_verts
Pointer to the Vertex list.
Definition: AbstractMesh.h:281
int m_nVertAttrib
the number of vertex attributes
Definition: HoudiniGeo.h:131
HoudiniGeo()
default constructor
Definition: HoudiniGeo.h:99
void loadPointAttribDictionary(std::ifstream &_stream)
method to load the attribute dictionary from the geo file. At present we are only worried about Norma...
Definition: HoudiniGeo.cpp:214
implementation files for RibExport class
Definition: AABB.cpp:22
boost::tokenizer< boost::char_separator< char > > tokenizer
GLint GLsizei const GLuint64 * values
Definition: glew.h:3624
void drawDebugPoints()
simple debug drawing of points will be removed once class finished
Definition: HoudiniGeo.cpp:272
bool m_loaded
flag to indicate if anything loaded for dtor
Definition: AbstractMesh.h:384
int m_nPointAttrib
the number of point attributes
Definition: HoudiniGeo.h:127
GLubyte GLubyte GLubyte GLubyte w
Definition: glew.h:1893
unsigned int m_nVerts
The number of vertices in the object.
Definition: AbstractMesh.h:265
inherited from AbstractMesh to load houdini geometry
int m_normalTokenIndex
normal token index used when parsing the geo file
Definition: HoudiniGeo.h:135
bool loadHeader(std::ifstream &_stream)
method to check the header and see if we have a valid geo file it also reads in the ammount of Points...
Definition: HoudiniGeo.cpp:86
unsigned int m_nFaces
the number of faces in the object
Definition: AbstractMesh.h:277
std::map< std::string, HouDictionaryEntry * > m_dictionary
map used when parsing the Attribute Data
Definition: HoudiniGeo.h:143
GLint GLint GLint GLint GLint x
Definition: glew.h:1255
GLsizeiptr size
Definition: glew.h:1684
void loadTexture(const std::string &_fname) noexcept
load int a texture and set it as the active texture of the Obj
bool m_vbo
flag to indicate if a VBO has been created
Definition: AbstractMesh.h:322
void calcDimensions() noexcept
a method to set the BBox and center
int m_uvTokenIndex
uv token index used when paring the geo file
Definition: HoudiniGeo.h:139
GLsizei const GLchar *const * string
Definition: glew.h:1847
std::vector< Face > m_face
Pointer to the Face list.
Definition: AbstractMesh.h:293