-
Notifications
You must be signed in to change notification settings - Fork 3
/
Drawer.cpp
476 lines (387 loc) · 12.8 KB
/
Drawer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
* Drawer.cpp
* mindstormssimulation
*
* Created by Torsten Kammer on 17.04.10
* Copyright 2010 RWTH Aachen University All rights reserved.
*
*/
#include "Drawer.h"
#include "EnvironmentDrawer.h"
#include "Model.h"
#include "RobotDrawer.h"
#include "ShowMessageBoxAndExit.h"
#include "Texture.h"
#include "UserInterface.h"
#define PROC_POINTERS_NOT_EXTERN 1
#include "OpenGL.h"
#include <iostream>
#if NEEDS_GL_FUNCTION_POINTERS
// For function pointers
#include <SDL.h>
#endif
namespace
{
const GLfloat lightAmbient [] = { 0.5f, 0.5f, 0.5f, 1.0f };
const GLfloat lightDiffuse [] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat materialAmbient [] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat materialSpecular [] = { 0.1f, 0.1f, 0.1f, 1.0f };
const GLfloat lightPosition [] = { -20.0f, 50.0f, -5.0f, 1.0f };
Drawer *globalDebugDrawer;
}
#ifdef ANDROID_NDK
Drawer::Drawer(EnvironmentEditor *editor, AndroidAssetManager *mgr)
{
assetManager = mgr;
#else
Drawer::Drawer(EnvironmentEditor *editor)
{
#endif
orientation = Normal;
#if !GLES
// Find out if we have OpenGL 1.5 or, failing that, support for
// GL_ARB_vertex_buffer_object
const char *version = (const char *) glGetString(GL_VERSION);
if (!version)
{
ShowErrorAndExit(L"Konnte OpenGL-Version nicht bestimmen.", L"Could not find OpenGL version");
}
unsigned major, minor;
sscanf(version, "%u.%u", &major, &minor);
if (major <= 1 && minor < 4)
{
// Earlier than OpenGL 1.5, so check for extension instead.
const char *extensions = (const char *) glGetString(GL_EXTENSIONS);
if (!strstr(extensions, "GL_ARB_vertex_buffer_object"))
{
ShowErrorAndExit(L"Ihre Grafikkarte unterst\u00FCtzt wichtige Funktionen nicht. Bitte aktualisieren sie ihre Treiber.", L"Your graphics card does not support some required functionality. Please update your drivers.");
}
}
#endif
#if NEEDS_GL_FUNCTION_POINTERS
// Setup OpenGL function pointers.
// Fall back to version with ARB suffix in the extremely rare case that the
// driver supports the extension, but not yet OpenGL 1.5
glGenBuffers = (PFNGLGENBUFFERSPROC) SDL_GL_GetProcAddress("glGenBuffers");
if (!glGenBuffers) glGenBuffers = (PFNGLGENBUFFERSPROC) SDL_GL_GetProcAddress("glGenBuffersARB");
glDeleteBuffers = (PFNGLDELETEBUFFERSPROC) SDL_GL_GetProcAddress("glDeleteBuffers");
if (!glDeleteBuffers) glDeleteBuffers = (PFNGLDELETEBUFFERSPROC) SDL_GL_GetProcAddress("glDeleteBuffersARB");
glBindBuffer = (PFNGLBINDBUFFERPROC) SDL_GL_GetProcAddress("glBindBuffer");
if (!glBindBuffer) glBindBuffer = (PFNGLBINDBUFFERPROC) SDL_GL_GetProcAddress("glBindBufferARB");
glBufferData = (PFNGLBUFFERDATAPROC) SDL_GL_GetProcAddress("glBufferData");
if (!glBufferData) glBufferData = (PFNGLBUFFERDATAPROC) SDL_GL_GetProcAddress("glBufferDataARB");
glBufferSubData = (PFNGLBUFFERSUBDATAPROC) SDL_GL_GetProcAddress("glBufferSubData");
if (!glBufferSubData) glBufferSubData = (PFNGLBUFFERSUBDATAPROC) SDL_GL_GetProcAddress("glBufferSubDataARB");
if (!glGenBuffers || !glDeleteBuffers || !glBindBuffer || !glBufferData || !glBufferSubData)
{
ShowErrorAndExit(L"Einige Grafikfunktionen konnten nicht geladen werden. Bitte aktualisieren sie ihre Treiber.", L"Some graphics functions could not be loaded. Please update your drivers.");
}
#endif
// Set up OpenGL properties
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); checkGLError();
glEnable(GL_DEPTH_TEST); checkGLError();
glEnableClientState(GL_VERTEX_ARRAY); checkGLError();
glEnableClientState(GL_NORMAL_ARRAY); checkGLError();
glEnableClientState(GL_COLOR_ARRAY); checkGLError();
glEnableClientState(GL_TEXTURE_COORD_ARRAY); checkGLError();
glEnable(GL_LIGHTING); checkGLError();
glEnable(GL_LIGHT0); checkGLError();
glEnable(GL_COLOR_MATERIAL); checkGLError();
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lightAmbient); checkGLError();
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse); checkGLError();
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); checkGLError();
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, materialAmbient); checkGLError();
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, materialSpecular); checkGLError();
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 6.0f); checkGLError();
glEnable(GL_TEXTURE_2D); checkGLError();
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); checkGLError();
// Set up drawers for the various sub-elements
environmentDrawer = new EnvironmentDrawer(editor, this);
checkGLError();
setCameraPosition(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
updateCamera(0.0f);
checkGLError();
globalDebugDrawer = this;
}
void Drawer::addRobot(Robot *aRobot)
{
robotDrawers.push_back(new RobotDrawer(aRobot, this));
}
void Drawer::removeRobot(Robot *aRobot)
{
for (std::vector<RobotDrawer *>::iterator iter = robotDrawers.begin(); iter != robotDrawers.end(); ++iter)
{
if ((*iter)->getRobot() == aRobot)
{
delete *iter;
robotDrawers.erase(iter);
return;
}
}
}
void Drawer::setWindowSize(float newWidth, float newHeight)
{
checkGLError();
glViewport(0, 0, GLsizei(newWidth), GLsizei(newHeight)); checkGLError();
screenWidth = newWidth;
screenHeight = newHeight;
float aspect = screenWidth/screenHeight;
frustumMatrix = matrix::frustum(65.0f, aspect, 1.0f, 100.0f);
inverseFrustumMatrix = matrix::inverseFrustum(65.0f, aspect, 1.0f, 100.0f);
orthogonalMatrix = matrix();
orthogonalMatrix.x.x = 2.0f / screenWidth;
orthogonalMatrix.y.y = 2.0f / screenHeight;
orthogonalMatrix.w.x = -1.0f;
orthogonalMatrix.w.y = -1.0f;
if (orientation == RotatedLeft)
{
float4 x = frustumMatrix.x;
frustumMatrix.x = frustumMatrix.y;
frustumMatrix.y = -x;
inverseFrustumMatrix = matrix::inverseFrustum(aspect*65.0f, screenHeight/screenWidth, 1.0f, 100.0f);
orthogonalMatrix = matrix();
orthogonalMatrix.x.x = 0.0f;
orthogonalMatrix.x.y = 2.0f / screenHeight;
orthogonalMatrix.y.x = -2.0f / screenWidth;
orthogonalMatrix.y.y = 0.0f;
orthogonalMatrix.w.x = +1.0f;
orthogonalMatrix.w.y = -1.0f;
}
else if (orientation == RotatedRight)
{
float4 y = frustumMatrix.y;
frustumMatrix.y = frustumMatrix.x;
frustumMatrix.x = -y;
inverseFrustumMatrix = matrix::inverseFrustum(aspect*65.0f, screenHeight/screenWidth, 1.0f, 100.0f);
orthogonalMatrix = matrix();
orthogonalMatrix.x.x = 0.0f;
orthogonalMatrix.x.y = -2.0f / screenHeight;
orthogonalMatrix.y.x = 2.0f / screenWidth;
orthogonalMatrix.y.y = 0.0f;
orthogonalMatrix.w.x = -1.0f;
orthogonalMatrix.w.y = +1.0f;
}
else if (orientation == UpsideDown)
{
frustumMatrix.x *= -1.0f;
frustumMatrix.y *= -1.0f;
orthogonalMatrix.x.x *= -1.0f;
orthogonalMatrix.y.y *= -1.0f;
orthogonalMatrix.w.x *= -1.0f;
orthogonalMatrix.w.y *= -1.0f;
}
glMatrixMode(GL_PROJECTION); checkGLError();
glLoadMatrixf(frustumMatrix.c_ptr()); checkGLError();
glMatrixMode(GL_MODELVIEW); checkGLError();
}
void Drawer::draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Draw lit objects
glEnable(GL_LIGHTING);
glEnableClientState(GL_NORMAL_ARRAY);
glEnable(GL_COLOR_MATERIAL);
glLoadIdentity();
glLoadMatrixf(modelMatrix.c_ptr());
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
// Draw environment
glEnableClientState(GL_COLOR_ARRAY);
environmentDrawer->draw();
glDisableClientState(GL_COLOR_ARRAY);
for (std::vector<RobotDrawer *>::iterator iter = robotDrawers.begin(); iter != robotDrawers.end(); ++iter)
(*iter)->draw();
// Draw unlit objects
glDisable(GL_LIGHTING);
glDisableClientState(GL_NORMAL_ARRAY);
glDisable(GL_COLOR_MATERIAL);
// - Draw 2D objects
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(orthogonalMatrix.c_ptr());
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_BLEND);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
if (orientation == RotatedLeft || orientation == RotatedRight)
userinterface->draw(screenHeight, screenWidth);
else
userinterface->draw(screenWidth, screenHeight);
glDisable(GL_BLEND);
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(frustumMatrix.c_ptr());
glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
}
void Drawer::setScreenOrientation(Drawer::ScreenOrientation newOrientation)
{
orientation = newOrientation;
this->setWindowSize(screenWidth, screenHeight);
}
void Drawer::updateCamera(float timedelta)
{
for (std::vector<RobotDrawer *>::iterator iter = robotDrawers.begin(); iter != robotDrawers.end(); ++iter)
(*iter)->update(timedelta);
environmentDrawer->update(timedelta);
cameraPitch += timedelta * cameraAngularSpeedPitch;
cameraYaw += timedelta * cameraAngularSpeedYaw;
// Recalculate camera
// Move a camera object there:
// glTranslatef(x, height, z);
// glRotatef(yaw, 0, 1, 0);
// glRotatef(pitch, 0, 0, 1);
// glTranslatef(0, 0, offset);
matrix positionMatrix = matrix::position(cameraPosition);
matrix yawMatrix = matrix::rotation(float4(0, 1, 0, 0), cameraYaw);
matrix pitchMatrix = matrix::rotation(float4(1, 0, 0, 0), cameraPitch);
matrix offsetMatrix = matrix::position(float4(0.0f, 0.0f, cameraZoom));
inverseModelMatrix = positionMatrix * yawMatrix * pitchMatrix * offsetMatrix;
modelMatrix = inverseModelMatrix.inverse();
cameraPosition -= yawMatrix.x * cameraSpeedX * timedelta;
cameraPosition -= yawMatrix.y * cameraSpeedY * timedelta;
cameraPosition -= yawMatrix.z * cameraSpeedZ * timedelta;
}
void Drawer::setCameraPosition(float x, float y, float z, float pitch, float yaw, float zoom)
{
cameraPosition = float4(x, y, z);
cameraPitch = pitch;
cameraYaw = yaw;
cameraSpeedX = 0.0f;
cameraSpeedY = 0.0f;
cameraSpeedZ = 0.0f;
cameraAngularSpeedPitch = 0.0f;
cameraAngularSpeedYaw = 0.0f;
cameraZoom = zoom;
}
void Drawer::setCameraSpeedX(float speed)
{
cameraSpeedX = speed;
}
void Drawer::setCameraSpeedY(float speed)
{
cameraSpeedY = speed;
}
void Drawer::setCameraSpeedZ(float speed)
{
cameraSpeedZ = speed;
}
void Drawer::setCameraAngularSpeedPitch(float value)
{
cameraAngularSpeedPitch = value;
}
void Drawer::setCameraAngularSpeedYaw(float value)
{
cameraAngularSpeedYaw = value;
}
void Drawer::rotateCamera(float radians)
{
cameraYaw -= radians * float(M_PI) / 90.0f;
}
void Drawer::moveCamera(float x, float y)
{
matrix yawMatrix = matrix::rotation(float4(0, 1, 0, 0), cameraYaw);
cameraPosition -= yawMatrix.x * x;
cameraPosition -= yawMatrix.z * y;
}
float4 Drawer::getCameraVelocity() const throw()
{
matrix yawMatrix = matrix::rotation(float4(0, 1, 0), cameraYaw);
float4 cameraVelocity(0.0f, 0.0f, 0.0f, 0.0f);
cameraVelocity -= yawMatrix.x * cameraSpeedX;
cameraVelocity -= yawMatrix.y * cameraSpeedY;
cameraVelocity -= yawMatrix.z * cameraSpeedZ;
return cameraVelocity;
}
float Drawer::getScreenWidth() const throw()
{
if (orientation == RotatedLeft || orientation == RotatedRight)
return screenHeight;
else
return screenWidth;
}
float Drawer::getScreenHeight() const throw()
{
if (orientation == RotatedLeft || orientation == RotatedRight)
return screenWidth;
else
return screenHeight;
}
void Drawer::transformPoint(float inX, float inY, float &outX, float &outY)
{
// Note that we transform always except on Mac OS X.
#if defined(__APPLE_CC__) && !defined(IPHONE) && !defined(IPHONE_SIMULATOR)
outX = inX;
outY = inY;
return;
#endif
switch(orientation)
{
case RotatedLeft:
outX = screenHeight - inY;
outY = screenWidth - inX;
break;
case RotatedRight:
outX = inY;
outY = inX;
break;
case UpsideDown:
outX = screenWidth - inX;
outY = inY;
break;
case Normal:
default:
outX = inX;
outY = screenHeight - inY;
break;
}
}
ray4 Drawer::rayForCoords(float x, float y)
{
float relX = x / getScreenWidth();
float relY = y / getScreenHeight();
relX = (relX * 2.0f - 1.0f);
relY = (relY * 2.0f - 1.0f);
// Create vectors
ray4 ray(float4(relX, relY, -1.0, 1.f), float4(relX, relY, 1.0, 1.f));
// Inverse matrix: Reverse order
matrix inverseMatrix = inverseModelMatrix*inverseFrustumMatrix;
ray = inverseMatrix * ray;
ray.start() /= ray.start().w;
ray.end() /= ray.end().w;
return ray;
}
Texture *Drawer::textureWithFilename(const std::string &filename)
{
Texture *& texture = textures[filename];
if (texture == NULL)
#ifdef ANDROID_NDK
texture = new Texture(assetManager, filename.c_str());
#else
texture = new Texture(filename.c_str());
#endif
return texture;
}
Model *Drawer::modelWithFilename(const std::string &filename)
{
Model *& model = models[filename];
if (model == NULL)
model = new Model(filename.c_str(), this);
return model;
}
void Drawer::debugLineClear(const std::string &name)
{
debugLines[name].clear();
}
void Drawer::debugLineAddSegment(const std::string &name, const float4 &a, const float4 &b)
{
debugLines[name].push_back(a);
debugLines[name].push_back(b);
}
void Drawer::clearDebugLine(const char *name)
{
globalDebugDrawer->debugLineClear(std::string(name));
}
void Drawer::addDebugLineSegment(const char *name, const float4 &a, const float4 &b)
{
globalDebugDrawer->debugLineAddSegment(std::string(name), a, b);
}