forked from nigels-com/glui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
viewmodel.cpp
357 lines (271 loc) · 6.74 KB
/
viewmodel.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
viewmodel.cpp
GLUI User Interface Toolkit
Copyright (c) 1998 Paul Rademacher
WWW: https://github.com/libglui/glui
Issues: https://github.com/libglui/glui/issues
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
/***********************************************************************
1996, Paul Rademacher ([email protected])
Oct 2003, Nigel Stewart - GLUI Code Cleaning
************************************************************************/
#include "viewmodel.h"
#include "GL/glui.h"
#include <cstdio>
#include <cstdlib>
void
ViewModel::set_distance(const float new_distance)
{
if ( new_distance <= 0.0 ) /* Distance has to be positive */
return;
/* We find the current forward vector */
forward = lookat - eye;
forward.normalize();
/* Set distance */
distance = new_distance;
/* Find new eye point */
eye = lookat - forward * distance;
}
void
ViewModel::set_up(const vec3 &new_up)
{
up = new_up;
update();
}
void
ViewModel::set_up(const float x, const float y, const float z)
{
set_up(vec3(x,y,z));
}
void
ViewModel::set_eye(const vec3 &new_eye)
{
eye = new_eye;
update();
}
void
ViewModel::set_eye(const float x, const float y, const float z)
{
set_eye(vec3(x,y,z));
}
void
ViewModel::set_lookat(const vec3 &new_lookat)
{
lookat = new_lookat;
update();
}
void
ViewModel::set_lookat(const float x, const float y, const float z)
{
set_lookat(vec3(x,y,z));
}
void
ViewModel::roll(const float angle)
{
mat4 rot = rotation3D( forward, angle );
up = rot * up;
update();
}
void
ViewModel::eye_yaw(const float angle)
{
vec3 eye_pt = eye - lookat; /* eye w/lookat at center */
mat4 rot = rotation3D( up, angle );
eye_pt = rot * eye_pt;
eye = lookat + eye_pt;
update();
}
void
ViewModel::eye_yaw_abs(const float angle, const vec3 &axis)
{
vec3 eye_pt = eye - lookat; /* eye w/lookat at center */
mat4 rot = rotation3D( axis, angle );
eye_pt = rot * eye_pt;
eye = lookat + eye_pt;
up = rot * up;
update();
}
void
ViewModel::eye_pitch(const float angle)
{
vec3 eye_pt = eye - lookat; /* eye w/lookat at center */
mat4 rot = rotation3D( side, angle );
eye_pt = rot * eye_pt;
eye = lookat + eye_pt;
up = rot * up;
update();
}
void
ViewModel::lookat_yaw(const float angle)
{
vec3 lookat_pt = lookat - eye; /* lookat w/eye at center */
mat4 rot = rotation3D( up, -angle );
lookat_pt = rot * lookat_pt;
lookat = eye + lookat_pt;
update();
}
void
ViewModel::lookat_pitch(const float angle)
{
vec3 lookat_pt = lookat - eye; /* lookat w/eye at center */
mat4 rot = rotation3D( side, -angle );
lookat_pt = rot * lookat_pt;
lookat = eye + lookat_pt;
up = rot * up;
update();
}
void
ViewModel::reset_up(const int axis_num)
{
float eye_distance = (lookat - eye).length();
eye[axis_num] = lookat[axis_num];
/* Bring eye to same level as lookat */
vec3 vector = eye - lookat;
vector.normalize();
vector *= eye_distance;
eye = lookat + vector;
up.set( 0.0, 0.0, 0.0 );
up[axis_num] = 1.0;
update();
}
void
ViewModel::reset_up()
{
reset_up( VY ); /* Resets to the Y axis */
}
void
ViewModel::move(const float side_move, const float up_move, const float forw_move)
{
eye += forward * forw_move;
eye += side * side_move;
eye += up * up_move;
lookat += forward * forw_move;
lookat += side * side_move;
lookat += up * up_move;
update();
}
void
ViewModel::move(const vec3 &v) /* A vector version of the above command */
{
move( v[VX], v[VY], v[VZ] );
}
void
ViewModel::move_by_eye(const vec3 &new_eye)
{
vec3 diff = new_eye - eye;
eye += diff;
lookat += diff;
update();
}
void
ViewModel::move_by_lookat(const vec3 &new_lookat)
{
vec3 diff = new_lookat - lookat;
lookat += diff;
eye += diff;
update();
}
void
ViewModel::move_abs(const vec3 &v)
{
eye += v;
lookat += v;
update();
}
void
ViewModel::rot_about_eye(const mat4 &rot)
{
vec3 view = lookat - eye;
view = rot * view;
up = rot * up;
lookat = eye + view;
update();
}
void
ViewModel::rot_about_lookat(const mat4 &rot)
{
// NOT QUITE RIGHT YET
vec3 view = eye - lookat;
view = rot * view;
up = rot * up;
eye = lookat + view;
update();
}
void
ViewModel::make_mtx()
{
update();
mtx[0][0]=side[VX]; mtx[0][1]=up[VX]; mtx[0][2]=forward[VX]; mtx[0][3]=0.0;
mtx[1][0]=side[VY]; mtx[1][1]=up[VY]; mtx[1][2]=forward[VY]; mtx[1][3]=0.0;
mtx[2][0]=side[VZ]; mtx[2][1]=up[VZ]; mtx[2][2]=forward[VZ]; mtx[2][3]=0.0;
mtx[3][0]=0.0; mtx[3][1]=0.0; mtx[3][2]= 0.0; mtx[3][3]=1.0;
}
void
ViewModel::load_to_openGL()
{
mat4 m;
make_mtx();
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glMultMatrixf( (float*) &mtx[0][0]);
glTranslatef( -eye[VX], -eye[VY], -eye[VZ] );
}
void
ViewModel::load_to_openGL_noident()
{
mat4 m;
make_mtx();
glMatrixMode( GL_MODELVIEW );
glMultMatrixf( (float*) &mtx[0][0]);
glTranslatef( -eye[VX], -eye[VY], -eye[VZ] );
}
void
ViewModel::reset()
{
up.set( 0.0, 1.0, 0.0 );
eye.set(0.0,0.0,10.0);
lookat.set(0.0,0.0,0.0);
mtx = identity3D();
update();
}
ViewModel::ViewModel()
{
reset();
}
void
ViewModel::update()
{
/* get proper side and forward vectors, and distance */
forward = -(lookat - eye);
distance = forward.length();
forward /= distance;
side = up ^ forward;
up = forward ^ side;
forward.normalize();
up.normalize();
side.normalize();
}
void
ViewModel::dump(FILE *output) const
{
fprintf( output, "Viewmodel: \n" );
eye.print( output, " eye" );
lookat.print( output, " lookat" );
up.print( output, " up" );
side.print( output, " side" );
forward.print(output, " forward");
mtx.print( output, " mtx" );
}