gKit2 light
object_tuto1.cpp
1 
2 #include "window.h"
3 
4 #include "vec.h"
5 #include "mat.h"
6 #include "color.h"
7 
8 #include "mesh.h"
9 #include "orbiter.h"
10 
11 #include "wavefront.h"
12 #include "draw.h"
13 
14 
15 Orbiter camera;
16 
17 
18 struct Object
19 {
20  Mesh mesh;
21  Transform model;
22  Transform dynamic;
23 
24  Object( ) : model(make_identity()), dynamic(make_identity()) {}
25 
26  Object( const Mesh& m, const Color& c ) : mesh(m), model(make_identity()), dynamic(make_identity())
27  {
28  mesh.color= c;
29  }
30 };
31 
32 void draw( Object& object, const Transform& view, const Transform& projection )
33 {
34  draw(object.mesh, object.model * object.dynamic, view, projection);
35 }
36 
37 void draw( Object& object, const Orbiter& orbiter )
38 {
39  draw(object.mesh, object.model * object.dynamic, orbiter);
40 }
41 
42 
43 std::vector<Object> objects;
44 Mesh grid;
45 
46 // utilitaire. creation d'une grille / repere.
47 Mesh make_grid( )
48 {
49  Mesh grid= create_mesh(GL_LINES);
50 
51  for(int x= 0; x < 10; x++)
52  {
53  float px= (float) x - 5.f + 0.5f;
54  push_vertex(grid, make_point(px, 0, -4.5f));
55  push_vertex(grid, make_point(px, 0, 4.5f));
56  }
57 
58  for(int z= 0; z < 10; z++)
59  {
60  float pz= (float) z - 5.f + 0.5f;
61  push_vertex(grid, make_point(-4.5f, 0, pz));
62  push_vertex(grid, make_point(4.5f, 0, pz));
63  }
64 
65  return grid;
66 }
67 
68 // creation des objets openGL
69 int init( )
70 {
71  // lire un maillage
72  Mesh cube= read_mesh("data/cube.obj");
73  if(cube == Mesh::error())
74  return -1;
75 
76  Object cube1(cube, make_red());
77  cube1.model= make_translation(-1, 0, 0);
78 
79  Object cube2(cube, make_blue());
80  cube2.model= make_translation(1, 0, 0);
81 
82  objects.push_back(cube1);
83  objects.push_back(cube2);
84 
85  camera= make_orbiter();
86  grid= make_grid();
87 
88  // etat par defaut
89  glClearColor(0.2f, 0.2f, 0.2f, 1); // couleur par defaut de la fenetre
90 
91  glClearDepthf(1); // profondeur par defaut
92  glDepthFunc(GL_LEQUAL); // ztest, conserver l'intersection la plus proche de la camera
93  glEnable(GL_DEPTH_TEST); // activer le ztest
94 
95  glLineWidth(2.5f); // epaisseur des lignes de la grille (pixels)
96 
97  return 0; // renvoyer 0 ras, pas d'erreur, sinon renvoyer -1
98 }
99 
100 // affichage
101 int draw( )
102 {
103  // on commence par effacer la fenetre avant de dessiner quelquechose
104  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
105  // on efface aussi le zbuffer
106 
107  // recupere les mouvements de la souris, utilise directement SDL2
108  int mx, my;
109  unsigned int mb= SDL_GetRelativeMouseState(&mx, &my);
110 
111  // deplace la camera
112  if(mb & SDL_BUTTON(1)) // le bouton gauche est enfonce
113  // tourne autour de l'objet
114  orbiter_rotation(camera, mx, my);
115 
116  else if(mb & SDL_BUTTON(3)) // le bouton droit est enfonce
117  // approche / eloigne l'objet
118  orbiter_move(camera, mx);
119 
120  else if(mb & SDL_BUTTON(2)) // le bouton du milieu est enfonce
121  // deplace le point de rotation
122  orbiter_translation(camera, (float) mx / (float) window_width(), (float) my / (float) window_height());
123 
124  // affiche la grille / repere
125  draw(grid, camera);
126 
127 #if 1
128  float time= (float) SDL_GetTicks();
129 
130  static float cube1x= 0;
131  if(key_state('q'))
132  cube1x-= 0.1f;
133  if(key_state('s'))
134  cube1x+= 0.1f;
135 
136  //~ objects[0].dynamic= make_translation(cube1x, 0, 0);
137  //~ objects[0].dynamic= make_rotationY(time / 16) * make_translation(cube1x, 0, 0);
138  objects[0].dynamic= make_translation(cube1x, 0, 0) * make_rotationY(time / 16);
139 
140  static float cube2z= 0;
141  if(key_state('r'))
142  cube2z-= 0.1f;
143  if(key_state('f'))
144  cube2z+= 0.1f;
145 
146  objects[1].dynamic= make_translation(0, 0, cube2z);
147 
148  // affiche les objets
149  for(unsigned int i= 0; i < objects.size(); i++)
150  draw(objects[i], camera);
151 #endif
152 
153  return 1; // on continue, renvoyer 0 pour sortir de l'application
154 }
155 
156 // destruction des objets openGL
157 int quit( )
158 {
159  return 0; // ras, pas d'erreur
160 }
161 
162 
163 int main( int argc, char **argv )
164 {
165  // etape 1 : creer la fenetre
166  Window window= create_window(1024, 640);
167  if(window == NULL)
168  return 1; // erreur lors de la creation de la fenetre ou de l'init de sdl2
169 
170  // etape 2 : creer un contexte opengl pour pouvoir dessiner
171  Context context= create_context(window);
172  if(context == NULL)
173  return 1; // erreur lors de la creation du contexte opengl
174 
175  // etape 3 : creation des objets
176  if(init() < 0)
177  {
178  printf("[error] init failed.\n");
179  return 1;
180  }
181 
182  // etape 4 : affichage de l'application, tant que la fenetre n'est pas fermee. ou que draw() ne renvoie pas 0
183  run(window, draw);
184 
185  // etape 5 : nettoyage
186  quit();
187  release_context(context);
188  release_window(window);
189  return 0;
190 }
Context create_context(Window window, const int major, const int minor)
cree et configure un contexte opengl
Definition: window.cpp:252
representation de la camera, type orbiter, placee sur une sphere autour du centre de l'objet...
Definition: orbiter.h:16
Mesh & color(const vec4 &c)
definit la couleur du prochain sommet.
Definition: mesh.cpp:37
representation d'un objet / maillage.
Definition: mesh.h:88
representation d'une couleur (rgba) transparente ou opaque.
Definition: color.h:13
void draw(Mesh &m, const Transform &model, const Transform &view, const Transform &projection, const GLuint texture)
applique une texture a la surface de l'objet. ne fonctionne que si les coordonnees de textures sont f...
Definition: draw.cpp:6
int window_width()
renvoie la largeur de la fenetre de l'application.
Definition: window.cpp:14
Window create_window(const int w, const int h)
creation d'une fenetre pour l'application.
Definition: window.cpp:186
static Mesh & error()
Definition: mesh.h:250
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:24
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 window_height()
renvoie la hauteur de la fenetre de l'application.
Definition: window.cpp:18
representation d'une transformation, une matrice 4x4, organisee par ligne / row major.
Definition: mat.h:20
void release_context(Context context)
detruit le contexte openGL.
Definition: window.cpp:306
void release_window(Window window)
destruction de la fenetre.
Definition: window.cpp:222
int run(Window window, int(*draw)(void))
fonction principale. gestion des evenements et appel de la fonction draw de l'application.
Mesh read_mesh(const char *filename)
charge un fichier wavefront .obj et renvoie un mesh compose de triangles non indexes. utiliser glDrawArrays pour l'afficher. a detruire avec Mesh::release( ).
Definition: wavefront.cpp:8