Vis 2 Demo  1.0
Technical illustration type real-time rendering of geometry
 All Classes Namespaces Files Functions Variables Typedefs Macros
main.cpp
Go to the documentation of this file.
1 #include <Windows.h>
2 #include "../resource.h"
3 #include <tchar.h>
4 
5 #include <iostream>
6 #include <glew.h>
7 #include <GL/glfw.h>
8 #include <glext.h>
9 #include <glm/glm.hpp>
10 #include <glm/gtc/type_ptr.hpp>
11 #include <DevIL/IL/il.h>
12 
13 #include "utils/DebugFunctions.h"
14 #include "geometry/SpinningCube.h"
15 #include "visualisation/Shader.h"
16 #include "LoaderScene.h"
17 
18 #define WINDOW_WIDTH 1000
19 #define WINDOW_HEIGHT 750
20 
22 // ----------------- VARIABLES IN DATA SEGMENT ----------------- //
24 
25 double d_DELTA_TIME;
26 
27 // -------------------- user input and camera -------------------- //
28 
29 glm::vec4 mouse_data = glm::vec4(0.0f);
30 glm::vec2 mouse_data_prev = glm::vec2(0.0f);
31 
32 int mouse_wheel_pos = 0;
34 float zoom_dist_total = 0.0f;
35 glm::vec2 zoom_limits;
36 
37 bool mouse_right_click = false;
38 
39 unsigned int nr_steps = 16;
44 
45 glm::vec4 axis_X;
46 glm::vec4 axis_Y;
47 glm::vec4 axis_Z;
48 
49 glm::mat4 transformAccum;
50 
51 // ---------------------- scene loader -------------------------- //
52 
53 char* scene_path;
55 
56 // ------------- additional objects in the scene ----------------- //
57 
58 // world axes
60 
61 glm::mat4 mM_VP;
62 glm::mat4 mM_VD;
63 
66 
68 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //
69 // %% ---------------------------- SETUP DIALOG ---------------------------- %% //
70 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //
72 
75 {
76  static LPCTSTR temptitle = _T("{ddaba162-3aae-41f1-9980-f6a9efcd14dd}");
77  TCHAR title[512];
78  if(GetConsoleTitle(title, sizeof(title)/sizeof(TCHAR)) == 0)
79  return NULL;
80  SetConsoleTitle(temptitle);
81  HWND wnd = FindWindow(NULL, temptitle);
82  SetConsoleTitle(title);
83  return wnd;
84 }
85 
87 
93 LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
94 {
95  switch(Msg)
96  {
97  case WM_INITDIALOG:
98  return TRUE;
99  case WM_COMMAND:
100  switch(wParam)
101  {
102  case IDC_BUTTON1:
103  scene_path = "_resources/Levels/level.txt";
104  EndDialog(hWndDlg, 0);
105  return TRUE;
106  case IDC_BUTTON2:
107  scene_path = "_resources/Levels/levelB.txt";
108  EndDialog(hWndDlg, 0);
109  return TRUE;
110  case IDCANCEL:
111  EndDialog(hWndDlg, 0);
112  system("EXIT");
113  exit(0);
114  }
115  break;
116  }
117 
118  return FALSE;
119 }
120 
122 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //
123 // %% ----------------------------- USER INPUT ----------------------------- %% //
124 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //
126 
127 // ------------------------------- MOUSE POSITION ----------------------------- //
128 
130 
135 void GLFWCALL myMousePosCallBack(int _x, int _y)
136 {
137  mouse_data.x = float(_x) / float(WINDOW_WIDTH);
138  mouse_data.y = 1.0f - float(_y) / float(WINDOW_HEIGHT);
141 }
142 
143 // --------------------------- MOUSE ROTATE AND PAN --------------------------- //
144 
146 
150 void GLFWCALL myMouseButtonCallBack(int _button, int _action)
151 {
152  switch (_button)
153  {
154  case GLFW_MOUSE_BUTTON_LEFT:
155  if (_action == GLFW_PRESS)
156  {
157  // reset velocity calculations
160  mouse_data.z = 0.0f;
161  mouse_data.w = 0.0f;
162  // std::cout << "Left mouse button pressed at (" << mouse_data.x << ", " << mouse_data.y << ")." << std::endl;
163  }
164  else if (_action == GLFW_RELEASE)
165  {
166  // std::cout << "Left mouse button released at (" << mouse_data.x << ", " << mouse_data.y << ")." << std::endl;
167  // std::cout << "velocity (" << mouse_data.z << ", " << mouse_data.w << ")." << std::endl;
168  // calculate rotation
169  delta_rotX = 0.0f;
170  delta_rotY = 0.0f;
171  if (abs(mouse_data.z) > 0.01)
172  delta_rotY = - 50.0f * mouse_data.z;
173  if (abs(mouse_data.w) > 0.01)
174  delta_rotX = 50.0f * mouse_data.w;
175 
176  step_rotY = delta_rotY * float(d_DELTA_TIME * 100)/float(nr_steps);
177  step_rotX = delta_rotX * float(d_DELTA_TIME * 100)/float(nr_steps);
178  }
179  break;
180  case GLFW_MOUSE_BUTTON_RIGHT:
181  if (_action == GLFW_PRESS)
182  {
183  mouse_right_click = true;
184  // std::cout << "Right mouse button." << std::endl;
185  }
186  break;
187  case GLFW_MOUSE_BUTTON_MIDDLE:
188  if (_action == GLFW_PRESS)
189  {
190  // reset velocity calculations
193  mouse_data.z = 0.0f;
194  mouse_data.w = 0.0f;
195  // std::cout << "Middle mouse button pressed at (" << mouse_data.x << ", " << mouse_data.y << ")." << std::endl;
196  }
197  else if (_action == GLFW_RELEASE)
198  {
199  // std::cout << "Middle mouse button released." << std::endl;
200  // calculate pan
201  delta_panX = 0.0f;
202  delta_panY = 0.0f;
203  if (abs(mouse_data.z) > 0.01)
205  if (abs(mouse_data.w) > 0.01)
207 
208  step_panX = delta_panX * float(d_DELTA_TIME * 100)/float(nr_steps);
209  step_panY = delta_panY * float(d_DELTA_TIME * 100)/float(nr_steps);
210  }
211  break;
212  default:
213  break;
214  }
215 }
216 
217 // ------------------------------- MOUSE ZOOM --------------------------------- //
218 
220 
223 void GLFWCALL myMouseWheelCallBack(int _pos)
224 {
226  mouse_wheel_pos = _pos;
227  // std::cout << "wheel scroll strength: " << (mouse_wheel_pos_prev - mouse_wheel_pos) << " ... " << std::endl;
228  // TRANSLATION - ZOOM
229  float delta_transl = -5.0f * float(d_DELTA_TIME) * (mouse_wheel_pos_prev - mouse_wheel_pos);
230 
231  // check if we are within the zoom limits
232  if (zoom_dist_total + delta_transl > zoom_limits.x)
233  delta_transl = zoom_limits.x - zoom_dist_total;
234  else if (zoom_dist_total + delta_transl < zoom_limits.y)
235  delta_transl = zoom_limits.y - zoom_dist_total;
236 
237  zoom_dist_total += delta_transl;
239 
240  transformAccum = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, delta_transl)) * transformAccum;
241 }
242 
244 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //
245 // %% ------------------------ INITIALIZE OPENGL --------------------------- %% //
246 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //
248 
250 
256 {
257 
258  // initialise GLFW
259  if(!glfwInit())
260  {
261  std::cout << "glfwInit failed!" << std::endl;
262  system("PAUSE");
263  exit(-1);
264  }
265 
266  // setup opengl context
267 #if _DEBUG
268  // Create a debug OpenGL context or tell your OpenGL library (GLFW, SDL) to do so.
269  glfwOpenWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
270 #endif
271 
272  glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
273  glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
274  glfwOpenWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
275 
276  // open window
277  if (!glfwOpenWindow(WINDOW_WIDTH, WINDOW_HEIGHT,16,16,16,16,32,0,GLFW_WINDOW))
278  {
279  glfwTerminate();
280  std::cout << "glfwOpenWindow failed!" << std::endl;
281  system("PAUSE");
282  exit(-1);
283  }
284  glfwSetWindowTitle("VIS 2 DEMO");
285 
286  //check if the just opened window causes GLEW erorrs
287  // GLEW_OK is interpreted as FALSE by the C++ compiler
288  if(glewInit() != GLEW_OK)
289  {
290  std::cout << "Failed to initialise GLEW!" << std::endl;
291  system("PAUSE");
292  exit(-1);
293  }
294  // glew Bugfixes -> adapt function names
295  __glewGenVertexArrays = (PFNGLGENVERTEXARRAYSPROC)glfwGetProcAddress("glGenVertexArrays");
296  __glewBindVertexArray = (PFNGLBINDVERTEXARRAYPROC)glfwGetProcAddress("glBindVertexArray");
297  __glewDeleteVertexArrays = (PFNGLDELETEVERTEXARRAYSPROC)glfwGetProcAddress("glDeleteVertexArrays");
298  __glewGenerateMipmap = (PFNGLGENERATEMIPMAPPROC)glfwGetProcAddress("glGenerateMipmap");
299  __glewBindFragDataLocation = (PFNGLBINDFRAGDATALOCATIONPROC)glfwGetProcAddress("glBindFragDataLocation");
300 
301  __glewGenFramebuffers = (PFNGLGENFRAMEBUFFERSPROC)glfwGetProcAddress("glGenFramebuffers");
302  __glewBindFramebuffer = (PFNGLBINDFRAMEBUFFERPROC)glfwGetProcAddress("glBindFramebuffer");
303  __glewGenRenderbuffers = (PFNGLGENRENDERBUFFERSPROC)glfwGetProcAddress("glGenRenderbuffers");
304  __glewBindRenderbuffer = (PFNGLBINDRENDERBUFFERPROC)glfwGetProcAddress("glBindRenderbuffer");
305  __glewRenderbufferStorage = (PFNGLRENDERBUFFERSTORAGEPROC)glfwGetProcAddress("glRenderbufferStorage");
306  __glewFramebufferRenderbuffer = (PFNGLFRAMEBUFFERRENDERBUFFERPROC)glfwGetProcAddress("glFramebufferRenderbuffer");
307  __glewFramebufferTexture2D = (PFNGLFRAMEBUFFERTEXTURE2DPROC)glfwGetProcAddress("glFramebufferTexture2D");
308  __glewCheckFramebufferStatus = (PFNGLCHECKFRAMEBUFFERSTATUSPROC)glfwGetProcAddress("glCheckFramebufferStatus");
309  __glewDeleteFramebuffers = (PFNGLDELETEFRAMEBUFFERSPROC)glfwGetProcAddress("glDeleteFramebuffers");
310  __glewDeleteRenderbuffers = (PFNGLDELETERENDERBUFFERSPROC)glfwGetProcAddress("glDeleteRenderbuffers");
311 
312 
313 #if _DEBUG
314  // register the callback function
316 #endif
317  //provoke error///////////////
318  //glTranslatef(0.0, 2.0, 5.0);//
320 
321  // set clear color and viewport
322  glClearColor(0.9f, 0.9f, 0.9f, 0.0f);
323  glViewport(0,0,WINDOW_WIDTH,WINDOW_HEIGHT);
324 
325  // activate the depth buffering -> may need adjustment later
326  glEnable(GL_DEPTH_TEST);
327  // activate the back face culling -> may need adjustment later
328  glEnable(GL_CULL_FACE);
329  // glCullFace(GL_FRONT);
330 
331  //initilize DevIL
332  if (ilGetInteger(IL_VERSION_NUM) < IL_VERSION)
333  {
334  std::cout << "Wrong DevIL version!" << std::endl;
335  system("PAUSE");
336  exit(-1);
337  }
338  ilInit();
339 
340  // set theGLFW CALLBACK functions
341  glfwSetMousePosCallback(myMousePosCallBack);
342  glfwSetMouseButtonCallback(myMouseButtonCallBack);
343  glfwSetMouseWheelCallback(myMouseWheelCallBack);
344 }
345 
347 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //
348 // %% ------------------------- INITIALIZE SCENE --------------------------- %% //
349 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //
351 
353 
356 void vis2_Init()
357 {
358 
359  // scene loader
360  loader_scene_Ptr = new vis2::LoaderScene(scene_path, WINDOW_WIDTH, WINDOW_HEIGHT);
361  loader_scene_Ptr->loadScene();
362  zoom_limits = loader_scene_Ptr->getZoomLimits();
363 
364  // axes visualizers
365  shader_axes_Ptr = new vis2::Shader("_resources/Shader/basic_vert.glsl", "_resources/Shader/basic_frag.glsl");
366  mM_VP = glm::scale(glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, 3.5f)), glm::vec3(0.2f, 0.2f, 0.2f));
367  mM_VD = glm::scale(glm::mat4(1.0f), glm::vec3(0.1f, 0.1f, 0.1f));
368  cube_VP = new vis2::SpinningCube(mM_VP, shader_axes_Ptr);
369  cube_VD = new vis2::SpinningCube(mM_VD, shader_axes_Ptr);
370 
371  // define transformations for all
372  axis_X = glm::vec4(1.0f, 0.0f, 0.0f, 0.0f);
373  axis_Y = glm::vec4(0.0f, 1.0f, 0.0f, 0.0f);
374  axis_Z = glm::vec4(0.0f, 0.0f, 1.0f, 0.0f);
375 
376  transformAccum = glm::mat4(1.0f);
377 
378 }
379 
381 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //
382 // %% -------------------------------- UPDATE ------------------------------ %% //
383 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //
385 
386 // ------------------------- UPDATE ROTATION AND PAN -------------------------- //
387 
390 {
391  bool b_rotation = false;
392  // apply rotation
393  if (abs(delta_rotX) > abs(step_rotX))
394  {
395  transformAccum = glm::rotate(transformAccum, -step_rotX, glm::vec3(axis_X.x, axis_X.y, axis_X.z));
397  b_rotation = true;
398  }
399  else if (abs(delta_rotX) > 0 && abs(delta_rotX) <= abs(step_rotX))
400  {
401  transformAccum = glm::rotate(transformAccum, -delta_rotX, glm::vec3(axis_X.x, axis_X.y, axis_X.z));
402  delta_rotX = 0.0f;
403  b_rotation = true;
404  }
405  if (abs(delta_rotY) > abs(step_rotY))
406  {
407  transformAccum = glm::rotate(transformAccum, -step_rotY, glm::vec3(axis_Y.x, axis_Y.y, axis_Y.z));
409  b_rotation = true;
410  }
411  else if (abs(delta_rotY) > 0 && abs(delta_rotY) <= abs(step_rotY))
412  {
413  transformAccum = glm::rotate(transformAccum, -delta_rotY, glm::vec3(axis_Y.x, axis_Y.y, axis_Y.z));
414  delta_rotY = 0.0f;
415  b_rotation = true;
416  }
417 
418  if (b_rotation)
419  {
420  axis_X = glm::vec4(1.0f, 0.0f, 0.0f, 0.0f) * transformAccum;
421  axis_Y = glm::vec4(0.0f, 1.0f, 0.0f, 0.0f) * transformAccum;
422  axis_Z = glm::vec4(0.0f, 0.0f, 1.0f, 0.0f) * transformAccum;
423  // std::cout << "axis_X: " << axis_X.x << " " << axis_X.y << " " << axis_X.z << std::endl;
424  // std::cout << "axis_Y: " << axis_Y.x << " " << axis_Y.y << " " << axis_Y.z << std::endl;
425  // std::cout << "view vec: " << axis_Z.x << " " << axis_Z.y << " " << axis_Z.z << std::endl;
426  }
427 
428  bool b_pan = false;
429  // apply pan
430  if (abs(delta_panX) > abs(step_panX))
431  {
432  transformAccum = glm::translate(glm::mat4(1.0f), glm::vec3(step_panX, 0.0f, 0.0f)) * transformAccum;
434  b_pan = true;
435  }
436  else if (abs(delta_panX) > 0 && abs(delta_panX) <= abs(step_panX))
437  {
438  transformAccum = glm::translate(glm::mat4(1.0f), glm::vec3(delta_panX, 0.0f, 0.0f)) * transformAccum;
439  delta_panX = 0.0f;
440  b_pan = true;
441  }
442  if (abs(delta_panY) > abs(step_panY))
443  {
444  transformAccum = glm::translate(glm::mat4(1.0f), glm::vec3(0.0, step_panY, 0.0f)) * transformAccum;
446  b_pan = true;
447  }
448  else if (abs(delta_panY) > 0 && abs(delta_panY) <= abs(step_panY))
449  {
450  transformAccum = glm::translate(glm::mat4(1.0f), glm::vec3(0.0, delta_panY, 0.0f)) * transformAccum;
451  delta_panY = 0.0f;
452  b_pan = true;
453  }
454 
455  if (b_pan)
456  {
457  axis_X = glm::vec4(1.0f, 0.0f, 0.0f, 0.0f) * transformAccum;
458  axis_Y = glm::vec4(0.0f, 1.0f, 0.0f, 0.0f) * transformAccum;
459  axis_Z = glm::vec4(0.0f, 0.0f, 1.0f, 0.0f) * transformAccum;
460  // std::cout << "axis_X: " << axis_X.x << " " << axis_X.y << " " << axis_X.z << std::endl;
461  // std::cout << "axis_Y: " << axis_Y.x << " " << axis_Y.y << " " << axis_Y.z << std::endl;
462  // std::cout << "view vec: " << axis_Z.x << " " << axis_Z.y << " " << axis_Z.z << std::endl;
463  }
464 
465 }
466 
467 // ---------------------------- UPDATE SCENE OBJECTS -------------------------- //
468 
470 
472 {
473  // update transformAccum acc to user input
474  adjustCamera();
475 
476  // update axes followed by multipl by projectionMatrix_init ->
477  // same as no update and multiplication with the projectionMatrix
478  cube_VP->updateMm(transformAccum * mM_VP);
479  cube_VD->updateMm(transformAccum * mM_VD);
480 
481  //update the scene
482  loader_scene_Ptr->updateScene(transformAccum);
483 
484 }
485 
487 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //
488 // %% --------------------------------- DRAW ------------------------------- %% //
489 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //
491 
493 
494 void vis2_Draw()
495 {
496 
497  // %%%%%%%%%%%%%%%%%%%%%%%%% SET OPENGL STATE %%%%%%%%%%%%%%%%%%%%%%%%%%%%% //
498  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
499 
500  // get projection matrix from the scene
501  glm::mat4 projectionMatrix = loader_scene_Ptr->getProjectionMatrix();
502 
503  // draw axes
504  // cube_VP->draw(projectionMatrix);
505  // cube_VD->draw(projectionMatrix);
506 
507  // draw scene
508  loader_scene_Ptr->drawScene(mouse_right_click, mouse_data.x, mouse_data.y, true, false, 2);
509  if (mouse_right_click)
510  mouse_right_click = false;
511 
512  // %%%%%%%%%%%%%%%%%%%%%%%%%% RESET OPENGL STATE %%%%%%%%%%%%%%%%%%%%%%%%%% //
513  glfwSwapBuffers();
514 
515 }
516 
518 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //
519 // %% ------------------------------- CLEANUP ------------------------------ %% //
520 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //
522 
524 
526 {
527  // delete scene loader
528  delete loader_scene_Ptr;
529 
530  // delete axes
531  delete cube_VP;
532  delete cube_VD;
533 
534  delete shader_axes_Ptr;
535 
536 }
537 
539 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //
540 // %% --------------------------------- MAIN ------------------------------- %% //
541 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //
543 
545 
549 int main()
550 {
551  // call setup dialog
552  DialogBox(NULL, MAKEINTRESOURCE(IDD_DIALOG1), FindConsoleHandle(), reinterpret_cast<DLGPROC>(DlgProc));
553 
554  // initialize opengl context
556  // initilize shaders and geometry
557  vis2_Init();
558 
559  // set up render loop
560  bool b_running = true;
561  d_DELTA_TIME = 0.0;
562  double d_timeSinceLastFrame = 0.0;
563 
564  //int counter = 0;
565  while (b_running)
566  {
567  //calculate time of last frame (in seconds):
568  double d_time = glfwGetTime();
569  d_DELTA_TIME = d_time - d_timeSinceLastFrame;
570  d_timeSinceLastFrame = d_time;
571  /*
572  counter++;
573  if (counter % 60)
574  {
575  std::cout << "Frame Time: " << d_DELTA_TIME * 1000 << " ms" << std::endl;
576  counter = 0;
577  }
578  */
579  // main functionality
580  vis2_Update();
581  vis2_Draw();
582 
583  b_running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam(GLFW_OPENED);
584 
585  // ask OPENGL if there were any errors -> was replaced by a DEBUG CALLBACK FUNCTION
586  // if (glGetError() != GL_NO_ERROR)
587  // std::cout << "OPEN GL produced an error: " << glGetError() << std::endl;
588  }
589 
590 
591  // CLEAN-UP
592  vis2_Cleanup();
593  glfwTerminate();
594 
595  return 0;
596 }