Main.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include <GL/freeglut.h>
  2. #include "JancoProject.h"
  3. #include <stdio.h>
  4. #define STB_IMAGE_IMPLEMENTATION
  5. #include "stb_image.h"
  6. void configureOpenGL(void);
  7. CrystalPoint* app;
  8. bool justMoved = false;
  9. int main(int argc, char* argv[])
  10. {
  11. app = new CrystalPoint();
  12. glutInit(&argc, argv);
  13. srand (time(NULL));
  14. configureOpenGL();
  15. app->init();
  16. glutDisplayFunc([]() { app->draw(); } );
  17. glutIdleFunc([]() { app->update(); } );
  18. glutReshapeFunc([](int w, int h) { app->width = w; app->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. //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("Janco");
  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. glutWarpPointer(app->width / 2, app->height / 2);
  72. }