Main.cpp 3.5 KB

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