HOG2
GLUThog.cpp
Go to the documentation of this file.
1 /*
2  * $Id: main.cpp
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 11/02/06.
6  * Modified by Nathan Sturtevant on 02/29/20.
7  *
8  * This file is part of HOG2. See https://github.com/nathansttt/hog2 for licensing information.
9  *
10  */
11 
12 
13 int hog_main(int argc, char **argv);
14 int main(int argc, char **argv)
15 {
16  return hog_main(argc, argv);
17 }
18 
19 
20 #include "Trackball.h"
21 #include "Common.h"
22 #include "GLUThog.h"
23 #include "TextBox.h"
24 #include <stdlib.h>
25 #include <time.h>
26 #include <math.h>
27 #include <string.h>
28 
29 using namespace std;
30 
32 GLint gDollyPanStartPoint[2] = {0, 0};
33 GLfloat gTrackBallRotation [4] = {0.0f, 0.0f, 0.0f, 0.0f};
34 GLboolean gDolly = GL_FALSE;
35 GLboolean gPan = GL_FALSE;
36 GLboolean gTrackball = GL_FALSE;
38 int gCurrButton = -1;
39 //bool pointpath = false;
40 //int ppMouseClicks = 0;
42 double fps = 30.0;
43 
44 pRecContext GetContext(unsigned long windowID)
45 {
46  return pContextInfo;
47 }
48 
50 {
51  return pContextInfo;
52 }
53 
54 void RunHOGGUI(int argc, char** argv, int windowDimension)
55 {
56  RunHOGGUI(argc, argv, windowDimension, windowDimension);
57 }
58 
59 void RunHOGGUI(int argc, char* argv[], int xDimension, int yDimension)
60 {
61 // // Init traj global
62 // startTrajRecap = false;
63 
64  srandom(unsigned(time(0)));
65 
66 // InstallCommandLineHandler(processFramesPerSecond, "-fps", "-fps <int>", "[System Option] Specifies the maximum frames per second.");
67  //initializeHandlers();
68  ProcessCommandLineArgs(argc, argv);
69 
71  //resetCamera(&(pContextInfo->camera));
72 
73  glutInit(&argc, argv);
74  glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
75  glutInitWindowPosition(0, 0);
76  glutInitWindowSize(xDimension, yDimension);
77  glutCreateWindow("Map Abstraction");
78  glutReshapeFunc(resizeWindow);
79  glutDisplayFunc(renderScene);
80  glutIdleFunc(renderScene);
81  glutMouseFunc(mousePressedButton);
82  glutMotionFunc(mouseMovedButton);
83  glutPassiveMotionFunc(mouseMovedNoButton);
84  glutKeyboardFunc(keyPressed);
86  buildGL();
88  createMenus();
89 
90  // Initialize the tank model
91  //initTankTextures();
92 
93  glutMainLoop();
94 
95  //processStats(pContextInfo->unitLayer->getStats());
96  //delete pContextInfo->unitLayer;
97  delete pContextInfo;
98 }
99 
101 {
102 }
103 
104 void processMenuEvents(int option)
105 {
106 }
107 
108 
109 
113 void keyPressed(unsigned char key, int, int)
114 {
115  //x+=y;
116  bool shift = (glutGetModifiers() == GLUT_ACTIVE_SHIFT);
117  bool alt = (glutGetModifiers() == GLUT_ACTIVE_ALT);
118  bool cntrl = (glutGetModifiers() == GLUT_ACTIVE_CTRL);
119  DoKeyboardCommand(pContextInfo, key, shift, cntrl, alt);
120 }
121 
122 
123 
124 void mouseMovedNoButton(int x, int y)
125 {
126  point3d p(x, y, 0);// = GetOGLPos(pContextInfo, x, y);
127  tButtonType bType = kNoButton;
128  if (HandleMouseClick(pContextInfo, x, y, p, bType, kMouseMove))
129  return;
130 
131 
133  {
134  if (gPan == GL_FALSE)
135  {
136  gDollyPanStartPoint[0] = (GLint)x;
137  gDollyPanStartPoint[1] = (GLint)y;
139  gPan = GL_TRUE;
140  glutSetCursor(GLUT_CURSOR_NONE);
141  }
142 
143  float dx = gDollyPanStartPoint[0]-x;
144  float dy = gDollyPanStartPoint[1]-y;
145  gDollyPanStartPoint[0] = (GLint)x;
146  gDollyPanStartPoint[1] = (GLint)y;
147  float rotation[4] = {0.0f, 0.0f, 0.0f, 0.0f};
148 
149 // rotation[0] = -dy/24;
150 // rotation[1] = 1;
151 // addToRotationTrackball(rotation, pContextInfo->camera[pContextInfo->currPort].rotations.cameraRotation);
152  //pContextInfo->controlShip->addRotation(rotation);
153  //addToRotationTrackball(rotation, pContextInfo->fRot);
154  rotation[0] = -dx/24;
155  rotation[1] = 0;
156  rotation[2] = 1;
157  // if ((pContextInfo->controlShip) && (rotation[0] != 0))
158  // pContextInfo->controlShip->addRotation(rotation);
159 
161 
162 
164  y > pContextInfo->globalCamera.viewHeight || x < 1 || y < 1)
165  {
169  }
170  }
171 }
172 
176 void mouseMovedButton(int x, int y)
177 {
178  point3d p(x, y, 0);// = GetOGLPos(pContextInfo, x, y);
179  tButtonType bType = kLeftButton;
180  switch (gCurrButton)
181  {
182  case GLUT_RIGHT_BUTTON: bType = kRightButton; break;
183  case GLUT_LEFT_BUTTON: bType = kLeftButton; break;
184  case GLUT_MIDDLE_BUTTON: bType = kMiddleButton; break;
185  }
186  if (HandleMouseClick(pContextInfo, x, y, p, bType, kMouseDrag))
187  return;
188 
189 
190 // if (!pContextInfo->camera[pContextInfo->currPort].thirdPerson)
191 // {
192 // float dx = gDollyPanStartPoint[0]-x;
193 // float dy = gDollyPanStartPoint[1]-y;
194 // gDollyPanStartPoint[0] = (GLint)x;
195 // gDollyPanStartPoint[1] = (GLint)y;
196 // float rotation[4] = {0.0f, 0.0f, 0.0f, 0.0f};
197 //
198 // rotation[0] = -dy/24;
199 // rotation[1] = 1;
200 // addToRotationTrackball(rotation, pContextInfo->camera[pContextInfo->currPort].rotations.cameraRotation);
201 // //pContextInfo->controlShip->addRotation(rotation);
202 // //addToRotationTrackball(rotation, pContextInfo->fRot);
203 // rotation[0] = -dx/24;
204 // rotation[1] = 0;
205 // rotation[2] = 1;
208 //
209 // addToRotationTrackball(rotation, pContextInfo->camera[pContextInfo->currPort].rotations.cameraRotation);
210 // }
211  //else
212  if (gTrackball) {
213  rollToTrackball((long) x, (long) y, gTrackBallRotation);
214  }
215  else if (gDolly) {
216  mouseDolly(x, y, pContextInfo);
218  {
219  for (int x = 0; x < pContextInfo->numPorts; x++)
220  {
221  updateProjection(pContextInfo, x); // update projection matrix
222  }
223  }
224  else {
225  updateProjection(pContextInfo, pContextInfo->currPort); // update projection matrix
226  }
227  }
228  else if (gPan) {
229  mousePan(x, y, pContextInfo);
230  }
231 
232 }
233 
234 
238 //void mousePressedButton(int button, int state, int x, int y)
239 {
240  gCurrButton = button;
241  int modifiers = glutGetModifiers();
242 
243  //printf("Button = %d\n", button);
244  if (state == GLUT_DOWN) {
245  point3d p(x, y, 0);// = GetOGLPos(pContextInfo, x, y);
246  tButtonType bType = kLeftButton;
247  switch (gCurrButton)
248  {
249  case GLUT_RIGHT_BUTTON: bType = kRightButton; break;
250  case GLUT_LEFT_BUTTON: bType = kLeftButton; break;
251  case GLUT_MIDDLE_BUTTON: bType = kMiddleButton; break;
252  }
253  if (HandleMouseClick(pContextInfo, x, y, p, bType, kMouseDown))
254  return;
255 
257  {
258  gDollyPanStartPoint[0] = (GLint)x;
259  gDollyPanStartPoint[1] = (GLint)y;
260  gPan = GL_TRUE;
262  }
263  else if ((button == GLUT_RIGHT_BUTTON) || ((button == GLUT_LEFT_BUTTON) && (modifiers == GLUT_ACTIVE_CTRL)))
264  { // pan
265  if (gTrackball)
266  { // if we are currently tracking, end trackball
267  gTrackball = GL_FALSE;
268  if (gTrackBallRotation[0] != 0.0)
269  {
270  // Mouse moves world object
272  {
274  {
275  for (int x = 0; x < pContextInfo->numPorts; x++)
277  }
278 
279  else {
281  }
282  }
283  else {
285  {
286  for (int x = 0; x < pContextInfo->numPorts; x++)
288  }
289 
290  else {
292  }
293  }
294  }
296  }
297  else if (gDolly)
298  { // if we are currently dollying, end dolly
299  gDolly = GL_FALSE;
300  }
301  gDollyPanStartPoint[0] = (GLint)x;
302  gDollyPanStartPoint[1] = (GLint)y;
303  gPan = GL_TRUE;
305  }
306  else if ((button == GLUT_MIDDLE_BUTTON) || ((button == GLUT_LEFT_BUTTON) && (modifiers == GLUT_ACTIVE_SHIFT)))
307  { // dolly
308  if (gTrackball)
309  { // if we are currently tracking, end trackball
310  gTrackball = GL_FALSE;
311  if (gTrackBallRotation[0] != 0.0)
312  {
313  // Mouse moves world object
315  {
317  {
318  for (int x = 0; x < pContextInfo->numPorts; x++)
320  }
321 
322  else {
324  }
325  }
326  else {
328  {
329  for (int x = 0; x < pContextInfo->numPorts; x++)
331  }
332 
333  else {
335  }
336  }
337  }
339  }
340  else if (gPan)
341  { // if we are currently panning, end pan
342  gPan = GL_FALSE;
343  }
344  gDollyPanStartPoint[0] = (GLint)x;
345  gDollyPanStartPoint[1] = (GLint)y;
346  gDolly = GL_TRUE;
348  }
349  else if (button == GLUT_LEFT_BUTTON)
350  { // trackball
351  if (gDolly)
352  { // if we are currently dollying, end dolly
353  gDolly = GL_FALSE;
354  gTrackingContextInfo = NULL;
355  }
356  else if (gPan)
357  { // if we are currently panning, end pan
358  gPan = GL_FALSE;
359  gTrackingContextInfo = NULL;
360  }
361  startTrackball((long)x, (long)y,
366  gTrackball = GL_TRUE;
368  }
369  }
370 
371 
372 
373  if (state == GLUT_UP)
374  {
375  // stop trackball, pan, or dolly
376  point3d p(x, y, 0);// = GetOGLPos(pContextInfo, x, y);
377  tButtonType bType = kLeftButton;
378  switch (gCurrButton)
379  {
380  case GLUT_RIGHT_BUTTON: bType = kRightButton; break;
381  case GLUT_LEFT_BUTTON: bType = kLeftButton; break;
382  case GLUT_MIDDLE_BUTTON: bType = kMiddleButton; break;
383  }
384  if (HandleMouseClick(pContextInfo, x, y, p, bType, kMouseUp))
385  return;
386 
387 
388  // if we want to handle final movement when mouse is released
389 // if (!pContextInfo->camera[pContextInfo->currPort].thirdPerson)
390 // {
391 // }
392 
393 
394  if (gDolly) { // end dolly
395  gDolly = GL_FALSE;
396  }
397  else if (gPan) { // end pan
398  gPan = GL_FALSE;
399  }
400  else if (gTrackball) { // end trackball
401  gTrackball = GL_FALSE;
402  if (gTrackBallRotation[0] != 0.0)
403  {
404  // Mouse moves world object
406  {
408  {
409  for (int x = 0; x < pContextInfo->numPorts; x++)
411  }
412 
413  else {
415  }
416  }
417  else {
419  {
420  for (int x = 0; x < pContextInfo->numPorts; x++)
422  }
423 
424  else {
426  }
427  }
428  }
430  }
431  gTrackingContextInfo = NULL;
432  }
433 }
434 
435 
436 // move camera in x/y plane
437 static void mousePan (int x, int y, pRecContext pContextInfo)
438 {
439  GLfloat panX = (gDollyPanStartPoint[0] - x) / (900.0f / -pContextInfo->camera[pContextInfo->currPort].viewPos.z);
440  GLfloat panY = (gDollyPanStartPoint[1] - y) / (900.0f / -pContextInfo->camera[pContextInfo->currPort].viewPos.z);
442  {
443  for (int x = 0; x < pContextInfo->numPorts; x++)
444  {
445  pContextInfo->camera[x].viewPos.x += panX;
446  pContextInfo->camera[x].viewPos.y += panY;
447  }
448  }
449  else {
452  }
453  gDollyPanStartPoint[0] = (GLint) x;
454  gDollyPanStartPoint[1] = (GLint) y;
455 }
456 
457 
458 // move camera in z axis
459 static void mouseDolly (int x, int y, pRecContext pContextInfo)
460 {
461  GLfloat dolly = (gDollyPanStartPoint[1] - y)/20.f;// * -pContextInfo->camera[pContextInfo->currPort].viewPos.z / 300.0f;
462 
464  {
465  for (int x = 0; x < pContextInfo->numPorts; x++)
466  {
467  pContextInfo->camera[x].aperture += dolly;
468  if (pContextInfo->camera[x].aperture < 1)
469  pContextInfo->camera[x].aperture = 1;
470 // pContextInfo->camera[x].viewPos.z += dolly;
471  if (pContextInfo->camera[x].viewPos.z == 0.0) // do not let z = 0.0
472  pContextInfo->camera[x].viewPos.z = 0.0001;
473  updateProjection(pContextInfo, x); // update projection matrix
474  }
475  }
476  else {
478  if (pContextInfo->camera[x].aperture < 1)
479  pContextInfo->camera[x].aperture = 1;
480 // pContextInfo->camera[pContextInfo->currPort].viewPos.z += dolly;
481  if (pContextInfo->camera[pContextInfo->currPort].viewPos.z == 0.0) // do not let z = 0.0
483  updateProjection(pContextInfo, pContextInfo->currPort); // update projection matrix
484  }
485  gDollyPanStartPoint[0] = (GLint) x;
486  gDollyPanStartPoint[1] = (GLint) y;
487 }
488 
493 void renderScene(void)
494 {
495 
496  // Update the tank model frame (basically the treads)
497  //tankModelFrameUpdate();
498 
499  static double lastTime = ((double)clock()/CLOCKS_PER_SEC);
500  double currTime = ((double)clock()/CLOCKS_PER_SEC);
501 
502  if ((currTime - lastTime) > (1/fps)) {
503  lastTime = currTime;
505  }
506 
507 }
508 
509 
513 void resizeWindow(int x, int y)
514 {
515  CGRect rect;
516  while (0 != x%4)
517  x++;
518  while (0 != y%4)
519  y++;
520  int scale = 2;//glutGet(GLUT_WINDOW_SCALE);
521  rect.size.width = scale*x;
522  rect.size.height = scale*y;
523  rect.origin.x = 0;
524  rect.origin.y = 0;
525  resizeGL(pContextInfo, rect);
526 }
527 
528 
534 {
535  if (!pContextInfo)
536  return;
537 
540 
541  pContextInfo->globalCamera.viewWidth = (GLint)viewRect.size.width;
542  pContextInfo->globalCamera.viewHeight = (GLint)viewRect.size.height;
543 
544  for (int x = 0; x < pContextInfo->numPorts; x++)
545  {
547  }
548  // glViewport(0, 0, pContextInfo->camera.viewWidth, pContextInfo->camera.viewHeight);
549 
550  updateProjection(pContextInfo); // update projection matrix
551 }
552 
553 
554 
561 {
562  GLdouble ratio, radians, wd2;
563  int minVal, maxVal;
564  if (viewPort == -1)
565  {
566  minVal = 0;
567  maxVal = pContextInfo->numPorts-1;
568  }
569  else {
570  minVal = maxVal = viewPort;
571  }
572  for (int x = minVal; x <= maxVal; x++)
573  {
574  pContextInfo->camera[x].frust.near = 0.01;
575  pContextInfo->camera[x].frust.far = 20.0;
576 
577  radians = 0.0174532925 * pContextInfo->camera[x].aperture / 2; // half aperture degrees to radians
578  wd2 = pContextInfo->camera[x].frust.near * tan(radians);
579  ratio = pContextInfo->camera[x].viewWidth / (float) pContextInfo->camera[x].viewHeight;
580  if (ratio >= 1.0) {
581  pContextInfo->camera[x].frust.left = -ratio * wd2;
582  pContextInfo->camera[x].frust.right = ratio * wd2;
583  pContextInfo->camera[x].frust.top = wd2;
584  pContextInfo->camera[x].frust.bottom = -wd2;
585  } else {
586  pContextInfo->camera[x].frust.left = -wd2;
587  pContextInfo->camera[x].frust.right = wd2;
588  pContextInfo->camera[x].frust.top = wd2 / ratio;
589  pContextInfo->camera[x].frust.bottom = -wd2 / ratio;
590  }
591  }
592 }
593 
594 
599 {
600  // move view
601  glMatrixMode(GL_MODELVIEW);
602  glLoadIdentity();
603 
604  // mouse transforms object
605  if (pContextInfo->camera[currPort].thirdPerson)
606  {
607  gluLookAt (pContextInfo->camera[currPort].viewPos.x,
608  pContextInfo->camera[currPort].viewPos.y,
609  pContextInfo->camera[currPort].viewPos.z,
610  pContextInfo->camera[currPort].viewPos.x + pContextInfo->camera[currPort].viewDir.x,
611  pContextInfo->camera[currPort].viewPos.y + pContextInfo->camera[currPort].viewDir.y,
612  pContextInfo->camera[currPort].viewPos.z + pContextInfo->camera[currPort].viewDir.z,
613  pContextInfo->camera[currPort].viewUp.x, pContextInfo->camera[currPort].viewUp.y ,pContextInfo->camera[currPort].viewUp.z);
614 
615  if ((gTrackingContextInfo == pContextInfo) && gTrackBallRotation[0] != 0.0f) // if we have trackball rotation to map (this IS the test I want as it can be explicitly 0.0f)
616  {
619  }
620  else {
621  }
622 
623  // accumlated world rotation via trackball
624  glRotatef (pContextInfo->camera[currPort].rotations.worldRotation[0],
628  }
629  // if mouse moves whole world:
630  else {
631  glRotatef (pContextInfo->camera[currPort].rotations.cameraRotation[0],
635 
636  if ((gTrackingContextInfo == pContextInfo) && gTrackBallRotation[0] != 0.0f) // if we have trackball rotation to map (this IS the test I want as it can be explicitly 0.0f)
637  {
640  }
641 
642  //glRotatef(fRot[0], fRot[1], fRot[2], fRot[3]);
643 // glTranslated(-viewPos.x, -viewPos.y, -viewPos.z);
644 
645  glTranslatef(pContextInfo->camera[currPort].viewPos.x,
646  pContextInfo->camera[currPort].viewPos.y,
647  pContextInfo->camera[currPort].viewPos.z);
648 
649  if ((gTrackingContextInfo == pContextInfo) && gTrackBallRotation[0] != 0.0f) // if we have trackball rotation to map (this IS the test I want as it can be explicitly 0.0f)
650  {
651 // if (pContextInfo->currPort == currPort || pContextInfo->moveAllPortsTogether)
652 // glRotatef (gTrackBallRotation[0], gTrackBallRotation[1], gTrackBallRotation[2], gTrackBallRotation[3]);
653  }
654  else {
655  }
656 
657  // accumlated world rotation via trackball
658  // glRotatef (pContextInfo->rotations[currPort].worldRotation[0],
659  // pContextInfo->rotations[currPort].worldRotation[1],
660  // pContextInfo->rotations[currPort].worldRotation[2],
661  // pContextInfo->rotations[currPort].worldRotation[3]);
662 
663 
664 // glRotatef (pContextInfo->camera[currPort].rotations.worldRotation[0],
665 // pContextInfo->camera[currPort].rotations.worldRotation[1],
666 // pContextInfo->camera[currPort].rotations.worldRotation[2],
667 // pContextInfo->camera[currPort].rotations.worldRotation[3]);
668 //
669 // glTranslatef(pContextInfo->camera[currPort].viewPos.x,
670 // pContextInfo->camera[currPort].viewPos.y,
671 // pContextInfo->camera[currPort].viewPos.z);
672  }
673 
674 }
675 
676 
680 void drawCStringGL (char * cstrOut, GLuint fontList)
681 {
682  GLint i = 0;
683  if (!cstrOut)
684  return;
685  while (cstrOut [i])
686  glCallList (fontList + cstrOut[i++]);
687 }
688 
690 
691 void appendTextToBuffer(const char *tempStr)
692 {
693  int ind = int(strlen(pContextInfo->message));
694  pContextInfo->message[ind] = ' ';
695  sprintf(&pContextInfo->message[ind+1], "%s", tempStr);
696 
697  delete myTextBox;
698  point3d a(-.95, .95, -.95), b(.95, -.95, .95);
699  rgbColor rc(1, 1, 1);
700  myTextBox = new TextBox(pContextInfo->message, 120, a, b, 1000, true);
701  myTextBox->setColor(rc);
702 }
703 
704 void submitTextToBuffer(const char *val)
705 {
706  strncpy(pContextInfo->message, val, 255);
707  delete myTextBox;
708  point3d a(-.95, .95, -.95), b(.95, -.95, .95);
709  rgbColor rc(1, 1, 1);
710  myTextBox = new TextBox(pContextInfo->message, 120, a, b, 1000, true);
711  myTextBox->setColor(rc);
712 }
713 
714 void DrawGraphics(Graphics::Display &display, int port)
715 {
716 // float epsilon = 0.0002;
717 // float de = 0.00008;
718  //for (int x = 0; x < display.backgroundDrawCommands.size(); x++)
719  for (auto &i : display.text)
720  {
721  if (i.viewport != port)
722  continue;
723  glColor3f(i.c.r, i.c.g, i.c.b);
724  if (i.align == Graphics::textAlignCenter)
725  DrawTextCentered(i.loc.x, i.loc.y,i.loc.z, i.size*2, i.s.c_str());
726  else
727  DrawText(i.loc.x, i.loc.y,i.loc.z, i.size*2, i.s.c_str());
728  // std::string s;
729  // point loc;
730  // rgbColor c;
731  // float size;
732 
733  }
734 
735  for (auto &i : display.lineSegments)
736  {
737  if (i.viewport != port)
738  continue;
739  glColor3f(i.c.r, i.c.g, i.c.b);
740 
741  glBegin(GL_QUADS);
742  for (int t = 0; t < i.points.size()-1; t++)
743  {
744  GLfloat xOff = i.points[t].x-i.points[t+1].x;
745  GLfloat yOff = i.points[t].y-i.points[t+1].y;
746  GLfloat ratio = i.size/sqrt(xOff*xOff+yOff*yOff);
747  glVertex3f(i.points[t].x-ratio*yOff, i.points[t].y-ratio*xOff, i.points[t].z);
748  glVertex3f(i.points[t].x+ratio*yOff, i.points[t].y+ratio*xOff, i.points[t].z);
749  glVertex3f(i.points[t+1].x+ratio*yOff, i.points[t+1].y+ratio*xOff, i.points[t].z);
750  glVertex3f(i.points[t+1].x-ratio*yOff, i.points[t+1].y-ratio*xOff, i.points[t].z);
751  }
752  glEnd();
753 
754 // glLineWidth(i.size);
755 // glBegin(GL_LINE_STRIP);
756 // for (auto &p : i.points)
757 // {
758 // glVertex3f(p.x, p.y, p.z);
759 // }
760 // glEnd();
761  }
762 
763  for (int x = display.drawCommands.size()-1; x >= 0; x--)
764  //for (auto &i: drawCommands)
765  {
766  auto &i = display.drawCommands[x];
767  if (i.viewport != port)
768  continue;
769  switch (i.what)
770  {
772  {
773  glColor3f(i.shape.c.r, i.shape.c.g, i.shape.c.b);
774  glBegin(GL_QUADS);
775  glVertex2f(i.shape.r.left, i.shape.r.bottom);
776  glVertex2f(i.shape.r.left, i.shape.r.top);
777  glVertex2f(i.shape.r.right, i.shape.r.top);
778  glVertex2f(i.shape.r.right, i.shape.r.bottom);
779  glEnd();
780  // epsilon -= de;
781  break;
782  }
784  {
785  glColor3f(i.shape.c.r, i.shape.c.g, i.shape.c.b);
786  glLineWidth(i.shape.width);
787  // glBegin(GL_LINE_LOOP);
788  // glVertex2f(i.shape.r.left, i.shape.r.bottom);
789  // glVertex2f(i.shape.r.left, i.shape.r.top);
790  // glVertex2f(i.shape.r.right, i.shape.r.top);
791  // glVertex2f(i.shape.r.right, i.shape.r.bottom);
792  // glEnd();
793 
794  glBegin(GL_TRIANGLE_STRIP);
795  GLfloat rad = i.shape.width/2.0;
796  glVertex2f(i.shape.r.left-rad, i.shape.r.bottom+rad);
797  glVertex2f(i.shape.r.left+rad, i.shape.r.bottom-rad);
798 
799  glVertex2f(i.shape.r.left-rad, i.shape.r.top-rad);
800  glVertex2f(i.shape.r.left+rad, i.shape.r.top+rad);
801 
802  glVertex2f(i.shape.r.right+rad, i.shape.r.top-rad);
803  glVertex2f(i.shape.r.right-rad, i.shape.r.top+rad);
804 
805  glVertex2f(i.shape.r.right+rad, i.shape.r.bottom+rad);
806  glVertex2f(i.shape.r.right-rad, i.shape.r.bottom-rad);
807 
808  glVertex2f(i.shape.r.left-rad, i.shape.r.bottom+rad);
809  glVertex2f(i.shape.r.left+rad, i.shape.r.bottom-rad);
810 
811  glEnd();
812 
813  break;
814  }
816  {
817  glColor3f(i.shape.c.r, i.shape.c.g, i.shape.c.b);
818  //DrawSphere((i.shape.r.right+i.shape.r.left)/2, (i.shape.r.top+i.shape.r.bottom)/2, epsilon, fabs(i.shape.r.top-i.shape.r.bottom)/2.0);
819  DrawCircle((i.shape.r.right+i.shape.r.left)/2, (i.shape.r.top+i.shape.r.bottom)/2, fabs(i.shape.r.top-i.shape.r.bottom)/2.0, 64);
820  break;
821  }
823  {
824  glColor3f(i.shape.c.r, i.shape.c.g, i.shape.c.b);
825  FrameCircle((i.shape.r.right+i.shape.r.left)/2, (i.shape.r.top+i.shape.r.bottom)/2,
826  fabs(i.shape.r.top-i.shape.r.bottom)/2.0,
827  i.shape.width,
828  64);
829  break;
830  }
832  {
833  glColor3f(i.polygon.c.r, i.polygon.c.g, i.polygon.c.b);
834  DrawCircle(i.polygon.center.x, i.polygon.center.y, i.polygon.radius, i.polygon.segments, i.polygon.rotate);
835  break;
836  }
838  {
839  glColor3f(i.polygon.c.r, i.polygon.c.g, i.polygon.c.b);
840  //FrameCircle(i.polygon.p.x, i.polygon.p.y, i.polygon.radius, i.polygon.segments, , i.polygon.rotate);
841  break;
842  }
844  {
845  glLineWidth(i.line.width);
846  glBegin(GL_LINES);
847  glColor3f(i.line.c.r, i.line.c.g, i.line.c.b);
848  glVertex2f(i.line.start.x, i.line.start.y);
849  glVertex2f(i.line.end.x, i.line.end.y);
850  glEnd();
851  break;
852  }
853  }
854  }
855  for (auto &i : display.backgroundLineSegments)
856  {
857  if (i.viewport != port)
858  continue;
859  glColor3f(i.c.r, i.c.g, i.c.b);
860 
861  glBegin(GL_QUADS);
862  for (int t = 0; t < i.points.size()-1; t++)
863  {
864  GLfloat xOff = i.points[t].x-i.points[t+1].x;
865  GLfloat yOff = i.points[t].y-i.points[t+1].y;
866  GLfloat ratio = i.size/sqrt(xOff*xOff+yOff*yOff);
867  glVertex3f(i.points[t].x-ratio*yOff, i.points[t].y-ratio*xOff, i.points[t].z);
868  glVertex3f(i.points[t].x+ratio*yOff, i.points[t].y+ratio*xOff, i.points[t].z);
869  glVertex3f(i.points[t+1].x+ratio*yOff, i.points[t+1].y+ratio*xOff, i.points[t].z);
870  glVertex3f(i.points[t+1].x-ratio*yOff, i.points[t+1].y-ratio*xOff, i.points[t].z);
871  }
872  glEnd();
873 
874 // glLineWidth(i.size);
875 // glBegin(GL_LINE_STRIP);
876 // for (auto &p : i.points)
877 // {
878 // glVertex3f(p.x, p.y, p.z);
879 // }
880 // glEnd();
881  }
882  for (int x = display.backgroundDrawCommands.size()-1; x >= 0; x--)
883  //for (auto &i: display.backgroundDrawCommands)
884  {
885  auto &i = display.backgroundDrawCommands[x];
886  if (i.viewport != port)
887  continue;
888  switch (i.what)
889  {
891  {
892  glColor3f(i.shape.c.r, i.shape.c.g, i.shape.c.b);
893 
894  glBegin(GL_QUADS);
895  glVertex2f(i.shape.r.left, i.shape.r.bottom);
896  glVertex2f(i.shape.r.left, i.shape.r.top);
897  glVertex2f(i.shape.r.right, i.shape.r.top);
898  glVertex2f(i.shape.r.right, i.shape.r.bottom);
899  glEnd();
900  // epsilon -= de;
901  break;
902  }
904  {
905  glColor3f(i.shape.c.r, i.shape.c.g, i.shape.c.b);
906  glLineWidth(i.shape.width);
907  // glBegin(GL_LINE_LOOP);
908  // glVertex2f(i.shape.r.left, i.shape.r.bottom);
909  // glVertex2f(i.shape.r.left, i.shape.r.top);
910  // glVertex2f(i.shape.r.right, i.shape.r.top);
911  // glVertex2f(i.shape.r.right, i.shape.r.bottom);
912  // glEnd();
913 
914  glBegin(GL_TRIANGLE_STRIP);
915  GLfloat rad = i.shape.width/2.0;
916  glVertex2f(i.shape.r.left-rad, i.shape.r.bottom+rad);
917  glVertex2f(i.shape.r.left+rad, i.shape.r.bottom-rad);
918 
919  glVertex2f(i.shape.r.left-rad, i.shape.r.top-rad);
920  glVertex2f(i.shape.r.left+rad, i.shape.r.top+rad);
921 
922  glVertex2f(i.shape.r.right+rad, i.shape.r.top-rad);
923  glVertex2f(i.shape.r.right-rad, i.shape.r.top+rad);
924 
925  glVertex2f(i.shape.r.right+rad, i.shape.r.bottom+rad);
926  glVertex2f(i.shape.r.right-rad, i.shape.r.bottom-rad);
927 
928  glVertex2f(i.shape.r.left-rad, i.shape.r.bottom+rad);
929  glVertex2f(i.shape.r.left+rad, i.shape.r.bottom-rad);
930 
931  glEnd();
932 
933 
934  break;
935  }
937  {
938  glColor3f(i.shape.c.r, i.shape.c.g, i.shape.c.b);
939  DrawCircle((i.shape.r.right+i.shape.r.left)/2, (i.shape.r.top+i.shape.r.bottom)/2, fabs(i.shape.r.top-i.shape.r.bottom)/2.0, 64);
940  break;
941  }
943  {
944  glColor3f(i.polygon.c.r, i.polygon.c.g, i.polygon.c.b);
945  DrawCircle(i.polygon.center.x, i.polygon.center.y, i.polygon.radius, i.polygon.segments, i.polygon.rotate);
946  break;
947  }
949  {
950  glColor3f(i.polygon.c.r, i.polygon.c.g, i.polygon.c.b);
951  //FrameCircle(i.polygon.p.x, i.polygon.p.y, i.polygon.radius, i.polygon.segments, , i.polygon.rotate);
952  break;
953  }
955  {
956 
957  break;
958  }
960  {
961  glLineWidth(i.line.width);
962  glBegin(GL_LINES);
963  glColor3f(i.line.c.r, i.line.c.g, i.line.c.b);
964  glVertex2f(i.line.start.x, i.line.start.y);
965  glVertex2f(i.line.end.x, i.line.end.y);
966  glEnd();
967  // epsilon -= de;
968  break;
969  }
970  }
971  }
972 
973 // std::vector<data> backgroundDrawCommands;
974 // std::vector<textInfo> backgroundText;
975 // std::vector<segments> backgroundLineSegments;
976 //
977 // std::vector<data> drawCommands;
978 // std::vector<textInfo> text;
979 // std::vector<segments> lineSegments;
980  // These are static items that don't usually change from frame to frame
981 
982 }
983 
988 {
989  if (!pContextInfo)
990  return;
992  // clear our drawable
993  glClear(GL_COLOR_BUFFER_BIT);
994 
995  for (int x = 0; x < pContextInfo->numPorts; x++)
996  {
997  glClear(GL_DEPTH_BUFFER_BIT);
998 
1000  //if (pContextInfo->drawing)
1001  {
1002  // set projection
1003  glMatrixMode(GL_PROJECTION);
1004  glLoadIdentity();
1005 
1009  // projection matrix already set
1011 
1014  if (pContextInfo->currPort == x)
1015  {
1016  glMatrixMode(GL_PROJECTION); glLoadIdentity();
1017  glMatrixMode(GL_MODELVIEW); glLoadIdentity();
1018  gluOrtho2D(0, pContextInfo->camera[x].viewWidth, 0, pContextInfo->camera[x].viewHeight);
1019  glDisable(GL_LIGHTING);
1020  glColor3ub(255, 255, 255);
1021  glBegin(GL_LINE_LOOP);
1022  glVertex2i(0, 0);
1023  glVertex2i(pContextInfo->camera[x].viewWidth, 0);
1025  glVertex2i(0, pContextInfo->camera[x].viewHeight);
1026  glEnd();
1027  }
1028  }
1029  }
1030  if (myTextBox)
1031  {
1032  myTextBox->stepTime(0.1);
1033  myTextBox->draw();
1034  }
1036  glutSwapBuffers();
1037 }
1038 
1039 
1043 /*void trajectoryDrawGL (pRecContext pContextInfo)
1044 {
1045  if (!pContextInfo)
1046  return;
1047 
1048  // clear our drawable
1049  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1050 
1051  // projection matrix already set
1052  updateModelView (pContextInfo);
1053 
1054  glDisable(GL_LIGHTING);
1055  pContextInfo->unitLayer->GetMap()->OpenGLDraw(kPolygons);
1056  if (pContextInfo->unitLayer->getMapAbstractionDisplay())
1057  {
1058  pContextInfo->unitLayer->getMapAbstractionDisplay()->OpenGLDraw();
1059  }
1060  glEnable(GL_LIGHTING);
1061 
1062  static double lastTime = ((double)clock()/CLOCKS_PER_SEC);
1063  double currTime = ((double)clock()/CLOCKS_PER_SEC);
1064 
1065  pContextInfo->unitLayer->advanceTime(currTime-lastTime);
1066  lastTime = currTime;
1067  if (pContextInfo->drawing)
1068  {
1069  pContextInfo->unitLayer->OpenGLDraw();
1070  if (pContextInfo->info) {
1071  glDisable(GL_LIGHTING);
1072  drawInfo (pContextInfo);
1073  }
1074  }
1075  frameCallback(pContextInfo->unitLayer);
1076 
1077  //glFlush();
1078  glutSwapBuffers();
1079 
1080 }*/
1081 
1082 
1086 void buildGL(void)
1087 {
1088  if (NULL == pContextInfo)
1089  return;
1090 
1091  // build context
1092  CGRect viewRect = {{0.0f, 0.0f}, {0.0f, 0.0f}};
1093 
1094 // switch (pContextInfo->modeFSAA) {
1095 // case kFSAAOff:
1096 //#ifndef WIN32
1097 // //glDisable (GL_MULTISAMPLE_ARB);
1098 //#endif
1099 // break;
1100 // case kFSAAFast:
1103 // break;
1104 // case kFSAANice:
1105 // #ifndef WIN32
1106 // //glEnable (GL_MULTISAMPLE_ARB);
1107 // //glHint (GL_MULTISAMPLE_FILTER_HINT_NV, GL_NICEST);
1108 // #endif
1109 // break;
1110 // }
1111 
1112  // init GL stuff here
1113  glEnable(GL_DEPTH_TEST);
1114 
1115  glShadeModel(GL_SMOOTH);
1116  glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
1117  glFrontFace(GL_CCW);
1118  glPolygonOffset (1.0, 1.0);
1119 
1120  glClearColor(0.0,0.0,0.0,0.0);
1121 
1122  int scale = 2;//glutGet(GLUT_WINDOW_SCALE);
1123  viewRect.size.width = scale*glutGet(GLUT_WINDOW_WIDTH);
1124  viewRect.size.height = scale*glutGet(GLUT_WINDOW_HEIGHT);
1125 
1126  // setup viewport and prespective
1127  resizeGL(pContextInfo, viewRect); // forces projection matrix update
1128 
1129  SetLighting();
1130 }
1131 
1132 
HandleFrame
void HandleFrame(pRecContext pContextInfo, int viewport)
Definition: Common.cpp:163
getCurrentContext
pRecContext getCurrentContext()
Definition: GLUThog.cpp:49
recContext::globalCamera
recCamera globalCamera
Definition: Common.h:67
addToRotationTrackball
void addToRotationTrackball(float *dA, float *A)
Definition: Trackball.cpp:168
rgbColor
A color; r/g/b are between 0...1.
Definition: Colors.h:17
DrawCircle
void DrawCircle(GLdouble _x, GLdouble _y, GLdouble tRadius, int segments, float rotation)
Definition: GLUtil.cpp:421
CGRect::origin
CGPoint origin
Definition: GLUThog.h:32
CGRect::size
CGSize size
Definition: GLUThog.h:33
TextBox
Definition: TextBox.h:19
createMenus
void createMenus()
Definition: GLUThog.cpp:100
kWindowCreated
@ kWindowCreated
Definition: Common.h:159
modifiers
int modifiers
Called when a mouse button is pressed.
Definition: GLUThog.cpp:241
updateProjection
void updateProjection(pRecContext pContextInfo, int viewPort)
Update the projection matrix based on camera and view info.
Definition: GLUThog.cpp:560
gTrackball
GLboolean gTrackball
Definition: GLUThog.cpp:36
gPan
GLboolean gPan
Definition: GLUThog.cpp:35
recVec::z
GLdouble z
Definition: GLUtil.h:98
ProcessCommandLineArgs
void ProcessCommandLineArgs(int argc, char *argv[])
Definition: Common.cpp:222
Graphics::Display::EndFrame
void EndFrame()
Definition: Graphics.cpp:37
HandleWindowEvent
void HandleWindowEvent(pRecContext pContextInfo, tWindowEventType e)
Definition: Common.cpp:652
if
if(state==GLUT_DOWN)
Definition: GLUThog.cpp:244
Common.h
Graphics::Display::kFrameRectangle
@ kFrameRectangle
Definition: Graphics.h:217
CGPoint::y
float y
Definition: GLUThog.h:25
recCamera::frust
recFrustum frust
Definition: Common.h:45
Graphics::Display::backgroundDrawCommands
std::vector< data > backgroundDrawCommands
Definition: Graphics.h:272
TextBox::setColor
void setColor(rgbColor _myColor)
Definition: TextBox.h:27
recContext::camera
recCamera camera[MAXPORTS]
Definition: Common.h:69
Graphics::Display::kLine
@ kLine
Definition: Graphics.h:224
main
int main(int argc, char **argv)
Definition: GLUThog.cpp:14
startTrackball
void startTrackball(long x, long y, long originX, long originY, long width, long height)
Definition: Trackball.cpp:62
updateModelView
void updateModelView(pRecContext pContextInfo, int currPort)
Updates the viewpoint of the model.
Definition: GLUThog.cpp:598
Graphics::Display::kFillRectangle
@ kFillRectangle
Definition: Graphics.h:216
mouseMovedNoButton
void mouseMovedNoButton(int x, int y)
Definition: GLUThog.cpp:124
TextBox.h
rotation
float rotation[3][3]
Definition: RC.cpp:21
DoKeyboardCommand
bool DoKeyboardCommand(pRecContext pContextInfo, unsigned char keyHit, bool shift, bool cntrl, bool alt)
Definition: Common.cpp:703
recCamera::viewOriginY
GLfloat viewOriginY
Definition: Common.h:48
SetLighting
void SetLighting(GLfloat ambientf=0.2f, GLfloat diffusef=1.0f, GLfloat specularf=1.0f)
Definition: GLUtil.cpp:565
Graphics::Display::StartFrame
void StartFrame()
Definition: Graphics.cpp:29
HandleMouseClick
bool HandleMouseClick(pRecContext pContextInfo, int viewport, int x, int y, point3d where, tButtonType button, tMouseEventType mouse)
Definition: Common.cpp:600
recContext::message
char message[256]
Definition: Common.h:76
recContext
struct recContext recContext
Definition: Common.h:82
Graphics::Display::kFrameOval
@ kFrameOval
Definition: Graphics.h:221
mousePressedButton
void mousePressedButton(int button, int state, int x, int y)
Called when a mouse button is pressed.
Definition: SFML_HOG.cpp:311
resizeGL
void resizeGL(pRecContext pContextInfo, CGRect viewRect)
Handles resizing of GL need context update and if the window dimensions change, a window dimension up...
Definition: GLUThog.cpp:533
rollToTrackball
void rollToTrackball(long x, long y, float rot[4])
Definition: Trackball.cpp:104
recFrustum::far
GLdouble far
Definition: Common.h:31
recContext::display
Graphics::Display display
Definition: Common.h:79
TextBox::draw
void draw()
Definition: TextBox.cpp:46
CGPoint::x
float x
Definition: GLUThog.h:24
submitTextToBuffer
void submitTextToBuffer(const char *val)
Definition: GLUThog.cpp:704
Graphics::Display::kFrameNGon
@ kFrameNGon
Definition: Graphics.h:223
recCamera::viewDir
recVec viewDir
Definition: Common.h:40
Graphics::Display::drawCommands
std::vector< data > drawCommands
Definition: Graphics.h:268
renderScene
void renderScene(void)
Renders the scene.
Definition: GLUThog.cpp:493
kMiddleButton
@ kMiddleButton
Definition: Common.h:147
kMouseDrag
@ kMouseDrag
Definition: Common.h:154
Graphics::Display::kFillOval
@ kFillOval
Definition: Graphics.h:220
DrawTextCentered
void DrawTextCentered(double x, double y, double z, double scale, const char *str)
Definition: GLUtil.cpp:542
recRotation::worldRotation
GLfloat worldRotation[4]
Definition: Common.h:26
point3d
#define point3d
Definition: GLUtil.h:123
mouseDolly
static void mouseDolly(int x, int y, pRecContext pContextInfo)
Definition: GLUThog.cpp:459
Graphics::Display
Definition: Graphics.h:146
gDollyPanStartPoint
GLint gDollyPanStartPoint[2]
Definition: GLUThog.cpp:32
mousePan
static void mousePan(int x, int y, pRecContext pContextInfo)
Definition: GLUThog.cpp:437
gTrackingContextInfo
pRecContext gTrackingContextInfo
Definition: GLUThog.cpp:37
recContext
Definition: Common.h:64
kMouseDown
@ kMouseDown
Definition: Common.h:152
DrawText
void DrawText(double x, double y, double z, double scale, const char *str)
Definition: GLUtil.cpp:526
myTextBox
TextBox * myTextBox
Definition: GLUThog.cpp:689
recCamera::rotations
recRotation rotations
Definition: Common.h:42
hog_main
int hog_main(int argc, char **argv)
recRotation::cameraRotation
GLfloat cameraRotation[4]
Definition: Common.h:27
recContext::currPort
int currPort
Definition: Common.h:71
setPortCamera
void setPortCamera(pRecContext pContextInfo, int currPort)
Definition: Common.cpp:823
resizeWindow
void resizeWindow(int x, int y)
Called when the window is resized.
Definition: GLUThog.cpp:513
Graphics::Display::kFillNGon
@ kFillNGon
Definition: Graphics.h:222
kRightButton
@ kRightButton
Definition: Common.h:146
kLeftButton
@ kLeftButton
Definition: Common.h:145
mouseMovedButton
void mouseMovedButton(int x, int y)
Called when the mouse is moved with a button pressed down.
Definition: GLUThog.cpp:176
Trackball.h
appendTextToBuffer
void appendTextToBuffer(const char *tempStr)
Definition: GLUThog.cpp:691
recCamera::viewOriginX
GLfloat viewOriginX
Definition: Common.h:48
CGRect
Definition: GLUThog.h:31
recCamera::thirdPerson
bool thirdPerson
Definition: Common.h:37
recCamera::viewWidth
GLint viewWidth
Definition: Common.h:47
kNoButton
@ kNoButton
Definition: Common.h:148
FrameCircle
void FrameCircle(GLdouble _x, GLdouble _y, GLdouble tRadius, GLdouble lineWidth, int segments, float rotation)
Definition: GLUtil.cpp:407
TextBox::stepTime
void stepTime(double amount)
Definition: TextBox.cpp:41
Graphics::textAlignCenter
@ textAlignCenter
Definition: Graphics.h:20
gTrackBallRotation
GLfloat gTrackBallRotation[4]
Definition: GLUThog.cpp:33
Graphics::Display::text
std::vector< textInfo > text
Definition: Graphics.h:269
DrawGraphics
void DrawGraphics(Graphics::Display &display, int port)
Definition: GLUThog.cpp:714
recFrustum::left
GLdouble left
Definition: Common.h:31
recFrustum::near
GLdouble near
Definition: Common.h:31
gCurrButton
int gCurrButton
Definition: GLUThog.cpp:38
setViewport
void setViewport(pRecContext pContextInfo, int currPort)
Definition: Common.cpp:846
drawGL
void drawGL(pRecContext pContextInfo)
Main OpenGL drawing function.
Definition: GLUThog.cpp:987
CGSize::height
float height
Definition: GLUThog.h:16
GetContext
pRecContext GetContext(unsigned long windowID)
Definition: GLUThog.cpp:44
std
Definition: CanonicalGraphEnvironment.h:26
recFrustum::bottom
GLdouble bottom
Definition: Common.h:31
kMouseMove
@ kMouseMove
Definition: Common.h:155
recContext::numPorts
int numPorts
Definition: Common.h:71
CGSize::width
float width
Definition: GLUThog.h:15
Graphics::Display::lineSegments
std::vector< segments > lineSegments
Definition: Graphics.h:270
buildGL
void buildGL(void)
End OpenGL drawing function - for visualizing trajectory merging.
Definition: GLUThog.cpp:1086
recFrustum::top
GLdouble top
Definition: Common.h:31
backup
pRecContext backup
Definition: GLUThog.cpp:41
GLUThog.h
kMouseUp
@ kMouseUp
Definition: Common.h:153
recFrustum::right
GLdouble right
Definition: Common.h:31
recVec::y
GLdouble y
Definition: GLUtil.h:98
recCamera::aperture
GLdouble aperture
Definition: Common.h:46
tButtonType
tButtonType
Definition: Common.h:144
gDolly
GLboolean gDolly
Definition: GLUThog.cpp:34
processMenuEvents
void processMenuEvents(int option)
Definition: GLUThog.cpp:104
Graphics::Display::backgroundLineSegments
std::vector< segments > backgroundLineSegments
Definition: Graphics.h:274
recVec::x
GLdouble x
Definition: GLUtil.h:98
recCamera::viewUp
recVec viewUp
Definition: Common.h:41
initialConditions
void initialConditions(pRecContext pContextInfo)
Definition: Common.cpp:680
recCamera::viewHeight
GLint viewHeight
Definition: Common.h:47
keyPressed
void keyPressed(unsigned char key, int, int)
Called when a key is pressed, and no other keys are held down.
Definition: GLUThog.cpp:113
drawCStringGL
void drawCStringGL(char *cstrOut, GLuint fontList)
Draws a CString in OpenGL.
Definition: GLUThog.cpp:680
recContext::moveAllPortsTogether
bool moveAllPortsTogether
Definition: Common.h:72
recCamera::viewPos
recVec viewPos
Definition: Common.h:35
fps
double fps
Definition: GLUThog.cpp:42
RunHOGGUI
void RunHOGGUI(int argc, char **argv, int windowDimension)
Definition: GLUThog.cpp:54
pContextInfo
pRecContext pContextInfo
Definition: GLUThog.cpp:31