Partage
  • Partager sur Facebook
  • Partager sur Twitter

[SDL]deplacer objet sous opengl

Avec le clavier

Sujet résolu
    29 juillet 2007 à 16:23:40

    J'aurais voulu savoir comment pouvait on deplacer un objet 3d crée avec opengl (de type GlQuads), avec le clavier afin de pouvoir aller à gauche à droite en bas en haut.
    J'ai essayé avec gltranslated et la gestion des événements SDL_Event mais je n'y arrive pas. :(

    void jouer ()
    {
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity( );
    glEnable(GL_DEPTH_TEST);
    //        CUBE
    glPushMatrix ();
    glTranslated (0,0,0);
        glBegin(GL_QUADS);

        glColor3ub(255,0,0); //face rouge
        glVertex3d(1,1,1);
        glVertex3d(1,1,-1);
        glVertex3d(-1,1,-1);
        glVertex3d(-1,1,1);

        glColor3ub(0,255,0); //face verte
        glVertex3d(1,-1,1);
        glVertex3d(1,-1,-1);
        glVertex3d(1,1,-1);
        glVertex3d(1,1,1);

        glColor3ub(0,0,255); //face bleue
        glVertex3d(-1,-1,1);
        glVertex3d(-1,-1,-1);
        glVertex3d(1,-1,-1);
        glVertex3d(1,-1,1);

        glColor3ub(255,255,0); //face jaune
        glVertex3d(-1,1,1);
        glVertex3d(-1,1,-1);
        glVertex3d(-1,-1,-1);
        glVertex3d(-1,-1,1);

        glColor3ub(0,255,255); //face cyan
        glVertex3d(1,1,-1);
        glVertex3d(1,-1,-1);
        glVertex3d(-1,-1,-1);
        glVertex3d(-1,1,-1);

        glColor3ub(255,0,255); //face magenta
        glVertex3d(1,-1,1);
        glVertex3d(1,1,1);
        glVertex3d(-1,1,1);
        glVertex3d(-1,-1,1);

        glEnd();
    glPopMatrix ();
        glFlush();
        SDL_GL_SwapBuffers();
    • Partager sur Facebook
    • Partager sur Twitter
      29 juillet 2007 à 20:13:50

      Les transformations, c'est une question d'ordre. Elles s'applique à tout ce qui est dessiné aprés (je crois). Par contre la gestion des êvenements avec la SDL, ne devraient pas poser problème.
      Personnellement je créerais des variables qui stockent les translations appliquées à ton objet 3d. Par exemple:
      float deplacement_objet_x = 0.0f;
      float deplacement_objet_y = 0.0f;
      float deplacement_objet_z = 0.0f;
      • Partager sur Facebook
      • Partager sur Twitter
        29 juillet 2007 à 22:04:08

        J'ai déclaré la variable t_x pour l'instant, je verrai pour y et z quand je saurais faire avec x.J'ai changé la SDLK_UP et gltranslated.
        Mais je vois pas comment corriger mon erreur :(


        GLuint texture[1];
        double t_x = 0;
        void gestionclavier ()
        {
            int continuer = 1;
            SDL_Event evenement;

        while (continuer)
        {
        SDL_WaitEvent(&evenement);
        switch(evenement.type)
        {
        case SDL_QUIT:
        continuer = 0;
        break;
        case SDL_KEYDOWN:
        switch (evenement.key.keysym.sym)
        {
        case SDLK_UP:
        t_x -= 1;
        break;
        }
        }
        }
        }

        void chargertexture ()
        {
        texture[0] = loadTexture("pelouse.jpg");
        }

        void jouer ()
        {
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_TEXTURE_2D);
        chargertexture ();
        glMatrixMode( GL_PROJECTION );
        glLoadIdentity();
        gluPerspective(70,(double)800/600,1,1000);

        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity( );
        glEnable(GL_DEPTH_TEST);

        gluLookAt(5,3,4,0,0,0,0,0,1);

        //         TEXTURE

        glBindTexture(GL_TEXTURE_2D, texture[0]);
             glBegin(GL_QUADS);

             glTexCoord2i(0,0);      glVertex3i(-30,-30,-1);
             glTexCoord2i(1,0);      glVertex3i(30,-30,-1);
             glTexCoord2i(1,1);      glVertex3i(30,30,-1);
             glTexCoord2i(0,1);      glVertex3i(-30,30,-1);
             glEnd();

        glDisable(GL_TEXTURE_2D);

        //        CUBE
        glPushMatrix ();
        glTranslated (t_x,0,0);
            glBegin(GL_QUADS);

            glColor3ub(255,0,0); //face rouge
            glVertex3d(1,1,1);
            glVertex3d(1,1,-1);
            glVertex3d(-1,1,-1);
            glVertex3d(-1,1,1);

            glColor3ub(0,255,0); //face verte
            glVertex3d(1,-1,1);
            glVertex3d(1,-1,-1);
            glVertex3d(1,1,-1);
            glVertex3d(1,1,1);

            glColor3ub(0,0,255); //face bleue
            glVertex3d(-1,-1,1);
            glVertex3d(-1,-1,-1);
            glVertex3d(1,-1,-1);
            glVertex3d(1,-1,1);

            glColor3ub(255,255,0); //face jaune
            glVertex3d(-1,1,1);
            glVertex3d(-1,1,-1);
            glVertex3d(-1,-1,-1);
            glVertex3d(-1,-1,1);

            glColor3ub(0,255,255); //face cyan
            glVertex3d(1,1,-1);
            glVertex3d(1,-1,-1);
            glVertex3d(-1,-1,-1);
            glVertex3d(-1,1,-1);

            glColor3ub(255,0,255); //face magenta
            glVertex3d(1,-1,1);
            glVertex3d(1,1,1);
            glVertex3d(-1,1,1);
            glVertex3d(-1,-1,1);

            glEnd();
        glPopMatrix ();
            glFlush();
            SDL_GL_SwapBuffers();

         glEnable(GL_TEXTURE_2D);
        }
         

        • Partager sur Facebook
        • Partager sur Twitter
          29 juillet 2007 à 22:35:26

          Un peu d'indentation est si cher que ça?
          La politesse aussi?
          Si tu n'appelles jamais ta fonction jouer tu risque fortement de ne rien voir.
          Enfin essaye de faire en déplacant qu'un seul quadrilatère non texturé.
          Aussi une initialisation de matrice, le chargement d'une texture ne doit être fait qu'une seule fois.
          • Partager sur Facebook
          • Partager sur Twitter
            30 juillet 2007 à 9:06:47

            la fonction jouer est deja intégrée dans le main, je n'ai pas donné entièrement le code.
            quand j'exécute le programme tout y est sauf l'exécution au clavier.
            Je persiste, je continuer à chercher, à trifouiller.
            MERCI quand même!
            • Partager sur Facebook
            • Partager sur Twitter
              30 juillet 2007 à 9:12:43

              Alors donne le vrai code.
              • Partager sur Facebook
              • Partager sur Twitter
                30 juillet 2007 à 14:10:18

                donc voici le main et la fonction jouer que je vous ai déjà mis au dessus:

                int main ( int argc, char** argv )
                {
                SDL_Init(SDL_INIT_VIDEO);// CHARGEMENT DE LA SDL
                SDL_WM_SetCaption("Mon premier jeu video !", NULL);
                SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE | SDL_RESIZABLE);
                SDL_Surface *ecran = NULL;
                ecran = SDL_SetVideoMode(LARGEUR_FENETRE, HAUTEUR_FENETRE, 32, SDL_HWSURFACE | SDL_RESIZABLE);
                TTF_Init();
                SDL_Surface *texte = NULL, *texte1 = NULL, *texte2 = NULL, *texte3 = NULL, *texte4 = NULL, *ballongauche, *ballondroite, *ballonbasgauche, *ballonbasdroite;
                SDL_Rect position, position1, position2, position3, position4, positionballongauche, positionballondroite, positionballonbasgauche, positionballonbasdroite;
                TTF_Font *police = NULL;


                SDL_Color rouge = {255, 0, 0}, bleu = {110, 191, 224};

                police = TTF_OpenFont ("arial.ttf", 50);
                texte = TTF_RenderText_Blended (police, "Football", rouge);
                police = TTF_OpenFont ("arial.ttf", 25);
                texte1 = TTF_RenderText_Blended (police, "1)Jouer", bleu);
                texte2 = TTF_RenderText_Blended (police, "2)Charger", bleu);
                texte3 = TTF_RenderText_Blended (police, "3)Options", bleu);
                texte4 = TTF_RenderText_Blended (police, "4)Credits", bleu);


                position.x = 300;
                position.y = 0;
                position1.x = 300;
                position1.y = 200;
                position2.x = 300;
                position2.y = 250;
                position3.x = 300;
                position3.y = 300;
                position4.x = 300;
                position4.y = 350;

                positionballongauche.x = 5;
                positionballongauche.y = 5;
                positionballondroite.x = 763;
                positionballondroite.y = 5;
                positionballonbasgauche.x = 5;
                positionballonbasgauche.y = 563;
                positionballonbasdroite.x = 763;
                positionballonbasdroite.y = 563;

                ballongauche = SDL_LoadBMP ("iconeballon.bmp");
                ballondroite = SDL_LoadBMP ("iconeballon.bmp");
                ballonbasgauche = SDL_LoadBMP ("iconeballon.bmp");
                ballonbasdroite = SDL_LoadBMP ("iconeballon.bmp");

                SDL_SetColorKey(ballongauche, SDL_SRCCOLORKEY, SDL_MapRGB(ballongauche->format, 0, 0, 0));
                SDL_SetColorKey(ballondroite, SDL_SRCCOLORKEY, SDL_MapRGB(ballondroite->format, 0, 0, 0));
                SDL_SetColorKey(ballonbasgauche, SDL_SRCCOLORKEY, SDL_MapRGB(ballonbasgauche->format, 0, 0, 0));
                SDL_SetColorKey(ballonbasdroite, SDL_SRCCOLORKEY, SDL_MapRGB(ballonbasdroite->format, 0, 0, 0));

                SDL_BlitSurface(texte, NULL, ecran, &position); /* Blit du texte par-dessus */
                SDL_BlitSurface(texte1, NULL, ecran, &position1);
                SDL_BlitSurface(texte2, NULL, ecran, &position2);
                SDL_BlitSurface(texte3, NULL, ecran, &position3);
                SDL_BlitSurface(texte4, NULL, ecran, &position4);
                SDL_BlitSurface(ballongauche, NULL, ecran, &positionballongauche);
                SDL_BlitSurface(ballondroite, NULL, ecran, &positionballondroite);
                SDL_BlitSurface(ballonbasgauche, NULL, ecran, &positionballonbasgauche);
                SDL_BlitSurface(ballonbasdroite, NULL, ecran, &positionballonbasdroite);

                SDL_Flip(ecran);
                TTF_CloseFont(police);

                SDL_FreeSurface(texte);
                SDL_FreeSurface(texte1);
                SDL_FreeSurface(texte2);
                SDL_FreeSurface(texte3);
                SDL_FreeSurface(texte4);
                SDL_FreeSurface(ballongauche);
                SDL_FreeSurface(ballondroite);
                SDL_FreeSurface(ballonbasgauche);
                SDL_FreeSurface(ballonbasdroite);

                gestionclavier ();

                SDL_Quit( );// ARRET DE LA SDL
                TTF_Quit();// ARRET DE TTF
                return 0;
                }
                • Partager sur Facebook
                • Partager sur Twitter
                  30 juillet 2007 à 14:54:02

                  Et tu veux que ça fasse quoi si tu n'appelles pas la fonction jouer?
                  • Partager sur Facebook
                  • Partager sur Twitter
                    30 juillet 2007 à 15:50:15

                    Enfin!! je vais pouvoir te laisser tranquille :p
                    Je tiens à te remercier pour m'avoir aider et à avoir gaspiller ton temps pour un newbie impolie comme moi :-° , effectivement il n'y avait que cette fonction d'oublier.
                    Merci aussi à magnanime :)
                    • Partager sur Facebook
                    • Partager sur Twitter

                    [SDL]deplacer objet sous opengl

                    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
                    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
                    • Editeur
                    • Markdown