Main.cpp 3.9 KB

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