MissileMesh.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 <math.h>
00022 #include <tankgraph/MissileMesh.h>
00023 #include <graph/ModelRenderer.h>
00024 #include <graph/ModelRendererStore.h>
00025 #include <3dsparse/Model.h>
00026 #include <GLEXT/GLLenseFlare.h>
00027 #include <landscape/Landscape.h>
00028 #include <landscapemap/LandscapeMaps.h>
00029 #include <client/ScorchedClient.h>
00030 #include <common/Defines.h> // For porting
00031 
00032 MissileMesh::MissileMesh(ModelID &missile) : 
00033         innerScale_(1.0f),
00034         scale_(1.0f)
00035 {
00036         model_ = ModelRendererStore::instance()->loadModel(missile);
00037 
00038         // Make sure the missile is not too large
00039         const float maxSize = 2.0f;
00040         Vector min = model_->getModel()->getMin();
00041         Vector max = model_->getModel()->getMax();
00042         float size = (max - min).Magnitude();
00043         if (size > maxSize) innerScale_ = 2.2f / size;
00044 
00045         // Add lense flares (if any)
00046         std::vector<Mesh *>::iterator itor;
00047         for (itor = model_->getModel()->getMeshes().begin();
00048                 itor != model_->getModel()->getMeshes().end();
00049                 itor++)
00050         {
00051                 Mesh *mesh = (*itor);
00052                 const char *name = mesh->getName();
00053                 if (strstr(name, "\"Flare") == name ||
00054                         strstr(name, "\"flare") == name)
00055                 {
00056                         FlareInfo info;
00057 
00058                         // Find the center that the flare should be eminated from
00059                         info.position = (mesh->getMax() + mesh->getMin()) / 2.0f;
00060                         info.size = MAX(1.0f, (mesh->getMax() - mesh->getMin()).Magnitude());
00061 
00062                         flares_.push_back(info);
00063                 }
00064         }       
00065 }
00066 
00067 MissileMesh::~MissileMesh()
00068 {
00069 }
00070 
00071 void MissileMesh::draw(Vector &position, Vector &direction, int flareType, float rotation, float frame)
00072 {
00073         // Figure out the opengl roation matrix from the direction
00074         // of the fired missile
00075         Vector dir = direction.Normalize();
00076         const float radToDeg = 180.0f / 3.14f;
00077         float angXYRad = 3.14f - atan2f(dir[0], dir[1]);
00078         float angYZRad = acosf(dir[2]);
00079         float angXYDeg = angXYRad * radToDeg;
00080         float angYZDeg = angYZRad * radToDeg;
00081 
00082         // Apply the matrix and render the missile
00083         float scale = innerScale_ * scale_;
00084         glPushMatrix();
00085                 glTranslatef(position[0], position[1], position[2]);
00086                 
00087                 glRotatef(angXYDeg, 0.0f, 0.0f, 1.0f);
00088                 glRotatef(angYZDeg, 1.0f, 0.0f, 0.0f);
00089 
00090                 glRotatef(rotation, 0.0f, 0.0f, 1.0f);
00091                 glScalef(scale, scale, scale);
00092                 model_->draw(frame, 0.0f, 1.0f, true);
00093         glPopMatrix();
00094 
00095         // Draw any lense flares associated with the missile
00096         std::list<FlareInfo>::iterator flareItor;
00097         for (flareItor =  flares_.begin();
00098                  flareItor != flares_.end();
00099                  flareItor++)
00100         {
00101                 FlareInfo info = (*flareItor);
00102 
00103                 float newX = info.position[0];
00104                 float newY = info.position[1];
00105                 float newZ = info.position[2];
00106 
00107                 float newX2 = 
00108                         (newX * getFastCos(rotation)) - 
00109                         (newY * getFastSin(rotation));
00110                 float newY2 = 
00111                         (newX * getFastSin(rotation)) + 
00112                         (newY * getFastCos(rotation)); 
00113                 float newZ2 = 
00114                         newZ;
00115 
00116         float newX3 = 
00117                         newX2;
00118                 float newY3 = 
00119                         (newY2 * getFastCos(angYZRad)) - 
00120                         (newZ2 * getFastSin(angYZRad));
00121                 float newZ3 = 
00122                         (newY2 * getFastSin(angYZRad)) + 
00123                         (newZ2 * getFastCos(angYZRad)); 
00124 
00125                 float newX4 = 
00126                         (newX3 * getFastCos(angXYRad)) - 
00127                         (newY3 * getFastSin(angXYRad));
00128                 float newY4 = 
00129                         (newX3 * getFastSin(angXYRad)) + 
00130                         (newY3 * getFastCos(angXYRad)); 
00131                 float newZ4 = 
00132                         newZ3;
00133 
00134                 Vector newPos;
00135                 newPos[0] = position[0] + newX4 * scale;
00136                 newPos[1] = position[1] + newY4 * scale;
00137                 newPos[2] = position[2] + newZ4 * scale;
00138 
00139                 GLLenseFlare::instance()->draw(newPos, false, flareType, info.size);
00140         }
00141 }
00142 
00143 void MissileMesh::setScale(float scale)
00144 {
00145         scale_=scale;
00146 }

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