17Mesh make_grid(
const int n= 10 )
24 float w= float(n-1) / 2;
25 for(
int x= 0; x < n; x++)
31 for(
int z= 0; z < n; z++)
54Mesh make_ground(
const int n= 10 )
59 int a= grid.
vertex(-n/2, -1, n/2);
60 int b= grid.
vertex( n/2, -1, n/2);
61 int c= grid.
vertex( n/2, -1, -n/2);
62 int d= grid.
vertex(-n/2, -1, -n/2);
76 camera.vertex(
Point(0, 0, 0));
77 camera.vertex(
Point(1, 0, 0));
79 camera.color(
Green());
80 camera.vertex(
Point(0, 0, 0));
81 camera.vertex(
Point(0, 1, 0));
84 camera.vertex(
Point(0, 0, 0));
85 camera.vertex(
Point(0, 0, 1));
97 camera.vertex(-1, -1, -1);
98 camera.vertex(-1, 1, -1);
99 camera.vertex(-1, 1, -1);
100 camera.vertex(1, 1, -1);
101 camera.vertex(1, 1, -1);
102 camera.vertex(1, -1, -1);
103 camera.vertex(1, -1, -1);
104 camera.vertex(-1, -1, -1);
107 camera.vertex(-1, -1, 1);
108 camera.vertex(-1, 1, 1);
109 camera.vertex(-1, 1, 1);
110 camera.vertex(1, 1, 1);
111 camera.vertex(1, 1, 1);
112 camera.vertex(1, -1, 1);
113 camera.vertex(1, -1, 1);
114 camera.vertex(-1, -1, 1);
117 camera.vertex(-1, -1, -1);
118 camera.vertex(-1, -1, 1);
119 camera.vertex(-1, 1, -1);
120 camera.vertex(-1, 1, 1);
121 camera.vertex(1, 1, -1);
122 camera.vertex(1, 1, 1);
123 camera.vertex(1, -1, -1);
124 camera.vertex(1, -1, 1);
139 m_repere= make_grid(20);
140 m_local= make_grid(2);
141 m_ground= make_ground(20);
144 m_frustum= make_frustum();
149 m_decal_program=
read_program(
"gkit2_tutos/draw_decal.glsl");
152 m_shadow_program=
read_program(
"gkit2_tutos/decal.glsl");
161 m_ground.bounds(pmin, pmax);
166 m_framebuffer.create(1024, 1024);
167 m_framebuffer.clear_color(
White());
168 m_framebuffer.clear_depth(1);
171 glClearColor(0.2f, 0.2f, 0.2f, 1.f);
174 glDepthFunc(GL_LESS);
175 glEnable(GL_DEPTH_TEST);
191 m_framebuffer.release();
198 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
223 Transform decal_projection=
Ortho(-2, 2, -2, 2,
float(0.1),
float(10));
233 projection= decal_projection;
237 draw(m_objet, m_position, view, projection);
239 draw(m_local, m_position, view, projection);
242 draw(m_proxy, m_position * m, view, projection);
249 draw(m_frustum, decal_m, view, projection);
255 draw(m_ground,
Identity(), view, projection);
260 m_framebuffer.bind(m_decal_program,
true,
true,
false,
false,
false,
false);
262 glUseProgram(m_decal_program);
268 program_uniform(m_decal_program,
"mvpMatrix", mvp);
270 m_objet.draw(m_decal_program);
277 glUseProgram(m_shadow_program);
284 program_uniform(m_shadow_program,
"mvpMatrix", mvp);
285 program_uniform(m_shadow_program,
"mvMatrix", mv);
290 Transform decal= decal_viewport * decal_projection * decal_view;
292 program_uniform(m_shadow_program,
"decalMatrix", decal);
295 m_framebuffer.use_color_texture(m_shadow_program,
"decal", 0);
299 m_ground.draw(m_shadow_program);
303 m_framebuffer.unbind_textures();
308 m_framebuffer.blit_color(0, 0, 256, 256);
335 GLuint m_decal_program;
336 GLuint m_shadow_program;
340int main(
int argc,
char **argv )
const Orbiter & camera() const
renvoie l'orbiter gere par l'application.
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.
int run()
execution de l'application.
representation d'un objet / maillage.
unsigned int vertex(const vec3 &p)
insere un sommet de position p, et ses attributs (s'ils sont definis par color(), texcoord(),...
Mesh & triangle(const unsigned int a, const unsigned int b, const unsigned int c)
Mesh & normal(const vec3 &n)
definit la normale du prochain sommet.
Mesh & color(const vec4 &c)
definit la couleur du prochain sommet.
void lookat(const Point ¢er, const float size)
observe le point center a une distance size.
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...
Transform view() const
renvoie la transformation vue.
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.
int window_height()
renvoie la hauteur de la fenetre de l'application.
void clear_key_state(const SDL_Keycode key)
desactive une touche du clavier.
int key_state(const SDL_Keycode key)
renvoie l'etat d'une touche du clavier. cf la doc SDL2 pour les codes.
int window_width()
renvoie la largeur de la fenetre de l'application.
Color Red()
utilitaire. renvoie une couleur rouge.
Color Yellow()
utilitaire. renvoie une couleur jaune.
Color Blue()
utilitaire. renvoie une couleur bleue.
Color Green()
utilitaire. renvoie une couleur verte.
Color White()
utilitaire. renvoie une couleur blanche.
Transform Inverse(const Transform &m)
renvoie l'inverse de la matrice.
Transform Viewport(const float width, const float height)
renvoie la matrice representant une transformation viewport.
Transform Identity()
construit la transformation identite.
Transform RotationX(const float a)
renvoie la matrice representation une rotation de angle degree autour de l'axe X.
Transform RotationY(const float a)
renvoie la matrice representation une rotation de a degree autour de l'axe Y.
Transform Ortho(const float left, const float right, const float bottom, const float top, const float znear, const float zfar)
renvoie la matrice representant une transformation orthographique, passage d'un cube []x[]x[] vers [-...
Transform Translation(const Vector &v)
renvoie la matrice representant une translation par un vecteur.
Mesh read_mesh(const char *filename)
charge un fichier wavefront .obj et renvoie un mesh compose de triangles non indexes....
int screenshot(const char *filename)
enregistre le contenu de la fenetre dans un fichier. doit etre de type .png / .bmp
GLuint read_program(const char *filename, const char *definitions)
int program_print_errors(const GLuint program)
affiche les erreurs de compilation.
int release_program(const GLuint program)
detruit les shaders et le program.
representation d'un point 3d.