Bonjour,
Voila j'utilise opengl 4.5,et je rencontre un petit soucis...
mon objectif,est de dessiner a l'écran plusieurs objets (cubes,sphères etc,mais je reste en 2D pour l'instant,donc ça ne sera que des "planes") mais voila,le problème c'est que lorsque j'utilise la fonction glVertexAttribPointer dans la fonction main,ça fonctionne,mais lorsque j'essaye d'utiliser la programmation orientée objet,et de mettre les glVertexAttribPointer dans le constructeur de l'objet en lui meme,ça ne fonctionne plus.
Je vous donne mes codes :
ça c'est le main.cpp
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 #include "Object.h" #define WINDOW_WIDTH 800 #define WINDOW_HEIGHT 600 using namespace glm; using namespace std; int main(int argc, char *argv[]) { SDL_Init(SDL_INIT_EVERYTHING); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,24); Window window=Window(800,800,"Opengl"); SDL_Event event; Textures *texture=new Textures("E:/Program Files/Blender Foundation/Public_Domain_Grunge_Textures/photorealistic/photorealistic_floor/floor043.jpg"); Textures *texture1= new Textures("E:/Program Files/Blender Foundation/Public_Domain_Grunge_Textures/photorealistic/photorealistic_floor/floor047.jpg"); bool ok=true; Shader *shader=new Shader("vertex.vert","fragment.frag"); float t=0; glClearColor(0,0,0,0); mat4 projection,modelview; projection=perspective(70.,(double)WINDOW_WIDTH/WINDOW_HEIGHT,1.,100.); modelview=mat4(1.); Object *plane=new Object(0.5,0,1,2); Object *plane1=new Object(0.5,4,5,6); float x=0,y=0,z=0,x1=0,y1=0,z1=0,taille=0.5; while(ok){ glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); plane->Load(modelview,projection,shader,texture); while(SDL_PollEvent(&event)){ if(event.window.event==SDL_WINDOWEVENT_CLOSE){ ok=false; } else if(event.type==SDL_KEYDOWN){ if(event.key.keysym.sym==SDLK_LEFT){ x-=0.1; cout<<"x:"<<x<<endl; } else if(event.key.keysym.sym==SDLK_UP){ y+=0.1; cout<<"y:"<<y<<endl; } else if(event.key.keysym.sym==SDLK_DOWN){ y-=0.1; cout<<"y:"<<y<<endl; } else if(event.key.keysym.sym==SDLK_RIGHT){ x+=0.1; cout<<"x:"<<x<<endl; } else if(event.key.keysym.sym==SDLK_z){ y1+=0.1; cout<<"y1:"<<y1<<endl; } } } SDL_GL_SwapWindow(window.GetWindow()); SDL_Delay(10); } return 0; }
et ceci est objet.cpp:
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 #include "Object.h" using namespace glm; Object::Object(float taille,long vert,long col,long tex) { taille=taille; vertexID=vert; colorID=col; textureID=tex; float vertices[]={-taille,-taille , -taille,taille , taille,taille , taille,taille , -taille,-taille ,taille,-taille }; float text[]={0,0 , 0,1 , 1,1 , 1,1 , 0,0 , 1,0}; float color[]={1,0,0 ,0,1,0, 0,0,1 , 1,0,0 ,0,1,0, 0,0,1 }; glVertexAttribPointer(vertexID,2,GL_FLOAT,GL_FALSE,0,vertices); glVertexAttribPointer(colorID,3,GL_FLOAT,GL_FALSE,0,color); glVertexAttribPointer(textureID,2,GL_FLOAT,GL_FALSE,0,text); } Object::~Object() { //dtor } void Object::Load(mat4 modelview,mat4 projection,Shader *shader,Textures *texture){ shader->UseProgramVF(); texture->Bind(); glEnableVertexAttribArray(vertexID); glEnableVertexAttribArray(colorID); glEnableVertexAttribArray(textureID); glUniformMatrix4fv(glGetUniformLocation(shader->getProgram(),"projection"),1,GL_FALSE,value_ptr(projection)); glUniformMatrix4fv(glGetUniformLocation(shader->getProgram(),"modelview"),1,GL_FALSE,value_ptr(modelview)); glDrawArrays(GL_TRIANGLES,0,6); glDisableVertexAttribArray(textureID); glDisableVertexAttribArray(colorID); glDisableVertexAttribArray(vertexID); texture->UnBind(); shader->Stop(); }
et objet.h:
normalement je suis censé faire plane1->Load();plane2->Load(); etc pour avoir de multiples objets...
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 #ifndef OBJECT_H #define OBJECT_H #include "Textures.h" class Object { public: Object(float,long,long,long); virtual ~Object(); void Load(glm::mat4 modelview,glm::mat4 projection,Shader *Shader,Textures *texture); protected: private: float taille; long vertexID,colorID,textureID; float *vertices; float *vertice; float *color; }; #endif // OBJECT_H
le problème,je le rappelle,c'est que je suis obligé de mettre la fonction glVertexAttribPointer dans la fonction main,sinon,l'application ne fait aucun rendu,l'écran est noir...et ceci est problématique si on veux profiter de la programmation objet.
Merci de m'avoir lu.
Partager