Main.cpp 4.5 KB

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