00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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
00068
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
00077 glPushMatrix();
00078
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
00087 glScalef(w_ / 4.0f, w_ / 4.0f, w_ / 4.0f);
00088 GLState tankState(GLState::TEXTURE_OFF | GLState::DEPTH_ON);
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();
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 }