Main.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 = -30;
  16. float posY = -30;
  17. float posZ = -30;
  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. camera.posX += (float)cos((camera.rotY + angle) / 180 * M_PI) * fac;
  41. camera.posZ += (float)sin((camera.rotY + angle) / 180 * M_PI) * fac;
  42. if(heigth)
  43. camera.posY += (float)sin((camera.rotX + angle) / 180 * M_PI) * fac;
  44. }
  45. void idle()
  46. {
  47. float frameTime = glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
  48. float deltaTime = frameTime - lastFrameTime;
  49. lastFrameTime = frameTime;
  50. float speed = 10;
  51. if (keys['a']) move(0, deltaTime*speed, false);
  52. if (keys['d']) move(180, deltaTime*speed, false);
  53. if (keys['w']) move(90, deltaTime*speed, true);
  54. if (keys['s']) move(270, deltaTime*speed, true);
  55. glutPostRedisplay();
  56. }
  57. void mousemotion(int x, int y)
  58. {
  59. int dx = x - Width / 2;
  60. int dy = y - Height / 2;
  61. if ((dx != 0 || dy != 0) && abs(dx) < 400 && abs(dy) < 400)
  62. {
  63. camera.rotY += dx / 10.0f;
  64. camera.rotX += dy / 10.0f;
  65. glutWarpPointer(Width / 2, Height / 2);
  66. }
  67. }
  68. void keyboard(unsigned char key, int, int)
  69. {
  70. if (key == 27)
  71. exit(0);
  72. keys[key] = true;
  73. }
  74. void keyboardup(unsigned char key, int, int)
  75. {
  76. keys[key] = false;
  77. }
  78. int main(int argc, char* argv[])
  79. {
  80. //Init GLUT
  81. glutInit(&argc, argv);
  82. //Configre OpenGL and FreeGLut
  83. configureOpenGL();
  84. //Bind functions
  85. bindFuncOpenGL();
  86. //Init models
  87. loadModels();
  88. //Start the main loop
  89. glutMainLoop();
  90. return 0;
  91. }
  92. void bindFuncOpenGL()
  93. {
  94. glutDisplayFunc(display);
  95. glutIdleFunc(idle);
  96. glutReshapeFunc([](int w, int h) { Width = w; Height= h; glViewport(0, 0, w, h); });
  97. //Keyboard
  98. glutKeyboardFunc(keyboard);
  99. glutKeyboardUpFunc(keyboardup);
  100. //Mouse
  101. //glutMouseFunc(mouse);
  102. glutPassiveMotionFunc(mousemotion);
  103. }
  104. void configureOpenGL()
  105. {
  106. //Init window and glut display mode
  107. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  108. glutInitWindowSize(800, 600);
  109. glutCreateWindow("Crystal Point");
  110. glutFullScreen();
  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[] = { 50.0 };
  122. GLfloat light_position[] = { 30.0, 30.0, 30.0, 0.0 };
  123. glClearColor(0.0, 0.0, 0.0, 0.0);
  124. glShadeModel(GL_SMOOTH);
  125. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  126. glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  127. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  128. glEnable(GL_LIGHTING);
  129. glEnable(GL_LIGHT0);
  130. //Cursor
  131. glutWarpPointer(Width / 2, Height / 2);
  132. glutSetCursor(GLUT_CURSOR_NONE);
  133. }
  134. void loadModels()
  135. {
  136. models.push_back(new Model("models/ship/shipA_OBJ.obj"));
  137. }