Main.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include <GL/freeglut.h>
  2. #include "CrystalJohan.h"
  3. #include <stdio.h>
  4. #include "vector.h"
  5. void configureOpenGL(void);
  6. CrystalJohan* app;
  7. bool justMoved = false;
  8. int main(int argc, char* argv[])
  9. {
  10. app = new CrystalJohan();
  11. glutInit(&argc, argv);
  12. configureOpenGL();
  13. app->init();
  14. glutDisplayFunc([]() { app->draw(); } );
  15. glutIdleFunc([]() { app->update(); } );
  16. glutReshapeFunc([](int w, int h) { app->width = w; app->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. //Mouse
  21. glutPassiveMotionFunc([](int x, int y)
  22. {
  23. if (justMoved)
  24. {
  25. justMoved = false;
  26. return;
  27. }
  28. int dx = x - app->width / 2;
  29. int dy = y - app->height / 2;
  30. if ((dx != 0 || dy != 0) && abs(dx) < 400 && abs(dy) < 400)
  31. {
  32. app->mouseOffset = app->mouseOffset + Vec2f(dx,dy);
  33. glutWarpPointer(app->width / 2, app->height / 2);
  34. justMoved = true;
  35. }
  36. });
  37. glutMainLoop();
  38. return 0;
  39. }
  40. void configureOpenGL()
  41. {
  42. //Init window and glut display mode
  43. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  44. glutInitWindowSize(800, 600);
  45. glutCreateWindow("Crystal Point");
  46. glutFullScreen();
  47. //Depth testing
  48. glEnable(GL_DEPTH_TEST);
  49. //Alpha blending
  50. glEnable(GL_BLEND);
  51. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  52. //Alpha testing
  53. glEnable(GL_ALPHA_TEST);
  54. glAlphaFunc(GL_GREATER, 0.01f);
  55. //Lighting
  56. GLfloat mat_specular[] = { 0.2, 0.2, 0.2, 0 };
  57. //GLfloat mat_shininess[] = { 5.0 };
  58. GLfloat light_position[] = { 0.0, 2.0, 1.0, 0 };
  59. GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 0 };
  60. GLfloat light_ambient[] = { 0.3, 0.3, 0.3, 0 };
  61. glClearColor(0.7, 0.7, 1.0, 1.0);
  62. //glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  63. //glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  64. //glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  65. //glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  66. //glEnable(GL_LIGHTING);
  67. //glEnable(GL_LIGHT0);
  68. glutSetCursor(GLUT_CURSOR_CROSSHAIR);
  69. }