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
00048 #include <OSGDrawActionBase.h>
00049 #include <OSGCamera.h>
00050 #include <OSGViewport.h>
00051 #include <OSGTextureChunk.h>
00052 #include <OSGImage.h>
00053
00054 #include "OSGSkyBackground.h"
00055
00056 #include <OSGGL.h>
00057
00058 OSG_USING_NAMESPACE
00059
00060
00061
00062
00063
00078
00079
00080 SkyBackground::SkyBackground(void) :
00081 Inherited()
00082 {
00083 }
00084
00085 SkyBackground::SkyBackground(const SkyBackground &source) :
00086 Inherited(source)
00087 {
00088 }
00089
00090 SkyBackground::~SkyBackground(void)
00091 {
00092 }
00093
00094
00095
00096 void SkyBackground::initMethod (void)
00097 {
00098 }
00099
00100 void SkyBackground::changed(BitVector whichField, UInt32 origin)
00101 {
00102 Inherited::changed(whichField, origin);
00103 }
00104
00105 void SkyBackground::dump( UInt32 ,
00106 const BitVector) const
00107 {
00108 SLOG << "Dump SkyBackground NI" << std::endl;
00109 }
00110
00111
00112
00113 void SkyBackground::drawFace( DrawActionBase * action,
00114 TextureChunkPtr tex,
00115 StateChunk *&oldtex,
00116 const Pnt3f &p1,
00117 const Pnt3f &p2,
00118 const Pnt3f &p3,
00119 const Pnt3f &p4,
00120 const Vec3f * texCoord)
00121 {
00122
00123 if(tex != NullFC)
00124 {
00125 if(oldtex != NULL)
00126 {
00127 tex->changeFrom(action, oldtex);
00128 }
00129 else
00130 {
00131 tex->activate(action);
00132 }
00133
00134 if(tex->isTransparent())
00135 {
00136 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00137 glEnable(GL_BLEND);
00138 }
00139
00140
00141
00142
00143 glBegin(GL_QUADS);
00144 glTexCoord3fv((GLfloat*) texCoord[0].getValues());
00145 glVertex3fv((GLfloat*) p1.getValues());
00146 glTexCoord3fv((GLfloat*) texCoord[1].getValues());
00147 glVertex3fv((GLfloat*) p2.getValues());
00148 glTexCoord3fv((GLfloat*) texCoord[2].getValues());
00149 glVertex3fv((GLfloat*) p3.getValues());
00150 glTexCoord3fv((GLfloat*) texCoord[3].getValues());
00151 glVertex3fv((GLfloat*) p4.getValues());
00152 glEnd();
00153
00154 if(tex->isTransparent())
00155 {
00156 glDisable(GL_BLEND);
00157 }
00158
00159 oldtex = tex.getCPtr();
00160 }
00161
00162 }
00163
00164 void SkyBackground::clear(DrawActionBase *action, Viewport *viewport)
00165 {
00166
00167 glPushAttrib(GL_POLYGON_BIT | GL_DEPTH_BUFFER_BIT |
00168 GL_LIGHTING_BIT);
00169
00170 glDisable(GL_LIGHTING);
00171 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00172 glDisable(GL_DEPTH_TEST);
00173
00174 glMatrixMode(GL_MODELVIEW);
00175 glPushMatrix();
00176
00177 Matrix m,t;
00178 action->getCamera()->getViewing(m, viewport->getPixelWidth(),
00179 viewport->getPixelHeight());
00180 action->getCamera()->getProjectionTranslation(t,
00181 viewport->getPixelWidth(),
00182 viewport->getPixelHeight());
00183 m.multLeft(t);
00184
00185 if (getBeacon() != NullFC)
00186 {
00187 getBeacon()->getToWorld(t);
00188 m.mult(t);
00189 }
00190
00191 m[3][0] = m[3][1] = m[3][2] = 0;
00192 glLoadMatrixf(m.getValues());
00193
00194 glMatrixMode(GL_PROJECTION);
00195 glPushMatrix();
00196
00197 glLoadIdentity();
00198 glTranslatef(0.f, 0.f, 0.5);
00199 glScalef(1.f, 1.f, 0.f);
00200
00201 action->getCamera()->getProjection( m,
00202 viewport->getPixelWidth(),
00203 viewport->getPixelHeight());
00204 glMultMatrixf(m.getValues());
00205
00206 UInt32 i, j;
00207 UInt32 sr = _sfSphereRes.getValue() + 1;
00208
00209 if ( _cosval.size() != sr )
00210 {
00211 Real32 da = 2 * Pi / (sr - 1);
00212
00213 _cosval.resize(sr);
00214 _sinval.resize(sr);
00215
00216 for(i = 0; i < sr; ++i)
00217 {
00218 _cosval[i] = osgcos(i * da);
00219 _sinval[i] = osgsin(i * da);
00220 }
00221 }
00222
00223 Real32 vcos1,vsin1,vcos2,vsin2;
00224
00225 if(_mfSkyAngle.size() > 0)
00226 {
00227 vcos1 = osgcos(_mfSkyAngle[0]);
00228 vsin1 = osgsin(_mfSkyAngle[0]);
00229
00230 glBegin(GL_TRIANGLE_FAN);
00231 glColor4fv((GLfloat*) _mfSkyColor[0].getValuesRGBA());
00232 glVertex3f(0, 1, 0);
00233 glColor4fv((GLfloat*) _mfSkyColor[1].getValuesRGBA());
00234
00235 for(i = 0; i < sr; ++i)
00236 {
00237 glVertex3f(vsin1 * _sinval[i], vcos1, vsin1 * _cosval[i]);
00238 }
00239
00240 glEnd();
00241
00242
00243 for(j = 0; j < _mfSkyAngle.size() - 1; ++j)
00244 {
00245 Color4f c1, c2;
00246
00247 c1 = _mfSkyColor[j+1];
00248 c2 = _mfSkyColor[j+2];
00249
00250 vcos1 = osgcos(_mfSkyAngle[j ]);
00251 vsin1 = osgsin(_mfSkyAngle[j ]);
00252 vcos2 = osgcos(_mfSkyAngle[j+1]);
00253 vsin2 = osgsin(_mfSkyAngle[j+1]);
00254
00255 glBegin(GL_TRIANGLE_STRIP);
00256
00257 for(i = 0; i < sr; ++i)
00258 {
00259 glColor4fv((GLfloat*) c1.getValuesRGBA());
00260 glVertex3f(vsin1 * _sinval[i], vcos1, vsin1 * _cosval[i]);
00261 glColor4fv((GLfloat*) c2.getValuesRGBA());
00262 glVertex3f(vsin2 * _sinval[i], vcos2, vsin2 * _cosval[i]);
00263 }
00264 glEnd();
00265 }
00266
00267 if(osgabs(_mfSkyAngle[j] - Pi) > Eps)
00268 {
00269 glBegin(GL_TRIANGLE_FAN);
00270 glColor4fv((GLfloat*) _mfSkyColor[j+1].getValuesRGBA());
00271 glVertex3f(0, -1, 0);
00272 vcos1 = osgcos(_mfSkyAngle[j]);
00273 vsin1 = osgsin(_mfSkyAngle[j]);
00274
00275 for(i = 0; i < sr; ++i)
00276 {
00277 glVertex3f(vsin1 * _sinval[i], vcos1, vsin1 * _cosval[i]);
00278 }
00279
00280 glEnd();
00281 }
00282 }
00283 else
00284 {
00285 if(_mfSkyColor.size() > 0)
00286 {
00287 glClearColor(_mfSkyColor[0][0], _mfSkyColor[0][1],
00288 _mfSkyColor[0][2], 0);
00289 }
00290 else
00291 {
00292 glClearColor(0, 0, 0, 0);
00293 }
00294 glClear(GL_COLOR_BUFFER_BIT);
00295 }
00296
00297
00298
00299
00300 if(_mfGroundAngle.size() > 0)
00301 {
00302 vcos1 = -osgcos(_mfGroundAngle[0]);
00303 vsin1 = osgsin(_mfGroundAngle[0]);
00304
00305 glBegin(GL_TRIANGLE_FAN);
00306
00307 if (_mfGroundColor.size())
00308 glColor4fv((GLfloat*) _mfGroundColor[0].getValuesRGBA());
00309
00310 glVertex3f(0, -1, 0);
00311
00312 if (_mfGroundColor.size() > 1)
00313 glColor4fv((GLfloat*) _mfGroundColor[1].getValuesRGBA());
00314
00315 for(i = 0; i < sr; ++i)
00316 {
00317 glVertex3f(vsin1 * _sinval[i], vcos1, vsin1 * _cosval[i]);
00318 }
00319
00320 glEnd();
00321
00322
00323 for(j = 0; j < _mfGroundAngle.size() - 1; ++j)
00324 {
00325 Color4f c1, c2;
00326
00327 if (_mfGroundColor.size() > j+2)
00328 {
00329 c1 = _mfGroundColor[j+1];
00330 c2 = _mfGroundColor[j+2];
00331 }
00332
00333 vcos1 = -osgcos(_mfGroundAngle[j ]);
00334 vsin1 = osgsin(_mfGroundAngle[j ]);
00335 vcos2 = -osgcos(_mfGroundAngle[j+1]);
00336 vsin2 = osgsin(_mfGroundAngle[j+1]);
00337
00338 glBegin(GL_TRIANGLE_STRIP);
00339
00340 for(i = 0; i < sr; ++i)
00341 {
00342 glColor4fv((GLfloat*) c1.getValuesRGBA());
00343 glVertex3f(vsin1 * _sinval[i], vcos1, vsin1 * _cosval[i]);
00344 glColor4fv((GLfloat*) c2.getValuesRGBA());
00345 glVertex3f(vsin2 * _sinval[i], vcos2, vsin2 * _cosval[i]);
00346 }
00347 glEnd();
00348 }
00349 }
00350
00351
00352 StateChunk *tchunk = NULL;
00353 static Vec3f defaulttc[7][4] = {
00354
00355 { Vec3f(0,0,0), Vec3f(1,0,0), Vec3f(1,1,0), Vec3f(0,1,0) },
00356
00357
00358 { Vec3f(-1, 1,-1), Vec3f( 1, 1,-1), Vec3f( 1,-1,-1), Vec3f(-1,-1,-1) },
00359 { Vec3f( 1, 1, 1), Vec3f(-1, 1, 1), Vec3f(-1,-1, 1), Vec3f( 1,-1, 1) },
00360 { Vec3f( 1, 1,-1), Vec3f(-1, 1,-1), Vec3f(-1, 1, 1), Vec3f( 1, 1, 1) },
00361 { Vec3f( 1,-1, 1), Vec3f(-1,-1, 1), Vec3f(-1,-1,-1), Vec3f( 1,-1,-1) },
00362 { Vec3f( 1, 1,-1), Vec3f( 1, 1, 1), Vec3f( 1,-1, 1), Vec3f( 1,-1,-1) },
00363 { Vec3f(-1, 1, 1), Vec3f(-1, 1,-1), Vec3f(-1,-1,-1), Vec3f(-1,-1, 1) },
00364 };
00365
00366 #undef tfac
00367 #define tfac(t,c) \
00368 defaulttc[(c)*((t) != NullFC && (t)->getImage() != NullFC && \
00369 (t)->getImage()->getSideCount() == 6)]
00370
00371 drawFace(action, getBackTexture(), tchunk,
00372 Pnt3f(0.5, -0.5, 0.5),
00373 Pnt3f(-0.5, -0.5, 0.5),
00374 Pnt3f(-0.5, 0.5, 0.5),
00375 Pnt3f(0.5, 0.5, 0.5),
00376 getBackTexCoord().size()?
00377 &getBackTexCoord()[0]:
00378 tfac(getBackTexture(), 1));
00379
00380 drawFace(action, getFrontTexture(), tchunk,
00381 Pnt3f(-0.5, -0.5, -0.5),
00382 Pnt3f(0.5, -0.5, -0.5),
00383 Pnt3f(0.5, 0.5, -0.5),
00384 Pnt3f(-0.5, 0.5, -0.5),
00385 getFrontTexCoord().size()?
00386 &getFrontTexCoord()[0]:
00387 tfac(getFrontTexture(), 2));
00388
00389 drawFace(action, getBottomTexture(), tchunk,
00390 Pnt3f(-0.5, -0.5, 0.5),
00391 Pnt3f(0.5, -0.5, 0.5),
00392 Pnt3f(0.5, -0.5, -0.5),
00393 Pnt3f(-0.5, -0.5, -0.5),
00394 getBottomTexCoord().size()?
00395 &getBottomTexCoord()[0]:
00396 tfac(getBottomTexture(), 3));
00397
00398 drawFace(action, getTopTexture(), tchunk,
00399 Pnt3f(-0.5, 0.5, -0.5),
00400 Pnt3f(0.5, 0.5, -0.5),
00401 Pnt3f(0.5, 0.5, 0.5),
00402 Pnt3f(-0.5, 0.5, 0.5),
00403 getTopTexCoord().size()?
00404 &getTopTexCoord()[0]:
00405 tfac(getTopTexture(), 4));
00406
00407 drawFace(action, getLeftTexture(), tchunk,
00408 Pnt3f(-0.5, -0.5, 0.5),
00409 Pnt3f(-0.5, -0.5, -0.5),
00410 Pnt3f(-0.5, 0.5, -0.5),
00411 Pnt3f(-0.5, 0.5, 0.5),
00412 getLeftTexCoord().size()?
00413 &getLeftTexCoord()[0]:
00414 tfac(getLeftTexture(), 5));
00415
00416 drawFace(action, getRightTexture(), tchunk,
00417 Pnt3f(0.5, -0.5, -0.5),
00418 Pnt3f(0.5, -0.5, 0.5),
00419 Pnt3f(0.5, 0.5, 0.5),
00420 Pnt3f(0.5, 0.5, -0.5),
00421 getRightTexCoord().size()?
00422 &getRightTexCoord()[0]:
00423 tfac(getRightTexture(), 6));
00424 #undef tfac
00425
00426 if(tchunk != NULL)
00427 tchunk->deactivate(action);
00428
00429 Int32 bit = getClearStencilBit();
00430
00431 if (bit >= 0)
00432 {
00433 glClearStencil(bit);
00434 glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
00435 }
00436 else
00437 {
00438 glClear(GL_DEPTH_BUFFER_BIT);
00439 }
00440
00441 glPopMatrix();
00442 glMatrixMode(GL_MODELVIEW);
00443 glPopMatrix();
00444
00445 glPopAttrib();
00446
00447 glColor3f(1.0, 1.0, 1.0);
00448 }
00449
00450
00451
00452
00453
00454 #ifdef __sgi
00455 #pragma set woff 1174
00456 #endif
00457
00458 #ifdef OSG_LINUX_ICC
00459 #pragma warning(disable : 177)
00460 #endif
00461
00462 namespace
00463 {
00464 static char cvsid_cpp[] = "@(#)$Id: $";
00465 static char cvsid_hpp[] = OSGSKYBACKGROUND_HEADER_CVSID;
00466 static char cvsid_inl[] = OSGSKYBACKGROUND_INLINE_CVSID;
00467 }