FreeWRL/FreeX3D  3.0.0
backend.cc
1 /*
2 ** License Applicability. Except to the extent portions of this file are
3 ** made subject to an alternative license as permitted in the SGI Free
4 ** Software License B, Version 1.1 (the "License"), the contents of this
5 ** file are subject only to the provisions of the License. You may not use
6 ** this file except in compliance with the License. You may obtain a copy
7 ** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
8 ** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
9 **
10 ** http://oss.sgi.com/projects/FreeB
11 **
12 ** Note that, as provided in the License, the Software is distributed on an
13 ** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
14 ** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
15 ** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
16 ** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
17 **
18 ** Original Code. The Original Code is: OpenGL Sample Implementation,
19 ** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
20 ** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
21 ** Copyright in any portions created by third parties is as indicated
22 ** elsewhere herein. All Rights Reserved.
23 **
24 ** Additional Notice Provisions: The application programming interfaces
25 ** established by SGI in conjunction with the Original Code are The
26 ** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
27 ** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
28 ** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
29 ** Window System(R) (Version 1.3), released October 19, 1998. This software
30 ** was created using the OpenGL(R) version 1.2.1 Sample Implementation
31 ** published by SGI, but has not been independently verified as being
32 ** compliant with the OpenGL(R) version 1.2.1 Specification.
33 */
34 
35 /*
36  * backend.c++
37  *
38  */
39 
40 /* Bezier surface backend
41  - interprets display mode (wireframe,shaded,...)
42 */
43 #include <stdio.h>
44 #include "glimports.h"
45 #include "mystdio.h"
46 #include "backend.h"
47 #include "basiccrveval.h"
48 #include "basicsurfeval.h"
49 #include "nurbsconsts.h"
50 
51 #define NOWIREFRAME
52 
53 
54 /*-------------------------------------------------------------------------
55  * bgnsurf - preamble to surface definition and evaluations
56  *-------------------------------------------------------------------------
57  */
58 void
59 Backend::bgnsurf( int wiretris, int wirequads, long nuid )
60 {
61 /*#ifndef NOWIREFRAME*/ //need this for old version
62  wireframetris = wiretris;
63  wireframequads = wirequads;
64 /*#endif*/
65 
66  /*in the spec, GLU_DISPLAY_MODE is either
67  * GLU_FILL
68  * GLU_OUTLINE_POLY
69  * GLU_OUTLINE_PATCH.
70  *In fact, GLU_FLL is has the same effect as
71  * set GL_FRONT_AND_BACK to be GL_FILL
72  * and GLU_OUTLINE_POLY is the same as set
73  * GL_FRONT_AND_BACK to be GL_LINE
74  *It is more efficient to do this once at the beginning of
75  *each surface than to do it for each primitive.
76  * The internal has more options: outline_triangle and outline_quad
77  *can be seperated. But since this is not in spec, and more importantly,
78  *this is not so useful, so we don't need to keep this option.
79  */
80 
81  surfaceEvaluator.bgnmap2f( nuid );
82 
83  if(wiretris)
84  surfaceEvaluator.polymode(N_MESHLINE);
85  else
86  surfaceEvaluator.polymode(N_MESHFILL);
87 }
88 
89 void
90 Backend::patch( REAL ulo, REAL uhi, REAL vlo, REAL vhi )
91 {
92  surfaceEvaluator.domain2f( ulo, uhi, vlo, vhi );
93 }
94 
95 void
96 Backend::surfbbox( long type, REAL *from, REAL *to )
97 {
98  surfaceEvaluator.range2f( type, from, to );
99 }
100 
101 /*-------------------------------------------------------------------------
102  * surfpts - pass a desription of a surface map
103  *-------------------------------------------------------------------------
104  */
105 void
106 Backend::surfpts(
107  long type, /* geometry, color, texture, normal */
108  REAL *pts, /* control points */
109  long ustride, /* distance to next point in u direction */
110  long vstride, /* distance to next point in v direction */
111  int uorder, /* u parametric order */
112  int vorder, /* v parametric order */
113  REAL ulo, /* u lower bound */
114  REAL uhi, /* u upper bound */
115  REAL vlo, /* v lower bound */
116  REAL vhi ) /* v upper bound */
117 {
118  surfaceEvaluator.map2f( type,ulo,uhi,ustride,uorder,vlo,vhi,vstride,vorder,pts );
119  surfaceEvaluator.enable( type );
120 }
121 
122 /*-------------------------------------------------------------------------
123  * surfgrid - define a lattice of points with origin and offset
124  *-------------------------------------------------------------------------
125  */
126 void
127 Backend::surfgrid( REAL u0, REAL u1, long nu, REAL v0, REAL v1, long nv )
128 {
129  surfaceEvaluator.mapgrid2f( nu, u0, u1, nv, v0, v1 );
130 }
131 
132 /*-------------------------------------------------------------------------
133  * surfmesh - evaluate a mesh of points on lattice
134  *-------------------------------------------------------------------------
135  */
136 void
137 Backend::surfmesh( long u, long v, long n, long m )
138 {
139 #ifndef NOWIREFRAME
140  if( wireframequads ) {
141  long v0, v1;
142  long u0f = u, u1f = u+n;
143  long v0f = v, v1f = v+m;
144  long parity = (u & 1);
145 
146  for( v0 = v0f, v1 = v0f++ ; v0<v1f; v0 = v1, v1++ ) {
147  surfaceEvaluator.bgnline();
148  for( long u = u0f; u<=u1f; u++ ) {
149  if( parity ) {
150  surfaceEvaluator.evalpoint2i( u, v0 );
151  surfaceEvaluator.evalpoint2i( u, v1 );
152  } else {
153  surfaceEvaluator.evalpoint2i( u, v1 );
154  surfaceEvaluator.evalpoint2i( u, v0 );
155  }
156  parity = 1 - parity;
157  }
158  surfaceEvaluator.endline();
159  }
160  } else {
161  surfaceEvaluator.mapmesh2f( N_MESHFILL, u, u+n, v, v+m );
162  }
163 #else
164  if( wireframequads ) {
165 
166  surfaceEvaluator.mapmesh2f( N_MESHLINE, u, u+n, v, v+m );
167  } else {
168 
169  surfaceEvaluator.mapmesh2f( N_MESHFILL, u, u+n, v, v+m );
170  }
171 #endif
172 }
173 
174 /*-------------------------------------------------------------------------
175  * endsurf - postamble to surface
176  *-------------------------------------------------------------------------
177  */
178 void
179 Backend::endsurf( void )
180 {
181  surfaceEvaluator.endmap2f();
182 }
183 
184 /***************************************/
185 void
186 Backend::bgntfan( void )
187 {
188  surfaceEvaluator.bgntfan();
189 /*
190  if(wireframetris)
191  surfaceEvaluator.polymode( N_MESHLINE );
192  else
193  surfaceEvaluator.polymode( N_MESHFILL );
194 */
195 }
196 
197 void
198 Backend::endtfan( void )
199 {
200  surfaceEvaluator.endtfan();
201 }
202 
203 void
204 Backend::bgnqstrip( void )
205 {
206  surfaceEvaluator.bgnqstrip();
207 /*
208  if(wireframequads)
209  surfaceEvaluator.polymode( N_MESHLINE );
210  else
211  surfaceEvaluator.polymode( N_MESHFILL );
212 */
213 }
214 
215 void
216 Backend::endqstrip( void )
217 {
218  surfaceEvaluator.endqstrip();
219 }
220 
221 void
222 Backend::evalUStrip(int n_upper, REAL v_upper, REAL* upper_val,
223  int n_lower, REAL v_lower, REAL* lower_val
224  )
225 {
226  surfaceEvaluator.evalUStrip(n_upper, v_upper, upper_val,
227  n_lower, v_lower, lower_val);
228 }
229 
230 void
231 Backend::evalVStrip(int n_left, REAL u_left, REAL* left_val,
232  int n_right, REAL u_right, REAL* right_val
233  )
234 {
235  surfaceEvaluator.evalVStrip(n_left, u_left, left_val,
236  n_right, u_right, right_val);
237 }
238 
239 /***************************************/
240 
241 
242 /*-------------------------------------------------------------------------
243  * bgntmesh - preamble to a triangle mesh
244  *-------------------------------------------------------------------------
245  */
246 void
247 Backend::bgntmesh( const char * )
248 {
249 #ifndef NOWIREFRAME
250 
251  meshindex = 0; /* I think these need to be initialized to zero */
252  npts = 0;
253 
254  if( !wireframetris ) {
255  surfaceEvaluator.bgntmesh();
256  }
257 #else
258 
259  if( wireframetris ) {
260  surfaceEvaluator.bgntmesh();
261  surfaceEvaluator.polymode( N_MESHLINE );
262  } else {
263  surfaceEvaluator.bgntmesh();
264  surfaceEvaluator.polymode( N_MESHFILL );
265  }
266 #endif
267 }
268 
269 void
270 Backend::tmeshvert( GridTrimVertex *v )
271 {
272  if( v->isGridVert() ) {
273  tmeshvert( v->g );
274  } else {
275  tmeshvert( v->t );
276  }
277 }
278 
279 void
280 Backend::tmeshvertNOGE(TrimVertex *t)
281 {
282 // surfaceEvaluator.inDoEvalCoord2NOGE( t->param[0], t->param[1], temp, ttt);
283 #ifdef USE_OPTTT
284  surfaceEvaluator.inDoEvalCoord2NOGE( t->param[0], t->param[1], t->cache_point, t->cache_normal);
285 #endif
286 }
287 
288 //opt for a line with the same u.
289 void
290 Backend::tmeshvertNOGE_BU(TrimVertex *t)
291 {
292 #ifdef USE_OPTTT
293  surfaceEvaluator.inDoEvalCoord2NOGE_BU( t->param[0], t->param[1], t->cache_point, t->cache_normal);
294 #endif
295 }
296 
297 //opt for a line with the same v.
298 void
299 Backend::tmeshvertNOGE_BV(TrimVertex *t)
300 {
301 #ifdef USE_OPTTT
302  surfaceEvaluator.inDoEvalCoord2NOGE_BV( t->param[0], t->param[1], t->cache_point, t->cache_normal);
303 #endif
304 }
305 
306 void
307 Backend::preEvaluateBU(REAL u)
308 {
309  surfaceEvaluator.inPreEvaluateBU_intfac(u);
310 }
311 
312 void
313 Backend::preEvaluateBV(REAL v)
314 {
315  surfaceEvaluator.inPreEvaluateBV_intfac(v);
316 }
317 
318 
319 /*-------------------------------------------------------------------------
320  * tmeshvert - evaluate a point on a triangle mesh
321  *-------------------------------------------------------------------------
322  */
323 void
324 Backend::tmeshvert( TrimVertex *t )
325 {
326 
327 #ifndef NOWIREFRAME
328  const long nuid = t->nuid;
329 #endif
330  const REAL u = t->param[0];
331  const REAL v = t->param[1];
332 
333 #ifndef NOWIREFRAME
334  npts++;
335  if( wireframetris ) {
336  if( npts >= 3 ) {
337  surfaceEvaluator.bgnclosedline();
338  if( mesh[0][2] == 0 )
339  surfaceEvaluator.evalcoord2f( mesh[0][3], mesh[0][0], mesh[0][1] );
340  else
341  surfaceEvaluator.evalpoint2i( (long) mesh[0][0], (long) mesh[0][1] );
342  if( mesh[1][2] == 0 )
343  surfaceEvaluator.evalcoord2f( mesh[1][3], mesh[1][0], mesh[1][1] );
344  else
345  surfaceEvaluator.evalpoint2i( (long) mesh[1][0], (long) mesh[1][1] );
346  surfaceEvaluator.evalcoord2f( nuid, u, v );
347  surfaceEvaluator.endclosedline();
348  }
349  mesh[meshindex][0] = u;
350  mesh[meshindex][1] = v;
351  mesh[meshindex][2] = 0;
352  mesh[meshindex][3] = nuid;
353  meshindex = (meshindex+1) % 2;
354  } else {
355  surfaceEvaluator.evalcoord2f( nuid, u, v );
356  }
357 #else
358 
359  surfaceEvaluator.evalcoord2f( 0, u, v );
360 //for uninitial memory read surfaceEvaluator.evalcoord2f( nuid, u, v );
361 #endif
362 }
363 
364 //the same as tmeshvert(trimvertex), for efficiency purpose
365 void
366 Backend::tmeshvert( REAL u, REAL v )
367 {
368 #ifndef NOWIREFRAME
369  const long nuid = 0;
370 
371  npts++;
372  if( wireframetris ) {
373  if( npts >= 3 ) {
374  surfaceEvaluator.bgnclosedline();
375  if( mesh[0][2] == 0 )
376  surfaceEvaluator.evalcoord2f( mesh[0][3], mesh[0][0], mesh[0][1] );
377  else
378  surfaceEvaluator.evalpoint2i( (long) mesh[0][0], (long) mesh[0][1] );
379  if( mesh[1][2] == 0 )
380  surfaceEvaluator.evalcoord2f( mesh[1][3], mesh[1][0], mesh[1][1] );
381  else
382  surfaceEvaluator.evalpoint2i( (long) mesh[1][0], (long) mesh[1][1] );
383  surfaceEvaluator.evalcoord2f( nuid, u, v );
384  surfaceEvaluator.endclosedline();
385  }
386  mesh[meshindex][0] = u;
387  mesh[meshindex][1] = v;
388  mesh[meshindex][2] = 0;
389  mesh[meshindex][3] = nuid;
390  meshindex = (meshindex+1) % 2;
391  } else {
392  surfaceEvaluator.evalcoord2f( nuid, u, v );
393  }
394 #else
395 
396  surfaceEvaluator.evalcoord2f( 0, u, v );
397 #endif
398 }
399 
400 /*-------------------------------------------------------------------------
401  * tmeshvert - evaluate a grid point of a triangle mesh
402  *-------------------------------------------------------------------------
403  */
404 void
405 Backend::tmeshvert( GridVertex *g )
406 {
407  const long u = g->gparam[0];
408  const long v = g->gparam[1];
409 
410 #ifndef NOWIREFRAME
411  npts++;
412  if( wireframetris ) {
413  if( npts >= 3 ) {
414  surfaceEvaluator.bgnclosedline();
415  if( mesh[0][2] == 0 )
416  surfaceEvaluator.evalcoord2f( (long) mesh[0][3], mesh[0][0], mesh[0][1] );
417  else
418  surfaceEvaluator.evalpoint2i( (long) mesh[0][0], (long) mesh[0][1] );
419  if( mesh[1][2] == 0 )
420  surfaceEvaluator.evalcoord2f( (long) mesh[1][3], mesh[1][0], mesh[1][1] );
421  else
422  surfaceEvaluator.evalpoint2i( (long) mesh[1][0], (long) mesh[1][1] );
423  surfaceEvaluator.evalpoint2i( u, v );
424  surfaceEvaluator.endclosedline();
425  }
426  mesh[meshindex][0] = u;
427  mesh[meshindex][1] = v;
428  mesh[meshindex][2] = 1;
429  meshindex = (meshindex+1) % 2;
430  } else {
431  surfaceEvaluator.evalpoint2i( u, v );
432  }
433 #else
434  surfaceEvaluator.evalpoint2i( u, v );
435 #endif
436 }
437 
438 /*-------------------------------------------------------------------------
439  * swaptmesh - perform a swap of the triangle mesh pointers
440  *-------------------------------------------------------------------------
441  */
442 void
443 Backend::swaptmesh( void )
444 {
445 #ifndef NOWIREFRAME
446  if( wireframetris ) {
447  meshindex = 1 - meshindex;
448  } else {
449  surfaceEvaluator.swaptmesh();
450  }
451 #else
452  surfaceEvaluator.swaptmesh();
453 #endif
454 }
455 
456 /*-------------------------------------------------------------------------
457  * endtmesh - postamble to triangle mesh
458  *-------------------------------------------------------------------------
459  */
460 void
461 Backend::endtmesh( void )
462 {
463 #ifndef NOWIREFRAME
464  if( ! wireframetris )
465  surfaceEvaluator.endtmesh();
466 #else
467  surfaceEvaluator.endtmesh();
468 /* surfaceEvaluator.polymode( N_MESHFILL );*/
469 #endif
470 }
471 
472 
473 /*-------------------------------------------------------------------------
474  * bgnoutline - preamble to outlined rendering
475  *-------------------------------------------------------------------------
476  */
477 void
478 Backend::bgnoutline( void )
479 {
480  surfaceEvaluator.bgnline();
481 }
482 
483 /*-------------------------------------------------------------------------
484  * linevert - evaluate a point on an outlined contour
485  *-------------------------------------------------------------------------
486  */
487 void
488 Backend::linevert( TrimVertex *t )
489 {
490  surfaceEvaluator.evalcoord2f( t->nuid, t->param[0], t->param[1] );
491 }
492 
493 /*-------------------------------------------------------------------------
494  * linevert - evaluate a grid point of an outlined contour
495  *-------------------------------------------------------------------------
496  */
497 void
498 Backend::linevert( GridVertex *g )
499 {
500  surfaceEvaluator.evalpoint2i( g->gparam[0], g->gparam[1] );
501 }
502 
503 /*-------------------------------------------------------------------------
504  * endoutline - postamble to outlined rendering
505  *-------------------------------------------------------------------------
506  */
507 void
508 Backend::endoutline( void )
509 {
510  surfaceEvaluator.endline();
511 }
512 
513 /*-------------------------------------------------------------------------
514  * triangle - output a triangle
515  *-------------------------------------------------------------------------
516  */
517 void
518 Backend::triangle( TrimVertex *a, TrimVertex *b, TrimVertex *c )
519 {
520 /* bgntmesh( "spittriangle" );*/
521  bgntfan();
522  tmeshvert( a );
523  tmeshvert( b );
524  tmeshvert( c );
525  endtfan();
526 /* endtmesh();*/
527 }
528 
529 void
530 Backend::bgncurv( void )
531 {
532  curveEvaluator.bgnmap1f( 0 );
533 }
534 
535 void
536 Backend::segment( REAL ulo, REAL uhi )
537 {
538  curveEvaluator.domain1f( ulo, uhi );
539 }
540 
541 void
542 Backend::curvpts(
543  long type, /* geometry, color, texture, normal */
544  REAL *pts, /* control points */
545  long stride, /* distance to next point */
546  int order, /* parametric order */
547  REAL ulo, /* lower parametric bound */
548  REAL uhi ) /* upper parametric bound */
549 
550 {
551  curveEvaluator.map1f( type, ulo, uhi, stride, order, pts );
552  curveEvaluator.enable( type );
553 }
554 
555 void
556 Backend::curvgrid( REAL u0, REAL u1, long nu )
557 {
558  curveEvaluator.mapgrid1f( nu, u0, u1 );
559 }
560 
561 void
562 Backend::curvmesh( long from, long n )
563 {
564  curveEvaluator.mapmesh1f( N_MESHFILL, from, from+n );
565 }
566 
567 void
568 Backend::curvpt(REAL u)
569 {
570  curveEvaluator.evalcoord1f( 0, u );
571 }
572 
573 void
574 Backend::bgnline( void )
575 {
576  curveEvaluator.bgnline();
577 }
578 
579 void
580 Backend::endline( void )
581 {
582  curveEvaluator.endline();
583 }
584 
585 void
586 Backend::endcurv( void )
587 {
588  curveEvaluator.endmap1f();
589 }