Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/world_entities/weapons/crosshair.cc @ 5045

Last change on this file since 5045 was 4955, checked in by bensch, 19 years ago

orxonox/trunk: extemely stupid aiming, but it works. Had to fix quite a few bugs in the process.

File size: 4.9 KB
Line 
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 "load_param.h"
22#include "graphics_engine.h"
23#include "glincl.h"
24#include "state.h"
25#include "material.h"
26
27#include <iostream>
28
29using namespace std;
30
31
32/**
33 * standart constructor
34 */
35Crosshair::Crosshair (const TiXmlElement* root)
36{
37  this->init();
38
39  if (root)
40    this->loadParams(root);
41  else
42    this->setTexture("maps/aim.png");
43}
44
45/**
46 * destroys a Crosshair
47*/
48Crosshair::~Crosshair ()
49{
50  if (this->material)
51  delete this->material;
52
53  // delete what has to be deleted here
54
55  GraphicsEngine::showMouse(true);
56  GraphicsEngine::stealWMEvents(false);
57}
58
59/**
60 * initializes the Crosshair
61 */
62void Crosshair::init()
63{
64  this->setClassID(CL_CROSSHAIR, "Crosshair");
65  this->setName("Crosshair");
66
67  this->setLayer(E2D_TOP);
68  this->rotation = 0;
69  this->setRotationSpeed(5);
70  this->setSize(GraphicsEngine::getInstance()->getResolutionX()/10.0);
71
72  this->material = new Material;
73
74  EventHandler::getInstance()->subscribe(this, ES_GAME, EV_MOUSE_MOTION);
75
76  // center the mouse on the screen, and also hide the cursors
77  SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
78  GraphicsEngine::showMouse(false);
79  GraphicsEngine::stealWMEvents(true);
80}
81
82
83void Crosshair::loadParams(const TiXmlElement* root)
84{
85  static_cast<PNode*>(this)->loadParams(root);
86  static_cast<EventListener*>(this)->loadParams(root);
87
88  LoadParam<Crosshair>(root, "texture", this, &Crosshair::setTexture)
89      .describe("the texture-file to load onto the Crosshair");
90
91  LoadParam<Crosshair>(root, "size", this, &Crosshair::setSize)
92      .describe("the size of the Crosshair in Pixels");
93
94  LoadParam<Crosshair>(root, "rotation-speed", this, &Crosshair::setRotationSpeed)
95      .describe("the Speed with which the Crosshair should rotate");
96}
97
98
99/**
100 * sets the size of the Crosshair.
101 * @param size the size in pixels
102 */
103void Crosshair::setSize(float size)
104{
105  this->size = size*.5;
106};
107
108/**
109 * sets the material to load
110 * @param textureFile The texture-file to load onto the crosshair
111 */
112void Crosshair::setTexture(const char* textureFile)
113{
114  this->material->setDiffuseMap(textureFile);
115}
116
117/**
118 * processes the input
119 * @param event the Event coming as input
120 */
121void Crosshair::process(const Event &event)
122{
123  if  (event.type == EV_MOUSE_MOTION)
124  {
125    this->position2D[0] = event.x;
126    this->position2D[1] = event.y;
127  }
128
129  /*
130  GLdouble z;
131  GLdouble objX, objY, objZ;
132  glReadPixels (event.x, event.y, 1, 1, GL_DEPTH_COMPONENT, GL_DOUBLE, &z);
133
134
135  if (gluUnProject(event.x,
136      event.y,
137      10,
138      GraphicsEngine::modMat,
139      GraphicsEngine::projMat,
140      GraphicsEngine::viewPort,
141      &objX,
142      &objY,
143      &objZ ))
144    PRINT(0)("screen %d %d -> obj(%f/%f/%f)\n", event.x, event.y, objX, objY, objZ);
145  else
146    PRINT(0)("shit\n");
147*/
148}
149
150/**
151 * ticks the Crosshair
152 * @param dt the time to ticks
153 */
154void Crosshair::tick(float dt)
155{
156  // let the crosshair rotate
157  this->rotation += dt * rotationSpeed;
158
159
160  float z;
161  glReadPixels ((int)position2D[0], GraphicsEngine::getInstance()->getResolutionY()-(int)position2D[1]-1, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z);
162  //cout << z << endl;
163
164  GLdouble objX, objY, objZ;
165  gluUnProject(position2D[0],
166               GraphicsEngine::getInstance()->getResolutionY()-position2D[1]-1,
167               .99,  // z
168               GraphicsEngine::modMat,
169               GraphicsEngine::projMat,
170               GraphicsEngine::viewPort,
171               &objX,
172               &objY,
173               &objZ );
174
175  this->setAbsCoor(objX, objY, objZ);
176
177  //this->positioning();
178}
179
180/**
181 * draws the crosshair
182 */
183void Crosshair::draw() const
184{
185//   glBegin(GL_TRIANGLES);
186//   glColor3f(1,0,0);
187//   glVertex3f(objX, objY, objZ);
188//   glVertex3f(objX, objY+1, objZ);
189//   glVertex3f(objX, objY, objZ+1);
190//   glEnd();
191  glPushMatrix();
192  GLdouble pos[3];
193  gluProject(this->getAbsCoor().x,
194             this->getAbsCoor().y,
195             this->getAbsCoor().z,
196             GraphicsEngine::modMat,
197             GraphicsEngine::projMat,
198             GraphicsEngine::viewPort,
199             pos, pos+1, pos+2 );
200
201
202  glTranslatef(position2D[0], position2D[1], 0);
203  glRotatef(this->rotation, 0,0,1);
204  this->material->select();
205  glBegin(GL_TRIANGLE_STRIP);
206  glTexCoord2f(0, 0);
207  glVertex2f(-size, -size);
208  glTexCoord2f(1, 0);
209  glVertex2f(size, -size);
210  glTexCoord2f(0, 1);
211  glVertex2f(-size, size);
212  glTexCoord2f(1, 1);
213  glVertex2f(size, size);
214  glEnd();
215  glPopMatrix();
216
217}
Note: See TracBrowser for help on using the repository browser.