Debian Codegraph @

lib/database/src/so/elements/SoGLLazyElement.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.3 $
44 |
45 | Classes:
46 | SoGLLazyElement
47 |
48 | Author(s) : Alan Norton, Gavin Bell
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/SoGLRenderAction.h>
56 #include <Inventor/elements/SoGLLazyElement.h>
57 #include <Inventor/errors/SoDebugError.h>
58 #include <Inventor/misc/SoState.h>
59 #include <Inventor/elements/SoSubElement.h>
60 #include <Inventor/elements/SoCacheElement.h>
61 #include <Inventor/caches/SoGLRenderCache.h>
62 #include <Inventor/elements/SoGLCacheContextElement.h>
63 #include <Inventor/nodes/SoPackedColor.h>
64 #include <Inventor/fields/SoMFColor.h>
65 #include <Inventor/fields/SoMFFloat.h>
66 #include <SoDebug.h>
67 #include <machine.h>
68
69 SO_ELEMENT_SOURCE(SoGLLazyElement);
70
71 ////////////////////////////////////////////////////////////////////////
72 //
73 // Description:
74 // Destructor.
75 //
76 // Use: SoINTERNAL public
77 ////////////////////////////////////////////////////////////////////////
78 SoGLLazyElement::~SoGLLazyElement()
79 {
80 }
81
82 ////////////////////////////////////////////////////////////////////////
83 //
84 // Description:
85 // Initializes element.
86 //
87 // Use: public
88 ////////////////////////////////////////////////////////////////////////
89 void
90 SoGLLazyElement::init(SoState *state)
91 {
92 SoLazyElement::init(state);
93 // We begin with GL invalid
94 // so it gets reset at first send.
95
96 glState.GLAmbient[0] = -1;
97 glState.GLEmissive[0] = -1;
98 glState.GLSpecular[0] = -1;
99
100 glState.GLAmbient[1] = glState.GLAmbient[2] = 0;
101 glState.GLEmissive[1] = glState.GLEmissive[2] = 0;
102 glState.GLSpecular[1] = glState.GLSpecular[2] = 0;
103
104 glState.GLShininess = -1;
105 glState.GLColorMaterial = -1;
106 glState.GLblending = -1;
107
108 glState.GLDiffuseNodeId = 0;
109 glState.GLTranspNodeId = 0;
110 //start with stipple undefined transparency
111 glState.GLStippleNum = -1;
112
113 // set the last fields to 1., they will stay that way:
114 glState.GLAmbient[3] = 1.0;
115 glState.GLEmissive[3] = 1.0;
116 glState.GLSpecular[3] = 1.0;
117
118 // and lightModel invalid
119 glState.GLLightModel = -1;
120
121 // Also, begin with GL and Inventor out of synch:
122 invalidBits = ALL_MASK;
123 // and nothing sent to GL
124 GLSendBits = 0;
125
126 // Determine if GL is in color index mode
127 GLboolean b;
128 glGetBooleanv(GL_RGBA_MODE, &b);
129 colorIndex = ! b;
130 }
131
132 ////////////////////////////////////////////////////////////////////////
133 //
134 // Description:
135 // Pushes element, copies GL state as needed:
136 //
137 // use: SoINTERNAL public
138 ////////////////////////////////////////////////////////////////////////
139 void
140 SoGLLazyElement::push(SoState *)
141 {
142 SoGLLazyElement *prevElt = (SoGLLazyElement*)getNextInStack();
143
144 // The push always happens before a true set()
145
146 ivState = prevElt->ivState;
147 glState = prevElt->glState;
148
149 colorIndex = prevElt->colorIndex;
150 invalidBits = prevElt->invalidBits;
151 GLSendBits = 0;
152 }
153
154
155 ////////////////////////////////////////////////////////////////////////
156 //
157 // Description:
158 // Pops element, causing side effects in GL.
159 //
160 //
161 // Use: SoINTERNAL public
162 ////////////////////////////////////////////////////////////////////////
163 void
164 SoGLLazyElement::pop(SoState *, const SoElement *prevTopElement)
165 {
166 //
167 //Copy all GL parts back from previous top element.
168 //Mark those that changed with invalidBits.
169
170 SoGLLazyElement* prevTop = (SoGLLazyElement*)prevTopElement;
171 uint32_t sendBits = prevTop->GLSendBits;
172
173 //merge GLSendBits into parent element
174 GLSendBits |= sendBits;
175
176 //mark corresponding parts as invalid:
177 invalidBits |= sendBits;
178
179 //copy GL state:
180 glState = prevTop->glState;
181
182
183 }
184
185 /////////////////////////////////////////////////////////////////////////
186 //
187 // Description: set Diffuse color in element
188 // virtual method for GL rendering
189 //
190 // Use: private, virtual
191 //
192 ////////////////////////////////////////////////////////////////////////
193 void
194 SoGLLazyElement::setDiffuseElt(SoNode *node,
195 int32_t numColors, const SbColor* colors, SoColorPacker *cPacker)
196 {
197 if (colorIndex) return;
198 ivState.diffuseColors = colors;
199 ivState.numDiffuseColors = numColors;
200 ivState.diffuseNodeId = (node->getNodeId());
201 ivState.packed = FALSE;
202 ivState.packedTransparent = FALSE;
203
204 //Pack the colors and transparencies, if necessary:
205 if(!cPacker->diffuseMatch(ivState.diffuseNodeId) ||
206 (!cPacker->transpMatch(ivState.transpNodeId)))
207 packColors(cPacker);
208
209 ivState.packedColors = cPacker->getPackedColors();
210
211 // for open caches, record the fact that set was called:
212 ivState.cacheLevelSetBits |= DIFFUSE_MASK;
213
214 if (ivState.diffuseNodeId != glState.GLDiffuseNodeId)
215 invalidBits |= DIFFUSE_MASK;
216
217 }
218
219 /////////////////////////////////////////////////////////////////////////
220 //
221 // Description: set transparency type
222 // virtual method for GL rendering
223 //
224 // Use: private, virtual
225 //
226 ////////////////////////////////////////////////////////////////////////
227 void
228 SoGLLazyElement::setTranspTypeElt(int32_t type)
229 {
230 if(ivState.transpType != type){
231 ivState.transpType = type;
232 // make sure transparencies send:
233 invalidBits |= TRANSPARENCY_MASK;
234 glState.GLStippleNum = -1;
235 }
236 }
237
238 /////////////////////////////////////////////////////////////////////////
239 //
240 // Description: set transparency in element
241 // virtual method for GL rendering
242 //
243 // Use: private, virtual
244 //
245 ////////////////////////////////////////////////////////////////////////
246 void
247 SoGLLazyElement::setTranspElt(SoNode *node,
248 int32_t numTrans, const float *trans, SoColorPacker *cPacker)
249 {
250 ivState.numTransparencies = numTrans;
251 ivState.transparencies = trans;
252
253 // determine the appropriate stippleNum:
254 // set to zero unless have nonzero stipple transparency.
255
256 ivState.stippleNum = 0;
257 if (trans[0] > 0.0) {
258 if (ivState.transpType == SoGLRenderAction::SCREEN_DOOR){
259 ivState.stippleNum =
260 (int)(trans[0]*getNumPatterns());
261 }
262 }
263 if (numTrans == 1 && trans[0] == 0) ivState.transpNodeId = 0;
264 else ivState.transpNodeId = node->getNodeId();
265 ivState.packed = FALSE;
266 ivState.packedTransparent = FALSE;
267 if(!cPacker->diffuseMatch(ivState.diffuseNodeId) ||
268 (!cPacker->transpMatch(ivState.transpNodeId)))
269 packColors(cPacker);
270
271 ivState.packedColors = cPacker->getPackedColors();
272
273 // for open caches, record the fact that set was called:
274 ivState.cacheLevelSetBits |= (TRANSPARENCY_MASK|DIFFUSE_MASK);
275
276 if (ivState.transpNodeId != glState.GLTranspNodeId)
277 invalidBits |= DIFFUSE_MASK;
278
279 if (ivState.stippleNum != glState.GLStippleNum)
280 invalidBits |= TRANSPARENCY_MASK;
281 else invalidBits &= ~TRANSPARENCY_MASK;
282
283 }
284 /////////////////////////////////////////////////////////////////////////
285 //
286 // Description: set color indices in element
287 // virtual method for GL rendering
288 //
289 // Use: private, virtual
290 //
291 ////////////////////////////////////////////////////////////////////////
292 void
293 SoGLLazyElement::setColorIndexElt(SoNode *node,
294 int32_t numIndices, const int32_t *indices)
295 {
296 if (!colorIndex) return;
297 ivState.numDiffuseColors = numIndices;
298 ivState.colorIndices = indices;
299
300 ivState.diffuseNodeId = (node->getNodeId());
301 ivState.packed = FALSE;
302 ivState.packedTransparent = FALSE;
303
304 // for open caches, record the fact that set was called:
305 ivState.cacheLevelSetBits |= DIFFUSE_MASK;
306
307 if (ivState.diffuseNodeId != glState.GLDiffuseNodeId)
308 invalidBits |= DIFFUSE_MASK;
309 else invalidBits &= ~DIFFUSE_MASK;
310
311 }
312
313 /////////////////////////////////////////////////////////////////////////
314 //
315 // Description: set Packed diffuse color in element
316 // sets both diffuse color and transparency.
317 // virtual method for GL rendering
318 //
319 // Use: private, virtual
320 //
321 ////////////////////////////////////////////////////////////////////////
322 void
323 SoGLLazyElement::setPackedElt(SoNode *node, int32_t numColors,
324 const uint32_t* colors)
325 {
326 if (colorIndex) return;
327 ivState.packedColors = colors;
328 ivState.numDiffuseColors = numColors;
329 ivState.numTransparencies = numColors;
330 ivState.diffuseNodeId = node->getNodeId();
331 ivState.transpNodeId = node->getNodeId();
332
333 ivState.stippleNum = 0;
334 if ((colors[0]&0xff) != 0xff){
335 if(ivState.transpType == SoGLRenderAction::SCREEN_DOOR){
336 ivState.stippleNum = (int)(getNumPatterns()*
337 (1.-(colors[0] & 0xff)*(1./255.)));
338 }
339 }
340
341 ivState.packed = TRUE;
342 ivState.packedTransparent = ((SoPackedColor*) node)->isTransparent();
343
344 // For open caches, record the fact that set was called:
345 ivState.cacheLevelSetBits |= (DIFFUSE_MASK|TRANSPARENCY_MASK);
346
347 if (invalidBits & (DIFFUSE_MASK|TRANSPARENCY_MASK)) return;
348
349 if (ivState.diffuseNodeId != glState.GLDiffuseNodeId ||
350 ivState.transpNodeId != glState.GLTranspNodeId)
351 invalidBits |= DIFFUSE_MASK;
352 else invalidBits &= ~DIFFUSE_MASK;
353
354 if (ivState.stippleNum != glState.GLStippleNum)
355 invalidBits |= TRANSPARENCY_MASK;
356 else invalidBits &= ~TRANSPARENCY_MASK;
357
358 }
359
360 //////////////////////////////////////////////////////////////////////////
361 //
362 // Description: set Ambient color in element
363 // virtual method for GL rendering
364 //
365 // Use: private, virtual
366 //
367 /////////////////////////////////////////////////////////////////////////
368 void
369 SoGLLazyElement::setAmbientElt(const SbColor* color)
370 {
371 ivState.ambientColor.setValue((float*)color);
372
373 // For open caches, record the fact that set was called:
374 ivState.cacheLevelSetBits |= AMBIENT_MASK;
375
376
377 for(int i=0; i<3; i++){
378 if (ivState.ambientColor[i] != glState.GLAmbient[i]){
379 invalidBits |= AMBIENT_MASK;
380 return;
381 }
382 }
383 invalidBits &= ~AMBIENT_MASK;
384 }
385
386 /////////////////////////////////////////////////////////////////////////
387 //
388 // Description: set emissive color in element
389 // virtual method for GL rendering
390 //
391 // Use: private, virtual
392 //
393 /////////////////////////////////////////////////////////////////////////
394 void
395 SoGLLazyElement::setEmissiveElt(const SbColor* color)
396 {
397 ivState.emissiveColor.setValue((float*)color);
398
399 // For open caches, record the fact that set was called:
400 ivState.cacheLevelSetBits |= EMISSIVE_MASK;
401
402 for(int i=0; i<3; i++){
403 if (ivState.emissiveColor[i] != glState.GLEmissive[i]){
404 invalidBits |= EMISSIVE_MASK;
405 return;
406 }
407 }
408 invalidBits &= ~EMISSIVE_MASK;
409 }
410 /////////////////////////////////////////////////////////////////////////
411 //
412 // Description: set specular color in element
413 // virtual method for GL rendering
414 //
415 // Use: private, virtual
416 //
417 /////////////////////////////////////////////////////////////////////////
418 void
419 SoGLLazyElement::setSpecularElt(const SbColor* color)
420 {
421 ivState.specularColor.setValue((float*)color);
422
423 // For open caches, record the fact that set was called:
424 ivState.cacheLevelSetBits |= SPECULAR_MASK;
425
426 for(int i=0; i<3; i++){
427 if (ivState.specularColor[i] != glState.GLSpecular[i]){
428 invalidBits |= SPECULAR_MASK;
429 return;
430 }
431 }
432 invalidBits &= ~SPECULAR_MASK;
433 }
434
435 /////////////////////////////////////////////////////////////////////////
436 //
437 // Description: set shininess in element
438 // virtual method for GL rendering
439 //
440 // Use: private, virtual
441 //
442 /////////////////////////////////////////////////////////////////////////
443 void
444 SoGLLazyElement::setShininessElt(float value)
445 {
446 ivState.shininess = value;
447
448 // For open caches, record the fact that set was called:
449 ivState.cacheLevelSetBits |= SHININESS_MASK;
450
451 // set invalid bit based on value
452 if (fabsf(ivState.shininess - glState.GLShininess) > SO_LAZY_SHINY_THRESHOLD)
453 invalidBits |= SHININESS_MASK;
454 else invalidBits &= ~SHININESS_MASK;
455 return;
456 }
457 /////////////////////////////////////////////////////////////////////////
458 //
459 // Description: set colorMaterial in element
460 // virtual method for GL rendering
461 //
462 // Use: private, virtual
463 //
464 /////////////////////////////////////////////////////////////////////////
465 void
466 SoGLLazyElement::setColorMaterialElt(SbBool value)
467 {
468 // don't turn on colorMaterial if lighting off:
469 if (ivState.lightModel == BASE_COLOR) value = FALSE;
470 ivState.colorMaterial = value;
471
472 // For open caches, record the fact that set was called:
473 ivState.cacheLevelSetBits |= COLOR_MATERIAL_MASK;
474
475
476 // set invalid bit based on value
477 if (ivState.colorMaterial != glState.GLColorMaterial)
478 invalidBits |= COLOR_MATERIAL_MASK;
479 else invalidBits &= ~COLOR_MATERIAL_MASK;
480 return;
481 }
482 /////////////////////////////////////////////////////////////////////////
483 //
484 // Description: set lightModel in element
485 // virtual method for GL rendering
486 //
487 // Use: private, virtual
488 //
489 /////////////////////////////////////////////////////////////////////////
490 void
491 SoGLLazyElement::setLightModelElt(SoState *state, int32_t value)
492 {
493 ivState.lightModel = value;
494
495 // For Open caches, record the fact that set was called:
496 ivState.cacheLevelSetBits |= LIGHT_MODEL_MASK;
497
498 // also set the shapestyle version of this:
499 SoShapeStyleElement::setLightModel(state, value);
500
501 // set invalid bit based on value
502 if (ivState.lightModel != glState.GLLightModel)
503 invalidBits |= LIGHT_MODEL_MASK;
504 else invalidBits &= ~LIGHT_MODEL_MASK;
505 // set Color Material off if necessary:
506 if (ivState.lightModel == BASE_COLOR)
507 setColorMaterialElt(FALSE);
508 return;
509 }
510 /////////////////////////////////////////////////////////////////////////
511 //
512 // Description: set blending in element
513 // virtual method for GL rendering
514 //
515 // Use: private, virtual
516 //
517 /////////////////////////////////////////////////////////////////////////
518 void
519 SoGLLazyElement::setBlendingElt(SbBool value)
520 {
521
522 ivState.blending = value;
523
524 // For open caches, record the fact that set was called:
525 ivState.cacheLevelSetBits |= BLENDING_MASK;
526
527 // set invalid bit based on value
528 if (ivState.blending != glState.GLblending)
529 invalidBits |= BLENDING_MASK;
530 else invalidBits &= ~BLENDING_MASK;
531
532 return;
533 }
534
535 ////////////////////////////////////////////////////////////////////////
536 //
537 // Description:
538 // set all materials in the element
539 // special method for SoMaterial nodes
540 //
541 // Use: private
542 ////////////////////////////////////////////////////////////////////////
543
544 void
545 SoGLLazyElement::setMaterialElt(SoNode* node, uint32_t mask,
546 SoColorPacker *cPacker,
547 const SoMFColor& diffuse, const SoMFFloat& transp,
548 const SoMFColor& ambient, const SoMFColor& emissive,
549 const SoMFColor& specular, const SoMFFloat& shininess)
550 {
551 if ((mask & DIFFUSE_MASK) && !colorIndex ){
552 ivState.diffuseColors = diffuse.getValues(0);
553 ivState.numDiffuseColors = diffuse.getNum();
554 ivState.diffuseNodeId = (node->getNodeId());
555 ivState.packed = FALSE;
556 ivState.packedTransparent = FALSE;
557 }
558
559 if (mask & TRANSPARENCY_MASK){
560 ivState.numTransparencies = transp.getNum();
561 ivState.transparencies = transp.getValues(0);
562 ivState.stippleNum = 0;
563 if (ivState.numTransparencies == 1 && ivState.transparencies[0] == 0.0)
564 ivState.transpNodeId = 0;
565 else {
566 ivState.transpNodeId = node->getNodeId();
567 if (ivState.transparencies[0] != 0.0 &&
568 ivState.transpType == SoGLRenderAction::SCREEN_DOOR){
569 ivState.stippleNum =
570 (int)(ivState.transparencies[0]*getNumPatterns());
571 }
572 }
573
574 if (ivState.stippleNum != glState.GLStippleNum)
575 invalidBits |= TRANSPARENCY_MASK;
576 else invalidBits &= ~TRANSPARENCY_MASK;
577 }
578
579 // do combined packed color/transparency work:
580 if (mask & (DIFFUSE_MASK|TRANSPARENCY_MASK)){
581 if(!cPacker->diffuseMatch(ivState.diffuseNodeId) ||
582 (!cPacker->transpMatch(ivState.transpNodeId)))
583 packColors(cPacker);
584
585 if (ivState.diffuseNodeId != glState.GLDiffuseNodeId ||
586 ivState.transpNodeId != glState.GLTranspNodeId)
587 invalidBits |= DIFFUSE_MASK;
588 else invalidBits &= ~DIFFUSE_MASK;
589
590 ivState.packedColors = cPacker->getPackedColors();
591 }
592
593 if (mask & AMBIENT_MASK){
594 ivState.ambientColor = ambient[0];
595 invalidBits &= ~AMBIENT_MASK;
596 for (int i=0; i<3; i++){
597 if (ivState.ambientColor[i] != glState.GLAmbient[i]){
598 invalidBits |= AMBIENT_MASK;
599 break;
600 }
601 }
602 }
603
604 if (mask & EMISSIVE_MASK){
605 ivState.emissiveColor = emissive[0];
606 invalidBits &= ~EMISSIVE_MASK;
607 for (int i=0; i<3; i++){
608 if (ivState.emissiveColor[i] != glState.GLEmissive[i]){
609 invalidBits |= EMISSIVE_MASK;
610 break;
611 }
612 }
613 }
614
615 if (mask & SPECULAR_MASK){
616 ivState.specularColor = specular[0];
617 invalidBits &= ~SPECULAR_MASK;
618 for (int i=0; i<3; i++){
619 if (ivState.specularColor[i] != glState.GLSpecular[i]){
620 invalidBits |= SPECULAR_MASK;
621 break;
622 }
623 }
624 }
625
626 if (mask & SHININESS_MASK){
627 ivState.shininess = shininess[0];
628 if (fabsf(ivState.shininess - glState.GLShininess)
629 > SO_LAZY_SHINY_THRESHOLD){
630 invalidBits |= SHININESS_MASK;
631 }
632 else invalidBits &= ~SHININESS_MASK;
633 }
634 // For open caches, record the fact that set was called:
635 ivState.cacheLevelSetBits |= mask;
636 }
637 //////////////////////////////////////////////////////////////////////////
638 //
639 // Description:
640 //
641 // Send a packed color to the GL, update GLstate of current
642 // GLLazyElement to indicate it. Also do the right thing for
643 // stipple transparency.
644 // This is special method for sending the first color from a
645 // vertexProperty node, and making that send consistent with the
646 // transparency state.
647 // MUST be invoked immediately after initial SoGLLazyElement::send.
648 // AND that send must issue a lazy send of both diffuse color and transparency
649 // or otherwise cache problems will result.
650 //
651 // use: SoInternal, public
652 ////////////////////////////////////////////////////////////////////////////
653 //
654 void
655 SoGLLazyElement::sendVPPacked(SoState* state, const unsigned char* pcolor)
656 {
657 unsigned char _pcolor[4];
658 DGL_HTON_INT32(*((int32_t*)_pcolor), *((int32_t*)pcolor));
659
660 if (glState.GLColorMaterial ||
661 (glState.GLLightModel == BASE_COLOR))
662 glColor4ubv((const GLubyte*)_pcolor);
663 else{
664 float col4[4];
665 col4[3] = (_pcolor[3]) * 1.0/255;
666 col4[2] = (_pcolor[2]) * 1.0/255;
667 col4[1] = (_pcolor[1]) * 1.0/255;
668 col4[0] = (_pcolor[0]) * 1.0/255;
669 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, col4);
670 }
671
672 //Remember what things we are sending to GL, they have to be invalidated:
673 uint32_t sendMask = DIFFUSE_MASK;
674
675 //Now see about sending transparency:
676
677 // If Stipple is not being used, nothing to be done for transparency.
678 if (ivState.transpType == SoGLRenderAction::SCREEN_DOOR){
679
680 uint32_t trans = _pcolor[3];
681
682 // If transparency was off and is still off, can quit here
683 if (glState.GLStippleNum != 0 || trans != 0xff) {
684
685 // Setup GL stipple with the new value being specified
686 int newStipNum = (int)(getNumPatterns()*(1.-trans*(1.0/255.)));
687 if (newStipNum != glState.GLStippleNum){
688
689 // a real send is occurring:
690 sendMask |= TRANSPARENCY_MASK;
691
692 if (newStipNum >0){
693 sendStipple(state, newStipNum);
694 // if previously was off, turn on GLStipple
695 if (glState.GLStippleNum <= 0)
696 glEnable(GL_POLYGON_STIPPLE);
697 }
698 else{
699 glDisable(GL_POLYGON_STIPPLE);
700 }
701 glState.GLStippleNum = newStipNum;
702
703 }
704 }
705 }
706
707 //Now reset/invalidate the diffuse and possibly transparency components.
708
709 reset(state, sendMask);
710 }
711 /////////////////////////////////////////////////////////////////////////
712 //
713 // Description:
714 //
715 // Register with the cache that a redundant set was called.
716 // Cache must know that a set was called, even if the set is redundant.
717 // Must only be invoked if cache is open.
718 //
719 // use: virtual, private, SoInternal
720 ////////////////////////////////////////////////////////////////////////////
721 void
722 SoGLLazyElement::registerRedundantSet(SoState* state, uint32_t bitmask)
723 {
724 #ifdef DEBUG
725 if (!(state->isCacheOpen())){
726 SoDebugError::post("SoGLLazyElement::registerRedundantSet",
727 "Cache is not open!");
728 }
729 #endif /*DEBUG*/
730 //Must obtain a writable version of element (i.e. may need to push)
731 SoGLLazyElement *le = (SoGLLazyElement*)getWInstance(state);
732 le->ivState.cacheLevelSetBits |= bitmask;
733
734 }
735 /////////////////////////////////////////////////////////////////////////
736 //
737 // Description:
738 //
739 // Register with the cache that a get() was called.
740 // Must only be invoked if cache is open.
741 //
742 // use: virtual, private, SoINTERNAL
743 ////////////////////////////////////////////////////////////////////////////
744 void
745 SoGLLazyElement::registerGetDependence(SoState* state, uint32_t bitmask)
746 {
747 #ifdef DEBUG
748 if (!(state->isCacheOpen())){
749 SoDebugError::post("SoGLLazyElement::registerGetDependence",
750 "Cache is not open!");
751 }
752 #endif /*DEBUG*/
753 SoGLRenderCache* thisCache = (SoGLRenderCache*)
754 SoCacheElement::getCurrentCache((SoState*)state);
755 SoGLLazyElement* cacheLazyElt = thisCache->getLazyElt();
756 uint32_t levelSetBits = ivState.cacheLevelSetBits;
757 uint32_t didRealSendBits = cacheLazyElt->GLSendBits;
758
759 //If there was no set and no real send, there is an IV dependence:
760 uint32_t checkIV = bitmask & (~didRealSendBits) & (~levelSetBits);
761
762 if (checkIV) {
763 copyIVValues(checkIV,cacheLazyElt);
764 thisCache->setLazyBits(checkIV, 0, 0);
765 }
766 }
767
768 /////////////////////////////////////////////////////////////////////////
769 //
770 // Description:
771 //
772 // reset the element to have no knowledge of GL state
773 //
774 // use: public
775 /////////////////////////////////////////////////////////////////////////
776 void
777 SoGLLazyElement::reset(SoState* state, uint32_t bitmask) const
778 {
779 //cast away const...
780 SoGLLazyElement *le = (SoGLLazyElement *)this;
781 le->invalidBits |= bitmask;
782 //Set GLSendBits, so invalidation will persist after pop().
783 le->GLSendBits |= bitmask;
784
785 // If cache is open, we must assume that there has been a GLSend
786 // of the component being reset, and set the corresponding GLSendBit.
787 if (state->isCacheOpen()){
788 SoGLRenderCache* thisCache = (SoGLRenderCache*)
789 SoCacheElement::getCurrentCache((SoState*)state);
790 SoGLLazyElement* cacheLazyElt = thisCache->getLazyElt();
791 cacheLazyElt->GLSendBits |= bitmask;
792 }
793
794 for(int j=0; (j< SO_LAZY_NUM_COMPONENTS) && bitmask; j++, bitmask >>=1){
795 if (bitmask&1) {
796 switch(j) {
797 case(LIGHT_MODEL_CASE):
798 le->glState.GLLightModel = -1;
799 break;
800
801 case(COLOR_MATERIAL_CASE):
802 le->glState.GLColorMaterial = -1;
803 break;
804
805 case(DIFFUSE_CASE):
806 le->glState.GLDiffuseNodeId = 1;
807 le->glState.GLTranspNodeId = 1;
808 break;
809
810 case(AMBIENT_CASE):
811 le->glState.GLAmbient[0]= -1;
812 break;
813
814 case(EMISSIVE_CASE):
815 le->glState.GLEmissive[0] = -1;
816 break;
817
818 case(SPECULAR_CASE):
819 le->glState.GLSpecular[0] = -1;
820 break;
821
822 case(SHININESS_CASE):
823 le->glState.GLShininess = -1;
824 break;
825
826 case(TRANSPARENCY_CASE):
827 le->glState.GLStippleNum = -1;
828 break;
829
830 case(BLENDING_CASE):
831 le->glState.GLblending = -1;
832 break;
833
834 }
835 }
836 }
837 return;
838 }
839 ////////////////////////////////////////////////////////////////////////
840 //
841 // Description:
842 // used to test whether a cache can be used. Invoked by lazyMatches.
843 //
844 // Compares element in state against cached element, looking for
845 // matches in specified GL and/or IV state
846 //
847 //
848 // Use: private
849 /////////////////////////////////////////////////////////////////////////
850
851 SbBool
852 SoGLLazyElement::fullLazyMatches(uint32_t checkGL, uint32_t checkIV,
853 const SoGLLazyElement *stateLazyElt)
854
855 {
856 // check to make sure transparency type has not changed:
857 if(ivState.transpType != stateLazyElt->ivState.transpType){
858 #ifdef DEBUG
859 if (SoDebug::GetEnv("IV_DEBUG_CACHES")) {
860 fprintf(stderr, "CACHE DEBUG: cache not valid\n ");
861 fprintf(stderr, "transparency type match failed,\n");
862 fprintf(stderr, "prev, current %d %d\n",
863 ivState.transpType, stateLazyElt->ivState.transpType);
864 }
865 #endif /*DEBUG*/
866 return FALSE;
867 }
868
869 int i;
870 uint32_t bitmask = checkIV;
871
872 for(i=0; (i< SO_LAZY_NUM_COMPONENTS)&&bitmask; i++,bitmask>>=1){
873
874 if (bitmask & 1){
875
876 int j;
877 switch(i){
878 case(LIGHT_MODEL_CASE):
879 if (ivState.lightModel != stateLazyElt->ivState.lightModel){
880 #ifdef DEBUG
881 if (SoDebug::GetEnv("IV_DEBUG_CACHES")) {
882 fprintf(stderr, "CACHE DEBUG: cache not valid\n ");
883 fprintf(stderr, "lightModel match failed,\n");
884 fprintf(stderr, "prev, current %d %d\n",
885 ivState.lightModel,
886 stateLazyElt->ivState.lightModel);
887 }
888 #endif /*DEBUG*/
889 return FALSE;
890 }
891 break;
892
893 case(COLOR_MATERIAL_CASE):
894 if (ivState.colorMaterial !=
895 stateLazyElt->ivState.colorMaterial){
896 #ifdef DEBUG
897 if (SoDebug::GetEnv("IV_DEBUG_CACHES")) {
898 fprintf(stderr, "CACHE DEBUG: cache not valid\n ");
899 fprintf(stderr, "colorMaterial match failed,\n");
900 fprintf(stderr, "prev, current %d %d\n",
901 ivState.colorMaterial,
902 stateLazyElt->ivState.colorMaterial);
903 }
904 #endif /*DEBUG*/
905 return FALSE;
906 }
907 break;
908
909 case(DIFFUSE_CASE):
910 if (ivState.diffuseNodeId !=
911 stateLazyElt->ivState.diffuseNodeId ||
912 ivState.transpNodeId !=
913 stateLazyElt->ivState.transpNodeId){
914 #ifdef DEBUG
915 if (SoDebug::GetEnv("IV_DEBUG_CACHES")) {
916 fprintf(stderr, "CACHE DEBUG: cache not valid\n");
917 fprintf(stderr, "diffuse&trans match failed,\n");
918 fprintf(stderr, "prev, current %d %d, %d %d\n",
919 ivState.diffuseNodeId, ivState.transpNodeId,
920 stateLazyElt->ivState.diffuseNodeId,
921 stateLazyElt->ivState.transpNodeId);
922 }
923 #endif /*DEBUG*/
924 return (FALSE);
925 }
926 break;
927 case(AMBIENT_CASE):
928 for(j=0; j<3; j++){
929 if (ivState.ambientColor[j]!=
930 stateLazyElt->ivState.ambientColor[j]){
931 #ifdef DEBUG
932 if (SoDebug::GetEnv("IV_DEBUG_CACHES")) {
933 fprintf(stderr, "CACHE DEBUG: cache not valid\n");
934 fprintf(stderr, "ambient %d match failed,\n", j);
935 fprintf(stderr, "prev, current %f %f\n",
936 ivState.ambientColor[j],
937 stateLazyElt->ivState.ambientColor[j]);
938 }
939 #endif /*DEBUG*/
940 return(FALSE);
941 }
942 }
943 break;
944
945 case(EMISSIVE_CASE):
946 for(j=0; j<3; j++){
947 if (ivState.emissiveColor[j]!=
948 stateLazyElt->ivState.emissiveColor[j]){
949 #ifdef DEBUG
950 if (SoDebug::GetEnv("IV_DEBUG_CACHES")) {
951 fprintf(stderr, "CACHE DEBUG: cache not valid\n");
952 fprintf(stderr, "emissive %d match failed,\n", j);
953 fprintf(stderr, "prev, current %f %f\n",
954 ivState.emissiveColor[j],
955 stateLazyElt->ivState.emissiveColor[j]);
956 }
957 #endif /*DEBUG*/
958 return(FALSE);
959 }
960 }
961 break;
962
963 case(SPECULAR_CASE):
964 for(j=0; j<3; j++){
965 if (ivState.specularColor[j]!=
966 stateLazyElt->ivState.specularColor[j]){
967 #ifdef DEBUG
968 if (SoDebug::GetEnv("IV_DEBUG_CACHES")) {
969 fprintf(stderr, "CACHE DEBUG: cache not valid\n");
970 fprintf(stderr, "specular %d match failed,\n", j);
971 fprintf(stderr, "prev, current %f %f\n",
972 ivState.specularColor[j],
973 stateLazyElt->ivState.specularColor[j]);
974 }
975 #endif /*DEBUG*/
976 return(FALSE);
977 }
978 }
979 break;
980
981 case(SHININESS_CASE):
982 if (fabsf(ivState.shininess - stateLazyElt->ivState.shininess)
983 > SO_LAZY_SHINY_THRESHOLD){
984 #ifdef DEBUG
985 if (SoDebug::GetEnv("IV_DEBUG_CACHES")) {
986 fprintf(stderr, "CACHE DEBUG: cache not valid\n");
987 fprintf(stderr, "shininess match failed,\n");
988 fprintf(stderr, "prev, current %f %f\n",
989 ivState.shininess,
990 stateLazyElt->ivState.shininess);
991 }
992 #endif /*DEBUG*/
993 return (FALSE);
994 }
995 break;
996
997 case(BLENDING_CASE):
998 if (ivState.blending != stateLazyElt->ivState.blending){
999 #ifdef DEBUG
1000 if (SoDebug::GetEnv("IV_DEBUG_CACHES")) {
1001 fprintf(stderr, "CACHE DEBUG: cache not valid\n");
1002 fprintf(stderr, "blend match failed,\n");
1003 fprintf(stderr, "prev, current %d %d\n",
1004 ivState.blending,
1005 stateLazyElt->ivState.blending);
1006 }
1007 #endif /*DEBUG*/
1008 return FALSE;
1009 }
1010 break;
1011
1012
1013 case(TRANSPARENCY_CASE):
1014 if (ivState.stippleNum !=
1015 stateLazyElt->ivState.stippleNum){
1016 #ifdef DEBUG
1017 if (SoDebug::GetEnv("IV_DEBUG_CACHES")) {
1018 fprintf(stderr, "CACHE DEBUG: cache not valid\n");
1019 fprintf(stderr, "IVstipple match failed,\n");
1020 fprintf(stderr, "prev, current %d %d\n",
1021 ivState.stippleNum,
1022 stateLazyElt->ivState.stippleNum);
1023 }
1024 #endif /*DEBUG*/
1025 return FALSE;
1026 }
1027 break;
1028
1029 default:
1030 #ifdef DEBUG
1031 SoDebugError::post("SoGLLazyElement::matches",
1032 "Invalid component of element");
1033 #endif /*DEBUG*/
1034 break;
1035 }
1036 }
1037 }
1038
1039 if (!(bitmask = checkGL)) return TRUE;
1040
1041 for(i=0; (i< SO_LAZY_NUM_COMPONENTS)&&bitmask; i++,bitmask>>=1){
1042
1043 if (bitmask & 1){
1044
1045 int j;
1046 switch(i){
1047 case(LIGHT_MODEL_CASE):
1048 if (glState.GLLightModel !=
1049 stateLazyElt->glState.GLLightModel){
1050 #ifdef DEBUG
1051 if (SoDebug::GetEnv("IV_DEBUG_CACHES")) {
1052 fprintf(stderr, "CACHE DEBUG: cache not valid\n ");
1053 fprintf(stderr, "GLLightModel match failed,\n");
1054 fprintf(stderr, "prev, current %d %d\n",
1055 glState.GLLightModel,
1056 stateLazyElt->glState.GLLightModel);
1057 }
1058 #endif /*DEBUG*/
1059 return FALSE;
1060 }
1061 break;
1062
1063 case(COLOR_MATERIAL_CASE):
1064 if (glState.GLColorMaterial !=
1065 stateLazyElt->glState.GLColorMaterial){
1066 #ifdef DEBUG
1067 if (SoDebug::GetEnv("IV_DEBUG_CACHES")) {
1068 fprintf(stderr, "CACHE DEBUG: cache not valid\n");
1069 fprintf(stderr, "GLColorMaterial match failed,\n");
1070 fprintf(stderr, "prev, current %d %d\n",
1071 glState.GLColorMaterial,
1072 stateLazyElt->glState.GLColorMaterial);
1073 }
1074 #endif /*DEBUG*/
1075 return FALSE;
1076 }
1077 break;
1078
1079 case(DIFFUSE_CASE):
1080 if (glState.GLDiffuseNodeId !=
1081 stateLazyElt->glState.GLDiffuseNodeId ||
1082 glState.GLTranspNodeId !=
1083 stateLazyElt->glState.GLTranspNodeId){
1084 #ifdef DEBUG
1085 if (SoDebug::GetEnv("IV_DEBUG_CACHES")) {
1086 fprintf(stderr, "CACHE DEBUG: cache not valid\n");
1087 fprintf(stderr, "GLDiffuse&Transp match failed,\n");
1088 fprintf(stderr, "prev, current %d %d, %d %d\n",
1089 glState.GLDiffuseNodeId,
1090 glState.GLTranspNodeId,
1091 stateLazyElt->glState.GLDiffuseNodeId,
1092 stateLazyElt->glState.GLTranspNodeId);
1093 }
1094 #endif /*DEBUG*/
1095 return (FALSE);
1096 }
1097 break;
1098
1099 case(AMBIENT_CASE):
1100 for(j=0; j<3; j++){
1101 if (glState.GLAmbient[j]!=
1102 stateLazyElt->glState.GLAmbient[j]) {
1103 #ifdef DEBUG
1104 if (SoDebug::GetEnv("IV_DEBUG_CACHES")) {
1105 fprintf(stderr, "CACHE DEBUG: cache not valid\n");
1106 fprintf(stderr, "GLambient %d match failed,\n",j);
1107 fprintf(stderr, "prev, current %f %f\n",
1108 glState.GLAmbient[j],
1109 stateLazyElt->glState.GLAmbient[j]);
1110 }
1111 #endif /*DEBUG*/
1112 return(FALSE);
1113 }
1114 }
1115 break;
1116
1117 case(EMISSIVE_CASE):
1118 for(j=0; j<3; j++){
1119 if (glState.GLEmissive[j]!=
1120 stateLazyElt->glState.GLEmissive[j]){
1121 #ifdef DEBUG
1122 if (SoDebug::GetEnv("IV_DEBUG_CACHES")) {
1123 fprintf(stderr, "CACHE DEBUG: cache not valid\n");
1124 fprintf(stderr, "GLemissive %d match failed,\n", j);
1125 fprintf(stderr, "prev, current %f %f\n",
1126 glState.GLEmissive[j],
1127 stateLazyElt->glState.GLEmissive[j]);
1128 }
1129 #endif /*DEBUG*/
1130 return(FALSE);
1131 }
1132 }
1133 break;
1134
1135 case(SPECULAR_CASE):
1136 for(j=0; j<3; j++){
1137 if (glState.GLSpecular[j]!=
1138 stateLazyElt->glState.GLSpecular[j]){
1139 #ifdef DEBUG
1140 if (SoDebug::GetEnv("IV_DEBUG_CACHES")) {
1141 fprintf(stderr, "CACHE DEBUG: cache not valid\n");
1142 fprintf(stderr, "GLspecular %d match failed,\n", j);
1143 fprintf(stderr, "prev, current %f %f\n",
1144 glState.GLSpecular[j],
1145 stateLazyElt->glState.GLSpecular[j]);
1146 }
1147 #endif /*DEBUG*/
1148 return(FALSE);
1149 }
1150 }
1151 break;
1152
1153 case(SHININESS_CASE):
1154 if (fabsf(glState.GLShininess -
1155 stateLazyElt->glState.GLShininess)>
1156 SO_LAZY_SHINY_THRESHOLD){
1157 #ifdef DEBUG
1158 if (SoDebug::GetEnv("IV_DEBUG_CACHES")) {
1159 fprintf(stderr, "CACHE DEBUG: cache not valid\n");
1160 fprintf(stderr, "GLshininess match failed,\n");
1161 fprintf(stderr, "prev, current %f %f\n",
1162 glState.GLShininess,
1163 stateLazyElt->glState.GLShininess);
1164 }
1165 #endif /*DEBUG*/
1166 return (FALSE);
1167 }
1168 break;
1169
1170 case(BLENDING_CASE):
1171 if (glState.GLblending !=
1172 stateLazyElt->glState.GLblending){
1173 #ifdef DEBUG
1174 if (SoDebug::GetEnv("IV_DEBUG_CACHES")) {
1175 fprintf(stderr, "CACHE DEBUG: cache not valid\n");
1176 fprintf(stderr, "GLblending match failed,\n");
1177 fprintf(stderr, "prev, current %d %d\n",
1178 glState.GLblending,
1179 stateLazyElt->glState.GLblending);
1180 }
1181 #endif /*DEBUG*/
1182 return FALSE;
1183 }
1184 break;
1185
1186
1187 case(TRANSPARENCY_CASE):
1188 if (glState.GLStippleNum !=
1189 stateLazyElt->glState.GLStippleNum){
1190 #ifdef DEBUG
1191 if (SoDebug::GetEnv("IV_DEBUG_CACHES")) {
1192 fprintf(stderr, "CACHE DEBUG: cache not valid\n");
1193 fprintf(stderr, "GLstipple match failed,\n");
1194 fprintf(stderr, "prev, current %d %d\n",
1195 glState.GLStippleNum,
1196 stateLazyElt->glState.GLStippleNum);
1197 }
1198 #endif /*DEBUG*/
1199 return FALSE;
1200 }
1201 break;
1202
1203 default:
1204 #ifdef DEBUG
1205 SoDebugError::post("SoGLLazyElement::matches",
1206 "Invalid component of element");
1207 #endif /*DEBUG*/
1208 break;
1209 }
1210
1211 }
1212 }
1213
1214 return(TRUE);
1215
1216 }
1217
1218 ////////////////////////////////////////////////////////////////////////
1219 //
1220 // Description:
1221 // Create a copy of this instance suitable for doing cache
1222 // matching on. Needs to have bitfields zeroed, and copy the
1223 // transparency type.
1224 // and "this" lazy element needs its cacheLevelSetBits zeroed.
1225 // and its cacheLevelSendBits zeroed.
1226 //
1227 // Use: public, SoINTERNAL
1228 ////////////////////////////////////////////////////////////////////////
1229 SoGLLazyElement *
1230 SoGLLazyElement::copyLazyMatchInfo(SoState *state)
1231 {
1232 // force a push, so we can set the setBits in a new element.
1233 SoGLLazyElement* newElt =
1234 (SoGLLazyElement *)SoLazyElement::getWInstance(state);
1235 newElt->ivState.cacheLevelSetBits = 0;
1236 newElt->ivState.cacheLevelSendBits = 0;
1237 SoGLLazyElement *result =
1238 (SoGLLazyElement *)getTypeId().createInstance();
1239
1240 result->GLSendBits = 0;
1241 result->ivState.transpType = newElt->ivState.transpType;
1242 return result;
1243 }
1244
1245 ////////////////////////////////////////////////////////////////////////
1246 //
1247 // Description:
1248 // Sends to GL only what needs to go:
1249 //
1250 // Use: private
1251 ////////////////////////////////////////////////////////////////////////
1252 void
1253 SoGLLazyElement::reallySend(const SoState *state, uint32_t bitmask)
1254
1255 //Send only the parts that don't match inventor state:
1256 // does the following:
1257 // copies the inventor state to the GL state
1258 // sends the GL value to GL
1259 // sets the appropriate invalidBits off.
1260 // sets the appropriate GLSendBits on.
1261 //
1262 // If a renderCache is open, it does additional work,
1263 // identifying if this is the first send of the component in the cache,
1264 // identifying if it has been set in the cache, etc.
1265 // This information is used to determine whether there is a cache dependency,
1266 // and if so, whether it is dependent on the inventor state, GL state, or
1267 // dependent on inventor matching GL.
1268 {
1269
1270 uint32_t sendBits = bitmask & invalidBits;
1271 uint32_t realSendBits = 0;
1272 // with base_color, don't send nondiffuse colors.
1273 if (ivState.lightModel == BASE_COLOR) sendBits &= ~OTHER_COLOR_MASK;
1274
1275 //everything that was requested to send will be valid afterward,
1276
1277 invalidBits &= ~(sendBits);
1278
1279 SbBool sendit;
1280
1281 for(int j = 0; (j< SO_LAZY_NUM_COMPONENTS) && (sendBits != 0);
1282 j++,sendBits >>=1){
1283 if (sendBits & 1){
1284 int i;
1285 switch(j){
1286 // Note that lightmodel and colormaterial have to send
1287 // before diffuse, so that we can force send diffuse
1288
1289 case(LIGHT_MODEL_CASE):
1290 if (glState.GLLightModel == ivState.lightModel) break;
1291 if (ivState.lightModel == PHONG){
1292 glEnable(GL_LIGHTING);
1293 if (colorIndex)glShadeModel(GL_FLAT);
1294 }
1295 else {
1296 glDisable(GL_LIGHTING);
1297 if (colorIndex) glShadeModel(GL_SMOOTH);
1298 }
1299 glState.GLLightModel = ivState.lightModel;
1300 realSendBits |= LIGHT_MODEL_MASK;
1301 //force-send the diffuse color:
1302 sendBits |= (DIFFUSE_MASK >> LIGHT_MODEL_CASE);
1303 glState.GLDiffuseNodeId = 1;
1304 break;
1305
1306 case(COLOR_MATERIAL_CASE):
1307 // Handle color material if light model does not change:
1308 if (ivState.colorMaterial == glState.GLColorMaterial) break;
1309 realSendBits |= COLOR_MATERIAL_MASK;
1310 glState.GLColorMaterial = ivState.colorMaterial;
1311 if (ivState.colorMaterial){
1312 glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
1313 glEnable(GL_COLOR_MATERIAL);
1314 }
1315 else glDisable(GL_COLOR_MATERIAL);
1316 #ifdef DEBUG
1317 if ((glState.GLLightModel == BASE_COLOR) &&
1318 (glState.GLColorMaterial)){
1319 SoDebugError::post("SoGLLazyElement::reallySend",
1320 "ColorMaterial being used with BASE_COLOR");
1321 }
1322 #endif /*DEBUG*/
1323 // ensure diffuse color will send:
1324 sendBits |= (DIFFUSE_MASK >> COLOR_MATERIAL_CASE);
1325 glState.GLDiffuseNodeId = 1;
1326 break;
1327
1328 case(DIFFUSE_CASE):
1329 // in this case, always send color[0]
1330 if (glState.GLDiffuseNodeId == ivState.diffuseNodeId &&
1331 glState.GLTranspNodeId == ivState.transpNodeId)
1332 break;
1333 realSendBits |= DIFFUSE_MASK;
1334 glState.GLDiffuseNodeId = ivState.diffuseNodeId;
1335 glState.GLTranspNodeId = ivState.transpNodeId;
1336
1337 if(colorIndex){
1338 #ifdef DEBUG
1339 if (glState.GLLightModel != BASE_COLOR){
1340 SoDebugError::post("SoGLLazyElement::reallySend",
1341 "PHONG shading used in colorIndex mode");
1342 }
1343 #endif
1344 glIndexi((GLint)ivState.colorIndices[0]);
1345 break;
1346 }
1347 float col4[4];
1348
1349 if (glState.GLColorMaterial ||
1350 (glState.GLLightModel== BASE_COLOR)) {
1351 uint32_t pColors;
1352 DGL_HTON_INT32(pColors, *(ivState.packedColors));
1353 glColor4ubv((GLubyte*)&pColors);
1354 }
1355 else{
1356 col4[3] = (ivState.packedColors[0] &
1357 0xff) * 1.0/255;
1358 col4[2] = ((ivState.packedColors[0] &
1359 0xff00) >> 8) * 1.0/255;
1360 col4[1] = ((ivState.packedColors[0] &
1361 0xff0000)>> 16) * 1.0/255;
1362 col4[0] = ((ivState.packedColors[0] &
1363 0xff000000)>>24) * 1.0/255;
1364 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, col4);
1365 }
1366 break;
1367
1368 case(AMBIENT_CASE):
1369 sendit = FALSE;
1370 for(i=0; i<3; i++){
1371 if (glState.GLAmbient[i] != ivState.ambientColor[i]){
1372 sendit=TRUE;
1373 glState.GLAmbient[i]=ivState.ambientColor[i];
1374 }
1375 }
1376 if (!sendit) break;
1377 realSendBits |= AMBIENT_MASK;
1378 glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,
1379 (GLfloat*)glState.GLAmbient);
1380 break;
1381
1382 case(EMISSIVE_CASE):
1383 sendit = FALSE;
1384 for(i=0; i<3; i++){
1385 if (glState.GLEmissive[i]!=ivState.emissiveColor[i]){
1386 sendit = TRUE;
1387 glState.GLEmissive[i]=ivState.emissiveColor[i];
1388 }
1389 }
1390 if (!sendit) break;
1391 realSendBits |= EMISSIVE_MASK;
1392 glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,
1393 (GLfloat*)glState.GLEmissive);
1394 break;
1395
1396 case(SPECULAR_CASE):
1397 sendit = FALSE;
1398 for(i=0; i<3; i++){
1399 if (glState.GLSpecular[i]!=ivState.specularColor[i]){
1400 sendit = TRUE;
1401 glState.GLSpecular[i]=ivState.specularColor[i];
1402 }
1403 }
1404 if (!sendit) break;
1405 realSendBits |= SPECULAR_MASK;
1406 glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,
1407 (GLfloat*)glState.GLSpecular);
1408 break;
1409
1410 case(SHININESS_CASE):
1411 if (fabsf(glState.GLShininess-ivState.shininess)<
1412 SO_LAZY_SHINY_THRESHOLD) break;
1413 realSendBits |= SHININESS_MASK;
1414 glState.GLShininess=ivState.shininess;
1415 glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,
1416 (GLfloat)glState.GLShininess*128.0);
1417 break;
1418
1419 case(BLENDING_CASE):
1420
1421 if (glState.GLblending == ivState.blending) break;
1422 realSendBits |= BLENDING_MASK;
1423 glState.GLblending = ivState.blending;
1424 if (ivState.blending == TRUE){
1425 glEnable(GL_BLEND);
1426 }
1427 // blend is being turned off:
1428 else glDisable(GL_BLEND);
1429 break;
1430
1431 case(TRANSPARENCY_CASE):
1432
1433 if (ivState.stippleNum == glState.GLStippleNum) break;
1434
1435 if (ivState.stippleNum == 0){
1436 glDisable(GL_POLYGON_STIPPLE);
1437 }
1438 else{
1439 sendStipple(state, ivState.stippleNum);
1440 if (glState.GLStippleNum <= 0)
1441 glEnable(GL_POLYGON_STIPPLE);
1442 }
1443 glState.GLStippleNum = ivState.stippleNum;
1444
1445 realSendBits |= TRANSPARENCY_MASK;
1446 break;
1447 }
1448 }
1449 }
1450 //Record the real_sends:
1451 GLSendBits |= realSendBits;
1452
1453 // if cache is open, record required info:
1454 if (state->isCacheOpen()){
1455 SoGLRenderCache* thisCache = (SoGLRenderCache*)
1456 SoCacheElement::getCurrentCache((SoState*)state);
1457 SoGLLazyElement* cacheLazyElt = thisCache->getLazyElt();
1458 uint32_t didRealSendBits = cacheLazyElt->GLSendBits;
1459 uint32_t levelSetBits = ivState.cacheLevelSetBits;
1460 uint32_t levelSendBits = ivState.cacheLevelSendBits;
1461
1462 // To determine which of doSend, checkGL, and checkIV bits to set,
1463 // All combinations of the bits
1464 // A: levelSendBits
1465 // B: levelSetBits
1466 // C: didRealSendBits
1467 // D: realSendBits
1468 // are considered. The following matrix describes the resulting
1469 // dependencies:
1470 //
1471 // A=0 A=1 A=0 A=1
1472 // B=0 B=0 B=1 B=1
1473 //
1474 // C=0,D=0 IV=GL (IV=GL) GL GL
1475 //
1476 // C=0,D=1 IV (IV) OK OK
1477 //
1478 // C=1,D=0 IV (IV) OK OK
1479 //
1480 // C=1,D=1 IV IV OK OK
1481 //
1482 // In the above, OK means no dependence, a dependence in parens
1483 // means that the dependence is already implied by another
1484 // send in the same scene graph.
1485
1486 // doSend flag indicates dependencies on IV=GL; these are components
1487 // with no set and a non-real send in the cache.
1488 uint32_t doSend = bitmask&(~didRealSendBits)&(~realSendBits)
1489 &(~levelSetBits)&(~levelSendBits);
1490
1491 // checkIV flag indicates dependence on IV. These are components that
1492 // did a real send not preceded by a set:
1493 uint32_t checkIV = bitmask&(~levelSetBits)&
1494 (((~levelSendBits)&(realSendBits))|
1495 ((~levelSendBits)&(didRealSendBits))|
1496 ((didRealSendBits)&(realSendBits)));
1497
1498 //checkGL indicates a dependence on GL. These are components that did
1499 //a non-real send after a real set.
1500 uint32_t checkGL = bitmask&(~didRealSendBits)&(~realSendBits)
1501 &(levelSetBits);
1502
1503 //with light model, a dependence on IV=GL is also a dependence on GL
1504
1505 if(doSend & LIGHT_MODEL_MASK){
1506 checkGL |= LIGHT_MODEL_MASK;
1507 }
1508 //with diffuse colors, if there are multiple colors, an IV=GL
1509 //dependence is also an IV dependence
1510 if((doSend & DIFFUSE_MASK)&&(ivState.numDiffuseColors > 1)){
1511 checkIV |= DIFFUSE_MASK;
1512 }
1513 cacheLazyElt->GLSendBits |= realSendBits;
1514 if (checkGL) copyGLValues(checkGL,cacheLazyElt);
1515 if (checkIV) copyIVValues(checkIV,cacheLazyElt);
1516 thisCache->setLazyBits(checkIV, checkGL, doSend);
1517
1518 //If we set a bit in levelSendBits, must do this on a writable element:
1519 if (ivState.cacheLevelSendBits != (ivState.cacheLevelSendBits|bitmask)){
1520 SoGLLazyElement* le =
1521 (SoGLLazyElement*)getWInstance((SoState *)state);
1522 le->ivState.cacheLevelSendBits |= bitmask;
1523 }
1524 }
1525
1526 return;
1527 }
1528 ///////////////////////////////////////////////////////////////////////////
1529 //
1530 // Description:
1531 // Send color and transparency to GL.
1532 // This method is included for compatibility with MaterialBundles.
1533 // Note that this does not update entire material state, should not be the
1534 // first "send" of a shape node.
1535 //
1536 // use: public, SoEXTENDER
1537 ////////////////////////////////////////////////////////////////////////////
1538 void
1539 SoGLLazyElement::sendDiffuseByIndex(int index) const
1540 {
1541 float col4[4];
1542 #ifdef DEBUG
1543 //check to make sure lazy element has updated GL state;
1544 //this method should only be called after an initial call to
1545 //SoLazyElement::reallySend
1546 if ((invalidBits & NO_COLOR_MASK) != 0){
1547 SoDebugError::post("SoGLLazyElement::sendDiffuseByIndex",
1548 "Indexed send not preceded by send of lazy element");
1549 }
1550 if (index >= ivState.numDiffuseColors){
1551 SoDebugError::post("SoGLLazyElement::sendDiffuseByIndex",
1552 "Not enough diffuse colors provided");
1553 }
1554 if (index >= ivState.numTransparencies &&
1555 ivState.numTransparencies > 1){
1556 SoDebugError::post("SoGLLazyElement::sendDiffuseByIndex",
1557 "Not enough transparencies provided");
1558 }
1559 #endif /*DEBUG*/
1560 //If in color index mode, ignore transparency.
1561 if (colorIndex){
1562 glIndexi((GLint)ivState.colorIndices[index]);
1563 return;
1564 }
1565
1566 if (glState.GLColorMaterial || (glState.GLLightModel == BASE_COLOR)) {
1567 uint32_t pColors;
1568 DGL_HTON_INT32(pColors, *(ivState.packedColors+index));
1569 glColor4ubv((GLubyte*)&pColors);
1570 }
1571 else {
1572 col4[3] = (ivState.packedColors[index] & 0xff) * 1.0/255;
1573 col4[2] = ((ivState.packedColors[index] & 0xff00) >> 8) * 1.0/255;
1574 col4[1] = ((ivState.packedColors[index] & 0xff0000)>> 16) * 1.0/255;
1575 col4[0] = ((ivState.packedColors[index] & 0xff000000)>>24) * 1.0/255;
1576 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, col4);
1577 }
1578 return;
1579 }
1580 ///////////////////////////////////////////////////////////////////////////
1581 //
1582 // Description:
1583 //
1584 // This is to be invoked when a cache ("child cache") is called while
1585 // another cache "parent cache" is open. It sets the appropriate
1586 // bitflags to make the lazy state of the child merge into the parent.
1587 //
1588 // use: SoINTERNAL public
1589 //
1590 ////////////////////////////////////////////////////////////////////////////
1591 void
1592 SoGLLazyElement::mergeCacheInfo(SoGLRenderCache* childCache,
1593 SoGLRenderCache* parentCache, uint32_t doSendFlag, uint32_t checkIVFlag,
1594 uint32_t checkGLFlag)
1595 {
1596 SoGLLazyElement* parentLazyElt = parentCache->getLazyElt();
1597 SoGLLazyElement* childLazyElt = childCache->getLazyElt();
1598 uint32_t parentDidRealSendBits = parentLazyElt->GLSendBits;
1599 uint32_t parentSetBits = ivState.cacheLevelSetBits;
1600 uint32_t doSend = (~parentDidRealSendBits)& doSendFlag
1601 &(~ivState.cacheLevelSendBits)&(~ivState.cacheLevelSetBits);
1602 uint32_t checkIV = checkIVFlag & (~ivState.cacheLevelSetBits);
1603 uint32_t checkGL = (~parentDidRealSendBits)& checkGLFlag;
1604
1605
1606 if (checkGL)
1607 childLazyElt->copyGLValues(checkGL, parentLazyElt);
1608 if (checkIV)
1609 childLazyElt->copyIVValues(checkIV, parentLazyElt);
1610
1611 // in addition we must consider the case where the parent cache issued
1612 // a set, and the child issued a send which was not a real_send.
1613 uint32_t moreGL = (~parentDidRealSendBits)&(parentSetBits)&
1614 ( checkGLFlag|doSendFlag );
1615 if (moreGL) copyGLValues(moreGL,parentLazyElt);
1616
1617 parentCache->setLazyBits
1618 (checkIV, checkGL|moreGL, doSend);
1619 // merge the child didRealSendBits into parent:
1620 parentLazyElt->GLSendBits |= childLazyElt->GLSendBits;
1621
1622
1623 }
1624 ////////////////////////////////////////////////////////////////////////
1625 //
1626 // Description:
1627 // Copy GL values of state element into cacheGLState, based on GLSendBits
1628 // in the cacheLazyElement.
1629 // This is intended to be called at
1630 // cache close(), to keep track of what GL was sent in the cache.
1631 // "this" is the current lazy element in the state.
1632 //
1633 // use: SO_INTERNAL public
1634 //
1635 ////////////////////////////////////////////////////////////////////////
1636 void
1637 SoGLLazyElement::getCopyGL(SoGLLazyElement* cacheLazyElement,
1638 SoGLLazyElement::GLLazyState& cacheGLState)
1639 {
1640 uint32_t bitmask = cacheLazyElement->GLSendBits;
1641 for(int j=0; (j<SO_LAZY_NUM_COMPONENTS)&&bitmask; j++, bitmask>>=1){
1642
1643 if (bitmask&1){
1644 int i;
1645 switch(j){
1646 case(LIGHT_MODEL_CASE):
1647 cacheGLState.GLLightModel = glState.GLLightModel;
1648 break;
1649
1650 case(COLOR_MATERIAL_CASE):
1651 cacheGLState.GLColorMaterial =
1652 glState.GLColorMaterial;
1653 break;
1654
1655 case(DIFFUSE_CASE):
1656 cacheGLState.GLDiffuseNodeId =
1657 glState.GLDiffuseNodeId;
1658 cacheGLState.GLTranspNodeId =
1659 glState.GLTranspNodeId;
1660 break;
1661
1662 case(AMBIENT_CASE):
1663 for(i=0; i<3; i++)
1664 cacheGLState.GLAmbient[i] =
1665 glState.GLAmbient[i];
1666 break;
1667
1668 case(EMISSIVE_CASE):
1669 for(i=0; i<3; i++)
1670 cacheGLState.GLEmissive[i] =
1671 glState.GLEmissive[i];
1672 break;
1673
1674 case(SPECULAR_CASE):
1675 for(i=0; i<3; i++)
1676 cacheGLState.GLSpecular[i] =
1677 glState.GLSpecular[i];
1678 break;
1679
1680 case(SHININESS_CASE):
1681 cacheGLState.GLShininess =
1682 glState.GLShininess;
1683 break;
1684
1685 case(BLENDING_CASE):
1686 cacheGLState.GLblending =
1687 glState.GLblending;
1688 break;
1689
1690 case(TRANSPARENCY_CASE):
1691 cacheGLState.GLStippleNum =
1692 glState.GLStippleNum;
1693 break;
1694 }
1695 }
1696 }
1697 }
1698 ////////////////////////////////////////////////////////////////////////
1699 //
1700 // Description:
1701 // Copy GL values to state element from cacheLazyElement, based on GLSendBits
1702 // in the cacheLazyElement. To be used after a cache is called.
1703 // also sets the GLsend bits and invalid bits in the state lazy element.
1704 //
1705 // use: SO_INTERNAL private
1706 //
1707 ////////////////////////////////////////////////////////////////////////
1708 void
1709 SoGLLazyElement::reallyCopyBackGL(uint32_t bitmask,
1710 SoGLLazyElement::GLLazyState& cacheGLState)
1711 {
1712 GLSendBits |= bitmask;
1713 invalidBits |= bitmask;
1714
1715 for(int j=0; (j<SO_LAZY_NUM_COMPONENTS)&&bitmask; j++, bitmask>>=1){
1716
1717 if (bitmask&1){
1718 int i;
1719 switch(j){
1720 case(LIGHT_MODEL_CASE):
1721 glState.GLLightModel =
1722 cacheGLState.GLLightModel;
1723 break;
1724
1725 case(COLOR_MATERIAL_CASE):
1726 glState.GLColorMaterial =
1727 cacheGLState.GLColorMaterial;
1728 break;
1729
1730 case(DIFFUSE_CASE):
1731 glState.GLDiffuseNodeId =
1732 cacheGLState.GLDiffuseNodeId;
1733 glState.GLTranspNodeId =
1734 cacheGLState.GLTranspNodeId;
1735 break;
1736
1737 case(AMBIENT_CASE):
1738 for(i=0; i<3; i++)
1739 glState.GLAmbient[i] =
1740 cacheGLState.GLAmbient[i];
1741 break;
1742
1743 case(EMISSIVE_CASE):
1744 for(i=0; i<3; i++)
1745 glState.GLEmissive[i] =
1746 cacheGLState.GLEmissive[i];
1747 break;
1748
1749 case(SPECULAR_CASE):
1750 for(i=0; i<3; i++)
1751 glState.GLSpecular[i] =
1752 cacheGLState.GLSpecular[i];
1753 break;
1754
1755 case(SHININESS_CASE):
1756 glState.GLShininess =
1757 cacheGLState.GLShininess;
1758 break;
1759
1760 case(BLENDING_CASE):
1761 glState.GLblending =
1762 cacheGLState.GLblending;
1763 break;
1764
1765 case(TRANSPARENCY_CASE):
1766 glState.GLStippleNum =
1767 cacheGLState.GLStippleNum;
1768 break;
1769 }
1770 }
1771 }
1772 }
1773 ////////////////////////////////////////////////////////////////////////
1774 //
1775 // Description:
1776 // Copy GL values of specified components into specified
1777 // cacheLazyElement. Invoked by this element and GLRenderCache.
1778 //
1779 // use: SoINTERNAL public
1780 //
1781 ////////////////////////////////////////////////////////////////////////
1782
1783 void
1784 SoGLLazyElement::copyGLValues(uint32_t bitmask,SoGLLazyElement* lazyElt)
1785 {
1786 for(int j=0; (j<SO_LAZY_NUM_COMPONENTS)&&bitmask; j++, bitmask>>=1){
1787
1788 if (bitmask&1){
1789 int i;
1790 switch(j){
1791 case(LIGHT_MODEL_CASE):
1792 lazyElt->glState.GLLightModel = glState.GLLightModel;
1793 break;
1794
1795 case(COLOR_MATERIAL_CASE):
1796 lazyElt->glState.GLColorMaterial = glState.GLColorMaterial;
1797 break;
1798
1799 case(DIFFUSE_CASE):
1800 lazyElt->glState.GLDiffuseNodeId = glState.GLDiffuseNodeId;
1801 lazyElt->glState.GLTranspNodeId = glState.GLTranspNodeId;
1802 break;
1803
1804 case(AMBIENT_CASE):
1805 for(i=0; i<3; i++)
1806 lazyElt->glState.GLAmbient[i]=glState.GLAmbient[i];
1807 break;
1808
1809 case(EMISSIVE_CASE):
1810 for(i=0; i<3; i++)
1811 lazyElt->glState.GLEmissive[i]=glState.GLEmissive[i];
1812 break;
1813
1814 case(SPECULAR_CASE):
1815 for(i=0; i<3; i++)
1816 lazyElt->glState.GLSpecular[i]=glState.GLSpecular[i];
1817 break;
1818
1819 case(SHININESS_CASE):
1820 lazyElt->glState.GLShininess=glState.GLShininess;
1821 break;
1822
1823 case(BLENDING_CASE):
1824 lazyElt->glState.GLblending = glState.GLblending;
1825 break;
1826
1827 case(TRANSPARENCY_CASE):
1828 lazyElt->glState.GLStippleNum = glState.GLStippleNum;
1829 break;
1830 }
1831 }
1832 }
1833 }
1834
1835 ////////////////////////////////////////////////////////////////////////
1836 //
1837 // Description:
1838 // Copy Inventor values of specified components into specified
1839 // cacheLazyElement
1840 //
1841 // use: SoINTERNAL public
1842 //
1843 ////////////////////////////////////////////////////////////////////////
1844 void
1845 SoGLLazyElement::copyIVValues(uint32_t bitmask,SoGLLazyElement* lazyElt)
1846 {
1847 for(int j=0; (j<SO_LAZY_NUM_COMPONENTS)&&bitmask; j++, bitmask>>=1){
1848
1849 if (bitmask&1){
1850 int i;
1851 switch(j){
1852
1853 case(LIGHT_MODEL_CASE):
1854 lazyElt->ivState.lightModel = ivState.lightModel;
1855 break;
1856
1857 case(COLOR_MATERIAL_CASE):
1858 lazyElt->ivState.colorMaterial = ivState.colorMaterial;
1859 break;
1860
1861 case(DIFFUSE_CASE):
1862 lazyElt->ivState.diffuseNodeId = ivState.diffuseNodeId;
1863 lazyElt->ivState.transpNodeId = ivState.transpNodeId;
1864 break;
1865
1866 case(AMBIENT_CASE):
1867 for(i=0; i<3; i++)
1868 lazyElt->ivState.ambientColor[i] =
1869 ivState.ambientColor[i];
1870 break;
1871
1872 case(EMISSIVE_CASE):
1873 for(i=0; i<3; i++)
1874 lazyElt->ivState.emissiveColor[i] =
1875 ivState.emissiveColor[i];
1876 break;
1877
1878 case(SPECULAR_CASE):
1879 for(i=0; i<3; i++)
1880 lazyElt->ivState.specularColor[i] =
1881 ivState.specularColor[i];
1882 break;
1883
1884 case(SHININESS_CASE):
1885 lazyElt->ivState.shininess = ivState.shininess;
1886 break;
1887
1888 case(BLENDING_CASE):
1889 lazyElt->ivState.blending = ivState.blending;
1890 break;
1891
1892 case(TRANSPARENCY_CASE):
1893 lazyElt->ivState.stippleNum = ivState.stippleNum;
1894 break;
1895 }
1896 }
1897 }
1898 }
1899 ////////////////////////////////////////////////////////////////////////
1900 //
1901 // Description:
1902 // Put the current diffuse diffuse color and transparency into a packed
1903 // color array, associated with specified SoColorPacker.
1904 //
1905 // use: private
1906 //
1907 ////////////////////////////////////////////////////////////////////////
1908 void
1909 SoGLLazyElement::packColors(SoColorPacker *cPacker)
1910 {
1911 //First determine if we have enough space:
1912 if (cPacker->getSize() < ivState.numDiffuseColors)
1913 cPacker->reallocate(ivState.numDiffuseColors);
1914
1915 uint32_t *packedArray = cPacker->getPackedColors();
1916 SbBool multTrans = (ivState.numTransparencies >= ivState.numDiffuseColors);
1917 int indx = 0;
1918 uint32_t transp;
1919 for (int i=0; i< ivState.numDiffuseColors; i++){
1920 if (isPacked()){
1921 if (i == 0 || multTrans)
1922 transp = (uint32_t)((1.0 - ivState.transparencies[i])*255.);
1923 packedArray[i] = (ivState.packedColors[i] & 0xffffff00)|
1924 (transp & 0xff);
1925 }
1926 else{
1927 if (multTrans) indx = i;
1928 packedArray[i] = (ivState.diffuseColors + i)->
1929 getPackedValue(ivState.transparencies[indx]);
1930 }
1931
1932 }
1933 cPacker->setNodeIds(ivState.diffuseNodeId, ivState.transpNodeId);
1934 }
1935 ////////////////////////////////////////////////////////////////////////////////
1936 // Data associated with stipple patterns
1937 // Holds defined 32x32 bit stipple patterns. Each is defined as 32
1938 // rows of 4 bytes (32 bits) each.
1939 ////////////////////////////////////////////////////////////////////////////////
1940 u_char SoGLLazyElement::patterns[64+1][32 * 4];
1941
1942 // Indicates whether patterns were created and stored yet
1943 SbBool SoGLLazyElement::patternsCreated = FALSE;
1944
1945 // Holds flags to indicate whether we defined a display list for the
1946 // corresponding stipple pattern
1947 SbBool SoGLLazyElement::patternListDefined[64+1];
1948
1949 // Stores base display list index for patterns. Initialized to -1 to
1950 // indicate that no pattern lists were allocated.
1951 int SoGLLazyElement::patternListBase = -1;
1952
1953 // Stores cache context in which display lists were created. We can't
1954 // call a list from a context other than this one.
1955 int SoGLLazyElement::patternListContext;
1956
1957
1958
1959
1960 ////////////////////////////////////////////////////////////////////////
1961 //
1962 // Description:
1963 // Sends stipple transparency. Assumes it really needs to send it.
1964 //
1965 // Use: private
1966 ////////////////////////////////////////////////////////////////////////
1967 void
1968 SoGLLazyElement::sendStipple(const SoState *state, int patIndex )
1969 {
1970
1971 #ifdef DEBUG
1972 if (patIndex <= 0 || patIndex > getNumPatterns()){
1973 SoDebugError::post("SoGLLazyElement::sendStipple",
1974 "Invalid pattern index");
1975 }
1976 #endif /*DEBUG*/
1977
1978 // Create pattern arrays if not already done
1979 if (! patternsCreated) {
1980 createPatterns();
1981 patternsCreated = TRUE;
1982
1983 // Make sure we know that no patterns were sent
1984 int i;
1985 for (i = 0; i <= getNumPatterns(); i++)
1986 patternListDefined[i] = FALSE;
1987 }
1988
1989 // Determine the current cache context
1990 int currentContext = SoGLCacheContextElement::get((SoState*)state);
1991
1992 // If we already have a display list for this pattern
1993 if (patternListDefined[patIndex]) {
1994
1995 // If the cache context is valid, just call the list
1996 if (currentContext == patternListContext)
1997 glCallList(patternListBase + patIndex);
1998
1999 // Otherwise, just send the pattern
2000 else
2001 glPolygonStipple(patterns[patIndex]);
2002 }
2003
2004 // If we are in the middle of building a cache or the
2005 // context is different from the one in which we allocated
2006 // the display lists, we can't create a new display list,
2007 // so just send the pattern as is
2008 else if (state->isCacheOpen() ||
2009 (patternListBase >= 0 &&
2010 currentContext != patternListContext))
2011 glPolygonStipple(patterns[patIndex]);
2012
2013 // Otherwise, build a display list and send it
2014 else {
2015
2016 // If we haven't allocated the pattern list indices, do so
2017 if (patternListBase < 0) {
2018 patternListBase = (int) glGenLists(getNumPatterns() + 1);
2019 patternListContext = currentContext;
2020 }
2021
2022 // Create and send the list
2023 glNewList(patternListBase + patIndex, GL_COMPILE_AND_EXECUTE);
2024 glPolygonStipple(patterns[patIndex]);
2025 glEndList();
2026
2027 patternListDefined[patIndex] = TRUE;
2028 }
2029
2030 }
2031
2032 /////////////////////////////////////////////////////////////////////////////
2033 //
2034 // Description:
2035 // This fills in the "patterns" array with polygon stipples that
2036 // simulate transparency levels using a standard dither matrix.
2037 // This code was adapted from Foley, van Dam, Feiner, and Hughes,
2038 // pages 569-572.
2039 //
2040 // Use: private, static
2041 //////////////////////////////////////////////////////////////////////////
2042 void
2043 SoGLLazyElement::createPatterns()
2044 {
2045 static short ditherMatrix[8][8] = {
2046 { 0, 32, 8, 40, 2, 34, 10, 42 },
2047 { 48, 16, 56, 24, 50, 18, 58, 26 },
2048 { 12, 44, 4, 36, 14, 46, 6, 38 },
2049 { 60, 28, 52, 20, 62, 30, 54, 22 },
2050 { 3, 35, 11, 43, 1, 33, 9, 41 },
2051 { 51, 19, 59, 27, 49, 17, 57, 25 },
2052 { 15, 47, 7, 39, 13, 45, 5, 37 },
2053 { 63, 31, 55, 23, 61, 29, 53, 21 },
2054 };
2055
2056 u_char pat[8];
2057 int pattern, x, y;
2058
2059 // For each pattern
2060 for (pattern = 0; pattern <= 64; pattern++) {
2061
2062 // Set up an 8x8 pixel pattern in "pat", 1 bit per pixel
2063 for (y = 0; y < 8; y++) {
2064 pat[y] = 0;
2065 for (x = 0; x < 8; x++)
2066 if (ditherMatrix[y][x] >= pattern)
2067 pat[y] |= (1 << (7 - x));
2068 }
2069
2070 // Store the 8x8 pattern in the correct slot in "patterns".
2071 // Since we need a 32x32, replicate the pattern 4 times in
2072 // each dimension.
2073
2074 #define PAT_INDEX(x, y) ((y) * 4 + x)
2075
2076 for (y = 0; y < 8; y++) {
2077 for (x = 0; x < 4; x++) {
2078 patterns[pattern][PAT_INDEX(x, y + 0)] = pat[y];
2079 patterns[pattern][PAT_INDEX(x, y + 8)] = pat[y];
2080 patterns[pattern][PAT_INDEX(x, y + 16)] = pat[y];
2081 patterns[pattern][PAT_INDEX(x, y + 24)] = pat[y];
2082 }
2083 }
2084
2085 #undef PAT_INDEX
2086
2087 }
2088 }