gKit2 light
Loading...
Searching...
No Matches
fragments.cpp
Go to the documentation of this file.
1
3
4
5#include "wavefront_fast.h"
6#include "texture.h"
7
8#include "program.h"
9#include "uniforms.h"
10
11#include "draw.h"
12#include "app_camera.h" // classe Application a deriver
13
14
15// utilitaire. creation d'une grille / repere.
16Mesh make_grid( const int n= 10 )
17{
18 Mesh grid= Mesh(GL_LINES);
19
20 // grille
21 grid.color(White());
22 for(int x= 0; x < n; x++)
23 {
24 float px= float(x) - float(n)/2 + .5f;
25 grid.vertex(Point(px, 0, - float(n)/2 + .5f));
26 grid.vertex(Point(px, 0, float(n)/2 - .5f));
27 }
28
29 for(int z= 0; z < n; z++)
30 {
31 float pz= float(z) - float(n)/2 + .5f;
32 grid.vertex(Point(- float(n)/2 + .5f, 0, pz));
33 grid.vertex(Point(float(n)/2 - .5f, 0, pz));
34 }
35
36 // axes XYZ
37 grid.color(Red());
38 grid.vertex(Point(0, .1, 0));
39 grid.vertex(Point(1, .1, 0));
40
41 grid.color(Green());
42 grid.vertex(Point(0, .1, 0));
43 grid.vertex(Point(0, 1, 0));
44
45 grid.color(Blue());
46 grid.vertex(Point(0, .1, 0));
47 grid.vertex(Point(0, .1, 1));
48
49 glLineWidth(2);
50
51 return grid;
52}
53
54
55class TP : public AppCamera
56{
57public:
58 // constructeur : donner les dimensions de l'image, et eventuellement la version d'openGL.
59 TP( ) : AppCamera(1024, 640) {}
60
61 // creation des objets de l'application
62 int init( )
63 {
64 // decrire un repere / grille
65 repere= make_grid(10);
66
67 // charge un objet
68 mesh= read_mesh_fast("data/bigguy.obj");
69 //~ mesh= read_mesh_fast("/home/jciehl/scenes/flying-world/export.obj");
70 //~ mesh= read_mesh_fast("/home/jciehl/scenes/bistro/exterior.obj");
71
72 Point pmin, pmax;
73 mesh.bounds(pmin, pmax);
74 camera().lookat(pmin, pmax);
75
76 vao= mesh.create_buffers( false, false, false, false ); // positions
77 n= mesh.vertex_count();
78
79 program_record= read_program("gkit2_tutos/M2/fragment_record.glsl");
80 program_print_errors(program_record);
81
82 program_replay= read_program("gkit2_tutos/M2/fragment.glsl");
83 program_print_errors(program_replay);
84
85 //
86 size= 1024*1024*8;
87
88 glGenBuffers(1, &buffer);
89 glBindBuffer(GL_ARRAY_BUFFER, buffer);
90 glBufferData(GL_ARRAY_BUFFER, size * 6*sizeof(float), nullptr, GL_DYNAMIC_DRAW);
91
92 //
94
95 //
96 glGenVertexArrays(1, &vao_replay);
97
98 glBindVertexArray(vao_replay);
99 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), (const void *) (1*sizeof(unsigned)));
100 glEnableVertexAttribArray(0);
101 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), (const void *) (1*sizeof(unsigned) + 3*sizeof(float)));
102 glEnableVertexAttribArray(1);
103
104 // etat openGL par defaut
105 glBindVertexArray(0);
106 glBindBuffer(GL_ARRAY_BUFFER, 0);
107
108 glClearColor(0.2f, 0.2f, 0.2f, 1.f); // couleur par defaut de la fenetre
109
110 glClearDepth(1.f); // profondeur par defaut
111 glDepthFunc(GL_LEQUAL); // ztest, conserver l'intersection la plus proche de la camera
112 glEnable(GL_DEPTH_TEST); // activer le ztest
113
114 glFrontFace(GL_CCW);
115 glCullFace(GL_BACK);
116 //~ glEnable(GL_CULL_FACE);
117 glDisable(GL_CULL_FACE);
118
119 return 0; // pas d'erreur, sinon renvoyer -1
120 }
121
122 // destruction des objets de l'application
123 int quit( )
124 {
125 repere.release();
126 return 0; // pas d'erreur
127 }
128
129 // dessiner une nouvelle image
130 int render( )
131 {
132 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
133
134 Transform model= Identity();
135 Transform view= camera().view();
136 Transform projection= camera().projection();
137 Transform mv= view * model;
138 Transform mvp= projection * mv;
139
140 // clear
141 static unsigned init= 0;
142 static unsigned zinit= 0;
143 static unsigned replay= 0;
144
145 if(key_state('z'))
146 {
147 clear_key_state('z');
148 zinit= (zinit +1) %2;
149
150 init= 0; // force une nouvelle captire
151
152 if(zinit)
153 printf("Zpre pass ON\n"); // + fragment early tests
154 else
155 printf("Zpre pass OFF\n");
156 }
157
158 if(!init)
159 {
160 if(zinit)
161 {
162 draw(mesh, model, view, projection);
163 }
164
165 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, buffer);
166 {
167 float zero= 0;
168 glClearBufferData(GL_SHADER_STORAGE_BUFFER, GL_R32F, GL_RED, GL_FLOAT, &zero);
169 }
170
171 glBindImageTexture(0, texture, 0, GL_TRUE, 0, GL_READ_WRITE, GL_R32UI);
172 {
173 GLuint zero= 0;
174 glClearTexImage(texture, 0, GL_RED_INTEGER, GL_UNSIGNED_INT, &zero);
175 }
176
177 replay= 0;
178
179 // passe 1 : record
180 glBindVertexArray(vao);
181 glUseProgram(program_record);
182
183 program_uniform(program_record, "mvpMatrix", mvp);
184 program_uniform(program_record, "mvMatrix", mv);
185 program_uniform(program_record, "counters", 0);
186
187 glDrawArrays(GL_TRIANGLES, 0, n);
188
189 glMemoryBarrier( GL_ALL_BARRIER_BITS);
190
191 if(key_state(' '))
192 {
193 clear_key_state(' ');
194 init= 1;
195 }
196 }
197
198 if(init)
199 {
200 // passe 2 : replay
201 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
202
203 draw(repere, /* model */ Identity(), view, projection);
204
205 glBindVertexArray(vao_replay);
206 glUseProgram(program_replay);
207
208 program_uniform(program_replay, "mvpMatrix", mvp);
209
210 replay+= 4096;
211 if(replay > size)
212 replay= 0;
213 glDrawArrays(GL_POINTS, 0, replay);
214
215 static unsigned points= 1;
216 if(key_state(SDLK_KP_PLUS))
217 {
218 clear_key_state(SDLK_KP_PLUS);
219 points+= 1;
220 }
221 if(key_state(SDLK_KP_MINUS))
222 {
223 clear_key_state(SDLK_KP_MINUS);
224 points-= 1;
225 if(points < 1)
226 points= 1;
227 }
228 glPointSize(points);
229
230 static bool video= false;
231 if(key_state(SDLK_RETURN))
232 {
233 clear_key_state(SDLK_RETURN);
234 video= !video;
235
236 if(video) printf("start video capture...\n");
237 else printf("stop video capture.\n");
238 }
239
240 if(video)
241 capture("fragments");
242 }
243
244 return 1;
245 }
246
247protected:
248 Mesh repere;
249 Mesh mesh;
250
251 GLuint vao;
252 GLuint vao_replay;
253 unsigned n;
254
255 GLuint texture;
256 GLuint buffer;
257 unsigned size;
258
259 GLuint program_record;
260 GLuint program_replay;
261};
262
263
264int main( int argc, char **argv )
265{
266 // il ne reste plus qu'a creer un objet application et la lancer
267 TP tp;
268 tp.run();
269
270 return 0;
271}
classe application.
Definition app_camera.h:19
const Orbiter & camera() const
renvoie l'orbiter gere par l'application.
Definition app_camera.h:37
AppCamera(const int width, const int height, const int major=3, const int minor=3, const int samples=0)
constructeur, dimensions de la fenetre et version d'openGL.
Definition app_camera.cpp:5
int run()
execution de l'application.
Definition app.cpp:36
representation d'un objet / maillage.
Definition mesh.h:121
unsigned int vertex(const vec3 &p)
insere un sommet de position p, et ses attributs (s'ils sont definis par color(), texcoord(),...
Definition mesh.cpp:97
Mesh & color(const vec4 &c)
definit la couleur du prochain sommet.
Definition mesh.cpp:66
void lookat(const Point &center, const float size)
observe le point center a une distance size.
Definition orbiter.cpp:7
Transform projection(const int width, const int height, const float fov)
fixe la projection reglee pour une image d'aspect width / height, et une demi ouverture de fov degres...
Definition orbiter.cpp:48
Transform view() const
renvoie la transformation vue.
Definition orbiter.cpp:41
Definition alpha.cpp:58
int render()
a deriver pour afficher les objets. renvoie 1 pour continuer, 0 pour fermer l'application.
int quit()
a deriver pour detruire les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon.
int init()
a deriver pour creer les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon.
Definition fragments.cpp:62
int window_height()
renvoie la hauteur de la fenetre de l'application.
Definition window.cpp:27
void clear_key_state(const SDL_Keycode key)
desactive une touche du clavier.
Definition window.cpp:46
void printf(Text &text, const int px, const int py, const char *format,...)
affiche un texte a la position x, y. meme utilisation que printf().
Definition text.cpp:140
int key_state(const SDL_Keycode key)
renvoie l'etat d'une touche du clavier. cf la doc SDL2 pour les codes.
Definition window.cpp:40
int window_width()
renvoie la largeur de la fenetre de l'application.
Definition window.cpp:23
Color Red()
utilitaire. renvoie une couleur rouge.
Definition color.cpp:28
Color Blue()
utilitaire. renvoie une couleur bleue.
Definition color.cpp:38
Color Green()
utilitaire. renvoie une couleur verte.
Definition color.cpp:33
Color White()
utilitaire. renvoie une couleur blanche.
Definition color.cpp:23
Transform Identity()
construit la transformation identite.
Definition mat.cpp:187
Mesh read_mesh_fast(const char *filename)
charge un fichier wavefront .obj et renvoie un mesh compose de triangles non indexes....
int capture(const char *prefix)
Definition texture.cpp:205
GLuint read_program(const char *filename, const char *definitions)
Definition program.cpp:218
int program_print_errors(const GLuint program)
affiche les erreurs de compilation.
Definition program.cpp:446
GLuint make_uint_texture(const int unit, const int width, const int height, const GLenum texel_type)
creation de textures pour stocker des donnees (autres qu'une couleur).
Definition texture.cpp:151
representation d'un point 3d.
Definition vec.h:21
representation d'une transformation, une matrice 4x4, organisee par ligne / row major.
Definition mat.h:21