ParserGL.cpp 12.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
/*
 This file is part of the VRender library.
 Copyright (C) 2005 Cyril Soler (Cyril.Soler@imag.fr)
 Version 1.0.0, released on June 27, 2005.

 http://artis.imag.fr/Members/Cyril.Soler/VRender

 VRender is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 VRender is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with VRender; if not, write to the Free Software Foundation, Inc.,
 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/

/****************************************************************************

 Copyright (C) 2002-2014 Gilles Debunne. All rights reserved.

 This file is part of the QGLViewer library version 2.6.1.

 http://www.libqglviewer.com - contact@libqglviewer.com

 This file may be used under the terms of the GNU General Public License 
 versions 2.0 or 3.0 as published by the Free Software Foundation and
 appearing in the LICENSE file included in the packaging of this file.
 In addition, as a special exception, Gilles Debunne gives you certain 
 additional rights, described in the file GPL_EXCEPTION in this package.

 libQGLViewer uses dual licensing. Commercial/proprietary software must
 purchase a libQGLViewer Commercial License.

 This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

*****************************************************************************/

#include <assert.h>
#include <math.h>
#include <stdio.h>

#include "VRender.h"
#include "ParserGL.h"

using namespace vrender ;
using namespace std;

class ParserUtils
{
	public:
		static void NormalizeBufferCoordinates(GLint size, GLfloat * buffer, GLfloat MaxSize, GLfloat& zmin, GLfloat& zmax) ;

		static PtrPrimitive checkPoint(Point *& P);
		static PtrPrimitive checkSegment(Segment *& P);
		static PtrPrimitive checkPolygon(Polygone *& P);

		static void ComputeBufferBB(GLint size, GLfloat * buffer,
				GLfloat & xmin, GLfloat & xmax,
				GLfloat & ymin, GLfloat & ymax,
				GLfloat & zmin, GLfloat & zmax) ;

	private:
		static void print3DcolorVertex(GLint size, GLint * count, GLfloat * buffer) ;
		static void debug_printBuffer(GLint size, GLfloat *buffer) ;

		static void NormalizePrimitiveCoordinates(GLfloat * & loc,GLfloat MaxSize,GLfloat zmin,GLfloat zmax) ;
		static void ComputePrimitiveBB(	GLfloat * & loc,
				GLfloat & xmin,GLfloat & xmax,
				GLfloat & ymin,GLfloat & ymax,
				GLfloat & zmin,GLfloat & zmax);

		static const char *nameOfToken(int token);

		static const double EGALITY_EPS ;
};

const double ParserUtils::EGALITY_EPS = 0.00001 ;

