ParserGL.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the VRender library.
3  Copyright (C) 2005 Cyril Soler (Cyril.Soler@imag.fr)
4  Version 1.0.0, released on June 27, 2005.
5 
6  http://artis.imag.fr/Members/Cyril.Soler/VRender
7 
8  VRender is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12 
13  VRender is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with VRender; if not, write to the Free Software Foundation, Inc.,
20  51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
21 */
22 
23 /****************************************************************************
24 
25  Copyright (C) 2002-2013 Gilles Debunne. All rights reserved.
26 
27  This file is part of the QGLViewer library version 2.4.0.
28 
29  http://www.libqglviewer.com - contact@libqglviewer.com
30 
31  This file may be used under the terms of the GNU General Public License
32  versions 2.0 or 3.0 as published by the Free Software Foundation and
33  appearing in the LICENSE file included in the packaging of this file.
34  In addition, as a special exception, Gilles Debunne gives you certain
35  additional rights, described in the file GPL_EXCEPTION in this package.
36 
37  libQGLViewer uses dual licensing. Commercial/proprietary software must
38  purchase a libQGLViewer Commercial License.
39 
40  This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
41  WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
42 
43 *****************************************************************************/
44 
45 #include <assert.h>
46 #include <math.h>
47 #include <stdio.h>
48 
49 #include "VRender.h"
50 #include "ParserGL.h"
51 
52 using namespace vrender ;
53 using namespace std;
54 
56 {
57  public:
58  static void NormalizeBufferCoordinates(GLint size, GLfloat * buffer, GLfloat MaxSize, GLfloat& zmin, GLfloat& zmax) ;
59 
60  static PtrPrimitive checkPoint(Point *& P);
61  static PtrPrimitive checkSegment(Segment *& P);
62  static PtrPrimitive checkPolygon(Polygone *& P);
63 
64  static void ComputeBufferBB(GLint size, GLfloat * buffer,
65  GLfloat & xmin, GLfloat & xmax,
66  GLfloat & ymin, GLfloat & ymax,
67  GLfloat & zmin, GLfloat & zmax) ;
68 
69  private:
70  static void print3DcolorVertex(GLint size, GLint * count, GLfloat * buffer) ;
71  static void debug_printBuffer(GLint size, GLfloat *buffer) ;
72 
73  static void NormalizePrimitiveCoordinates(GLfloat * & loc,GLfloat MaxSize,GLfloat zmin,GLfloat zmax) ;
74  static void ComputePrimitiveBB( GLfloat * & loc,
75  GLfloat & xmin,GLfloat & xmax,
76  GLfloat & ymin,GLfloat & ymax,
77  GLfloat & zmin,GLfloat & zmax);
78 
79  static const char *nameOfToken(int token);
80 
81  static const double EGALITY_EPS ;
82 };
83 
84 const double ParserUtils::EGALITY_EPS = 0.00001 ;
85 
86 void ParserGL::parseFeedbackBuffer( GLfloat *buffer,int size,
87  std::vector<PtrPrimitive>& primitive_tab,
88  VRenderParams& vparams)
89 {
90  int token;
91  int nvertices = 0 ;
92  nb_lines = 0 ;
93  nb_polys = 0 ;
94  nb_points = 0 ;
95  nb_degenerated_lines = 0 ;
96  nb_degenerated_polys = 0 ;
97  nb_degenerated_points = 0 ;
98 
99  // pre-treatment of coordinates so as to get something more consistent
100 
101  _xmin = FLT_MAX ;
102  _ymin = FLT_MAX ;
103  _zmin = FLT_MAX ;
104  _xmax = -FLT_MAX ;
105  _ymax = -FLT_MAX ;
106  _zmax = -FLT_MAX ;
107 
108  ParserUtils::ComputeBufferBB(size, buffer, _xmin,_xmax,_ymin,_ymax,_zmin,_zmax) ;
109 
110 #ifdef DEBUGEPSRENDER
111  printf("Buffer bounding box: %f %f %f %f %f %f\n",xmin,xmax,ymin,ymax,zmin,zmax) ;
112 #endif
113  float Zdepth = max(_ymax-_ymin,_xmax-_xmin) ;
114  ParserUtils::NormalizeBufferCoordinates(size,buffer,Zdepth,_zmin,_zmax) ;
115 
116  // now, read buffer
117  GLfloat *end = buffer + size;
118 
119  GLfloat *loc = buffer ;
120  int next_step = 0 ;
121  int N = size/200 + 1 ;
122 
123  while (loc < end)
124  {
125  token = int(0.5f + *loc) ;
126  loc++;
127 
128  if((end-loc)/N >= next_step)
129  vparams.progress((end-loc)/(float)size, QGLViewer::tr("Parsing feedback buffer.")), ++next_step ;
130 
131  switch (token)
132  {
133  case GL_LINE_TOKEN:
134  case GL_LINE_RESET_TOKEN:
135  {
137 
138  primitive_tab.push_back(ParserUtils::checkSegment(S)) ;
139 
140  if(S == NULL)
141  nb_degenerated_lines++ ;
142 
143  nb_lines++ ;
145  }
146  break;
147 
148  case GL_POLYGON_TOKEN:
149  {
150  nvertices = int(0.5f + *loc) ;
151  loc++;
152 
153  std::vector<Feedback3DColor> verts ;
154 
155  for(int i=0;i<nvertices;++i)
156  verts.push_back(Feedback3DColor(loc)),loc+=Feedback3DColor::sizeInBuffer() ;
157 
158  Polygone *P = new Polygone(verts) ;
159 
160  primitive_tab.push_back(ParserUtils::checkPolygon(P)) ;
161 
162  if(P == NULL)
163  nb_degenerated_polys++ ;
164 
165  nb_polys++ ;
166  }
167  break ;
168 
169  case GL_POINT_TOKEN:
170  {
171  Point *Pt = new Point(Feedback3DColor(loc)) ;
172 
173  primitive_tab.push_back(Pt);//ParserUtils::checkPoint(Pt)) ;
174 
175  if(Pt == NULL)
176  nb_degenerated_points++ ;
177 
178  nb_points++ ;
180  }
181  break;
182  default:
183  break;
184  }
185  }
186 
187 }
188 
189 // Traitement des cas degeneres. Renvoie false si le polygone est degenere.
190 // Traitement des cas degeneres. Renvoie false si le segment est degenere.
191 
193 {
194  return P ;
195 }
196 
198 {
199  if((P->vertex(0) - P->vertex(1)).infNorm() < EGALITY_EPS)
200  {
201  Point *pp = new Point(P->sommet3DColor(0)) ;
202  delete P ;
203  P = NULL ;
204 
205  return checkPoint(pp) ;
206  }
207 
208  return P ;
209 }
210 
212 {
213  if(P->nbVertices() != 3)
214  {
215  cout << "unexpected case: Polygon with " << P->nbVertices() << " vertices !" << endl ;
216  delete P ;
217  return NULL ;
218  }
219 
220  if(P->FlatFactor() < FLAT_POLYGON_EPS)
221  {
222  // On ne traite que le cas du triangle plat, vu qu'on est sur d'avoir un triangle
223 
224  int n = P->nbVertices() ;
225 
226  for(int i=0;i<n;++i)
227  if( (P->vertex(i) - P->vertex((i+1)%n)).norm() > EGALITY_EPS)
228  {
229  Segment *pp = new Segment(P->sommet3DColor((i+1)%n),P->sommet3DColor((i+2)%n)) ;
230  delete P ;
231  P = NULL ;
232 
233  return checkSegment(pp) ;
234  }
235 
236  Point *pp = new Point(P->sommet3DColor(0)) ;
237  delete P ;
238  P = NULL ;
239 
240  return checkPoint(pp) ;
241  }
242 
243  // No problem detected.
244 
245  return P ;
246 }
247 
248 
249 /* Write contents of one vertex to stdout. */
250 
251 void ParserUtils::print3DcolorVertex(GLint size, GLint * count, GLfloat * buffer)
252 {
253  int i;
254 
255  printf(" ");
256  for (i = 0; i < Feedback3DColor::sizeInBuffer(); i++)
257  {
258  printf("%4.2f ", buffer[size - (*count)]);
259  *count = *count - 1;
260  }
261  printf("\n");
262 }
263 
264 void ParserUtils::debug_printBuffer(GLint size, GLfloat * buffer)
265 {
266  GLint count;
267  int token, nvertices;
268 
269  count = size;
270  while (count) {
271  token = int(buffer[size - count]);
272  count--;
273  switch (token)
274  {
275  case GL_PASS_THROUGH_TOKEN:
276  printf("GL_PASS_THROUGH_TOKEN\n");
277  printf(" %4.2f\n", buffer[size - count]);
278  count--;
279  break;
280  case GL_POINT_TOKEN:
281  printf("GL_POINT_TOKEN\n");
282  print3DcolorVertex(size, &count, buffer);
283  break;
284  case GL_LINE_TOKEN:
285  printf("GL_LINE_TOKEN\n");
286  print3DcolorVertex(size, &count, buffer);
287  print3DcolorVertex(size, &count, buffer);
288  break;
289  case GL_LINE_RESET_TOKEN:
290  printf("GL_LINE_RESET_TOKEN\n");
291  print3DcolorVertex(size, &count, buffer);
292  print3DcolorVertex(size, &count, buffer);
293  break;
294  case GL_POLYGON_TOKEN:
295  printf("GL_POLYGON_TOKEN\n");
296  nvertices = int(buffer[size - count]) ;
297  count--;
298  for (; nvertices > 0; nvertices--)
299  print3DcolorVertex(size, &count, buffer);
300  }
301  }
302 }
303 
304 void ParserUtils::NormalizePrimitiveCoordinates(GLfloat * & loc,GLfloat MaxSize,GLfloat zmin,GLfloat zmax)
305 {
306  int token;
307  int nvertices, i;
308 
309  token = int(*loc) ;
310  loc++;
311  int size = Feedback3DColor::sizeInBuffer() ;
312 
313  switch (token)
314  {
315  case GL_LINE_RESET_TOKEN:
316  case GL_LINE_TOKEN:
317  {
318  for (i = 0; i < 2; i++)
319  (loc+size*i)[2] = ((loc+size*i)[2] - zmin)/(zmax-zmin)*MaxSize ;
320 
321  loc += 2*size; /* Each vertex element in the feedback buffer is size GLfloats. */
322  break;
323  }
324  case GL_POLYGON_TOKEN:
325  {
326  nvertices = int(*loc) ;
327  loc++;
328 
329  for (i = 0; i < nvertices; i++)
330  (loc+size*i)[2] = ((loc+size*i)[2] - zmin)/(zmax-zmin)*MaxSize ;
331 
332  loc += nvertices * size; /* Each vertex element in the feedback buffer is size GLfloats. */
333  break;
334  }
335  case GL_POINT_TOKEN:
336  {
337  loc[2] = (loc[2] - zmin)/(zmax-zmin)*MaxSize ;
338 
339  loc += size; /* Each vertex element in the feedback buffer is size GLfloats. */
340  break;
341  }
342  default:
343  /* XXX Left as an excersie to the reader. */
344 #ifdef DEBUGEPSRENDER
345  printf("%s (%d) not handled yet. Sorry.\n", ParserUtils::nameOfToken(token), token);
346 #endif
347  ;
348  }
349 }
350 
351 void ParserUtils::ComputePrimitiveBB(GLfloat * & loc,GLfloat & xmin,GLfloat & xmax,GLfloat & ymin,GLfloat & ymax, GLfloat & zmin,GLfloat & zmax)
352 {
353  int token;
354  int nvertices, i;
355 
356  token = int(*loc) ;
357  loc++;
358  int size = Feedback3DColor::sizeInBuffer() ;
359 
360  switch (token)
361  {
362  case GL_LINE_RESET_TOKEN:
363  case GL_LINE_TOKEN:
364  {
365  for (i = 0; i < 2; i++)
366  {
367  Feedback3DColor f(loc+size*i) ;
368 
369  if(f.x() < xmin) xmin = GLfloat(f.x()) ;
370  if(f.y() < ymin) ymin = GLfloat(f.y()) ;
371  if(f.z() < zmin) zmin = GLfloat(f.z()) ;
372  if(f.x() > xmax) xmax = GLfloat(f.x()) ;
373  if(f.y() > ymax) ymax = GLfloat(f.y()) ;
374  if(f.z() > zmax) zmax = GLfloat(f.z()) ;
375  }
376 
377  loc += 2*size; /* Each vertex element in the feedback
378  buffer is size GLfloats. */
379  break;
380  }
381  case GL_POLYGON_TOKEN:
382  {
383  nvertices = int(*loc) ;
384  loc++;
385 
386  for (i = 0; i < nvertices; i++)
387  {
388  Feedback3DColor f(loc+size*i) ;
389 
390  if(f.x() < xmin) xmin = GLfloat(f.x()) ;
391  if(f.y() < ymin) ymin = GLfloat(f.y()) ;
392  if(f.z() < zmin) zmin = GLfloat(f.z()) ;
393  if(f.x() > xmax) xmax = GLfloat(f.x()) ;
394  if(f.y() > ymax) ymax = GLfloat(f.y()) ;
395  if(f.z() > zmax) zmax = GLfloat(f.z()) ;
396  }
397 
398  loc += nvertices * size; /* Each vertex element in the
399  feedback buffer is size GLfloats. */
400  break;
401  }
402  case GL_POINT_TOKEN:
403  {
404  Feedback3DColor f(loc) ;
405 
406  if(f.x() < xmin) xmin = GLfloat(f.x()) ;
407  if(f.y() < ymin) ymin = GLfloat(f.y()) ;
408  if(f.z() < zmin) zmin = GLfloat(f.z()) ;
409  if(f.x() > xmax) xmax = GLfloat(f.x()) ;
410  if(f.y() > ymax) ymax = GLfloat(f.y()) ;
411  if(f.z() > zmax) zmax = GLfloat(f.z()) ;
412 
413  loc += size; /* Each vertex element in the feedback
414  buffer is size GLfloats. */
415  break;
416  }
417  default:
418  /* XXX Left as an excersie to the reader. */
419 #ifdef DEBUGEPSRENDER
420  printf("Incomplete implementation. Unexpected token (%d).\n", token);
421 #endif
422  ;
423  }
424 }
425 
426 void ParserUtils::NormalizeBufferCoordinates(GLint size, GLfloat * buffer, GLfloat MaxSize, GLfloat& zmin,GLfloat& zmax)
427 {
428  GLfloat *loc, *end;
429 
430  if(zmax == zmin)
431  {
432 #ifdef DEBUGEPSRENDER
433  printf("Warning: zmin = zmax in NormalizePrimitiveCoordinates\n") ;
434 #endif
435  return ;
436  }
437 
438  loc = buffer;
439  end = buffer + size;
440 
441  while (loc < end)
442  NormalizePrimitiveCoordinates(loc,MaxSize,zmin,zmax);
443 
444  zmin = 0.0 ;
445  zmax = MaxSize ;
446 }
447 
448 void ParserUtils::ComputeBufferBB(GLint size, GLfloat * buffer,
449  GLfloat & xmin, GLfloat & xmax,
450  GLfloat & ymin, GLfloat & ymax,
451  GLfloat & zmin, GLfloat & zmax)
452 {
453  GLfloat *loc, *end;
454 
455  loc = buffer;
456  end = buffer + size;
457 
458  while (loc < end)
459  ComputePrimitiveBB(loc,xmin,xmax,ymin,ymax,zmin,zmax);
460 }
461 
462 typedef struct _DepthIndex {
463  GLfloat *ptr;
464  GLfloat depth;
465 } DepthIndex;
466 
467 const char *ParserUtils::nameOfToken(int token)
468 {
469  switch(token)
470  {
471  case GL_PASS_THROUGH_TOKEN: return "GL_PASS_THROUGH_TOKEN" ;
472  case GL_POINT_TOKEN: return "GL_POINT_TOKEN" ;
473  case GL_LINE_TOKEN: return "GL_LINE_TOKEN" ;
474  case GL_POLYGON_TOKEN: return "GL_POLYGON_TOKEN" ;
475  case GL_BITMAP_TOKEN: return "GL_BITMAP_TOKEN" ;
476  case GL_DRAW_PIXEL_TOKEN: return "GL_DRAW_PIXEL_TOKEN" ;
477  case GL_COPY_PIXEL_TOKEN: return "GL_COPY_PIXEL_TOKEN" ;
478  case GL_LINE_RESET_TOKEN: return "GL_LINE_RESET_TOKEN" ;
479  default:
480  return "(Unidentified token)" ;
481  }
482 }
483 
virtual unsigned int nbVertices() const
Definition: Primitive.h:197
#define FLT_MAX
Definition: Vector3.h:51
static const double EGALITY_EPS
Definition: ParserGL.cpp:81
FLOAT x() const
Definition: Primitive.h:82
static PtrPrimitive checkPolygon(Polygone *&P)
Definition: ParserGL.cpp:211
static PtrPrimitive checkSegment(Segment *&P)
Definition: ParserGL.cpp:197
static const char * nameOfToken(int token)
Definition: ParserGL.cpp:467
const float FLAT_POLYGON_EPS
Definition: Types.h:71
static void ComputeBufferBB(GLint size, GLfloat *buffer, GLfloat &xmin, GLfloat &xmax, GLfloat &ymin, GLfloat &ymax, GLfloat &zmin, GLfloat &zmax)
Definition: ParserGL.cpp:448
static void NormalizePrimitiveCoordinates(GLfloat *&loc, GLfloat MaxSize, GLfloat zmin, GLfloat zmax)
Definition: ParserGL.cpp:304
virtual const Vector3 & vertex(int) const
Definition: Primitive.cpp:78
virtual const Feedback3DColor & sommet3DColor(int) const
Definition: Primitive.cpp:91
static void NormalizeBufferCoordinates(GLint size, GLfloat *buffer, GLfloat MaxSize, GLfloat &zmin, GLfloat &zmax)
Definition: ParserGL.cpp:426
void progress(float, const QString &)
Definition: VRender.cpp:255
double EGALITY_EPS
static void debug_printBuffer(GLint size, GLfloat *buffer)
Definition: ParserGL.cpp:264
virtual const Vector3 & vertex(int) const
Definition: Primitive.cpp:96
static void ComputePrimitiveBB(GLfloat *&loc, GLfloat &xmin, GLfloat &xmax, GLfloat &ymin, GLfloat &ymax, GLfloat &zmin, GLfloat &zmax)
Definition: ParserGL.cpp:351
void parseFeedbackBuffer(GLfloat *, int size, std::vector< PtrPrimitive > &primitive_tab, VRenderParams &vparams)
Definition: ParserGL.cpp:86
static PtrPrimitive checkPoint(Point *&P)
Definition: ParserGL.cpp:192
FLOAT z() const
Definition: Primitive.h:84
FLOAT y() const
Definition: Primitive.h:83
static void print3DcolorVertex(GLint size, GLint *count, GLfloat *buffer)
Definition: ParserGL.cpp:251
GLfloat depth
Definition: ParserGL.cpp:464
FLOAT FlatFactor() const
Definition: Primitive.h:203
virtual const Feedback3DColor & sommet3DColor(int i) const
Definition: Primitive.cpp:68
GLfloat * ptr
Definition: ParserGL.cpp:463
static int sizeInBuffer()
Definition: Primitive.h:104
struct _DepthIndex DepthIndex


octovis
Author(s): Kai M. Wurm , Armin Hornung
autogenerated on Mon Jun 10 2019 14:00:25