Debian Codegraph @

lib/database/src/so/nodes/SoCamera.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 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.1.1.1 $
44 |
45 | Classes:
46 | SoCamera
47 |
48 | Author(s) : Paul S. Strauss
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/actions/SoCallbackAction.h>
56 #include <Inventor/actions/SoGLRenderAction.h>
57 #include <Inventor/actions/SoGetBoundingBoxAction.h>
58 #include <Inventor/actions/SoGetMatrixAction.h>
59 #include <Inventor/actions/SoHandleEventAction.h>
60 #include <Inventor/actions/SoRayPickAction.h>
61 #include <Inventor/elements/SoFocalDistanceElement.h>
62 #include <Inventor/elements/SoGLCacheContextElement.h>
63 #include <Inventor/elements/SoGLLazyElement.h>
64 #include <Inventor/elements/SoGLProjectionMatrixElement.h>
65 #include <Inventor/elements/SoGLRenderPassElement.h>
66 #include <Inventor/elements/SoGLUpdateAreaElement.h>
67 #include <Inventor/elements/SoGLViewingMatrixElement.h>
68 #include <Inventor/elements/SoGLViewportRegionElement.h>
69 #include <Inventor/elements/SoModelMatrixElement.h>
70 #include <Inventor/elements/SoViewVolumeElement.h>
71 #include <Inventor/nodes/SoCamera.h>
72 #include <cstdlib>
73
74 SO_NODE_ABSTRACT_SOURCE(SoCamera);
75
76 ////////////////////////////////////////////////////////////////////////
77 //
78 // Description:
79 // Constructor
80 //
81 // Use: protected
82
83 SoCamera::SoCamera()
84 //
85 ////////////////////////////////////////////////////////////////////////
86 {
87 SO_NODE_CONSTRUCTOR(SoCamera);
88
89 SO_NODE_ADD_FIELD(viewportMapping, (ADJUST_CAMERA));
90 SO_NODE_ADD_FIELD(position, (0.0, 0.0, 1.0));
91 SO_NODE_ADD_FIELD(orientation, (0.0, 0.0, 0.0, 1.0));
92 SO_NODE_ADD_FIELD(aspectRatio, (SO_ASPECT_SQUARE));
93 SO_NODE_ADD_FIELD(nearDistance, (1.0));
94 SO_NODE_ADD_FIELD(farDistance, (10.0));
95 SO_NODE_ADD_FIELD(focalDistance, (SoFocalDistanceElement::getDefault()));
96
97 // Set up static info for enumerated type field
98 SO_NODE_DEFINE_ENUM_VALUE(ViewportMapping, CROP_VIEWPORT_FILL_FRAME);
99 SO_NODE_DEFINE_ENUM_VALUE(ViewportMapping, CROP_VIEWPORT_LINE_FRAME);
100 SO_NODE_DEFINE_ENUM_VALUE(ViewportMapping, CROP_VIEWPORT_NO_FRAME);
101 SO_NODE_DEFINE_ENUM_VALUE(ViewportMapping, ADJUST_CAMERA);
102 SO_NODE_DEFINE_ENUM_VALUE(ViewportMapping, LEAVE_ALONE);
103
104 // Set up info in enumerated type field
105 SO_NODE_SET_SF_ENUM_TYPE(viewportMapping, ViewportMapping);
106
107 isBuiltIn = TRUE;
108 }
109
110 ////////////////////////////////////////////////////////////////////////
111 //
112 // Description:
113 // Destructor (necessary since inline destructor is too complex)
114 //
115 // Use: private
116
117 SoCamera::~SoCamera()
118 //
119 ////////////////////////////////////////////////////////////////////////
120 {
121 }
122
123 ////////////////////////////////////////////////////////////////////////
124 //
125 // Description:
126 // Sets the orientation of the camera so that it points toward the
127 // given target point while keeping the "up" direction of the
128 // camera parallel to the positive y-axis. If this is not possible,
129 // it uses the positive z-axis as "up".
130 //
131 // Use: public
132
133 void
134 SoCamera::pointAt(const SbVec3f &targetPoint)
135 //
136 ////////////////////////////////////////////////////////////////////////
137 {
138 SbVec3f d(targetPoint - position.getValue());
139
140 SbRotation yRot(SbVec3f(0,1,0), atan2(-d[0], -d[2]));
141 SbRotation xRot(SbVec3f(1,0,0), atan2(d[1], sqrtf(d[0]*d[0] + d[2]*d[2])));
142
143 orientation.setValue(xRot * yRot);
144 }
145
146 ////////////////////////////////////////////////////////////////////////
147 //
148 // Description:
149 // Sets the camera up to view the scene under the given node. The
150 // near and far clipping planes will be positioned 'slack' bounding
151 // sphere radii away from the bounding box's center. A value of 1.0
152 // will make the clipping planes the tightest around the bounding
153 // sphere.
154 //
155 // Use: public
156
157 void
158 SoCamera::viewAll(SoNode *root, const SbViewportRegion &vpRegion, float slack)
159 //
160 ////////////////////////////////////////////////////////////////////////
161 {
162 SoGetBoundingBoxAction action(vpRegion);
163
164 // Crop the viewport region if necessary
165 SbViewportRegion croppedReg = getViewportBounds(vpRegion);
166
167 // Find the bounding box of the scene, then adjust camera to view it
168 action.apply(root);
169 viewBoundingBox(action.getBoundingBox(),
170 croppedReg.getViewportAspectRatio(), slack);
171 }
172
173 ////////////////////////////////////////////////////////////////////////
174 //
175 // Description:
176 // Sets the camera up to view the scene defined by the given path.
177 // The near and far clipping planes will be positioned 'slack'
178 // bounding sphere radii away from the bounding box's center. A
179 // value of 1.0 will make the clipping planes the tightest around
180 // the bounding sphere.
181 //
182 // Use: public
183
184 void
185 SoCamera::viewAll(SoPath *path, const SbViewportRegion &vpRegion, float slack)
186 //
187 ////////////////////////////////////////////////////////////////////////
188 {
189 SoGetBoundingBoxAction action(vpRegion);
190
191 // Crop the viewport region if necessary
192 SbViewportRegion croppedReg = getViewportBounds(vpRegion);
193
194 // Find the bounding box of the scene, then adjust camera to view it
195 action.apply(path);
196 viewBoundingBox(action.getBoundingBox(),
197 croppedReg.getViewportAspectRatio(), slack);
198 }
199
200 ////////////////////////////////////////////////////////////////////////
201 //
202 // Description:
203 // Returns the viewport region this camera would use to render into
204 // a given viewport region, accounting for cropping.
205 //
206 // Use: public
207
208 SbViewportRegion
209 SoCamera::getViewportBounds(const SbViewportRegion &region) const
210 //
211 ////////////////////////////////////////////////////////////////////////
212 {
213 float vpAspect, camAspect;
214
215 // Start with the same region as we are given
216 SbViewportRegion croppedRegion = region;
217
218 switch (viewportMapping.getValue()) {
219
220 case CROP_VIEWPORT_FILL_FRAME:
221 case CROP_VIEWPORT_LINE_FRAME:
222 case CROP_VIEWPORT_NO_FRAME:
223
224 vpAspect = region.getViewportAspectRatio();
225 camAspect = aspectRatio.getValue();
226
227 // Make a smaller viewport that
228 // [a] fits within the old viewport and
229 // [b] uses the camera's aspect ratio
230
231 if (camAspect > vpAspect)
232 croppedRegion.scaleHeight(vpAspect / camAspect);
233
234 else if (camAspect < vpAspect)
235 croppedRegion.scaleWidth(camAspect / vpAspect);
236
237 break;
238
239 case ADJUST_CAMERA:
240 case LEAVE_ALONE:
241 // Neither of these adjusts the viewport size.
242 break;
243 }
244
245 return croppedRegion;
246 }
247
248 ////////////////////////////////////////////////////////////////////////
249 //
250 // Description:
251 // Performs most actions that SoCamera supports.
252 //
253 // Use: extender
254
255 void
256 SoCamera::doAction(SoAction *action)
257 //
258 ////////////////////////////////////////////////////////////////////////
259 {
260 SbViewVolume viewVol;
261 SbBool changeRegion;
262
263 // Get the current viewport region
264 const SbViewportRegion &vpReg =
265 SoViewportRegionElement::get(action->getState());
266
267 // Compute the view volume
268 SoCamera::computeView(vpReg, viewVol, changeRegion);
269
270 // Set the state
271 SbVec3f dummyJitter;
272 setElements(action, viewVol, changeRegion,
273 changeRegion ? getViewportBounds(vpReg) : vpReg,
274 FALSE, dummyJitter);
275 }
276
277 ////////////////////////////////////////////////////////////////////////
278 //
279 // Description:
280 // Does the callback action.
281 //
282 // Use: extender
283
284 void
285 SoCamera::callback(SoCallbackAction *action)
286 //
287 ////////////////////////////////////////////////////////////////////////
288 {
289 SoCamera::doAction(action);
290 }
291
292 ////////////////////////////////////////////////////////////////////////
293 //
294 // Description:
295 // Performs GL rendering on a camera node.
296 //
297 // Use: extender
298
299 void
300 SoCamera::GLRender(SoGLRenderAction *action)
301 //
302 ////////////////////////////////////////////////////////////////////////
303 {
304 SbViewportRegion croppedReg;
305 SbViewVolume viewVol;
306 SbMatrix viewMat, projMat;
307 SbVec2f uaOrigin, uaSize;
308 SbVec3f jitterAmount;
309 SbBool changeRegion;
310 SoState *state = action->getState();
311
312 // Get the current viewport region
313 const SbViewportRegion &vpReg = SoGLViewportRegionElement::get(state);
314
315 // Compute the view volume
316 SoCamera::computeView(vpReg, viewVol, changeRegion);
317
318 // Draw frame, if necessary, using current (full) viewport
319 if (changeRegion) {
320 croppedReg = getViewportBounds(vpReg);
321 drawFrame(action, vpReg, croppedReg);
322 }
323
324 // Jitter the camera for anti-aliasing if doing multiple passes
325 if (action->getNumPasses() > 1)
326 jitter(action->getNumPasses(), SoGLRenderPassElement::get(state),
327 changeRegion ? croppedReg : vpReg, jitterAmount);
328
329 // Set the state
330 setElements(action, viewVol, changeRegion, croppedReg,
331 action->getNumPasses() > 1, jitterAmount);
332
333 // Compute and set culling volume if different from view volume
334 if (! SoGLUpdateAreaElement::get(state, uaOrigin, uaSize)) {
335 SbViewVolume cvv = viewVol.narrow(uaOrigin[0], uaOrigin[1],
336 uaSize[0], uaSize[1]);
337 SoModelMatrixElement::setCullMatrix(state, this, cvv.getMatrix());
338 }
339 // Otherwise, just set culling volume to be same as view volume
340 else
341 SoModelMatrixElement::setCullMatrix(state, this, viewVol.getMatrix());
342
343 // Don't auto-cache above cameras:
344 SoGLCacheContextElement::shouldAutoCache(state,
345 SoGLCacheContextElement::DONT_AUTO_CACHE);
346 }
347
348 ////////////////////////////////////////////////////////////////////////
349 //
350 // Description:
351 // Returns amount to jitter camera in normalized device coordinates
352 // (after projection) for anti-aliasing during multi-pass rendering.
353 //
354 // Use: protected, virtual
355
356 void
357 SoCamera::jitter(int numPasses, int curPass,
358 const SbViewportRegion &vpReg, SbVec3f &jitterAmount) const
359 //
360 ////////////////////////////////////////////////////////////////////////
361 {
362 // Get the current sample point within the pixel
363 SbVec2f samplePoint;
364 getJitterSample(numPasses, curPass, samplePoint);
365
366 // Compute the jitter amount for the projection matrix. This
367 // jitter will be in post-perspective space, which goes from -1 to
368 // +1 in x and y. In this space, the size of a pixel is 2/width by
369 // 2/height.
370 const SbVec2s &vpSize = vpReg.getViewportSizePixels();
371 jitterAmount[0] = samplePoint[0] * 2.0 / (float) vpSize[0];
372 jitterAmount[1] = samplePoint[1] * 2.0 / (float) vpSize[1];
373 jitterAmount[2] = 0.0;
374 }
375
376 ////////////////////////////////////////////////////////////////////////
377 //
378 // Description:
379 // Returns a 2D sample point within a pixel.
380 //
381 // Use: private, static
382
383 void
384 SoCamera::getJitterSample(int numPasses, int curPass, SbVec2f &samplePoint)
385 //
386 ////////////////////////////////////////////////////////////////////////
387 {
388 // These arrays define filter kernels to be used when the total
389 // number of passes is small. For N passes, use the smallest
390 // kernel that is >= N.
391 static const float kernel2[2][2] = {{ 0.246490, 0.249999 },
392 {-0.246490, -0.249999 }},
393 kernel3[3][2] = {{-0.373411, -0.250550 },
394 { 0.256263, 0.368119 },
395 { 0.117148, -0.117570 }},
396 kernel4[4][2] = {{-0.208147, 0.353730 },
397 { 0.203849, -0.353780 },
398 {-0.292626, -0.149945 },
399 { 0.296924, 0.149994 }},
400 kernel8[8][2] = {{-0.334818, 0.435331 },
401 { 0.286438, -0.393495 },
402 { 0.459462, 0.141540 },
403 {-0.414498, -0.192829 },
404 {-0.183790, 0.082102 },
405 {-0.079263, -0.317383 },
406 { 0.102254, 0.299133 },
407 { 0.164216, -0.054399 }},
408 kernel15[15][2] = {{ 0.285561, 0.188437 },
409 { 0.360176, -0.065688 },
410 {-0.111751, 0.275019 },
411 {-0.055918, -0.215197 },
412 {-0.080231, -0.470965 },
413 { 0.138721, 0.409168 },
414 { 0.384120, 0.458500 },
415 {-0.454968, 0.134088 },
416 { 0.179271, -0.331196 },
417 {-0.307049, -0.364927 },
418 { 0.105354, -0.010099 },
419 {-0.154180, 0.021794 },
420 {-0.370135, -0.116425 },
421 { 0.451636, -0.300013 },
422 {-0.370610, 0.387504 }},
423 kernel24[24][2] = {{ 0.030245, 0.136384 },
424 { 0.018865, -0.348867 },
425 {-0.350114, -0.472309 },
426 { 0.222181, 0.149524 },
427 {-0.393670, -0.266873 },
428 { 0.404568, 0.230436 },
429 { 0.098381, 0.465337 },
430 { 0.462671, 0.442116 },
431 { 0.400373, -0.212720 },
432 {-0.409988, 0.263345 },
433 {-0.115878, -0.001981 },
434 { 0.348425, -0.009237 },
435 {-0.464016, 0.066467 },
436 {-0.138674, -0.468006 },
437 { 0.144932, -0.022780 },
438 {-0.250195, 0.150161 },
439 {-0.181400, -0.264219 },
440 { 0.196097, -0.234139 },
441 {-0.311082, -0.078815 },
442 { 0.268379, 0.366778 },
443 {-0.040601, 0.327109 },
444 {-0.234392, 0.354659 },
445 {-0.003102, -0.154402 },
446 { 0.297997, -0.417965 }},
447 kernel66[66][2] = {{ 0.266377, -0.218171 },
448 {-0.170919, -0.429368 },
449 { 0.047356, -0.387135 },
450 {-0.430063, 0.363413 },
451 {-0.221638, -0.313768 },
452 { 0.124758, -0.197109 },
453 {-0.400021, 0.482195 },
454 { 0.247882, 0.152010 },
455 {-0.286709, -0.470214 },
456 {-0.426790, 0.004977 },
457 {-0.361249, -0.104549 },
458 {-0.040643, 0.123453 },
459 {-0.189296, 0.438963 },
460 {-0.453521, -0.299889 },
461 { 0.408216, -0.457699 },
462 { 0.328973, -0.101914 },
463 {-0.055540, -0.477952 },
464 { 0.194421, 0.453510 },
465 { 0.404051, 0.224974 },
466 { 0.310136, 0.419700 },
467 {-0.021743, 0.403898 },
468 {-0.466210, 0.248839 },
469 { 0.341369, 0.081490 },
470 { 0.124156, -0.016859 },
471 {-0.461321, -0.176661 },
472 { 0.013210, 0.234401 },
473 { 0.174258, -0.311854 },
474 { 0.294061, 0.263364 },
475 {-0.114836, 0.328189 },
476 { 0.041206, -0.106205 },
477 { 0.079227, 0.345021 },
478 {-0.109319, -0.242380 },
479 { 0.425005, -0.332397 },
480 { 0.009146, 0.015098 },
481 {-0.339084, -0.355707 },
482 {-0.224596, -0.189548 },
483 { 0.083475, 0.117028 },
484 { 0.295962, -0.334699 },
485 { 0.452998, 0.025397 },
486 { 0.206511, -0.104668 },
487 { 0.447544, -0.096004 },
488 {-0.108006, -0.002471 },
489 {-0.380810, 0.130036 },
490 {-0.242440, 0.186934 },
491 {-0.200363, 0.070863 },
492 {-0.344844, -0.230814 },
493 { 0.408660, 0.345826 },
494 {-0.233016, 0.305203 },
495 { 0.158475, -0.430762 },
496 { 0.486972, 0.139163 },
497 {-0.301610, 0.009319 },
498 { 0.282245, -0.458671 },
499 { 0.482046, 0.443890 },
500 {-0.121527, 0.210223 },
501 {-0.477606, -0.424878 },
502 {-0.083941, -0.121440 },
503 {-0.345773, 0.253779 },
504 { 0.234646, 0.034549 },
505 { 0.394102, -0.210901 },
506 {-0.312571, 0.397656 },
507 { 0.200906, 0.333293 },
508 { 0.018703, -0.261792 },
509 {-0.209349, -0.065383 },
510 { 0.076248, 0.478538 },
511 {-0.073036, -0.355064 },
512 { 0.145087, 0.221726 }};
513
514 // Determine which kernel to use and access the correct jitter amount
515 if (numPasses == 2)
516 samplePoint = kernel2[curPass];
517 else if (numPasses == 3)
518 samplePoint = kernel3[curPass];
519 else if (numPasses == 4)
520 samplePoint = kernel4[curPass];
521 else if (numPasses <= 8)
522 samplePoint = kernel8[curPass];
523 else if (numPasses <= 15)
524 samplePoint = kernel15[curPass];
525 else if (numPasses <= 24)
526 samplePoint = kernel24[curPass];
527 else if (numPasses <= 66)
528 samplePoint = kernel66[curPass];
529
530 // If we don't have enough samples in the largest kernel, we'll
531 // allocate an array and fill it with random samples to cover the
532 // ones not in the kernel
533 else {
534 static float * extraSamples = NULL;
535 static int numExtraSamples = 0;
536
537 // Do the allocation (if necessary) only on the first pass, to
538 // avoid unnecessary tests
539 if (curPass == 1) {
540
541 int numExtraNeeded = numPasses - 66;
542
543 if (numExtraSamples < numExtraNeeded) {
544 if (extraSamples != NULL)
545 delete extraSamples;
546 extraSamples = new float [numExtraNeeded * 2];
547
548 // Set 2 coords of sample to random number between -1 and +1.
549 for (int i = 0; i < 2 * numExtraNeeded; i++)
550 extraSamples[i] = 2.0 * drand48() - 1.0;
551 }
552 }
553
554 if (curPass < 66)
555 samplePoint = kernel66[curPass];
556 else
557 samplePoint.setValue(extraSamples[(curPass - 66) * 2 + 0],
558 extraSamples[(curPass - 66) * 2 + 1]);
559
560 }
561 }
562
563 ////////////////////////////////////////////////////////////////////////
564 //
565 // Description:
566 // Does the getBoundingBox action. If the isCameraSpace() flag is
567 // set in the action, this also transforms the model matrix to set
568 // up camera space.
569 //
570 // Use: extender
571
572 void
573 SoCamera::getBoundingBox(SoGetBoundingBoxAction *action)
574 //
575 ////////////////////////////////////////////////////////////////////////
576 {
577 // Do the usual stuff first
578 SoCamera::doAction(action);
579 }
580
581 ////////////////////////////////////////////////////////////////////////
582 //
583 // Description:
584 // Sets up viewing info for event handling.
585 //
586 // Use: extender
587
588 void
589 SoCamera::handleEvent(SoHandleEventAction *action)
590 //
591 ////////////////////////////////////////////////////////////////////////
592 {
593 // Set only a subset of the elements for this action
594
595 SbViewVolume viewVol;
596 SbBool changeRegion;
597 SoState *state = action->getState();
598
599 // Get the current viewport region
600 const SbViewportRegion &vpReg = SoViewportRegionElement::get(state);
601
602 // Compute the view volume
603 SoCamera::computeView(vpReg, viewVol, changeRegion);
604
605 // Set necessary elements in state
606 SoViewVolumeElement::set(state, this, viewVol);
607 if (changeRegion)
608 SoViewportRegionElement::set(state, getViewportBounds(vpReg));
609 }
610
611 ////////////////////////////////////////////////////////////////////////
612 //
613 // Description:
614 // Sets up viewing info for ray picking.
615 //
616 // Use: extender
617
618 void
619 SoCamera::rayPick(SoRayPickAction *action)
620 //
621 ////////////////////////////////////////////////////////////////////////
622 {
623 SoCamera::doAction(action);
624
625 // Tell the action how to set up world space view
626 action->computeWorldSpaceRay();
627 }
628
629 ////////////////////////////////////////////////////////////////////////
630 //
631 // Description:
632 // Computes the view volume the camera represents, based on the
633 // given viewport region. Returns TRUE in changeRegion if the
634 // viewport region needs to be adjusted based on the current
635 // viewport mapping, meaning that it has to be changed in the
636 // state.
637 //
638 // Use: private
639
640 void
641 SoCamera::computeView(const SbViewportRegion &vpReg,
642 SbViewVolume &viewVol, SbBool &changeRegion)
643 //
644 ////////////////////////////////////////////////////////////////////////
645 {
646 float camAspect, vpAspect;
647
648 changeRegion = FALSE;
649
650 camAspect = aspectRatio.getValue();
651
652 switch (viewportMapping.getValue()) {
653 case CROP_VIEWPORT_FILL_FRAME:
654 case CROP_VIEWPORT_LINE_FRAME:
655 case CROP_VIEWPORT_NO_FRAME:
656 changeRegion = TRUE;
657 viewVol = getViewVolume(camAspect);
658 break;
659
660 case ADJUST_CAMERA:
661 vpAspect = vpReg.getViewportAspectRatio();
662 viewVol = getViewVolume(vpAspect);
663 // Adjust size of view volume if it will fill viewport better
664 if (vpAspect < 1.0)
665 viewVol.scale(1.0 / vpAspect);
666 break;
667
668 case LEAVE_ALONE:
669 // Do nothing. The window specified by the camera will be
670 // stretched to fit into the current viewport.
671 viewVol = getViewVolume(camAspect);
672 break;
673 }
674 }
675
676 ////////////////////////////////////////////////////////////////////////
677 //
678 // Description:
679 // Sets relevant info in elements in state.
680 //
681 // Use: private
682
683 void
684 SoCamera::setElements(SoAction *action, SbViewVolume &viewVol,
685 SbBool setRegion, const SbViewportRegion &vpReg,
686 SbBool doJitter, const SbVec3f &jitterAmount)
687 //
688 ////////////////////////////////////////////////////////////////////////
689 {
690 SoState *state = action->getState();
691 SbMatrix viewMat, projMat;
692
693 // Compute viewing and projection matrices
694 viewVol.getMatrices(viewMat, projMat);
695
696 // Jitter if necessary
697 if (doJitter) {
698 SbMatrix m;
699 m.setTranslate(jitterAmount);
700 projMat.multRight(m);
701 }
702
703 // Set necessary elements in state
704 SoProjectionMatrixElement::set(state, this, projMat);
705
706 // Set the viewport region only if it has changed
707 if (setRegion)
708 SoViewportRegionElement::set(state, vpReg);
709
710 // If the current model matrix is not identity, the camera has to
711 // be transformed by it. This is equivalent to transforming the
712 // world by the inverse of the model matrix. So we set the viewing
713 // matrix to be the product of the inverse of the model matrix and
714 // the viewMat.
715 SbBool modelIsIdent;
716 const SbMatrix &modelMat = SoModelMatrixElement::get(state, modelIsIdent);
717 if (! modelIsIdent) {
718 viewMat.multRight(modelMat.inverse());
719
720 // Also, transform the view volume by the model matrix
721 viewVol.transform(modelMat);
722 }
723
724 SoViewVolumeElement::set(state, this, viewVol);
725 SoViewingMatrixElement::set(state, this, viewMat);
726 }
727
728 ////////////////////////////////////////////////////////////////////////
729 //
730 // Description:
731 // Draws cropping frame for use when rendering.
732 //
733 // Use: private
734
735 void
736 SoCamera::drawFrame(SoGLRenderAction *action,
737 const SbViewportRegion &vpReg,
738 const SbViewportRegion &croppedReg)
739 //
740 ////////////////////////////////////////////////////////////////////////
741 {
742 // Frame color as RGB. In color index mode, just use the background
743 // color, since we have no idea what's in the color map.
744 #define GREY1 .38, .38, .38
745 #define GREY1_INDEX 0
746
747 // Get origin and size of viewports in pixels
748 const SbVec2s & vpOrig = vpReg.getViewportOriginPixels();
749 const SbVec2s & vpSize = vpReg.getViewportSizePixels();
750 const SbVec2s & cropLL = croppedReg.getViewportOriginPixels();
751 const SbVec2s & cropUR = croppedReg.getViewportSizePixels() + cropLL;
752
753 // Save stuff we're going to change explicitly
754 glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT |
755 GL_POLYGON_BIT | GL_LINE_BIT);
756
757 glDisable(GL_LIGHTING);
758 if (SoGLLazyElement::isColorIndex(action->getState()))
759 glIndexi(GREY1_INDEX);
760 else
761 glColor3f(GREY1);
762
763 // OK to blow away the old projection matrix, because it will be
764 // changed anyway by the camera's render action right after this...
765 glMatrixMode(GL_PROJECTION);
766 glLoadIdentity();
767 glOrtho(0.0, vpSize[0] - 1.0, 0.0, vpSize[1] - 1.0, -1.0, 1.0);
768
769 // Ditto for the viewing matrix
770 glMatrixMode(GL_MODELVIEW);
771 glPushMatrix();
772 glLoadIdentity();
773
774 if (viewportMapping.getValue() == CROP_VIEWPORT_LINE_FRAME) {
775 // Draw a rectangle one pixel bigger than the camera's viewport
776 glPolygonMode(GL_FRONT, GL_LINE);
777 glLineWidth(1.0);
778 glDisable(GL_LINE_STIPPLE);
779 glRects(cropLL[0] - 1 - vpOrig[0], cropLL[1] - 1 - vpOrig[1],
780 cropUR[0] + 1 - vpOrig[0], cropUR[1] + 1 - vpOrig[1]);
781 }
782 else if (viewportMapping.getValue() == CROP_VIEWPORT_FILL_FRAME) {
783 glPolygonMode(GL_FRONT, GL_FILL);
784 glDisable(GL_POLYGON_STIPPLE);
785
786 if (croppedReg.getViewportAspectRatio() >
787 vpReg.getViewportAspectRatio()) {
788
789 // Draw filled rectangles for a border
790 glRects(0, 0,
791 vpSize[0] - 1, cropLL[1] - vpOrig[1] - 1);
792 glRects(0, cropUR[1] - vpOrig[1] + 1 ,
793 vpSize[0] - 1, vpSize[1] - 1);
794 }
795 else {
796 glRects(0, 0,
797 cropLL[0] - vpOrig[0] - 1, vpSize[1] - 1);
798 glRects(cropUR[0] - vpOrig[0] + 1, 0,
799 vpSize[0] - 1, vpSize[1] - 1);
800 }
801 }
802
803 glPopMatrix();
804 glPopAttrib();
805
806 #undef GREY1
807 #undef GREY1_INDEX
808 }