1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2006 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: David Hasenfratz |
---|
13 | */ |
---|
14 | |
---|
15 | #include "billboard.h" |
---|
16 | |
---|
17 | #include "util/loading/load_param.h" |
---|
18 | #include "util/loading/factory.h" |
---|
19 | |
---|
20 | #include "graphics_engine.h" |
---|
21 | #include "material.h" |
---|
22 | #include "glincl.h" |
---|
23 | #include "state.h" |
---|
24 | |
---|
25 | |
---|
26 | using namespace std; |
---|
27 | |
---|
28 | |
---|
29 | CREATE_FACTORY(Billboard, CL_BILLBOARD); |
---|
30 | |
---|
31 | |
---|
32 | /** |
---|
33 | * standart constructor |
---|
34 | */ |
---|
35 | Billboard::Billboard (const TiXmlElement* root) |
---|
36 | { |
---|
37 | this->init(); |
---|
38 | |
---|
39 | if( root) |
---|
40 | this->loadParams(root); |
---|
41 | } |
---|
42 | |
---|
43 | |
---|
44 | /** |
---|
45 | * destroys a Billboard |
---|
46 | */ |
---|
47 | Billboard::~Billboard () |
---|
48 | { |
---|
49 | if (this->material) |
---|
50 | delete this->material; |
---|
51 | } |
---|
52 | |
---|
53 | |
---|
54 | /** |
---|
55 | * initializes the Billboard |
---|
56 | */ |
---|
57 | void Billboard::init() |
---|
58 | { |
---|
59 | this->setClassID(CL_BILLBOARD, "Billboard"); |
---|
60 | this->setName("Billboard"); |
---|
61 | |
---|
62 | this->toList(OM_COMMON); |
---|
63 | |
---|
64 | this->material = new Material(); |
---|
65 | //this->setTexture("maps/lightning_bolt.png"); |
---|
66 | //this->setAbsCoor(0, 0, 0); |
---|
67 | this->setVisibiliy(true); |
---|
68 | this->setSize(10, 10); |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | /** |
---|
73 | * load params |
---|
74 | * @param root TiXmlElement object |
---|
75 | */ |
---|
76 | void Billboard::loadParams(const TiXmlElement* root) |
---|
77 | { |
---|
78 | LoadParam(root, "texture", this->material, Material, setDiffuseMap) |
---|
79 | .describe("the texture-file to load onto the Billboard"); |
---|
80 | |
---|
81 | LoadParam(root, "size", this, Billboard, setSize) |
---|
82 | .describe("the size of the Billboard in Pixels"); |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | /** |
---|
87 | * sets the size of the Billboard. |
---|
88 | * @param size the size in pixels |
---|
89 | */ |
---|
90 | void Billboard::setSize(float sizeX, float sizeY) |
---|
91 | { |
---|
92 | this->sizeX = sizeX; |
---|
93 | this->sizeY = sizeY; |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | /** |
---|
98 | * sets the material to load |
---|
99 | * @param textureFile The texture-file to load |
---|
100 | */ |
---|
101 | void Billboard::setTexture(const std::string& textureFile) |
---|
102 | { |
---|
103 | this->material->setDiffuseMap(textureFile); |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | /** |
---|
108 | * ticks the Billboard |
---|
109 | * @param dt the time to ticks |
---|
110 | */ |
---|
111 | void Billboard::tick(float dt) |
---|
112 | {/* |
---|
113 | float z = 0.0f; |
---|
114 | glReadPixels ((int)this->getAbsCoor2D().x, |
---|
115 | GraphicsEngine::getInstance()->getResolutionY()-(int)this->getAbsCoor2D().y-1, |
---|
116 | 1, |
---|
117 | 1, |
---|
118 | GL_DEPTH_COMPONENT, |
---|
119 | GL_FLOAT, |
---|
120 | &z); |
---|
121 | |
---|
122 | GLdouble objX=.0, objY=.0, objZ=.0; |
---|
123 | gluUnProject(this->getAbsCoor2D().x, |
---|
124 | GraphicsEngine::getInstance()->getResolutionY()-this->getAbsCoor2D().y-1, |
---|
125 | .99, // z |
---|
126 | GraphicsEngine::modMat, |
---|
127 | GraphicsEngine::projMat, |
---|
128 | GraphicsEngine::viewPort, |
---|
129 | &objX, |
---|
130 | &objY, |
---|
131 | &objZ );*/ |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | /** |
---|
136 | * draws the billboard |
---|
137 | */ |
---|
138 | void Billboard::draw() const |
---|
139 | { |
---|
140 | if( !this->isVisible()) |
---|
141 | return; |
---|
142 | |
---|
143 | glPushMatrix(); |
---|
144 | |
---|
145 | //glTranslatef(this->getAbsCoor().x, this->getAbsCoor().y, this->getAbsCoor().z); |
---|
146 | glTranslatef(0,0,0); |
---|
147 | this->material->select(); |
---|
148 | /* |
---|
149 | glBegin(GL_QUADS); |
---|
150 | glTexCoord2f(1.0f, 1.0f); glVertex3f(-sizeX/2, -sizeY/2, 0.0f); |
---|
151 | glTexCoord2f(0.0f, 1.0f); glVertex3f( sizeX/2, -sizeY/2, 0.0f); |
---|
152 | glTexCoord2f(0.0f, 0.0f); glVertex3f( sizeX/2, sizeY/2, 0.0f); |
---|
153 | glTexCoord2f(1.0f, 0.0f); glVertex3f(-sizeX/2, sizeY/2, 0.0f); |
---|
154 | glEnd(); |
---|
155 | */ |
---|
156 | |
---|
157 | const PNode* camera = State::getCameraNode(); //!< @todo MUST be different |
---|
158 | Vector cameraPos = camera->getAbsCoor(); |
---|
159 | Vector cameraTargetPos = State::getCameraTargetNode()->getAbsCoor(); |
---|
160 | Vector view = cameraTargetPos - cameraPos; |
---|
161 | Vector up = Vector(0, 1, 0); |
---|
162 | up = camera->getAbsDir().apply(up); |
---|
163 | Vector h = up.cross(view); |
---|
164 | Vector v = h.cross(view); |
---|
165 | h.normalize(); |
---|
166 | v.normalize(); |
---|
167 | |
---|
168 | float radius = 5; |
---|
169 | v *= .5 * radius; |
---|
170 | h *= .5 * radius; |
---|
171 | /* |
---|
172 | glBegin(GL_TRIANGLE_STRIP); |
---|
173 | glTexCoord2i(1, 1); |
---|
174 | glVertex3f(0 - h.x - v.x, |
---|
175 | 0 - h.y - v.y, |
---|
176 | 0 - h.z - v.z); |
---|
177 | glTexCoord2i(0, 1); |
---|
178 | glVertex3f(0 - h.x + v.x, |
---|
179 | 0 - h.y + v.y, |
---|
180 | 0 - h.z + v.z); |
---|
181 | glTexCoord2i(1, 0); |
---|
182 | glVertex3f(0 + h.x - v.x, |
---|
183 | 0 + h.y - v.y, |
---|
184 | 0 + h.z - v.z); |
---|
185 | glTexCoord2i(0, 0); |
---|
186 | glVertex3f(0 + h.x + v.x, |
---|
187 | 0 + h.y + v.y, |
---|
188 | 0 + h.z + v.z); |
---|
189 | |
---|
190 | glEnd();*/ |
---|
191 | |
---|
192 | glBegin(GL_QUADS); |
---|
193 | glTexCoord2f(0.0f, 0.0f); |
---|
194 | glVertex3f(0 - h.x - v.x, |
---|
195 | 0 - h.y - v.y, |
---|
196 | 0 - h.z - v.z); |
---|
197 | glTexCoord2f(1.0f, 0.0f); |
---|
198 | glVertex3f( 0 + h.x - v.x, |
---|
199 | 0 + h.y - v.y, |
---|
200 | 0 + h.z - v.z); |
---|
201 | glTexCoord2f(1.0f, 1.0f); |
---|
202 | glVertex3f(0 + h.x + v.x, |
---|
203 | 0 + h.y + v.y, |
---|
204 | 0 + h.z + v.z); |
---|
205 | glTexCoord2f(0.0f, 1.0f); |
---|
206 | glVertex3f(0 - h.x + v.x, |
---|
207 | 0 - h.y + v.y, |
---|
208 | 0 - h.z + v.z); |
---|
209 | glEnd(); |
---|
210 | |
---|
211 | |
---|
212 | glPopMatrix(); |
---|
213 | } |
---|