SkyDome.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <sky/SkyDome.h>
00022 #include <sky/Sky.h>
00023 #include <sky/Hemisphere.h>
00024 #include <landscape/Landscape.h>
00025 #include <landscapemap/LandscapeMaps.h>
00026 #include <landscapedef/LandscapeTex.h>
00027 #include <landscapedef/LandscapeDefinition.h>
00028 #include <GLEXT/GLState.h>
00029 #include <GLEXT/GLCamera.h>
00030 #include <image/ImageFactory.h>
00031 #include <client/ScorchedClient.h>
00032 #include <graph/OptionsDisplay.h>
00033 #include <common/OptionsTransient.h>
00034 #include <common/Defines.h>
00035 
00036 SkyDome::SkyDome() : 
00037         xy_(0.0f), 
00038         cloudSpeed_(500.0f), 
00039         flashTime_(0.0f)
00040 {
00041 }
00042 
00043 SkyDome::~SkyDome()
00044 {
00045 }
00046 
00047 void SkyDome::flash()
00048 {
00049         flashTime_ = 0.25f;
00050 }
00051 
00052 void SkyDome::generate()
00053 {
00054         LandscapeTex *tex = ScorchedClient::instance()->
00055                 getLandscapeMaps().getDefinitions().getTex();   
00056 
00057         // Sky
00058         std::string ctex(S3D::getDataFile(tex->skytexture.c_str()));
00059         std::string ctexm(S3D::getDataFile(tex->skytexturemask.c_str()));
00060         ImageHandle bitmapCloud = ImageFactory::loadImageHandle(ctex.c_str(), ctexm.c_str(), false);
00061         DIALOG_ASSERT(cloudTexture_.replace(bitmapCloud));
00062         skyColorsMap_ = ImageFactory::loadImageHandle(S3D::getDataFile(tex->skycolormap.c_str()));
00063 
00064         // Stars
00065         useStarTexture_ = false;
00066         if (!tex->skytexturestatic.empty())
00067         {
00068                 useStarTexture_ = true;
00069                 std::string stex(S3D::getDataFile(tex->skytexturestatic.c_str()));
00070                 ImageHandle bitmapStars = ImageFactory::loadImageHandle(stex.c_str(), stex.c_str(), false);
00071                 DIALOG_ASSERT(starTexture_.replace(bitmapStars));
00072         }
00073         noSunFog_ = tex->nosunfog;
00074         horizonGlow_ = !tex->nohorizonglow;
00075 
00076         // Skyline
00077         skyLine1_.clear();
00078         useSkyLine_ = false;
00079         if (!tex->skyline.empty())
00080         {
00081                 useSkyLine_ = true;
00082                 std::string stex(S3D::getDataFile(tex->skyline.c_str()));
00083                 std::string stexa(S3D::getDataFile(tex->skylinemask.c_str()));
00084                 ImageHandle bitmapSkyLine = ImageFactory::loadImageHandle(stex.c_str(), stexa.c_str());
00085                 DIALOG_ASSERT(skyLineTexture_.replace(bitmapSkyLine));
00086         }
00087 
00088         // Force refresh of colors
00089         colors_.clear();
00090 }
00091 
00092 void SkyDome::simulate(float frameTime)
00093 {
00094         if (OptionsDisplay::instance()->getNoSkyMovement()) return;
00095 
00096         float fastSpeed = 100;
00097         float slowSpeed = 500;
00098         float currentSpeed = ScorchedClient::instance()->getOptionsTransient().getWindSpeed().asFloat();
00099         float wantedAngle = ScorchedClient::instance()->getOptionsTransient().getWindAngle().asFloat();
00100         float wantedSpeed = (((5.0f - currentSpeed) / 5.0f) * (slowSpeed - fastSpeed)) + fastSpeed;
00101 
00102         // Move the cloud layer
00103         cloudSpeed_ = wantedSpeed;
00104         cloudDirection_ = -ScorchedClient::instance()->getOptionsTransient().getWindDirection().asVector();
00105         xy_ += frameTime / cloudSpeed_;
00106 
00107         // The sky flash
00108         flashTime_ -= frameTime;
00109 }
00110 
00111 void SkyDome::drawBackdrop()
00112 {
00113         Vector &pos = GLCamera::getCurrentCamera()->getCurrentPos();
00114         Vector sunDir = 
00115                 Landscape::instance()->getSky().getSun().getPosition().Normalize();
00116         LandscapeTex &tex =
00117                 *ScorchedClient::instance()->getLandscapeMaps().getDefinitions().getTex();
00118 
00119         // Cannot use a display list for heimisphere as we change texture
00120         // coordinates all the time
00121         glPushMatrix();
00122                 // Translate scene so it is in the middle of the camera
00123                 glTranslatef(pos[0], pos[1], -15.0f);
00124 
00125                 // Draw sky color
00126                 GLState mainState2(GLState::TEXTURE_OFF | GLState::BLEND_OFF);
00127                 colors_.drawColored(2000, 225, skyColorsMap_, sunDir, tex.skytimeofday, horizonGlow_);
00128 
00129                 // Draw stars
00130                 if (useStarTexture_)
00131                 {
00132                         glDisable(GL_FOG); 
00133 
00134                         glColor4f(1.0f, 1.0f, 1.0f, 0.7f);
00135                         GLState currentState(GLState::TEXTURE_ON | GLState::BLEND_ON);
00136                         starTexture_.draw();
00137 
00138                         glBlendFunc(GL_SRC_ALPHA, GL_ONE);
00139 
00140                         glMatrixMode(GL_TEXTURE);
00141                         glLoadIdentity();
00142                         glScalef(9.0f, 9.0f, 0.0f);
00143                         glMatrixMode(GL_MODELVIEW);
00144 
00145                         stars_.draw(1990, 215, Hemisphere::eWidthTexture);
00146                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00147 
00148                         glMatrixMode(GL_TEXTURE);
00149                         glLoadIdentity();
00150                         glMatrixMode(GL_MODELVIEW);
00151                 }
00152 
00153         glPopMatrix();
00154 
00155         if (!OptionsDisplay::instance()->getNoFog())
00156         {
00157                 glEnable(GL_FOG);
00158         }
00159 
00160         // Draw Sun Glow
00161         if (noSunFog_) glDisable(GL_FOG); 
00162         Landscape::instance()->getSky().getSun().draw();
00163 
00164         if (!OptionsDisplay::instance()->getNoFog())
00165         {
00166                 glEnable(GL_FOG);
00167         }
00168 }
00169 
00170 void SkyDome::drawLayers()
00171 {
00172         Vector &pos = GLCamera::getCurrentCamera()->getCurrentPos();
00173 
00174         glPushMatrix();
00175                 // Translate scene so it is in the middle of the land
00176                 glTranslatef(pos[0], pos[1], -15.0f);
00177 
00178                 // Skyline
00179                 if (useSkyLine_)
00180                 {
00181                         //glDisable(GL_FOG);
00182 
00183                         skyLineTexture_.draw();
00184                         skyLine1_.draw(1000.0f, 1000.0f, 200.0f);
00185 
00186                         //glEnable(GL_FOG);
00187                 }
00188 
00189                 // Layer 2
00190                 if (!OptionsDisplay::instance()->getNoSkyLayers())
00191                 {
00192                         // Rotate the scene so clouds blow the correct way
00193                         float slowXY = (xy_ + 45.5f) / 1.5f;
00194                         GLState currentState(GLState::TEXTURE_ON | GLState::BLEND_ON);
00195                         glColor4f(1.0f, 1.0f, 1.0f, 0.7f);
00196 
00197                         // Cloud texture
00198                         cloudTexture_.draw();
00199 
00200                         // Cloud Layer 1
00201                         glMatrixMode(GL_TEXTURE);
00202                         glLoadIdentity();
00203                         glTranslatef(xy_ * cloudDirection_[0], 
00204                                 xy_ * cloudDirection_[1], 0.0f);
00205                         glMatrixMode(GL_MODELVIEW);
00206 
00207                         clouds1_.draw(1980, 210, Hemisphere::eWidthTexture);
00208 
00209                         // Cloud Layer 2
00210                         glMatrixMode(GL_TEXTURE);
00211                         glLoadIdentity();
00212                         glTranslatef((slowXY) * (cloudDirection_[0] + cloudDirection_[1] * 0.3f), 
00213                                 (slowXY) * (cloudDirection_[1] - cloudDirection_[0] * 0.3f), 0.0f);
00214                         glMatrixMode(GL_MODELVIEW);
00215 
00216                         clouds2_.draw(1980, 170, Hemisphere::eWidthTexture);
00217 
00218                         // Reset tex matrix
00219                         glMatrixMode(GL_TEXTURE);
00220                         glLoadIdentity();
00221                         glMatrixMode(GL_MODELVIEW);
00222                 }
00223 
00224         glPopMatrix();
00225 }

Generated on Mon Feb 16 15:14:47 2009 for Scorched3D by  doxygen 1.5.3