git-svn-id: https://scorched3d.svn.sourceforge.net/svnroot/scorched3d/trunk/scorched...
[scorched3d/parasti.git] / src / client / GLW / GLWTankModel.cpp
blob440ef1e75f50ccca4ad06adbf682a5c32561087b
1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2009
3 //
4 // This file is part of Scorched3D.
5 //
6 // Scorched3D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // Scorched3D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with Scorched3D; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ////////////////////////////////////////////////////////////////////////////////
21 #include <GLW/GLWTankModel.h>
22 #include <client/ScorchedClient.h>
23 #include <graph/MainCamera.h>
24 #include <graph/ModelRendererMesh.h>
25 #include <client/ClientState.h>
26 #include <tankgraph/TargetRendererImplTank.h>
27 #include <tank/TankContainer.h>
28 #include <tank/TankPosition.h>
29 #include <common/Vector4.h>
30 #include <landscape/Landscape.h>
31 #include <sky/Sky.h>
32 #include <GLW/GLWTranslate.h>
34 REGISTER_CLASS_SOURCE(GLWTankModel);
36 GLWTankModel::GLWTankModel(float x, float y, float w, float h) :
37 GLWidget(x, y, w, h),
38 totalTime_(0.0f)
42 GLWTankModel::~GLWTankModel()
46 void GLWTankModel::draw()
48 Tank *current =
49 ScorchedClient::instance()->getTankContainer().getCurrentTank();
50 if (!current) return;
52 TargetRendererImplTank *renderer = (TargetRendererImplTank *)
53 current->getRenderer();
54 if (!renderer) return;
56 Vector4 sunPosition(-100.0f, 100.0f, 400.0f, 1.0f);
57 Vector4 sunDiffuse(0.9f, 0.9f, 0.9f, 1.0f);
58 Vector4 sunAmbient(0.4f, 0.4f, 0.4f, 1.0f);
59 glLightfv(GL_LIGHT1, GL_AMBIENT, sunAmbient);
60 glLightfv(GL_LIGHT1, GL_DIFFUSE, sunDiffuse);
62 // Add the tooltip for the model name+attributes
63 GLWToolTip::instance()->addToolTip(&renderer->getTips()->tankTip,
64 GLWTranslate::getPosX() + x_ + 20.0f,
65 GLWTranslate::getPosY() + y_ + 20.0f, 80.0f, 80.0f);
67 // Find the angles to rotate so the tank is at the
68 // same angle as the "real" tank on the landscape
69 Vector &lookFrom = MainCamera::instance()->getCamera().getCurrentPos();
70 Vector &lookAt = MainCamera::instance()->getCamera().getLookAt();
71 Vector dir = (lookAt - lookFrom).Normalize();
72 float angXY = atan2f(dir[0], dir[1]) / 3.14f * 180.0f;
73 float angYZ = acosf(dir[2]) / 3.14f * 180.0f + 180.0f;
74 if (angYZ < 280.0f) angYZ = 280.0f;
76 // Draw the tank
77 glPushMatrix();
78 // Set the tank angle
79 glTranslatef(x_ + w_ / 2.0f, y_ + w_ / 2.0f, 0.0f);
81 glLightfv(GL_LIGHT1, GL_POSITION, sunPosition);
83 glRotatef(angYZ, 1.0f, 0.0f, 0.0f);
84 glRotatef(angXY, 0.0f, 0.0f, 1.0f);
86 // Draw the tank model
87 glScalef(w_ / 4.0f, w_ / 4.0f, w_ / 4.0f);
88 GLState tankState(GLState::TEXTURE_OFF | GLState::DEPTH_ON); // For no tank skins
89 Vector position;
90 TankMesh *mesh = renderer->getMesh();
91 if (mesh)
93 Vector4 rotation(1.0f, 0.0f, 0.0f, 0.0f);
94 float matrix[16];
95 rotation.getOpenGLRotationMatrix(matrix);
97 mesh->draw(
98 totalTime_ * 20.0f,
99 false, matrix, position, 0.0f,
100 current->getPosition().getRotationGunXY().asFloat(),
101 current->getPosition().getRotationGunYZ().asFloat(),
102 true);
104 glPopMatrix();
106 Landscape::instance()->getSky().getSun().setLightPosition(); // Reset light
109 void GLWTankModel::mouseDown(int button, float x, float y, bool &skipRest)
111 if (inBox(x, y, x_, y_, w_, h_))
113 skipRest = true;
114 MainCamera::instance()->getTarget().setCameraType(TargetCamera::CamBehind);
118 void GLWTankModel::simulate(float frameTime)
120 totalTime_ += frameTime;