xoreos  0.0.5
cube.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include "glm/mat4x4.hpp"
26 #include "glm/gtc/matrix_transform.hpp"
27 
28 #include "src/common/util.h"
29 #include "src/common/ustring.h"
30 #include "src/common/readstream.h"
31 #include "src/common/maths.h"
32 
33 #include "src/graphics/graphics.h"
34 
36 
40 
41 #include "src/events/events.h"
42 #include "src/events/requests.h"
43 
44 using Events::RequestID;
45 
46 namespace Graphics {
47 
48 namespace Aurora {
49 
50 CubeSide::CubeSide(Cube &parent, int n) : _parent(&parent), _n(n) {
52 
53  show();
54 }
55 
57  hide();
58 }
59 
61  glm::mat4 m;
62 
64 
65  _distance = ABS(m[3][0]) + ABS(m[3][1]) + ABS(m[3][2]);
66 }
67 
69  bool isTransparent = _parent->_texture.getTexture().hasAlpha();
70  if (((pass == kRenderPassOpaque) && isTransparent) ||
71  ((pass == kRenderPassTransparent) && !isTransparent))
72  return;
73 
76  _parent->callList();
77 
78  _parent->newFrame();
79 }
80 
81 
82 Cube::Cube(const Common::UString &texture) : _firstTime(true), _lastRotateTime(0), _rotation(0.0f), _list(0) {
83  _texture = TextureMan.get(texture);
84 
85  for (int i = 0; i < 6; i++)
86  _sides[i] = new CubeSide(*this, i);
87 
88  RequestMan.dispatchAndForget(RequestMan.rebuild(*this));
89 }
90 
93 
94  for (int i = 0; i < 6; i++)
95  delete _sides[i];
96 
97  if (_list != 0)
98  GfxMan.abandon(_list, 1);
99 }
100 
102  _list = glGenLists(1);
103 
104  glNewList(_list, GL_COMPILE);
105 
106  glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
107 
108  glBegin(GL_QUADS);
109  glTexCoord2f(0.0f, 0.0f);
110  glVertex3f(-1.00f, -1.00f, 0.00f);
111  glTexCoord2f(1.0f, 0.0f);
112  glVertex3f( 1.00f, -1.00f, 0.00f);
113  glTexCoord2f(1.0f, 1.0f);
114  glVertex3f( 1.00f, 1.00f, 0.00f);
115  glTexCoord2f(0.0f, 1.0f);
116  glVertex3f(-1.00f, 1.00f, 0.00f);
117 
118  glTexCoord2f(0.0f, 0.0f);
119  glVertex3f(-1.00f, -1.00f, 0.00f);
120  glTexCoord2f(0.0f, 1.0f);
121  glVertex3f(-1.00f, 1.00f, 0.00f);
122  glTexCoord2f(1.0f, 1.0f);
123  glVertex3f( 1.00f, 1.00f, 0.00f);
124  glTexCoord2f(1.0f, 0.0f);
125  glVertex3f( 1.00f, -1.00f, 0.00f);
126  glEnd();
127 
128  glEndList();
129 }
130 
132  if (_list == 0)
133  return;
134 
135  glDeleteLists(_list, 1);
136 
137  _list = 0;
138 }
139 
141  if (_lastRotateTime == 0)
142  _lastRotateTime = EventMan.getTimestamp();
143 
144  uint32 curTime = EventMan.getTimestamp();
145  uint32 diffTime = curTime - _lastRotateTime;
146 
147  _rotation = diffTime * 0.1f;
148  if (_rotation >= 360)
149  _lastRotateTime = curTime;
150 }
151 
153  glTranslatef(0.0f, 0.0f, -3.0f);
154 
155  glRotatef(-_rotation, 1.0f, 0.0f, 0.0f);
156  glRotatef( _rotation, 0.0f, 1.0f, 0.0f);
157  glRotatef( _rotation, 0.0f, 0.0f, 1.0f);
158 
159  glScalef(0.5f, 0.5f, 0.5f);
160 
161  switch (n) {
162  case 0:
163  glTranslatef(0.0f, 0.0f, 1.0f);
164  break;
165  case 1:
166  glTranslatef(0.0f, 0.0f, -1.0f);
167  break;
168  case 2:
169  glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
170  glTranslatef(0.0f, 0.0f, 1.0f);
171  break;
172  case 3:
173  glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
174  glTranslatef(0.0f, 0.0f, -1.0f);
175  break;
176  case 4:
177  glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
178  glTranslatef(0.0f, 0.0f, 1.0f);
179  break;
180  case 5:
181  glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
182  glTranslatef(0.0f, 0.0f, -1.0f);
183  break;
184  }
185 }
186 
187 void Cube::applyTransformation(int n, glm::mat4 &m) {
188  m = glm::translate(m, glm::vec3(0.0f, 0.0f, -3.0f));
189 
190  m = glm::rotate(m, Common::deg2rad(-_rotation), glm::vec3(1.0f, 0.0f, 0.0f));
191  m = glm::rotate(m, Common::deg2rad(_rotation), glm::vec3(0.0f, 1.0f, 0.0f));
192  m = glm::rotate(m, Common::deg2rad(_rotation), glm::vec3(0.0f, 0.0f, 1.0f));
193 
194  m = glm::scale(m, glm::vec3(0.5f, 0.5f, 0.5f));
195 
196  switch (n) {
197  case 0:
198  m = glm::translate(m, glm::vec3(0.0f, 0.0f, 1.0f));
199  break;
200  case 1:
201  m = glm::translate(m, glm::vec3(0.0f, 0.0f, -1.0f));
202  break;
203  case 2:
204  m = glm::rotate(m, Common::deg2rad(90.0f), glm::vec3(0.0f, 1.0f, 0.0f));
205  m = glm::translate(m, glm::vec3(0.0f, 0.0f, 1.0f));
206  break;
207  case 3:
208  m = glm::rotate(m, Common::deg2rad(90.0f), glm::vec3(0.0f, 1.0f, 0.0f));
209  m = glm::translate(m, glm::vec3(0.0f, 0.0f, -1.0f));
210  break;
211  case 4:
212  m = glm::rotate(m, Common::deg2rad(90.0f), glm::vec3(1.0f, 0.0f, 0.0f));
213  m = glm::translate(m, glm::vec3(0.0f, 0.0f, 1.0f));
214  break;
215  case 5:
216  m = glm::rotate(m, Common::deg2rad(90.0f), glm::vec3(1.0f, 0.0f, 0.0f));
217  m = glm::translate(m, glm::vec3(0.0f, 0.0f, -1.0f));
218  break;
219  }
220 }
221 
223  TextureMan.set(_texture);
224 }
225 
227  glCallList(_list);
228 }
229 
230 } // End of namespace Aurora
231 
232 } // End of namespace Graphics
T ABS(T x)
Definition: util.h:69
virtual void show()
Show the object.
Definition: renderable.cpp:114
Inter-thread request events.
TextureHandle _texture
Definition: cube.h:84
bool hasAlpha() const
Definition: texture.cpp:84
Only render transparent parts.
Definition: types.h:99
The global graphics manager.
virtual void hide()
Hide the object.
Definition: renderable.cpp:123
An object containing OpenGL structures.
Definition: types.h:84
A class holding an UTF-8 string.
Definition: ustring.h:48
#define TextureMan
Shortcut for accessing the texture manager.
Definition: textureman.h:127
double _distance
The distance of the object from the viewer.
Definition: renderable.h:101
The Aurora texture manager.
Mathematical helpers.
A simple cube object, for testing.
CubeSide(Cube &parent, int n)
Definition: cube.cpp:50
void render(RenderPass pass)
Render the object.
Definition: cube.cpp:68
RenderPass
Definition: types.h:97
void calculateDistance()
Calculate the object&#39;s distance.
Definition: cube.cpp:60
Utility templates and functions.
uint32 _lastRotateTime
Definition: cube.h:79
void applyTransformation(int n)
Definition: cube.cpp:152
The global events manager.
RequestList::iterator RequestID
Definition: requests.h:48
void removeFromQueue(QueueType queue)
Definition: queueable.cpp:56
Basic reading stream interfaces.
#define EventMan
Shortcut for accessing the events manager.
Definition: events.h:210
CubeSide * _sides[6]
Definition: cube.h:75
Unicode string handling.
friend class CubeSide
Definition: cube.h:94
Decoding TGA (TarGa) images.
A texture as used in the Aurora engines.
uint32_t uint32
Definition: types.h:204
#define RequestMan
Shortcut for accessing the request manager.
Definition: requests.h:127
A simple, rotating cube.
Definition: cube.h:64
Cube(const Common::UString &texture)
Definition: cube.cpp:82
#define pass
Definition: fft.cpp:257
static float deg2rad(float deg)
Definition: maths.h:97
#define GfxMan
Shortcut for accessing the graphics manager.
Definition: graphics.h:299
Only render opaque parts.
Definition: types.h:98