Main.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include <GL/freeglut.h>
  2. #include "CrystalPoint.h"
  3. #include <stdio.h>
  4. #include "Vector.h"
  5. void configureOpenGL(void);
  6. CrystalPoint* app;
  7. bool justMoved = false;
  8. int main(int argc, char* argv[])
  9. {
  10. app = new CrystalPoint();
  11. glutInit(&argc, argv);
  12. configureOpenGL();
  13. app->init();
  14. glutDisplayFunc([]() { app->draw(); } );
  15. glutIdleFunc([]() { app->update(); } );
  16. glutReshapeFunc([](int w, int h) { app->width = w; app->height = h; glViewport(0, 0, w, h); });
  17. //Keyboard
  18. glutKeyboardFunc([](unsigned char c, int, int) { app->keyboardState.keys[c] = true; });
  19. glutKeyboardUpFunc([](unsigned char c, int, int) { app->keyboardState.keys[c] = false; });
  20. glutSpecialFunc([](int c, int, int) { app->keyboardState.special[c] = true; });
  21. glutSpecialUpFunc([](int c, int, int) { app->keyboardState.special[c] = false; });
  22. //Mouse
  23. glutPassiveMotionFunc([](int x, int y)
  24. {
  25. if (justMoved)
  26. {
  27. justMoved = false;
  28. return;
  29. }
  30. int dx = x - app->width / 2;
  31. int dy = y - app->height / 2;
  32. if ((dx != 0 || dy != 0) && abs(dx) < 400 && abs(dy) < 400)
  33. {
  34. app->mouseOffset = app->mouseOffset + Vec2f(dx,dy);
  35. glutWarpPointer(app->width / 2, app->height / 2);
  36. justMoved = true;
  37. }
  38. });
  39. glutMainLoop();
  40. return 0;
  41. }
  42. void configureOpenGL()
  43. {
  44. //Init window and glut display mode
  45. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  46. glutInitWindowSize(800, 600);
  47. glutCreateWindow("Crystal Point");
  48. glutFullScreen();
  49. //Depth testing
  50. glEnable(GL_DEPTH_TEST);
  51. //Alpha blending
  52. glEnable(GL_BLEND);
  53. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  54. //Alpha testing
  55. glEnable(GL_ALPHA_TEST);
  56. glAlphaFunc(GL_GREATER, 0.01f);
  57. //Lighting
  58. GLfloat mat_specular[] = { 0.2, 0.2, 0.2, 0 };
  59. //GLfloat mat_shininess[] = { 5.0 };
  60. GLfloat light_position[] = { 0.0, 2.0, 1.0, 0 };
  61. GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 0 };
  62. GLfloat light_ambient[] = { 0.3, 0.3, 0.3, 0 };
  63. glClearColor(0.7, 0.7, 1.0, 1.0);
  64. //glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  65. //glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  66. //glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  67. //glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  68. glEnable(GL_LIGHTING);
  69. glEnable(GL_LIGHT0);
  70. glutSetCursor(GLUT_CURSOR_CROSSHAIR);
  71. }