OSGTextureTransformChunk.cpp

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *             Copyright (C) 2000-2002 by the OpenSG Forum                   *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037 \*---------------------------------------------------------------------------*/
00038 
00039 //---------------------------------------------------------------------------
00040 //  Includes
00041 //---------------------------------------------------------------------------
00042 
00043 #include <cstdlib>
00044 #include <cstdio>
00045 
00046 #include <OSGConfig.h>
00047 
00048 #include <OSGGL.h>
00049 
00050 #include "OSGTextureTransformChunk.h"
00051 #include <OSGWindow.h>
00052 #include <OSGCamera.h>
00053 #include <OSGViewport.h>
00054 #include <OSGTextureBaseChunk.h>
00055 #include <OSGDrawEnv.h>
00056 
00057 OSG_USING_NAMESPACE
00058 
00059 // Documentation for this class is emited in the
00060 // OSGTextureTransformChunkBase.cpp file.
00061 // To modify it, please change the .fcd file (OSGTextureTransformChunk.fcd) and
00062 // regenerate the base file.
00063 
00064 /***************************************************************************\
00065  *                           Class variables                               *
00066 \***************************************************************************/
00067 
00068 StateChunkClass TextureTransformChunk::_class("TextureTransform",
00069                                               osgMaxTexCoords);
00070 
00071 /***************************************************************************\
00072  *                           Class methods                                 *
00073 \***************************************************************************/
00074 
00075 void TextureTransformChunk::initMethod(InitPhase ePhase)
00076 {
00077     Inherited::initMethod(ePhase);
00078 }
00079 
00080 /***************************************************************************\
00081  *                           Instance methods                              *
00082 \***************************************************************************/
00083 
00084 /*-------------------------------------------------------------------------*\
00085  -  private                                                                 -
00086 \*-------------------------------------------------------------------------*/
00087 
00088 TextureTransformChunk::TextureTransformChunk(void) :
00089     Inherited()
00090 {
00091 }
00092 
00093 TextureTransformChunk::TextureTransformChunk(
00094     const TextureTransformChunk &source) :
00095 
00096     Inherited(source)
00097 {
00098 }
00099 
00100 TextureTransformChunk::~TextureTransformChunk(void)
00101 {
00102 }
00103 
00104 /*------------------------- Chunk Class Access ---------------------------*/
00105 
00106 const StateChunkClass *TextureTransformChunk::getClass(void) const
00107 {
00108     return &_class;
00109 }
00110 
00111 /*------------------------------- Sync -----------------------------------*/
00112 
00113 void TextureTransformChunk::changed(ConstFieldMaskArg whichField, 
00114                                     UInt32            origin,
00115                                     BitVector         details)
00116 {
00117     Inherited::changed(whichField, origin, details);
00118 }
00119 
00120 /*------------------------------ Output ----------------------------------*/
00121 
00122 void TextureTransformChunk::dump(      UInt32    , 
00123                                  const BitVector ) const
00124 {
00125     SLOG << "Dump TextureTransformChunk NI" << std::endl;
00126 }
00127 
00128 /*------------------------------ State ------------------------------------*/
00129 
00130 void TextureTransformChunk::activate(DrawEnv *pEnv, UInt32 idx)
00131 {
00132     Window *win = pEnv->getWindow();
00133     
00134     Real32 ntexcoords;
00135     if((ntexcoords = win->getConstantValue(GL_MAX_TEXTURE_COORDS_ARB)) ==
00136        Window::unknownConstant
00137       )
00138     {
00139         ntexcoords = win->getConstantValue(GL_MAX_TEXTURE_UNITS_ARB);
00140         // sgi doesn't support GL_MAX_TEXTURE_UNITS_ARB!
00141         if(ntexcoords == Window::unknownConstant)
00142             ntexcoords = 1.0f;
00143     }
00144 
00145     if(idx >= static_cast<UInt32>(ntexcoords))
00146     {
00147 #ifdef OSG_DEBUG
00148         FWARNING(("TextureTransformChunk::deactivate: Trying to bind texcoord unit %d,"
00149                   " but Window %p only supports %d!\n",
00150                   idx, win, ntexcoords));
00151 #endif
00152         return;        
00153     }
00154 
00155     TextureBaseChunk::activateTexture(win, idx);
00156   
00157     glMatrixMode(GL_TEXTURE);
00158 
00159     if(getUseCameraBeacon())
00160     {
00161         Matrix m = pEnv->getCameraToWorld();
00162         
00163         m[3].setValues(0, 0, 0, 1);
00164         glLoadMatrixf(m.getValues());
00165     }
00166     else
00167     {
00168         glLoadMatrixf(getMatrix().getValues());
00169     }
00170     glMatrixMode(GL_MODELVIEW);
00171 }
00172 
00173 void TextureTransformChunk::changeFrom(DrawEnv    *pEnv, 
00174                                        StateChunk *old, 
00175                                        UInt32      idx )
00176 {
00177     // change from me to me?
00178     // this assumes I haven't changed in the meantime. is that a valid assumption?
00179     if ( old == this )
00180         return;
00181 
00182     Window *win = pEnv->getWindow();
00183     
00184     Real32 ntexcoords;
00185     if((ntexcoords = win->getConstantValue(GL_MAX_TEXTURE_COORDS_ARB)) ==
00186        Window::unknownConstant
00187       )
00188     {
00189         ntexcoords = win->getConstantValue(GL_MAX_TEXTURE_UNITS_ARB);
00190         // sgi doesn't support GL_MAX_TEXTURE_UNITS_ARB!
00191         if(ntexcoords == Window::unknownConstant)
00192             ntexcoords = 1.0f;
00193     }
00194 
00195     if(idx >= static_cast<UInt32>(ntexcoords))
00196     {
00197 #ifdef OSG_DEBUG
00198         FWARNING(("TextureTransformChunk::deactivate: Trying to bind texcoord unit %d,"
00199                   " but Window %p only supports %d!\n",
00200                   idx, win, ntexcoords));
00201 #endif
00202         return;        
00203     }
00204 
00205     TextureBaseChunk::activateTexture(win, idx);
00206 
00207     glMatrixMode(GL_TEXTURE);
00208     if(getUseCameraBeacon())
00209     {
00210         Matrix m = pEnv->getCameraToWorld();
00211 
00212         m[3].setValues(0, 0, 0, 1);
00213         glLoadMatrixf(m.getValues());
00214     }
00215     else
00216     {
00217         glLoadMatrixf(getMatrix().getValues());
00218     }
00219     glMatrixMode(GL_MODELVIEW);
00220 }
00221 
00222 void TextureTransformChunk::deactivate(DrawEnv *pEnv, UInt32 idx)
00223 {
00224     Window *win = pEnv->getWindow();   
00225 
00226     Real32 ntexcoords;
00227     if((ntexcoords = win->getConstantValue(GL_MAX_TEXTURE_COORDS_ARB)) ==
00228        Window::unknownConstant
00229       )
00230     {
00231         ntexcoords = win->getConstantValue(GL_MAX_TEXTURE_UNITS_ARB);
00232         // sgi doesn't support GL_MAX_TEXTURE_UNITS_ARB!
00233         if(ntexcoords == Window::unknownConstant)
00234             ntexcoords = 1.0f;
00235     }
00236 
00237     if(idx >= static_cast<UInt32>(ntexcoords))
00238     {
00239 #ifdef OSG_DEBUG
00240         FWARNING(("TextureTransformChunk::deactivate: Trying to bind texcoord unit %d,"
00241                   " but Window %p only supports %d!\n",
00242                   idx, win, ntexcoords));
00243 #endif
00244         return;        
00245     }
00246 
00247     TextureBaseChunk::activateTexture(win, idx);
00248 
00249     glMatrixMode(GL_TEXTURE);
00250     glLoadIdentity();
00251     glMatrixMode(GL_MODELVIEW);
00252 }
00253 
00254 
00255 /*-------------------------- Comparison -----------------------------------*/
00256 
00257 Real32 TextureTransformChunk::switchCost(StateChunk *OSG_CHECK_ARG(chunk))
00258 {
00259     return 0;
00260 }
00261 
00262 bool TextureTransformChunk::operator <(const StateChunk &other) const
00263 {
00264     return this < &other;
00265 }
00266 
00267 bool TextureTransformChunk::operator ==(const StateChunk &other) const
00268 {
00269     TextureTransformChunk const *tother = 
00270         dynamic_cast<TextureTransformChunk const*>(&other);
00271 
00272     if(!tother)
00273         return false;
00274 
00275     if(tother == this)
00276         return true;
00277 
00278     if(getMatrix() != tother->getMatrix())
00279         return false;
00280 
00281     return true;
00282 }
00283 
00284 bool TextureTransformChunk::operator !=(const StateChunk &other) const
00285 {
00286     return !(*this == other);
00287 }