Debian Codegraph @

lib/database/src/so/nodes/SoIndexedNurbsCurve.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 /*
38 * Copyright (C) 1990,91,92,93 Silicon Graphics, Inc.
39 *
40 _______________________________________________________________________
41 ______________ S I L I C O N G R A P H I C S I N C . ____________
42 |
43 | $Revision: 1.2 $
44 |
45 | Classes:
46 | SoIndexedNurbsCurve
47 |
48 | Author(s) : Dave Immel
49 |
50 ______________ S I L I C O N G R A P H I C S I N C . ____________
51 _______________________________________________________________________
52 */
53
54 #include <GL/gl.h>
55 #include <Inventor/SbBox.h>
56 #include <Inventor/SoDB.h>
57 #include <Inventor/actions/SoGLRenderAction.h>
58 #include <Inventor/actions/SoGetBoundingBoxAction.h>
59 #include <Inventor/actions/SoRayPickAction.h>
60 #include <Inventor/bundles/SoMaterialBundle.h>
61 #include <Inventor/caches/SoBoundingBoxCache.h>
62 #include <Inventor/details/SoDetail.h>
63 #include <Inventor/elements/SoComplexityElement.h>
64 #include <Inventor/elements/SoComplexityTypeElement.h>
65 #include <Inventor/elements/SoCoordinateElement.h>
66 #include <Inventor/elements/SoDrawStyleElement.h>
67 #include <Inventor/elements/SoGLTextureEnabledElement.h>
68 #include <Inventor/elements/SoLightModelElement.h>
69 #include <Inventor/elements/SoModelMatrixElement.h>
70 #include <Inventor/elements/SoProjectionMatrixElement.h>
71 #include <Inventor/elements/SoViewingMatrixElement.h>
72 #include <Inventor/elements/SoViewportRegionElement.h>
73 #include <Inventor/nodes/SoIndexedNurbsCurve.h>
74 #include "nurbs/SoAddPrefix.h"
75 #include "nurbs/SoGLRender.h"
76 #include "nurbs/SoPickRender.h"
77 #include "nurbs/SoPrimRender.h"
78
79 SO_NODE_SOURCE(SoIndexedNurbsCurve);
80
81 ////////////////////////////////////////////////////////////////////////
82 //
83 // Description:
84 // Constructor
85 //
86 // Use: public
87
88 SoIndexedNurbsCurve::SoIndexedNurbsCurve()
89 //
90 ////////////////////////////////////////////////////////////////////////
91 {
92 SO_NODE_CONSTRUCTOR(SoIndexedNurbsCurve);
93
94 SO_NODE_ADD_FIELD(numControlPoints, (0));
95 SO_NODE_ADD_FIELD(coordIndex, (0));
96 SO_NODE_ADD_FIELD(knotVector, (0.0));
97
98 isBuiltIn = TRUE;
99 }
100
101 ////////////////////////////////////////////////////////////////////////
102 //
103 // Description:
104 // Destructor
105 //
106 // Use: private
107
108 SoIndexedNurbsCurve::~SoIndexedNurbsCurve()
109 //
110 ////////////////////////////////////////////////////////////////////////
111 {
112 }
113
114 ////////////////////////////////////////////////////////////////////////
115 //
116 // Description:
117 // Performs GL rendering of the NURBS surface.
118 //
119 // Use: private
120
121 void
122 SoIndexedNurbsCurve::GLRender(SoGLRenderAction *action)
123 //
124 ////////////////////////////////////////////////////////////////////////
125 {
126 // First see if the object is visible and should be rendered now
127 if (! shouldGLRender(action))
128 return;
129
130 SoState *state = action->getState();
131 state->push();
132
133 // Draw unlit:
134 if (SoLightModelElement::get(state) != SoLightModelElement::BASE_COLOR) {
135 SoLightModelElement::set(state,
136 SoLightModelElement::BASE_COLOR);
137 }
138 // Make sure textures are disabled, as texturing of NURBS curves
139 // has not been implemented.
140 if (SoGLTextureEnabledElement::get(state)) {
141 SoGLTextureEnabledElement::set(state, FALSE);
142 }
143
144 // Make sure the first current material is sent to GL
145 SoMaterialBundle mb(action);
146 mb.sendFirst();
147
148 const SoCoordinateElement *ce =
149 SoCoordinateElement::getInstance(state);
150
151
152 //
153 // Create a NURBS library renderer, and use it by making regular
154 // GL calls to it.
155 //
156 _SoNurbsGLRender *GLRender = new _SoNurbsGLRender();
157
158 float val = SoComplexityElement::get(state);
159 if (val < 0.0) val = 0.0;
160 if (val > 1.0) val = 1.0;
161
162 if (SoComplexityTypeElement::get(state) ==
163 SoComplexityTypeElement::OBJECT_SPACE)
164 {
165 //
166 // Set the nurbs properties to render the curve with uniform
167 // tessellation over the whole curve.
168 //
169 int steps;
170 if (val < 0.5) steps = (int)(18.0 *val) + 1;
171 else steps = (int)(380.0*val) - 180;
172
173 GLRender->setnurbsproperty( N_V3D, N_SAMPLINGMETHOD,
174 N_FIXEDRATE );
175 GLRender->setnurbsproperty( N_V3DR, N_SAMPLINGMETHOD,
176 N_FIXEDRATE );
177 GLRender->setnurbsproperty( N_V3D, N_S_STEPS, steps);
178 GLRender->setnurbsproperty( N_V3D, N_T_STEPS, steps);
179 GLRender->setnurbsproperty( N_V3DR, N_S_STEPS, steps);
180 GLRender->setnurbsproperty( N_V3DR, N_T_STEPS, steps);
181 }
182 else
183 {
184 //
185 // Set the nurbs properties to render the curve with a view
186 // dependent tessellation using the given pixel tolerance.
187 //
188 float pixTolerance;
189
190 if (val < 0.10) pixTolerance = 10;
191 else if (val < 0.20) pixTolerance = 8;
192 else if (val < 0.30) pixTolerance = 6;
193 else if (val < 0.40) pixTolerance = 4;
194 else if (val < 0.50) pixTolerance = 2;
195 else if (val < 0.70) pixTolerance = 1;
196 else if (val < 0.80) pixTolerance = .5;
197 else if (val < 0.90) pixTolerance = .25;
198 else pixTolerance = .125;
199
200 GLRender->setnurbsproperty( N_V3D, N_SAMPLINGMETHOD,
201 N_PARAMETRICDISTANCE );
202 GLRender->setnurbsproperty( N_V3DR, N_SAMPLINGMETHOD,
203 N_PARAMETRICDISTANCE );
204 GLRender->setnurbsproperty( N_V3D, N_PIXEL_TOLERANCE, pixTolerance );
205 GLRender->setnurbsproperty( N_V3DR, N_PIXEL_TOLERANCE, pixTolerance );
206
207 //
208 // Calculate the total viewing matrix by concatenating the modeling
209 // matrix, the camera's viewing matrix, and the projection matrix.
210 // Pass the resulting matrix to the NURBS library for use in
211 // determining sampling and culling of the curve.
212 //
213 const SbViewportRegion & vpRegion =
214 SoViewportRegionElement::get(state);
215 const SbVec2s & vpSize = vpRegion.getViewportSizePixels();
216 SbMatrix totalMat;
217 calcTotalMatrix (state, totalMat);
218 GLRender->loadMatrices (totalMat, vpSize);
219 }
220
221 // Draw the curve.
222 drawNURBS (GLRender, state);
223 delete GLRender;
224
225 // Restore state
226 state->pop();
227
228 return;
229 }
230
231 ////////////////////////////////////////////////////////////////////////
232 //
233 // Description:
234 // Implements ray picking
235 //
236 // Use: protected
237
238 void
239 SoIndexedNurbsCurve::rayPick(SoRayPickAction *action)
240 //
241 ////////////////////////////////////////////////////////////////////////
242 {
243 // First see if the object is pickable
244 if (! shouldRayPick(action))
245 return;
246
247 action->setObjectSpace();
248
249 //
250 // Create an SoPickRender class which performs picking with the
251 // software NURBS library.
252 //
253 _SoNurbsPickRender pickRender(action);
254 float pixTolerance;
255 float val = SoComplexityElement::get(action->getState());
256 if (val < 0.10) pixTolerance = 10;
257 else if (val < 0.20) pixTolerance = 8;
258 else if (val < 0.30) pixTolerance = 6;
259 else if (val < 0.40) pixTolerance = 4;
260 else if (val < 0.50) pixTolerance = 2;
261 else if (val < 0.70) pixTolerance = 1;
262 else if (val < 0.80) pixTolerance = .5;
263 else if (val < 0.90) pixTolerance = .25;
264 else pixTolerance = .125;
265
266 //
267 // Use screen space tessellation with the pixel tolerance
268 // set to pixTolerance, and notify the library that the sampling
269 // and culling matrices will be passed in.
270 //
271 pickRender.setnurbsproperty (N_V3D, N_PIXEL_TOLERANCE, pixTolerance);
272 pickRender.setnurbsproperty (N_V3DR, N_PIXEL_TOLERANCE, pixTolerance);
273 pickRender.setnurbsproperty (N_V3D,N_SAMPLINGMETHOD, N_PARAMETRICDISTANCE);
274 pickRender.setnurbsproperty (N_V3DR,N_SAMPLINGMETHOD,N_PARAMETRICDISTANCE);
275 pickRender.setnurbsproperty (N_V3D, N_CULLING, N_CULLINGON);
276 pickRender.setnurbsproperty (N_V3DR, N_CULLING, N_CULLINGON);
277
278 //
279 // Calculate the total viewing matrix by concatenating the modeling
280 // matrix, the camera's viewing matrix, and the projection matrix.
281 // Pass the resulting matrix to the NURBS library for use in determining
282 // sampling and culling of the curve.
283 //
284 const SbViewportRegion & vpRegion =
285 SoViewportRegionElement::get(action->getState());
286 const SbVec2s & vpSize = vpRegion.getViewportSizePixels();
287 SbMatrix totalMat;
288 calcTotalMatrix(action->getState(), totalMat);
289 pickRender.loadMatrices(totalMat, vpSize);
290
291 //
292 // Draw the NURBS curve. The SoPickRender class will receive primitive
293 // drawn by the NURBS library and test them for intersection.
294 //
295 drawNURBS(&pickRender, action->getState());
296 }
297
298 ////////////////////////////////////////////////////////////////////////
299 //
300 // Description:
301 // Generates lines representing the NURBS curve.
302 //
303 // Use: protected
304
305 void
306 SoIndexedNurbsCurve::generatePrimitives(SoAction *action)
307 //
308 ////////////////////////////////////////////////////////////////////////
309 {
310 _SoNurbsPrimRender primRender(action, primCB, (void *)this);
311
312 //
313 // Find the number of steps required for tessellation and the pixel
314 // tolerance used for tessellation.
315 //
316 float val = SoComplexityElement::get(action->getState());
317
318 if (SoComplexityTypeElement::get(action->getState()) ==
319 SoComplexityTypeElement::OBJECT_SPACE)
320 {
321 //
322 // Set the nurbs properties to render the curve with uniform
323 // tessellation over the whole curve.
324 //
325 int steps;
326 if (val < 0.5) steps = (int)(18.0 *val) + 1;
327 else steps = (int)(380.0*val) - 180;
328
329 primRender.setnurbsproperty( N_V3D, N_SAMPLINGMETHOD,
330 N_FIXEDRATE );
331 primRender.setnurbsproperty( N_V3DR, N_SAMPLINGMETHOD,
332 N_FIXEDRATE );
333 primRender.setnurbsproperty( N_V3D, N_S_STEPS, steps);
334 primRender.setnurbsproperty( N_V3D, N_T_STEPS, steps);
335 primRender.setnurbsproperty( N_V3DR, N_S_STEPS, steps);
336 primRender.setnurbsproperty( N_V3DR, N_T_STEPS, steps);
337 }
338 else
339 {
340 //
341 // Set the nurbs properties to render the curve with a view
342 // dependent tessellation using the given pixel tolerance.
343 //
344 float pixTolerance;
345
346 if (val < 0.10) pixTolerance = 10;
347 else if (val < 0.20) pixTolerance = 8;
348 else if (val < 0.30) pixTolerance = 6;
349 else if (val < 0.40) pixTolerance = 4;
350 else if (val < 0.50) pixTolerance = 2;
351 else if (val < 0.70) pixTolerance = 1;
352 else if (val < 0.80) pixTolerance = .5;
353 else if (val < 0.90) pixTolerance = .25;
354 else pixTolerance = .125;
355
356 primRender.setnurbsproperty( N_V3D, N_SAMPLINGMETHOD,
357 N_PARAMETRICDISTANCE );
358 primRender.setnurbsproperty( N_V3DR, N_SAMPLINGMETHOD,
359 N_PARAMETRICDISTANCE );
360 primRender.setnurbsproperty( N_V3D, N_PIXEL_TOLERANCE, pixTolerance );
361 primRender.setnurbsproperty( N_V3DR, N_PIXEL_TOLERANCE, pixTolerance );
362
363 //
364 // Calculate the total viewing matrix by concatenating the modeling
365 // matrix, the camera's viewing matrix, and the projection matrix.
366 // Pass the resulting matrix to the NURBS library for use in
367 // determining sampling and culling of the surface.
368 //
369 const SbViewportRegion & vpRegion =
370 SoViewportRegionElement::get(action->getState());
371 const SbVec2s & vpSize = vpRegion.getViewportSizePixels();
372 SbMatrix totalMat;
373 calcTotalMatrix (action->getState(), totalMat);
374 primRender.loadMatrices (totalMat, vpSize);
375 }
376
377 //
378 // Draw the NURBS surface. The SoPrimRender class will receive primitive
379 // drawn by the NURBS library, triangulate them, and send them to the
380 // callback.
381 drawNURBS(&primRender, action->getState());
382 }
383
384 ////////////////////////////////////////////////////////////////////////
385 //
386 // Description:
387 // Gets details back from the NURBS library and sends them to the
388 // generate primitive callbacks.
389 //
390 // Use: private, static
391
392 void
393 SoIndexedNurbsCurve::primCB(
394 SoAction *act,
395 SoPrimitiveVertex *vertices,
396 void *userData)
397
398 //
399 ////////////////////////////////////////////////////////////////////////
400 {
401 SoIndexedNurbsCurve *curve = (SoIndexedNurbsCurve *)userData;
402 curve->sendPrimitive(act, vertices);
403 }
404
405 ////////////////////////////////////////////////////////////////////////
406 //
407 // Description:
408 // Sends primitives to the generate primitive callbacks.
409 //
410 // Use: internal, public
411
412 void
413 SoIndexedNurbsCurve::sendPrimitive(
414 SoAction *act,
415 SoPrimitiveVertex *vertices )
416
417 //
418 ////////////////////////////////////////////////////////////////////////
419 {
420 invokeLineSegmentCallbacks(act, &vertices[0], &vertices[1]);
421 }
422
423 ////////////////////////////////////////////////////////////////////////
424 //
425 // Description:
426 // Redefines this to tell open caches that they contain lines.
427 //
428 // Use: extender
429
430 void
431 SoIndexedNurbsCurve::getBoundingBox(SoGetBoundingBoxAction *action)
432 //
433 ////////////////////////////////////////////////////////////////////////
434 {
435 // Let our parent class do the real work
436 SoShape::getBoundingBox(action);
437
438 // If there are any open bounding box caches, tell them that they
439 // contain lines
440 SoBoundingBoxCache::setHasLinesOrPoints(action->getState());
441 }
442
443 ////////////////////////////////////////////////////////////////////////
444 //
445 // Description:
446 // Computes bounding box for the NURBS curve.
447 //
448 // Use: protected
449
450 void
451 SoIndexedNurbsCurve::computeBBox(SoAction *action,
452 SbBox3f &box, SbVec3f &center)
453 //
454 ////////////////////////////////////////////////////////////////////////
455 {
456 const SoCoordinateElement *ce =
457 SoCoordinateElement::getInstance(action->getState());
458 const int32_t *coordIndices;
459 int32_t numCoordIndices;
460 int i;
461
462 numCoordIndices = coordIndex.getNum();
463
464 // Check for a degenerate curve
465 if (numCoordIndices == 0)
466 return;
467
468 //
469 // Loop through coordinates, keeping max bounding box and sum of coords
470 // If the coordinates are rational, divide the first three values by
471 // the fourth value before extending the bounding box.
472 //
473 coordIndices = coordIndex.getValues(0);
474 center.setValue(0.0, 0.0, 0.0);
475 if (ce->is3D()) {
476 for (i=0; i<numCoordIndices; i++)
477 {
478 const SbVec3f &coord = ce->get3((int)coordIndices[i]);
479 box.extendBy(coord);
480 center += coord;
481 }
482 }
483 else {
484 SbVec3f tmpCoord;
485 for (i=0; i<numCoordIndices; i++)
486 {
487 const SbVec4f &coord = ce->get4((int)coordIndices[i]);
488 coord.getReal(tmpCoord);
489 box.extendBy(tmpCoord);
490 center += tmpCoord;
491 }
492 }
493
494 center /= (float) numCoordIndices;
495 }
496
497
498 ////////////////////////////////////////////////////////////////////////
499 //
500 // Description:
501 // Draws the NURBS curve.
502 //
503 // Use: protected
504
505 void
506 SoIndexedNurbsCurve::drawNURBS(
507 _SoNurbsNurbsTessellator *render,
508 SoState *state )
509
510 //
511 ////////////////////////////////////////////////////////////////////////
512 {
513 const SoCoordinateElement *ce =
514 SoCoordinateElement::getInstance(state);
515 const int32_t *coordIndices;
516 int32_t numCoordIndices;
517 int32_t type, offset;
518 float *coords;
519 int i;
520
521 numCoordIndices = coordIndex.getNum();
522
523 // Check for a degenerate curve
524 if (numCoordIndices == 0)
525 return;
526
527 //
528 // Get the array of control points. Check to see if they are
529 // non-rational or rational.
530 //
531 coordIndices = coordIndex.getValues(0);
532 if (ce->is3D()) {
533 coords = new float[3*numCoordIndices];
534 float *tmpCoord = coords;
535 for (i=0; i<numCoordIndices; i++)
536 {
537 const SbVec3f &c3 = ce->get3((int)coordIndices[i]);
538 *tmpCoord++ = c3[0];
539 *tmpCoord++ = c3[1];
540 *tmpCoord++ = c3[2];
541 }
542 offset = 3 * sizeof(float);
543 type = N_V3D;
544 }
545 else {
546 coords = new float[4*numCoordIndices];
547 float *tmpCoord = coords;
548 for (i=0; i<numCoordIndices; i++)
549 {
550 const SbVec4f &c4 = ce->get4((int)coordIndices[i]);
551 *tmpCoord++ = c4[0];
552 *tmpCoord++ = c4[1];
553 *tmpCoord++ = c4[2];
554 *tmpCoord++ = c4[3];
555 }
556 offset = 4 * sizeof(float);
557 type = N_V3DR;
558 }
559
560 //
561 // Render the NURBS curve as if to the GL, but use the given
562 // NurbsTessellator instead which will make calls to the
563 // software NURBS library.
564 //
565 render->bgncurve(0);
566 render->nurbscurve (knotVector.getNum(),
567 (INREAL *) knotVector.getValues(0),
568 offset, (INREAL *)coords,
569 knotVector.getNum() - numControlPoints.getValue(),
570 type);
571
572 render->endcurve();
573 delete[] coords;
574 }
575
576 ////////////////////////////////////////////////////////////////////////
577 //
578 // Description:
579 // Calculate the total transformation matrix by contatenating the
580 // modeling matrix, the camera's viewing matrix, and the projection
581 // matrix all together.
582 //
583 // Use: protected
584
585 void
586 SoIndexedNurbsCurve::calcTotalMatrix(
587 SoState *state,
588 SbMatrix &totalMat )
589
590 //
591 ////////////////////////////////////////////////////////////////////////
592 {
593 SbMatrix mMat, pMat, vMat;
594 SbMatrix viewMat;
595
596 mMat = SoModelMatrixElement::get (state);
597 pMat = SoProjectionMatrixElement::get (state);
598 vMat = SoViewingMatrixElement::get (state);
599 multMatrix4d (viewMat, mMat, vMat);
600 multMatrix4d (totalMat, viewMat, pMat);
601 }
602
603
604
605 ////////////////////////////////////////////////////////////////////////
606 //
607 // Description:
608 // Multiply two matrices together.
609 // new = [left][right]
610 //
611 // Use: protected
612
613 void
614 SoIndexedNurbsCurve::multMatrix4d (
615 SbMatrix &n,
616 SbMatrix left,
617 SbMatrix right )
618
619 //
620 ////////////////////////////////////////////////////////////////////////
621 {
622 int i;
623
624 for (i=0; i<4; i++)
625 {
626 n[i][0] = left[i][0]*right[0][0] + left[i][1]*right[1][0] +
627 left[i][2]*right[2][0] + left[i][3]*right[3][0];
628 n[i][1] = left[i][0]*right[0][1] + left[i][1]*right[1][1] +
629 left[i][2]*right[2][1] + left[i][3]*right[3][1];
630 n[i][2] = left[i][0]*right[0][2] + left[i][1]*right[1][2] +
631 left[i][2]*right[2][2] + left[i][3]*right[3][2];
632 n[i][3] = left[i][0]*right[0][3] + left[i][1]*right[1][3] +
633 left[i][2]*right[2][3] + left[i][3]*right[3][3];
634 }
635 }