Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

OSGTextureBackground.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 <stdlib.h>
00044 #include <stdio.h>
00045 
00046 #include <OSGConfig.h>
00047 #include <OSGDrawActionBase.h>
00048 #include <OSGViewport.h>
00049 #include <OSGTextureChunk.h>
00050 
00051 #include "OSGTextureBackground.h"
00052 
00053 #include<iostream>
00054 
00055 OSG_USING_NAMESPACE
00056 
00057 /***************************************************************************\
00058  *                            Description                                  *
00059 \***************************************************************************/
00060 
00065 /***************************************************************************\
00066  *                           Class variables                               *
00067 \***************************************************************************/
00068 
00069 /***************************************************************************\
00070  *                           Class methods                                 *
00071 \***************************************************************************/
00072 
00073 void TextureBackground::initMethod (void)
00074 {
00075 }
00076 
00077 
00078 /***************************************************************************\
00079  *                           Instance methods                              *
00080 \***************************************************************************/
00081 
00082 /*-------------------------------------------------------------------------*\
00083   -  private                                                                 -
00084   \*-------------------------------------------------------------------------*/
00085 
00086 /*----------------------- constructors & destructors ----------------------*/
00087 
00088 TextureBackground::TextureBackground(void) :
00089     Inherited()
00090 {
00091 }
00092 
00093 TextureBackground::TextureBackground(const TextureBackground &source) :
00094     Inherited(source)
00095 {
00096 }
00097 
00098 TextureBackground::~TextureBackground(void)
00099 {
00100 }
00101 
00102 /*----------------------------- class specific ----------------------------*/
00103 
00104 
00105 
00106 
00107 void TextureBackground::changed(BitVector whichField, UInt32 origin)
00108 {
00109     Inherited::changed(whichField, origin);
00110  
00111     // all updates are handled in updateGrid()
00112 }
00113 
00114 
00115 void TextureBackground::updateGrid(void)
00116 {
00117     bool gridChanged=( (getHor() != _hor) || 
00118                        (getVert() != _vert) );
00119                        
00120     if(gridChanged)
00121     {
00122         //resize grid
00123         UInt32 gridCoords=(getHor()+2)*(getVert()+2);
00124         _textureCoordArray.resize(gridCoords);
00125         _vertexCoordArray.resize(gridCoords);
00126         
00127         int indexArraySize=(getHor()+2)*((getVert()+1)*2);
00128         _indexArray.resize(indexArraySize);
00129         
00130         _hor = getHor();
00131         _vert = getVert();
00132     }
00133     
00134     if(gridChanged || _radialDistortion   != getRadialDistortion() ||
00135                       _centerOfDistortion != getCenterOfDistortion()
00136       )
00137     {
00138         _radialDistortion = getRadialDistortion();
00139         _centerOfDistortion = getCenterOfDistortion();
00140         
00141         // calculate grid coordinates and triangle strip indices
00142         float xStep=1.0/float(getHor()+1);
00143         float yStep=1.0/float(getVert()+1);
00144         std::vector<Vec2f>::iterator texCoord=_textureCoordArray.begin();
00145         std::vector<Vec2f>::iterator vertexCoord=_vertexCoordArray.begin();
00146         std::vector<UInt32>::iterator index=_indexArray.begin();
00147         UInt32 coord0(0),coord1(0);
00148         GLfloat x,y;
00149         Int16 xx,yy;
00150         Int16 xxmax=getHor()+2,yymax=getVert()+2;
00151         for(yy=0,y=0.0f;yy<yymax;yy++,y+=yStep)
00152         {
00153             if(yy>0)
00154             {
00155                 coord1=yy*xxmax;
00156                 coord0=coord1-xxmax;
00157                 *index++=coord1++;
00158                 *index++=coord0++;
00159             }
00160             float dy=y-getCenterOfDistortion().y();
00161             float dy2=dy*dy;
00162             for(xx=0,x=0.0f;xx<xxmax;xx++,x+=xStep)
00163             {
00164                 *texCoord++=Vec2f(x,y);
00165                 float dx=(x-getCenterOfDistortion().x());
00166                 float dx2=dx*dx;
00167                 float dist2=dx2+dy2;
00168                 float deltaX=dx*getRadialDistortion()*dist2;
00169                 float deltaY=dy*getRadialDistortion()*dist2;
00170                 *vertexCoord++=Vec2f(x+deltaX,y+deltaY);
00171                 if(yy>0&&xx>0)
00172                 {
00173                     *index++=coord1++;
00174                     *index++=coord0++;
00175                 }
00176             }
00177         }
00178     }
00179 }
00180 
00181 void TextureBackground::clear(DrawActionBase *action, Viewport *OSG_CHECK_ARG(viewport))
00182 {
00183     TextureChunkPtr tex = getTexture();
00184     if(tex == NullFC)
00185     {
00186         glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
00187         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00188         return;
00189     }
00190     GLboolean light = glIsEnabled(GL_LIGHTING);
00191     if (light == GL_TRUE)
00192         glDisable(GL_LIGHTING);
00193 
00194     GLint fill[2];
00195     glGetIntegerv(GL_POLYGON_MODE, fill);
00196 #if 1
00197     // original mode
00198     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00199 #else
00200     // for testing the grid
00201     glColor3f(1.0f, 1.0f, 1.0f);
00202     glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
00203 #endif
00204     glClear(GL_DEPTH_BUFFER_BIT);
00205 
00206     GLboolean depth = glIsEnabled(GL_DEPTH_TEST);
00207     glDisable(GL_DEPTH_TEST);
00208 
00209     GLint depthFunc;
00210     glGetIntegerv(GL_DEPTH_FUNC, &depthFunc);
00211     glDepthFunc(GL_ALWAYS);
00212 
00213     glDepthMask(GL_FALSE);
00214 
00215     glMatrixMode(GL_MODELVIEW);
00216     glPushMatrix();
00217     glLoadIdentity();
00218 
00219     glMatrixMode(GL_PROJECTION);
00220     glPushMatrix();
00221 
00222     glLoadIdentity();
00223     glOrtho(0, 1, 0, 1, 0, 1);
00224 
00225     glColor4fv(getColor().getValuesRGBA());
00226 
00227     tex->activate(action);
00228 
00229     if(tex->isTransparent())
00230     {
00231         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00232         glEnable(GL_BLEND);
00233     }
00234     if(osgabs(getRadialDistortion())<Eps)
00235     {
00236         if(getTexCoords().getSize() < 4)
00237         {
00238             // set some default texture coordinates.
00239             glBegin(GL_QUADS);
00240                 glTexCoord2f(0.0f, 0.0f);
00241                 glVertex3f(0.0f, 0.0f, 0.0f);
00242                 glTexCoord2f(1.0f, 0.0f);
00243                 glVertex3f(1.0f, 0.0f, 0.0f);
00244                 glTexCoord2f(1.0f, 1.0f);
00245                 glVertex3f(1.0f, 1.0f, 0.0f);
00246                 glTexCoord2f(0.0f, 1.0f);
00247                 glVertex3f(0.0f, 1.0f, 0.0f);
00248             glEnd();
00249         }
00250         else
00251         {
00252             glBegin(GL_QUADS);
00253                 glTexCoord2f(getTexCoords()[0].getValues()[0],
00254                              getTexCoords()[0].getValues()[1]);
00255                 glVertex3f(0.0f, 0.0f, 0.0f);
00256                 glTexCoord2f(getTexCoords()[1].getValues()[0],
00257                              getTexCoords()[1].getValues()[1]);
00258                 glVertex3f(1.0f, 0.0f, 0.0f);
00259                 glTexCoord2f(getTexCoords()[2].getValues()[0],
00260                              getTexCoords()[2].getValues()[1]);
00261                 glVertex3f(1.0f, 1.0f, 0.0f);
00262                 glTexCoord2f(getTexCoords()[3].getValues()[0],
00263                              getTexCoords()[3].getValues()[1]);
00264                 glVertex3f(0.0f, 1.0f, 0.0f);
00265             glEnd();
00266         }
00267     }
00268     else // map texture to distortion grid
00269     {
00270         updateGrid();
00271         Int16 xxmax=getHor()+2,yymax=getVert()+2;
00272 
00273         UInt32 gridCoords=xxmax*yymax;
00274         int indexArraySize=xxmax*((getVert()+1)*2);
00275 
00276         if(_vertexCoordArray.size()==gridCoords &&
00277            _textureCoordArray.size()==gridCoords &&
00278            _indexArray.size()==indexArraySize)
00279         {
00280             // clear background, because possibly the distortion grid could not cover th whole window
00281             glClearColor(.5f, 0.5f, 0.5f, 1.0f);
00282             glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00283             std::vector<UInt32>::iterator i;
00284             UInt32 yMax=getVert()+1;
00285             for(UInt32 y=0;y<yMax;y++)
00286             {
00287                 glBegin(GL_TRIANGLE_STRIP);
00288                 std::vector<UInt32>::iterator begin=_indexArray.begin()+(y*2*xxmax);
00289                 std::vector<UInt32>::iterator end=begin+2*xxmax;
00290                 for(std::vector<UInt32>::iterator i=begin;i!=end;i++)
00291                 {
00292                     glTexCoord2fv(_textureCoordArray[*i].getValues());
00293                     glVertex2fv(_vertexCoordArray[*i].getValues());
00294 
00295                 }
00296                 glEnd();
00297             }
00298         }
00299     }
00300     if(tex->isTransparent())
00301     {
00302         glDisable(GL_BLEND);
00303     }
00304 
00305     tex->deactivate(action);
00306 
00307     glClear(GL_DEPTH_BUFFER_BIT);
00308 
00309     glPopMatrix();
00310     glMatrixMode(GL_MODELVIEW);
00311     glPopMatrix();
00312 
00313     glDepthMask(GL_TRUE);
00314     if(depth)
00315         glEnable(GL_DEPTH_TEST);
00316     glDepthFunc(depthFunc);
00317 
00318     glPolygonMode(GL_FRONT, fill[0]);
00319     glPolygonMode(GL_BACK , fill[1]);
00320     if(light)
00321         glEnable(GL_LIGHTING);
00322     glColor3f(1.0f, 1.0f, 1.0f);
00323 }
00324 
00325 
00326 void TextureBackground::dump(      UInt32    ,
00327                    const BitVector ) const
00328 {
00329     SLOG << "Dump TextureBackground NI" << std::endl;
00330 }
00331 
00332 
00333 /*------------------------------------------------------------------------*/
00334 /*                              cvs id's                                  */
00335 
00336 #ifdef OSG_SGI_CC
00337 #pragma set woff 1174
00338 #endif
00339 
00340 #ifdef OSG_LINUX_ICC
00341 #pragma warning( disable : 177 )
00342 #endif
00343 
00344 namespace
00345 {
00346     static Char8 cvsid_cpp       [] = "@(#)$Id: OSGTextureBackground.cpp,v 1.6 2005/07/06 16:00:44 dirk Exp $";
00347     static Char8 cvsid_hpp       [] = OSGTEXTUREBACKGROUNDBASE_HEADER_CVSID;
00348     static Char8 cvsid_inl       [] = OSGTEXTUREBACKGROUNDBASE_INLINE_CVSID;
00349 
00350     static Char8 cvsid_fields_hpp[] = OSGTEXTUREBACKGROUNDFIELDS_HEADER_CVSID;
00351 }
00352 
00353 #ifdef __sgi
00354 #pragma reset woff 1174
00355 #endif
00356 

Generated on Thu Aug 25 04:11:18 2005 for OpenSG by  doxygen 1.4.3