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)
- {
- }
- 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, 1);
- // 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_OPENGL);
- if(m_fenetre == 0)
- {
- std::cout << "Erreur lors de la creation de la fenetre : " << SDL_GetError() << std::endl;
- SDL_Quit();
- return false;
- }
- // 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 / 50);//50fps
- Uint32 debutBoucle(0), finBoucle(0), tempsEcoule(0);
- //Objets
- Caisse monoCube(2.2,"Shaders/texture.vert","Shaders/texture.frag","Textures/planks.bmp");
- //Matrices
- glm::mat4 projection;
- glm::mat4 modelView(1.0);
- glm::mat4 originSave(1.0);
- glm::mat4 sauvegarde(1.0);//Pour sauvegarder modelView
- float rotation(0.0);
- float acceleration(0.0);
- //Vecteurs
- glm::vec3 posYeux(5.0,5.0,5.0);
- glm::vec3 focus(0.0,0.0,0.0);
- glm::vec3 axeY(0.0,1.0,0.0);
- glm::vec3 axeZ(0.0,0.0,1.0);
- ///Texture
- Surface3D sol(15.0, 15.0, "Shaders/texture.vert", "Shaders/texture.frag", "Textures/stonebrick.bmp",4.0);
- ///Initialisation
- projection = glm::perspective(70.0, (double) m_largeurFenetre / m_hauteurFenetre, 1.0, 100.0);
- /// 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);
- if (m_input.getTouche(SDL_SCANCODE_RIGHT))
- acceleration+=0.05;
- if (m_input.getTouche(SDL_SCANCODE_LEFT))
- acceleration-=0.05;
- rotation+=acceleration;
- acceleration*=0.99;
- if (rotation>=360.0)
- rotation-=360.0;
- // Nettoyage
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- originSave = modelView = glm::lookAt(posYeux,focus,axeY);
- //Rotation générale
- modelView = glm::rotate(modelView, rotation, axeY);
- ///Cube 1
- //Save
- sauvegarde = modelView;
- //Transformations
- modelView = glm::translate(modelView, glm::vec3(-2.5,0.0,0.0));
- modelView = glm::rotate(modelView, rotation, axeZ);
- modelView = glm::rotate(modelView, 2*rotation, axeY);
- //Afficher
- monoCube.afficher(projection,modelView);
- //Restauration
- modelView = sauvegarde;
- ///Cube 2
- //Transformations
- modelView = glm::translate(modelView, glm::vec3(2.5,0.0,0.0));
- modelView = glm::rotate(modelView, 2*rotation, axeY);
- //Afficher
- monoCube.afficher(projection,modelView);
- ///Texture
- //Config initiale
- modelView = originSave;
- modelView = glm::translate(modelView, glm::vec3(0.0,-1.1,0.0));
- modelView = glm::rotate(modelView, -rotation, axeY);
- sol.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);
- }
- }
|