Bonjour/bonsoir
Voilà j'essai de m'essayer au joie de opengl 3.0 avec ses VBOs, Shader etc... et pour l'instant j'essai de simplement dessiner un cube mais malheureusement je ne le vois pas enfin parfois j'ai ma fenêtre complètement remplis de couleurs.
Voici pour commencer ma méthode init. Je précise que j'utilise la librairie GLM pour tout ce qui est manipulation de matrice, vecteur etc...
Maintenant ma fonction draw :
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 //Initialisation de la couleur d'effacement de l'écran glClearColor(0.2, 0.2, 0.2, 1.0); /* Creation d'un vertex et fragment shader (glCreateShader(type)) * Association code source shader à l'objet créé (glShaderSource()) * Compilation code source objet shader (glCompileShader()) */ _shaderPgm = ShaderProgram::createStdShader(); if (_shaderPgm == NULL) qFatal("Std Shader pgm fail !"); /* Redifinition de la commande glUseProgramm(programm) * Commande pour utiliser programme pour traiter un sommet ou un fragment * en fonction du type de shader créé avec glCreateShader() */ _shaderPgm->startUseProgram(); /* Récupération index des variables uniform projectionMatrix et viewMatrix */ _projIndex = glGetUniformLocation(_shaderPgm->programId(), "projectionMatrix"); _viewIndex = glGetUniformLocation(_shaderPgm->programId(), "viewMatrix"); /* récupération index permettant de mettre à jour les variables concernées * dans le vertex shaderprogram */ _colorIndex = glGetAttribLocation(_shaderPgm->programId(), "color_in"); _positionIndex = glGetAttribLocation(_shaderPgm->programId(), "vertexPosition_in"); /* Désactivation du programme shader */ _shaderPgm->stopUseProgram(); //Initialisation avec la matrice identité des matrices de vue et de projection mat4 id = mat4(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f); _proj = id; _view = id; //TODO implémenter ici les initialisations du dessin ! _proj = glm::perspective(60.0f, ((GLfloat) width()) / ((GLfloat) height()), 1.0f, 500.0f); _view = glm::lookAt(vec3(0.0f, 0.0f, 0.0f), vec3(0.0f, 0.0f, -100.0f, vec3(0.0f, 1.0f, 0.0f) ); GLfloat vertices[] = { -1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f }; GLuint indices[] = { 0, 1, 2, 2, 1, 3, 4, 5, 6, 6, 5, 7, 3, 1, 5, 5, 1, 7, 0, 2, 6, 6, 2, 4, 6, 7, 0, 0, 7, 1, 2, 3, 4, 4, 3, 5 }; glGenBuffers(NUM_BUFFERS, buffers); glBindBuffer(GL_ARRAY_BUFFER, buffers[VERTICES]); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[INDICES]); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Je peux vous mettre aussi les fichier .vert et .frag (vertex shader and fragment shader) :
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 //Définition de la zone de dessin (en espace écran) glViewport(0, 0, width(), height()); //Effacement de l'écran et du tampon de profondeur glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); //Activation du program de shaders _shaderPgm->startUseProgram(); //Upload projection matrix glUniformMatrix4fv(_projIndex, 1, GL_FALSE, value_ptr(_proj)); //Upload view matrix glUniformMatrix4fv(_viewIndex, 1, GL_FALSE, value_ptr(_view)); //TODO implémenter ici les actions de dessin ! /* On active les deux tableaux génériques */ glEnableVertexAttribArray(_positionIndex); glEnableVertexAttribArray(_colorIndex); /* On active le buffer des sommets */ glBindBuffer(GL_ARRAY_BUFFER, buffers[VERTICES]); /* On envoie les données sommets et couleurs */ glVertexAttribPointer(_positionIndex, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), BUFFER_OFFSET(0)); glVertexAttribPointer(_colorIndex, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid *) BUFFER_OFFSET(3)); /* On active le buffer des indices */ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[INDICES]); /* Rendu de la géométrie */ glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0); /* Désactivation des buffers vertices et indices */ glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); /* Désactivation des tableaux génériques */ glDisableVertexAttribArray(_positionIndex); glDisableVertexAttribArray(_colorIndex); /* Désactivation du shader */ _shaderPgm->stopUseProgram(); printGLErrors("error during drawing");
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 // VERTEX SHADER #version 130 // Attributs in vec3 vertexPosition_in; in vec3 color_in; // Uniform uniform mat4 projectionMatrix; uniform mat4 viewMatrix; // Sorties out vec3 color_v; void main () { gl_Position = projectionMatrix * viewMatrix * vec4(vertexPosition_in, 1.0); color_v = color_in; }J'aimerais donc savoir ce que je fais de mal pour ne pas voir un carré (si je vois le cube de face) et après cela je ferais des méthodes pour zoom/dezoom et de navigation.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 #version 130 //Entrée (varying) in vec3 color_v; // Sortie out vec4 color_out; void main() { color_out = vec4(color_v, 1.0); }
Merci d'avance.
EDIT : Je mets en plus quelques définitions de mon .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 #define VERTICES 0 #define INDICES 1 #define NUM_BUFFERS 2 #define BUFFER_OFFSET(bytes) (bytes * sizeof(GLfloat)) /** Programme shader standard */ ShaderProgram* _shaderPgm; /** Identifiant de l'attribut représentant les coordonnées des sommets*/ GLint _positionIndex; /** Identifiant de l'attribut représentant la couleur des sommets*/ GLint _colorIndex; /** Identifiant de la variable uniforme représentant la matrice de projection*/ GLint _projIndex; /** Identifiant de la variable uniforme représentant la matrice de vue*/ GLint _viewIndex; /** Matrice de vue*/ glm::mat4 _view; /** Matrice de projection*/ glm::mat4 _proj; /* Tableau contenant les sommets et les couleurs */ GLfloat vertices[]; /* Tableau contenant les indices des sommets */ GLuint indices[]; /* Buffers pour le VBO */ GLuint buffers[NUM_BUFFERS];
Partager