gKit2 light
alpha.cpp
1 
2 // "hashed alpha testing" https://casual-effects.com/research/Wyman2017Hashed/
3 
4 // \todo utiliser la quantification du bruit pour le stabiliser...
5 
6 // \todo comparer avec "Weighted Blended Order-Independent Transparency" http://jcgt.org/published/0002/02/09/
7 
8 
9 #include "wavefront.h"
10 
11 #include "draw.h"
12 #include "program.h"
13 #include "texture.h"
14 #include "uniforms.h"
15 #include "app_camera.h"
16 
17 
18 // utilitaire. creation d'une grille / repere.
19 Mesh make_grid( const int n= 10 )
20 {
21  Mesh grid= Mesh(GL_LINES);
22 
23  // grille
24  grid.color(White());
25  for(int x= 0; x < n; x++)
26  {
27  float px= float(x) - float(n)/2 + .5f;
28  grid.vertex(Point(px, 0, - float(n)/2 + .5f));
29  grid.vertex(Point(px, 0, float(n)/2 - .5f));
30  }
31 
32  for(int z= 0; z < n; z++)
33  {
34  float pz= float(z) - float(n)/2 + .5f;
35  grid.vertex(Point(- float(n)/2 + .5f, 0, pz));
36  grid.vertex(Point(float(n)/2 - .5f, 0, pz));
37  }
38 
39  // axes XYZ
40  grid.color(Red());
41  grid.vertex(Point(0, .1, 0));
42  grid.vertex(Point(1, .1, 0));
43 
44  grid.color(Green());
45  grid.vertex(Point(0, .1, 0));
46  grid.vertex(Point(0, 1, 0));
47 
48  grid.color(Blue());
49  grid.vertex(Point(0, .1, 0));
50  grid.vertex(Point(0, .1, 1));
51 
52  glLineWidth(2);
53 
54  return grid;
55 }
56 
57 
58 class TP : public AppCamera
59 {
60 public:
61  // constructeur : donner les dimensions de l'image, version openGL + nombres de samples MSAA (ou 1)
62  TP( const int msaa= 1 ) : AppCamera(1024, 640, 4,3, msaa) {}
63 
64  // creation des objets de l'application
65  int init( )
66  {
67  // decrire un repere / grille
68  m_repere= make_grid(20);
69 
70  Point grid_pmin, grid_pmax;
71  m_repere.bounds(grid_pmin, grid_pmax);
72 
73  //~ m_objet= read_mesh("data/alpha.obj"); // utiliser alpha.glsl !!
74  m_objet= read_mesh("data/robot.obj"); // utiliser alpha_notexture.glsl !!
75 
76  Point pmin, pmax;
77  m_objet.bounds(pmin, pmax);
78 
79  pmin= min(pmin, grid_pmin);
80  pmax= max(pmax, grid_pmax);
81 
82  // parametrer la camera de l'application, renvoyee par la fonction camera()
83  camera().lookat(pmin, pmax);
84 
85  // charge les textures, si necessaire
86  Materials& materials= m_objet.materials();
87  m_textures.resize(materials.filename_count());
88  for(unsigned i= 0; i < m_textures.size(); i++)
89  m_textures[i]= read_texture(0, materials.filename(i));
90 
91  // affiche les matieres
92  {
93  for(int i= 0; i < materials.count(); i++)
94  {
95  const Material& material= materials.material(i);
96  printf("material[%d] '%s' kd %f %f %f ", i, materials.name(i), material.diffuse.r, material.diffuse.g, material.diffuse.b);
97  if(material.diffuse_texture != -1)
98  printf("texture '%s'", materials.filename(material.diffuse_texture));
99  printf("\n");
100  }
101  }
102 
103  m_groups= m_objet.groups();
104  m_vao= m_objet.create_buffers( /* texcoords */ true, /* normals */ true, /* color */ false, /* material index */ false);
105 
106  //
107  //~ m_program= read_program("tutos/alpha.glsl");
108  m_program= read_program("tutos/alpha_notexture.glsl");
109  program_print_errors(m_program);
110 
111  // etat openGL par defaut
112  glClearColor(0.2f, 0.2f, 0.2f, 1.f); // couleur par defaut de la fenetre
113 
114  glClearDepth(1.f); // profondeur par defaut
115  glDepthFunc(GL_LESS); // ztest, conserver l'intersection la plus proche de la camera
116  glEnable(GL_DEPTH_TEST); // activer le ztest
117 
118  glEnable(GL_MULTISAMPLE); // MSAA
119  glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE); // transformer la transparence en visibilite des samples MSAA
120 
121  return 0; // ras, pas d'erreur
122  }
123 
124  // destruction des objets de l'application
125  int quit( )
126  {
127  m_objet.release();
128  m_repere.release();
129  return 0;
130  }
131 
132  // dessiner une nouvelle image
133  int render( )
134  {
135  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
136 
137  // dessine aussi le repere, pour le meme point de vue
138  draw(m_repere, Identity(), camera());
139 
140  if(key_state('r'))
141  {
142  // recharge le shader a la volee...
143  clear_key_state('r');
144  //~ reload_program(m_program, "tutos/alpha.glsl");
145  reload_program(m_program, "tutos/alpha_notexture.glsl");
146  program_print_errors(m_program);
147  }
148 
149  glBindVertexArray(m_vao);
150  glUseProgram(m_program);
151 
152  Transform model= Identity();
153  Transform view= camera().view();
154  Transform projection= camera().projection();
155  Transform mv= view * model;
156  Transform mvp= projection * mv;
157 
158  program_uniform(m_program, "mvMatrix", mv);
159  program_uniform(m_program, "mvpMatrix", mvp);
160 
161  // dessine tous les groupes de triangles, tries par matiere
162  const Materials& materials= m_objet.materials();
163  for(unsigned i= 0; i < m_groups.size(); i++)
164  {
165  const Material& material= materials.material(m_groups[i].material_index);
166  if(material.diffuse_texture != -1)
167  program_use_texture(m_program, "alpha", 0, m_textures[material.diffuse_texture]);
168  else
169  program_use_texture(m_program, "alpha", 0, 0);
170 
171  //~ printf("group[%u] material %d first %d n %d, texture %d\n", i, m_groups[i].material_index, m_groups[i].first, m_groups[i].n, material.diffuse_texture);
172 
173  glDrawArrays(GL_TRIANGLES, m_groups[i].first, m_groups[i].n);
174  }
175 
176  if(key_state('s'))
177  {
178  clear_key_state('s');
179  static int id= 1;
180  screenshot("alpha", id++);
181  }
182 
183  // continuer...
184  return 1;
185  }
186 
187 protected:
188  Mesh m_objet;
189  Mesh m_repere;
190  GLuint m_vao;
191  GLuint m_program;
192  std::vector<GLuint> m_textures;
193  std::vector<TriangleGroup> m_groups;
194 };
195 
196 
197 int main( int argc, char **argv )
198 {
199  // il ne reste plus qu'a creer un objet application et la lancer
200  TP tp(8);
201  //~ TP tp(1);
202  tp.run();
203 
204  return 0;
205 }
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
representation d'un objet / maillage.
Definition: mesh.h:112
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:109
GLuint create_buffers(const bool use_texcoord, const bool use_normal, const bool use_color, const bool use_material_index)
construit les buffers et le vertex array object necessaires pour dessiner l'objet avec openGL....
Definition: mesh.cpp:579
std::vector< TriangleGroup > groups()
renvoie les groupes de triangles de meme matiere. re-organise les triangles. permet d'afficher l'obje...
Definition: mesh.cpp:303
void bounds(Point &pmin, Point &pmax) const
renvoie min et max les coordonnees des extremites des positions des sommets de l'objet (boite engloba...
Definition: mesh.cpp:501
void release()
detruit les objets openGL.
Definition: mesh.cpp:62
Mesh & color(const vec4 &c)
definit la couleur du prochain sommet.
Definition: mesh.cpp:78
const Materials & materials() const
renvoie la description des matieres.
Definition: mesh.cpp:265
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:47
Transform view() const
renvoie la transformation vue.
Definition: orbiter.cpp:40
Definition: alpha.cpp:59
int render()
a deriver pour afficher les objets. renvoie 1 pour continuer, 0 pour fermer l'application.
Definition: alpha.cpp:133
int quit()
a deriver pour detruire les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon.
Definition: alpha.cpp:125
int init()
a deriver pour creer les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon.
Definition: alpha.cpp:65
void clear_key_state(const SDL_Keycode key)
desactive une touche du clavier.
Definition: window.cpp:48
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:42
Color Red()
utilitaire. renvoie une couleur rouge.
Definition: color.cpp:41
Color Blue()
utilitaire. renvoie une couleur bleue.
Definition: color.cpp:51
Color Green()
utilitaire. renvoie une couleur verte.
Definition: color.cpp:46
Color White()
utilitaire. renvoie une couleur blanche.
Definition: color.cpp:36
Point max(const Point &a, const Point &b)
renvoie la plus grande composante de chaque point. x, y, z= max(a.x, b.x), max(a.y,...
Definition: vec.cpp:35
Transform Identity()
construit la transformation identite.
Definition: mat.cpp:187
Point min(const Point &a, const Point &b)
renvoie la plus petite composante de chaque point. x, y, z= min(a.x, b.x), min(a.y,...
Definition: vec.cpp:30
Mesh read_mesh(const char *filename)
charge un fichier wavefront .obj et renvoie un mesh compose de triangles non indexes....
Definition: wavefront.cpp:14
int screenshot(const char *filename)
enregistre le contenu de la fenetre dans un fichier. doit etre de type .png / .bmp
Definition: texture.cpp:194
GLuint read_program(const char *filename, const char *definitions)
Definition: program.cpp:204
void program_uniform(const GLuint program, const char *uniform, const std::vector< unsigned > &v)
affecte un tableau de valeurs a un uniform du shader program.
Definition: uniforms.cpp:94
int program_print_errors(const GLuint program)
affiche les erreurs de compilation.
Definition: program.cpp:432
void program_use_texture(const GLuint program, const char *uniform, const int unit, const GLuint texture, const GLuint sampler)
configure le pipeline et le shader program pour utiliser une texture, et des parametres de filtrage,...
Definition: uniforms.cpp:198
GLuint read_texture(const int unit, const char *filename, const GLenum texel_type)
Definition: texture.cpp:154
int diffuse_texture
indice de la texture de la couleur de base, ou -1.
Definition: materials.h:21
Color diffuse
couleur diffuse / de base.
Definition: materials.h:17
int filename_count() const
renvoie le nombre de noms de fichiers de textures.
Definition: materials.h:139
const Material & material(const int id) const
renvoie la ieme matiere.
Definition: materials.h:102
int count() const
nombre de matieres.
Definition: materials.h:94
const char * filename(const int id) const
renvoie le nombre de noms de fichiers de textures.
Definition: materials.h:141
const char * name(const int id) const
renvoie le nom de la ieme matiere.
Definition: materials.h:97
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