-
Notifications
You must be signed in to change notification settings - Fork 1
/
ViewerBasic.cpp
287 lines (218 loc) · 8.49 KB
/
ViewerBasic.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <ViewerBasic.h>
using namespace std;
ViewerBasic::ViewerBasic() : App(1024, 768), mb_cullface(true), mb_wireframe(false), b_draw_grid(true), b_draw_axe(true)
{
}
void ViewerBasic::help()
{
printf("HELP:\n");
printf("\th: help\n");
printf("\tc: (des)active GL_CULL_FACE\n");
printf("\tw: (des)active wireframe\n");
printf("\ta: (des)active l'affichage de l'axe\n");
printf("\tg: (des)active l'affichage de la grille\n");
printf("\tz: (des)active l'affichage de la courbe d'animation\n");
printf("\tfleches/pageUp/pageDown: bouge la caméra\n");
printf("\tCtrl+fleche/pageUp/pageDown: bouge la source de lumière\n");
printf("\tSouris+bouton gauche: rotation\n");
printf("\tSouris mouvement vertical+bouton droit: (de)zoom\n");
}
int ViewerBasic::init()
{
SDL_SetWindowTitle(m_window, "Chaan");
cout<<"==>ViewerBasic"<<endl;
int major = 0;
int minor = 0;
glGetIntegerv(GL_MAJOR_VERSION, &major);
glGetIntegerv(GL_MINOR_VERSION, &minor);
cout << "OpenGL version " << major << " " << minor << endl;
const GLubyte* txt;
txt = glGetString(GL_VENDOR);
if (txt) cout << "OpenGl Vendor "<< (const char*)txt<< endl;
txt = glGetString(GL_RENDERER);
if (txt) cout << "OpenGl Renderer " << (const char*)txt << endl;
txt = glGetString(GL_VERSION);
if (txt) cout << "OpenGl Version " << (const char*)txt << endl;
txt = glGetString(GL_SHADING_LANGUAGE_VERSION);
if (txt) cout << "OpenGl Shading Language Version " << (const char*)txt << endl;
// etat par defaut openGL
glClearColor(0.5f, 0.5f, 0.9f, 1);
// glClearDepthf(1);
//glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
// glFrontFace(GL_CCW);
// glCullFace(GL_BACK);
//
// if (mb_cullface)
// glEnable(GL_CULL_FACE);
// else
// glDisable(GL_CULL_FACE); // good for debug
// //glEnable(GL_TEXTURE_2D);
//
// //glEnable (GL_BLEND);
// //glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
m_camera.lookat( Point(0,0,0), 30 );
gl.light( Point(0, 20, 20), White() );
init_axe();
init_grid();
init_cube();
init_quad();
return 1;
}
void ViewerBasic::init_axe()
{
m_axe = Mesh(GL_LINES);
m_axe.color( Color(1, 0, 0));
m_axe.vertex( 0, 0, 0);
m_axe.vertex( 1, 0, 0);
m_axe.color( Color(0, 1, 0));
m_axe.vertex( 0, 0, 0);
m_axe.vertex( 0, 1, 0);
m_axe.color( Color( 0, 0, 1));
m_axe.vertex( 0, 0, 0);
m_axe.vertex( 0, 0, 1);
}
void ViewerBasic::init_grid()
{
m_grid = Mesh(GL_LINES);
m_grid.color( Color(1, 1, 1));
int i,j;
for(i=-5;i<=5;++i)
for(j=-5;j<=5;++j)
{
m_grid.vertex( -5, 0, j);
m_grid.vertex( 5, 0, j);
m_grid.vertex( i, 0, -5);
m_grid.vertex( i, 0, 5);
}
}
void ViewerBasic::init_cube()
{
// 0 1 2 3 4 5 6 7
static float pt[8][3] = { {-1,-1,-1}, {1,-1,-1}, {1,-1,1}, {-1,-1,1}, {-1,1,-1}, {1,1,-1}, {1,1,1}, {-1,1,1} };
static int f[6][4] = { {0,1,2,3}, {5,4,7,6}, {2,1,5,6}, {0,3,7,4}, {3,2,6,7}, {1,0,4,5} };
static float n[6][3] = { {0,-1,0}, {0,1,0}, {1,0,0}, {-1,0,0}, {0,0,1}, {0,0,-1} };
int i;
m_cube = Mesh(GL_TRIANGLE_STRIP);
m_cube.color( Color(1, 1, 1) );
for (i=0;i<6;i++)
{
m_cube.normal( n[i][0], n[i][1], n[i][2] );
m_cube.texcoord( 0,0 );
m_cube.vertex( pt[ f[i][0] ][0], pt[ f[i][0] ][1], pt[ f[i][0] ][2] );
m_cube.texcoord( 1,0);
m_cube.vertex( pt[ f[i][1] ][0], pt[ f[i][1] ][1], pt[ f[i][1] ][2] );
m_cube.texcoord(0,1);
m_cube.vertex(pt[ f[i][3] ][0], pt[ f[i][3] ][1], pt[ f[i][3] ][2] );
m_cube.texcoord(1,1);
m_cube.vertex( pt[ f[i][2] ][0], pt[ f[i][2] ][1], pt[ f[i][2] ][2] );
m_cube.restart_strip();
}
}
void ViewerBasic::init_quad()
{
m_quad = Mesh(GL_TRIANGLE_STRIP);
m_quad.color( Color(1, 1, 1));
m_quad.normal( 0, 0, 1 );
m_quad.texcoord(0,0 );
m_quad.vertex(-1, -1, 0 );
m_quad.texcoord(1,0);
m_quad.vertex( 1, -1, 0 );
m_quad.texcoord(0,1);
m_quad.vertex( -1, 1, 0 );
m_quad.texcoord( 1,1);
m_quad.vertex( 1, 1, 0 );
}
int ViewerBasic::render( )
{
// Efface l'ecran
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Deplace la camera, lumiere, etc.
manageCameraLight();
// donne notre camera au shader
gl.camera(m_camera);
return 1;
}
void ViewerBasic::draw_axe(const Transform& T)
{
gl.model(T);
gl.texture(0);
gl.lighting(false);
gl.draw(m_axe);
gl.lighting(true);
}
void ViewerBasic::draw_grid(const Transform& T)
{
gl.lighting(false);
gl.texture(0);
gl.model(T);
gl.draw(m_grid);
}
void ViewerBasic::draw_cube(const Transform& T)
{
gl.lighting(true);
gl.texture(0);
gl.model(T);
gl.draw(m_cube);
}
void ViewerBasic::draw_quad(const Transform& T)
{
gl.lighting(true);
gl.texture(0);
gl.model(T);
gl.draw(m_quad);
}
void ViewerBasic::manageCameraLight()
{
// recupere les mouvements de la souris pour deplacer la camera, cf tutos/tuto6.cpp
int mx, my;
unsigned int mb= SDL_GetRelativeMouseState(&mx, &my);
// deplace la camera
if((mb & SDL_BUTTON(1)) && (mb& SDL_BUTTON(3))) // le bouton du milieu est enfonce
m_camera.translation( (float) mx / (float) window_width(), (float) my / (float) window_height()); // deplace le point de rotation
else if (mb & SDL_BUTTON(1)) // le bouton gauche est enfonce
{
m_camera.rotation(mx, my); // tourne autour de l'objet
}
else if (mb & SDL_BUTTON(3)) // le bouton droit est enfonce
{
m_camera.move(my); // approche / eloigne l'objet
}
if (key_state(SDLK_PAGEUP) && (!key_state(SDLK_LCTRL)) && (!key_state(SDLK_LALT))) { m_camera.translation(0, 0.01); }
if (key_state(SDLK_PAGEDOWN) && (!key_state(SDLK_LCTRL)) && (!key_state(SDLK_LALT))) { m_camera.translation(0, -0.01); }
if (key_state(SDLK_LEFT) && (!key_state(SDLK_LCTRL)) && (!key_state(SDLK_LALT))) { m_camera.translation(0.01, 0); }
if (key_state(SDLK_RIGHT) && (!key_state(SDLK_LCTRL)) && (!key_state(SDLK_LALT))) { m_camera.translation(-0.01, 0); }
if (key_state(SDLK_UP) && (!key_state(SDLK_LCTRL)) && (!key_state(SDLK_LALT))) { m_camera.move(1); }
if (key_state(SDLK_DOWN) && (!key_state(SDLK_LCTRL)) && (!key_state(SDLK_LALT))) { m_camera.move(-1); }
// Deplace la lumiere
const float step = m_camera.radius()*0.005f;
if (key_state(SDLK_RIGHT) && key_state(SDLK_LCTRL)) { gl.light( gl.light()+Vector(step,0,0)); }
if (key_state(SDLK_LEFT) && key_state(SDLK_LCTRL)) { gl.light( gl.light()+Vector(-step,0,0)); }
if (key_state(SDLK_UP) && key_state(SDLK_LCTRL)) { gl.light( gl.light()+Vector(0,0,-step)); }
if (key_state(SDLK_DOWN) && key_state(SDLK_LCTRL)) { gl.light( gl.light()+Vector(0,0,step)); }
if (key_state(SDLK_PAGEUP) && key_state(SDLK_LCTRL)) { gl.light( gl.light()+Vector(0,step,0)); }
if (key_state(SDLK_PAGEDOWN) && key_state(SDLK_LCTRL)) { gl.light( gl.light()+Vector(0,-step,0)); }
// (De)Active la grille / les axes
if (key_state('h')) help();
if (key_state('c')) { clear_key_state('c'); mb_cullface=!mb_cullface; if (mb_cullface) glEnable(GL_CULL_FACE);else glDisable(GL_CULL_FACE); }
if (key_state('w')) { clear_key_state('w'); mb_wireframe=!mb_wireframe; if (mb_wireframe) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); }
if (key_state('g')) { b_draw_grid = !b_draw_grid; clear_key_state('g'); }
if (key_state('a')) { b_draw_axe = !b_draw_axe; clear_key_state('a'); }
gl.camera(m_camera);
//draw(cube, Translation( Vector( gl.light()))*Scale(0.3, 0.3, 0.3), camera);
//draw_param.texture(quad_texture).camera(camera).model(Translation( 3, 5, 0 )).draw(quad);
// AXE et GRILLE
gl.model( Scale(10.*step,10.0*step,10.0*step) );
if (b_draw_grid) gl.draw(m_grid);
if (b_draw_axe) gl.draw(m_axe);
// LIGHT
gl.texture( 0 );
gl.lighting(false);
gl.model(Translation(Vector(gl.light()))*Scale(step, step, step));
gl.draw(m_cube);
gl.lighting(true);
}