Bonjour à tous.
Je cherche à charger le nom des textures que je récupère dans un fichier, dans un tableau dynamique GLUint, afin de ne pas avoir à recharger la texture à chaque frame, pour accélérer mon programme. Voila une partie de mon code:
initialisation:
fonction load:
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 int main(int argc, char *argv[]) { SDL_Event event; const Uint32 time_per_frame = 1000/FPS; unsigned int width = LARGEUR_FENETRE; unsigned int height = HAUTEUR_FENETRE; Uint32 last_time,current_time,elapsed_time; //for time animation Uint32 start_time,stop_time; //for frame limit SDL_Init(SDL_INIT_VIDEO); atexit(stop); SDL_WM_SetCaption("SDL GL Application", NULL); SDL_SetVideoMode(width, height, 32, SDL_OPENGL); initFullScreen(&width,&height); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective(70,(double)width/height,0.001,1000); glEnable (GL_DEPTH_TEST); glEnable (GL_TEXTURE_2D); load();
fonction loadtexture:
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
139
140
141
142
143
144
145
146 void load() { ofstream fichier8("doco.txt", ios::out | ios::trunc); int j, teka=0, pointagep, pointaget; int nbrPoints=0, nbrTex=0; ifstream fichier2("doc/liste1.jaj", ios::in); ifstream fichier3("doc/liste2.jaj", ios::in); fichier2>>nbobjets; glGenTextures(nbobjets,&identifiant); nbrfacesp = new int [nbobjets]; for(j=0;j<nbobjets;j++) { pointagep=nbrPoints; pointaget=nbrTex; nomsobjets.push_back(""); nomstextures.push_back(""); fichier2>>nomsobjets[j]; fichier3>>nomstextures[j]; loadTexture(nomstextures[j].c_str(),identifiant+j); texture.push_back(identifiant + j); nbrfacesp[j]=0; ifstream fichier(nomsobjets[j].c_str(), ios::in); if(fichier) { char caractere; while( !fichier.eof() ) { fichier.get(caractere); fichier8<<caractere<<endl; switch(caractere) { case 'v' : fichier.get(caractere); if(caractere==' ') { point1.push_back(1); point2.push_back(1); point3.push_back(1); fichier >> point1[nbrPoints]>> point2[nbrPoints]>> point3[nbrPoints]; fichier8 << point1[nbrPoints]<< point2[nbrPoints]<< point3[nbrPoints]<<endl; nbrPoints++; } else if(caractere=='t') { tex1.push_back(1); tex2.push_back(1); fichier >> tex1[nbrTex]>> tex2[nbrTex]; fichier8 << tex1[nbrTex]<< tex2[nbrTex]<<endl; nbrTex++; } break; case 'f' : texFace1.push_back(1); // (*) texFace2.push_back(1); texFace3.push_back(1); face1.push_back(1); face2.push_back(1); face3.push_back(1); teka=0; fichier>>teka; fichier8<<teka<<endl; face1[nbrFaces]= teka + pointagep; fichier.get(caractere); fichier >> teka; fichier8<<teka<<endl; texFace1[nbrFaces] = teka + pointaget; fichier>>teka; fichier8<<teka<<endl; face2[nbrFaces] = teka + pointagep; fichier.get(caractere); fichier >> teka; fichier8<<teka<<endl; texFace2[nbrFaces] = teka + pointaget; fichier>>teka; fichier8<<teka<<endl; face3[nbrFaces] = teka + pointagep; fichier.get(caractere); fichier >> teka; fichier8<<teka<<endl; texFace3[nbrFaces] = teka + pointaget; nbrFaces++; nbrfacesp[j]++; break; default: while(caractere!='\n' && !fichier.eof() ) fichier.get(caractere); break; } } } else cerr << "Impossible d'ouvrir le fichier !" << endl; fichier.close(); } fichier2.close(); fichier3.close(); }
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 void loadTexture(const char * filename,GLuint k) { 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) #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); glBindTexture(GL_TEXTURE_2D, k); 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); 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_MAG_FILTER,GL_LINEAR); SDL_FreeSurface(gl_fliped_surface); SDL_FreeSurface(gl_surface); SDL_FreeSurface(picture_surface); }
fonction dessiner:
Alors le programme se lance mais la fenêtre se ferme aussitôt qu'elle s'ouvre. Et grace au "fichier8", j'ai pu trouver que le programme plantait au "(*)" indiqué au deuxième passage dans la boucle ! Or ca ne plantait pas lorsque j'utilisais un simple GLuint au lieu d'un tableau.
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 int i = 0,k=0, bim; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); //Crée une matrice glLoadIdentity(); //Iniatlise la matriceGLuint texture; camera->look(); glBindTexture(GL_TEXTURE_2D, texture[0]); glEnd(); for(int j=0; j<nbrFaces; j++) { if(k==nbrfacesp[i]) { i++; glBindTexture(GL_TEXTURE_2D, texture[i]); glEnd(); k=0; } k++; glBegin(GL_TRIANGLES); glTexCoord2f(tex1[ texFace1[j]-1],tex2[ texFace1[j]-1]); glVertex3f(point1[ face1[j]-1], point2[ face1[j]-1], point3[ face1[j]-1]); glTexCoord2f(tex1[ texFace2[j]-1],tex2[ texFace2[j]-1]); glVertex3f(point1[ face2[j]-1], point2[ face2[j]-1], point3[ face2[j]-1]); glTexCoord2f(tex1[ texFace3[j]-1],tex2[ texFace3[j]-1]); glVertex3f(point1[ face3[j]-1], point2[ face3[j]-1], point3[ face3[j]-1]); glEnd(); } glFlush(); SDL_GL_SwapBuffers(); }
Pourriez-vous m'aider à resoudre ce problème S'il vous plait. Merci d'avance
Partager