gKit2 light
draw.h
Go to the documentation of this file.
1 
2 #ifndef _DRAW_H
3 #define _DRAW_H
4 
5 #include "mesh.h"
6 #include "orbiter.h"
7 #include "window.h"
8 #include "program.h"
9 
10 
12 
14 
16 void draw( Mesh& m, const Transform& model, const Transform& view, const Transform& projection );
18 void draw( Mesh& m, const Transform& model, const Transform& view, const Transform& projection, const GLuint texture );
19 
21 void draw( Mesh& m, Orbiter& camera );
23 void draw( Mesh& m, const Transform& model, Orbiter& camera );
24 
26 void draw( Mesh& m, Orbiter& camera, const GLuint texture );
28 void draw( Mesh& m, const Transform& model, Orbiter& camera, const GLuint texture );
29 
31 
32 void draw( const TriangleGroup& group, Mesh& mesh, Orbiter& camera );
35 void draw( const TriangleGroup& group, Mesh& mesh, const Transform& model, Orbiter& camera );
37 void draw( const TriangleGroup& group, Mesh& mesh, const Transform& model, const Transform& view, const Transform& projection );
38 
40 void draw( const TriangleGroup& group, Mesh& mesh, Orbiter& camera, const GLuint texture );
42 void draw( const TriangleGroup& group, Mesh& mesh, const Transform& model, Orbiter& camera, const GLuint texture );
44 void draw( const TriangleGroup& group, Mesh& mesh, const Transform& model, const Transform& view, const Transform& projection, const GLuint texture );
46 
66 class DrawParam
67 {
68 public:
70  DrawParam( ) : m_model(), m_view(), m_projection(),
71  m_use_light(false), m_light(), m_light_color(),
72  m_use_texture(false), m_texture(0),
73  m_use_alpha_test(false), m_alpha_min(0.3f),
74  m_debug_normals(false), m_normals_scale(1),
75  m_debug_texcoords(false)
76  {}
77 
79  DrawParam& model( const Transform& m ) { m_model= m; return *this; }
81  DrawParam& view( const Transform& m ) { m_view= m; return *this; }
83  DrawParam& projection( const Transform& m ) { m_projection= m; return *this; }
84 
86  DrawParam& camera( Orbiter& o ) { m_view= o.view(); m_projection= o.projection(); return *this; }
88  DrawParam& camera( Orbiter& o, const int width, const int height, const float fov ) { m_view= o.view(); m_projection= o.projection(width, height, fov); return *this; }
90  DrawParam& light( const Point& p, const Color& c= White() ) { m_use_light= true; m_light= p; m_light_color=c; return *this; }
92  DrawParam& texture( const GLuint t ) { m_use_texture= true; m_texture= t; return *this; }
93 
95  DrawParam& alpha_texture( const GLuint t, const float a= 0.5f ) { m_use_alpha_test= (a > 0); m_alpha_min= a; m_use_texture= true; m_texture= t; return *this; }
96 
97 // a virer
99  DrawParam& lighting( const bool use_light= true ) { m_use_light= use_light; return *this; }
101  const Point& light() const { return m_light; }
102 // les params sont configures une fois par groupe d'options, pas a chaque draw. l'idee est de dessiner tous les objets utilisant la meme config ensemble / en suivant, les uns apres les autres.
103 
105  DrawParam& debug_normals( const float s= 1 ) { m_debug_normals= true; m_normals_scale= s; return *this;}
107  DrawParam& debug_texcoords( ) { m_debug_texcoords= true; return *this;}
108 
110  void draw( Mesh& mesh );
111  void draw( const TriangleGroup& group, Mesh& mesh );
112 
113 protected:
121  GLuint create_program( const GLenum primitives, const bool use_texcoord, const bool use_normal, const bool use_color, const bool use_light, const bool use_alpha_test );
122  GLuint create_debug_normals_program( const GLenum primitives, const bool use_texcoord, const bool use_normal, const bool use_color, const bool use_light, const bool use_alpha_test );
123  GLuint create_debug_texcoords_program( const GLenum primitives, const bool use_texcoord, const bool use_normal, const bool use_color, const bool use_light, const bool use_alpha_test );
124 
125  Transform m_model;
126  Transform m_view;
127  Transform m_projection;
128 
129  bool m_use_light;
130  Point m_light;
131  Color m_light_color;
132 
133  bool m_use_texture;
134  GLuint m_texture;
135 
136  bool m_use_alpha_test;
137  float m_alpha_min;
138 
139  bool m_debug_normals;
140  float m_normals_scale;
141  bool m_debug_texcoords;
142 };
143 
144 
145 void draw( Mesh& m, DrawParam& param );
146 // \todo void draw( Mesh& m, const Transform& model, DrawParam& param );
147 // drawparams = parametres globaux, camera, light, texture, debug, etc...
148 
151 {
152  std::string filename;
153  std::string definitions;
154  GLuint program;
155 };
156 
159 {
160 public:
161  ~PipelineCache( )
162  {
163  printf("[pipeline cache] %d programs.\n", int(m_programs.size()));
164  // le contexte est deja detruit lorsque ce destructeur est appelle...
165  // trop tard pour detruire les programs et les shaders...
166  }
167 
169  PipelineProgram *find( const char *filename, const char *definitions= "" )
170  {
171  // todo peut etre remplacer par une unordered_map ? mais il y a peu de shaders utilise normalement...
172  for(auto program : m_programs)
173  {
174  if(program->filename == filename && program->definitions == definitions)
175  return program;
176  }
177 
178  // cree le programme s'il n'existe pas deja...
179  GLuint p= read_program(filename, definitions);
181 
182  PipelineProgram *program= new PipelineProgram {filename, definitions, p};
183  m_programs.push_back(program);
184  return program;
185  }
186 
188  static PipelineCache& manager( )
189  {
190  static PipelineCache cache;
191  return cache;
192  }
193 
194 protected:
196  PipelineCache( ) : m_programs() {}
197 
198  std::vector<PipelineProgram *> m_programs;
199 };
200 
201 
202 #endif
Definition: draw.h:67
DrawParam & debug_texcoords()
visualise les coordonnees de textures des sommets des triangles.
Definition: draw.h:107
DrawParam & light(const Point &p, const Color &c=White())
eclaire l'objet avec une source ponctuelle, de position p et de couleur c.
Definition: draw.h:90
DrawParam & camera(Orbiter &o)
utilise les transformations view et projection definies par une camera.
Definition: draw.h:86
DrawParam & lighting(const bool use_light=true)
utilise une source de lumire pour eclairer l'objet, ou pas si use_light= false.
Definition: draw.h:99
DrawParam & view(const Transform &m)
modifie la transformation view utilisee pour afficher l'objet.
Definition: draw.h:81
DrawParam & model(const Transform &m)
modifie la transformation model utilisee pour afficher l'objet.
Definition: draw.h:79
DrawParam()
constructeur par defaut.
Definition: draw.h:70
DrawParam & debug_normals(const float s=1)
visualise les normales des sommets des triangles et les normales geometrique des triangles
Definition: draw.h:105
DrawParam & alpha_texture(const GLuint t, const float a=0.5f)
utilise une texture semi transparente, si l'alpha du texel est plus petit que a, le pixel est transpa...
Definition: draw.h:95
void draw(Mesh &mesh)
dessine l'objet avec l'ensemble des parametres definis.
Definition: draw.cpp:157
DrawParam & projection(const Transform &m)
modifie la transformation projection utilisee pour afficher l'objet.
Definition: draw.h:83
DrawParam & camera(Orbiter &o, const int width, const int height, const float fov)
utilise les transformations view et projection definies par une camera. parametres explicites de la p...
Definition: draw.h:88
const Point & light() const
renvoie la position de la lumière.
Definition: draw.h:101
GLuint create_program(const GLenum primitives, const bool use_texcoord, const bool use_normal, const bool use_color, const bool use_light, const bool use_alpha_test)
Definition: draw.cpp:112
DrawParam & texture(const GLuint t)
plaque une texture opaque a la surface de l'objet.
Definition: draw.h:92
representation d'un objet / maillage.
Definition: mesh.h:112
representation de la camera, type orbiter, placee sur une sphere autour du centre de l'objet.
Definition: orbiter.h:17
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
ensemble de shader programs compiles. singleton.
Definition: draw.h:159
static PipelineCache & manager()
access au singleton.
Definition: draw.h:188
PipelineProgram * find(const char *filename, const char *definitions="")
renvoie un shader program compile.
Definition: draw.h:169
PipelineCache()
constructeur prive.
Definition: draw.h:196
void draw(Mesh &m, const Transform &model, const Transform &view, const Transform &projection)
dessine l'objet avec les transformations model, vue et projection.
Definition: draw.cpp:33
description d'un shader program compile.
Definition: draw.h:151
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
Color White()
utilitaire. renvoie une couleur blanche.
Definition: color.cpp:36
representation d'un ensemble de triangles de meme matiere.
Definition: mesh.h:103
GLuint read_program(const char *filename, const char *definitions)
Definition: program.cpp:204
int program_print_errors(const GLuint program)
affiche les erreurs de compilation.
Definition: program.cpp:432
representation d'une couleur (rgba) transparente ou opaque.
Definition: color.h:14
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