fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / State / OpenGL / OSGTextureEnvChunk.cpp
blobe29ea13e5a13964c7fea175b6a87972a5615d3c9
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
40 // Includes
41 //---------------------------------------------------------------------------
43 #include <cstdlib>
44 #include <cstdio>
46 #include <boost/bind.hpp>
48 #include "OSGConfig.h"
50 #include "OSGGL.h"
51 #include "OSGGLU.h"
52 #include "OSGGLEXT.h"
53 #include "OSGImage.h"
55 #include "OSGDrawEnv.h"
57 #include "OSGTextureEnvChunk.h"
58 #include "OSGTextureBaseChunk.h"
60 //#define OSG_DUMP_TEX
62 OSG_USING_NAMESPACE
64 // Documentation for this class is emited in the
65 // OSGTextureEnvChunkBase.cpp file.
66 // To modify it, please change the .fcd file (OSGTextureEnvChunk.fcd) and
67 // regenerate the base file.
69 /***************************************************************************\
70 * Class variables *
71 \***************************************************************************/
73 StateChunkClass TextureEnvChunk::_class("TextureEnv", osgMaxTexImages, 120);
75 UInt32 TextureEnvChunk::_extPointSprite = Window::invalidExtensionID;
76 UInt32 TextureEnvChunk::_extTextureShader = Window::invalidExtensionID;
77 UInt32 TextureEnvChunk::_extTextureShader2 = Window::invalidExtensionID;
78 UInt32 TextureEnvChunk::_extTextureShader3 = Window::invalidExtensionID;
79 UInt32 TextureEnvChunk::_extTextureLodBias = Window::invalidExtensionID;
81 /***************************************************************************\
82 * Class methods *
83 \***************************************************************************/
85 /*-------------------------------------------------------------------------*\
86 - private -
87 \*-------------------------------------------------------------------------*/
89 void TextureEnvChunk::initMethod(InitPhase ePhase)
91 Inherited::initMethod(ePhase);
93 if(ePhase == TypeObject::SystemPost)
95 _extPointSprite =
96 Window::registerExtension("GL_ARB_point_sprite" );
97 _extTextureShader =
98 Window::registerExtension("GL_NV_texture_shader" );
99 _extTextureShader2 =
100 Window::registerExtension("GL_NV_texture_shader2" );
101 _extTextureShader3 =
102 Window::registerExtension("GL_NV_texture_shader3" );
103 _extTextureLodBias =
104 Window::registerExtension("GL_EXT_texture_lod_bias");
106 Window::registerConstant(GL_MAX_TEXTURE_UNITS_ARB );
107 Window::registerConstant(GL_MAX_TEXTURE_IMAGE_UNITS_ARB);
108 Window::registerConstant(GL_MAX_TEXTURE_COORDS_ARB );
112 /***************************************************************************\
113 * Instance methods *
114 \***************************************************************************/
116 /*-------------------------------------------------------------------------*\
117 - private -
118 \*-------------------------------------------------------------------------*/
121 /*------------- constructors & destructors --------------------------------*/
123 TextureEnvChunk::TextureEnvChunk(void) :
124 Inherited()
128 TextureEnvChunk::TextureEnvChunk(const TextureEnvChunk &source) :
129 Inherited(source)
133 TextureEnvChunk::~TextureEnvChunk(void)
137 /*------------------------- Chunk Class Access ---------------------------*/
139 const StateChunkClass *TextureEnvChunk::getClass(void) const
141 return &_class;
144 /*------------------------------ State ------------------------------------*/
146 /*! Texture handler. Create/update a single texture.
147 Also used by derived CubeMap chunk.
150 void TextureEnvChunk::handleTextureShader(Window *win, GLenum bindtarget)
152 #if !defined(OSG_OGL_COREONLY) || defined(OSG_CHECK_COREONLY)
153 if(!win->hasExtension(_extTextureShader))
155 if(getShaderOperation() != GL_NONE)
156 FINFO(("NV Texture Shaders not supported on Window %p!\n",
157 static_cast<void *>(win)));
158 return;
161 glErr("textureShader precheck");
163 glTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV,
164 getShaderOperation());
166 glErr("textureShader setup: operation");
168 if(getShaderOperation() == GL_NONE)
169 return;
171 if(bindtarget == GL_TEXTURE_3D && !win->hasExtension(_extTextureShader2))
173 FINFO(("NV Texture Shaders 2 not supported on Window %p!\n",
174 static_cast<void *>(win)));
175 return;
178 if(getShaderInput() != GL_NONE)
180 glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV,
181 getShaderInput());
184 glErr("textureShader setup: input");
186 if(getShaderRGBADotProduct() != GL_NONE)
188 glTexEnvi(GL_TEXTURE_SHADER_NV,
189 GL_RGBA_UNSIGNED_DOT_PRODUCT_MAPPING_NV,
190 getShaderRGBADotProduct());
193 glErr("textureShader setup: rgba dotprod");
195 if(getMFShaderOffsetMatrix()->size() == 4)
197 glTexEnvfv(GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_MATRIX_NV,
198 &((*getMFShaderOffsetMatrix())[0]));
200 glErr("textureShader setup: offset matrix");
202 else if(getMFShaderOffsetMatrix()->size() != 0)
204 FWARNING(("TextureEnvChunk::handleTextureShader: shaderOffsetMatrix "
205 "has to have 4 entries, not %" PRISize "!\n",
206 getMFShaderOffsetMatrix()->size() ));
209 glTexEnvf(GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_SCALE_NV,
210 getShaderOffsetScale());
212 glErr("textureShader setup: offset scale");
214 glTexEnvf(GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_BIAS_NV,
215 getShaderOffsetBias());
217 glErr("textureShader setup: offset bias");
219 GLint cullmodes[4];
221 if(getShaderCullModes() & 0x1)
223 cullmodes[0] = GL_GEQUAL;
225 else
227 cullmodes[0] = GL_LESS;
230 if(getShaderCullModes() & 0x2)
232 cullmodes[1] = GL_GEQUAL;
234 else
236 cullmodes[1] = GL_LESS;
239 if(getShaderCullModes() & 0x4)
241 cullmodes[2] = GL_GEQUAL;
243 else
245 cullmodes[2] = GL_LESS;
248 if(getShaderCullModes() & 0x8)
250 cullmodes[3] = GL_GEQUAL;
252 else
254 cullmodes[3] = GL_LESS;
257 glTexEnviv(GL_TEXTURE_SHADER_NV, GL_CULL_MODES_NV,
258 cullmodes);
260 glErr("textureShader setup: cull modes");
262 glTexEnvfv(GL_TEXTURE_SHADER_NV, GL_CONST_EYE_NV,
263 getShaderConstEye().getValues());
265 glErr("textureShader setup: const eye");
269 #ifdef OSG_DEBUG
270 GLint consistent;
271 glGetTexEnviv(GL_TEXTURE_SHADER_NV, GL_SHADER_CONSISTENT_NV,
272 &consistent);
273 if(!consistent)
275 FWARNING(("Texture shaders not consistent!\n"));
277 #endif
278 #endif
281 /*------------------------------- Sync -----------------------------------*/
283 /*! React to field changes.
284 Note: this function also handles CubeTexture changes, make sure to keep
285 it consistent with the cubeTexture specifics
288 void TextureEnvChunk::changed(ConstFieldMaskArg whichField,
289 UInt32 origin,
290 BitVector details)
292 Inherited::changed(whichField, origin, details);
295 bool TextureEnvChunk::isTransparent(void) const
297 // Even if the texture has alpha, the Blending is makes the sorting
298 // important, thus textures per se are not transparent
299 return false;
303 /*----------------------------- onCreate --------------------------------*/
305 void TextureEnvChunk::onCreate(const TextureEnvChunk *source)
307 Inherited::onCreate(source);
310 void TextureEnvChunk::onCreateAspect(const TextureEnvChunk *createAspect,
311 const TextureEnvChunk *source )
313 Inherited::onCreateAspect(createAspect, source);
316 /*------------------------------ Output ----------------------------------*/
318 void TextureEnvChunk::dump( UInt32 uiIndent,
319 const BitVector bvFlags ) const
321 Inherited::dump(uiIndent, bvFlags);
323 if((bvFlags & EnvModeFieldMask) != 0)
325 indentLog(uiIndent, PLOG);
326 PLOG << "envMode "
327 << GLDefineMapper::the()->toString(_sfEnvMode.getValue())
328 << "\n";
331 if((bvFlags & EnvColorFieldMask) != 0)
333 indentLog(uiIndent, PLOG);
334 PLOG << "envColor " << _sfEnvColor.getValue() << "\n";
337 if((bvFlags & EnvCombineRGBFieldMask) != 0)
339 indentLog(uiIndent, PLOG);
340 PLOG << "envCombineRGB "
341 << GLDefineMapper::the()->toString(_sfEnvCombineRGB.getValue())
342 << "\n";
345 if((bvFlags & EnvCombineAlphaFieldMask) != 0)
347 indentLog(uiIndent, PLOG);
348 PLOG << "envCombineAlpha "
349 << GLDefineMapper::the()->toString(_sfEnvCombineAlpha.getValue())
350 << "\n";
353 if((bvFlags & EnvScaleRGBFieldMask) != 0)
355 indentLog(uiIndent, PLOG);
356 PLOG << "envScaleRGB "
357 << _sfEnvScaleRGB.getValue()
358 << "\n";
361 if((bvFlags & EnvScaleAlphaFieldMask) != 0)
363 indentLog(uiIndent, PLOG);
364 PLOG << "envScaleAlpha "
365 << _sfEnvScaleAlpha.getValue()
366 << "\n";
369 if((bvFlags & EnvSource0RGBFieldMask) != 0)
371 indentLog(uiIndent, PLOG);
372 PLOG << "envSource0RGB "
373 << GLDefineMapper::the()->toString(_sfEnvSource0RGB.getValue())
374 << "\n";
377 if((bvFlags & EnvSource1RGBFieldMask) != 0)
379 indentLog(uiIndent, PLOG);
380 PLOG << "envSource1RGB "
381 << GLDefineMapper::the()->toString(_sfEnvSource1RGB.getValue())
382 << "\n";
385 if((bvFlags & EnvSource2RGBFieldMask) != 0)
387 indentLog(uiIndent, PLOG);
388 PLOG << "envSource2RGB "
389 << GLDefineMapper::the()->toString(_sfEnvSource2RGB.getValue())
390 << "\n";
393 if((bvFlags & EnvSource0AlphaFieldMask) != 0)
395 indentLog(uiIndent, PLOG);
396 PLOG << "envSource0Alpha "
397 << GLDefineMapper::the()->toString(_sfEnvSource0Alpha.getValue())
398 << "\n";
401 if((bvFlags & EnvSource1AlphaFieldMask) != 0)
403 indentLog(uiIndent, PLOG);
404 PLOG << "envSource1Alpha "
405 << GLDefineMapper::the()->toString(_sfEnvSource1Alpha.getValue())
406 << "\n";
409 if((bvFlags & EnvSource2AlphaFieldMask) != 0)
411 indentLog(uiIndent, PLOG);
412 PLOG << "envSource2Alpha "
413 << GLDefineMapper::the()->toString(_sfEnvSource2Alpha.getValue())
414 << "\n";
417 if((bvFlags & EnvOperand0RGBFieldMask) != 0)
419 indentLog(uiIndent, PLOG);
420 PLOG << "envOperand0RGB "
421 << GLDefineMapper::the()->toString(_sfEnvOperand0RGB.getValue())
422 << "\n";
425 if((bvFlags & EnvOperand1RGBFieldMask) != 0)
427 indentLog(uiIndent, PLOG);
428 PLOG << "envOperand1RGB "
429 << GLDefineMapper::the()->toString(_sfEnvOperand1RGB.getValue())
430 << "\n";
433 if((bvFlags & EnvOperand2RGBFieldMask) != 0)
435 indentLog(uiIndent, PLOG);
436 PLOG << "envOperand2RGB "
437 << GLDefineMapper::the()->toString(_sfEnvOperand2RGB.getValue())
438 << "\n";
441 if((bvFlags & EnvOperand0AlphaFieldMask) != 0)
443 indentLog(uiIndent, PLOG);
444 PLOG << "envOperand0Alpha "
445 << GLDefineMapper::the()->toString(_sfEnvOperand0Alpha.getValue())
446 << "\n";
449 if((bvFlags & EnvOperand1AlphaFieldMask) != 0)
451 indentLog(uiIndent, PLOG);
452 PLOG << "envOperand1Alpha "
453 << GLDefineMapper::the()->toString(_sfEnvOperand1Alpha.getValue())
454 << "\n";
457 if((bvFlags & EnvOperand2AlphaFieldMask) != 0)
459 indentLog(uiIndent, PLOG);
460 PLOG << "envOperand2Alpha "
461 << GLDefineMapper::the()->toString(_sfEnvOperand2Alpha.getValue())
462 << "\n";
465 if((bvFlags & PointSpriteFieldMask) != 0)
467 indentLog(uiIndent, PLOG);
468 PLOG << "pointSprite " << _sfPointSprite.getValue() << "\n";
471 if((bvFlags & ShaderOperationFieldMask) != 0)
473 indentLog(uiIndent, PLOG);
474 PLOG << "shaderOperationAlpha "
475 << GLDefineMapper::the()->toString(_sfShaderOperation.getValue())
476 << "\n";
479 if((bvFlags & ShaderInputFieldMask) != 0)
481 indentLog(uiIndent, PLOG);
482 PLOG << "shaderInputAlpha "
483 << GLDefineMapper::the()->toString(_sfShaderInput.getValue())
484 << "\n";
487 if((bvFlags & ShaderOffsetMatrixFieldMask) != 0)
489 indentLog(uiIndent, PLOG);
490 PLOG << "shaderOffsetMatrix " << "NIY" << "\n";
493 if((bvFlags & ShaderOffsetScaleFieldMask) != 0)
495 indentLog(uiIndent, PLOG);
496 PLOG << "shaderOffsetScale " << _sfShaderOffsetScale.getValue() << "\n";
499 if((bvFlags & ShaderOffsetBiasFieldMask) != 0)
501 indentLog(uiIndent, PLOG);
502 PLOG << "shaderOffsetBias " << _sfShaderOffsetBias.getValue() << "\n";
505 if((bvFlags & ShaderRGBADotProductFieldMask) != 0)
507 indentLog(uiIndent, PLOG);
508 PLOG << "shaderRGBADotProduct "
509 << GLDefineMapper::the()->toString(_sfShaderRGBADotProduct.getValue())
510 << "\n";
513 if((bvFlags & ShaderCullModesFieldMask) != 0)
515 indentLog(uiIndent, PLOG);
516 PLOG << "shaderCullModes " << _sfShaderCullModes.getValue() << "\n";
519 if((bvFlags & ShaderConstEyeFieldMask) != 0)
521 indentLog(uiIndent, PLOG);
522 PLOG << "shaderConstEye " << _sfShaderConstEye.getValue() << "\n";
525 if((bvFlags & LodBiasFieldMask) != 0)
527 indentLog(uiIndent, PLOG);
528 PLOG << "lodBias " << _sfLodBias.getValue() << "\n";
533 /*------------------------------ State ------------------------------------*/
536 void TextureEnvChunk::activate(DrawEnv *pEnv, UInt32 idx)
538 #if !defined(OSG_OGL_COREONLY) || defined(OSG_CHECK_COREONLY)
539 pEnv->incNumChunkChanges();
541 Window *win = pEnv->getWindow();
543 Real32 nteximages, ntexcoords;
545 if((nteximages = win->getConstantValue(GL_MAX_TEXTURE_IMAGE_UNITS_ARB)) ==
546 Window::unknownConstant)
548 nteximages = win->getConstantValue(GL_MAX_TEXTURE_UNITS_ARB);
550 // sgi doesn't support GL_MAX_TEXTURE_UNITS_ARB!
551 if(nteximages == Window::unknownConstant)
552 nteximages = 1.0f;
555 if((ntexcoords = win->getConstantValue(GL_MAX_TEXTURE_COORDS_ARB)) ==
556 Window::unknownConstant)
558 ntexcoords = win->getConstantValue(GL_MAX_TEXTURE_UNITS_ARB);
560 // sgi doesn't support GL_MAX_TEXTURE_UNITS_ARB!
561 if(ntexcoords == Window::unknownConstant)
562 ntexcoords = 1.0f;
565 if(idx >= static_cast<UInt32>(nteximages))
567 #ifdef OSG_DEBUG
568 FWARNING(("TextureEnvChunk::activate: Trying to bind image unit %d,"
569 " but Window %p only supports %lf!\n",
570 idx,
571 static_cast<void *>(win),
572 nteximages));
573 #endif
574 return;
577 if(TextureBaseChunk::activateTexture(win, idx) == true)
578 return;
580 #ifdef GL_ARB_point_sprite
581 if(getPointSprite() && win->hasExtOrVersion(_extPointSprite, 0x0200))
583 if(idx < static_cast<UInt32>(ntexcoords))
585 glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
588 #endif
590 if(getLodBias() != 0.0f && win->hasExtOrVersion(_extTextureLodBias, 0x0104))
592 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
593 GL_TEXTURE_LOD_BIAS_EXT,
594 getLodBias());
597 Real32 ntexunits = win->getConstantValue(GL_MAX_TEXTURE_UNITS_ARB);
599 // sgi doesn't support GL_MAX_TEXTURE_UNITS_ARB!
600 if(ntexunits == Window::unknownConstant)
601 ntexunits = 1.0f;
603 if(idx < static_cast<UInt32>(ntexunits))
605 // texture env
606 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, getEnvMode());
608 glTexEnvfv(GL_TEXTURE_ENV,
609 GL_TEXTURE_ENV_COLOR,
610 getEnvColor().getValuesRGBA());
612 if(getEnvMode() == GL_COMBINE_EXT)
614 glTexEnvi(GL_TEXTURE_ENV,
615 GL_COMBINE_RGB_EXT,
616 getEnvCombineRGB ());
618 glTexEnvf(GL_TEXTURE_ENV,
619 GL_RGB_SCALE_EXT,
620 getEnvScaleRGB ());
622 glTexEnvi(GL_TEXTURE_ENV,
623 GL_SOURCE0_RGB_EXT,
624 getEnvSource0RGB ());
626 glTexEnvi(GL_TEXTURE_ENV,
627 GL_SOURCE1_RGB_EXT,
628 getEnvSource1RGB ());
630 glTexEnvi(GL_TEXTURE_ENV,
631 GL_SOURCE2_RGB_EXT,
632 getEnvSource2RGB ());
634 glTexEnvi(GL_TEXTURE_ENV,
635 GL_OPERAND0_RGB_EXT,
636 getEnvOperand0RGB());
638 glTexEnvi(GL_TEXTURE_ENV,
639 GL_OPERAND1_RGB_EXT,
640 getEnvOperand1RGB());
642 glTexEnvi(GL_TEXTURE_ENV,
643 GL_OPERAND2_RGB_EXT,
644 getEnvOperand2RGB());
647 glTexEnvi(GL_TEXTURE_ENV,
648 GL_COMBINE_ALPHA_EXT,
649 getEnvCombineAlpha ());
651 glTexEnvf(GL_TEXTURE_ENV,
652 GL_ALPHA_SCALE,
653 getEnvScaleAlpha ());
655 glTexEnvi(GL_TEXTURE_ENV,
656 GL_SOURCE0_ALPHA_EXT,
657 getEnvSource0Alpha ());
659 glTexEnvi(GL_TEXTURE_ENV,
660 GL_SOURCE1_ALPHA_EXT,
661 getEnvSource1Alpha ());
663 glTexEnvi(GL_TEXTURE_ENV,
664 GL_SOURCE2_ALPHA_EXT,
665 getEnvSource2Alpha ());
667 glTexEnvi(GL_TEXTURE_ENV,
668 GL_OPERAND0_ALPHA_EXT,
669 getEnvOperand0Alpha());
671 glTexEnvi(GL_TEXTURE_ENV,
672 GL_OPERAND1_ALPHA_EXT,
673 getEnvOperand1Alpha());
675 glTexEnvi(GL_TEXTURE_ENV,
676 GL_OPERAND2_ALPHA_EXT,
677 getEnvOperand2Alpha());
680 TextureEnvChunk::handleTextureShader(
681 win,
682 pEnv->getActiveTexTarget(idx));
684 if(idx == 0 &&
685 getShaderOperation() != GL_NONE &&
686 win->hasExtension(_extTextureShader) == true )
688 glEnable(GL_TEXTURE_SHADER_NV);
691 #else
692 OSG_ASSERT(false);
693 #endif
695 glErr("TextureEnvChunk::activate");
698 void TextureEnvChunk::changeFrom(DrawEnv *pEnv,
699 StateChunk *old ,
700 UInt32 idx )
702 #if !defined(OSG_OGL_COREONLY) || defined(OSG_CHECK_COREONLY)
703 // change from me to me?
704 // this assumes I haven't changed in the meantime.
705 // is that a valid assumption?
706 if(old == this)
707 return;
709 pEnv->incNumChunkChanges();
711 TextureEnvChunk *oldp = dynamic_cast<TextureEnvChunk *>(old);
713 glErr("TextureEnvChunk::changeFrom precheck");
715 Window *win = pEnv->getWindow();
717 Real32 nteximages, ntexcoords;
719 if((nteximages = win->getConstantValue(GL_MAX_TEXTURE_IMAGE_UNITS_ARB)) ==
720 Window::unknownConstant)
722 nteximages = win->getConstantValue(GL_MAX_TEXTURE_UNITS_ARB);
724 // sgi doesn't support GL_MAX_TEXTURE_UNITS_ARB!
725 if(nteximages == Window::unknownConstant)
726 nteximages = 1.0f;
729 if((ntexcoords = win->getConstantValue(GL_MAX_TEXTURE_COORDS_ARB)) ==
730 Window::unknownConstant)
732 ntexcoords = win->getConstantValue(GL_MAX_TEXTURE_UNITS_ARB);
734 // sgi doesn't support GL_MAX_TEXTURE_UNITS_ARB!
735 if(ntexcoords == Window::unknownConstant)
736 ntexcoords = 1.0f;
739 if(idx >= nteximages)
741 #ifdef OSG_DEBUG
742 FWARNING(("TextureEnvChunk::changeFrom: Trying to bind image unit %d,"
743 " but Window %p only supports %lf!\n",
744 idx,
745 static_cast<void *>(win),
746 nteximages));
747 #endif
748 return;
751 if(TextureBaseChunk::activateTexture(win, idx))
752 return; // trying to use too many textures
754 #ifdef GL_ARB_point_sprite
755 if(idx < ntexcoords)
757 if(oldp->getPointSprite() != getPointSprite() &&
758 win->hasExtOrVersion(_extPointSprite, 0x0200))
760 glTexEnvi(GL_POINT_SPRITE_ARB,
761 GL_COORD_REPLACE_ARB,
762 getPointSprite());
765 #endif
767 if(oldp->getLodBias() != getLodBias() &&
768 win->hasExtOrVersion(_extTextureLodBias, 0x0104))
770 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
771 GL_TEXTURE_LOD_BIAS_EXT,
772 getLodBias());
775 Real32 ntexunits = win->getConstantValue(GL_MAX_TEXTURE_UNITS_ARB);
777 // sgi doesn't support GL_MAX_TEXTURE_UNITS_ARB!
778 if(ntexunits == Window::unknownConstant)
779 ntexunits = 1.0f;
781 if(idx < ntexunits)
783 if(oldp->getEnvMode() != getEnvMode())
785 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, getEnvMode());
788 glTexEnvfv(GL_TEXTURE_ENV,
789 GL_TEXTURE_ENV_COLOR,
790 getEnvColor().getValuesRGBA());
792 if(getEnvMode() == GL_COMBINE_EXT)
794 glTexEnvi(GL_TEXTURE_ENV,
795 GL_COMBINE_RGB_EXT,
796 getEnvCombineRGB ());
798 glTexEnvf(GL_TEXTURE_ENV,
799 GL_RGB_SCALE_EXT,
800 getEnvScaleRGB ());
802 glTexEnvi(GL_TEXTURE_ENV,
803 GL_SOURCE0_RGB_EXT,
804 getEnvSource0RGB ());
806 glTexEnvi(GL_TEXTURE_ENV,
807 GL_SOURCE1_RGB_EXT,
808 getEnvSource1RGB ());
810 glTexEnvi(GL_TEXTURE_ENV,
811 GL_SOURCE2_RGB_EXT,
812 getEnvSource2RGB ());
814 glTexEnvi(GL_TEXTURE_ENV,
815 GL_OPERAND0_RGB_EXT,
816 getEnvOperand0RGB());
818 glTexEnvi(GL_TEXTURE_ENV,
819 GL_OPERAND1_RGB_EXT,
820 getEnvOperand1RGB());
822 glTexEnvi(GL_TEXTURE_ENV,
823 GL_OPERAND2_RGB_EXT,
824 getEnvOperand2RGB());
828 glTexEnvi(GL_TEXTURE_ENV,
829 GL_COMBINE_ALPHA_EXT,
830 getEnvCombineAlpha ());
832 glTexEnvf(GL_TEXTURE_ENV,
833 GL_ALPHA_SCALE,
834 getEnvScaleAlpha ());
836 glTexEnvi(GL_TEXTURE_ENV,
837 GL_SOURCE0_ALPHA_EXT,
838 getEnvSource0Alpha ());
840 glTexEnvi(GL_TEXTURE_ENV,
841 GL_SOURCE1_ALPHA_EXT,
842 getEnvSource1Alpha ());
844 glTexEnvi(GL_TEXTURE_ENV,
845 GL_SOURCE2_ALPHA_EXT,
846 getEnvSource2Alpha ());
848 glTexEnvi(GL_TEXTURE_ENV,
849 GL_OPERAND0_ALPHA_EXT,
850 getEnvOperand0Alpha());
852 glTexEnvi(GL_TEXTURE_ENV,
853 GL_OPERAND1_ALPHA_EXT,
854 getEnvOperand1Alpha());
856 glTexEnvi(GL_TEXTURE_ENV,
857 GL_OPERAND2_ALPHA_EXT,
858 getEnvOperand2Alpha());
861 if(win->hasExtension(_extTextureShader))
863 if( getShaderOperation() != GL_NONE &&
864 oldp->getShaderOperation() == GL_NONE )
866 handleTextureShader(win,
867 pEnv->getActiveTexTarget(idx));
869 if(idx == 0)
870 glEnable(GL_TEXTURE_SHADER_NV);
872 else if( getShaderOperation() == GL_NONE &&
873 oldp->getShaderOperation() != GL_NONE )
875 glTexEnvi(GL_TEXTURE_SHADER_NV,
876 GL_SHADER_OPERATION_NV,
877 GL_NONE);
879 if(idx == 0)
881 glDisable(GL_TEXTURE_SHADER_NV);
886 #else
887 OSG_ASSERT(false);
888 #endif
890 glErr("TextureEnvChunk::changeFrom");
894 void TextureEnvChunk::deactivate(DrawEnv *pEnv, UInt32 idx)
896 #if !defined(OSG_OGL_COREONLY) || defined(OSG_CHECK_COREONLY)
897 Window *win = pEnv->getWindow();
899 Real32 nteximages, ntexcoords;
901 if((nteximages = win->getConstantValue(GL_MAX_TEXTURE_IMAGE_UNITS_ARB)) ==
902 Window::unknownConstant )
904 nteximages = win->getConstantValue(GL_MAX_TEXTURE_UNITS_ARB);
906 // sgi doesn't support GL_MAX_TEXTURE_UNITS_ARB!
907 if(nteximages == Window::unknownConstant)
908 nteximages = 1.0f;
911 if((ntexcoords = win->getConstantValue(GL_MAX_TEXTURE_COORDS_ARB)) ==
912 Window::unknownConstant )
914 ntexcoords = win->getConstantValue(GL_MAX_TEXTURE_UNITS_ARB);
916 // sgi doesn't support GL_MAX_TEXTURE_UNITS_ARB!
917 if(ntexcoords == Window::unknownConstant)
918 ntexcoords = 1.0f;
921 if(idx >= static_cast<UInt32>(nteximages))
923 #ifdef OSG_DEBUG
924 FWARNING(("CubeTextureEnvChunk::deactivate: Trying to bind image unit "
925 "%d, but Window %p only supports %lf!\n",
926 idx,
927 static_cast<void *>(win),
928 nteximages));
929 #endif
930 return;
933 bool isActive = false;
935 #ifdef GL_ARB_point_sprite
936 if(getPointSprite() &&
937 win->hasExtOrVersion(_extPointSprite, 0x0200) &&
938 idx < static_cast<UInt32>(ntexcoords) )
940 if(!isActive)
942 TextureBaseChunk::activateTexture(win, idx);
943 isActive = true;
946 glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_FALSE);
948 #endif
950 if(getLodBias() != 0.0f && win->hasExtOrVersion(_extTextureLodBias, 0x0104))
952 if(!isActive)
954 TextureBaseChunk::activateTexture(win, idx);
955 isActive = true;
958 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
959 GL_TEXTURE_LOD_BIAS_EXT,
960 0.0f);
964 Real32 ntexunits = win->getConstantValue(GL_MAX_TEXTURE_UNITS_ARB);
966 if(ntexunits == Window::unknownConstant)
967 ntexunits = 1.0f;
969 if(idx >= static_cast<UInt32>(ntexunits))
971 return; // tetxures >= MTU are not enabled and don't have an env
974 if(!isActive)
975 TextureBaseChunk::activateTexture(win, idx);
978 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
980 glTexEnvfv(GL_TEXTURE_ENV,
981 GL_TEXTURE_ENV_COLOR,
982 Vec4f::Null.getValues());
984 if(getShaderOperation() != GL_NONE &&
985 win->hasExtension(_extTextureShader))
987 glTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_NONE);
989 if(idx == 0)
990 glDisable(GL_TEXTURE_SHADER_NV);
992 #else
993 OSG_ASSERT(false);
994 #endif
996 glErr("TextureEnvChunk::deactivate");
999 /*-------------------------- Comparison -----------------------------------*/
1001 Real32 TextureEnvChunk::switchCost(StateChunk *OSG_CHECK_ARG(chunk))
1003 return 0;
1006 bool TextureEnvChunk::operator < (const StateChunk &other) const
1008 return this < &other;
1011 bool TextureEnvChunk::operator == (const StateChunk &other) const
1013 TextureEnvChunk const *tother =
1014 dynamic_cast<TextureEnvChunk const*>(&other);
1016 if(!tother)
1017 return false;
1019 if(tother == this)
1020 return true;
1022 bool returnValue =
1023 getEnvMode () == tother->getEnvMode ();
1025 if(returnValue == true && getEnvMode() == GL_COMBINE_EXT)
1027 returnValue =
1028 getEnvCombineRGB () == tother->getEnvCombineRGB () &&
1030 getEnvSource0RGB () == tother->getEnvSource0RGB () &&
1031 getEnvSource1RGB () == tother->getEnvSource1RGB () &&
1032 getEnvSource2RGB () == tother->getEnvSource2RGB () &&
1034 getEnvOperand0RGB() == tother->getEnvOperand0RGB () &&
1035 getEnvOperand1RGB() == tother->getEnvOperand1RGB () &&
1036 getEnvOperand2RGB() == tother->getEnvOperand2RGB () &&
1038 getEnvCombineAlpha () == tother->getEnvCombineAlpha () &&
1040 getEnvSource0Alpha () == tother->getEnvSource0Alpha () &&
1041 getEnvSource1Alpha () == tother->getEnvSource1Alpha () &&
1042 getEnvSource2Alpha () == tother->getEnvSource2Alpha () &&
1044 getEnvOperand0Alpha() == tother->getEnvOperand0Alpha() &&
1045 getEnvOperand1Alpha() == tother->getEnvOperand1Alpha() &&
1046 getEnvOperand2Alpha() == tother->getEnvOperand2Alpha();
1049 returnValue &=
1050 (( getEnvScaleRGB () - tother->getEnvScaleRGB ()) <
1051 TypeTraits<Real32>::getDefaultEps() ) &&
1052 ((tother->getEnvScaleRGB () - getEnvScaleRGB ()) <
1053 TypeTraits<Real32>::getDefaultEps() ) &&
1054 (( getEnvScaleAlpha() - tother->getEnvScaleAlpha()) <
1055 TypeTraits<Real32>::getDefaultEps() ) &&
1056 ((tother->getEnvScaleAlpha() - getEnvScaleAlpha()) <
1057 TypeTraits<Real32>::getDefaultEps() );
1060 return returnValue;
1063 bool TextureEnvChunk::operator != (const StateChunk &other) const
1065 return ! (*this == other);