GLWTankModel.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/GLWTankModel.h>
00022 #include <client/ScorchedClient.h>
00023 #include <graph/MainCamera.h>
00024 #include <graph/ModelRendererMesh.h>
00025 #include <client/ClientState.h>
00026 #include <tankgraph/TargetRendererImplTank.h>
00027 #include <tank/TankContainer.h>
00028 #include <tank/TankPosition.h>
00029 #include <common/Vector4.h>
00030 #include <landscape/Landscape.h>
00031 #include <sky/Sky.h>
00032 #include <GLW/GLWTranslate.h>
00033 
00034 REGISTER_CLASS_SOURCE(GLWTankModel);
00035 
00036 GLWTankModel::GLWTankModel(float x, float y, float w, float h) :
00037         GLWidget(x, y, w, h), 
00038         totalTime_(0.0f)
00039 {
00040 }
00041 
00042 GLWTankModel::~GLWTankModel()
00043 {
00044 }
00045 
00046 void GLWTankModel::draw()
00047 {
00048         Tank *current = 
00049                 ScorchedClient::instance()->getTankContainer().getCurrentTank();
00050         if (!current) return;
00051 
00052         TargetRendererImplTank *renderer = (TargetRendererImplTank *) 
00053                 current->getRenderer();
00054         if (!renderer) return;
00055 
00056         Vector4 sunPosition(-100.0f, 100.0f, 400.0f, 1.0f);
00057         Vector4 sunDiffuse(0.9f, 0.9f, 0.9f, 1.0f);
00058         Vector4 sunAmbient(0.4f, 0.4f, 0.4f, 1.0f);
00059         glLightfv(GL_LIGHT1, GL_AMBIENT, sunAmbient);
00060         glLightfv(GL_LIGHT1, GL_DIFFUSE, sunDiffuse);
00061 
00062         // Add the tooltip for the model name+attributes
00063         GLWToolTip::instance()->addToolTip(&renderer->getTips()->tankTip, 
00064                 GLWTranslate::getPosX() + x_ + 20.0f, 
00065                 GLWTranslate::getPosY() + y_ + 20.0f, 80.0f, 80.0f);
00066 
00067         // Find the angles to rotate so the tank is at the
00068         // same angle as the "real" tank on the landscape
00069         Vector &lookFrom = MainCamera::instance()->getCamera().getCurrentPos();
00070         Vector &lookAt = MainCamera::instance()->getCamera().getLookAt();
00071         Vector dir = (lookAt - lookFrom).Normalize();
00072         float angXY = atan2f(dir[0], dir[1]) / 3.14f * 180.0f;
00073         float angYZ = acosf(dir[2]) / 3.14f * 180.0f + 180.0f;
00074         if (angYZ < 280.0f) angYZ = 280.0f;
00075 
00076         // Draw the tank
00077         glPushMatrix();
00078                 // Set the tank angle
00079                 glTranslatef(x_ + w_ / 2.0f, y_ + w_ / 2.0f, 0.0f);
00080 
00081                 glLightfv(GL_LIGHT1, GL_POSITION, sunPosition);
00082 
00083                 glRotatef(angYZ, 1.0f, 0.0f, 0.0f);
00084                 glRotatef(angXY, 0.0f, 0.0f, 1.0f);
00085 
00086                 // Draw the tank model
00087                 glScalef(w_ / 4.0f, w_ / 4.0f, w_ / 4.0f);
00088                 GLState tankState(GLState::TEXTURE_OFF | GLState::DEPTH_ON); // For no tank skins
00089                 Vector position;
00090                 TankMesh *mesh = renderer->getMesh();
00091                 if (mesh)
00092                 {
00093                         Vector4 rotation(1.0f, 0.0f, 0.0f, 0.0f);
00094                         float matrix[16];
00095                         rotation.getOpenGLRotationMatrix(matrix);
00096 
00097                         mesh->draw(
00098                                 totalTime_ * 20.0f,
00099                                 false, matrix, position, 0.0f,
00100                                 current->getPosition().getRotationGunXY().asFloat(),
00101                                 current->getPosition().getRotationGunYZ().asFloat(),
00102                                 true);
00103                 }
00104         glPopMatrix();
00105 
00106         Landscape::instance()->getSky().getSun().setLightPosition(); // Reset light
00107 }
00108 
00109 void GLWTankModel::mouseDown(int button, float x, float y, bool &skipRest)
00110 {
00111         if (inBox(x, y, x_, y_, w_, h_))
00112         {
00113                 skipRest = true;
00114                 MainCamera::instance()->getTarget().setCameraType(TargetCamera::CamBehind);
00115         }
00116 }
00117 
00118 void GLWTankModel::simulate(float frameTime)
00119 {
00120         totalTime_ += frameTime;
00121 }

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