gKit2 light
Loading...
Searching...
No Matches
tuto_pad.cpp
Go to the documentation of this file.
1
2
4
5#include "mat.h"
6#include "wavefront.h"
7#include "texture.h"
8
9#include "orbiter.h"
10#include "draw.h"
11#include "app.h" // classe Application a deriver
12#include "app_camera.h" // gestion camera
13
14#include "gamepads.h"
15
16
17// utilitaire. creation d'une grille / repere.
18Mesh make_grid( )
19{
20 Mesh grid= Mesh(GL_LINES);
21
22 for(int x= 0; x < 10; x++)
23 {
24 float px= (float) x - 5.f + 0.5f;
25 grid.vertex(Point(px, 0, -4.5f));
26 grid.vertex(Point(px, 0, 4.5f));
27 }
28
29 for(int z= 0; z < 10; z++)
30 {
31 float pz= (float) z - 5.f + 0.5f;
32 grid.vertex(Point(-4.5f, 0, pz));
33 grid.vertex(Point(4.5f, 0, pz));
34 }
35
36 return grid;
37}
38
39class Pad : public App
40{
41public:
42 // constructeur : donner les dimensions de l'image, et eventuellement la version d'openGL.
43 Pad( ) : App(1024, 640) {}
44
45 int init( )
46 {
47 m_objet= read_mesh("data/cube.obj");
48 m_grid= make_grid();
49
50 Point pmin, pmax;
51 m_grid.bounds(pmin, pmax);
52 m_camera.lookat(pmin, pmax);
53
54 m_texture= read_texture(0, "data/debug2x2red.png");
55
56 // etat openGL par defaut
57 glClearColor(0.2f, 0.2f, 0.2f, 1.f); // couleur par defaut de la fenetre
58
59 glClearDepth(1.f); // profondeur par defaut
60 glDepthFunc(GL_LESS); // ztest, conserver l'intersection la plus proche de la camera
61 glEnable(GL_DEPTH_TEST); // activer le ztest
62
63 //
64 if(!m_gamepads.create())
65 return -1;
66
67 return 0; // ras, pas d'erreur
68 }
69
70 // destruction des objets de l'application
71 int quit( )
72 {
73 m_objet.release();
74 m_grid.release();
75 glDeleteTextures(1, &m_texture);
76
77 //
78 m_gamepads.release();
79 return 0;
80 }
81
82 int update( const float time, const float delta )
83 {
84 m_gamepads.update();
85
86 return 0;
87 }
88
89 // dessiner une nouvelle image
90 int render( )
91 {
92 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
93
94 // gestion mouvements camera
95 update_camera( m_camera );
96
97 static float position= 0;
98 //~ float speed= m_gamepads.axis(0, SDL_CONTROLLER_AXIS_TRIGGERLEFT);
99 float speed= m_gamepads.pad(0).axis(SDL_CONTROLLER_AXIS_TRIGGERLEFT);
100 position= position + speed;
101
102 static float rotation= 0;
103 //~ float angle= m_gamepads.axis(0, SDL_CONTROLLER_AXIS_RIGHTX);
104 float angle= m_gamepads.pad(0).axis(SDL_CONTROLLER_AXIS_RIGHTX);
105 rotation= rotation + angle;
106
107 m_model= Translation(0, 0, -position) * RotationY(-rotation) * RotationX(-global_time() / 4);
108
109 draw(m_grid, m_camera);
110 draw(m_objet, m_model, m_camera, m_texture);
111
112 // quitter
113 int stop= 1;
114 //~ if(m_gamepads.button(0, SDL_CONTROLLER_BUTTON_BACK))
115 if(m_gamepads.pad(0).button(SDL_CONTROLLER_BUTTON_BACK))
116 stop= 0;
117 return stop;
118 }
119
120protected:
121 Transform m_model;
122 Mesh m_objet;
123 Mesh m_grid;
124
125 GLuint m_texture;
126 Orbiter m_camera;
127 Gamepads m_gamepads;
128};
129
130
131int main( int argc, char **argv )
132{
133 Pad app;
134 app.run();
135
136 return 0;
137}
int run()
execution de l'application.
Definition app.cpp:36
App(const int width, const int height, const int major=3, const int minor=3)
constructeur, dimensions de la fenetre et version d'openGL.
Definition app.cpp:11
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
representation de la camera, type orbiter, placee sur une sphere autour du centre de l'objet.
Definition orbiter.h:17
int init()
a deriver pour creer les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon.
Definition tuto_pad.cpp:45
int render()
a deriver pour afficher les objets. renvoie 1 pour continuer, 0 pour fermer l'application.
Definition tuto_pad.cpp:90
int update(const float time, const float delta)
a deriver et redefinir pour animer les objets en fonction du temps.
Definition tuto_pad.cpp:82
int quit()
a deriver pour detruire les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon.
Definition tuto_pad.cpp:71
float global_time()
renvoie le temps ecoule depuis le lancement de l'application, en millisecondes.
Definition window.cpp:139
Transform RotationX(const float a)
renvoie la matrice representation une rotation de angle degree autour de l'axe X.
Definition mat.cpp:230
Transform RotationY(const float a)
renvoie la matrice representation une rotation de a degree autour de l'axe Y.
Definition mat.cpp:242
Transform Translation(const Vector &v)
renvoie la matrice representant une translation par un vecteur.
Definition mat.cpp:216
Mesh read_mesh(const char *filename)
charge un fichier wavefront .obj et renvoie un mesh compose de triangles non indexes....
Definition wavefront.cpp:14
GLuint read_texture(const int unit, const char *filename, const GLenum texel_type)
Definition texture.cpp:133
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