00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
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
00059
00060
00065
00066
00067
00068
00069
00070
00071
00072
00073 void TextureBackground::initMethod (void)
00074 {
00075 }
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
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
00103
00104
00105
00106
00107 void TextureBackground::changed(BitVector whichField, UInt32 origin)
00108 {
00109 Inherited::changed(whichField, origin);
00110
00111
00112 }
00113
00114
00115 void TextureBackground::updateGrid(void)
00116 {
00117 bool gridChanged=( (getHor() != _hor) ||
00118 (getVert() != _vert) );
00119
00120 if(gridChanged)
00121 {
00122
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
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
00198 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00199 #else
00200
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
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
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
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
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