| 1 | /* |
|---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
|---|
| 3 | |
|---|
| 4 | Copyright (C) 2004 orx |
|---|
| 5 | |
|---|
| 6 | This program 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, or (at your option) |
|---|
| 9 | any later version. |
|---|
| 10 | |
|---|
| 11 | ### File Specific: |
|---|
| 12 | main-programmer: Benjamin Grauer |
|---|
| 13 | co-programmer: ... |
|---|
| 14 | */ |
|---|
| 15 | |
|---|
| 16 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ |
|---|
| 17 | |
|---|
| 18 | #include "crosshair.h" |
|---|
| 19 | #include "event_handler.h" |
|---|
| 20 | |
|---|
| 21 | #include "graphics_engine.h" |
|---|
| 22 | #include "glincl.h" |
|---|
| 23 | #include "p_node.h" |
|---|
| 24 | #include "state.h" |
|---|
| 25 | |
|---|
| 26 | using namespace std; |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | /** |
|---|
| 30 | \brief standard constructor |
|---|
| 31 | */ |
|---|
| 32 | Crosshair::Crosshair () |
|---|
| 33 | { |
|---|
| 34 | this->setClassID(CL_CROSSHAIR, "Crosshair"); |
|---|
| 35 | this->setName("Crosshair"); |
|---|
| 36 | |
|---|
| 37 | EventHandler::getInstance()->subscribe(this, ES_GAME, EV_MOUSE_MOTION); |
|---|
| 38 | |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | /** |
|---|
| 43 | \brief standard deconstructor |
|---|
| 44 | */ |
|---|
| 45 | Crosshair::~Crosshair () |
|---|
| 46 | { |
|---|
| 47 | // delete what has to be deleted here |
|---|
| 48 | EventHandler::getInstance()->unsubscribe(this); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | void Crosshair::process(const Event &event) |
|---|
| 52 | { |
|---|
| 53 | if (event.type == EV_MOUSE_MOTION) |
|---|
| 54 | { |
|---|
| 55 | this->position2D[0] = event.x; |
|---|
| 56 | this->position2D[1] = event.y; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | void Crosshair::draw() const |
|---|
| 63 | { |
|---|
| 64 | const PNode* camera = State::getInstance()->getCamera(); //!< \todo MUST be different |
|---|
| 65 | Vector cameraPos = camera->getAbsCoor(); |
|---|
| 66 | Vector cameraTargetPos = State::getInstance()->getCameraTarget()->getAbsCoor(); |
|---|
| 67 | Vector view = cameraTargetPos - cameraPos; |
|---|
| 68 | Vector up = Vector(0, 1, 0); |
|---|
| 69 | up = camera->getAbsDir().apply(up); |
|---|
| 70 | Vector h = up.cross(view); |
|---|
| 71 | Vector v = h.cross(view); |
|---|
| 72 | h.normalize(); |
|---|
| 73 | v.normalize(); |
|---|
| 74 | |
|---|
| 75 | float px = (position2D[0]-GraphicsEngine::getInstance()->getResolutionX()/2)*.05; |
|---|
| 76 | float py = -(position2D[1]-GraphicsEngine::getInstance()->getResolutionY()/2)*.05; |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | glBegin(GL_TRIANGLES); |
|---|
| 81 | glVertex3f(cameraTargetPos.x - h.x*px - v.x*py, |
|---|
| 82 | cameraTargetPos.y - h.y*px - v.y*py, |
|---|
| 83 | cameraTargetPos.z - h.z*px - v.z*py); |
|---|
| 84 | |
|---|
| 85 | glVertex3f(cameraTargetPos.x - h.x*(px+1) - v.x*py, |
|---|
| 86 | cameraTargetPos.y - h.y*(px+1) - v.y*py, |
|---|
| 87 | cameraTargetPos.z - h.z*(px+1) - v.z*py); |
|---|
| 88 | |
|---|
| 89 | glVertex3f(cameraTargetPos.x - h.x*px - v.x*(py+1), |
|---|
| 90 | cameraTargetPos.y - h.y*px - v.y*(py+1), |
|---|
| 91 | cameraTargetPos.z - h.z*px - v.z*(py+1)); |
|---|
| 92 | |
|---|
| 93 | glEnd(); |
|---|
| 94 | |
|---|
| 95 | } |
|---|