GLWWindView.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 <GLW/GLWWindView.h>
00022 #include <client/ScorchedClient.h>
00023 #include <graph/MainCamera.h>
00024 #include <GLEXT/GLViewPort.h>
00025 #include <common/OptionsTransient.h>
00026 #include <common/Defines.h>
00027 #include <3dsparse/ModelStore.h>
00028 #include <graph/ModelRenderer.h>
00029 #include <graph/ModelRendererStore.h>
00030 #include <graph/ModelRendererSimulator.h>
00031 #include <landscape/Landscape.h>
00032 #include <landscapemap/LandscapeMaps.h>
00033 #include <lang/LangResource.h>
00034 #include <sky/Sky.h>
00035 #include <math.h>
00036 
00037 REGISTER_CLASS_SOURCE(GLWWindView);
00038 
00039 WindDialogToolTip::WindDialogToolTip()
00040 {
00041 }
00042 
00043 WindDialogToolTip::~WindDialogToolTip()
00044 {
00045 }
00046 
00047 void WindDialogToolTip::populate()
00048 {
00049         LangString wallTypeStr = LANG_RESOURCE("WALLS_NONE", "Currently no walls");
00050         OptionsTransient::WallType wallType =
00051                 ScorchedClient::instance()->getOptionsTransient().getWallType();
00052         switch (wallType)
00053         {
00054         case OptionsTransient::wallBouncy:
00055                 wallTypeStr = LANG_RESOURCE("WALLS_BOUNCY", "Current Wall Type : Bouncy");
00056                 break;
00057         case OptionsTransient::wallConcrete:
00058                 wallTypeStr = LANG_RESOURCE("WALLS_CONCRETE", "Current Wall Type : Concrete");
00059                 break;
00060         case OptionsTransient::wallWrapAround:
00061                 wallTypeStr = LANG_RESOURCE("WALL_WRAP", "Current Wall Type : Wrap Around");
00062                 break;
00063         }
00064 
00065         if (ScorchedClient::instance()->
00066                 getOptionsTransient().getWindSpeed() == 0)
00067         {
00068                 setText(ToolTip::ToolTipHelp, 
00069                         LANG_RESOURCE("WIND", "Wind"), 
00070                         LANG_RESOURCE("WIND_TOOLTIP_NOWIND", 
00071                         "Displays the current wind direction\n"
00072                         "and speed, and the wall type.\n"
00073                         "Currently No Wind.\n") + 
00074                         wallTypeStr);
00075         }
00076         else
00077         {
00078                 setText(ToolTip::ToolTipHelp, LANG_RESOURCE("WIND", "Wind"), 
00079                         LANG_RESOURCE_1("WIND_TOOLTIP_WIND",
00080                         "Displays the current wind direction\n"
00081                         "and speed, and the wall type.\n"
00082                         "Current Wind Force : {0} (out of 5)\n",
00083                         S3D::formatStringBuffer("%i", (int) ScorchedClient::instance()->
00084                         getOptionsTransient().getWindSpeed().asFloat())) +
00085                         wallTypeStr);
00086         }
00087 }
00088 
00089 GLWWindView::GLWWindView(float x, float y, float w, float h) :
00090         GLWidget(x, y, w, h),
00091         listNo_(0), changeCount_(0), windModel_(0)
00092 {
00093         setToolTip(new WindDialogToolTip());
00094 }
00095 
00096 GLWWindView::~GLWWindView()
00097 {
00098         delete windModel_;
00099         if (glIsList(listNo_)) glDeleteLists(listNo_, 1);
00100 }
00101 
00102 void GLWWindView::draw()
00103 {
00104         if (!windModel_)
00105         {
00106                 ModelID id;
00107                 id.initFromString("ase", "data/meshes/wind.ase", "none");
00108                 windModel_ = new ModelRendererSimulator(
00109                         ModelRendererStore::instance()->loadModel(id));
00110         }
00111 
00112         if (changeCount_ != Landscape::instance()->getChangeCount())
00113         {
00114                 changeCount_ = Landscape::instance()->getChangeCount();
00115                 if (glIsList(listNo_)) glDeleteLists(listNo_, 1);
00116                 listNo_ = 0;
00117         }
00118 
00119         GLWidget::draw();
00120         drawDisplay();
00121 }
00122 
00123 void GLWWindView::drawDisplay()
00124 {
00125         Vector &lookFrom = MainCamera::instance()->getCamera().getCurrentPos();
00126         Vector &lookAt = MainCamera::instance()->getCamera().getLookAt();
00127         Vector dir = (lookAt - lookFrom).Normalize();
00128 
00129         float scale2 = MIN(w_, h_) / 90.0f;
00130         float angXY = atan2f(dir[0], dir[1]) / 3.14f * 180.0f;
00131         float angYZ = acosf(dir[2]) / 3.14f * 180.0f + 180.0f;
00132         if (angYZ < 280.0f) angYZ = 280.0f;
00133 
00134         GLState currentState(GLState::DEPTH_ON | GLState::TEXTURE_ON);
00135         glPushMatrix();
00136                 glTranslatef(x_ + w_ / 2.0f, y_ + h_ / 2.0f, 0.0f);
00137                 glRotatef(angYZ, 1.0f, 0.0f, 0.0f);
00138                 glRotatef(angXY, 0.0f, 0.0f, 1.0f);
00139 
00140                 Landscape::instance()->getSky().getSun().setLightPosition(); // Reset light
00141 
00142                 // Draw the minature landscape
00143                 glScalef(scale2, scale2, scale2);
00144                 glPushMatrix();
00145                         if (listNo_) glCallList(listNo_);
00146                         else
00147                         {
00148                                 glNewList(listNo_ = glGenLists(1), GL_COMPILE_AND_EXECUTE);
00149                                         drawScene();
00150                                 glEndList();
00151                         }
00152                 glPopMatrix();
00153 
00154                 // Draw the wind arrow for direction
00155                 GLState texState(GLState::TEXTURE_OFF);
00156                 if (ScorchedClient::instance()->getOptionsTransient().getWindOn())
00157                 {
00158                         Vector4 sunPosition(-100.0f, 100.0f, 400.0f, 1.0f);
00159                         Vector4 sunDiffuse(0.9f, 0.9f, 0.9f, 1.0f);
00160                         Vector4 sunAmbient(0.4f, 0.4f, 0.4f, 1.0f);
00161                         glLightfv(GL_LIGHT1, GL_AMBIENT, sunAmbient);
00162                         glLightfv(GL_LIGHT1, GL_DIFFUSE, sunDiffuse);
00163                         glLightfv(GL_LIGHT1, GL_POSITION, sunPosition);
00164 
00165                         glTranslatef(0.0f, 0.0f, 20.0f);
00166                         glScalef(0.1f, 0.1f, 0.1f);
00167 
00168                         glRotatef(-ScorchedClient::instance()->getOptionsTransient().getWindAngle().asFloat(), 
00169                                 0.0f, 0.0f, 1.0f);
00170                         drawArrow();
00171                 }
00172         glPopMatrix();
00173 
00174         Landscape::instance()->getSky().getSun().setLightPosition(); // Reset light
00175 }
00176 
00177 void GLWWindView::drawScene()
00178 {
00179         Landscape::instance()->getPlanTexture().draw(true);
00180         glColor3f(1.0f, 1.0f, 1.0f);
00181 
00182         HeightMap &hmp = ScorchedClient::instance()->getLandscapeMaps().
00183                 getGroundMaps().getHeightMap();
00184 
00185         int arenaX = ScorchedClient::instance()->getLandscapeMaps().
00186                 getGroundMaps().getArenaX();
00187         int arenaY = ScorchedClient::instance()->getLandscapeMaps().
00188                 getGroundMaps().getArenaY();
00189         int arenaWidth = ScorchedClient::instance()->getLandscapeMaps().
00190                 getGroundMaps().getArenaWidth();
00191         int arenaHeight = ScorchedClient::instance()->getLandscapeMaps().
00192                 getGroundMaps().getArenaHeight();
00193         int landscapeWidth = ScorchedClient::instance()->getLandscapeMaps().
00194                 getGroundMaps().getLandscapeWidth();
00195         int landscapeHeight = ScorchedClient::instance()->getLandscapeMaps().
00196                 getGroundMaps().getLandscapeHeight();
00197 
00198         int maxSize = MAX(arenaWidth, arenaHeight);
00199         float scale = 60.0f / maxSize;
00200         glScalef(scale, scale, scale);
00201         glTranslatef(arenaWidth/-2.0f-arenaX, arenaHeight/-2.0f-arenaY, 0.0f);
00202 
00203         int sqSizeW = arenaWidth / 16;
00204         int sqSizeH = arenaHeight / 16;
00205         for (int y=arenaY; y<=arenaY+arenaHeight-sqSizeH; y+=sqSizeH)
00206         {
00207                 glBegin(GL_QUAD_STRIP);
00208                 for (int x=arenaX; x<=arenaX+arenaWidth; x+=sqSizeW)
00209                 {
00210                         float xPer = float(x) / float(landscapeWidth);
00211                         float yPer = float(y) / float(landscapeHeight);
00212                         float yPer2 = float(y + sqSizeH) / float(landscapeHeight);
00213 
00214                         glTexCoord2f(xPer, yPer2);
00215                         glVertex3f(float(x), float(y + sqSizeH), hmp.getHeight(x, y+sqSizeH).asFloat());
00216                         glTexCoord2f(xPer, yPer);
00217                         glVertex3f(float(x), float(y), hmp.getHeight(x, y).asFloat());
00218                 }
00219                 glEnd();
00220         }
00221 
00222         Landscape::instance()->getMagTexture().draw();
00223         glBegin(GL_QUAD_STRIP);
00224         for (int y=arenaY; y<=arenaY+arenaHeight; y+=sqSizeH)
00225         {
00226                 float per = float(y - arenaY) / float(arenaHeight);
00227 
00228                 glTexCoord2f(per, 0.0f);
00229                 glVertex3f(float(arenaX), float(y), -15.0f);
00230                 glTexCoord2f(per, 1.0f);
00231                 glVertex3f(float(arenaX), float(y), hmp.getHeight(arenaX, y).asFloat());                
00232         }
00233         for (int x=arenaX; x<=arenaX+arenaWidth; x+=sqSizeW)
00234         {
00235                 float per = float(x - arenaX) / float(arenaWidth);
00236 
00237                 glTexCoord2f(per, 0.0f);
00238                 glVertex3f(float(x), float(arenaY + arenaHeight), -15.0f);
00239                 glTexCoord2f(per, 1.0f);
00240                 glVertex3f(float(x), float(arenaY + arenaHeight), hmp.getHeight(x, arenaY + arenaHeight).asFloat());            
00241         }
00242         for (int y=arenaY+arenaHeight; y>=arenaY; y-=sqSizeH)
00243         {
00244                 float per = float(y - arenaY) / float(arenaHeight);
00245 
00246                 glTexCoord2f(per, 0.0f);
00247                 glVertex3f(float(arenaX + arenaWidth), float(y), -15.0f);
00248                 glTexCoord2f(per, 1.0f);
00249                 glVertex3f(float(arenaX + arenaWidth), float(y), hmp.getHeight(arenaX + arenaWidth, y).asFloat());              
00250         }
00251         for (int x=arenaX+arenaWidth; x>=arenaX; x-=sqSizeW)
00252         {
00253                 float per = float(x - arenaX) / float(arenaWidth);
00254 
00255                 glTexCoord2f(per, 0.0f);
00256                 glVertex3f(float(x), float(arenaY), -15.0f);
00257                 glTexCoord2f(per, 1.0f);
00258                 glVertex3f(float(x), float(arenaY), hmp.getHeight(x, arenaY).asFloat());                
00259         }
00260         glEnd();
00261 }
00262 
00263 void GLWWindView::drawArrow()
00264 {
00265         windModel_->draw();
00266 }
00267 
00268 void GLWWindView::mouseDown(int button, float x, float y, bool &skipRest)
00269 {
00270         if (inBox(x, y, x_, y_, w_, h_))
00271         {
00272                 skipRest = true;
00273 
00274                 unsigned int type =
00275                         MainCamera::instance()->getTarget().getCameraType();
00276                 type++;
00277                 if (type >= TargetCamera::CamFree) type = 0;
00278 
00279                 MainCamera::instance()->getTarget().setCameraType(
00280                         (TargetCamera::CamType) type);
00281         }
00282 }

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