Main.cpp 2.5 KB

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