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
00191 glPushAttrib(GL_POLYGON_BIT | GL_DEPTH_BUFFER_BIT |
00192 GL_LIGHTING_BIT);
00193
00194 glDisable(GL_LIGHTING);
00195
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 glDisable(GL_DEPTH_TEST);
00207 glDepthFunc(GL_ALWAYS);
00208 glDepthMask(GL_FALSE);
00209
00210 glMatrixMode(GL_MODELVIEW);
00211 glPushMatrix();
00212 glLoadIdentity();
00213
00214 glMatrixMode(GL_PROJECTION);
00215 glPushMatrix();
00216
00217 glLoadIdentity();
00218 glOrtho(0, 1, 0, 1, 0, 1);
00219
00220 glColor4fv(getColor().getValuesRGBA());
00221
00222 tex->activate(action);
00223
00224 if(tex->isTransparent())
00225 {
00226 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00227 glEnable(GL_BLEND);
00228 }
00229 if(osgabs(getRadialDistortion())<Eps)
00230 {
00231 if(getTexCoords().getSize() < 4)
00232 {
00233
00234 glBegin(GL_QUADS);
00235 glTexCoord2f(0.0f, 0.0f);
00236 glVertex3f(0.0f, 0.0f, 0.0f);
00237 glTexCoord2f(1.0f, 0.0f);
00238 glVertex3f(1.0f, 0.0f, 0.0f);
00239 glTexCoord2f(1.0f, 1.0f);
00240 glVertex3f(1.0f, 1.0f, 0.0f);
00241 glTexCoord2f(0.0f, 1.0f);
00242 glVertex3f(0.0f, 1.0f, 0.0f);
00243 glEnd();
00244 }
00245 else
00246 {
00247 glBegin(GL_QUADS);
00248 glTexCoord2f(getTexCoords()[0].getValues()[0],
00249 getTexCoords()[0].getValues()[1]);
00250 glVertex3f(0.0f, 0.0f, 0.0f);
00251 glTexCoord2f(getTexCoords()[1].getValues()[0],
00252 getTexCoords()[1].getValues()[1]);
00253 glVertex3f(1.0f, 0.0f, 0.0f);
00254 glTexCoord2f(getTexCoords()[2].getValues()[0],
00255 getTexCoords()[2].getValues()[1]);
00256 glVertex3f(1.0f, 1.0f, 0.0f);
00257 glTexCoord2f(getTexCoords()[3].getValues()[0],
00258 getTexCoords()[3].getValues()[1]);
00259 glVertex3f(0.0f, 1.0f, 0.0f);
00260 glEnd();
00261 }
00262 }
00263 else
00264 {
00265 updateGrid();
00266 Int16 xxmax=getHor()+2,yymax=getVert()+2;
00267
00268 UInt32 gridCoords=xxmax*yymax;
00269 int indexArraySize=xxmax*((getVert()+1)*2);
00270
00271 if(_vertexCoordArray.size()==gridCoords &&
00272 _textureCoordArray.size()==gridCoords &&
00273 _indexArray.size()==indexArraySize)
00274 {
00275
00276 glClearColor(.5f, 0.5f, 0.5f, 1.0f);
00277 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00278 std::vector<UInt32>::iterator i;
00279 UInt32 yMax=getVert()+1;
00280 for(UInt32 y=0;y<yMax;y++)
00281 {
00282 glBegin(GL_TRIANGLE_STRIP);
00283 std::vector<UInt32>::iterator begin=_indexArray.begin()+(y*2*xxmax);
00284 std::vector<UInt32>::iterator end=begin+2*xxmax;
00285 for(std::vector<UInt32>::iterator i=begin;i!=end;i++)
00286 {
00287 glTexCoord2fv(_textureCoordArray[*i].getValues());
00288 glVertex2fv(_vertexCoordArray[*i].getValues());
00289
00290 }
00291 glEnd();
00292 }
00293 }
00294 }
00295 if(tex->isTransparent())
00296 {
00297 glDisable(GL_BLEND);
00298 }
00299
00300 tex->deactivate(action);
00301
00302 Int32 bit = getClearStencilBit();
00303
00304 if (bit >= 0)
00305 {
00306 glClearStencil(bit);
00307 glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
00308 }
00309 else
00310 {
00311 glClear(GL_DEPTH_BUFFER_BIT);
00312 }
00313
00314 glPopMatrix();
00315 glMatrixMode(GL_MODELVIEW);
00316 glPopMatrix();
00317
00318 glPopAttrib();
00319 glColor3f(1.0f, 1.0f, 1.0f);
00320 }
00321
00322
00323 void TextureBackground::dump( UInt32 ,
00324 const BitVector ) const
00325 {
00326 SLOG << "Dump TextureBackground NI" << std::endl;
00327 }
00328
00329
00330
00331
00332
00333 #ifdef OSG_SGI_CC
00334 #pragma set woff 1174
00335 #endif
00336
00337 #ifdef OSG_LINUX_ICC
00338 #pragma warning( disable : 177 )
00339 #endif
00340
00341 namespace
00342 {
00343 static Char8 cvsid_cpp [] = "@(#)$Id: OSGTextureBackground.cpp,v 1.8 2006/09/08 13:45:30 yjung Exp $";
00344 static Char8 cvsid_hpp [] = OSGTEXTUREBACKGROUNDBASE_HEADER_CVSID;
00345 static Char8 cvsid_inl [] = OSGTEXTUREBACKGROUNDBASE_INLINE_CVSID;
00346
00347 static Char8 cvsid_fields_hpp[] = OSGTEXTUREBACKGROUNDFIELDS_HEADER_CVSID;
00348 }
00349
00350 #ifdef __sgi
00351 #pragma reset woff 1174
00352 #endif
00353