void ParserGL::parseFeedbackBuffer(	GLfloat *buffer,int size,
												std::vector<PtrPrimitive>& primitive_tab,
												VRenderParams& vparams)
{
	int token;
	int nvertices = 0 ;
	nb_lines = 0 ;
	nb_polys = 0 ;
	nb_points = 0 ;
	nb_degenerated_lines = 0 ;
	nb_degenerated_polys = 0 ;
	nb_degenerated_points = 0 ;

	// pre-treatment of coordinates so as to get something more consistent

	_xmin = FLT_MAX ;
	_ymin = FLT_MAX ;
	_zmin = FLT_MAX ;
	_xmax = -FLT_MAX ;
	_ymax = -FLT_MAX ;
	_zmax = -FLT_MAX ;

	ParserUtils::ComputeBufferBB(size, buffer, _xmin,_xmax,_ymin,_ymax,_zmin,_zmax) ;

#ifdef DEBUGEPSRENDER
	printf("Buffer bounding box: %f %f %f %f %f %f\n",xmin,xmax,ymin,ymax,zmin,zmax) ;
#endif
	float Zdepth = max(_ymax-_ymin,_xmax-_xmin) ;
	ParserUtils::NormalizeBufferCoordinates(size,buffer,Zdepth,_zmin,_zmax) ;

	// now, read buffer
	GLfloat *end = buffer + size;

	GLfloat *loc = buffer ;
	int next_step = 0 ;
	int N = size/200 + 1 ;

	while (loc < end)
	{
		token = int(0.5f + *loc) ;
		loc++;

		if((end-loc)/N >= next_step)
			vparams.progress((end-loc)/(float)size, QGLViewer::tr("Parsing feedback buffer.")), ++next_step ;

		switch (token)
		{
			case GL_LINE_TOKEN:
			case GL_LINE_RESET_TOKEN:
				{
					Segment *S = new Segment(Feedback3DColor(loc),Feedback3DColor(loc+Feedback3DColor::sizeInBuffer())) ;

					primitive_tab.push_back(ParserUtils::checkSegment(S)) ;

					if(S == NULL)
						nb_degenerated_lines++ ;

					nb_lines++ ;
					loc += 2*Feedback3DColor::sizeInBuffer();
				}
				break;

			case GL_POLYGON_TOKEN:
				{
					nvertices = int(0.5f + *loc) ;
					loc++;

					std::vector<Feedback3DColor> verts ;

					for(int i=0;i<nvertices;++i)
						verts.push_back(Feedback3DColor(loc)),loc+=Feedback3DColor::sizeInBuffer() ;

					Polygone *P = new Polygone(verts) ;

					primitive_tab.push_back(ParserUtils::checkPolygon(P)) ;

					if(P == NULL)
						nb_degenerated_polys++ ;

					nb_polys++ ;
				}
				break ;

			case GL_POINT_TOKEN:
				{
					Point *Pt = new Point(Feedback3DColor(loc)) ;

					primitive_tab.push_back(Pt);//ParserUtils::checkPoint(Pt)) ;

					if(Pt == NULL)
						nb_degenerated_points++ ;

					nb_points++ ;
					loc += Feedback3DColor::sizeInBuffer();
				}
				break;
			default:
				break;
		}
	}

}

// Traitement des cas degeneres. Renvoie false si le polygone est degenere.
// Traitement des cas degeneres. Renvoie false si le segment est degenere.

PtrPrimitive ParserUtils::checkPoint(Point *& P)
{
	return P ;
}

PtrPrimitive ParserUtils::checkSegment(Segment *& P)
{
	if((P->vertex(0) - P->vertex(1)).infNorm() < EGALITY_EPS)
	{
		Point *pp = new Point(P->sommet3DColor(0)) ;
		delete P ;
		P = NULL ;

		return checkPoint(pp) ;
	}

	return P ;
}

PtrPrimitive ParserUtils::checkPolygon(Polygone *& P)
{
	if(P->nbVertices() != 3)
	{
		cout << "unexpected case: Polygon with " << P->nbVertices() << " vertices !" << endl ;
		delete P ;
		return NULL ;
	}

	if(P->FlatFactor() < FLAT_POLYGON_EPS)
	{
		// On ne traite que le cas du triangle plat, vu qu'on est sur d'avoir un triangle

		size_t n = P->nbVertices() ;

		for(size_t i=0;i<n;++i)
			if( (P->vertex(i) - P->vertex((i+1)%n)).norm() > EGALITY_EPS)
			{
				Segment *pp = new Segment(P->sommet3DColor((i+1)%n),P->sommet3DColor((i+2)%n)) ;
				delete P ;
				P = NULL ;

				return checkSegment(pp) ;
			}

		Point *pp = new Point(P->sommet3DColor(0)) ;
		delete P ;
		P = NULL ;

		return checkPoint(pp) ;
	}

	// No problem detected.

	return P ;
}


/* Write contents of one vertex to stdout. */

void ParserUtils::print3DcolorVertex(GLint size, GLint * count, GLfloat * buffer)
{
	printf("  ");
	for (size_t i = 0; i < Feedback3DColor::sizeInBuffer(); i++)
	{
		printf("%4.2f ", buffer[size - (*count)]);
		*count = *count - 1;
	}
	printf("\n");
}

