00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <math.h>
00022 #include <tankgraph/TankMesh.h>
00023 #include <GLEXT/GLState.h>
00024 #include <graph/ModelRenderer.h>
00025 #include <graph/OptionsDisplay.h>
00026 #include <common/Defines.h>
00027
00028 TankMesh::TankMesh(Model &tank) :
00029 ModelRendererMesh(&tank),
00030 scale_(1.0f)
00031 {
00032 setupTankMesh();
00033 }
00034
00035 TankMesh::~TankMesh()
00036 {
00037 }
00038
00039 int TankMesh::getNoTris()
00040 {
00041 return (model_?model_->getNumberTriangles():0);
00042 }
00043
00044 void TankMesh::setupTankMesh()
00045 {
00046
00047 const float maxSize = 3.0f;
00048 Vector &min = model_->getMin();
00049 Vector &max = model_->getMax();
00050 float size = (max - min).Magnitude();
00051 if (size > maxSize) scale_ = 2.2f / size;
00052
00053
00054 Mesh *turretPivot = 0, *gunPivot = 0;
00055 int turretCount = 0;
00056
00057 std::vector<Mesh *>::iterator itor;
00058 for (itor = model_->getMeshes().begin();
00059 itor != model_->getMeshes().end();
00060 itor++)
00061 {
00062 Mesh *mesh = (*itor);
00063 const char *name = mesh->getName();
00064
00065 if (strstr(name, "\"Turret") == name ||
00066 strstr(name, "\"turret") == name)
00067 {
00068 if (strstr(name, "pivot") ||
00069 strstr(name, "Pivot"))
00070 {
00071 turretPivot = mesh;
00072 }
00073 else
00074 {
00075
00076 turretCount++;
00077 turretCenter_ += (mesh->getMax() + mesh->getMin()) / 2.0f;
00078 }
00079
00080 meshTypes_.push_back(eTurret);
00081 }
00082 else if (strstr(name, "\"Gun") == name ||
00083 strstr(name, "\"gun") == name)
00084 {
00085 if (strstr(name, "pivot") ||
00086 strstr(name, "Pivot"))
00087 {
00088 gunPivot = mesh;
00089 }
00090
00091 meshTypes_.push_back(eGun);
00092 }
00093 else
00094 {
00095 meshTypes_.push_back(eNone);
00096 }
00097 }
00098
00099
00100 if (turretPivot)
00101 {
00102 turretCenter_ = (turretPivot->getMax() + turretPivot->getMin()) / 2.0f;
00103 }
00104 else
00105 {
00106 turretCenter_ /= float(turretCount);
00107 }
00108 Vector gunCenter = turretCenter_;
00109 turretCenter_[2] = 0.0f;
00110
00111
00112 if (gunPivot)
00113 {
00114 gunCenter = (gunPivot->getMax() + gunPivot->getMin()) / 2.0f;
00115 }
00116 gunOffset_ = gunCenter - turretCenter_;
00117 }
00118
00119 void TankMesh::draw(float frame, bool drawS, float *rotMatrix, Vector &position,
00120 float fireOffset, float rotXY, float rotXZ,
00121 bool absCenter, float scale, float fade, bool setState)
00122 {
00123 rotXY_ = rotXY;
00124 rotXZ_ = rotXZ;
00125 drawS_ = drawS;
00126 fireOffSet_ = fireOffset;
00127
00128 glPushMatrix();
00129 glTranslatef(position[0], position[1], position[2]);
00130 glMultMatrixf(rotMatrix);
00131 glScalef(scale * scale_, scale * scale_, scale * scale_);
00132
00133 if (absCenter) ModelRendererMesh::draw(frame, 0.0f, fade, setState);
00134 else ModelRendererMesh::drawBottomAligned(frame, 0.0f, fade, setState);
00135 glPopMatrix();
00136 }
00137
00138 void TankMesh::drawMesh(unsigned int m, Mesh *mesh, float currentFrame, bool setState)
00139 {
00140 glPushMatrix();
00141 MeshType type = meshTypes_[m];
00142 vertexTranslation_ = -turretCenter_;
00143
00144 if (type == eTurret || type == eGun)
00145 {
00146 glRotatef(rotXY_, 0.0f, 0.0f, 1.0f);
00147 if (type == eGun)
00148 {
00149 glTranslatef(gunOffset_[0], gunOffset_[1], gunOffset_[2]);
00150 vertexTranslation_ -= gunOffset_;
00151 glRotatef(rotXZ_, 1.0f, 0.0f, 0.0f);
00152
00153
00154 if (drawS_ &&
00155 OptionsDisplay::instance()->getDrawPlayerSight() &&
00156 OptionsDisplay::instance()->getOldSightPosition())
00157 {
00158 glPushMatrix();
00159 glScalef(1.0f / scale_, 1.0f / scale_, 1.0f / scale_);
00160 drawSight();
00161 glPopMatrix();
00162 }
00163
00164 if (fireOffSet_ != 0.0f) glTranslatef(0.0f, fireOffSet_, 0.0f);
00165 }
00166 }
00167
00168 ModelRendererMesh::drawMesh(m, mesh, currentFrame, setState);
00169 glPopMatrix();
00170
00171 vertexTranslation_.zero();
00172 }
00173
00174 void TankMesh::drawSight()
00175 {
00176 GLState sightState(GLState::BLEND_OFF | GLState::TEXTURE_OFF | GLState::LIGHTING_OFF);
00177
00178 static GLuint sightList_ = 0;
00179 if (!sightList_)
00180 {
00181 glNewList(sightList_ = glGenLists(1), GL_COMPILE);
00182 glBegin(GL_QUAD_STRIP);
00183 float x;
00184 for (x=126.0f; x>=90.0f; x-=9.0f)
00185 {
00186 const float deg = 3.14f / 180.0f;
00187 float dx = x * deg;
00188 float color = 1.0f - fabsf(90.0f - x) / 45.0f;
00189
00190 glColor3f(1.0f * color, 0.5f * color, 0.5f * color);
00191 glVertex3f(+0.03f * color, 2.0f * sinf(dx), 2.0f * cosf(dx));
00192 glVertex3f(+0.03f * color, 10.0f * sinf(dx), 10.0f * cosf(dx));
00193 }
00194 for (x=90.0f; x<135.0f; x+=9.0f)
00195 {
00196 const float deg = 3.14f / 180.0f;
00197 float dx = x * deg;
00198 float color = 1.0f - fabsf(90.0f - x) / 45.0f;
00199
00200 glColor3f(1.0f * color, 0.5f * color, 0.5f * color);
00201 glVertex3f(-0.03f * color, 2.0f * sinf(dx), 2.0f * cosf(dx));
00202 glVertex3f(-0.03f * color, 10.0f * sinf(dx), 10.0f * cosf(dx));
00203 }
00204 glEnd();
00205 glEndList();
00206 }
00207 glCallList(sightList_);
00208 }