Bonsoir, je suis entrain de créer un projet en SDL2 et j'ai l'habitude de travailler en SDL1, et les premiers problèmes commencent : Rien que pour charger une texture, le programme plante en me renvoyant 3 comme code d'erreur. Pour effectuer le chargement, j'utilise cette fonction :
Pour remédier à cela, j'ai décidé de récupérer le code donné dans le tuto sur OpenGL 3.3, et j'ai modifié un peu la fonction :
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 GLuint loadTexture(const char * filename,int 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; }
Le code n'est pas encore achevé et j'ai déjà des problèmes : Les valeurs de "formatInterne" et "format" sont erronées, cela vient apparemment du paramètre "BytesPerPixel" qui étrangement ne vaut ni 3 ni 4 mais 8, c'est pourtant une copie du cod. Pour info l'image utilisée est au format TGA 24 bits donc logiquement "BytesPerPixel" devrait valoir 3. En attendant de trouver une solution, j'ai ajouté cette ligne :
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 GLuint loadTexture(const char * filename,int useMipMap,int versionSDL) { GLuint glID; SDL_Surface * picture_surface = NULL; SDL_Surface *gl_surface = NULL; SDL_Surface * gl_fliped_surface = NULL; Uint32 rmask, gmask, bmask, amask; GLenum formatInterne,format; picture_surface = IMG_Load(filename); if (picture_surface == NULL) return 0; if(versionSDL==1) { //Même code qu'au-dessus } else if(versionSDL==2) { glGenTextures(1,&glID); glBindTexture(GL_TEXTURE_2D,glID); if(picture_surface->format->BytesPerPixel==3) { formatInterne=GL_RGB; if(picture_surface->format->Rmask==0xff) format=GL_RGB; else format=GL_BGR; } else if(picture_surface->format->BytesPerPixel==4) { formatInterne=GL_RGBA; if(picture_surface->format->Rmask==0xff) format=GL_RGBA; else format=GL_BGRA; } printf("%x %x\n",formatInterne,format); } return glID; }
Et là crac, le programme plante avec 3 comme code d'erreur.
Code : Sélectionner tout - Visualiser dans une fenêtre à part glTexImage2D(GL_TEXTURE_2D,0,4,picture_surface->w,picture_surface->h,0,GL_RGBA,GL_UNSIGNED_BYTE,picture_surface->pixels);
Bref, je suis un peu coincé pour charger les textures en SDL2, auriez-vous une solution svp ?
Partager