Je développe un moteur 3D, basé sur OpenGL. Tout fonctionne cependant c'est lent, pour seulement 2000 polygones les FPS sont divisés par 10.
Voilà directement mes codes.
Ce qui est utilisé (en gros) dans mes structure:
Affichage objet :
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 typedef struct ge_Vertex { float u, v; //Coordonnées de texture unsigned int color; //couleur, format 0xAABBGGRR float nx, ny, nz; //vecteur normal float x, y, z; //position dans l'espace } ge_Vertex; typedef struct ge_Object { ge_Vertex* verts; //tableau des vertices int nVerts; //nombre de vertices bool lighted; //indique s affecté par l'éclairage ge_Image* texture; //la texture u32 vertex_buffer; //l'ID OpenGL du VBO /*......*/ } ge_Object; typedef struct ge_Scene { ge_Object** objs; //tableaux de pointeurs vers les objets int nObjs; //nombre d'objets /*......*/ ge_Light* lights[8]; //tableaux de pointeurs vers les lumières int nLights; //nombre de lumières } ge_Scene; typedef struct ge_Image { /*......*/ u32 glId; //ID OpenGL de texture } ge_Image;
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 void DrawObject(ge_Object* obj){ //J'utilise les VBOs pour plus de vitesse //activation du VBO glBindBuffer(GL_ARRAY_BUFFER, obj->vertex_buffer); void* vertices = BUFFER_OFFSET(24); void* colors = BUFFER_OFFSET(8); void* normals = BUFFER_OFFSET(12); void* tex_coords = BUFFER_OFFSET(0); glVertexPointer(3, GL_FLOAT, sizeof(ge_Vertex), vertices); glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(ge_Vertex), colors); glNormalPointer(GL_FLOAT, sizeof(ge_Vertex), normals); glTexCoordPointer(2, GL_FLOAT, sizeof(ge_Vertex), tex_coords); //plus besoins du VBO glBindBuffer(GL_ARRAY_BUFFER, 0); //pour savoir si il a une texture textured = obj->texture?true:false; //si oui, la binder if(textured)glBindTexture(GL_TEXTURE_2D, obj->texture->glId); //j'utilise les shaders (ils ne ralentissent pas le prog, au contraire, c'est testé) glUniform1i(location_textured, textured); glUniform1i(location_lighted, obj->lighted); //Affichage glDrawArrays(GL_TRIANGLES, 0, obj->nVerts); }
Affichage scène :
Si il y a moyen d'améliorer ça... je pense que ça ne tient que dans un soucis d'organisation...
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 void ge3dDrawScene(ge_Scene* scene){ // Paramétrages des lumières // ligths_type[] est passé au shader int i = 0; for(i=0; i<scene->nLights; i++){ ligths_type[i] = scene->lights[i]->type * scene->lights[i]->used; //*scene->lights[i]->used je fais cela plutot qu'un if(scene->lights[i]->used) qui est plus consommateur //Tout ce bordel pour quelques positions glLightfv(GL_LIGHT0+i, GL_POSITION, &scene->lights[i]->x); if(scene->lights[i]->type == GE_LIGHT_TYPE_SPOT){ glLightfv(GL_LIGHT0+i, GL_SPOT_DIRECTION, &scene->lights[i]->cX); } } //Passage de variables au shader if(shader_supported && shaderId){ glUniform1iv(location_ligths_type, GL_MAX_LIGHTS, ligths_type); glUniform1i(location_count_lights, scene->nLights); } //Affichage des objets, un à un ! for(i=0; i<scene->nObjs; i++){ //Paramétrage du matériau glMaterialfv(GL_FRONT, GL_AMBIENT, scene->objs[i]->material.ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, scene->objs[i]->material.diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, scene->objs[i]->material.specular); //on affiche enfin l'objet... DrawObject(scene->objs[i]); } }
Merci !
Je passe à environ 1000 FPS pour seulement 1701 polygones faits de 17 objets, en résolution 1680x1050. Sur le PC de mon ami (un portable) ça tourne autour des 18 FPS....
Ma config: Athlon X2 @3Ghz, ATI HD Radeon 4870
EDIT: Pour ceux qui voudraient tester (Z,Q,S,D pour se déplacer, E pour la lampe torche)
Partager