Salut a tous,
Après avoir combattu mille est une fois avec mon code j'ai céder!
Je ne parviens pas a afficher une scène 3D chose facile sur le papier, mais sur le clavier c'est toute autre.
Texture.cpp
Texture.h
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 #include "Texture.h" #include <iostream> #include <string.h> /* #include <fstream> #include <cstdlib> #include <cstdio> */ Texture::Texture(void* data, int w, int h, int format){ glGenTextures(1, &textureID); glBindTexture(GL_TEXTURE_2D, textureID); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h , 0, format, GL_UNSIGNED_BYTE, data); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glBindTexture(GL_TEXTURE_2D, 0); } Texture* Texture::loadBMP(const char* filename){ FILE* fp; fp = fopen(filename, "rb"); if(!fp){ cout << filename << "\n erreur d'ouverture!\n" << endl; fclose(fp); return NULL; } char *headerField = new char[2]; fread(headerField, 2, sizeof(char), fp); if(strcmp(headerField, "BM")){ delete [] headerField; cout << "\nLe fichier n'est pas un bitmap!\n" << endl; fclose(fp); return NULL; } delete [] headerField; unsigned int bmpDataLocation; unsigned int bmpWidth; unsigned int bmpHeight; unsigned short numColorPlanes; unsigned short bitsPerPixel; unsigned int compressionMethod; unsigned int bmpDataSize; fseek(fp, 0x000a, SEEK_SET); fread(&bmpDataLocation, 1, sizeof(unsigned int), fp); fseek(fp, 0x0012, SEEK_SET); fread(&bmpWidth, 1, sizeof(unsigned int), fp); fread(&bmpHeight, 1, sizeof(unsigned int), fp); fread(&numColorPlanes, 1, sizeof(unsigned short), fp); fread(&bitsPerPixel, 1, sizeof(unsigned short), fp); fread(&compressionMethod, 1, sizeof(unsigned int), fp); fread(&bmpDataSize, 1, sizeof(unsigned int), fp); if(numColorPlanes != 1 || bitsPerPixel != 24 || compressionMethod !=0){ cout << "\nLe fichier n'est pas une Raw bmp24!\n" << endl; fclose(fp); return NULL; } unsigned char* bmpData = new unsigned char[bmpDataSize]; fseek(fp, bmpDataLocation, SEEK_SET); fread(bmpData, bmpDataSize, sizeof(unsigned char), fp); fclose(fp); return new Texture(bmpData, bmpWidth, bmpHeight, GL_RGB); }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 #include <iostream> #include <GL/gl.h> using namespace std; class Texture{ public: unsigned int textureID; public: Texture(void *data, int w, int h, int format); static Texture* loadBMP(const char* filename); };
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 void Display_Quads() { inputActions(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // la Fonction "glclear" permet de nettoyer les buffers glLoadIdentity(); // Réinitialiser la matrice de transformation. //glTranslatef(0.0f,0.0f,-5.0f); //glRotatef(rot,0.0f,1.0f,0.0f); // Tourner le cube sur X, Y, et Z //Transformations du Camera glRotatef(Camera::rotation.x, 1.0f, 0.0f, 0.0f); glRotatef(Camera::rotation.y, 0.0f, 1.0f, 0.0f); glRotatef(Camera::rotation.z, 0.0f, 0.0f, 1.0f); glTranslatef(-Camera::position.x, -Camera::position.y, -Camera::position.z); glBindTexture(GL_TEXTURE_2D, text->textureID); glBegin(GL_QUADS); glColor3f(1, 1, 1); glTexCoord2f(100, 100); glVertex3f(100, 0, 100); glTexCoord2f(-100, 100); glVertex3f(-100, 0, 100); glTexCoord2f(-100, -100); glVertex3f(-100, 0, -100); glTexCoord2f(100, -100); glVertex3f(100, 0, -100); glEnd(); glBindTexture(GL_TEXTURE_2D, 0); // En mode double buffer, vide le tampon image à lécran glutSwapBuffers(); }
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 int main(int argc, char **argv) { glutInit(&argc, argv); /* Definit le mode d'affichage: Couleur RGB - Double buffering [Pour optimiser l'affichage] */ glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); /* Definit la taille de la fenetre */ glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); /* Definit la position de la fenetre */ glutInitWindowPosition(100, 100); /* Definit le nom de la fenetre */ glutCreateWindow(WINDOW_TITLE); /* Indique la fonction à utiliser lors de laffichage graphique.. */ glutDisplayFunc(Display_Quads); /* La fonction glutIdleFunc est lancée lorsque le système na plus rien à faire (affichage terminé, pas dévènements clavier). Elle sert à faire des animations */ glutIdleFunc(Display_Quads); /* La fonction "ReshapeFunc" est appelee pour le redimentionnement de la fenetre. */ glutReshapeFunc(reshape); /* La fonction "KeyboardFunc" est appelee quand un evenement clavier est detecter. */ //glutKeyboardFunc(keyboard); /* La fonction "Init" permet l'initialisation de GL. */ Init(); /* La fonction "glutKeyboardFunc" permet la saisit. */ glutKeyboardFunc(Keyboard::keyDown); glutKeyboardUpFunc(Keyboard::keyUp); glutMotionFunc(Mouse::move); glutPassiveMotionFunc(Mouse::move); //Enable Features glEnable(GL_DEPTH_TEST); glEnable(GL_TEXTURE_2D); //Setup Content text = Texture::loadBMP("tiles_1.bmp"); if(!text){ return 1; } Camera::position.y = 1; /* La fonction "MainLoop" boucle principale de gestion des evenements */ glutMainLoop(); return 0; }
Donc si une personne parviens à me dire pourquoi ce code ne veut pas être dresser comme il ce doit !!!
Merci de votre aide
ps: Sa compile, sa affiche le terminale avec le message suivant erreur ouverture, j'utilise codeBlocks









Répondre avec citation






Partager