123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- #include "SceneOpenGL.h"
- SceneOpenGL::SceneOpenGL(std::string titre, int largeur, int hauteur)
- : m_titreFenetre(titre), m_largeurFenetre(largeur), m_hauteurFenetre(hauteur), m_fenetre(0), m_contexteOpenGL(0),
- m_camera(glm::vec3(-30.0f,1.78f*2.0f,-22.2f),glm::vec3(0.0,2.6,0.0),glm::vec3(0.0,1.0,0.0),0.32,0.36)
- {
- }
- SceneOpenGL::~SceneOpenGL()
- {
- SDL_GL_DeleteContext(m_contexteOpenGL);
- SDL_DestroyWindow(m_fenetre);
- SDL_Quit();
- }
- bool SceneOpenGL::initialiserFenetre()
- {
- // Initialisation de la SDL
- if(SDL_Init(SDL_INIT_VIDEO) < 0)
- {
- std::cout << "Erreur lors de l'initialisation de la SDL : " << SDL_GetError() << std::endl;
- SDL_Quit();
- return false;
- }
- // Version d'OpenGL
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
- // Double Buffer
- SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
- SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
- // Création de la fenêtre
- m_fenetre = SDL_CreateWindow(m_titreFenetre.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
- m_largeurFenetre, m_hauteurFenetre, SDL_WINDOW_SHOWN | SDL_WINDOW_FULLSCREEN | SDL_WINDOW_OPENGL);
- if(m_fenetre == 0)
- {
- std::cout << "Erreur lors de la creation de la fenetre : " << SDL_GetError() << std::endl;
- SDL_Quit();
- return false;
- }
- m_input.setWindow(m_fenetre);
- // Création du contexte OpenGL
- m_contexteOpenGL = SDL_GL_CreateContext(m_fenetre);
- if(m_contexteOpenGL == 0)
- {
- std::cout << SDL_GetError() << std::endl;
- SDL_DestroyWindow(m_fenetre);
- SDL_Quit();
- return false;
- }
- return true;
- }
- bool SceneOpenGL::initGL()
- {
- glEnable(GL_DEPTH_TEST);
- return true;
- }
- void SceneOpenGL::bouclePrincipale()
- {
- /// Variables
- //Gestion du temps
- unsigned int frameRate (1000 / 36);//36fps
- Uint32 debutBoucle(0), finBoucle(0), tempsEcoule(0);
- //Matrices
- glm::mat4 projection;
- glm::mat4 modelView(1.0);
- glm::mat4 sauvegarde(1.0);//Pour sauvegarder modelView
- //Vecteurs
- glm::vec3 axeX(1.0,0.0,0.0);
- glm::vec3 axeY(0.0,1.0,0.0);
- glm::vec3 axeZ(0.0,0.0,1.0);
- //Objets
- Cube caisseBasique(2.0,"Shaders/couleur3D.vert", "Shaders/couleur3D.frag");
- caisseBasique.charger();
- float rotation(0.0);
- float translation(1.0);
- float acceleration(0.0);
- ///Initialisation
- projection = glm::perspective(70.0, (double) m_largeurFenetre / m_hauteurFenetre, 1.0, 100.0);
- m_input.capturerPointeur(true);
- m_input.afficherPointeur(false);
- m_input.setMoveKeys(SDL_SCANCODE_W, SDL_SCANCODE_S, SDL_SCANCODE_D, SDL_SCANCODE_A);
- /// Boucle principale
- bool terminer(false);
- while(!m_input.terminer() && !terminer)
- {
- // Amorce boucle
- debutBoucle = SDL_GetTicks();
- // Gestion des évènements
- m_input.updateEvenements();
- terminer = m_input.getTouche(SDL_SCANCODE_ESCAPE);
- m_camera.setVol(m_input.getTouche(SDL_SCANCODE_LSHIFT));
- if (m_input.getTouche(SDL_SCANCODE_P))
- m_camera.setPosition(glm::vec3(30.0,1.78*2.0,22.2));
- m_camera.deplacer(m_input);
- // Nettoyage
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- m_camera.lookAt(modelView);
- // Mouvements
- rotation += 1.0;
- if (rotation >= 360.0)
- rotation -= 360.0;
- acceleration = -translation*0.004+acceleration;
- translation += acceleration;
- //Save
- sauvegarde = modelView;
- ///Cube 1
- //Transformations
- modelView = glm::translate(sauvegarde, glm::vec3(-2.5,1.0,-0.3));
- modelView = glm::rotate(modelView, 36.6f, axeY);
- //Afficher
- caisseBasique.afficher(projection,modelView);
- ///Cube 2
- //Transformations
- modelView = glm::translate(sauvegarde, glm::vec3(-0.4,1.0,-3.2));
- //Afficher
- caisseBasique.afficher(projection,modelView);
- ///Cube 3
- //Transformations
- modelView = glm::translate(sauvegarde, glm::vec3(2.5,1.0,0.0));
- //Afficher
- caisseBasique.afficher(projection,modelView);
- /// Actualisation de la fenêtre
- SDL_GL_SwapWindow(m_fenetre);
- /// Temps
- // Calcul du temps écoulé
- finBoucle = SDL_GetTicks();
- tempsEcoule = finBoucle - debutBoucle;
- // Si nécessaire, on met en pause le programme
- if(tempsEcoule < frameRate)
- SDL_Delay(frameRate - tempsEcoule);
- }
- }
|