Main.cpp 2.9 KB

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