IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

OpenGL Discussion :

[libjpeg && opengl] problème "multi-texturing"


Sujet :

OpenGL

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Février 2009
    Messages
    13
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2009
    Messages : 13
    Points : 12
    Points
    12
    Par défaut [libjpeg && opengl] problème "multi-texturing"
    Bonsoir tout le monde;

    Voilà j'ai un problème très urgent car je n'arrive pas à utiliser le "multi texturing", je m'explique: je souhaite en fait utiliser plusieurs images comme textures pour mes diverses faces avec opengl. Pour cela j'utilise la librairie jpeg avec un sample que j'ai trouvé sur le net tout compile bien mais le problème est que l'image "plaqué" sur ma face ne correspond pas à l'image que je souhaite plaquer :S :S :S s'il vous plait pouvez-vous m'aider :'( merci bien

    Sinon voici mes quelques fonctions:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
     
    int texture[20];//tableau pour le "multi-texturing"
    /* OpenGL texture info */
    typedef struct {
         GLsizei width;
         GLsizei height;
     
         GLenum format;
         GLint internalFormat;
         GLuint id;
     
         GLubyte *texels;
    } 
    gl_texture_t;
     
    //sample pour charger les textures images en .jpg
    gl_texture_t*ReadJPEGFromFile (const char *filename) {
      gl_texture_t *texinfo = NULL;
      FILE *fp = NULL;
      struct jpeg_decompress_struct cinfo;
      struct jpeg_error_mgr jerr;
      JSAMPROW j;
      int i;
     
      /* open image file */
      fp = fopen (filename, "rb");
      if (!fp)
        {
          fprintf (stderr, "error: couldn't open \"%s\"!\n", filename);
          return NULL;
        }
     
      /* create and configure decompressor */
      jpeg_create_decompress (&cinfo);
      cinfo.err = jpeg_std_error (&jerr);
      jpeg_stdio_src (&cinfo, fp);
     
      /*
       * NOTE: this is the simplest "readJpegFile" function. There
       * is no advanced error handling.  It would be a good idea to
       * setup an error manager with a setjmp/longjmp mechanism.
       * In this function, if an error occurs during reading the JPEG
       * file, the libjpeg abords the program.
       * See jpeg_mem.c (or RTFM) for an advanced error handling which
       * prevent this kind of behavior (http://tfc.duke.free.fr)
       */
     
      /* read header and prepare for decompression */
      jpeg_read_header (&cinfo, TRUE);
      jpeg_start_decompress (&cinfo);
     
      /* initialize image's member variables */
      texinfo = (gl_texture_t *)malloc (sizeof (gl_texture_t));
      texinfo->width = cinfo.image_width;
      texinfo->height = cinfo.image_height;
      texinfo->internalFormat = cinfo.num_components;
     
      if (cinfo.num_components == 1)
        texinfo->format = GL_LUMINANCE;
      else
        texinfo->format = GL_RGB;
     
      texinfo->texels = (GLubyte *)malloc (sizeof (GLubyte) * texinfo->width
    			       * texinfo->height * texinfo->internalFormat);
     
      /* extract each scanline of the image */
      for (i = 0; i < texinfo->height; ++i)
        {
          j = (texinfo->texels +
    	((texinfo->height - (i + 1)) * texinfo->width * texinfo->internalFormat));
          jpeg_read_scanlines (&cinfo, &j, 1);
        }
     
      /* finish decompression and release memory */
      jpeg_finish_decompress (&cinfo);
      jpeg_destroy_decompress (&cinfo);
     
      fclose (fp);
      return texinfo;
    }
     
     
    GLuint loadJPEGTexture (const char *filename) {
         gl_texture_t *jpeg_tex = NULL;
         GLuint tex_id = 0;
     
         jpeg_tex = ReadJPEGFromFile(filename);
     
         if (jpeg_tex && jpeg_tex->texels){
          /* generate texture */
          glGenTextures (1, &jpeg_tex->id);
          glBindTexture (GL_TEXTURE_2D, jpeg_tex->id);
     
          /* setup some parameters for texture filters and mipmapping */
          glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
          glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     
     
                   /* glTexImage2D (GL_TEXTURE_2D, 0, jpeg_tex->internalFormat,
    		    jpeg_tex->width, jpeg_tex->height, 0, jpeg_tex->format,
    		    GL_UNSIGNED_BYTE, jpeg_tex->texels);*/
     
     
          gluBuild2DMipmaps (GL_TEXTURE_2D, jpeg_tex->internalFormat, jpeg_tex->width, jpeg_tex->height, jpeg_tex->format, GL_UNSIGNED_BYTE, jpeg_tex->texels);
     
          tex_id = jpeg_tex->id;
          /* OpenGL has its own copy of texture data */
          free (jpeg_tex->texels);
          free (jpeg_tex);
        }
     
      return tex_id;
    }
     
     
     
    int main(int argc, char **argv){
     
    	glutInit(&argc, argv);
    	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);
    	glutInitWindowSize(480, 272);
    	glutInitWindowPosition(0, 0);
    	window = glutCreateWindow("pspflashsystem");
     
    	loadobj(&model, "cube.obj", 1);
    	loadobj(&models, "cubes.obj", 0);
        texture[0] = loadJPEGTexture("./data/cube.jpg");
    	texture[1] = loadJPEGTexture("./data/cube2.jpg");
     
    	initGL(480.0f, 272.0f);
     
    	glutDisplayFunc(Display);//fonction principale pour l'affichage des éléments
    	glutIdleFunc(Display);//pour raffraichir et exécuter à chaque fois les rotation et animation que l'on effectue à travers les touches ;)
    	glutKeyboardFunc(padGL);
    	glutSpecialFunc(padspecialGL);
    	glutMainLoop();//gestion par glut des différentes fonction dans cette boucle sorte de boucle principale :)
    	return (0);
    }
    Voilà et quand je fais:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    dans ma fonction d'affichage le programme affiche texture[1] c'est-à-dire la dernière texture chargée et non texture[0] donc cela affiche cube2.jpg et non cube.jpg


    S'il vous plait aidez-moi je suis un peu désespéré (excusez-moi si j'ai l'air noob :S)

  2. #2
    Expert confirmé
    Avatar de shenron666
    Homme Profil pro
    avancé
    Inscrit en
    Avril 2005
    Messages
    2 527
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Seine et Marne (Île de France)

    Informations professionnelles :
    Activité : avancé

    Informations forums :
    Inscription : Avril 2005
    Messages : 2 527
    Points : 5 195
    Points
    5 195
    Par défaut
    Citation Envoyé par pspflashsystem Voir le message
    le problème est que l'image "plaqué" sur ma face ne correspond pas à l'image que je souhaite plaquer
    tu as chargé la bonne image ?

    plus sérieusement, si tu pouvais décrire ton problème plus précisément que ça on pourrait t'aider
    une capture d'écran pourrait aussi aider à comprendre le problème

    pour faire simple, moins la description de ton problème est précise, moins tu auras de réponses rapide
    problème urgent ou pas, prends au moins le temps de le décrire

    en passant, je te conseillerait plutot de passer par des bibliothèques qui utilisent libjpeg pour charger une texture plutot que de réinventer la roue en utilisant toi même libjpeg
    par exemple : Simple OpenGL Image Library

  3. #3
    Membre à l'essai
    Profil pro
    Inscrit en
    Février 2009
    Messages
    13
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2009
    Messages : 13
    Points : 12
    Points
    12
    Par défaut
    Ok je suis désolé

    Sinon je souhaite cette fonction car je programme également sur plateforme portable (psp entre autre) et certaines libs pc ne fonctionne pas sur psp et dans un souci de portabilité je souhaite garder celle-ci. En espérant que vous me comprenderez

    donc voilà des screenshots pour mieux comprendre :

    1) Voici ce qu'affiche ma fênetre (je fais l'affichage d'une sorte d'avion en bleu et d'un simple cube en blanc en rotation):


    2) Or normalement mon programme doit plaquer la texture image cube.jpg suivante qui est stocké dans texture[0] (donc l'avion devrait être en marron bois):



    3) Le programme plaque tout le temps la texture suivante (c'est-à-dire cube2.jpg qui est stocké dans texture[1]):


    Voilà en espérant que c'est plus claire si vous voulez d'autres détails je suis présent ^^

    Sinon voici ma fonction Display:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
     
    void Display(void){
         int i;
    	 int j;
    	 int p;
    	 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
    	 glMatrixMode(GL_MODELVIEW);
    	 glLoadIdentity();
    	 gluLookAt(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
    	 glEnable(GL_TEXTURE_2D);
    	 glEnable(GL_DEPTH_TEST);//pour autoriser le Z-Buffer
    	 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//on nettoie l'écran et le Z-Buffer (==depth buffer)
     
    	 glMatrixMode(GL_MODELVIEW);
    	 glLoadIdentity();
    	 gluLookAt(0.0f, 25.0f, 40.0f, 0.0f, 10.0f, 0.0f, 0.0f, 1.0f, 0.0f);
     
    	 anglepi+=0.05f;
     
    	 //exemple d'utilisation du loader
    	 //mode complexe de chargement avec coordonnées texture ;)
    	 glPushMatrix();
    	 glTranslatef(10.0f, 0.0f, -30.0f);
    	 glRotatef(anglepi, 1.0f, 0.0f, 0.0f);
    	 glBegin(GL_TRIANGLES);
        	glBindTexture(GL_TEXTURE_2D, texture[0]);
    	     for(i=1; i<model.lineobjordervertex; i++) {//model.lineobjordervertex stocke tout le nombre de vertex et des coordonnées de texture du fichier .obj
    		     glTexCoord2f(model.texcoord[model.ordertexcoord[i].t1].x, model.texcoord[model.ordertexcoord[i].t1].y); glVertex3f(model.vertex[model.ordervertex[i].v1].x, model.vertex[model.ordervertex[i].v1].y, model.vertex[model.ordervertex[i].v1].z);
    		     glTexCoord2f(model.texcoord[model.ordertexcoord[i].t2].x, model.texcoord[model.ordertexcoord[i].t2].y); glVertex3f(model.vertex[model.ordervertex[i].v2].x, model.vertex[model.ordervertex[i].v2].y, model.vertex[model.ordervertex[i].v2].z);
    		     glTexCoord2f(model.texcoord[model.ordertexcoord[i].t3].x, model.texcoord[model.ordertexcoord[i].t3].y); glVertex3f(model.vertex[model.ordervertex[i].v3].x, model.vertex[model.ordervertex[i].v3].y, model.vertex[model.ordervertex[i].v3].z);
    	     }
    	 glEnd();
    	 glPopMatrix(); 
     
    	 //mode simple du loader sans coordonnées texture ;)
    	 glPushMatrix();
    	 glDisable(GL_TEXTURE_2D);
    	 glTranslatef(-5.0f, 20.0f, 19.0f);
    	 glRotatef(anglepi, 1.0f, 1.0f, 1.0f);
    	 glColor3ub(255, 255, 255);
    	 glBegin(GL_TRIANGLES);
    	 for(j=1; j<models.lineobjordervertex; j++) {
    	     glVertex3f(models.vertex[models.ordervertex[j].v1].x, models.vertex[models.ordervertex[j].v1].y, models.vertex[models.ordervertex[j].v1].z);
    		 glVertex3f(models.vertex[models.ordervertex[j].v2].x, models.vertex[models.ordervertex[j].v2].y, models.vertex[models.ordervertex[j].v2].z);
    		 glVertex3f(models.vertex[models.ordervertex[j].v3].x, models.vertex[models.ordervertex[j].v3].y, models.vertex[models.ordervertex[j].v3].z);
    	 }
    	 glEnd();
    	 glPopMatrix();
    	 glColor3ub(255, 255, 255);
         glutSwapBuffers();
    }
    Merci encore pour votre aide bénévole c'est vraiment sympa

  4. #4
    Expert confirmé
    Avatar de shenron666
    Homme Profil pro
    avancé
    Inscrit en
    Avril 2005
    Messages
    2 527
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Seine et Marne (Île de France)

    Informations professionnelles :
    Activité : avancé

    Informations forums :
    Inscription : Avril 2005
    Messages : 2 527
    Points : 5 195
    Points
    5 195
    Par défaut
    ok je comprend mieux, autant que possible il faudrait déjà t'assurer que ton image est bien chargée
    bon c'est pas facile quand on développe sur console sans les outils appropriés

    sinon après un coup d'oeil rapide, j'ai remarqué que tu avais mis glBindTexture entre glBegin et glEnd dans ta fonction Display

    hors la documentation de glBindTexture stipule bien :
    GL_INVALID_OPERATION is generated if glBindTexture is
    executed between the execution of glBegin and the
    corresponding execution of glEnd.

  5. #5
    Membre à l'essai
    Profil pro
    Inscrit en
    Février 2009
    Messages
    13
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2009
    Messages : 13
    Points : 12
    Points
    12
    Par défaut
    Merci beaucoup oui c'était bien là le problème: je ne savais pas cette "règle" au moins j'ai appris une chose ^^

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Problème de multi textures
    Par Moldix dans le forum OpenGL
    Réponses: 2
    Dernier message: 03/01/2014, 04h45
  2. Réponses: 10
    Dernier message: 08/06/2009, 11h30
  3. Réponses: 15
    Dernier message: 29/03/2008, 15h08
  4. OpenGL: problème pour incorporer des textures
    Par milena dans le forum OpenGL
    Réponses: 5
    Dernier message: 23/03/2008, 17h16

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo