-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLS_Simulator3D.cpp
More file actions
363 lines (297 loc) · 10.2 KB
/
LS_Simulator3D.cpp
File metadata and controls
363 lines (297 loc) · 10.2 KB
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
#include "LS_Simulator3D.h"
#include "LS_Sphere.h"
#include "LS_Physics.h"
#include <stdio.h>
bool CLS_Simulator3D::fullscreen = false;
glm::vec2 CLS_Simulator3D::screenLocation[WINDOW_COUNT];
glm::vec2 CLS_Simulator3D::screenSize[WINDOW_COUNT];
glm::vec2 CLS_Simulator3D::mouseLocation = glm::vec2(0,0);
bool CLS_Simulator3D::mouseButton[MOUSE_BUTTONS];
glm::vec3 CLS_Simulator3D::cameraLocation = glm::vec3(0, 0, 0);
glm::vec2 CLS_Simulator3D::cameraRotation = glm::vec2(0, 0);
#ifdef GLUseShader
glm::mat4 CLS_Simulator3D::camera[NUM_OF_CAMERA_MATX];
#endif
GLuint CLS_Simulator3D::window[WINDOW_COUNT];
long long CLS_Simulator3D::lastFrameTime = 0;
const glm::vec2 CLS_Simulator3D::worldSize = glm::vec2(10, 10);
glm::vec2 CLS_Simulator3D::screenAspectRatio = glm::vec2(1,1);
std::vector<CLS_Shapes*> CLS_Simulator3D::objects;
#ifdef _DEBUG
std::list<long long> CLS_Simulator3D::frameRenderTime;
long CLS_Simulator3D::averageRenderTime = 0;
#endif
CLS_Simulator3D::CLS_Simulator3D()
{
}
//TODO CLS_Simulator3D::~CLS_Simulator3D
CLS_Simulator3D::~CLS_Simulator3D()
{
}
void CLS_Simulator3D::init(int argc, char ** argv)
{
glutInit(&argc, argv);
//Initilise the primary window
initGlut();
#ifdef _DEBUG
//Initilise the debug window
debug_window_init();
#endif
//Initilized glew and output if successful
GLenum err = glewInit();
printf("GLEW Setup... ");
if (err == GLEW_OK)
{
printf("OK");
}
else
{
printf("FAILED");
}
int glMajor, glMinor = 0;
glGetIntegerv(GL_MAJOR_VERSION, &glMajor);
glGetIntegerv(GL_MINOR_VERSION, &glMinor);
printf("\nOpenGL Version: %i.%i", glMajor, glMinor);
(true);
//TODO Change to glm::vec2
CLS_Physics::setScreenSize(CLS_VectorPoint<float>(worldSize.x, worldSize.y));
CLS_Physics::CCDStaus(false, 5);
}
//MINOR CLS_Simulator3D::mainloop
void CLS_Simulator3D::mainloop()
{
lastFrameTime = CLS_Simulator3D::getTimeStamp();
while (true)
{
cameraRotation.x += 0.1f;
glutMainLoopEvent();
for (int i = 0; i < WINDOW_COUNT; i++)
{
glutSetWindow(window[i]);
glutPostRedisplay();
}
while (CLS_Simulator3D::getTimeStamp() - lastFrameTime < 16)
{ }
CLS_Physics::applyPhysics(CLS_Simulator3D::getTimeStamp() - lastFrameTime, &objects);
lastFrameTime = CLS_Simulator3D::getTimeStamp();
}
}
void CLS_Simulator3D::setScreenSizeLoc(GLuint screenID, int top, int left, int width, int height, bool full_screen)
{
//Make sure the size of the screen is a valid size
if (width > 199 &&
height > 199)
{
if (screenID == WINDOW_PRIMARY)
fullscreen = full_screen;
screenSize[screenID].x = width;
screenSize[screenID].y = height;
}
else
throw "Screen size too small";
screenLocation[screenID].x = left;
screenLocation[screenID].y = top;
}
void CLS_Simulator3D::initGlut()
{
glutInitContextVersion(4, 5);
glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(CLS_Simulator3D::screenSize[WINDOW_PRIMARY].x, CLS_Simulator3D::screenSize[WINDOW_PRIMARY].y);
glutInitWindowPosition(screenLocation[WINDOW_PRIMARY].x, screenLocation[WINDOW_PRIMARY].y);
window[WINDOW_PRIMARY] = glutCreateWindow("PhysicsTest");
glClearColor(1, 1, 1, 1);
//as the display function is a static this call is made very easy.
glutDisplayFunc(CLS_Simulator3D::display);
glutKeyboardFunc(CLS_Simulator3D::event_keyboard);
glutSpecialFunc(CLS_Simulator3D::event_keyboardSpecialKeys);
glutPassiveMotionFunc(CLS_Simulator3D::event_MouseMovment);
glutMotionFunc(CLS_Simulator3D::event_MouseMovment);
glutMouseFunc(CLS_Simulator3D::event_mouseButtons);
glutReshapeFunc(CLS_Simulator3D::event_screenResize);
}
//TODO CLS_Simulator3D::event_keyboard
void CLS_Simulator3D::event_keyboard(unsigned char, int, int)
{
}
//TODO CLS_Simulator3D::event_keyboardSpecialKeys
void CLS_Simulator3D::event_keyboardSpecialKeys(int, int, int)
{
}
void CLS_Simulator3D::event_MouseMovment(int x, int y)
{
if (mouseButton[GLUT_RIGHT_BUTTON])
{
if (cameraRotation.y >= -90 && cameraRotation.y <= 90)
cameraRotation.y += mouseLocation.y - y;
else if (cameraRotation.y < -90)
cameraRotation.y = -90;
else
cameraRotation.y = 90;
cameraRotation.x += mouseLocation.x - x;
if (cameraRotation.x > 360)
cameraRotation.x -= 360;
else if (cameraRotation.x < 0)
cameraRotation.x += 360;
/*if (cameraRotation.y > 360)
cameraRotation.y -= 360;
else if (cameraRotation.y < 0)
cameraRotation.y += 360;*/
}
//set the mouse location
mouseLocation.x = x;
mouseLocation.y = y;
}
void CLS_Simulator3D::event_mouseButtons(int button, int state, int x, int y)
{
if (button < MOUSE_BUTTONS)
{
mouseButton[button] = state == 1 ? false : true;
}
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
CLS_Sphere *newObject = new CLS_Sphere();
//Set the objects attributes
newObject->setLocation(glm::vec3(
(worldSize.x * (x / screenSize[WINDOW_PRIMARY].x)) - (worldSize.x /2),
-((worldSize.y * (y / screenSize[WINDOW_PRIMARY].y)) - (worldSize.y /2))
, 0.0f));
newObject->setSpeed(glm::vec3(0.0f, 0.0f, 0.0f));
newObject->setMass(7.0f);
newObject->setColour(glm::vec4(float(rand() % 255) / 255.0f, float(rand() % 255) / 255.0f, float(rand() % 255) / 255.0f, 1.0f));
newObject->setBounceFactor(1.0f);
newObject->setScale(1.0f);
printf("\nObject Created at: { %.3f, %.3f, %.3f }", newObject->getLocation().x, newObject->getLocation().y, newObject->getLocation().z);
objects.push_back(newObject);
}
}
void CLS_Simulator3D::event_screenResize(int width, int height)
{
screenAspectRatio.y = float(height) / float(width);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
#ifdef GLUseShader
gluPerspective(50.0f, screenAspectRatio.y, 0.0f, 100.0f);
#else
glOrtho( -(worldSize.x / 2) * screenAspectRatio.y,
(worldSize.x / 2) * screenAspectRatio.y,
-(worldSize.y / 2) * screenAspectRatio.x,
(worldSize.y / 2) * screenAspectRatio.x,
-400, 400);
#endif
printf("\nScreen Resized to { %i, %i } new Aspect Ratio { %.3f, %.3f }", width, height, screenAspectRatio.x, screenAspectRatio.y);
//glutPostRedisplay();
}
//TODO CLS_Simulator3D::display
void CLS_Simulator3D::display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glPushMatrix();
glRotatef(cameraRotation.x, 0.0f, 0.5f, 0.0f);
glRotatef(cameraRotation.y, -0.5f, 0.0f, 0.0f);
for (std::vector<CLS_Shapes*>::iterator i = objects.begin(); i != objects.end(); i++)
{
#ifdef GLUseShader
(*i)->draw(shaderProgram);
#else
(*i)->draw();
#endif
}
glPopMatrix();
glutSwapBuffers();
}
/*This will draw to the screen in screen cordinates rather then world cordinates
This will be usefull for debugging text*/
void CLS_Simulator3D::screenPrint(int row, int col, const char *fmt, ...)
{
//Sourced from the freeglut demo
static char buf[256];
int viewport[4];
void *font = GLUT_BITMAP_9_BY_15;
va_list args;
glColor3d(0.1, 0.1, 0.4);
va_start(args, fmt);
(void)_vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
glGetIntegerv(GL_VIEWPORT, viewport);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, viewport[2], 0, viewport[3], -1, 1);
glRasterPos2i
(
glutBitmapWidth(font, ' ') * col,
-glutBitmapHeight(font) * row + viewport[3]
);
glutBitmapString(font, (unsigned char*)buf);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}
long long CLS_Simulator3D::getTimeStamp()
{
static LARGE_INTEGER s_frequency;
static long long timepoint;
static BOOL s_use_qpc = QueryPerformanceFrequency(&s_frequency);
if (s_use_qpc) {
LARGE_INTEGER now;
QueryPerformanceCounter(&now);
timepoint = (1000LL * now.QuadPart) / s_frequency.QuadPart;
}
else {
timepoint = GetTickCount();
}
#ifdef _DEBUG
addTimePoint(timepoint - lastFrameTime);
#endif
return timepoint;
}
#ifdef _DEBUG
void CLS_Simulator3D::debug_window_init()
{
glutInitContextVersion(4, 5);
glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(screenSize[WINDOW_DEBUG].x, screenSize[WINDOW_DEBUG].y);
glutInitWindowPosition(screenLocation[WINDOW_DEBUG].x, screenLocation[WINDOW_DEBUG].y);
window[WINDOW_DEBUG] = glutCreateWindow("Debug infomation");
glClearColor(1, 1, 1, 1);
//as the display function is a static this call is made very easy.
glutDisplayFunc(CLS_Simulator3D::debug_window_display);
}
void CLS_Simulator3D::debug_window_display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
int row = 1;
screenPrint(row++, 1, "Mouse location: {%3.0f,%3.0f}", mouseLocation.x, mouseLocation.y);
screenPrint(row++, 1, "Camera location: {%3.0f,%3.0f,%3.0f} Rotation: {%3.0f,%3.0f}", cameraLocation.x, cameraLocation.y, cameraLocation.z, cameraRotation.x,cameraRotation.y);
screenPrint(row++, 1, "Mouse buttons pressed: %s%s%s", mouseButton[0] ? "Left " : "", mouseButton[1] ? "Middle " : "", mouseButton[2] ? "Right " : "");
screenPrint(row++, 1, "SPF: %4llu mS (%4llu mS average over the last %i frames)", frameRenderTime.back(), (long long)(averageRenderTime / (int)NUMBER_OF_TIMEPOINTS), (int)NUMBER_OF_TIMEPOINTS);
screenPrint(row++, 1, "FPS: %4llu (%4llu Average)", 1000 / frameRenderTime.back(), 1000 / ((long long)(averageRenderTime / (int)NUMBER_OF_TIMEPOINTS)));
screenPrint(row++, 1, "Number of Objects: %i Number of Collisions %llu", objects.size(), CLS_Physics::collisionCounter);
for (int i = 0; i < objects.size(); i++)
{
screenPrint(row++, 1, "Object %i Speed: {%3.3f,%3.3f}",i, objects[i]->getSpeed().x, objects[i]->getSpeed().y);
}
glutSwapBuffers();
}
void CLS_Simulator3D::addTimePoint(long long nextTimepoint)
{
//Pust the time point to the back of the list
frameRenderTime.push_back(nextTimepoint);
// By adding this frome to a total time and subacting the time that get poped off we will save a small
// amount of processing power as we do not have to add all x number of timepoints every frame.
averageRenderTime += nextTimepoint;
//if the list is larger then the max pop the front
while (frameRenderTime.size() > NUMBER_OF_TIMEPOINTS)
{
averageRenderTime -= frameRenderTime.front();
frameRenderTime.pop_front();
}
}
#endif