void ParserUtils::debug_printBuffer(GLint size, GLfloat * buffer)
{
	GLint count;
	int token, nvertices;

	count = size;
	while (count) {
		token = int(buffer[size - count]);
		count--;
		switch (token)
		{
			case GL_PASS_THROUGH_TOKEN:
				printf("GL_PASS_THROUGH_TOKEN\n");
				printf("  %4.2f\n", buffer[size - count]);
				count--;
				break;
			case GL_POINT_TOKEN:
				printf("GL_POINT_TOKEN\n");
				print3DcolorVertex(size, &count, buffer);
				break;
			case GL_LINE_TOKEN:
				printf("GL_LINE_TOKEN\n");
				print3DcolorVertex(size, &count, buffer);
				print3DcolorVertex(size, &count, buffer);
				break;
			case GL_LINE_RESET_TOKEN:
				printf("GL_LINE_RESET_TOKEN\n");
				print3DcolorVertex(size, &count, buffer);
				print3DcolorVertex(size, &count, buffer);
				break;
			case GL_POLYGON_TOKEN:
				printf("GL_POLYGON_TOKEN\n");
				nvertices = int(buffer[size - count]) ;
				count--;
				for (; nvertices > 0; nvertices--)
					print3DcolorVertex(size, &count, buffer);
		}
	}
}

void ParserUtils::NormalizePrimitiveCoordinates(GLfloat * & loc,GLfloat MaxSize,GLfloat zmin,GLfloat zmax)
{
	int token;
	int nvertices, i;

	token = int(*loc) ;
	loc++;
309
	int size = int(Feedback3DColor::sizeInBuffer()) ;
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355

	switch (token)
	{
		case GL_LINE_RESET_TOKEN:
		case GL_LINE_TOKEN:
			{
				for (i = 0; i < 2; i++)
					(loc+size*i)[2] = ((loc+size*i)[2] - zmin)/(zmax-zmin)*MaxSize ;

				loc += 2*size;          /* Each vertex element in the feedback buffer is size GLfloats. */
				break;
			}
		case GL_POLYGON_TOKEN:
			{
				nvertices = int(*loc) ;
				loc++;

				for (i = 0; i < nvertices; i++)
					(loc+size*i)[2] = ((loc+size*i)[2] - zmin)/(zmax-zmin)*MaxSize ;

				loc += nvertices * size;  /* Each vertex element in the feedback buffer is size GLfloats. */
				break;
			}
		case GL_POINT_TOKEN:
			{
				loc[2] = (loc[2] - zmin)/(zmax-zmin)*MaxSize ;

				loc += size;           /* Each vertex element in the feedback buffer is size GLfloats. */
				break;
			}
		default:
			/* XXX Left as an excersie to the reader. */
#ifdef DEBUGEPSRENDER
			printf("%s (%d) not handled yet. Sorry.\n", ParserUtils::nameOfToken(token), token);
#endif
			;
	}
}

