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 | #include <iostream> |
---|
27 | |
---|
28 | using namespace std; |
---|
29 | |
---|
30 | |
---|
31 | /** |
---|
32 | \brief standard constructor |
---|
33 | */ |
---|
34 | Crosshair::Crosshair () |
---|
35 | { |
---|
36 | this->setClassID(CL_CROSSHAIR, "Crosshair"); |
---|
37 | this->setName("Crosshair"); |
---|
38 | |
---|
39 | EventHandler::getInstance()->subscribe(this, ES_GAME, EV_MOUSE_MOTION); |
---|
40 | |
---|
41 | } |
---|
42 | |
---|
43 | |
---|
44 | /** |
---|
45 | \brief standard deconstructor |
---|
46 | */ |
---|
47 | Crosshair::~Crosshair () |
---|
48 | { |
---|
49 | // delete what has to be deleted here |
---|
50 | EventHandler::getInstance()->unsubscribe(this); |
---|
51 | } |
---|
52 | |
---|
53 | void Crosshair::process(const Event &event) |
---|
54 | { |
---|
55 | if (event.type == EV_MOUSE_MOTION) |
---|
56 | { |
---|
57 | this->position2D[0] = event.x; |
---|
58 | this->position2D[1] = event.y; |
---|
59 | } |
---|
60 | |
---|
61 | /* |
---|
62 | GLdouble z; |
---|
63 | GLdouble objX, objY, objZ; |
---|
64 | glReadPixels (event.x, event.y, 1, 1, GL_DEPTH_COMPONENT, GL_DOUBLE, &z); |
---|
65 | |
---|
66 | |
---|
67 | if (gluUnProject(event.x, |
---|
68 | event.y, |
---|
69 | 10, |
---|
70 | GraphicsEngine::modMat, |
---|
71 | GraphicsEngine::projMat, |
---|
72 | GraphicsEngine::viewPort, |
---|
73 | &objX, |
---|
74 | &objY, |
---|
75 | &objZ )) |
---|
76 | PRINT(0)("screen %d %d -> obj(%f/%f/%f)\n", event.x, event.y, objX, objY, objZ); |
---|
77 | else |
---|
78 | PRINT(0)("shit\n"); |
---|
79 | */ |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | void Crosshair::draw() const |
---|
84 | { |
---|
85 | /* |
---|
86 | const PNode* camera = State::getInstance()->getCamera(); //!< \todo MUST be different |
---|
87 | Vector cameraPos = camera->getAbsCoor(); |
---|
88 | Vector cameraTargetPos = State::getInstance()->getCameraTarget()->getAbsCoor(); |
---|
89 | Vector view = cameraTargetPos - cameraPos; |
---|
90 | Vector up = Vector(0, 1, 0); |
---|
91 | up = camera->getAbsDir().apply(up); |
---|
92 | Vector h = up.cross(view); |
---|
93 | Vector v = h.cross(view); |
---|
94 | h.normalize(); |
---|
95 | v.normalize(); |
---|
96 | |
---|
97 | float px = (position2D[0]-GraphicsEngine::getInstance()->getResolutionX()/2)*.05; |
---|
98 | float py = -(position2D[1]-GraphicsEngine::getInstance()->getResolutionY()/2)*.05; |
---|
99 | |
---|
100 | glBegin(GL_TRIANGLES); |
---|
101 | glVertex3f(cameraTargetPos.x - h.x*px - v.x*py, |
---|
102 | cameraTargetPos.y - h.y*px - v.y*py, |
---|
103 | cameraTargetPos.z - h.z*px - v.z*py); |
---|
104 | |
---|
105 | glVertex3f(cameraTargetPos.x - h.x*(px+1) - v.x*py, |
---|
106 | cameraTargetPos.y - h.y*(px+1) - v.y*py, |
---|
107 | cameraTargetPos.z - h.z*(px+1) - v.z*py); |
---|
108 | |
---|
109 | glVertex3f(cameraTargetPos.x - h.x*px - v.x*(py+1), |
---|
110 | cameraTargetPos.y - h.y*px - v.y*(py+1), |
---|
111 | cameraTargetPos.z - h.z*px - v.z*(py+1)); |
---|
112 | |
---|
113 | glEnd(); |
---|
114 | |
---|
115 | */ |
---|
116 | |
---|
117 | |
---|
118 | GraphicsEngine::storeMatrices(); |
---|
119 | |
---|
120 | float z; |
---|
121 | glReadPixels ((int)position2D[0], GraphicsEngine::getInstance()->getResolutionY()-(int)position2D[1]-1, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z); |
---|
122 | |
---|
123 | |
---|
124 | printf("%f %f %f\n", (int)position2D[0], (int)position2D[1], z); |
---|
125 | |
---|
126 | //cout << z <<" "<< scale << " " << bias<< endl; |
---|
127 | |
---|
128 | GLdouble objX, objY, objZ; |
---|
129 | gluUnProject(position2D[0], |
---|
130 | GraphicsEngine::getInstance()->getResolutionY()-position2D[1]-1, |
---|
131 | .5, |
---|
132 | GraphicsEngine::modMat, |
---|
133 | GraphicsEngine::projMat, |
---|
134 | GraphicsEngine::viewPort, |
---|
135 | &objX, |
---|
136 | &objY, |
---|
137 | &objZ ); |
---|
138 | |
---|
139 | glBegin(GL_TRIANGLES); |
---|
140 | glColor3f(1,0,0); |
---|
141 | glVertex3f(objX, objY, objZ); |
---|
142 | glVertex3f(objX, objY+1, objZ); |
---|
143 | glVertex3f(objX, objY, objZ+1); |
---|
144 | glEnd(); |
---|
145 | } |
---|