Main.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include "Header.h"
  2. #include "Model.h"
  3. #include "Player.h"
  4. #include "StateHandler.h"
  5. #include "State.h"
  6. //Prototypes
  7. void bindFuncOpenGL(void);
  8. void configureOpenGL(void);
  9. void loadModels(void);
  10. static int Width;
  11. static int Height;
  12. float lastFrameTime = 0;
  13. bool keys[255];
  14. //vector<Model*> models;
  15. //int currentModel = 0;
  16. StateHandler *statehandler;
  17. void display()
  18. {
  19. glClearColor(0.6f, 0.6f, 1, 1);
  20. glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  21. glMatrixMode(GL_PROJECTION);
  22. glLoadIdentity();
  23. gluPerspective(60.0f, (float)Width / Height, 0.5, 300);
  24. glMatrixMode(GL_MODELVIEW);
  25. glLoadIdentity();
  26. /*glRotatef(player.eyes.rotX, 1, 0, 0);
  27. glRotatef(player.eyes.rotY, 0, 1, 0);
  28. glTranslatef(player.eyes.posX, player.eyes.posY, player.eyes.posZ);
  29. glPushMatrix();
  30. glScalef(0.5f, 0.5f, 0.5f);
  31. glRotatef(180, 1, 0, 0);
  32. glRotatef(45, 0, 0, 1);
  33. glRotatef(90, 0, 1, 0);
  34. models[currentModel]->draw();
  35. glPopMatrix();*/
  36. //Draw here
  37. statehandler->GetCurrentState()->Display();
  38. //player->Display();
  39. //glutSolidCube(10.0);
  40. glDisable(GL_TEXTURE_2D);
  41. glutSwapBuffers();
  42. }
  43. void idle()
  44. {
  45. float frameTime = glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
  46. float deltaTime = frameTime - lastFrameTime;
  47. lastFrameTime = frameTime;
  48. statehandler->GetCurrentState()->Keyboard(keys,deltaTime);
  49. statehandler->GetCurrentState()->Idle(deltaTime);
  50. glutPostRedisplay();
  51. }
  52. void mousemotion(int x, int y)
  53. {
  54. int dx = x - Width / 2;
  55. int dy = y - Height / 2;
  56. if ((dx != 0 || dy != 0) && abs(dx) < 400 && abs(dy) < 400)
  57. {
  58. statehandler->GetCurrentState()->MouseMove(x, y, dx, dy);
  59. glutWarpPointer(Width / 2, Height / 2);
  60. }
  61. }
  62. void mouse(int button, int type, int x, int y)
  63. {
  64. statehandler->GetCurrentState()->MouseClick(button, type, x, y);
  65. }
  66. void keyboard(unsigned char key, int, int)
  67. {
  68. if (key == 27)
  69. exit(0);
  70. //std::cout << key << std::endl;
  71. keys[key] = true;
  72. }
  73. void keyboardup(unsigned char key, int, int)
  74. {
  75. keys[key] = false;
  76. }
  77. int main(int argc, char* argv[])
  78. {
  79. //Init GLUT
  80. glutInit(&argc, argv);
  81. //Configre OpenGL and FreeGLut
  82. configureOpenGL();
  83. //Bind functions
  84. bindFuncOpenGL();
  85. //Init models
  86. loadModels();
  87. //Start the main loop
  88. glutMainLoop();
  89. return 0;
  90. }
  91. void bindFuncOpenGL()
  92. {
  93. glutDisplayFunc(display);
  94. glutIdleFunc(idle);
  95. glutReshapeFunc([](int w, int h) { Width = w; Height= h; glViewport(0, 0, w, h); });
  96. //Keyboard
  97. glutKeyboardFunc(keyboard);
  98. glutKeyboardUpFunc(keyboardup);
  99. //Mouse
  100. glutMouseFunc(mouse);
  101. glutPassiveMotionFunc(mousemotion);
  102. }
  103. void configureOpenGL()
  104. {
  105. //Init window and glut display mode
  106. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  107. glutInitWindowSize(800, 600);
  108. glutCreateWindow("Crystal Point");
  109. glutFullScreen();
  110. //glutPositionWindow((glutGet(GLUT_SCREEN_WIDTH) / 2) - (glutGet(GLUT_WINDOW_WIDTH) / 2), (glutGet(GLUT_SCREEN_HEIGHT) / 2) - (glutGet(GLUT_WINDOW_HEIGHT) / 2));
  111. //Depth testing
  112. glEnable(GL_DEPTH_TEST);
  113. //Alpha blending
  114. glEnable(GL_BLEND);
  115. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  116. //Alpha testing
  117. glEnable(GL_ALPHA_TEST);
  118. glAlphaFunc(GL_GREATER, 0.01f);
  119. //Lighting
  120. GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  121. GLfloat mat_shininess[] = { 20.0 };
  122. GLfloat light_position[] = { 30.0, 30.0, 30.0, 1.0 };
  123. GLfloat light_diffuse[] = { 2.0, 2.0, 2.0, 1.0 };
  124. GLfloat light_ambient[] = { 2.0, 2.0, 2.0, 1.0 };
  125. glClearColor(0.0, 0.0, 0.0, 0.0);
  126. glShadeModel(GL_SMOOTH);
  127. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  128. glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  129. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  130. glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  131. glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  132. glEnable(GL_LIGHTING);
  133. glEnable(GL_LIGHT0);
  134. //Cursor
  135. glutWarpPointer(Width / 2, Height / 2);
  136. glutSetCursor(GLUT_CURSOR_CROSSHAIR);
  137. }
  138. void loadModels()
  139. {
  140. statehandler = new StateHandler();
  141. statehandler->Navigate(statehandler->WORLD_STATE);
  142. //models.push_back(new Model("models/weapons/ZwaardMetTextures/TextureZwaard.obj"));
  143. }