OSGSkyBackground.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 "OSGCamera.h"
00049 #include "OSGViewport.h"
00050 #include "OSGTextureObjChunk.h"
00051 #include "OSGImage.h"
00052 #include "OSGDrawEnv.h"
00053
00054 #include "OSGSkyBackground.h"
00055
00056 #include "OSGGL.h"
00057
00058 OSG_USING_NAMESPACE
00059
00060 // Documentation for this class is emited in the
00061 // OSGSkyBackgroundBase.cpp file.
00062 // To modify it, please change the .fcd file (OSGSkyBackground.fcd) and
00063 // regenerate the base file.
00064
00065 const Vec3f SkyBackground::_defaultTexCoords[7][4] =
00066 {
00067     // 2D default TCs
00068     { Vec3f( 0, 0, 0), Vec3f( 1, 0, 0), Vec3f( 1, 1, 0), Vec3f( 0, 1, 0) },
00069
00070      // Cubetex Default TCs
00071     { Vec3f( 1,-1, 1), Vec3f(-1,-1, 1), Vec3f(-1, 1, 1), Vec3f( 1, 1, 1) },
00072     { Vec3f(-1,-1,-1), Vec3f( 1,-1,-1), Vec3f( 1, 1,-1), Vec3f(-1, 1,-1) },
00073
00074     { Vec3f(-1,-1, 1), Vec3f( 1,-1, 1), Vec3f( 1,-1,-1), Vec3f(-1,-1,-1) },
00075     { Vec3f(-1, 1,-1), Vec3f( 1, 1,-1), Vec3f( 1, 1, 1), Vec3f(-1, 1, 1) },
00076
00077     { Vec3f(-1,-1, 1), Vec3f(-1,-1,-1), Vec3f(-1, 1,-1), Vec3f(-1, 1, 1) },
00078     { Vec3f( 1,-1,-1), Vec3f( 1,-1, 1), Vec3f( 1, 1, 1), Vec3f( 1, 1,-1) }
00079 };
00080
00081 const Vec3f SkyBackground::_defaultVRMLTexCoords[7][4] =
00082 {
00083     // 2D default TCs
00084     { Vec3f( 0, 0, 0), Vec3f( 1, 0, 0), Vec3f( 1, 1, 0), Vec3f( 0, 1, 0) },
00085
00086      // Cubetex Default TCs
00087     { Vec3f(-1, 1,-1), Vec3f( 1, 1,-1), Vec3f( 1,-1,-1), Vec3f(-1,-1,-1) },
00088     { Vec3f( 1, 1, 1), Vec3f(-1, 1, 1), Vec3f(-1,-1, 1), Vec3f( 1,-1, 1) },
00089
00090     { Vec3f( 1, 1,-1), Vec3f(-1, 1,-1), Vec3f(-1, 1, 1), Vec3f( 1, 1, 1) },
00091     { Vec3f( 1,-1, 1), Vec3f(-1,-1, 1), Vec3f(-1,-1,-1), Vec3f( 1,-1,-1) },
00092
00093     { Vec3f( 1, 1,-1), Vec3f( 1, 1, 1), Vec3f( 1,-1, 1), Vec3f( 1,-1,-1) },
00094     { Vec3f(-1, 1, 1), Vec3f(-1, 1,-1), Vec3f(-1,-1,-1), Vec3f(-1,-1, 1) }
00095 };
00096
00097 /*----------------------- constructors & destructors ----------------------*/
00098
00099 SkyBackground::SkyBackground(void) :
00100     Inherited()
00101 {
00102 }
00103
00104 SkyBackground::SkyBackground(const SkyBackground &source) :
00105     Inherited(source)
00106 {
00107 }
00108
00109 SkyBackground::~SkyBackground(void)
00110 {
00111 }
00112
00113 /*----------------------------- class specific ----------------------------*/
00114
00115 void SkyBackground::initMethod(InitPhase ePhase)
00116 {
00117     Inherited::initMethod(ePhase);
00118 }
00119
00120 void SkyBackground::changed(ConstFieldMaskArg whichField,
00121                             UInt32            origin,
00122                             BitVector         details)
00123 {
00124     Inherited::changed(whichField, origin, details);
00125 }
00126
00127 void SkyBackground::dump(      UInt32   ,
00128                          const BitVector) const
00129 {
00130     SLOG << "Dump SkyBackground NI" << std::endl;
00131 }
00132
00133 /*-------------------------- drawing ---------------------------------*/
00134
00135 void SkyBackground::drawFace(      DrawEnv             * pEnv,
00136                                    TextureBaseChunk    * tex,
00137                                    StateChunk          *&oldtex,
00138                              const Pnt3f                &p1,
00139                              const Pnt3f                &p2,
00140                              const Pnt3f                &p3,
00141                              const Pnt3f                &p4,
00142                              const Vec3f               * texCoord)
00143 {
00144     if(tex != NULL)
00145     {
00146         if(oldtex != NULL)
00147         {
00148             tex->changeFrom(pEnv, oldtex);
00149         }
00150         else
00151         {
00152             tex->activate(pEnv);
00153         }
00154
00155         if(tex->isTransparent())
00156         {
00157             glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00158             glEnable(GL_BLEND);
00159         }
00160
00161         // ENRICO: this part holds the informations about
00162         // custom texture coordinates
00163         // Mess with the best, die like the rest
00164         glBegin(GL_QUADS);
00165         {
00166 #if 0 // strange crash with par drawers.
00167             glTexCoord3fv(static_cast<const GLfloat *>(
00168                               texCoord[0].getValues()));
00169             glVertex3fv  (static_cast<const GLfloat *>(p1.getValues()));
00170
00171             glTexCoord3fv(static_cast<const GLfloat *>(
00172                               texCoord[1].getValues()));
00173             glVertex3fv  (static_cast<const GLfloat *>(p2.getValues()));
00174
00175             glTexCoord3fv(static_cast<const GLfloat *>(
00176                               texCoord[2].getValues()));
00177             glVertex3fv  (static_cast<const GLfloat *>(p3.getValues()));
00178
00179             glTexCoord3fv(static_cast<const GLfloat *>(
00180                               texCoord[3].getValues()));
00181             glVertex3fv  (static_cast<const GLfloat *>(p4.getValues()));
00182 #else
00183             glTexCoord3fv(static_cast<const GLfloat *>(
00184                               texCoord[0].getValues()));
00185             glVertex3f  (p1[0], p1[1], p1[2]);
00186
00187             glTexCoord3fv(static_cast<const GLfloat *>(
00188                               texCoord[1].getValues()));
00189             glVertex3f   (p2[0], p2[1], p2[2]);
00190
00191             glTexCoord3fv(static_cast<const GLfloat *>(
00192                               texCoord[2].getValues()));
00193             glVertex3f   (p3[0], p3[1], p3[2]);
00194
00195             glTexCoord3fv(texCoord[3].getValues());
00196             glVertex3f   (p4[0], p4[1], p4[2]);
00197 #endif
00198         }
00199         glEnd();
00200
00201         if(tex->isTransparent())
00202         {
00203             glDisable(GL_BLEND);
00204         }
00205
00206         oldtex = tex;
00207     }
00208
00209 }
00210
00211 void SkyBackground::clear(DrawEnv *pEnv)
00212 {
00213     glPushAttrib(GL_POLYGON_BIT | GL_DEPTH_BUFFER_BIT |
00214                  GL_LIGHTING_BIT);
00215
00216     glDisable(GL_LIGHTING);
00217     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00218     glDisable(GL_DEPTH_TEST);
00219
00220     glMatrixMode(GL_MODELVIEW);
00221     glPushMatrix();
00222
00223     Matrix m,t;
00224
00225 /*
00226     action->getCamera()->getViewing(m, viewport->getPixelWidth(),
00227                                         viewport->getPixelHeight());
00228     action->getCamera()->getProjectionTranslation(t,
00229                                         viewport->getPixelWidth(),
00230                                         viewport->getPixelHeight());
00231  */
00232
00233     m = pEnv->getCameraViewing();
00234     t = pEnv->getCameraProjectionTrans();
00235
00236     m.multLeft(t);
00237
00238     if(getBeacon() != NULL)
00239     {
00240         getBeacon()->getToWorld(t);
00241         m.mult(t);
00242     }
00243
00244     m[3][0] = m[3][1] = m[3][2] = 0;
00245     glLoadMatrixf(m.getValues());
00246
00247     glMatrixMode(GL_PROJECTION);
00248     glPushMatrix();
00249
00250     glLoadIdentity();
00251     glTranslatef(0.f, 0.f, 0.5);
00252     glScalef(1.f, 1.f, 0.f);
00253
00254 /*
00255     action->getCamera()->getProjection(m, viewport->getPixelWidth(),
00256                                            viewport->getPixelHeight());
00257  */
00258     m = pEnv->getCameraProjection();
00259
00260     glMultMatrixf(m.getValues());
00261
00262     UInt32 i, j;
00263     UInt32 sr = _sfSphereRes.getValue() + 1;      // sphere resolution
00264
00265     if(_cosval.size() != sr)
00266     {
00267         Real32 da = 2 * Pi / (sr - 1);
00268
00269         _cosval.resize(sr);
00270         _sinval.resize(sr);
00271
00272         for(i = 0; i < sr; ++i)
00273         {
00274             _cosval[i] = osgCos(i * da);
00275             _sinval[i] = osgSin(i * da);
00276         }
00277     }
00278
00279     Real32  vcos1,vsin1,vcos2,vsin2;
00280
00281     // better always clear and set a defined color...
00282     glColor3f(1, 1, 1);
00283
00284     if(_mfSkyColor.size() > 0)
00285     {
00286         glClearColor(_mfSkyColor[0][0], _mfSkyColor[0][1],
00287                      _mfSkyColor[0][2], _mfSkyColor[0][2]);
00288     }
00289     else
00290     {
00291         glClearColor(0, 0, 0, 1);
00292     }
00293
00294     glClear(GL_COLOR_BUFFER_BIT);
00295
00296     if(_mfSkyAngle.size() > 0)
00297     {
00298         vcos1 = osgCos(_mfSkyAngle[0]);
00299         vsin1 = osgSin(_mfSkyAngle[0]);
00300
00301         glBegin(GL_TRIANGLE_FAN);
00302         glColor4fv(
00303             static_cast<const GLfloat *>(_mfSkyColor[0].getValuesRGBA()));
00304         glVertex3f(0, 1, 0);
00305         glColor4fv(
00306             static_cast<const GLfloat *>(_mfSkyColor[1].getValuesRGBA()));
00307
00308         for(i = 0; i < sr; ++i)
00309         {
00310             glVertex3f(vsin1 * _sinval[i], vcos1, vsin1 * _cosval[i]);
00311         }
00312
00313         glEnd();
00314
00315
00316         for(j = 0; j < _mfSkyAngle.size() - 1; ++j)
00317         {
00318             Color4f c1, c2;
00319
00320             c1 = _mfSkyColor[j+1];
00321             c2 = _mfSkyColor[j+2];
00322
00323             vcos1 = osgCos(_mfSkyAngle[j  ]);
00324             vsin1 = osgSin(_mfSkyAngle[j  ]);
00325             vcos2 = osgCos(_mfSkyAngle[j+1]);
00326             vsin2 = osgSin(_mfSkyAngle[j+1]);
00327
00328             glBegin(GL_TRIANGLE_STRIP);
00329
00330             for(i = 0; i < sr; ++i)
00331             {
00332                 glColor4fv(static_cast<const GLfloat *>(c1.getValuesRGBA()));
00333                 glVertex3f(vsin1 * _sinval[i], vcos1, vsin1 * _cosval[i]);
00334                 glColor4fv(static_cast<const GLfloat *>(c2.getValuesRGBA()));
00335                 glVertex3f(vsin2 * _sinval[i], vcos2, vsin2 * _cosval[i]);
00336             }
00337             glEnd();
00338         }
00339
00340         //if(osgAbs(_mfSkyAngle[j] - Pi) > Eps)
00341         {
00342             glBegin(GL_TRIANGLE_FAN);
00343             glColor4fv(
00344                 static_cast<const GLfloat *>(_mfSkyColor[j+1].getValuesRGBA()));
00345             glVertex3f(0, -1, 0);
00346             vcos1 = osgCos(_mfSkyAngle[j]);
00347             vsin1 = osgSin(_mfSkyAngle[j]);
00348
00349             for(i = 0; i < sr; ++i)
00350             {
00351                 glVertex3f(vsin1 * _sinval[i], vcos1, vsin1 * _cosval[i]);
00352             }
00353
00354             glEnd();
00355         }
00356     }
00357
00358     // Draw the ground.
00359     // It's possible to be smarter about this, but for now just overdraw.
00360
00361     if(_mfGroundAngle.size() > 0)
00362     {
00363         vcos1 = -osgCos(_mfGroundAngle[0]);
00364         vsin1 =  osgSin(_mfGroundAngle[0]);
00365
00366         glBegin(GL_TRIANGLE_FAN);
00367
00368         if(_mfGroundColor.size())
00369         {
00370             glColor4fv(
00371                 static_cast<const GLfloat* >(
00372                     _mfGroundColor[0].getValuesRGBA()));
00373         }
00374
00375         glVertex3f(0, -1, 0);
00376
00377         if(_mfGroundColor.size() > 1)
00378         {
00379             glColor4fv(
00380                 static_cast<const GLfloat *>(
00381                     _mfGroundColor[1].getValuesRGBA()));
00382         }
00383
00384         for(i = 0; i < sr; ++i)
00385         {
00386             glVertex3f(vsin1 * _sinval[i], vcos1, vsin1 * _cosval[i]);
00387         }
00388
00389         glEnd();
00390
00391
00392         for(j = 0; j < _mfGroundAngle.size() - 1; ++j)
00393         {
00394             Color4f c1, c2;
00395
00396             if (_mfGroundColor.size() > j+2)
00397             {
00398                 c1 = _mfGroundColor[j+1];
00399                 c2 = _mfGroundColor[j+2];
00400             }
00401
00402             vcos1 = -osgCos(_mfGroundAngle[j  ]);
00403             vsin1 =  osgSin(_mfGroundAngle[j  ]);
00404             vcos2 = -osgCos(_mfGroundAngle[j+1]);
00405             vsin2 =  osgSin(_mfGroundAngle[j+1]);
00406
00407             glBegin(GL_TRIANGLE_STRIP);
00408
00409             for(i = 0; i < sr; ++i)
00410             {
00411                 glColor4fv(static_cast<const GLfloat *>(c1.getValuesRGBA()));
00412                 glVertex3f(vsin1 * _sinval[i], vcos1, vsin1 * _cosval[i]);
00413                 glColor4fv(static_cast<const GLfloat *>(c2.getValuesRGBA()));
00414                 glVertex3f(vsin2 * _sinval[i], vcos2, vsin2 * _cosval[i]);
00415             }
00416             glEnd();
00417         }
00418     }
00419
00420     // now draw the textures, if set
00421           StateChunk *tchunk = NULL;
00422     const Vec3f      *pTexCoords;
00423
00424     pTexCoords = selectTexCoords(
00425         getMFBackTexCoord()->size() ? &getMFBackTexCoord()->front() : NULL,
00426         getBackTexture(), 1);
00427
00428     drawFace(pEnv, getBackTexture(),   tchunk,
00429                                          Pnt3f( 0.5, -0.5,  0.5),
00430                                          Pnt3f(-0.5, -0.5,  0.5),
00431                                          Pnt3f(-0.5,  0.5,  0.5),
00432                                          Pnt3f( 0.5,  0.5,  0.5),
00433                                          pTexCoords             );
00434
00435     pTexCoords = selectTexCoords(
00436         getMFFrontTexCoord()->size() ? &getMFFrontTexCoord()->front() : NULL,
00437         getFrontTexture(), 2);
00438
00439     drawFace(pEnv, getFrontTexture(),  tchunk,
00440                                          Pnt3f(-0.5, -0.5, -0.5),
00441                                          Pnt3f( 0.5, -0.5, -0.5),
00442                                          Pnt3f( 0.5,  0.5, -0.5),
00443                                          Pnt3f(-0.5,  0.5, -0.5),
00444                                          pTexCoords              );
00445
00446     pTexCoords = selectTexCoords(
00447         getMFBottomTexCoord()->size() ? &getMFBottomTexCoord()->front() : NULL,
00448         getBottomTexture(), 3);
00449
00450     drawFace(pEnv, getBottomTexture(), tchunk,
00451                                          Pnt3f(-0.5, -0.5,  0.5),
00452                                          Pnt3f( 0.5, -0.5,  0.5),
00453                                          Pnt3f( 0.5, -0.5, -0.5),
00454                                          Pnt3f(-0.5, -0.5, -0.5),
00455                                          pTexCoords              );
00456
00457     pTexCoords = selectTexCoords(
00458         getMFTopTexCoord()->size() ? &getMFTopTexCoord()->front() : NULL,
00459         getTopTexture(), 4);
00460
00461     drawFace(pEnv, getTopTexture(),    tchunk,
00462                                          Pnt3f(-0.5,  0.5, -0.5),
00463                                          Pnt3f( 0.5,  0.5, -0.5),
00464                                          Pnt3f( 0.5,  0.5,  0.5),
00465                                          Pnt3f(-0.5,  0.5,  0.5),
00466                                          pTexCoords              );
00467
00468     pTexCoords = selectTexCoords(
00469         getMFLeftTexCoord()->size() ? &getMFLeftTexCoord()->front() : NULL,
00470         getLeftTexture(), 5);
00471
00472     drawFace(pEnv, getLeftTexture(),   tchunk,
00473                                          Pnt3f(-0.5, -0.5,  0.5),
00474                                          Pnt3f(-0.5, -0.5, -0.5),
00475                                          Pnt3f(-0.5,  0.5, -0.5),
00476                                          Pnt3f(-0.5,  0.5,  0.5),
00477                                          pTexCoords              );
00478
00479     pTexCoords = selectTexCoords(
00480         getMFRightTexCoord()->size() ? &getMFRightTexCoord()->front() : NULL,
00481         getRightTexture(), 6);
00482
00483     drawFace(pEnv, getRightTexture(),  tchunk,
00484                                          Pnt3f( 0.5, -0.5, -0.5),
00485                                          Pnt3f( 0.5, -0.5,  0.5),
00486                                          Pnt3f( 0.5,  0.5,  0.5),
00487                                          Pnt3f( 0.5,  0.5, -0.5),
00488                                          pTexCoords              );
00489     if(tchunk != NULL)
00490         tchunk->deactivate(pEnv);
00491
00492     Int32 bit = getClearStencilBit();
00493
00494     glClearDepth(1.f);
00495
00496     if(bit >= 0)
00497     {
00498         glClearStencil(bit);
00499         glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
00500     }
00501     else
00502     {
00503         glClear(GL_DEPTH_BUFFER_BIT);
00504     }
00505
00506     glPopMatrix();
00507     glMatrixMode(GL_MODELVIEW);
00508     glPopMatrix();
00509
00510     glPopAttrib();
00511
00512     glColor3f(1.0, 1.0, 1.0);
00513 }
00514