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
RevLine 
[4744]1/*
[1853]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.
[1855]10
11   ### File Specific:
[4779]12   main-programmer: Benjamin Grauer
[1855]13   co-programmer: ...
[1853]14*/
15
[3955]16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
[1853]17
[4779]18#include "crosshair.h"
[4780]19#include "event_handler.h"
[4781]20
[4832]21#include "load_param.h"
[4781]22#include "graphics_engine.h"
23#include "glincl.h"
24#include "state.h"
[4832]25#include "material.h"
[4781]26
[4826]27#include <iostream>
28
[1856]29using namespace std;
[1853]30
[1856]31
[3245]32/**
[4832]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
[3245]47*/
[4832]48Crosshair::~Crosshair ()
[3365]49{
[4832]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{
[4779]64  this->setClassID(CL_CROSSHAIR, "Crosshair");
65  this->setName("Crosshair");
[4320]66
[4830]67  this->rotation = 0;
[4834]68  this->setRotationSpeed(5);
[4832]69  this->setSize(GraphicsEngine::getInstance()->getResolutionX()/10.0);
[4830]70
[4832]71  this->material = new Material;
[4830]72
[4780]73  EventHandler::getInstance()->subscribe(this, ES_GAME, EV_MOUSE_MOTION);
74
[4831]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);
[3365]79}
[1853]80
81
[4832]82void Crosshair::loadParams(const TiXmlElement* root)
[3543]83{
[4832]84  static_cast<PNode*>(this)->loadParams(root);
85  static_cast<EventListener*>(this)->loadParams(root);
[4830]86
[4832]87  LoadParam<Crosshair>(root, "texture", this, &Crosshair::setTexture)
88      .describe("the texture-file to load onto the Crosshair");
[4831]89
[4832]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");
[3543]95}
[4779]96
[4832]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 */
[4779]120void Crosshair::process(const Event &event)
121{
[4781]122  if  (event.type == EV_MOUSE_MOTION)
123  {
124    this->position2D[0] = event.x;
125    this->position2D[1] = event.y;
126  }
[4779]127
[4823]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*/
[4779]147}
[4781]148
[4832]149/**
150 * ticks the Crosshair
151 * @param dt the time to ticks
152 */
153void Crosshair::tick(float dt)
154{
[4834]155  // let the crosshair rotate
[4832]156  this->rotation += dt * rotationSpeed;
[4781]157
[4832]158
[4834]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;
[4832]162
[4823]163  GLdouble objX, objY, objZ;
[4826]164  gluUnProject(position2D[0],
165               GraphicsEngine::getInstance()->getResolutionY()-position2D[1]-1,
[4830]166               .99,
[4826]167               GraphicsEngine::modMat,
168               GraphicsEngine::projMat,
169               GraphicsEngine::viewPort,
170               &objX,
171               &objY,
172               &objZ );
173
[4830]174  this->setAbsCoor(objX, objY, objZ);
175
[4834]176
177}
178
179/**
180 * draws the crosshair
181 */
[4849]182void Crosshair::draw() const
[4834]183{
184  GraphicsEngine::storeMatrices();
185
186
187
188
[4830]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
[4834]196  this->debugDraw();
[4830]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);
[4832]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();
[4830]222
223  GraphicsEngine::leave2DMode();
[4781]224}
Note: See TracBrowser for help on using the repository browser.