Partage
  • Partager sur Facebook
  • Partager sur Twitter

Probléme de textures

Sujet résolu
    11 juillet 2007 à 17:40:24

    Bonjour à tous,
    Voila je suis débutant en C++ et j'ai fait un petit jeu en 3D à l'aide du tuto Opengl.
    Le jeu fonctionné bien donc j'ai voulu passer aux textures et la ca compile plus, je comprend pas j'ai pourtant me semble t-il suivi le tuto à la lettre :
    fonctions.cpp:146: error: `vert' undeclared (first use this function)
    fonctions.cpp:151: error: `bleu' undeclared (first use this function)
    fonctions.cpp:156: error: `blanc' undeclared (first use this function)
    fonctions.cpp:161: error: `jaune' undeclared (first use this function)
    fonctions.cpp:166: error: `orange' undeclared (first use this function)
    Process terminated with status 1 (0 minutes, 0 seconds)


    voila le code :
    main.cpp

    #include <GL/glu.h>
    #include <cstdlib>
    #include <math.h>
    #include "fonctions.h"
    #include "sdlglutils.h" //cf le tuto Opengl


    GLuint blanc;
    GLuint jaune;
    GLuint orange;
    GLuint rouge;
    GLuint bleu;
    GLuint vert;

    int main(int argc, char *argv[])
    {
        SDL_Init(SDL_INIT_VIDEO);
        atexit(SDL_Quit);
        SDL_WM_SetCaption("Jeu en 3D",NULL);SetVideoMode(640, 480, 32, SDL_OPENGL);
        glMatrixMode( GL_PROJECTION );
        glLoadIdentity();
        gluPerspective(65,(double)640/480,1,1000);

        glEnable(GL_DEPTH_TEST);
        glEnable(GL_TEXTURE_2D);
        blanc = loadTexture("blanc.jpg");
        jaune = loadTexture("jaune.jpg");
        orange = loadTexture("orange.jpg");
        rouge = loadTexture("rouge.jpg");
        bleu = loadTexture("bleu.jpg");
        vert = loadTexture("vert.jpg");

    etc... boucle principale appelant des fonctions de fonctions.cpp et sdlglutils.cpp
    }

    fonctions.cpp qui comprend différentes fonctions dont :
    #include <SDL/SDL.h>
    #include <GL/gl.h>
    #include <GL/glu.h>
    #include <cstdlib>
    #include <math.h>
    #include "fonctions.h"
    #include <time.h>
    #include "sdlglutils.h"

    void couleur(int nb)
    {
        switch(nb)
        {
            case 1:
                glBindTexture(GL_TEXTURE_2D, rouge);
            break;

            case 2:
                glBindTexture(GL_TEXTURE_2D, vert);
            break;

            case 3:
                glBindTexture(GL_TEXTURE_2D, bleu);
            break;

            case 4:
                glBindTexture(GL_TEXTURE_2D, blanc);
            break;

            case 5:
                glBindTexture(GL_TEXTURE_2D, jaune);
            break;

            case 6:
                glBindTexture(GL_TEXTURE_2D, orange);
            break;
        }
    }
    et celle qui est trés souvant appelée et qui affiche un carré avec une texture de la couleur souhaité

    int affcarre(int coordonnees[3], int nbcouleur)
    {
            etc ... code basic

            glBegin(GL_QUADS);
            couleur(nbcouleur);
            glTexCoord2d(0,1);  glVertex3d(coord[0] + vect[0][0],coord[1] + vect[0][1] + vect[1][1],coord[2] + vect[1][2]);
            glTexCoord2d(0,0);  glVertex3d(coord[0] + vect[0][0],coord[1] + vect[0][1] - vect[1][1],coord[2] - vect[1][2]);
            glTexCoord2d(1,0);  glVertex3d(coord[0] - vect[0][0],coord[1] - vect[0][1] - vect[1][1],coord[2] - vect[1][2]);
            glTexCoord2d(1,1);  glVertex3d(coord[0] - vect[0][0],coord[1] - vect[0][1] + vect[1][1],coord[2] + vect[1][2]);
            glEnd();

        return 0;
    }

    bon si quelqu'un voit la solution
    Merci d'avance
    • Partager sur Facebook
    • Partager sur Twitter
      11 juillet 2007 à 17:45:50

      Tu as oublié d'inclure gl.h (et note aussi qu'il doit être inclu avant glu.h).
      • Partager sur Facebook
      • Partager sur Twitter
        11 juillet 2007 à 17:56:51

        désolé j'ai oublié de le mettre dans le message mais je l'avez bien tapé dans mon main.
        D'ailleur j'ai aussi oublié :
        #include <SDL/SDL.h>
        • Partager sur Facebook
        • Partager sur Twitter
          11 juillet 2007 à 18:07:24

          Alors merci de nous fournir un code complet et à jour. Fais un copier/coller.
          Indiques-nous également toutes les erreurs que te donne ton compilateur.
          • Partager sur Facebook
          • Partager sur Twitter
            11 juillet 2007 à 18:29:04

            Hello

            Déjà pour commencer, une remarque: JAMAIS DE VARIABLES GLOBALES !!!!, on peut toujours s'en sortir autrement.

            Sinon pour ton problème. Tu déclares tes variables globales dans le fichier main.cpp. Le fichier fonctions.cpp et la fonction void couleur(int) en particulier ne connaissent pas l'existence de ces variables puisqu'elles ne sont pas déclarées dans ce fichier.

            Pour résoudre ce problème, il y a 3 solutions:

            1) Supprimer les variables globales (C'est la meilleure des 3 mais ça demande pas mal de modifications)

            2) Mettre la déclaration de tes variables dans un fichier .h séparé que tu inclurais dans tous les autres .cpp

            3) Ajouter les lignes suivantes dans fichiers.cpp:

            extern GLuint jaune;
            extern GLuint rouge;
            //...
             


            qui indiquent au compilateur que les variables sont dans un autre fichier.

            Voilà
            • Partager sur Facebook
            • Partager sur Twitter
            Co-auteur du cours de C++. ||| Posez vos questions sur le forum ||| Me contacter.
              11 juillet 2007 à 19:18:14

              J'avais le même problème un moment. Les variables ne s'exportaient pas d'un fichier à l'autre. J'ai procédé autrement. J'ai déclaré les variables dans la fonction main puis j'ai attribué à chacune un pointeur que j'exporte dans ma fonction de dessin comme paramètre.
              • Partager sur Facebook
              • Partager sur Twitter
                11 juillet 2007 à 19:25:12

                Pour les erreurs j'ai seuleument

                fonctions.cpp:146: error: `vert' undeclared (first use this function)
                fonctions.cpp:151: error: `bleu' undeclared (first use this function)
                fonctions.cpp:156: error: `blanc' undeclared (first use this function)
                fonctions.cpp:161: error: `jaune' undeclared (first use this function)
                fonctions.cpp:166: error: `orange' undeclared (first use this function)
                Process terminated with status 1 (0 minutes, 0 seconds)
                ce qui correspond à l'appel de la fonction glBindTexture dans la fonction couleur de fonctions.cpp
                J'ai donc modifié en utilisant la 3eme solution (pas la meilleur je c'est mis la plus rapide)
                Etj'ai maintenant cette erreur
                .objs\sdlglutils.o:sdlglutils.cpp:(.text+0x2b): undefined reference to `IMG_Load'
                .objs\sdlglutils.o:sdlglutils.cpp:(.text+0x6f7): undefined reference to `IMG_Load'
                collect2: ld returned 1 exit status
                Process terminated with status 1 (0 minutes, 0 seconds)
                0 errors, 0 warnings


                et voici les pages de codes complettes

                main.cpp
                #include <SDL/SDL.h>
                #include <GL/gl.h>
                #include <GL/glu.h>
                #include <cstdlib>
                #include <math.h>
                #include "fonctions.h"
                #include "sdlglutils.h"


                GLuint blanc;
                GLuint jaune;
                GLuint orange;
                GLuint rouge;
                GLuint bleu;
                GLuint vert;

                int main(int argc, char *argv[])
                {
                    SDL_Init(SDL_INIT_VIDEO);
                    atexit(SDL_Quit);
                    SDL_WM_SetCaption("jeu 3D",NULL);
                    SDL_SetVideoMode(640, 480, 32, SDL_OPENGL);
                    glMatrixMode( GL_PROJECTION );
                    glLoadIdentity();
                    gluPerspective(65,(double)640/480,1,1000);

                    glEnable(GL_DEPTH_TEST);


                    glEnable(GL_TEXTURE_2D);
                    blanc = loadTexture("blanc.jpg");
                    jaune = loadTexture("jaune.jpg");
                    orange = loadTexture("orange.jpg");
                    rouge = loadTexture("rouge.jpg");
                    bleu = loadTexture("bleu.jpg");
                    vert = loadTexture("vert.jpg");


                    bool continuer = true;
                    int init = 0 ;




                    int rumicube[5][5][5];
                    initialisation( rumicube );
                    afficher(rumicube);
                    melange(rumicube);



                    SDL_Event event;
                    SDL_EnableKeyRepeat(500, 10);//permet de pouvoir détecter le maintien de la touche enfoncée

                    while (continuer)
                    {
                        SDL_WaitEvent(&event);
                        switch(event.type)
                        {
                            case SDL_QUIT:
                                continuer = false;
                                break;
                            case SDL_KEYDOWN:
                                switch(event.key.keysym.sym)
                                {
                                    case SDLK_RIGHT://Si on appuie sur la fléche de droite
                                        affrotation( rumicube, 0, 0, 1, 0, 2);
                                        rotmatrice( rumicube, 0, 0, 1, 0, 2);
                                        break;
                                    case SDLK_LEFT://Si on appuie sur la fléche de gauche
                                        affrotation( rumicube, 0, 0, 1, 1, 2);
                                        rotmatrice( rumicube, 0, 0, 1, 1, 2);
                                        break;
                                    case SDLK_UP://Si on appuie sur la fléche du haut
                                        affrotation( rumicube, 0, 1, 0, 0, 2);
                                        rotmatrice( rumicube, 0, 1, 0, 0, 2);
                                        break;
                                    case SDLK_DOWN://Si on appuie sur la fléche du bas
                                        affrotation( rumicube, 0, 1, 0, 1, 2);
                                        rotmatrice( rumicube, 0, 1, 0, 1, 2);
                                        break;
                                }
                                break;
                            case SDL_MOUSEBUTTONUP:
                                initialisation( rumicube );
                                afficher(rumicube);
                            break;

                        }

                      afficher(rumicube);



                    }

                    SDL_Quit();

                    return 0;
                }

                fonctions.h
                #include <SDL/SDL.h>

                int initialisation(int rumicube[5][5][5]);
                int melange(int rumicube[5][5][5]);
                int afficher(int rumicube[5][5][5]);
                int rotmatrice(int rumicube[5][5][5], bool axeX, bool axeY, bool axeZ, int sens, int partie);
                int affrotation(int rumicube[5][5][5], bool axeX, bool axeY, bool axeZ, int sens, int partie);
                int matricecoin(int rumicube[5][5][5], int centre[3], int v[2][3]);
                int matricecouronne(int rumicube[5][5][5], int centre[3], int v[2][3]);
                int affcarre(int coord[3], int nbcouleur);
                void couleur(int nb);
                void fond();


                fonction.cpp
                #include <SDL/SDL.h>
                #include <GL/gl.h>
                #include <GL/glu.h>
                #include <cstdlib>
                #include <math.h>
                #include "fonctions.h"
                #include <time.h>
                #include "sdlglutils.h"


                int initialisation(int rumicube[5][5][5])
                {
                    for (int i = 0 ; i < 5 ; i++ ) //initialisation (tout a 0)
                    {
                        for (int j = 0 ; j < 5 ; j++ )
                        {
                            for (int k = 0 ; k < 5 ; k++ )
                            {
                                rumicube[i][j][k] = 0;
                            }
                        }
                    }
                    for (int j = 1 ; j < 4 ; j++ ) //initialisation suite le met par dessus le reste
                    {
                        for (int k = 1 ; k < 4 ; k++ )
                        {
                            rumicube[0][j][k] = 1;
                            rumicube[4][j][k] = 3;
                            rumicube[j][0][k] = 4;
                            rumicube[j][4][k] = 2;
                            rumicube[j][k][0] = 5;
                            rumicube[j][k][4] = 6;
                        }
                    }
                    return 0;
                }

                int melange(int rumicube[5][5][5])
                {
                    srand(time(NULL));
                    int axe[3];
                    for (int t = 0; t < 5; t++) //20 normalement
                    {
                        axe[0] = 0;
                        axe[1] = 0;
                        axe[2] = 0;
                        axe[rand()%3] = 1;
                        bool sens = rand()%2;
                        int partie = rand()%3;
                        affrotation(rumicube, axe[0], axe[1], axe[2], sens, partie);
                        rotmatrice(rumicube, axe[0], axe[1], axe[2], sens, partie);
                    }
                }

                int  afficher(int rumicube[5][5][5])
                {

                    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
                    glMatrixMode( GL_MODELVIEW );
                    glLoadIdentity( );
                    gluLookAt(5,7,5,0,0,0,0,0,1);

                    fond();

                    int coordonnees[3];
                    for (int i = 0 ; i < 5 ; i++ )
                    {
                        for (int j = 0 ; j < 5 ; j++ )
                        {
                            for (int k = 0 ; k < 5 ; k++ )
                            {
                                coordonnees[0] = i;
                                coordonnees[1] = j;
                                coordonnees[2] = k;
                                if (rumicube[i][j][k]!=0)
                                {
                                    affcarre(coordonnees, rumicube[i][j][k]);
                                }
                            }
                        }
                    }


                    glFlush();
                    SDL_GL_SwapBuffers();
                    return 0;
                }

                void couleur(int nb)
                {
                    //glEnable(GL_TEXTURE_2D);
                    switch(nb)
                    {
                        case 1:
                            //glColor3ub(255,0,0); //face rouge
                            glBindTexture(GL_TEXTURE_2D, rouge);
                        break;

                        case 2:
                            //glColor3ub(0,255,0); //face verte
                            glBindTexture(GL_TEXTURE_2D, vert);
                        break;

                        case 3:
                            //glColor3ub(0,0,255); //face bleu
                            glBindTexture(GL_TEXTURE_2D, bleu);
                        break;

                        case 4:
                            //glColor3ub(255,255,255); //face blanche
                            glBindTexture(GL_TEXTURE_2D, blanc);
                        break;

                        case 5:
                            //glColor3ub(255,255,0); //face jaune
                            glBindTexture(GL_TEXTURE_2D, jaune);
                        break;

                        case 6:
                            //glColor3ub(255,128,0); //face orange
                            glBindTexture(GL_TEXTURE_2D, orange);
                        break;
                    }
                }

                int rotmatrice(int rumicube[5][5][5], bool axeX, bool axeY, bool axeZ, int sens, int partie)//partie = 0, 1 ou 2.
                {
                    int centre[3], v[2][3];
                    centre[0] = 2*partie*axeX + 2*( 1 - axeX ) ;
                    centre[1] = 2*partie*axeY + 2*( 1 - axeY ) ;
                    centre[2] = 2*partie*axeZ + 2*( 1 - axeZ ) ;
                    v[sens][0] = 1 - axeX;
                    v[sens][1] = 1 - axeY;
                    v[sens][2] = 0;
                    if (axeZ)
                    {
                        v[sens][1] = 0;
                    }
                    if (axeY)
                    {
                        v[sens][0] = 0;
                        v[sens][2] = 1;
                    }
                    v[1-sens][0] = 1 - axeX - v[sens][0];
                    v[1-sens][1] = 1 - axeY - v[sens][1];
                    v[1-sens][2] = 1 - axeZ - v[sens][2];
                    if (axeX)
                    {
                        v[1-sens][1] = 0;
                    }
                    //faire coin
                    if (partie!=1)
                    {
                        matricecoin(rumicube,centre,v);
                        matricecoin(rumicube,centre,v);
                    }


                    centre[0] = (partie + 1)*axeX + 2*( 1 - axeX ) ;
                    centre[1] = (partie + 1)*axeY + 2*( 1 - axeY ) ;
                    centre[2] = (partie + 1)*axeZ + 2*( 1 - axeZ ) ;
                    //faire couronne
                    matricecouronne( rumicube, centre, v);
                    matricecouronne( rumicube, centre, v);
                    matricecouronne( rumicube, centre, v);
                    return 0;
                }

                int matricecoin(int rumicube[5][5][5], int centre[3], int v[2][3])
                {
                    int valeur =  rumicube[centre[0] - v[0][0] - v[1][0]][centre[1] - v[0][1] - v[1][1]][centre[2] - v[0][2] - v[1][2]];
                    rumicube[centre[0] - v[0][0] - v[1][0]][centre[1] - v[0][1] - v[1][1]][centre[2] - v[0][2] - v[1][2]] = rumicube[centre[0] - v[0][0]][centre[1] - v[0][1]][centre[2] - v[0][2]];
                    rumicube[centre[0] - v[0][0]][centre[1] - v[0][1]][centre[2] - v[0][2]] = rumicube[centre[0] - v[0][0] + v[1][0]][centre[1] - v[0][1] + v[1][1]][centre[2] - v[0][2] + v[1][2]];
                    rumicube[centre[0] - v[0][0] + v[1][0]][centre[1] - v[0][1] + v[1][1]][centre[2] - v[0][2] + v[1][2]] = rumicube[centre[0] + v[1][0]][centre[1] + v[1][1]][centre[2] + v[1][2]];
                    rumicube[centre[0] + v[1][0]][centre[1] + v[1][1]][centre[2] + v[1][2]] = rumicube[centre[0] + v[0][0] + v[1][0]][centre[1] + v[0][1] + v[1][1]][centre[2] + v[0][2] + v[1][2]];
                    rumicube[centre[0] + v[0][0] + v[1][0]][centre[1] + v[0][1] + v[1][1]][centre[2] + v[0][2] + v[1][2]] = rumicube[centre[0] + v[0][0]][centre[1] + v[0][1]][centre[2] + v[0][2]];
                    rumicube[centre[0] + v[0][0]][centre[1] + v[0][1]][centre[2] + v[0][2]] = rumicube[centre[0] + v[0][0] - v[1][0]][centre[1] + v[0][1] - v[1][1]][centre[2] + v[0][2] - v[1][2]];
                    rumicube[centre[0] + v[0][0] - v[1][0]][centre[1] + v[0][1] - v[1][1]][centre[2] + v[0][2] - v[1][2]] = rumicube[centre[0] - v[1][0]][centre[1] - v[1][1]][centre[2] - v[1][2]];
                    rumicube[centre[0] - v[1][0]][centre[1] - v[1][1]][centre[2] - v[1][2]] = valeur;
                    return 0;
                }

                int matricecouronne(int rumicube[5][5][5], int centre[3], int v[2][3])
                {
                    int valeur = rumicube[centre[0] - 2 * v[0][0] - v[1][0]][centre[1] - 2 * v[0][1] - v[1][1]][centre[2] - 2 * v[0][2] - v[1][2]];
                    rumicube[centre[0] - 2 * v[0][0] - v[1][0]][centre[1] - 2 * v[0][1] - v[1][1]][centre[2] - 2 * v[0][2] - v[1][2]] = rumicube[centre[0] - 2 * v[0][0]][centre[1] - 2 * v[0][1]][centre[2] - 2 * v[0][2]];
                    rumicube[centre[0] - 2 * v[0][0]][centre[1] - 2 * v[0][1]][centre[2] - 2 * v[0][2]] = rumicube[centre[0] - 2 * v[0][0] + v[1][0]][centre[1] - 2 * v[0][1] + v[1][1]][centre[2] - 2 * v[0][2] + v[1][2]];
                    rumicube[centre[0] - 2 * v[0][0] + v[1][0]][centre[1] - 2 * v[0][1] + v[1][1]][centre[2] - 2 * v[0][2] + v[1][2]] = rumicube[centre[0] - v[0][0] + 2 * v[1][0]][centre[1] - v[0][1] + 2 * v[1][1]][centre[2] - v[0][2] + 2 * v[1][2]];
                    rumicube[centre[0] - v[0][0] + 2 * v[1][0]][centre[1] - v[0][1] + 2 * v[1][1]][centre[2] - v[0][2] + 2 * v[1][2]] = rumicube[centre[0] + 2 * v[1][0]][centre[1] + 2 * v[1][1]][centre[2] + 2 * v[1][2]];
                    rumicube[centre[0] + 2 * v[1][0]][centre[1] + 2 * v[1][1]][centre[2] + 2 * v[1][2]] = rumicube[centre[0] + v[0][0] + 2 * v[1][0]][centre[1] + v[0][1] + 2 * v[1][1]][centre[2] + v[0][2] + 2 * v[1][2]];
                    rumicube[centre[0] + v[0][0] + 2 * v[1][0]][centre[1] + v[0][1] + 2 * v[1][1]][centre[2] + v[0][2] + 2 * v[1][2]] = rumicube[centre[0] + 2 * v[0][0] + v[1][0]][centre[1] + 2 * v[0][1] + v[1][1]][centre[2] + 2 * v[0][2] + v[1][2]];
                    rumicube[centre[0] + 2 * v[0][0] + v[1][0]][centre[1] + 2 * v[0][1] + v[1][1]][centre[2] + 2 * v[0][2] + v[1][2]] = rumicube[centre[0] + 2 * v[0][0]][centre[1] + 2 * v[0][1]][centre[2] + 2 * v[0][2]];
                    rumicube[centre[0] + 2 * v[0][0]][centre[1] + 2 * v[0][1]][centre[2] + 2 * v[0][2]] = rumicube[centre[0] + 2 * v[0][0] - v[1][0]][centre[1] + 2 * v[0][1] - v[1][1]][centre[2] + 2 * v[0][2] - v[1][2]];
                    rumicube[centre[0] + 2 * v[0][0] - v[1][0]][centre[1] + 2 * v[0][1] - v[1][1]][centre[2] + 2 * v[0][2] - v[1][2]] = rumicube[centre[0] + v[0][0] - 2 * v[1][0]][centre[1] + v[0][1] - 2 * v[1][1]][centre[2] + v[0][2] - 2 * v[1][2]];
                    rumicube[centre[0] + v[0][0] - 2 * v[1][0]][centre[1] + v[0][1] - 2 * v[1][1]][centre[2] + v[0][2] - 2 * v[1][2]] = rumicube[centre[0] - 2 * v[1][0]][centre[1] - 2 * v[1][1]][centre[2] - 2 * v[1][2]];
                    rumicube[centre[0] - 2 * v[1][0]][centre[1] - 2 * v[1][1]][centre[2] - 2 * v[1][2]] = rumicube[centre[0] - v[0][0] - 2 * v[1][0]][centre[1] - v[0][1] - 2 * v[1][1]][centre[2] - v[0][2] - 2 * v[1][2]];
                    rumicube[centre[0] - v[0][0] - 2 * v[1][0]][centre[1] - v[0][1] - 2 * v[1][1]][centre[2] - v[0][2] - 2 * v[1][2]] = valeur;
                    return 0;
                }

                int affrotation(int rumicube[5][5][5], bool axeX, bool axeY, bool axeZ, int sens, int partie)//partie = 0, 1 ou 2.
                {
                    int ordre[3];
                    ordre[0] = 0;
                    ordre[1] = 1;
                    ordre[2] = 2;
                    if (partie==1)
                    {
                        ordre[1] = 2;
                        ordre[2] = 1;
                    }
                    if (partie==0)
                    {
                        ordre[0] = 2;
                        ordre[2] = 0;
                    }

                    int coordonnees[3], v[2][3];;
                     for (double angle = 0.0 ; angle <= 90 ; angle+=5 )
                    {
                        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
                        glMatrixMode( GL_MODELVIEW );
                        glLoadIdentity( );
                        gluLookAt(5,7,5,0,0,0,0,0,1);
                        fond();
                        for (int l = 0 ; l < 3 ; l++ )
                        {
                            if (l==2)
                            {
                                glRotated(angle*pow(-1,sens),axeX,axeY,axeZ);//faire la rotation

                            }
                            v[0][0] = 1 - axeX;
                            v[0][1] = 1 - axeY;
                            v[0][2] = 0;
                            if (axeZ)
                            {
                                v[0][1] = 0;
                            }
                            v[1][0] = 0;
                            v[1][1] = 1 - axeY - v[0][1];
                            v[1][2] = 1 - axeZ - v[0][2];
                            if (axeX)
                            {
                                v[1][1] = 0;
                            }
                            if (ordre[l]!=0)
                            {
                                glBegin(GL_QUADS);
                                glColor3ub(0,0,0);
                                glVertex3d((2*ordre[l] - 3)*axeX + 3*v[0][0],       (2*ordre[l] - 3)*axeY + 3*v[0][1] + 3*v[1][1],     (2*ordre[l] - 3)*axeZ + 3*v[1][2]);
                                glVertex3d((2*ordre[l] - 3)*axeX + 3*v[0][0],       (2*ordre[l] - 3)*axeY + 3*v[0][1] - 3*v[1][1],     (2*ordre[l] - 3)*axeZ - 3*v[1][2]);
                                glVertex3d((2*ordre[l] - 3)*axeX - 3*v[0][0],       (2*ordre[l] - 3)*axeY - 3*v[0][1] - 3*v[1][1],     (2*ordre[l] - 3)*axeZ - 3*v[1][2]);
                                glVertex3d((2*ordre[l] - 3)*axeX - 3*v[0][0],       (2*ordre[l] - 3)*axeY - 3*v[0][1] + 3*v[1][1],     (2*ordre[l] - 3)*axeZ + 3*v[1][2]);
                                glEnd();
                            }
                            if (ordre[l]!=2)
                            {
                                glBegin(GL_QUADS);
                                glColor3ub(0,0,0);
                                glVertex3d((2*ordre[l] - 1)*axeX + 3*v[0][0],       (2*ordre[l] - 1)*axeY + 3*v[0][1] + 3*v[1][1],     (2*ordre[l] - 1)*axeZ + 3*v[1][2]);
                                glVertex3d((2*ordre[l] - 1)*axeX + 3*v[0][0],       (2*ordre[l] - 1)*axeY + 3*v[0][1] - 3*v[1][1],     (2*ordre[l] - 1)*axeZ - 3*v[1][2]);
                                glVertex3d((2*ordre[l] - 1)*axeX - 3*v[0][0],       (2*ordre[l] - 1)*axeY - 3*v[0][1] - 3*v[1][1],     (2*ordre[l] - 1)*axeZ - 3*v[1][2]);
                                glVertex3d((2*ordre[l] - 1)*axeX - 3*v[0][0],       (2*ordre[l] - 1)*axeY - 3*v[0][1] + 3*v[1][1],     (2*ordre[l] - 1)*axeZ + 3*v[1][2]);
                                glEnd();
                            }
                            for (int i = 0 ; i < 4*(1 - axeX) + 1 ; i++ )
                            {
                                for (int j = 0 ; j < 4*(1 - axeY) + 1 ; j++ )
                                {
                                    for (int k = 0 ; k < 4*(1 - axeZ) + 1 ; k++ )
                                    {
                                        coordonnees[0] = 2*ordre[l]*axeX + i;
                                        coordonnees[1] = 2*ordre[l]*axeY + j;
                                        coordonnees[2] = 2*ordre[l]*axeZ + k;
                                        if (rumicube[coordonnees[0]][coordonnees[1]][coordonnees[2]]!=0)
                                        {
                                            affcarre(coordonnees, rumicube[coordonnees[0]][coordonnees[1]][coordonnees[2]]);
                                        }

                                        coordonnees[0] = (ordre[l] + 1)*axeX + i*(1 - axeX);
                                        coordonnees[1] = (ordre[l] + 1)*axeY + j*(1 - axeY);
                                        coordonnees[2] = (ordre[l] + 1)*axeZ + k*(1 - axeZ);
                                        if (rumicube[coordonnees[0]][coordonnees[1]][coordonnees[2]]!=0)
                                        {
                                            affcarre(coordonnees, rumicube[coordonnees[0]][coordonnees[1]][coordonnees[2]]);
                                        }
                                    }
                                }
                            }


                        }

                        glFlush();
                        SDL_GL_SwapBuffers();
                        SDL_Delay(25);
                    }

                    return 0;
                }

                int affcarre(int coordonnees[3], int nbcouleur)
                {
                    int vect[2][3], coord[3];
                    for (int n = 0 ; n < 3 ; n++ ) //convertion des coordonnées en coordonnées graphiques du centre du carré à afficher
                    {
                        switch(coordonnees[n])
                        {
                            case 0:
                                coord[n] = -3;
                                vect[0][n] = 0;
                                vect[1][n] = 0;
                            break;
                            case 1:
                                coord[n] = -2;
                                vect[0][n] = 1;
                                vect[1][n] = 1;
                            break;
                            case 2:
                                coord[n] = 0;
                                vect[0][n] = 1;
                                vect[1][n] = 1;
                            break;
                            case 3:
                                coord[n] = 2;
                                vect[0][n] = 1;
                                vect[1][n] = 1;
                            break;
                            case 4:
                                coord[n] = 3;
                                vect[0][n] = 0;
                                vect[1][n] = 0;
                            break;
                        }
                    }
                    if (vect[0][2]==0)
                    {
                        vect[0][1] = 0;
                    }
                    vect[0][2] = 0;
                    if (vect[1][0]==0)
                    {
                        vect[1][1] = 0;
                    }
                    vect[1][0] = 0;


                        glBegin(GL_QUADS);
                        couleur(nbcouleur);
                        glTexCoord2d(0,1);  glVertex3d(coord[0] + vect[0][0],coord[1] + vect[0][1] + vect[1][1],coord[2] + vect[1][2]);
                        glTexCoord2d(0,0);  glVertex3d(coord[0] + vect[0][0],coord[1] + vect[0][1] - vect[1][1],coord[2] - vect[1][2]);
                        glTexCoord2d(1,0);  glVertex3d(coord[0] - vect[0][0],coord[1] - vect[0][1] - vect[1][1],coord[2] - vect[1][2]);
                        glTexCoord2d(1,1);  glVertex3d(coord[0] - vect[0][0],coord[1] - vect[0][1] + vect[1][1],coord[2] + vect[1][2]);
                        glEnd();

                    //glDisable(GL_TEXTURE_2D);
                    return 0;
                }

                void fond()
                {
                    glBegin(GL_TRIANGLES);
                    glColor3ub(255,0,0);    glVertex3d(200,-100,-100);
                    glColor3ub(0,255,0);    glVertex3d(-100,100,-100);
                    glColor3ub(0,0,255);    glVertex3d(-100,-100,100);
                    glEnd();

                }


                sdlglutils.h
                #ifndef SDLGLUTILS_H
                #define SDLGLUTILS_H

                #include <GL/gl.h>
                #include <SDL/SDL.h>

                #ifndef GL_CLAMP_TO_EDGE
                #define GL_CLAMP_TO_EDGE 0x812F
                #endif

                GLuint loadTexture(const char * filename,bool useMipMap = true);
                int takeScreenshot(const char * filename);
                void drawAxis(double scale = 1);
                int initFullScreen(unsigned int * width = NULL,unsigned int * height = NULL);
                int XPMFromImage(const char * imagefile, const char * XPMfile);
                SDL_Cursor * cursorFromXPM(const char * xpm[]);

                #endif //SDLGLUTILS_H
                 


                sdlglutils.cpp
                #include "sdlglutils.h"
                #include <SDL/SDL.h>
                #include <SDL/SDL_image.h>
                #include <GL/glu.h>

                #include <cstring>
                #include <cstdlib>

                SDL_Surface * flipSurface(SDL_Surface * surface);

                GLuint loadTexture(const char * filename,bool useMipMap)
                {
                    GLuint glID;
                    SDL_Surface * picture_surface = NULL;
                    SDL_Surface *gl_surface = NULL;
                    SDL_Surface * gl_fliped_surface = NULL;
                    Uint32 rmask, gmask, bmask, amask;

                    picture_surface = IMG_Load(filename);
                    if (picture_surface == NULL)
                        return 0;

                #if SDL_BYTEORDER == SDL_BIG_ENDIAN

                    rmask = 0xff000000;
                    gmask = 0x00ff0000;
                    bmask = 0x0000ff00;
                    amask = 0x000000ff;
                #else

                    rmask = 0x000000ff;
                    gmask = 0x0000ff00;
                    bmask = 0x00ff0000;
                    amask = 0xff000000;
                #endif

                    SDL_PixelFormat format = *(picture_surface->format);
                    format.BitsPerPixel = 32;
                    format.BytesPerPixel = 4;
                    format.Rmask = rmask;
                    format.Gmask = gmask;
                    format.Bmask = bmask;
                    format.Amask = amask;

                    gl_surface = SDL_ConvertSurface(picture_surface,&format,SDL_SWSURFACE);

                    gl_fliped_surface = flipSurface(gl_surface);

                    glGenTextures(1, &glID);

                    glBindTexture(GL_TEXTURE_2D, glID);


                    if (useMipMap)
                    {

                        gluBuild2DMipmaps(GL_TEXTURE_2D, 4, gl_fliped_surface->w,
                                          gl_fliped_surface->h, GL_RGBA,GL_UNSIGNED_BYTE,
                                          gl_fliped_surface->pixels);

                        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,
                                        GL_LINEAR_MIPMAP_LINEAR);

                    }
                    else
                    {
                        glTexImage2D(GL_TEXTURE_2D, 0, 4, gl_fliped_surface->w,
                                     gl_fliped_surface->h, 0, GL_RGBA,GL_UNSIGNED_BYTE,
                                     gl_fliped_surface->pixels);
                        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
                    }
                    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);


                    SDL_FreeSurface(gl_fliped_surface);
                    SDL_FreeSurface(gl_surface);
                    SDL_FreeSurface(picture_surface);

                    return glID;
                }

                int takeScreenshot(const char * filename)
                {
                    GLint viewport[4];
                    Uint32 rmask, gmask, bmask, amask;
                    SDL_Surface * picture, * finalpicture;

                    glGetIntegerv(GL_VIEWPORT, viewport);

                #if SDL_BYTEORDER == SDL_BIG_ENDIAN

                    rmask = 0xff000000;
                    gmask = 0x00ff0000;
                    bmask = 0x0000ff00;
                    amask = 0x000000ff;
                #else

                    rmask = 0x000000ff;
                    gmask = 0x0000ff00;
                    bmask = 0x00ff0000;
                    amask = 0xff000000;
                #endif

                    picture = SDL_CreateRGBSurface(SDL_SWSURFACE,viewport[2],viewport[3], 32,
                                                   rmask, gmask, bmask, amask);
                    SDL_LockSurface(picture);
                    glReadPixels(viewport[0],viewport[1],viewport[2],viewport[3],GL_RGBA,
                                 GL_UNSIGNED_BYTE,picture->pixels);
                    SDL_UnlockSurface(picture);

                    finalpicture = flipSurface(picture);

                    if (SDL_SaveBMP(finalpicture, filename))
                    {
                        return -1;
                    }
                    SDL_FreeSurface(finalpicture);
                    SDL_FreeSurface(picture);

                    return 0;
                }

                SDL_Surface * flipSurface(SDL_Surface * surface)
                {
                    int current_line,pitch;
                    SDL_Surface * fliped_surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
                                                   surface->w,surface->h,
                                                   surface->format->BitsPerPixel,
                                                   surface->format->Rmask,
                                                   surface->format->Gmask,
                                                   surface->format->Bmask,
                                                   surface->format->Amask);



                    SDL_LockSurface(surface);
                    SDL_LockSurface(fliped_surface);

                    pitch = surface->pitch;
                    for (current_line = 0; current_line < surface->h; current_line ++)
                    {
                        memcpy(&((unsigned char* )fliped_surface->pixels)[current_line*pitch],
                               &((unsigned char* )surface->pixels)[(surface->h - 1  -
                                                                    current_line)*pitch],
                               pitch);
                    }

                    SDL_UnlockSurface(fliped_surface);
                    SDL_UnlockSurface(surface);
                    return fliped_surface;
                }

                void drawAxis(double scale)
                {
                    glPushAttrib(GL_ALL_ATTRIB_BITS);
                    glPushMatrix();
                    glDisable(GL_LIGHTING);
                    glDisable(GL_TEXTURE_2D);
                    glEnable(GL_BLEND);
                    glEnable(GL_LINE_SMOOTH);
                    glLineWidth(2);
                    glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
                    glScaled(scale,scale,scale);
                    glBegin(GL_LINES);
                    glColor3ub(255,0,0);
                    glVertex3i(0,0,0);
                    glVertex3i(1,0,0);
                    glColor3ub(0,255,0);
                    glVertex3i(0,0,0);
                    glVertex3i(0,1,0);
                    glColor3ub(0,0,255);
                    glVertex3i(0,0,0);
                    glVertex3i(0,0,1);
                    glEnd();
                    glPopMatrix();
                    glPopAttrib();
                }

                int initFullScreen(unsigned int * width,unsigned int * height)
                {
                    SDL_Rect ** modes;

                    modes = SDL_ListModes(NULL,SDL_FULLSCREEN|SDL_OPENGL);
                    if ((modes == (SDL_Rect **)0)||(modes == (SDL_Rect **)-1))
                        return 0;

                    if (width != NULL)
                        *width = modes[0]->w;
                    if (height != NULL)
                        *height = modes[0]->h;
                    if (SDL_SetVideoMode(modes[0]->w,
                                         modes[0]->h,
                                         SDL_GetVideoInfo()->vfmt->BitsPerPixel,
                                         SDL_FULLSCREEN|SDL_OPENGL) == NULL)
                        return -1;
                    else
                    {
                        return 0;
                    }
                }

                int XPMFromImage(const char * imagefile, const char * XPMfile)
                {
                    SDL_Surface * image,*image32bits;
                    FILE * xpm;
                    Uint32 pixel;
                    Uint8 r,g,b,a;
                    int x,y;
                    unsigned int w;
                    char * xpm_name;
                    Uint32 rmask, gmask, bmask, amask;

                    image = IMG_Load(imagefile);
                    if (image == NULL)
                        return -1;

                    #if SDL_BYTEORDER == SDL_BIG_ENDIAN

                    rmask = 0xff000000;
                    gmask = 0x00ff0000;
                    bmask = 0x0000ff00;
                    amask = 0x000000ff;
                #else

                    rmask = 0x000000ff;
                    gmask = 0x0000ff00;
                    bmask = 0x00ff0000;
                    amask = 0xff000000;
                #endif

                    image32bits = SDL_CreateRGBSurface(SDL_SWSURFACE,
                                                      image->w,image->h,
                                                      32,rmask, gmask, bmask, amask);

                    SDL_BlitSurface(image,NULL,image32bits,NULL);
                    SDL_FreeSurface(image);

                    xpm = fopen(XPMfile,"w");

                    xpm_name = (char*)malloc(strlen(imagefile)*sizeof(char));
                    strcpy(xpm_name,imagefile);
                    if (strchr(xpm_name,'.') != NULL)
                        *strchr(xpm_name,'.') = '\0';
                    fprintf(xpm,"const char *%s[] =\n",xpm_name);
                    free(xpm_name);

                    fprintf(xpm,"\t{\n");
                    fprintf(xpm,"\t\t/* width height num_colors chars_per_pixel */\n");
                    w = ((image->w%8) == 0)?image32bits->w:8*(image32bits->w/8+1);

                    fprintf(xpm,"\t\t\" %d %d 3 1 \",\n",w,image32bits->h);
                    fprintf(xpm,"\t\t/* colors */\n");
                    fprintf(xpm,"\t\t\"X c #000000\",\n");
                    fprintf(xpm,"\t\t\". c #ffffff\",\n");
                    fprintf(xpm,"\t\t\"  c None\",\n");
                    fprintf(xpm,"\t\t/* pixels */\n");

                    SDL_LockSurface(image32bits);

                    for (y = 0; y < image32bits->h; y ++)
                    {
                        fprintf(xpm,"\t\t\"");
                        for (x = 0; x < image32bits->w ; x ++)
                        {
                            pixel = ((Uint32*)image32bits->pixels)[x+y*image32bits->pitch/4];
                            SDL_GetRGBA(pixel,image32bits->format,&r,&g,&b,&a);
                            if (a < 128)
                                fprintf(xpm," ");
                            else if ((r >= 128)||(g >= 128)||(b >= 128))
                                fprintf(xpm,".");
                            else
                                fprintf(xpm,"X");
                        }
                        for (x = image32bits->w ; x < w ; x ++)
                            fprintf(xpm," ");
                        fprintf(xpm,"\",\n");
                    }

                    SDL_UnlockSurface(image32bits);
                    SDL_FreeSurface(image32bits);
                    fprintf(xpm,"\t\t\"0,0\"\n");
                    fprintf(xpm,"\t};\n");
                    return 0;
                }

                SDL_Cursor * cursorFromXPM(const char * xpm[])
                {
                    int i, row, col;
                    int width, height;
                    Uint8 * data;
                    Uint8 * mask;
                    int hot_x, hot_y;
                    SDL_Cursor * cursor = NULL;

                    sscanf(xpm[0], "%d %d", &width, &height);
                    data = (Uint8*)calloc(width/8*height,sizeof(Uint8));
                    mask = (Uint8*)calloc(width/8*height,sizeof(Uint8));

                    i = -1;
                    for ( row=0; row<height; ++row )
                    {
                        for ( col=0; col<width; ++col )
                        {
                            if ( col % 8 )
                            {
                                data[i] <<= 1;
                                mask[i] <<= 1;
                            }
                            else
                            {
                                ++i;
                                data[i] = mask[i] = 0;
                            }
                            switch (xpm[4+row][col])
                            {
                                case 'X':
                                data[i] |= 0x01;
                                mask[i] |= 0x01;
                                break;
                                case '.':
                                mask[i] |= 0x01;
                                break;
                                case ' ':
                                break;
                            }
                        }
                    }
                    sscanf(xpm[4+row], "%d,%d", &hot_x, &hot_y);
                    cursor = SDL_CreateCursor(data, mask, width, height, hot_x, hot_y);
                    free(data);
                    free(mask);
                    return cursor;
                }

                :-° voila
                • Partager sur Facebook
                • Partager sur Twitter
                  11 juillet 2007 à 19:30:49

                  Tu as oublié de linker la bibliothèque SDL_Image avec ton projet.

                  Regarde dans le tuto de Matteo, partie SDL pour voir comment faire.
                  • Partager sur Facebook
                  • Partager sur Twitter
                  Co-auteur du cours de C++. ||| Posez vos questions sur le forum ||| Me contacter.
                    11 juillet 2007 à 19:48:26

                    Merci beaucoup ca y est le probléme est résolue (quelques fichiers à rajouter dans le dossier)
                    C'est quand même rajant ce genre d'erreur, je suis vraiment un newbe.
                    Sinon j'ai vu qu'ils y avaient des petites erreur de textures mais c'était simple avec le tuto et ca marche !!!! :p
                    • Partager sur Facebook
                    • Partager sur Twitter

                    Probléme de textures

                    × 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