-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
205 lines (174 loc) · 6.07 KB
/
main.cpp
File metadata and controls
205 lines (174 loc) · 6.07 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include<vector>
#include <QStringList>
#include "GLSLShader.h"
#include "texture.h"
Texture text; // Objet gerant les textures
int currentShader;
int shaderCount;
GLuint vao; // Le handle du VAO.
GLuint vbo_positions; // Le handle du VBO.
GLuint vbo_indices; // Le handle du VBO.
GLSLShader * shader; // Objet gerant les shaders
GLint winHeight, winWidth; // Resolution de la fenetre
GLfloat mouseCoord[4];
float elapsed_time, time0; // temps en secondes
// Fonction d'initialisation, appelée une fois en début de programme.
static void init(void)
{
//On charge les images
text.load(QString("images/brick_grise.jpg"));
text.load(QString("images/brick_rouge.jpg"));
text.load(QString("images/uppa.gif"));
text.load(QString("images/om.gif"));
//On definie les valeurs par defaut des uniform
winHeight = 1000;
winWidth = 500;
mouseCoord[0] = 0.;
mouseCoord[1] = 0.;
mouseCoord[2] = 0.;
mouseCoord[3] = 0.;
QStringList listShader;
listShader.append("shaders/shadertoy_1.frag.glsl");
listShader.append("shaders/shadertoy_2.frag.glsl");
listShader.append("shaders/shadertoy_3.frag.glsl");
listShader.append("shaders/shadertoy_4.frag.glsl");
shaderCount = listShader.size();
shader = new GLSLShader[shaderCount];
for(int i=0; i<shaderCount; i++)
{
// Création des shaders depuis des fichiers sur le disque
shader[i].LoadFromFile(GL_VERTEX_SHADER, "shaders/shadertoy.vert.glsl");
shader[i].LoadFromFile(GL_FRAGMENT_SHADER, listShader.at(i).toStdString());
shader[i].CreateAndLinkProgram();
shader[i].AddUniform("iGlobalTime");
shader[i].AddUniform("iResolution");
shader[i].AddUniform("iMouse");
shader[i].AddUniform("iChannel0");
shader[i].AddAttribute("position");
}
currentShader = 0;
// Créations des buffers
GLfloat positions[] = {-2,2, -2,-2, 2,2, 2,-2};
GLuint indices[] = {0,1,2,3};
glGenBuffers(1, &vbo_positions); // VBO pour contenir les positions.
glBindBuffer(GL_ARRAY_BUFFER, vbo_positions); // Il devient le VBO courant.
glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);
glGenBuffers(1, &vbo_indices);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo_indices);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glGenVertexArrays(1, &vao); // VAO pour contenir les états de VBO/attributs.
glBindVertexArray(vao); // Il devient le VAO courant.
glBindBuffer(GL_ARRAY_BUFFER, vbo_positions);
glEnableVertexAttribArray(shader[0]["position"]);
glVertexAttribPointer(shader[0]["position"], 2, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo_indices);
glBindVertexArray(0); // On détache tous les buffers pour l'instant
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glClearColor(0.5, 0.3, 0.3, 1.0); // Couleur d'effacement de l'écran
time0 = glutGet(GLUT_ELAPSED_TIME)/1000.; // initialisation du temps d'origine
}
// Fonction d'affichage, appelée régulièrement, pour réafficher le contenu.
void display(void)
{
shader[currentShader].Use();
glClear(GL_COLOR_BUFFER_BIT); // On efface la fenêtre.
glUniform1f(shader[currentShader]("iGlobalTime"), elapsed_time);
glUniform3f(shader[currentShader]("iResolution"), winWidth, winHeight, 1.0);
glUniform4f(shader[currentShader]("iMouse"), mouseCoord[0], mouseCoord[1], mouseCoord[2], mouseCoord[3]);
glUniform1i(shader[currentShader]("iChannel0"), 3);
glBindSampler(3, text.getSamplerState()); // utilise samplerState pour UT3
glActiveTexture(GL_TEXTURE3); // Active l'unité de texture 3
glBindTexture(GL_TEXTURE_2D, text.getText());
glBindVertexArray(vao);
glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
glutSwapBuffers(); // Echange des buffers FRONT et BACK.
shader[currentShader].UnUse();
}
//Procedure appele si la fenetre est redimensionne
void reshape(GLint width, GLint height)
{
glViewport(0, 0, width, height);
winHeight = height;
winWidth = width;
}
//Calcul du temps ecoule
void animate()
{
elapsed_time = glutGet(GLUT_ELAPSED_TIME)/1000. - time0;
glutPostRedisplay();
}
// Fonction de libération des ressources, appélée en fin de programme.
static void freeResources(void)
{
glBindBuffer(GL_ARRAY_BUFFER, 0); // On indique qu'aucun VBO n'est le courant.
glDeleteBuffers(1, &vbo_positions); // Et on supprime les nôtres.
glBindVertexArray(0); // On indique qu'aucun VAO n'est le courant.
glDeleteVertexArrays(1, &vao); // Et on supprime le nôtre.
}
//Event clavier
void keyboard(unsigned char key, int x, int y)
{
switch(key)
{
case 'g':
text.setCurrent(0);
break;
case 'r':
text.setCurrent(1);
break;
case '+':
text.next();
break;
case 32:
currentShader++;
currentShader %= shaderCount;
break;
case 27:
freeResources();
exit(0);
break;
}
}
//Event souris
void mouse(int button, int state, int x, int y)
{
if((button == GLUT_LEFT_BUTTON) && (state == GLUT_UP))
{
mouseCoord[0]=0.;
mouseCoord[1]=0.;
mouseCoord[2]=0.;
mouseCoord[3]=0.;
}
}
//Event mouvement souris
void motion(int x, int y)
{
mouseCoord[0]=(float)x;
mouseCoord[1]=(float)(winHeight-y);
mouseCoord[2]=1.;
mouseCoord[3]=0.;
}
int main(int argc, char* argv[])
{
glutInitContextVersion(3, 3);
glutInitContextFlags(GLUT_FORWARD_COMPATIBLE | GLUT_DEBUG);
glutInitContextProfile(GLUT_CORE_PROFILE);
glewExperimental=true;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(1000, 500);
glutCreateWindow("Un triangle de couleur changeante en rotation");
glewInit(); // Initialisation de la bibliothèque GLEW.
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(animate);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
freeResources();
return 0;
}