SceneOpenGL.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include "SceneOpenGL.h"
  2. SceneOpenGL::SceneOpenGL(std::string titre, int largeur, int hauteur)
  3. : m_titreFenetre(titre), m_largeurFenetre(largeur), m_hauteurFenetre(hauteur), m_fenetre(0), m_contexteOpenGL(0)
  4. {
  5. }
  6. SceneOpenGL::~SceneOpenGL()
  7. {
  8. SDL_GL_DeleteContext(m_contexteOpenGL);
  9. SDL_DestroyWindow(m_fenetre);
  10. SDL_Quit();
  11. }
  12. bool SceneOpenGL::initialiserFenetre()
  13. {
  14. // Initialisation de la SDL
  15. if(SDL_Init(SDL_INIT_VIDEO) < 0)
  16. {
  17. std::cout << "Erreur lors de l'initialisation de la SDL : " << SDL_GetError() << std::endl;
  18. SDL_Quit();
  19. return false;
  20. }
  21. // Version d'OpenGL
  22. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
  23. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
  24. // Double Buffer
  25. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  26. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
  27. // Création de la fenêtre
  28. m_fenetre = SDL_CreateWindow(m_titreFenetre.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
  29. m_largeurFenetre, m_hauteurFenetre, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
  30. if(m_fenetre == 0)
  31. {
  32. std::cout << "Erreur lors de la creation de la fenetre : " << SDL_GetError() << std::endl;
  33. SDL_Quit();
  34. return false;
  35. }
  36. // Création du contexte OpenGL
  37. m_contexteOpenGL = SDL_GL_CreateContext(m_fenetre);
  38. if(m_contexteOpenGL == 0)
  39. {
  40. std::cout << SDL_GetError() << std::endl;
  41. SDL_DestroyWindow(m_fenetre);
  42. SDL_Quit();
  43. return false;
  44. }
  45. return true;
  46. }
  47. bool SceneOpenGL::initGL()
  48. {
  49. glEnable(GL_DEPTH_TEST);
  50. return true;
  51. }
  52. void SceneOpenGL::bouclePrincipale()
  53. {
  54. /// Variables
  55. //Gestion du temps
  56. unsigned int frameRate (1000 / 50);//50fps
  57. Uint32 debutBoucle(0), finBoucle(0), tempsEcoule(0);
  58. //Objets
  59. Caisse monoCube(2.2,"Shaders/texture.vert","Shaders/texture.frag","Textures/planks.bmp");
  60. //Matrices
  61. glm::mat4 projection;
  62. glm::mat4 modelView(1.0);
  63. glm::mat4 originSave(1.0);
  64. glm::mat4 sauvegarde(1.0);//Pour sauvegarder modelView
  65. float rotation(0.0);
  66. float acceleration(0.0);
  67. //Vecteurs
  68. glm::vec3 posYeux(5.0,5.0,5.0);
  69. glm::vec3 focus(0.0,0.0,0.0);
  70. glm::vec3 axeY(0.0,1.0,0.0);
  71. glm::vec3 axeZ(0.0,0.0,1.0);
  72. ///Texture
  73. Surface3D sol(15.0, 15.0, "Shaders/texture.vert", "Shaders/texture.frag", "Textures/stonebrick.bmp",4.0);
  74. ///Initialisation
  75. projection = glm::perspective(70.0, (double) m_largeurFenetre / m_hauteurFenetre, 1.0, 100.0);
  76. /// Boucle principale
  77. bool terminer(false);
  78. while(!m_input.terminer() && !terminer)
  79. {
  80. // Amorce boucle
  81. debutBoucle = SDL_GetTicks();
  82. // Gestion des évènements
  83. m_input.updateEvenements();
  84. terminer = m_input.getTouche(SDL_SCANCODE_ESCAPE);
  85. if (m_input.getTouche(SDL_SCANCODE_RIGHT))
  86. acceleration+=0.05;
  87. if (m_input.getTouche(SDL_SCANCODE_LEFT))
  88. acceleration-=0.05;
  89. rotation+=acceleration;
  90. acceleration*=0.99;
  91. if (rotation>=360.0)
  92. rotation-=360.0;
  93. // Nettoyage
  94. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  95. originSave = modelView = glm::lookAt(posYeux,focus,axeY);
  96. //Rotation générale
  97. modelView = glm::rotate(modelView, rotation, axeY);
  98. ///Cube 1
  99. //Save
  100. sauvegarde = modelView;
  101. //Transformations
  102. modelView = glm::translate(modelView, glm::vec3(-2.5,0.0,0.0));
  103. modelView = glm::rotate(modelView, rotation, axeZ);
  104. modelView = glm::rotate(modelView, 2*rotation, axeY);
  105. //Afficher
  106. monoCube.afficher(projection,modelView);
  107. //Restauration
  108. modelView = sauvegarde;
  109. ///Cube 2
  110. //Transformations
  111. modelView = glm::translate(modelView, glm::vec3(2.5,0.0,0.0));
  112. modelView = glm::rotate(modelView, 2*rotation, axeY);
  113. //Afficher
  114. monoCube.afficher(projection,modelView);
  115. ///Texture
  116. //Config initiale
  117. modelView = originSave;
  118. modelView = glm::translate(modelView, glm::vec3(0.0,-1.1,0.0));
  119. modelView = glm::rotate(modelView, -rotation, axeY);
  120. sol.afficher(projection,modelView);
  121. // Actualisation de la fenêtre
  122. SDL_GL_SwapWindow(m_fenetre);
  123. /// Temps
  124. // Calcul du temps écoulé
  125. finBoucle = SDL_GetTicks();
  126. tempsEcoule = finBoucle - debutBoucle;
  127. // Si nécessaire, on met en pause le programme
  128. if(tempsEcoule < frameRate)
  129. SDL_Delay(frameRate - tempsEcoule);
  130. }
  131. }