Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: the crosshair now renders as element2d

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->rotation = 0;
68  this->setRotationSpeed(5);
69  this->setSize(GraphicsEngine::getInstance()->getResolutionX()/10.0);
70
71  this->material = new Material;
72
73  EventHandler::getInstance()->subscribe(this, ES_GAME, EV_MOUSE_MOTION);
74
75  // center the mouse on the screen, and also hide the cursors
76  SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
77  GraphicsEngine::showMouse(false);
78  GraphicsEngine::stealWMEvents(true);
79}
80
81
82void Crosshair::loadParams(const TiXmlElement* root)
83{
84  static_cast<PNode*>(this)->loadParams(root);
85  static_cast<EventListener*>(this)->loadParams(root);
86
87  LoadParam<Crosshair>(root, "texture", this, &Crosshair::setTexture)
88      .describe("the texture-file to load onto the Crosshair");
89
90  LoadParam<Crosshair>(root, "size", this, &Crosshair::setSize)
91      .describe("the size of the Crosshair in Pixels");
92
93  LoadParam<Crosshair>(root, "rotation-speed", this, &Crosshair::setRotationSpeed)
94      .describe("the Speed with which the Crosshair should rotate");
95}
96
97
98/**
99 * sets the size of the Crosshair.
100 * @param size the size in pixels
101 */
102void Crosshair::setSize(float size)
103{
104  this->size = size*.5;
105};
106
107/**
108 * sets the material to load
109 * @param textureFile The texture-file to load onto the crosshair
110 */
111void Crosshair::setTexture(const char* textureFile)
112{
113  this->material->setDiffuseMap(textureFile);
114}
115
116/**
117 * processes the input
118 * @param event the Event coming as input
119 */
120void Crosshair::process(const Event &event)
121{
122  if  (event.type == EV_MOUSE_MOTION)
123  {
124    this->position2D[0] = event.x;
125    this->position2D[1] = event.y;
126  }
127
128  /*
129  GLdouble z;
130  GLdouble objX, objY, objZ;
131  glReadPixels (event.x, event.y, 1, 1, GL_DEPTH_COMPONENT, GL_DOUBLE, &z);
132
133
134  if (gluUnProject(event.x,
135      event.y,
136      10,
137      GraphicsEngine::modMat,
138      GraphicsEngine::projMat,
139      GraphicsEngine::viewPort,
140      &objX,
141      &objY,
142      &objZ ))
143    PRINT(0)("screen %d %d -> obj(%f/%f/%f)\n", event.x, event.y, objX, objY, objZ);
144  else
145    PRINT(0)("shit\n");
146*/
147}
148
149/**
150 * ticks the Crosshair
151 * @param dt the time to ticks
152 */
153void Crosshair::tick(float dt)
154{
155  // let the crosshair rotate
156  this->rotation += dt * rotationSpeed;
157
158
159  float z;
160  glReadPixels ((int)position2D[0], GraphicsEngine::getInstance()->getResolutionY()-(int)position2D[1]-1, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z);
161  //cout << z << endl;
162
163  GLdouble objX, objY, objZ;
164  gluUnProject(position2D[0],
165               GraphicsEngine::getInstance()->getResolutionY()-position2D[1]-1,
166               .99,
167               GraphicsEngine::modMat,
168               GraphicsEngine::projMat,
169               GraphicsEngine::viewPort,
170               &objX,
171               &objY,
172               &objZ );
173
174  this->setAbsCoor(objX, objY, objZ);
175
176
177}
178
179/**
180 * draws the crosshair
181 */
182void Crosshair::draw() const
183{
184  GraphicsEngine::storeMatrices();
185
186
187
188
189//   glBegin(GL_TRIANGLES);
190//   glColor3f(1,0,0);
191//   glVertex3f(objX, objY, objZ);
192//   glVertex3f(objX, objY+1, objZ);
193//   glVertex3f(objX, objY, objZ+1);
194//   glEnd();
195
196  this->debugDraw();
197
198  GraphicsEngine::enter2DMode();
199  GLdouble pos[3];
200  gluProject(this->getAbsCoor().x,
201             this->getAbsCoor().y,
202             this->getAbsCoor().z,
203             GraphicsEngine::modMat,
204             GraphicsEngine::projMat,
205             GraphicsEngine::viewPort,
206             pos, pos+1, pos+2 );
207
208
209  glTranslatef(position2D[0], position2D[1], 0);
210  glRotatef(this->rotation, 0,0,1);
211  this->material->select();
212  glBegin(GL_TRIANGLE_STRIP);
213  glTexCoord2f(0, 0);
214  glVertex2f(-size, -size);
215  glTexCoord2f(1, 0);
216  glVertex2f(size, -size);
217  glTexCoord2f(0, 1);
218  glVertex2f(-size, size);
219  glTexCoord2f(1, 1);
220  glVertex2f(size, size);
221  glEnd();
222
223  GraphicsEngine::leave2DMode();
224}
Note: See TracBrowser for help on using the repository browser.