00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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,
00113 60.0f, 60.0f,
00114 !tex.nosunblend,
00115 0);
00116 }
00117