Main.cpp 2.8 KB

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