GLWWeaponModel.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/GLWWeaponModel.h>
00022 #include <client/ScorchedClient.h>
00023 #include <client/ClientState.h>
00024 #include <tankgraph/TargetRendererImplTank.h>
00025 #include <tankgraph/MissileMesh.h>
00026 #include <tank/TankContainer.h>
00027 #include <tank/TankAccessories.h>
00028 #include <tank/TankState.h>
00029 #include <GLW/GLWTranslate.h>
00030 #include <weapons/AccessoryStore.h>
00031 #include <weapons/Accessory.h>
00032 #include <landscape/Landscape.h>
00033 #include <sky/Sky.h>
00034 #include <common/Vector4.h>
00035 #include <common/Defines.h>
00036 
00037 REGISTER_CLASS_SOURCE(GLWWeaponModel);
00038 
00039 GLWWeaponModel::GLWWeaponModel(float x, float y, float w, float h) :
00040         GLWidget(x, y, w, h),
00041         totalTime_(0.0f)
00042 {
00043 }
00044 
00045 GLWWeaponModel::~GLWWeaponModel()
00046 {
00047 }
00048 
00049 void GLWWeaponModel::simulate(float frameTime)
00050 {
00051         GLWidget::simulate(frameTime);
00052         totalTime_+=frameTime;
00053 }
00054 
00055 void GLWWeaponModel::draw()
00056 {
00057         GLWidget::draw();
00058 
00059         Tank *current = 
00060                 ScorchedClient::instance()->getTankContainer().getCurrentTank();
00061         if (!current ||
00062                 current->getState().getState() != TankState::sNormal)
00063         {
00064                 return;
00065         }
00066 
00067         TargetRendererImplTank *renderer = (TargetRendererImplTank *) 
00068                 current->getRenderer();
00069         if (!renderer) return;
00070 
00071         GLWTankTips *tankTips = renderer->getTips();
00072         Accessory *weapon = current->getAccessories().getWeapons().getCurrent();
00073         if (!weapon) return;
00074 
00075         Vector4 sunPosition(-100.0f, 100.0f, 400.0f, 1.0f);
00076         Vector4 sunDiffuse(0.9f, 0.9f, 0.9f, 1.0f);
00077         Vector4 sunAmbient(0.4f, 0.4f, 0.4f, 1.0f);
00078         glLightfv(GL_LIGHT1, GL_AMBIENT, sunAmbient);
00079         glLightfv(GL_LIGHT1, GL_DIFFUSE, sunDiffuse);
00080 
00081         // Cache some stuff we should only fetch when tank
00082         // or tank's weapon changes
00083         static Accessory *storedWeapon = 0;
00084         static Tank *storedTank = 0;
00085         static MissileMesh *storedMesh = 0;
00086         if (weapon != storedWeapon || current != storedTank)
00087         {
00088                 storedWeapon = weapon;
00089                 storedTank = current;
00090                 storedMesh = Accessory::getWeaponMesh(storedWeapon->getModel(), current);
00091         }
00092 
00093         GLState tankState(GLState::TEXTURE_ON | GLState::DEPTH_ON);
00094         setToolTip(&weapon->getToolTip());
00095 
00096         // Draw the current weapon
00097         glPushMatrix();
00098                 glTranslatef(x_ + w_ / 2.0f, y_ + w_ / 2.0f, 0.0f);
00099 
00100                 glLightfv(GL_LIGHT1, GL_POSITION, sunPosition);
00101 
00102                 glRotatef(totalTime_ * 45.0f, 0.0f, 0.0f, 1.0f);
00103                 Vector position;
00104                 Vector direction(0.3f, 1.0f, 1.0f);
00105                 float scale = MIN(storedWeapon->getModelScale().asFloat(), 1.0f);
00106                 storedMesh->setScale(w_ / 3.0f * scale);
00107                 storedMesh->draw(position, direction, 0, totalTime_ * 45.0f, totalTime_ * 20.0f);
00108         glPopMatrix();
00109 
00110         Landscape::instance()->getSky().getSun().setLightPosition(); // Reset light
00111 }
00112 
00113 void GLWWeaponModel::mouseDown(int button, float x, float y, bool &skipRest)
00114 {
00115         Tank *current = 
00116                 ScorchedClient::instance()->getTankContainer().getCurrentTank();
00117         if (!current ||
00118                 current->getState().getState() != TankState::sNormal)
00119         {
00120                 return;
00121         }
00122         TargetRendererImplTank *renderer = (TargetRendererImplTank *) 
00123                 current->getRenderer();
00124         if (!renderer) return;
00125         GLWTankTips *tankTips = renderer->getTips();
00126 
00127         if (inBox(x, y, x_, y_, w_, h_))
00128         {
00129                 skipRest = true;
00130 
00131                 tankTips->weaponTip.showItems(GLWTranslate::getPosX() + x, 
00132                         GLWTranslate::getPosY() + y);
00133         }
00134 }

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