void ParserUtils::ComputePrimitiveBB(GLfloat * & loc,GLfloat & xmin,GLfloat & xmax,GLfloat & ymin,GLfloat & ymax, GLfloat & zmin,GLfloat & zmax)
{
	int token;
	int nvertices, i;

	token = int(*loc) ;
	loc++;
356
	int size = int(Feedback3DColor::sizeInBuffer()) ;
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481

	switch (token)
	{
		case GL_LINE_RESET_TOKEN:
		case GL_LINE_TOKEN:
			{
				for (i = 0; i < 2; i++)
				{
					Feedback3DColor f(loc+size*i) ;

					if(f.x() < xmin) xmin = GLfloat(f.x()) ;
					if(f.y() < ymin) ymin = GLfloat(f.y()) ;
					if(f.z() < zmin) zmin = GLfloat(f.z()) ;
					if(f.x() > xmax) xmax = GLfloat(f.x()) ;
					if(f.y() > ymax) ymax = GLfloat(f.y()) ;
					if(f.z() > zmax) zmax = GLfloat(f.z()) ;
				}

				loc += 2*size;          /* Each vertex element in the feedback
													buffer is size GLfloats. */
				break;
			}
		case GL_POLYGON_TOKEN:
			{
				nvertices = int(*loc) ;
				loc++;

				for (i = 0; i < nvertices; i++)
				{
					Feedback3DColor f(loc+size*i) ;

					if(f.x() < xmin) xmin = GLfloat(f.x()) ;
					if(f.y() < ymin) ymin = GLfloat(f.y()) ;
					if(f.z() < zmin) zmin = GLfloat(f.z()) ;
					if(f.x() > xmax) xmax = GLfloat(f.x()) ;
					if(f.y() > ymax) ymax = GLfloat(f.y()) ;
					if(f.z() > zmax) zmax = GLfloat(f.z()) ;
				}

				loc += nvertices * size;  /* Each vertex element in the
													  feedback buffer is size GLfloats. */
				break;
			}
		case GL_POINT_TOKEN:
			{
				Feedback3DColor f(loc) ;

				if(f.x() < xmin) xmin = GLfloat(f.x()) ;
				if(f.y() < ymin) ymin = GLfloat(f.y()) ;
				if(f.z() < zmin) zmin = GLfloat(f.z()) ;
				if(f.x() > xmax) xmax = GLfloat(f.x()) ;
				if(f.y() > ymax) ymax = GLfloat(f.y()) ;
				if(f.z() > zmax) zmax = GLfloat(f.z()) ;

				loc += size;           /* Each vertex element in the feedback
												  buffer is size GLfloats. */
				break;
			}
		default:
			/* XXX Left as an excersie to the reader. */
#ifdef DEBUGEPSRENDER
			printf("Incomplete implementation.  Unexpected token (%d).\n", token);
#endif
			;
	}
}

void ParserUtils::NormalizeBufferCoordinates(GLint size, GLfloat * buffer, GLfloat MaxSize, GLfloat& zmin,GLfloat& zmax)
{
	GLfloat *loc, *end;

	if(zmax == zmin)
	{
#ifdef DEBUGEPSRENDER
		printf("Warning: zmin = zmax in NormalizePrimitiveCoordinates\n") ;
#endif
		return ;
	}

	loc = buffer;
	end = buffer + size;

	while (loc < end)
		NormalizePrimitiveCoordinates(loc,MaxSize,zmin,zmax);

	zmin = 0.0 ;
	zmax = MaxSize ;
}

void ParserUtils::ComputeBufferBB(GLint size, GLfloat * buffer,
		     GLfloat & xmin, GLfloat & xmax,
		     GLfloat & ymin, GLfloat & ymax,
		     GLfloat & zmin, GLfloat & zmax)
{
	GLfloat *loc, *end;

	loc = buffer;
	end = buffer + size;

	while (loc < end)
		ComputePrimitiveBB(loc,xmin,xmax,ymin,ymax,zmin,zmax);
}

typedef struct _DepthIndex {
  GLfloat *ptr;
  GLfloat depth;
} DepthIndex;

const char *ParserUtils::nameOfToken(int token)
{
	switch(token)
	{
		case GL_PASS_THROUGH_TOKEN: 	return "GL_PASS_THROUGH_TOKEN" ;
		case GL_POINT_TOKEN: 			return "GL_POINT_TOKEN" ;
		case GL_LINE_TOKEN: 				return "GL_LINE_TOKEN" ;
		case GL_POLYGON_TOKEN: 			return "GL_POLYGON_TOKEN" ;
		case GL_BITMAP_TOKEN: 			return "GL_BITMAP_TOKEN" ;
		case GL_DRAW_PIXEL_TOKEN:		return "GL_DRAW_PIXEL_TOKEN" ;
		case GL_COPY_PIXEL_TOKEN:		return "GL_COPY_PIXEL_TOKEN" ;
		case GL_LINE_RESET_TOKEN:		return "GL_LINE_RESET_TOKEN" ;
		default:
										  		return "(Unidentified token)" ;
	}
}