RenderGeoms.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 <tankgraph/RenderGeoms.h>
00022 #include <target/TargetSpace.h>
00023 #include <target/TargetContainer.h>
00024 #include <target/TargetLife.h>
00025 #include <client/ScorchedClient.h>
00026 #include <graph/OptionsDisplay.h>
00027 #include <engine/ActionController.h>
00028 #include <GLEXT/GLState.h>
00029 
00030 RenderGeoms *RenderGeoms::instance()
00031 {
00032         static RenderGeoms instance;
00033         return &instance;
00034 }
00035 
00036 RenderGeoms::RenderGeoms()
00037 {
00038 }
00039 
00040 RenderGeoms::~RenderGeoms()
00041 {
00042 }
00043 
00044 void RenderGeoms::draw(const unsigned state)
00045 {
00046         if (OptionsDisplay::instance()->getDrawCollisionGeoms())
00047         {
00048                 drawCollisionGeoms();
00049                 drawCollisionBounds();
00050         }
00051 
00052         if (OptionsDisplay::instance()->getDrawCollisionSpace())
00053         {
00054                 drawTargetSpace();
00055         }
00056 }
00057 
00058 void RenderGeoms::drawTargetSpace()
00059 {
00060         ScorchedClient::instance()->getContext().getTargetSpace().draw();
00061 }
00062 
00063 void RenderGeoms::drawCollisionBounds()
00064 {
00065         GLState glState(GLState::TEXTURE_OFF);
00066 
00067         glColor3f(0.0f, 0.0f, 1.0f);
00068         std::map<unsigned int, Target *> &targets = 
00069                 ScorchedClient::instance()->getTargetContainer().getTargets();
00070         std::map<unsigned int, Target *>::iterator itor;
00071         for (itor = targets.begin();
00072                 itor != targets.end();
00073                 itor++)
00074         {
00075                 Target *target = (*itor).second;
00076                 if (!target->getAlive())
00077                 {
00078                         continue;
00079                 }
00080 
00081                 Vector position = target->getLife().getFloatPosition();
00082                 Vector size = target->getLife().getAabbSize().asVector();
00083                 position[2] += size[2] / 2.0f;
00084 
00085                 double wid = size[0] + 0.1f;
00086                 double hgt = size[1] + 0.1f;
00087                 double dep = size[2] + 0.1f;
00088 
00089                 glPushMatrix();
00090                         glTranslated(position[0], position[1], position[2]);
00091                         glBegin(GL_LINE_LOOP);
00092                                 // Top
00093                                 glNormal3d(0,1,0);
00094                                 glVertex3d(-wid/2,hgt/2,dep/2);
00095                                 glVertex3d(wid/2,hgt/2,dep/2);
00096                                 glVertex3d(wid/2,hgt/2,-dep/2);
00097                                 glVertex3d(-wid/2,hgt/2,-dep/2);
00098                         glEnd();
00099                         glBegin(GL_LINE_LOOP);
00100                                 // Back
00101                                 glNormal3d(0,0,1);
00102                                 glVertex3d(-wid/2,hgt/2,dep/2);
00103                                 glVertex3d(-wid/2,-hgt/2,dep/2);
00104                                 glVertex3d(wid/2,-hgt/2,dep/2);
00105                                 glVertex3d(wid/2,hgt/2,dep/2);
00106                         glEnd();
00107                         glBegin(GL_LINE_LOOP);
00108                                 // Front
00109                                 glNormal3d(0,0,-1);
00110                                 glVertex3d(-wid/2,hgt/2,-dep/2);
00111                                 glVertex3d(wid/2,hgt/2,-dep/2);
00112                                 glVertex3d(wid/2,-hgt/2,-dep/2);
00113                                 glVertex3d(-wid/2,-hgt/2,-dep/2);
00114                         glEnd();
00115                         glBegin(GL_LINE_LOOP);
00116                                 // Left
00117                                 glNormal3d(1,0,0);
00118                                 glVertex3d(wid/2,hgt/2,-dep/2);
00119                                 glVertex3d(wid/2,hgt/2,dep/2);
00120                                 glVertex3d(wid/2,-hgt/2,dep/2);
00121                                 glVertex3d(wid/2,-hgt/2,-dep/2);
00122                         glEnd();
00123                         glBegin(GL_LINE_LOOP);
00124                                 // Right
00125                                 glNormal3d(-1,0,0);
00126                                 glVertex3d(-wid/2,hgt/2,dep/2);
00127                                 glVertex3d(-wid/2,hgt/2,-dep/2);
00128                                 glVertex3d(-wid/2,-hgt/2,-dep/2);
00129                                 glVertex3d(-wid/2,-hgt/2,dep/2);
00130                         glEnd();
00131                         glBegin(GL_LINE_LOOP);
00132                                 // Bottom
00133                                 glNormal3d(0,-1,0);
00134                                 glVertex3d(-wid/2,-hgt/2,dep/2);
00135                                 glVertex3d(-wid/2,-hgt/2,-dep/2);
00136                                 glVertex3d(wid/2,-hgt/2,-dep/2);
00137                                 glVertex3d(wid/2,-hgt/2,dep/2);
00138                         glEnd();
00139                 glPopMatrix();
00140         }
00141 }
00142 
00143 void RenderGeoms::drawCollisionGeoms()
00144 {
00145         GLState glState(GLState::TEXTURE_OFF);
00146 
00147         glColor3f(1.0f, 1.0f, 1.0f);
00148         std::map<unsigned int, Target *> &targets = 
00149                 ScorchedClient::instance()->getTargetContainer().getTargets();
00150         std::map<unsigned int, Target *>::iterator itor;
00151         for (itor = targets.begin();
00152                 itor != targets.end();
00153                 itor++)
00154         {
00155                 Target *target = (*itor).second;
00156                 if (!target->getAlive())
00157                 {
00158                         continue;
00159                 }
00160 
00161                 if (target->getLife().getBoundingSphere())
00162                 {
00163                         Vector position = target->getLife().getFloatPosition();
00164                         Vector &size = target->getLife().getSize().asVector();
00165                         position[2] += size[2] / 2.0f;
00166                         float radius = MAX(MAX(size[0], size[1]), size[2]) / 2.0f;
00167 
00168                         static GLUquadric *obj = 0;
00169                         if (!obj)
00170                         {
00171                                 obj = gluNewQuadric();
00172                                 gluQuadricDrawStyle(obj, GLU_LINE);
00173                         }
00174 
00175                         glPushMatrix();
00176                                 glTranslated(position[0], position[1], position[2]);
00177                                 gluSphere(obj, radius, 6, 6);
00178                         glPopMatrix();
00179                 }
00180                 else
00181                 {
00182                         Vector position = target->getLife().getFloatPosition();
00183                         Vector &size = target->getLife().getSize().asVector();
00184                         position[2] += size[2] / 2.0f;
00185 
00186                         double wid = size[0];
00187                         double hgt = size[1];
00188                         double dep = size[2];
00189 
00190                         static float rotMatrix[16];
00191                         target->getLife().getQuaternion().getOpenGLRotationMatrix(rotMatrix);
00192 
00193                         glPushMatrix();
00194                                 glTranslated(position[0], position[1], position[2]);
00195                                 glMultMatrixf(rotMatrix);
00196                                 glBegin(GL_LINE_LOOP);
00197                                         // Top
00198                                         glNormal3d(0,1,0);
00199                                         glVertex3d(-wid/2,hgt/2,dep/2);
00200                                         glVertex3d(wid/2,hgt/2,dep/2);
00201                                         glVertex3d(wid/2,hgt/2,-dep/2);
00202                                         glVertex3d(-wid/2,hgt/2,-dep/2);
00203                                 glEnd();
00204                                 glBegin(GL_LINE_LOOP);
00205                                         // Back
00206                                         glNormal3d(0,0,1);
00207                                         glVertex3d(-wid/2,hgt/2,dep/2);
00208                                         glVertex3d(-wid/2,-hgt/2,dep/2);
00209                                         glVertex3d(wid/2,-hgt/2,dep/2);
00210                                         glVertex3d(wid/2,hgt/2,dep/2);
00211                                 glEnd();
00212                                 glBegin(GL_LINE_LOOP);
00213                                         // Front
00214                                         glNormal3d(0,0,-1);
00215                                         glVertex3d(-wid/2,hgt/2,-dep/2);
00216                                         glVertex3d(wid/2,hgt/2,-dep/2);
00217                                         glVertex3d(wid/2,-hgt/2,-dep/2);
00218                                         glVertex3d(-wid/2,-hgt/2,-dep/2);
00219                                 glEnd();
00220                                 glBegin(GL_LINE_LOOP);
00221                                         // Left
00222                                         glNormal3d(1,0,0);
00223                                         glVertex3d(wid/2,hgt/2,-dep/2);
00224                                         glVertex3d(wid/2,hgt/2,dep/2);
00225                                         glVertex3d(wid/2,-hgt/2,dep/2);
00226                                         glVertex3d(wid/2,-hgt/2,-dep/2);
00227                                 glEnd();
00228                                 glBegin(GL_LINE_LOOP);
00229                                         // Right
00230                                         glNormal3d(-1,0,0);
00231                                         glVertex3d(-wid/2,hgt/2,dep/2);
00232                                         glVertex3d(-wid/2,hgt/2,-dep/2);
00233                                         glVertex3d(-wid/2,-hgt/2,-dep/2);
00234                                         glVertex3d(-wid/2,-hgt/2,dep/2);
00235                                 glEnd();
00236                                 glBegin(GL_LINE_LOOP);
00237                                         // Bottom
00238                                         glNormal3d(0,-1,0);
00239                                         glVertex3d(-wid/2,-hgt/2,dep/2);
00240                                         glVertex3d(-wid/2,-hgt/2,-dep/2);
00241                                         glVertex3d(wid/2,-hgt/2,-dep/2);
00242                                         glVertex3d(wid/2,-hgt/2,dep/2);
00243                                 glEnd();
00244                         glPopMatrix();
00245                 }
00246         }
00247 }

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