Sun.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/Sun.h>
00022 #include <landscapedef/LandscapeTex.h>
00023 #include <landscapedef/LandscapeDefn.h>
00024 #include <landscapemap/LandscapeMaps.h>
00025 #include <client/ScorchedClient.h>
00026 #include <common/OptionsScorched.h>
00027 #include <graph/OptionsDisplay.h>
00028 #include <graph/MainCamera.h>
00029 #include <common/Defines.h>
00030 #include <common/Vector4.h>
00031 #include <image/ImageFactory.h>
00032 #include <GLEXT/GLLenseFlare.h>
00033 #include <GLEXT/GLCameraFrustum.h>
00034 #include <GLEXT/GLViewPort.h>
00035 #include <stdlib.h>
00036 #include <math.h>
00037 
00038 Sun::Sun()
00039 {
00040 }
00041 
00042 Sun::~Sun()
00043 {
00044 }
00045 
00046 void Sun::setPosition(float sunRotXY, float sunRotYZ)
00047 {
00048         LandscapeDefn &defn = *ScorchedClient::instance()->
00049                 getLandscapeMaps().getDefinitions().getDefn();
00050 
00051         sunRotXY = sunRotXY / 180.0f * 3.14f;
00052         sunRotYZ = sunRotYZ / 180.0f * 3.14f;
00053         position_ = Vector(
00054                 (sinf(sunRotXY) * 900.0f * cosf(sunRotYZ)) + defn.getLandscapeWidth() / 2.0f, 
00055                 (cosf(sunRotXY) * 900.0f * cosf(sunRotYZ)) + defn.getLandscapeHeight() / 2.0f, 
00056                 (sinf(sunRotYZ) * 900.0f));
00057 }
00058 
00059 void Sun::generate()
00060 {
00061         LandscapeTex &tex = *ScorchedClient::instance()->
00062                 getLandscapeMaps().getDefinitions().getTex();
00063 
00064         std::string file = S3D::getDataFile(tex.suntexture.c_str());
00065         if (!tex.suntexturemask.c_str()[0])
00066         {
00067                 ImageHandle map = ImageFactory::loadImageHandle(file.c_str(), file.c_str(), false);
00068                 DIALOG_ASSERT(texture_.replace(map, true));
00069         }
00070         else
00071         {
00072                 std::string mask = S3D::getDataFile(tex.suntexturemask.c_str());
00073                 ImageHandle map = ImageFactory::loadImageHandle(file.c_str(), mask.c_str(), true);
00074                 DIALOG_ASSERT(texture_.replace(map, true));
00075         }
00076 }
00077 
00078 void Sun::setLightPosition(bool light0)
00079 {
00080         LandscapeTex &tex = *ScorchedClient::instance()->
00081                 getLandscapeMaps().getDefinitions().getTex();
00082 
00083         Vector4 sunPosition = getPosition();
00084         if (light0)
00085         {
00086                 Vector &cameraPos = MainCamera::instance()->getTarget().getCamera().getCurrentPos();
00087                 sunPosition = getPosition();
00088                 sunPosition[0] -= cameraPos[0];
00089                 sunPosition[1] -= cameraPos[1];
00090         }
00091 
00092         Vector4 sunDiffuse = tex.skydiffuse;
00093         Vector4 sunAmbient = tex.skyambience;
00094 
00095         GLenum lightNo = light0?GL_LIGHT0:GL_LIGHT1;
00096         glLightfv(lightNo, GL_AMBIENT, sunAmbient);
00097         glLightfv(lightNo, GL_DIFFUSE, sunDiffuse);
00098         glLightfv(lightNo, GL_POSITION, sunPosition);
00099 }
00100 
00101 void Sun::draw()
00102 {
00103         GLState currentStateOne(GLState::TEXTURE_ON | GLState::DEPTH_OFF | GLState::BLEND_ON);
00104         texture_.draw();
00105 
00106         LandscapeTex &tex = *ScorchedClient::instance()->
00107                 getLandscapeMaps().getDefinitions().getTex();
00108 
00109         GLCameraFrustum::instance()->drawBilboard(
00110                 position_, 
00111                 tex.suncolor, 
00112                 1.0f, // alpha
00113                 60.0f, 60.0f, // width, height
00114                 !tex.nosunblend, // additive texture
00115                 0); // tex coord
00116 }
00117 

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