Main.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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) { CrystalPoint::width = w; CrystalPoint::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. auto mousemotion = [](int x, int y)
  23. {
  24. if (justMoved)
  25. {
  26. justMoved = false;
  27. return;
  28. }
  29. int dx = x - app->width / 2;
  30. int dy = y - app->height / 2;
  31. if ((dx != 0 || dy != 0) && abs(dx) < 400 && abs(dy) < 400)
  32. {
  33. app->mouseOffset = app->mouseOffset + Vec2f(dx, dy);
  34. glutWarpPointer(app->width / 2, app->height / 2);
  35. justMoved = true;
  36. }
  37. };
  38. //Mouse
  39. glutPassiveMotionFunc(mousemotion);
  40. glutMotionFunc(mousemotion);
  41. CrystalPoint::height = GLUT_WINDOW_HEIGHT;
  42. CrystalPoint::width = GLUT_WINDOW_WIDTH;
  43. glutMainLoop();
  44. delete app;
  45. return 0;
  46. }
  47. void configureOpenGL()
  48. {
  49. //Init window and glut display mode
  50. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  51. glutInitWindowSize(800, 600);
  52. glutCreateWindow("Crystal Point");
  53. glutFullScreen();
  54. //Depth testing
  55. glEnable(GL_DEPTH_TEST);
  56. //Alpha blending
  57. glEnable(GL_BLEND);
  58. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  59. //Alpha testing
  60. glEnable(GL_ALPHA_TEST);
  61. glAlphaFunc(GL_GREATER, 0.01f);
  62. //Lighting
  63. GLfloat mat_specular[] = { 0.2, 0.2, 0.2, 0 };
  64. //GLfloat mat_shininess[] = { 5.0 };
  65. GLfloat light_position[] = { 0.0, 2.0, 1.0, 0 };
  66. GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 0 };
  67. GLfloat light_ambient[] = { 0.3, 0.3, 0.3, 0 };
  68. glClearColor(0.7, 0.7, 1.0, 1.0);
  69. //glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  70. //glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  71. //glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  72. //glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  73. glEnable(GL_LIGHTING);
  74. glEnable(GL_LIGHT0);
  75. //glEnable(GL_COLOR_MATERIAL);
  76. glutSetCursor(GLUT_CURSOR_NONE);
  77. }