]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/camwindow.cpp
Merge commit '54a2bda443aace9c00a1615af10cc1dc8b1f0cd1' into master-merge
[xonotic/netradiant.git] / radiant / camwindow.cpp
1 /*
2    Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3    For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 //
23 // Camera Window
24 //
25 // Leonardo Zide (leo@lokigames.com)
26 //
27
28 #include "camwindow.h"
29
30 #include <gtk/gtk.h>
31 #include <gdk/gdkkeysyms.h>
32
33 #include "debugging/debugging.h"
34
35 #include "iscenegraph.h"
36 #include "irender.h"
37 #include "igl.h"
38 #include "icamera.h"
39 #include "cullable.h"
40 #include "renderable.h"
41 #include "preferencesystem.h"
42
43 #include "signal/signal.h"
44 #include "container/array.h"
45 #include "scenelib.h"
46 #include "render.h"
47 #include "cmdlib.h"
48 #include "math/frustum.h"
49
50 #include "gtkutil/widget.h"
51 #include "gtkutil/button.h"
52 #include "gtkutil/toolbar.h"
53 #include "gtkutil/glwidget.h"
54 #include "gtkutil/xorrectangle.h"
55 #include "gtkmisc.h"
56 #include "selection.h"
57 #include "mainframe.h"
58 #include "preferences.h"
59 #include "commands.h"
60 #include "xywindow.h"
61 #include "windowobservers.h"
62 #include "renderstate.h"
63
64 #include "timer.h"
65
66 Signal0 g_cameraMoved_callbacks;
67
68 void AddCameraMovedCallback( const SignalHandler& handler ){
69         g_cameraMoved_callbacks.connectLast( handler );
70 }
71
72 void CameraMovedNotify(){
73         g_cameraMoved_callbacks();
74 }
75
76
77 struct camwindow_globals_private_t
78 {
79         int m_nFOV;
80         int m_nMoveSpeed;
81         bool m_bCamLinkSpeed;
82         int m_nAngleSpeed;
83         bool m_bCamInverseMouse;
84         bool m_bCamDiscrete;
85         bool m_bCubicClipping;
86         bool m_showStats;
87         int m_nStrafeMode;
88
89         camwindow_globals_private_t() :
90                 m_nFOV( 110 ),
91                 m_nMoveSpeed( 100 ),
92                 m_bCamLinkSpeed( true ),
93                 m_nAngleSpeed( 3 ),
94                 m_bCamInverseMouse( false ),
95                 m_bCamDiscrete( true ),
96                 m_bCubicClipping( false ),
97                 m_showStats( true ),
98                 m_nStrafeMode( 3 ){
99         }
100
101 };
102
103 camwindow_globals_private_t g_camwindow_globals_private;
104
105
106 const Matrix4 g_opengl2radiant(
107         0, 0,-1, 0,
108         -1, 0, 0, 0,
109         0, 1, 0, 0,
110         0, 0, 0, 1
111         );
112
113 const Matrix4 g_radiant2opengl(
114         0,-1, 0, 0,
115         0, 0, 1, 0,
116         -1, 0, 0, 0,
117         0, 0, 0, 1
118         );
119
120 struct camera_t;
121 void Camera_mouseMove( camera_t& camera, int x, int y );
122
123 enum camera_draw_mode
124 {
125         cd_wire,
126         cd_solid,
127         cd_texture,
128         cd_lighting
129 };
130
131 struct camera_t
132 {
133         int width, height;
134
135         bool timing;
136
137         Vector3 origin;
138         Vector3 angles;
139
140         Vector3 color; // background
141
142         Vector3 forward, right; // move matrix (TTimo: used to have up but it was not updated)
143         Vector3 vup, vpn, vright; // view matrix (taken from the modelview matrix)
144
145         Matrix4 projection;
146         Matrix4 modelview;
147
148         bool m_strafe; // true when in strafemode toggled by the ctrl-key
149         bool m_strafe_forward; // true when in strafemode by ctrl-key and shift is pressed for forward strafing
150         bool m_strafe_forward_invert; //silly option to invert forward strafing to support old fegs
151
152         unsigned int movementflags; // movement flags
153         Timer m_keycontrol_timer;
154         guint m_keymove_handler;
155
156
157         DeferredMotionDelta m_mouseMove;
158
159         static void motionDelta( int x, int y, void* data ){
160                 Camera_mouseMove( *reinterpret_cast<camera_t*>( data ), x, y );
161         }
162
163         View* m_view;
164         Callback<void()> m_update;
165
166         static camera_draw_mode draw_mode;
167
168         camera_t( View* view, const Callback<void()>& update )
169                 : width( 0 ),
170                 height( 0 ),
171                 timing( false ),
172                 origin( 0, 0, 0 ),
173                 angles( 0, 0, 0 ),
174                 color( 0, 0, 0 ),
175                 projection( g_matrix4_identity ),
176                 modelview( g_matrix4_identity ),
177                 movementflags( 0 ),
178                 m_keycontrol_timer(),
179                 m_keymove_handler( 0 ),
180                 m_mouseMove( motionDelta, this ),
181                 m_view( view ),
182                 m_update( update ){
183         }
184 };
185
186 camera_draw_mode camera_t::draw_mode = cd_texture;
187
188 inline Matrix4 projection_for_camera( float near_z, float far_z, float fieldOfView, int width, int height ){
189         const float half_width = static_cast<float>( near_z * tan( degrees_to_radians( fieldOfView * 0.5 ) ) );
190         const float half_height = half_width * ( static_cast<float>( height ) / static_cast<float>( width ) );
191
192         return matrix4_frustum(
193                            -half_width,
194                            half_width,
195                            -half_height,
196                            half_height,
197                            near_z,
198                            far_z
199                            );
200 }
201
202 float Camera_getFarClipPlane( camera_t& camera ){
203         return ( g_camwindow_globals_private.m_bCubicClipping ) ? pow( 2.0, ( g_camwindow_globals.m_nCubicScale + 7 ) / 2.0 ) : 32768.0f;
204 }
205
206 void Camera_updateProjection( camera_t& camera ){
207         float farClip = Camera_getFarClipPlane( camera );
208         camera.projection = projection_for_camera( farClip / 4096.0f, farClip, (float)g_camwindow_globals_private.m_nFOV, camera.width, camera.height );
209
210         camera.m_view->Construct( camera.projection, camera.modelview, camera.width, camera.height );
211 }
212
213 void Camera_updateVectors( camera_t& camera ){
214         for ( int i = 0 ; i < 3 ; i++ )
215         {
216                 camera.vright[i] = camera.modelview[( i << 2 ) + 0];
217                 camera.vup[i] = camera.modelview[( i << 2 ) + 1];
218                 camera.vpn[i] = camera.modelview[( i << 2 ) + 2];
219         }
220 }
221
222 void Camera_updateModelview( camera_t& camera ){
223         camera.modelview = g_matrix4_identity;
224
225         // roll, pitch, yaw
226         Vector3 radiant_eulerXYZ( 0, -camera.angles[CAMERA_PITCH], camera.angles[CAMERA_YAW] );
227
228         matrix4_translate_by_vec3( camera.modelview, camera.origin );
229         matrix4_rotate_by_euler_xyz_degrees( camera.modelview, radiant_eulerXYZ );
230         matrix4_multiply_by_matrix4( camera.modelview, g_radiant2opengl );
231         matrix4_affine_invert( camera.modelview );
232
233         Camera_updateVectors( camera );
234
235         camera.m_view->Construct( camera.projection, camera.modelview, camera.width, camera.height );
236 }
237
238
239 void Camera_Move_updateAxes( camera_t& camera ){
240         double ya = degrees_to_radians( camera.angles[CAMERA_YAW] );
241
242         // the movement matrix is kept 2d
243         camera.forward[0] = static_cast<float>( cos( ya ) );
244         camera.forward[1] = static_cast<float>( sin( ya ) );
245         camera.forward[2] = 0;
246         camera.right[0] = camera.forward[1];
247         camera.right[1] = -camera.forward[0];
248 }
249
250 void Camera_Freemove_updateAxes( camera_t& camera ){
251         camera.right = camera.vright;
252         camera.forward = vector3_negated( camera.vpn );
253 }
254
255 const Vector3& Camera_getOrigin( camera_t& camera ){
256         return camera.origin;
257 }
258
259 void Camera_setOrigin( camera_t& camera, const Vector3& origin ){
260         camera.origin = origin;
261         Camera_updateModelview( camera );
262         camera.m_update();
263         CameraMovedNotify();
264 }
265
266 const Vector3& Camera_getAngles( camera_t& camera ){
267         return camera.angles;
268 }
269
270 void Camera_setAngles( camera_t& camera, const Vector3& angles ){
271         camera.angles = angles;
272         Camera_updateModelview( camera );
273         camera.m_update();
274         CameraMovedNotify();
275 }
276
277
278 void Camera_FreeMove( camera_t& camera, int dx, int dy ){
279         // free strafe mode, toggled by the ctrl key with optional shift for forward movement
280         if ( camera.m_strafe ) {
281                 float strafespeed = 0.65f;
282
283                 if ( g_camwindow_globals_private.m_bCamLinkSpeed ) {
284                         strafespeed = (float)g_camwindow_globals_private.m_nMoveSpeed / 100;
285                 }
286
287                 camera.origin -= camera.vright * strafespeed * dx;
288                 if ( camera.m_strafe_forward ) {
289                         camera.origin += camera.m_strafe_forward_invert ? ( camera.vpn * strafespeed * dy ) : ( -camera.vpn * strafespeed * dy );
290                 }
291                 else{
292                         camera.origin += camera.vup * strafespeed * dy;
293                 }
294         }
295         else // free rotation
296         {
297                 const float dtime = 0.1f;
298
299                 if ( g_camwindow_globals_private.m_bCamInverseMouse ) {
300                         camera.angles[CAMERA_PITCH] -= dy * dtime * g_camwindow_globals_private.m_nAngleSpeed;
301                 }
302                 else{
303                         camera.angles[CAMERA_PITCH] += dy * dtime * g_camwindow_globals_private.m_nAngleSpeed;
304                 }
305
306                 camera.angles[CAMERA_YAW] += dx * dtime * g_camwindow_globals_private.m_nAngleSpeed;
307
308                 if ( camera.angles[CAMERA_PITCH] > 90 ) {
309                         camera.angles[CAMERA_PITCH] = 90;
310                 }
311                 else if ( camera.angles[CAMERA_PITCH] < -90 ) {
312                         camera.angles[CAMERA_PITCH] = -90;
313                 }
314
315                 if ( camera.angles[CAMERA_YAW] >= 360 ) {
316                         camera.angles[CAMERA_YAW] -= 360;
317                 }
318                 else if ( camera.angles[CAMERA_YAW] <= 0 ) {
319                         camera.angles[CAMERA_YAW] += 360;
320                 }
321         }
322
323         Camera_updateModelview( camera );
324         Camera_Freemove_updateAxes( camera );
325 }
326
327 void Cam_MouseControl( camera_t& camera, int x, int y ){
328         float xf = (float)( x - camera.width / 2 ) / ( camera.width / 2 );
329         float yf = (float)( y - camera.height / 2 ) / ( camera.height / 2 );
330
331         xf *= 1.0f - fabsf( yf );
332         if ( xf < 0 ) {
333                 xf += 0.1f;
334                 if ( xf > 0 ) {
335                         xf = 0;
336                 }
337         }
338         else
339         {
340                 xf -= 0.1f;
341                 if ( xf < 0 ) {
342                         xf = 0;
343                 }
344         }
345
346         vector3_add( camera.origin, vector3_scaled( camera.forward, yf * 0.1f * g_camwindow_globals_private.m_nMoveSpeed ) );
347         camera.angles[CAMERA_YAW] += xf * -0.1f * g_camwindow_globals_private.m_nAngleSpeed;
348
349         Camera_updateModelview( camera );
350 }
351
352 void Camera_mouseMove( camera_t& camera, int x, int y ){
353         //globalOutputStream() << "mousemove... ";
354         Camera_FreeMove( camera, -x, -y );
355         camera.m_update();
356         CameraMovedNotify();
357 }
358
359 const unsigned int MOVE_NONE = 0;
360 const unsigned int MOVE_FORWARD = 1 << 0;
361 const unsigned int MOVE_BACK = 1 << 1;
362 const unsigned int MOVE_ROTRIGHT = 1 << 2;
363 const unsigned int MOVE_ROTLEFT = 1 << 3;
364 const unsigned int MOVE_STRAFERIGHT = 1 << 4;
365 const unsigned int MOVE_STRAFELEFT = 1 << 5;
366 const unsigned int MOVE_UP = 1 << 6;
367 const unsigned int MOVE_DOWN = 1 << 7;
368 const unsigned int MOVE_PITCHUP = 1 << 8;
369 const unsigned int MOVE_PITCHDOWN = 1 << 9;
370 const unsigned int MOVE_ALL = MOVE_FORWARD | MOVE_BACK | MOVE_ROTRIGHT | MOVE_ROTLEFT | MOVE_STRAFERIGHT | MOVE_STRAFELEFT | MOVE_UP | MOVE_DOWN | MOVE_PITCHUP | MOVE_PITCHDOWN;
371
372 void Cam_KeyControl( camera_t& camera, float dtime ){
373         // Update angles
374         if ( camera.movementflags & MOVE_ROTLEFT ) {
375                 camera.angles[CAMERA_YAW] += 15 * dtime * g_camwindow_globals_private.m_nAngleSpeed;
376         }
377         if ( camera.movementflags & MOVE_ROTRIGHT ) {
378                 camera.angles[CAMERA_YAW] -= 15 * dtime * g_camwindow_globals_private.m_nAngleSpeed;
379         }
380         if ( camera.movementflags & MOVE_PITCHUP ) {
381                 camera.angles[CAMERA_PITCH] += 15 * dtime * g_camwindow_globals_private.m_nAngleSpeed;
382                 if ( camera.angles[CAMERA_PITCH] > 90 ) {
383                         camera.angles[CAMERA_PITCH] = 90;
384                 }
385         }
386         if ( camera.movementflags & MOVE_PITCHDOWN ) {
387                 camera.angles[CAMERA_PITCH] -= 15 * dtime * g_camwindow_globals_private.m_nAngleSpeed;
388                 if ( camera.angles[CAMERA_PITCH] < -90 ) {
389                         camera.angles[CAMERA_PITCH] = -90;
390                 }
391         }
392
393         Camera_updateModelview( camera );
394         Camera_Freemove_updateAxes( camera );
395
396         // Update position
397         if ( camera.movementflags & MOVE_FORWARD ) {
398                 vector3_add( camera.origin, vector3_scaled( camera.forward, dtime * g_camwindow_globals_private.m_nMoveSpeed ) );
399         }
400         if ( camera.movementflags & MOVE_BACK ) {
401                 vector3_add( camera.origin, vector3_scaled( camera.forward, -dtime * g_camwindow_globals_private.m_nMoveSpeed ) );
402         }
403         if ( camera.movementflags & MOVE_STRAFELEFT ) {
404                 vector3_add( camera.origin, vector3_scaled( camera.right, -dtime * g_camwindow_globals_private.m_nMoveSpeed ) );
405         }
406         if ( camera.movementflags & MOVE_STRAFERIGHT ) {
407                 vector3_add( camera.origin, vector3_scaled( camera.right, dtime * g_camwindow_globals_private.m_nMoveSpeed ) );
408         }
409         if ( camera.movementflags & MOVE_UP ) {
410                 vector3_add( camera.origin, vector3_scaled( g_vector3_axis_z, dtime * g_camwindow_globals_private.m_nMoveSpeed ) );
411         }
412         if ( camera.movementflags & MOVE_DOWN ) {
413                 vector3_add( camera.origin, vector3_scaled( g_vector3_axis_z, -dtime * g_camwindow_globals_private.m_nMoveSpeed ) );
414         }
415
416         Camera_updateModelview( camera );
417 }
418
419 void Camera_keyMove( camera_t& camera ){
420         camera.m_mouseMove.flush();
421
422         //globalOutputStream() << "keymove... ";
423         float time_seconds = camera.m_keycontrol_timer.elapsed_msec() / static_cast<float>( msec_per_sec );
424         camera.m_keycontrol_timer.start();
425         if ( time_seconds > 0.05f ) {
426                 time_seconds = 0.05f; // 20fps
427         }
428         Cam_KeyControl( camera, time_seconds * 5.0f );
429
430         camera.m_update();
431         CameraMovedNotify();
432 }
433
434 gboolean camera_keymove( gpointer data ){
435         Camera_keyMove( *reinterpret_cast<camera_t*>( data ) );
436         return TRUE;
437 }
438
439 void Camera_setMovementFlags( camera_t& camera, unsigned int mask ){
440         if ( ( ~camera.movementflags & mask ) != 0 && camera.movementflags == 0 ) {
441                 camera.m_keymove_handler = g_idle_add( camera_keymove, &camera );
442         }
443         camera.movementflags |= mask;
444 }
445 void Camera_clearMovementFlags( camera_t& camera, unsigned int mask ){
446         if ( ( camera.movementflags & ~mask ) == 0 && camera.movementflags != 0 ) {
447                 g_source_remove( camera.m_keymove_handler );
448                 camera.m_keymove_handler = 0;
449         }
450         camera.movementflags &= ~mask;
451 }
452
453 void Camera_MoveForward_KeyDown( camera_t& camera ){
454         Camera_setMovementFlags( camera, MOVE_FORWARD );
455 }
456 void Camera_MoveForward_KeyUp( camera_t& camera ){
457         Camera_clearMovementFlags( camera, MOVE_FORWARD );
458 }
459 void Camera_MoveBack_KeyDown( camera_t& camera ){
460         Camera_setMovementFlags( camera, MOVE_BACK );
461 }
462 void Camera_MoveBack_KeyUp( camera_t& camera ){
463         Camera_clearMovementFlags( camera, MOVE_BACK );
464 }
465
466 void Camera_MoveLeft_KeyDown( camera_t& camera ){
467         Camera_setMovementFlags( camera, MOVE_STRAFELEFT );
468 }
469 void Camera_MoveLeft_KeyUp( camera_t& camera ){
470         Camera_clearMovementFlags( camera, MOVE_STRAFELEFT );
471 }
472 void Camera_MoveRight_KeyDown( camera_t& camera ){
473         Camera_setMovementFlags( camera, MOVE_STRAFERIGHT );
474 }
475 void Camera_MoveRight_KeyUp( camera_t& camera ){
476         Camera_clearMovementFlags( camera, MOVE_STRAFERIGHT );
477 }
478
479 void Camera_MoveUp_KeyDown( camera_t& camera ){
480         Camera_setMovementFlags( camera, MOVE_UP );
481 }
482 void Camera_MoveUp_KeyUp( camera_t& camera ){
483         Camera_clearMovementFlags( camera, MOVE_UP );
484 }
485 void Camera_MoveDown_KeyDown( camera_t& camera ){
486         Camera_setMovementFlags( camera, MOVE_DOWN );
487 }
488 void Camera_MoveDown_KeyUp( camera_t& camera ){
489         Camera_clearMovementFlags( camera, MOVE_DOWN );
490 }
491
492 void Camera_RotateLeft_KeyDown( camera_t& camera ){
493         Camera_setMovementFlags( camera, MOVE_ROTLEFT );
494 }
495 void Camera_RotateLeft_KeyUp( camera_t& camera ){
496         Camera_clearMovementFlags( camera, MOVE_ROTLEFT );
497 }
498 void Camera_RotateRight_KeyDown( camera_t& camera ){
499         Camera_setMovementFlags( camera, MOVE_ROTRIGHT );
500 }
501 void Camera_RotateRight_KeyUp( camera_t& camera ){
502         Camera_clearMovementFlags( camera, MOVE_ROTRIGHT );
503 }
504
505 void Camera_PitchUp_KeyDown( camera_t& camera ){
506         Camera_setMovementFlags( camera, MOVE_PITCHUP );
507 }
508 void Camera_PitchUp_KeyUp( camera_t& camera ){
509         Camera_clearMovementFlags( camera, MOVE_PITCHUP );
510 }
511 void Camera_PitchDown_KeyDown( camera_t& camera ){
512         Camera_setMovementFlags( camera, MOVE_PITCHDOWN );
513 }
514 void Camera_PitchDown_KeyUp( camera_t& camera ){
515         Camera_clearMovementFlags( camera, MOVE_PITCHDOWN );
516 }
517
518
519 typedef ReferenceCaller<camera_t, void(), &Camera_MoveForward_KeyDown> FreeMoveCameraMoveForwardKeyDownCaller;
520 typedef ReferenceCaller<camera_t, void(), &Camera_MoveForward_KeyUp> FreeMoveCameraMoveForwardKeyUpCaller;
521 typedef ReferenceCaller<camera_t, void(), &Camera_MoveBack_KeyDown> FreeMoveCameraMoveBackKeyDownCaller;
522 typedef ReferenceCaller<camera_t, void(), &Camera_MoveBack_KeyUp> FreeMoveCameraMoveBackKeyUpCaller;
523 typedef ReferenceCaller<camera_t, void(), &Camera_MoveLeft_KeyDown> FreeMoveCameraMoveLeftKeyDownCaller;
524 typedef ReferenceCaller<camera_t, void(), &Camera_MoveLeft_KeyUp> FreeMoveCameraMoveLeftKeyUpCaller;
525 typedef ReferenceCaller<camera_t, void(), &Camera_MoveRight_KeyDown> FreeMoveCameraMoveRightKeyDownCaller;
526 typedef ReferenceCaller<camera_t, void(), &Camera_MoveRight_KeyUp> FreeMoveCameraMoveRightKeyUpCaller;
527 typedef ReferenceCaller<camera_t, void(), &Camera_MoveUp_KeyDown> FreeMoveCameraMoveUpKeyDownCaller;
528 typedef ReferenceCaller<camera_t, void(), &Camera_MoveUp_KeyUp> FreeMoveCameraMoveUpKeyUpCaller;
529 typedef ReferenceCaller<camera_t, void(), &Camera_MoveDown_KeyDown> FreeMoveCameraMoveDownKeyDownCaller;
530 typedef ReferenceCaller<camera_t, void(), &Camera_MoveDown_KeyUp> FreeMoveCameraMoveDownKeyUpCaller;
531
532
533 const float MIN_FOV = 60;
534 const float MAX_FOV = 179;
535 const float FOV_STEP = 10;
536 const float SPEED_MOVE = 32;
537 const float SPEED_TURN = 22.5;
538 const float MIN_CAM_SPEED = 10;
539 const float MAX_CAM_SPEED = 610;
540 const float CAM_SPEED_STEP = 50;
541
542 void Camera_MoveForward_Discrete( camera_t& camera ){
543         Camera_Move_updateAxes( camera );
544         Camera_setOrigin( camera, vector3_added( Camera_getOrigin( camera ), vector3_scaled( camera.forward, SPEED_MOVE ) ) );
545 }
546 void Camera_MoveBack_Discrete( camera_t& camera ){
547         Camera_Move_updateAxes( camera );
548         Camera_setOrigin( camera, vector3_added( Camera_getOrigin( camera ), vector3_scaled( camera.forward, -SPEED_MOVE ) ) );
549 }
550
551 void Camera_MoveUp_Discrete( camera_t& camera ){
552         Vector3 origin( Camera_getOrigin( camera ) );
553         origin[2] += SPEED_MOVE;
554         Camera_setOrigin( camera, origin );
555 }
556 void Camera_MoveDown_Discrete( camera_t& camera ){
557         Vector3 origin( Camera_getOrigin( camera ) );
558         origin[2] -= SPEED_MOVE;
559         Camera_setOrigin( camera, origin );
560 }
561
562 void Camera_MoveLeft_Discrete( camera_t& camera ){
563         Camera_Move_updateAxes( camera );
564         Camera_setOrigin( camera, vector3_added( Camera_getOrigin( camera ), vector3_scaled( camera.right, -SPEED_MOVE ) ) );
565 }
566 void Camera_MoveRight_Discrete( camera_t& camera ){
567         Camera_Move_updateAxes( camera );
568         Camera_setOrigin( camera, vector3_added( Camera_getOrigin( camera ), vector3_scaled( camera.right, SPEED_MOVE ) ) );
569 }
570
571 void Camera_RotateLeft_Discrete( camera_t& camera ){
572         Vector3 angles( Camera_getAngles( camera ) );
573         angles[CAMERA_YAW] += SPEED_TURN;
574         Camera_setAngles( camera, angles );
575 }
576 void Camera_RotateRight_Discrete( camera_t& camera ){
577         Vector3 angles( Camera_getAngles( camera ) );
578         angles[CAMERA_YAW] -= SPEED_TURN;
579         Camera_setAngles( camera, angles );
580 }
581
582 void Camera_PitchUp_Discrete( camera_t& camera ){
583         Vector3 angles( Camera_getAngles( camera ) );
584         angles[CAMERA_PITCH] += SPEED_TURN;
585         if ( angles[CAMERA_PITCH] > 90 ) {
586                 angles[CAMERA_PITCH] = 90;
587         }
588         Camera_setAngles( camera, angles );
589 }
590 void Camera_PitchDown_Discrete( camera_t& camera ){
591         Vector3 angles( Camera_getAngles( camera ) );
592         angles[CAMERA_PITCH] -= SPEED_TURN;
593         if ( angles[CAMERA_PITCH] < -90 ) {
594                 angles[CAMERA_PITCH] = -90;
595         }
596         Camera_setAngles( camera, angles );
597 }
598
599
600 class RadiantCameraView : public CameraView
601 {
602 camera_t& m_camera;
603 View* m_view;
604 Callback<void()> m_update;
605 public:
606 RadiantCameraView( camera_t& camera, View* view, const Callback<void()>& update ) : m_camera( camera ), m_view( view ), m_update( update ){
607 }
608 void update(){
609         m_view->Construct( m_camera.projection, m_camera.modelview, m_camera.width, m_camera.height );
610         m_update();
611 }
612 void setModelview( const Matrix4& modelview ){
613         m_camera.modelview = modelview;
614         matrix4_multiply_by_matrix4( m_camera.modelview, g_radiant2opengl );
615         matrix4_affine_invert( m_camera.modelview );
616         Camera_updateVectors( m_camera );
617         update();
618 }
619 void setFieldOfView( float fieldOfView ){
620         float farClip = Camera_getFarClipPlane( m_camera );
621         m_camera.projection = projection_for_camera( farClip / 4096.0f, farClip, fieldOfView, m_camera.width, m_camera.height );
622         update();
623 }
624 };
625
626
627 void Camera_motionDelta( int x, int y, unsigned int state, void* data ){
628         camera_t* cam = reinterpret_cast<camera_t*>( data );
629
630         cam->m_mouseMove.motion_delta( x, y, state );
631
632         cam->m_strafe_forward_invert = false;
633
634         switch ( g_camwindow_globals_private.m_nStrafeMode )
635         {
636         case 0:
637                 cam->m_strafe = false;
638                 break;
639         case 1:
640                 cam->m_strafe = ( state & GDK_CONTROL_MASK ) != 0 && ( state & GDK_SHIFT_MASK ) == 0;
641                 cam->m_strafe_forward = false;
642                 break;
643         case 2:
644                 cam->m_strafe = ( state & GDK_CONTROL_MASK ) != 0 && ( state & GDK_SHIFT_MASK ) == 0;
645                 cam->m_strafe_forward = cam->m_strafe;
646                 break;
647         case 4:
648                 cam->m_strafe_forward_invert = true;
649         default:
650                 cam->m_strafe = ( state & GDK_CONTROL_MASK ) != 0;
651                 if ( cam->m_strafe ) {
652                         cam->m_strafe_forward = ( state & GDK_SHIFT_MASK ) != 0;
653                 }
654                 else{
655                         cam->m_strafe_forward = false;
656                 }
657                 break;
658         }
659 }
660
661
662
663
664
665 class CamWnd
666 {
667 View m_view;
668 camera_t m_Camera;
669 RadiantCameraView m_cameraview;
670 #if 0
671 int m_PositionDragCursorX;
672 int m_PositionDragCursorY;
673 #endif
674
675 guint m_freemove_handle_focusout;
676
677 static Shader* m_state_select1;
678 static Shader* m_state_select2;
679
680 FreezePointer m_freezePointer;
681
682 public:
683 ui::GLArea m_gl_widget;
684 ui::Window m_parent{ui::null};
685
686 SelectionSystemWindowObserver* m_window_observer;
687 XORRectangle m_XORRectangle;
688
689 DeferredDraw m_deferredDraw;
690 DeferredMotion m_deferred_motion;
691
692 guint m_selection_button_press_handler;
693 guint m_selection_button_release_handler;
694 guint m_selection_motion_handler;
695
696 guint m_freelook_button_press_handler;
697
698 guint m_sizeHandler;
699 guint m_exposeHandler;
700
701 CamWnd();
702 ~CamWnd();
703
704 bool m_drawing;
705 void queue_draw(){
706         //ASSERT_MESSAGE(!m_drawing, "CamWnd::queue_draw(): called while draw is already in progress");
707         if ( m_drawing ) {
708                 return;
709         }
710         //globalOutputStream() << "queue... ";
711         m_deferredDraw.draw();
712 }
713 void draw();
714
715 static void captureStates(){
716         m_state_select1 = GlobalShaderCache().capture( "$CAM_HIGHLIGHT" );
717         m_state_select2 = GlobalShaderCache().capture( "$CAM_OVERLAY" );
718 }
719 static void releaseStates(){
720         GlobalShaderCache().release( "$CAM_HIGHLIGHT" );
721         GlobalShaderCache().release( "$CAM_OVERLAY" );
722 }
723
724 camera_t& getCamera(){
725         return m_Camera;
726 }
727
728 void BenchMark();
729 void Cam_ChangeFloor( bool up );
730
731 void DisableFreeMove();
732 void EnableFreeMove();
733 bool m_bFreeMove;
734
735 CameraView& getCameraView(){
736         return m_cameraview;
737 }
738
739 private:
740 void Cam_Draw();
741 };
742
743 typedef MemberCaller<CamWnd, void(), &CamWnd::queue_draw> CamWndQueueDraw;
744
745 Shader* CamWnd::m_state_select1 = 0;
746 Shader* CamWnd::m_state_select2 = 0;
747
748 CamWnd* NewCamWnd(){
749         return new CamWnd;
750 }
751 void DeleteCamWnd( CamWnd* camwnd ){
752         delete camwnd;
753 }
754
755 void CamWnd_constructStatic(){
756         CamWnd::captureStates();
757 }
758
759 void CamWnd_destroyStatic(){
760         CamWnd::releaseStates();
761 }
762
763 static CamWnd* g_camwnd = 0;
764
765 void GlobalCamera_setCamWnd( CamWnd& camwnd ){
766         g_camwnd = &camwnd;
767 }
768
769
770 ui::GLArea CamWnd_getWidget( CamWnd& camwnd ){
771         return camwnd.m_gl_widget;
772 }
773
774 ui::Window CamWnd_getParent( CamWnd& camwnd ){
775         return camwnd.m_parent;
776 }
777
778 ToggleShown g_camera_shown( true );
779
780 void CamWnd_setParent( CamWnd& camwnd, ui::Window parent ){
781         camwnd.m_parent = parent;
782         g_camera_shown.connect( camwnd.m_parent );
783 }
784
785 void CamWnd_Update( CamWnd& camwnd ){
786         camwnd.queue_draw();
787 }
788
789
790
791 camwindow_globals_t g_camwindow_globals;
792
793 const Vector3& Camera_getOrigin( CamWnd& camwnd ){
794         return Camera_getOrigin( camwnd.getCamera() );
795 }
796
797 void Camera_setOrigin( CamWnd& camwnd, const Vector3& origin ){
798         Camera_setOrigin( camwnd.getCamera(), origin );
799 }
800
801 const Vector3& Camera_getAngles( CamWnd& camwnd ){
802         return Camera_getAngles( camwnd.getCamera() );
803 }
804
805 void Camera_setAngles( CamWnd& camwnd, const Vector3& angles ){
806         Camera_setAngles( camwnd.getCamera(), angles );
807 }
808
809
810 // =============================================================================
811 // CamWnd class
812
813 gboolean enable_freelook_button_press( ui::Widget widget, GdkEventButton* event, CamWnd* camwnd ){
814         if ( event->type == GDK_BUTTON_PRESS && event->button == 3 && modifiers_for_state( event->state ) == c_modifierNone ) {
815                 camwnd->EnableFreeMove();
816                 return TRUE;
817         }
818         return FALSE;
819 }
820
821 gboolean disable_freelook_button_press( ui::Widget widget, GdkEventButton* event, CamWnd* camwnd ){
822         if ( event->type == GDK_BUTTON_PRESS && event->button == 3 && modifiers_for_state( event->state ) == c_modifierNone ) {
823                 camwnd->DisableFreeMove();
824                 return TRUE;
825         }
826         return FALSE;
827 }
828
829 #if 0
830 gboolean mousecontrol_button_press( ui::Widget widget, GdkEventButton* event, CamWnd* camwnd ){
831         if ( event->type == GDK_BUTTON_PRESS && event->button == 3 ) {
832                 Cam_MouseControl( camwnd->getCamera(), event->x, widget->allocation.height - 1 - event->y );
833         }
834         return FALSE;
835 }
836 #endif
837
838 void camwnd_update_xor_rectangle( CamWnd& self, rect_t area ){
839         if ( self.m_gl_widget.visible() ) {
840                 self.m_XORRectangle.set( rectangle_from_area( area.min, area.max, self.getCamera().width, self.getCamera().height ) );
841         }
842 }
843
844
845 gboolean selection_button_press( ui::Widget widget, GdkEventButton* event, WindowObserver* observer ){
846         if ( event->type == GDK_BUTTON_PRESS ) {
847                 gtk_widget_grab_focus( widget );
848                 observer->onMouseDown( WindowVector_forDouble( event->x, event->y ), button_for_button( event->button ), modifiers_for_state( event->state ) );
849         }
850         return FALSE;
851 }
852
853 gboolean selection_button_release( ui::Widget widget, GdkEventButton* event, WindowObserver* observer ){
854         if ( event->type == GDK_BUTTON_RELEASE ) {
855                 observer->onMouseUp( WindowVector_forDouble( event->x, event->y ), button_for_button( event->button ), modifiers_for_state( event->state ) );
856         }
857         return FALSE;
858 }
859
860 void selection_motion( gdouble x, gdouble y, guint state, void* data ){
861         //globalOutputStream() << "motion... ";
862         reinterpret_cast<WindowObserver*>( data )->onMouseMotion( WindowVector_forDouble( x, y ), modifiers_for_state( state ) );
863 }
864
865 inline WindowVector windowvector_for_widget_centre( ui::Widget widget ){
866         auto allocation = widget.dimensions();
867         return WindowVector( static_cast<float>( allocation.width / 2 ), static_cast<float>(allocation.height / 2 ) );
868 }
869
870 gboolean selection_button_press_freemove( ui::Widget widget, GdkEventButton* event, WindowObserver* observer ){
871         if ( event->type == GDK_BUTTON_PRESS ) {
872                 observer->onMouseDown( windowvector_for_widget_centre( widget ), button_for_button( event->button ), modifiers_for_state( event->state ) );
873         }
874         return FALSE;
875 }
876
877 gboolean selection_button_release_freemove( ui::Widget widget, GdkEventButton* event, WindowObserver* observer ){
878         if ( event->type == GDK_BUTTON_RELEASE ) {
879                 observer->onMouseUp( windowvector_for_widget_centre( widget ), button_for_button( event->button ), modifiers_for_state( event->state ) );
880         }
881         return FALSE;
882 }
883
884 gboolean selection_motion_freemove( ui::Widget widget, GdkEventMotion *event, WindowObserver* observer ){
885         observer->onMouseMotion( windowvector_for_widget_centre( widget ), modifiers_for_state( event->state ) );
886         return FALSE;
887 }
888
889 gboolean wheelmove_scroll( ui::Widget widget, GdkEventScroll* event, CamWnd* camwnd ){
890         //gtk_window_set_focus( camwnd->m_parent, camwnd->m_gl_widget );
891         gtk_widget_grab_focus( camwnd->m_gl_widget );
892         if( !gtk_window_is_active( camwnd->m_parent ) )
893                 gtk_window_present( camwnd->m_parent );
894
895         if ( event->direction == GDK_SCROLL_UP ) {
896                 Camera_Freemove_updateAxes( camwnd->getCamera() );
897                 if( camwnd->m_bFreeMove || !g_camwindow_globals.m_bZoomInToPointer ){
898                         Camera_setOrigin( *camwnd, vector3_added( Camera_getOrigin( *camwnd ), vector3_scaled( camwnd->getCamera().forward, static_cast<float>( g_camwindow_globals_private.m_nMoveSpeed ) ) ) );
899                 }
900                 else{
901                         //Matrix4 maa = matrix4_multiplied_by_matrix4( camwnd->getCamera().projection, camwnd->getCamera().modelview );
902                         Matrix4 maa = camwnd->getCamera().m_view->GetViewMatrix();
903                         matrix4_affine_invert( maa );
904
905                         float x = static_cast<float>( event->x );
906                         float y = static_cast<float>( event->y );
907                         Vector3 normalized;
908
909                         normalized[0] = 2.0f * ( x ) / static_cast<float>( camwnd->getCamera().width ) - 1.0f;
910                         normalized[1] = 2.0f * ( y )/ static_cast<float>( camwnd->getCamera().height ) - 1.0f;
911                         normalized[1] *= -1.f;
912                         normalized[2] = 0.f;
913
914                         normalized *= 16.0f;
915                                 //globalOutputStream() << normalized << " normalized    ";
916                         matrix4_transform_point( maa, normalized );
917                                 //globalOutputStream() << normalized << "\n";
918                         Vector3 norm = vector3_normalised( normalized - Camera_getOrigin( *camwnd ) );
919                                 //globalOutputStream() << normalized - Camera_getOrigin( *camwnd ) << "  normalized - Camera_getOrigin( *camwnd )\n";
920                                 //globalOutputStream() << norm << "  norm\n";
921                         Camera_setOrigin( *camwnd, vector3_added( Camera_getOrigin( *camwnd ), vector3_scaled( norm, static_cast<float>( g_camwindow_globals_private.m_nMoveSpeed ) ) ) );
922                 }
923         }
924         else if ( event->direction == GDK_SCROLL_DOWN ) {
925                 Camera_Freemove_updateAxes( camwnd->getCamera() );
926                 Camera_setOrigin( *camwnd, vector3_added( Camera_getOrigin( *camwnd ), vector3_scaled( camwnd->getCamera().forward, -static_cast<float>( g_camwindow_globals_private.m_nMoveSpeed ) ) ) );
927         }
928
929         return FALSE;
930 }
931
932 gboolean camera_size_allocate( ui::Widget widget, GtkAllocation* allocation, CamWnd* camwnd ){
933         camwnd->getCamera().width = allocation->width;
934         camwnd->getCamera().height = allocation->height;
935         Camera_updateProjection( camwnd->getCamera() );
936         camwnd->m_window_observer->onSizeChanged( camwnd->getCamera().width, camwnd->getCamera().height );
937         camwnd->queue_draw();
938         return FALSE;
939 }
940
941 gboolean camera_expose( ui::Widget widget, GdkEventExpose* event, gpointer data ){
942         reinterpret_cast<CamWnd*>( data )->draw();
943         return FALSE;
944 }
945
946 void KeyEvent_connect( const char* name ){
947         const KeyEvent& keyEvent = GlobalKeyEvents_find( name );
948         keydown_accelerators_add( keyEvent.m_accelerator, keyEvent.m_keyDown );
949         keyup_accelerators_add( keyEvent.m_accelerator, keyEvent.m_keyUp );
950 }
951
952 void KeyEvent_disconnect( const char* name ){
953         const KeyEvent& keyEvent = GlobalKeyEvents_find( name );
954         keydown_accelerators_remove( keyEvent.m_accelerator );
955         keyup_accelerators_remove( keyEvent.m_accelerator );
956 }
957
958 void CamWnd_registerCommands( CamWnd& camwnd ){
959         GlobalKeyEvents_insert( "CameraForward", accelerator_null(),
960                                                         ReferenceCaller<camera_t, void(), Camera_MoveForward_KeyDown>( camwnd.getCamera() ),
961                                                         ReferenceCaller<camera_t, void(), Camera_MoveForward_KeyUp>( camwnd.getCamera() )
962                                                         );
963         GlobalKeyEvents_insert( "CameraBack", accelerator_null(),
964                                                         ReferenceCaller<camera_t, void(), Camera_MoveBack_KeyDown>( camwnd.getCamera() ),
965                                                         ReferenceCaller<camera_t, void(), Camera_MoveBack_KeyUp>( camwnd.getCamera() )
966                                                         );
967         GlobalKeyEvents_insert( "CameraLeft", accelerator_null(),
968                                                         ReferenceCaller<camera_t, void(), Camera_RotateLeft_KeyDown>( camwnd.getCamera() ),
969                                                         ReferenceCaller<camera_t, void(), Camera_RotateLeft_KeyUp>( camwnd.getCamera() )
970                                                         );
971         GlobalKeyEvents_insert( "CameraRight", accelerator_null(),
972                                                         ReferenceCaller<camera_t, void(), Camera_RotateRight_KeyDown>( camwnd.getCamera() ),
973                                                         ReferenceCaller<camera_t, void(), Camera_RotateRight_KeyUp>( camwnd.getCamera() )
974                                                         );
975         GlobalKeyEvents_insert( "CameraStrafeRight", accelerator_null(),
976                                                         ReferenceCaller<camera_t, void(), Camera_MoveRight_KeyDown>( camwnd.getCamera() ),
977                                                         ReferenceCaller<camera_t, void(), Camera_MoveRight_KeyUp>( camwnd.getCamera() )
978                                                         );
979         GlobalKeyEvents_insert( "CameraStrafeLeft", accelerator_null(),
980                                                         ReferenceCaller<camera_t, void(), Camera_MoveLeft_KeyDown>( camwnd.getCamera() ),
981                                                         ReferenceCaller<camera_t, void(), Camera_MoveLeft_KeyUp>( camwnd.getCamera() )
982                                                         );
983         GlobalKeyEvents_insert( "CameraUp", accelerator_null(),
984                                                         ReferenceCaller<camera_t, void(), Camera_MoveUp_KeyDown>( camwnd.getCamera() ),
985                                                         ReferenceCaller<camera_t, void(), Camera_MoveUp_KeyUp>( camwnd.getCamera() )
986                                                         );
987         GlobalKeyEvents_insert( "CameraDown", accelerator_null(),
988                                                         ReferenceCaller<camera_t, void(), Camera_MoveDown_KeyDown>( camwnd.getCamera() ),
989                                                         ReferenceCaller<camera_t, void(), Camera_MoveDown_KeyUp>( camwnd.getCamera() )
990                                                         );
991         GlobalKeyEvents_insert( "CameraAngleUp", accelerator_null(),
992                                                         ReferenceCaller<camera_t, void(), Camera_PitchUp_KeyDown>( camwnd.getCamera() ),
993                                                         ReferenceCaller<camera_t, void(), Camera_PitchUp_KeyUp>( camwnd.getCamera() )
994                                                         );
995         GlobalKeyEvents_insert( "CameraAngleDown", accelerator_null(),
996                                                         ReferenceCaller<camera_t, void(), Camera_PitchDown_KeyDown>( camwnd.getCamera() ),
997                                                         ReferenceCaller<camera_t, void(), Camera_PitchDown_KeyUp>( camwnd.getCamera() )
998                                                         );
999
1000
1001         GlobalKeyEvents_insert( "CameraFreeMoveForward", accelerator_null(),
1002                                                         FreeMoveCameraMoveForwardKeyDownCaller( camwnd.getCamera() ),
1003                                                         FreeMoveCameraMoveForwardKeyUpCaller( camwnd.getCamera() )
1004                                                         );
1005         GlobalKeyEvents_insert( "CameraFreeMoveBack", accelerator_null(),
1006                                                         FreeMoveCameraMoveBackKeyDownCaller( camwnd.getCamera() ),
1007                                                         FreeMoveCameraMoveBackKeyUpCaller( camwnd.getCamera() )
1008                                                         );
1009         GlobalKeyEvents_insert( "CameraFreeMoveLeft", accelerator_null(),
1010                                                         FreeMoveCameraMoveLeftKeyDownCaller( camwnd.getCamera() ),
1011                                                         FreeMoveCameraMoveLeftKeyUpCaller( camwnd.getCamera() )
1012                                                         );
1013         GlobalKeyEvents_insert( "CameraFreeMoveRight", accelerator_null(),
1014                                                         FreeMoveCameraMoveRightKeyDownCaller( camwnd.getCamera() ),
1015                                                         FreeMoveCameraMoveRightKeyUpCaller( camwnd.getCamera() )
1016                                                         );
1017
1018         GlobalKeyEvents_insert( "CameraFreeMoveForward2", accelerator_null(),
1019                                                         FreeMoveCameraMoveForwardKeyDownCaller( camwnd.getCamera() ),
1020                                                         FreeMoveCameraMoveForwardKeyUpCaller( camwnd.getCamera() )
1021                                                         );
1022         GlobalKeyEvents_insert( "CameraFreeMoveBack2", accelerator_null(),
1023                                                         FreeMoveCameraMoveBackKeyDownCaller( camwnd.getCamera() ),
1024                                                         FreeMoveCameraMoveBackKeyUpCaller( camwnd.getCamera() )
1025                                                         );
1026         GlobalKeyEvents_insert( "CameraFreeMoveLeft2", accelerator_null(),
1027                                                         FreeMoveCameraMoveLeftKeyDownCaller( camwnd.getCamera() ),
1028                                                         FreeMoveCameraMoveLeftKeyUpCaller( camwnd.getCamera() )
1029                                                         );
1030         GlobalKeyEvents_insert( "CameraFreeMoveRight2", accelerator_null(),
1031                                                         FreeMoveCameraMoveRightKeyDownCaller( camwnd.getCamera() ),
1032                                                         FreeMoveCameraMoveRightKeyUpCaller( camwnd.getCamera() )
1033                                                         );
1034
1035         GlobalKeyEvents_insert( "CameraFreeMoveUp", accelerator_null(),
1036                                                         FreeMoveCameraMoveUpKeyDownCaller( camwnd.getCamera() ),
1037                                                         FreeMoveCameraMoveUpKeyUpCaller( camwnd.getCamera() )
1038                                                         );
1039         GlobalKeyEvents_insert( "CameraFreeMoveDown", accelerator_null(),
1040                                                         FreeMoveCameraMoveDownKeyDownCaller( camwnd.getCamera() ),
1041                                                         FreeMoveCameraMoveDownKeyUpCaller( camwnd.getCamera() )
1042                                                         );
1043
1044         GlobalCommands_insert( "CameraForward", ReferenceCaller<camera_t, void(), Camera_MoveForward_Discrete>( camwnd.getCamera() ) );
1045         GlobalCommands_insert( "CameraBack", ReferenceCaller<camera_t, void(), Camera_MoveBack_Discrete>( camwnd.getCamera() ) );
1046         GlobalCommands_insert( "CameraLeft", ReferenceCaller<camera_t, void(), Camera_RotateLeft_Discrete>( camwnd.getCamera() ) );
1047         GlobalCommands_insert( "CameraRight", ReferenceCaller<camera_t, void(), Camera_RotateRight_Discrete>( camwnd.getCamera() ) );
1048         GlobalCommands_insert( "CameraStrafeRight", ReferenceCaller<camera_t, void(), Camera_MoveRight_Discrete>( camwnd.getCamera() ) );
1049         GlobalCommands_insert( "CameraStrafeLeft", ReferenceCaller<camera_t, void(), Camera_MoveLeft_Discrete>( camwnd.getCamera() ) );
1050
1051         GlobalCommands_insert( "CameraUp", ReferenceCaller<camera_t, void(), Camera_MoveUp_Discrete>( camwnd.getCamera() ) );
1052         GlobalCommands_insert( "CameraDown", ReferenceCaller<camera_t, void(), Camera_MoveDown_Discrete>( camwnd.getCamera() ) );
1053         GlobalCommands_insert( "CameraAngleUp", ReferenceCaller<camera_t, void(), Camera_PitchUp_Discrete>( camwnd.getCamera() ) );
1054         GlobalCommands_insert( "CameraAngleDown", ReferenceCaller<camera_t, void(), Camera_PitchDown_Discrete>( camwnd.getCamera() ) );
1055 }
1056
1057 void CamWnd_Move_Enable( CamWnd& camwnd ){
1058         KeyEvent_connect( "CameraForward" );
1059         KeyEvent_connect( "CameraBack" );
1060         KeyEvent_connect( "CameraLeft" );
1061         KeyEvent_connect( "CameraRight" );
1062         KeyEvent_connect( "CameraStrafeRight" );
1063         KeyEvent_connect( "CameraStrafeLeft" );
1064         KeyEvent_connect( "CameraUp" );
1065         KeyEvent_connect( "CameraDown" );
1066         KeyEvent_connect( "CameraAngleUp" );
1067         KeyEvent_connect( "CameraAngleDown" );
1068 }
1069
1070 void CamWnd_Move_Disable( CamWnd& camwnd ){
1071         KeyEvent_disconnect( "CameraForward" );
1072         KeyEvent_disconnect( "CameraBack" );
1073         KeyEvent_disconnect( "CameraLeft" );
1074         KeyEvent_disconnect( "CameraRight" );
1075         KeyEvent_disconnect( "CameraStrafeRight" );
1076         KeyEvent_disconnect( "CameraStrafeLeft" );
1077         KeyEvent_disconnect( "CameraUp" );
1078         KeyEvent_disconnect( "CameraDown" );
1079         KeyEvent_disconnect( "CameraAngleUp" );
1080         KeyEvent_disconnect( "CameraAngleDown" );
1081 }
1082
1083 void CamWnd_Move_Discrete_Enable( CamWnd& camwnd ){
1084         command_connect_accelerator( "CameraForward" );
1085         command_connect_accelerator( "CameraBack" );
1086         command_connect_accelerator( "CameraLeft" );
1087         command_connect_accelerator( "CameraRight" );
1088         command_connect_accelerator( "CameraStrafeRight" );
1089         command_connect_accelerator( "CameraStrafeLeft" );
1090         command_connect_accelerator( "CameraUp" );
1091         command_connect_accelerator( "CameraDown" );
1092         command_connect_accelerator( "CameraAngleUp" );
1093         command_connect_accelerator( "CameraAngleDown" );
1094 }
1095
1096 void CamWnd_Move_Discrete_Disable( CamWnd& camwnd ){
1097         command_disconnect_accelerator( "CameraForward" );
1098         command_disconnect_accelerator( "CameraBack" );
1099         command_disconnect_accelerator( "CameraLeft" );
1100         command_disconnect_accelerator( "CameraRight" );
1101         command_disconnect_accelerator( "CameraStrafeRight" );
1102         command_disconnect_accelerator( "CameraStrafeLeft" );
1103         command_disconnect_accelerator( "CameraUp" );
1104         command_disconnect_accelerator( "CameraDown" );
1105         command_disconnect_accelerator( "CameraAngleUp" );
1106         command_disconnect_accelerator( "CameraAngleDown" );
1107 }
1108
1109 struct CamWnd_Move_Discrete {
1110         static void Export(const Callback<void(bool)> &returnz) {
1111                 returnz(g_camwindow_globals_private.m_bCamDiscrete);
1112         }
1113
1114         static void Import(bool value) {
1115                 if (g_camwnd) {
1116                         Import_(*g_camwnd, value);
1117                 } else {
1118                         g_camwindow_globals_private.m_bCamDiscrete = value;
1119                 }
1120         }
1121
1122         static void Import_(CamWnd &camwnd, bool value) {
1123         if ( g_camwindow_globals_private.m_bCamDiscrete ) {
1124                 CamWnd_Move_Discrete_Disable( camwnd );
1125                 } else {
1126                 CamWnd_Move_Disable( camwnd );
1127         }
1128
1129         g_camwindow_globals_private.m_bCamDiscrete = value;
1130
1131         if ( g_camwindow_globals_private.m_bCamDiscrete ) {
1132                 CamWnd_Move_Discrete_Enable( camwnd );
1133                 } else {
1134                 CamWnd_Move_Enable( camwnd );
1135         }
1136 }
1137 };
1138
1139
1140 void CamWnd_Add_Handlers_Move( CamWnd& camwnd ){
1141         camwnd.m_selection_button_press_handler = camwnd.m_gl_widget.connect( "button_press_event", G_CALLBACK( selection_button_press ), camwnd.m_window_observer );
1142         camwnd.m_selection_button_release_handler = camwnd.m_gl_widget.connect( "button_release_event", G_CALLBACK( selection_button_release ), camwnd.m_window_observer );
1143         camwnd.m_selection_motion_handler = camwnd.m_gl_widget.connect( "motion_notify_event", G_CALLBACK( DeferredMotion::gtk_motion ), &camwnd.m_deferred_motion );
1144
1145         camwnd.m_freelook_button_press_handler = camwnd.m_gl_widget.connect( "button_press_event", G_CALLBACK( enable_freelook_button_press ), &camwnd );
1146
1147         if ( g_camwindow_globals_private.m_bCamDiscrete ) {
1148                 CamWnd_Move_Discrete_Enable( camwnd );
1149         }
1150         else
1151         {
1152                 CamWnd_Move_Enable( camwnd );
1153         }
1154 }
1155
1156 void CamWnd_Remove_Handlers_Move( CamWnd& camwnd ){
1157         g_signal_handler_disconnect( G_OBJECT( camwnd.m_gl_widget ), camwnd.m_selection_button_press_handler );
1158         g_signal_handler_disconnect( G_OBJECT( camwnd.m_gl_widget ), camwnd.m_selection_button_release_handler );
1159         g_signal_handler_disconnect( G_OBJECT( camwnd.m_gl_widget ), camwnd.m_selection_motion_handler );
1160
1161         g_signal_handler_disconnect( G_OBJECT( camwnd.m_gl_widget ), camwnd.m_freelook_button_press_handler );
1162
1163         if ( g_camwindow_globals_private.m_bCamDiscrete ) {
1164                 CamWnd_Move_Discrete_Disable( camwnd );
1165         }
1166         else
1167         {
1168                 CamWnd_Move_Disable( camwnd );
1169         }
1170 }
1171
1172 void CamWnd_Add_Handlers_FreeMove( CamWnd& camwnd ){
1173         camwnd.m_selection_button_press_handler = camwnd.m_gl_widget.connect( "button_press_event", G_CALLBACK( selection_button_press_freemove ), camwnd.m_window_observer );
1174         camwnd.m_selection_button_release_handler = camwnd.m_gl_widget.connect( "button_release_event", G_CALLBACK( selection_button_release_freemove ), camwnd.m_window_observer );
1175         camwnd.m_selection_motion_handler = camwnd.m_gl_widget.connect( "motion_notify_event", G_CALLBACK( selection_motion_freemove ), camwnd.m_window_observer );
1176
1177         camwnd.m_freelook_button_press_handler = camwnd.m_gl_widget.connect( "button_press_event", G_CALLBACK( disable_freelook_button_press ), &camwnd );
1178
1179         KeyEvent_connect( "CameraFreeMoveForward" );
1180         KeyEvent_connect( "CameraFreeMoveBack" );
1181         KeyEvent_connect( "CameraFreeMoveLeft" );
1182         KeyEvent_connect( "CameraFreeMoveRight" );
1183
1184         KeyEvent_connect( "CameraFreeMoveForward2" );
1185         KeyEvent_connect( "CameraFreeMoveBack2" );
1186         KeyEvent_connect( "CameraFreeMoveLeft2" );
1187         KeyEvent_connect( "CameraFreeMoveRight2" );
1188
1189         KeyEvent_connect( "CameraFreeMoveUp" );
1190         KeyEvent_connect( "CameraFreeMoveDown" );
1191 }
1192
1193 void CamWnd_Remove_Handlers_FreeMove( CamWnd& camwnd ){
1194         KeyEvent_disconnect( "CameraFreeMoveForward" );
1195         KeyEvent_disconnect( "CameraFreeMoveBack" );
1196         KeyEvent_disconnect( "CameraFreeMoveLeft" );
1197         KeyEvent_disconnect( "CameraFreeMoveRight" );
1198
1199         KeyEvent_disconnect( "CameraFreeMoveForward2" );
1200         KeyEvent_disconnect( "CameraFreeMoveBack2" );
1201         KeyEvent_disconnect( "CameraFreeMoveLeft2" );
1202         KeyEvent_disconnect( "CameraFreeMoveRight2" );
1203
1204         KeyEvent_disconnect( "CameraFreeMoveUp" );
1205         KeyEvent_disconnect( "CameraFreeMoveDown" );
1206
1207         g_signal_handler_disconnect( G_OBJECT( camwnd.m_gl_widget ), camwnd.m_selection_button_press_handler );
1208         g_signal_handler_disconnect( G_OBJECT( camwnd.m_gl_widget ), camwnd.m_selection_button_release_handler );
1209         g_signal_handler_disconnect( G_OBJECT( camwnd.m_gl_widget ), camwnd.m_selection_motion_handler );
1210
1211         g_signal_handler_disconnect( G_OBJECT( camwnd.m_gl_widget ), camwnd.m_freelook_button_press_handler );
1212 }
1213
1214 CamWnd::CamWnd() :
1215         m_view( true ),
1216         m_Camera( &m_view, CamWndQueueDraw( *this ) ),
1217         m_cameraview( m_Camera, &m_view, ReferenceCaller<CamWnd, void(), CamWnd_Update>( *this ) ),
1218         m_gl_widget( glwidget_new( TRUE ) ),
1219         m_window_observer( NewWindowObserver() ),
1220         m_XORRectangle( m_gl_widget ),
1221         m_deferredDraw( WidgetQueueDrawCaller( m_gl_widget ) ),
1222         m_deferred_motion( selection_motion, m_window_observer ),
1223         m_selection_button_press_handler( 0 ),
1224         m_selection_button_release_handler( 0 ),
1225         m_selection_motion_handler( 0 ),
1226         m_freelook_button_press_handler( 0 ),
1227         m_drawing( false ){
1228         m_bFreeMove = false;
1229
1230         GlobalWindowObservers_add( m_window_observer );
1231         GlobalWindowObservers_connectWidget( m_gl_widget );
1232
1233         m_window_observer->setRectangleDrawCallback( ReferenceCaller<CamWnd, void(rect_t), camwnd_update_xor_rectangle>( *this ) );
1234         m_window_observer->setView( m_view );
1235
1236         g_object_ref( m_gl_widget._handle );
1237
1238         gtk_widget_set_events( m_gl_widget, GDK_DESTROY | GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_SCROLL_MASK );
1239         gtk_widget_set_can_focus( m_gl_widget, true );
1240
1241         m_sizeHandler = m_gl_widget.connect( "size_allocate", G_CALLBACK( camera_size_allocate ), this );
1242         m_exposeHandler = m_gl_widget.on_render( G_CALLBACK( camera_expose ), this );
1243
1244         Map_addValidCallback( g_map, DeferredDrawOnMapValidChangedCaller( m_deferredDraw ) );
1245
1246         CamWnd_registerCommands( *this );
1247
1248         CamWnd_Add_Handlers_Move( *this );
1249
1250         m_gl_widget.connect( "scroll_event", G_CALLBACK( wheelmove_scroll ), this );
1251
1252         AddSceneChangeCallback( ReferenceCaller<CamWnd, void(), CamWnd_Update>( *this ) );
1253
1254         PressedButtons_connect( g_pressedButtons, m_gl_widget );
1255 }
1256
1257 CamWnd::~CamWnd(){
1258         if ( m_bFreeMove ) {
1259                 DisableFreeMove();
1260         }
1261
1262         CamWnd_Remove_Handlers_Move( *this );
1263
1264         g_signal_handler_disconnect( G_OBJECT( m_gl_widget ), m_sizeHandler );
1265         g_signal_handler_disconnect( G_OBJECT( m_gl_widget ), m_exposeHandler );
1266
1267         m_gl_widget.unref();
1268
1269         m_window_observer->release();
1270 }
1271
1272 class FloorHeightWalker : public scene::Graph::Walker
1273 {
1274 float m_current;
1275 float& m_bestUp;
1276 float& m_bestDown;
1277 public:
1278 FloorHeightWalker( float current, float& bestUp, float& bestDown ) :
1279         m_current( current ), m_bestUp( bestUp ), m_bestDown( bestDown ){
1280         bestUp = g_MaxWorldCoord;
1281         bestDown = -g_MaxWorldCoord;
1282 }
1283 bool pre( const scene::Path& path, scene::Instance& instance ) const {
1284         if ( path.top().get().visible()
1285                  && Node_isBrush( path.top() ) ) { // this node is a floor
1286                 const AABB& aabb = instance.worldAABB();
1287                 float floorHeight = aabb.origin.z() + aabb.extents.z();
1288                 if ( floorHeight > m_current && floorHeight < m_bestUp ) {
1289                         m_bestUp = floorHeight;
1290                 }
1291                 if ( floorHeight < m_current && floorHeight > m_bestDown ) {
1292                         m_bestDown = floorHeight;
1293                 }
1294         }
1295         else if( !path.top().get().visible() ){
1296                 return false;
1297         }
1298         return true;
1299 }
1300 };
1301
1302 void CamWnd::Cam_ChangeFloor( bool up ){
1303         float current = m_Camera.origin[2] - 48;
1304         float bestUp;
1305         float bestDown;
1306         GlobalSceneGraph().traverse( FloorHeightWalker( current, bestUp, bestDown ) );
1307
1308         if ( up && bestUp != g_MaxWorldCoord ) {
1309                 current = bestUp;
1310         }
1311         if ( !up && bestDown != -g_MaxWorldCoord ) {
1312                 current = bestDown;
1313         }
1314
1315         m_Camera.origin[2] = current + 48;
1316         Camera_updateModelview( getCamera() );
1317         CamWnd_Update( *this );
1318         CameraMovedNotify();
1319 }
1320
1321
1322 #if 0
1323
1324 // button_press
1325 Sys_GetCursorPos( &m_PositionDragCursorX, &m_PositionDragCursorY );
1326
1327 // motion
1328 if ( ( m_bFreeMove && ( buttons == ( RAD_CONTROL | RAD_SHIFT ) ) )
1329          || ( !m_bFreeMove && ( buttons == ( RAD_RBUTTON | RAD_CONTROL ) ) ) ) {
1330         Cam_PositionDrag();
1331         CamWnd_Update( camwnd );
1332         CameraMovedNotify();
1333         return;
1334 }
1335
1336 void CamWnd::Cam_PositionDrag(){
1337         int x, y;
1338
1339         Sys_GetCursorPos( m_gl_widget, &x, &y );
1340         if ( x != m_PositionDragCursorX || y != m_PositionDragCursorY ) {
1341                 x -= m_PositionDragCursorX;
1342                 vector3_add( m_Camera.origin, vector3_scaled( m_Camera.vright, x ) );
1343                 y -= m_PositionDragCursorY;
1344                 m_Camera.origin[2] -= y;
1345                 Camera_updateModelview();
1346                 CamWnd_Update( camwnd );
1347                 CameraMovedNotify();
1348
1349                 Sys_SetCursorPos( m_gl_widget, m_PositionDragCursorX, m_PositionDragCursorY );
1350         }
1351 }
1352 #endif
1353
1354
1355 // NOTE TTimo if there's an OS-level focus out of the application
1356 //   then we can release the camera cursor grab
1357 static gboolean camwindow_freemove_focusout( ui::Widget widget, GdkEventFocus* event, gpointer data ){
1358         reinterpret_cast<CamWnd*>( data )->DisableFreeMove();
1359         return FALSE;
1360 }
1361
1362 void CamWnd::EnableFreeMove(){
1363         //globalOutputStream() << "EnableFreeMove\n";
1364
1365         ASSERT_MESSAGE( !m_bFreeMove, "EnableFreeMove: free-move was already enabled" );
1366         m_bFreeMove = true;
1367         Camera_clearMovementFlags( getCamera(), MOVE_ALL );
1368
1369         CamWnd_Remove_Handlers_Move( *this );
1370         CamWnd_Add_Handlers_FreeMove( *this );
1371
1372         gtk_window_set_focus( m_parent, m_gl_widget );
1373         m_freemove_handle_focusout = m_gl_widget.connect( "focus_out_event", G_CALLBACK( camwindow_freemove_focusout ), this );
1374         /* We chose to replace m_parent by m_gl_widget but NetRadiantCustom does:
1375         m_freezePointer.freeze_pointer( m_parent, m_gl_widget, Camera_motionDelta, &m_Camera ); */
1376         m_freezePointer.freeze_pointer( m_gl_widget, Camera_motionDelta, &m_Camera );
1377
1378         CamWnd_Update( *this );
1379 }
1380
1381 void CamWnd::DisableFreeMove(){
1382         //globalOutputStream() << "DisableFreeMove\n";
1383
1384         ASSERT_MESSAGE( m_bFreeMove, "DisableFreeMove: free-move was not enabled" );
1385         m_bFreeMove = false;
1386         Camera_clearMovementFlags( getCamera(), MOVE_ALL );
1387
1388         CamWnd_Remove_Handlers_FreeMove( *this );
1389         CamWnd_Add_Handlers_Move( *this );
1390
1391         m_freezePointer.unfreeze_pointer( m_gl_widget, true );
1392
1393         g_signal_handler_disconnect( G_OBJECT( m_gl_widget ), m_freemove_handle_focusout );
1394
1395         CamWnd_Update( *this );
1396 }
1397
1398
1399 #include "renderer.h"
1400
1401 class CamRenderer : public Renderer
1402 {
1403 struct state_type
1404 {
1405         state_type() : m_highlight( 0 ), m_state( 0 ), m_lights( 0 ){
1406         }
1407         unsigned int m_highlight;
1408         Shader* m_state;
1409         const LightList* m_lights;
1410 };
1411
1412 std::vector<state_type> m_state_stack;
1413 RenderStateFlags m_globalstate;
1414 Shader* m_state_select0;
1415 Shader* m_state_select1;
1416 const Vector3& m_viewer;
1417
1418 public:
1419 CamRenderer( RenderStateFlags globalstate, Shader* select0, Shader* select1, const Vector3& viewer ) :
1420         m_globalstate( globalstate ),
1421         m_state_select0( select0 ),
1422         m_state_select1( select1 ),
1423         m_viewer( viewer ){
1424         ASSERT_NOTNULL( select0 );
1425         ASSERT_NOTNULL( select1 );
1426         m_state_stack.push_back( state_type() );
1427 }
1428
1429 void SetState( Shader* state, EStyle style ){
1430         ASSERT_NOTNULL( state );
1431         if ( style == eFullMaterials ) {
1432                 m_state_stack.back().m_state = state;
1433         }
1434 }
1435 EStyle getStyle() const {
1436         return eFullMaterials;
1437 }
1438 void PushState(){
1439         m_state_stack.push_back( m_state_stack.back() );
1440 }
1441 void PopState(){
1442         ASSERT_MESSAGE( !m_state_stack.empty(), "popping empty stack" );
1443         m_state_stack.pop_back();
1444 }
1445 void Highlight( EHighlightMode mode, bool bEnable = true ){
1446         if ( bEnable ) {
1447                 m_state_stack.back().m_highlight |= mode;
1448         } else {
1449                 m_state_stack.back().m_highlight &= ~mode;
1450         }
1451 }
1452 void setLights( const LightList& lights ){
1453         m_state_stack.back().m_lights = &lights;
1454 }
1455 void addRenderable( const OpenGLRenderable& renderable, const Matrix4& world ){
1456         if ( m_state_stack.back().m_highlight & ePrimitive ) {
1457                 m_state_select0->addRenderable( renderable, world, m_state_stack.back().m_lights );
1458         }
1459         if ( m_state_stack.back().m_highlight & eFace ) {
1460                 m_state_select1->addRenderable( renderable, world, m_state_stack.back().m_lights );
1461         }
1462
1463         m_state_stack.back().m_state->addRenderable( renderable, world, m_state_stack.back().m_lights );
1464 }
1465
1466 void render( const Matrix4& modelview, const Matrix4& projection ){
1467         GlobalShaderCache().render( m_globalstate, modelview, projection, m_viewer );
1468 }
1469 };
1470
1471 /*
1472    ==============
1473    Cam_Draw
1474    ==============
1475  */
1476 /*
1477 void ShowStatsToggle(){
1478         g_camwindow_globals_private.m_showStats ^= 1;
1479 }
1480
1481 void ShowStatsExport( const Callback<void(bool)> &importer ){
1482         importer( g_camwindow_globals_private.m_showStats );
1483 }
1484
1485 FreeCaller<void(const Callback<void(bool)>&), ShowStatsExport> g_show_stats_caller;
1486 Callback<void(const Callback<void(bool)> &)> g_show_stats_callback( g_show_stats_caller );
1487 ToggleItem g_show_stats( g_show_stats_callback );
1488 */
1489
1490 void ShowStatsToggle(){
1491         g_camwindow_globals_private.m_showStats ^= 1;
1492 //      g_show_stats.update();
1493         UpdateAllWindows();
1494 }
1495 typedef FreeCaller<void(), ShowStatsToggle> ShowStatsToggleCaller;
1496 void ShowStatsExport( const Callback<void(bool)> & importer ){
1497         importer( g_camwindow_globals_private.m_showStats );
1498 }
1499 typedef FreeCaller<void(const Callback<void(bool)> &), ShowStatsExport> ShowStatsExportCaller;
1500
1501 ShowStatsExportCaller g_show_stats_caller;
1502 Callback<void(const Callback<void(bool)> &)> g_show_stats_callback( g_show_stats_caller );
1503 ToggleItem g_show_stats( g_show_stats_callback );
1504
1505 void CamWnd::Cam_Draw(){
1506         glViewport( 0, 0, m_Camera.width, m_Camera.height );
1507 #if 0
1508         GLint viewprt[4];
1509         glGetIntegerv( GL_VIEWPORT, viewprt );
1510 #endif
1511
1512         // enable depth buffer writes
1513         glDepthMask( GL_TRUE );
1514         glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
1515
1516         Vector3 clearColour( 0, 0, 0 );
1517         if ( m_Camera.draw_mode != cd_lighting ) {
1518                 clearColour = g_camwindow_globals.color_cameraback;
1519         }
1520
1521         glClearColor( clearColour[0], clearColour[1], clearColour[2], 0 );
1522         glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
1523
1524         extern void Renderer_ResetStats();
1525         Renderer_ResetStats();
1526         extern void Cull_ResetStats();
1527         Cull_ResetStats();
1528
1529         glMatrixMode( GL_PROJECTION );
1530         glLoadMatrixf( reinterpret_cast<const float*>( &m_Camera.projection ) );
1531
1532         glMatrixMode( GL_MODELVIEW );
1533         glLoadMatrixf( reinterpret_cast<const float*>( &m_Camera.modelview ) );
1534
1535
1536         // one directional light source directly behind the viewer
1537         {
1538                 GLfloat inverse_cam_dir[4], ambient[4], diffuse[4]; //, material[4];
1539
1540                 ambient[0] = ambient[1] = ambient[2] = 0.4f;
1541                 ambient[3] = 1.0f;
1542                 diffuse[0] = diffuse[1] = diffuse[2] = 0.4f;
1543                 diffuse[3] = 1.0f;
1544                 //material[0] = material[1] = material[2] = 0.8f;
1545                 //material[3] = 1.0f;
1546
1547                 inverse_cam_dir[0] = m_Camera.vpn[0];
1548                 inverse_cam_dir[1] = m_Camera.vpn[1];
1549                 inverse_cam_dir[2] = m_Camera.vpn[2];
1550                 inverse_cam_dir[3] = 0;
1551
1552                 glLightfv( GL_LIGHT0, GL_POSITION, inverse_cam_dir );
1553
1554                 glLightfv( GL_LIGHT0, GL_AMBIENT, ambient );
1555                 glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuse );
1556
1557                 glEnable( GL_LIGHT0 );
1558         }
1559
1560
1561         unsigned int globalstate = RENDER_DEPTHTEST | RENDER_COLOURWRITE | RENDER_DEPTHWRITE | RENDER_ALPHATEST | RENDER_BLEND | RENDER_CULLFACE | RENDER_COLOURARRAY | RENDER_OFFSETLINE | RENDER_POLYGONSMOOTH | RENDER_LINESMOOTH | RENDER_FOG | RENDER_COLOURCHANGE;
1562         switch ( m_Camera.draw_mode )
1563         {
1564         case cd_wire:
1565                 break;
1566         case cd_solid:
1567                 globalstate |= RENDER_FILL
1568                                            | RENDER_LIGHTING
1569                                            | RENDER_SMOOTH
1570                                            | RENDER_SCALED;
1571                 break;
1572         case cd_texture:
1573                 globalstate |= RENDER_FILL
1574                                            | RENDER_LIGHTING
1575                                            | RENDER_TEXTURE
1576                                            | RENDER_SMOOTH
1577                                            | RENDER_SCALED;
1578                 break;
1579         case cd_lighting:
1580                 globalstate |= RENDER_FILL
1581                                            | RENDER_LIGHTING
1582                                            | RENDER_TEXTURE
1583                                            | RENDER_SMOOTH
1584                                            | RENDER_SCALED
1585                                            | RENDER_BUMP
1586                                            | RENDER_PROGRAM
1587                                            | RENDER_SCREEN;
1588                 break;
1589         default:
1590                 globalstate = 0;
1591                 break;
1592         }
1593
1594 //      if ( !g_xywindow_globals.m_bNoStipple ) {
1595                 globalstate |= RENDER_LINESTIPPLE | RENDER_POLYGONSTIPPLE;
1596 //      }
1597
1598         {
1599                 CamRenderer renderer( globalstate, m_state_select2, m_state_select1, m_view.getViewer() );
1600
1601                 Scene_Render( renderer, m_view );
1602
1603                 renderer.render( m_Camera.modelview, m_Camera.projection );
1604         }
1605
1606         // prepare for 2d stuff
1607         glColor4f( 1, 1, 1, 1 );
1608         glDisable( GL_BLEND );
1609         glMatrixMode( GL_PROJECTION );
1610         glLoadIdentity();
1611         glOrtho( 0, (float)m_Camera.width, 0, (float)m_Camera.height, -100, 100 );
1612         glScalef( 1, -1, 1 );
1613         glTranslatef( 0, -(float)m_Camera.height, 0 );
1614         glMatrixMode( GL_MODELVIEW );
1615         glLoadIdentity();
1616
1617         if ( GlobalOpenGL().GL_1_3() ) {
1618                 glClientActiveTexture( GL_TEXTURE0 );
1619                 glActiveTexture( GL_TEXTURE0 );
1620         }
1621
1622         glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1623         glDisableClientState( GL_NORMAL_ARRAY );
1624         glDisableClientState( GL_COLOR_ARRAY );
1625
1626         glDisable( GL_TEXTURE_2D );
1627         glDisable( GL_LIGHTING );
1628         glDisable( GL_COLOR_MATERIAL );
1629         glDisable( GL_DEPTH_TEST );
1630         glColor3f( 1.f, 1.f, 1.f );
1631         glLineWidth( 1 );
1632
1633         // draw the crosshair
1634         if ( m_bFreeMove ) {
1635                 glBegin( GL_LINES );
1636                 glVertex2f( (float)m_Camera.width / 2.f, (float)m_Camera.height / 2.f + 6 );
1637                 glVertex2f( (float)m_Camera.width / 2.f, (float)m_Camera.height / 2.f + 2 );
1638                 glVertex2f( (float)m_Camera.width / 2.f, (float)m_Camera.height / 2.f - 6 );
1639                 glVertex2f( (float)m_Camera.width / 2.f, (float)m_Camera.height / 2.f - 2 );
1640                 glVertex2f( (float)m_Camera.width / 2.f + 6, (float)m_Camera.height / 2.f );
1641                 glVertex2f( (float)m_Camera.width / 2.f + 2, (float)m_Camera.height / 2.f );
1642                 glVertex2f( (float)m_Camera.width / 2.f - 6, (float)m_Camera.height / 2.f );
1643                 glVertex2f( (float)m_Camera.width / 2.f - 2, (float)m_Camera.height / 2.f );
1644                 glEnd();
1645         }
1646
1647         if ( g_camwindow_globals_private.m_showStats ) {
1648                 glRasterPos3f( 1.0f, static_cast<float>( m_Camera.height ) - GlobalOpenGL().m_font->getPixelDescent(), 0.0f );
1649                 extern const char* Renderer_GetStats();
1650                 GlobalOpenGL().drawString( Renderer_GetStats() );
1651
1652                 glRasterPos3f( 1.0f, static_cast<float>( m_Camera.height ) - GlobalOpenGL().m_font->getPixelDescent() - GlobalOpenGL().m_font->getPixelHeight(), 0.0f );
1653                 extern const char* Cull_GetStats();
1654                 GlobalOpenGL().drawString( Cull_GetStats() );
1655         }
1656
1657         // bind back to the default texture so that we don't have problems
1658         // elsewhere using/modifying texture maps between contexts
1659         glBindTexture( GL_TEXTURE_2D, 0 );
1660 }
1661
1662 void CamWnd::draw(){
1663         m_drawing = true;
1664
1665         //globalOutputStream() << "draw...\n";
1666         if ( glwidget_make_current( m_gl_widget ) != FALSE ) {
1667                 if ( Map_Valid( g_map ) && ScreenUpdates_Enabled() ) {
1668                         GlobalOpenGL_debugAssertNoErrors();
1669                         Cam_Draw();
1670                         GlobalOpenGL_debugAssertNoErrors();
1671                         //qglFinish();
1672
1673                         m_XORRectangle.set( rectangle_t() );
1674                 }
1675
1676                 glwidget_swap_buffers( m_gl_widget );
1677         }
1678
1679         m_drawing = false;
1680 }
1681
1682 void CamWnd::BenchMark(){
1683         double dStart = Sys_DoubleTime();
1684         for ( int i = 0 ; i < 100 ; i++ )
1685         {
1686                 Vector3 angles;
1687                 angles[CAMERA_ROLL] = 0;
1688                 angles[CAMERA_PITCH] = 0;
1689                 angles[CAMERA_YAW] = i * 360.0f / 100.0f;
1690                 Camera_setAngles( *this, angles );
1691         }
1692         double dEnd = Sys_DoubleTime();
1693         globalOutputStream() << FloatFormat( dEnd - dStart, 5, 2 ) << " seconds\n";
1694 }
1695
1696
1697 void GlobalCamera_ResetAngles(){
1698         CamWnd& camwnd = *g_camwnd;
1699         Vector3 angles;
1700         angles[CAMERA_ROLL] = angles[CAMERA_PITCH] = 0;
1701         angles[CAMERA_YAW] = 22.5f * floorf( ( Camera_getAngles( camwnd )[CAMERA_YAW] + 11 ) / 22.5f );
1702         Camera_setAngles( camwnd, angles );
1703 }
1704
1705 #include "select.h"
1706
1707 void GlobalCamera_FocusOnSelected(){
1708         CamWnd& camwnd = *g_camwnd;
1709
1710         Vector3 angles( Camera_getAngles( camwnd ) );
1711         Vector3 radangles( degrees_to_radians( angles[0] ), degrees_to_radians( angles[1] ), degrees_to_radians( angles[2] ) );
1712         Vector3 viewvector;
1713         viewvector[0] = cos( radangles[1] ) * cos( radangles[0] );
1714         viewvector[1] = sin( radangles[1] ) * cos( radangles[0] );
1715         viewvector[2] = sin( radangles[0] );
1716
1717         Vector3 camorigin( Camera_getOrigin( camwnd ) );
1718
1719         AABB aabb( aabb_for_minmax( Select_getWorkZone().d_work_min, Select_getWorkZone().d_work_max ) );
1720
1721         View& view = *( camwnd.getCamera().m_view );
1722
1723         Plane3 frustumPlanes[4];
1724         frustumPlanes[0] = plane3_translated( view.getFrustum().left, camorigin - aabb.origin );
1725         frustumPlanes[1] = plane3_translated( view.getFrustum().right, camorigin - aabb.origin );
1726         frustumPlanes[2] = plane3_translated( view.getFrustum().top, camorigin - aabb.origin );
1727         frustumPlanes[3] = plane3_translated( view.getFrustum().bottom, camorigin - aabb.origin );
1728
1729         float offset = 64.0f;
1730
1731         Vector3 corners[8];
1732         aabb_corners( aabb, corners );
1733
1734         for ( size_t i = 0; i < 4; ++i ){
1735                 for ( size_t j = 0; j < 8; ++j ){
1736                         Ray ray( aabb.origin, -viewvector );
1737                         //Plane3 newplane( frustumPlanes[i].normal(), vector3_dot( frustumPlanes[i].normal(), corners[j] - frustumPlanes[i].normal() * 16.0f ) );
1738                         Plane3 newplane( frustumPlanes[i].normal(), vector3_dot( frustumPlanes[i].normal(), corners[j] ) );
1739                         float d = vector3_dot( ray.direction, newplane.normal() );
1740                         if( d != 0 ){
1741                                 float s = vector3_dot( newplane.normal() * newplane.dist() - ray.origin, newplane.normal() ) / d;
1742                                 offset = std::max( offset, s );
1743                         }
1744                 }
1745         }
1746         Camera_setOrigin( camwnd, aabb.origin - viewvector * offset );
1747 }
1748
1749 void Camera_ChangeFloorUp(){
1750         CamWnd& camwnd = *g_camwnd;
1751         camwnd.Cam_ChangeFloor( true );
1752 }
1753
1754 void Camera_ChangeFloorDown(){
1755         CamWnd& camwnd = *g_camwnd;
1756         camwnd.Cam_ChangeFloor( false );
1757 }
1758
1759 void Camera_CubeIn(){
1760         CamWnd& camwnd = *g_camwnd;
1761         g_camwindow_globals.m_nCubicScale--;
1762         if ( g_camwindow_globals.m_nCubicScale < 1 ) {
1763                 g_camwindow_globals.m_nCubicScale = 1;
1764         }
1765         Camera_updateProjection( camwnd.getCamera() );
1766         CamWnd_Update( camwnd );
1767         g_pParentWnd->SetGridStatus();
1768 }
1769
1770 void Camera_CubeOut(){
1771         CamWnd& camwnd = *g_camwnd;
1772         g_camwindow_globals.m_nCubicScale++;
1773         if ( g_camwindow_globals.m_nCubicScale > 46 ) {
1774                 g_camwindow_globals.m_nCubicScale = 46;
1775         }
1776         Camera_updateProjection( camwnd.getCamera() );
1777         CamWnd_Update( camwnd );
1778         g_pParentWnd->SetGridStatus();
1779 }
1780
1781 bool Camera_GetFarClip(){
1782         return g_camwindow_globals_private.m_bCubicClipping;
1783 }
1784
1785 ConstReferenceCaller<bool, void(const Callback<void(bool)> &), PropertyImpl<bool>::Export> g_getfarclip_caller( g_camwindow_globals_private.m_bCubicClipping );
1786 ToggleItem g_getfarclip_item( g_getfarclip_caller );
1787
1788 void Camera_SetFarClip( bool value ){
1789         CamWnd& camwnd = *g_camwnd;
1790         g_camwindow_globals_private.m_bCubicClipping = value;
1791         g_getfarclip_item.update();
1792         Camera_updateProjection( camwnd.getCamera() );
1793         CamWnd_Update( camwnd );
1794 }
1795
1796 struct Camera_FarClip {
1797         static void Export(const Callback<void(bool)> &returnz) {
1798                 returnz(g_camwindow_globals_private.m_bCubicClipping);
1799         }
1800
1801         static void Import(bool value) {
1802                 Camera_SetFarClip(value);
1803         }
1804 };
1805
1806 void Camera_ToggleFarClip(){
1807         Camera_SetFarClip( !Camera_GetFarClip() );
1808 }
1809
1810
1811 void CamWnd_constructToolbar( ui::Toolbar toolbar ){
1812         toolbar_append_toggle_button( toolbar, "Cubic clip the camera view (Ctrl + \\)", "view_cubicclipping.png", "ToggleCubicClip" );
1813 }
1814
1815 void CamWnd_registerShortcuts(){
1816         toggle_add_accelerator( "ToggleCubicClip" );
1817
1818         if ( g_pGameDescription->mGameType == "doom3" ) {
1819                 command_connect_accelerator( "TogglePreview" );
1820         }
1821
1822         command_connect_accelerator( "FOVInc" );
1823         command_connect_accelerator( "FOVDec" );
1824         command_connect_accelerator( "CameraSpeedInc" );
1825         command_connect_accelerator( "CameraSpeedDec" );
1826 }
1827
1828
1829 void GlobalCamera_Benchmark(){
1830         CamWnd& camwnd = *g_camwnd;
1831         camwnd.BenchMark();
1832 }
1833
1834 void GlobalCamera_Update(){
1835         CamWnd& camwnd = *g_camwnd;
1836         CamWnd_Update( camwnd );
1837 }
1838
1839 camera_draw_mode CamWnd_GetMode(){
1840         return camera_t::draw_mode;
1841 }
1842 void CamWnd_SetMode( camera_draw_mode mode ){
1843         ShaderCache_setBumpEnabled( mode == cd_lighting );
1844         camera_t::draw_mode = mode;
1845         if ( g_camwnd != 0 ) {
1846                 CamWnd_Update( *g_camwnd );
1847         }
1848 }
1849
1850 void CamWnd_TogglePreview( void ){
1851         // gametype must be doom3 for this function to work
1852         // if the gametype is not doom3 something is wrong with the
1853         // global command list or somebody else calls this function.
1854         ASSERT_MESSAGE( g_pGameDescription->mGameType == "doom3", "CamWnd_TogglePreview called although mGameType is not doom3 compatible" );
1855
1856         // switch between textured and lighting mode
1857         CamWnd_SetMode( ( CamWnd_GetMode() == cd_lighting ) ? cd_texture : cd_lighting );
1858 }
1859
1860
1861 CameraModel* g_camera_model = 0;
1862
1863 void CamWnd_LookThroughCamera( CamWnd& camwnd ){
1864         if ( g_camera_model != 0 ) {
1865                 CamWnd_Add_Handlers_Move( camwnd );
1866                 g_camera_model->setCameraView( 0, Callback<void()>() );
1867                 g_camera_model = 0;
1868                 Camera_updateModelview( camwnd.getCamera() );
1869                 Camera_updateProjection( camwnd.getCamera() );
1870                 CamWnd_Update( camwnd );
1871         }
1872 }
1873
1874 inline CameraModel* Instance_getCameraModel( scene::Instance& instance ){
1875         return InstanceTypeCast<CameraModel>::cast( instance );
1876 }
1877
1878 void CamWnd_LookThroughSelected( CamWnd& camwnd ){
1879         if ( g_camera_model != 0 ) {
1880                 CamWnd_LookThroughCamera( camwnd );
1881         }
1882
1883         if ( GlobalSelectionSystem().countSelected() != 0 ) {
1884                 scene::Instance& instance = GlobalSelectionSystem().ultimateSelected();
1885                 CameraModel* cameraModel = Instance_getCameraModel( instance );
1886                 if ( cameraModel != 0 ) {
1887                         CamWnd_Remove_Handlers_Move( camwnd );
1888                         g_camera_model = cameraModel;
1889                         g_camera_model->setCameraView( &camwnd.getCameraView(), ReferenceCaller<CamWnd, void(), CamWnd_LookThroughCamera>( camwnd ) );
1890                 }
1891         }
1892 }
1893
1894 void GlobalCamera_LookThroughSelected(){
1895         CamWnd_LookThroughSelected( *g_camwnd );
1896 }
1897
1898 void GlobalCamera_LookThroughCamera(){
1899         CamWnd_LookThroughCamera( *g_camwnd );
1900 }
1901
1902 struct RenderMode {
1903         static void Export(const Callback<void(int)> &returnz) {
1904                 switch (CamWnd_GetMode()) {
1905                         case cd_wire:
1906                                 returnz(0);
1907                                 break;
1908                         case cd_solid:
1909                                 returnz(1);
1910                                 break;
1911                         case cd_texture:
1912                                 returnz(2);
1913                                 break;
1914                         case cd_lighting:
1915                                 returnz(3);
1916                                 break;
1917                 }
1918         }
1919
1920         static void Import(int value) {
1921                 switch (value) {
1922         case 0:
1923                 CamWnd_SetMode( cd_wire );
1924                 break;
1925         case 1:
1926                 CamWnd_SetMode( cd_solid );
1927                 break;
1928         case 2:
1929                 CamWnd_SetMode( cd_texture );
1930                 break;
1931         case 3:
1932                 CamWnd_SetMode( cd_lighting );
1933                 break;
1934         default:
1935                 CamWnd_SetMode( cd_texture );
1936         }
1937 }
1938 };
1939
1940 void Camera_constructPreferences( PreferencesPage& page ){
1941         page.appendSlider( "FOV", g_camwindow_globals_private.m_nFOV, TRUE, 0, 0, 100, MIN_FOV, MAX_FOV, 1, 10 );
1942         page.appendSlider( "Movement Speed", g_camwindow_globals_private.m_nMoveSpeed, TRUE, 0, 0, 100, MIN_CAM_SPEED, MAX_CAM_SPEED, 1, 10 );
1943         page.appendCheckBox( "", "Link strafe speed to movement speed", g_camwindow_globals_private.m_bCamLinkSpeed );
1944         page.appendSlider( "Rotation Speed", g_camwindow_globals_private.m_nAngleSpeed, TRUE, 0, 0, 3, 1, 180, 1, 10 );
1945         page.appendCheckBox( "", "Invert mouse vertical axis", g_camwindow_globals_private.m_bCamInverseMouse );
1946         page.appendCheckBox( "", "Zoom In to Mouse pointer", g_camwindow_globals.m_bZoomInToPointer );
1947         page.appendCheckBox(
1948                 "", "Discrete movement",
1949                 make_property<CamWnd_Move_Discrete>()
1950                 );
1951         page.appendCheckBox(
1952                 "", "Enable far-clip plane",
1953                 make_property<Camera_FarClip>()
1954                 );
1955
1956         if ( g_pGameDescription->mGameType == "doom3" ) {
1957                 const char* render_mode[] = { "Wireframe", "Flatshade", "Textured", "Lighting" };
1958
1959                 page.appendCombo(
1960                         "Render Mode",
1961                         STRING_ARRAY_RANGE( render_mode ),
1962                         make_property<RenderMode>()
1963                         );
1964         }
1965         else
1966         {
1967                 const char* render_mode[] = { "Wireframe", "Flatshade", "Textured" };
1968
1969                 page.appendCombo(
1970                         "Render Mode",
1971                         STRING_ARRAY_RANGE( render_mode ),
1972                         make_property<RenderMode>()
1973                         );
1974         }
1975
1976         const char* strafe_mode[] = { "None", "Up", "Forward", "Both", "Both Inverted" };
1977
1978         page.appendCombo(
1979                 "Strafe Mode",
1980                 g_camwindow_globals_private.m_nStrafeMode,
1981                 STRING_ARRAY_RANGE( strafe_mode )
1982                 );
1983 }
1984 void Camera_constructPage( PreferenceGroup& group ){
1985         PreferencesPage page( group.createPage( "Camera", "Camera View Preferences" ) );
1986         Camera_constructPreferences( page );
1987 }
1988 void Camera_registerPreferencesPage(){
1989         PreferencesDialog_addSettingsPage( makeCallbackF(Camera_constructPage) );
1990 }
1991
1992 #include "preferencesystem.h"
1993 #include "stringio.h"
1994 #include "dialog.h"
1995
1996 void FOV_increase(){
1997         CamWnd& camwnd = *g_camwnd;
1998         if ( g_camwindow_globals_private.m_nFOV <= ( MAX_FOV - FOV_STEP - 10 ) ) {
1999                 g_camwindow_globals_private.m_nFOV += FOV_STEP;
2000         }
2001         else {
2002                 g_camwindow_globals_private.m_nFOV = MAX_FOV - 10;
2003         }
2004         Camera_updateProjection( camwnd.getCamera() );
2005         CamWnd_Update( camwnd );
2006 }
2007
2008 void FOV_decrease(){
2009         CamWnd& camwnd = *g_camwnd;
2010         if ( g_camwindow_globals_private.m_nFOV >= ( MIN_FOV + FOV_STEP ) ) {
2011                 g_camwindow_globals_private.m_nFOV -= FOV_STEP;
2012         }
2013         else {
2014                 g_camwindow_globals_private.m_nFOV = MIN_FOV;
2015         }
2016         Camera_updateProjection( camwnd.getCamera() );
2017         CamWnd_Update( camwnd );
2018 }
2019
2020
2021 void CameraSpeed_increase(){
2022         if ( g_camwindow_globals_private.m_nMoveSpeed <= ( MAX_CAM_SPEED - CAM_SPEED_STEP - 10 ) ) {
2023                 g_camwindow_globals_private.m_nMoveSpeed += CAM_SPEED_STEP;
2024         }
2025         else {
2026                 g_camwindow_globals_private.m_nMoveSpeed = MAX_CAM_SPEED - 10;
2027         }
2028 }
2029
2030 void CameraSpeed_decrease(){
2031         if ( g_camwindow_globals_private.m_nMoveSpeed >= ( MIN_CAM_SPEED + CAM_SPEED_STEP ) ) {
2032                 g_camwindow_globals_private.m_nMoveSpeed -= CAM_SPEED_STEP;
2033         }
2034         else {
2035                 g_camwindow_globals_private.m_nMoveSpeed = MIN_CAM_SPEED;
2036         }
2037 }
2038
2039 /// \brief Initialisation for things that have the same lifespan as this module.
2040 void CamWnd_Construct(){
2041         GlobalCommands_insert( "CenterView", makeCallbackF(GlobalCamera_ResetAngles), Accelerator( GDK_KEY_End ) );
2042         GlobalCommands_insert( "CameraFocusOnSelected", makeCallbackF( GlobalCamera_FocusOnSelected ), Accelerator( GDK_Tab ) );
2043
2044         GlobalToggles_insert( "ToggleCubicClip", makeCallbackF(Camera_ToggleFarClip), ToggleItem::AddCallbackCaller( g_getfarclip_item ), Accelerator( '\\', (GdkModifierType)GDK_CONTROL_MASK ) );
2045         GlobalCommands_insert( "CubicClipZoomIn", makeCallbackF(Camera_CubeIn), Accelerator( '[', (GdkModifierType)GDK_CONTROL_MASK ) );
2046         GlobalCommands_insert( "CubicClipZoomOut", makeCallbackF(Camera_CubeOut), Accelerator( ']', (GdkModifierType)GDK_CONTROL_MASK ) );
2047
2048         GlobalCommands_insert( "UpFloor", makeCallbackF(Camera_ChangeFloorUp), Accelerator( GDK_KEY_Prior ) );
2049         GlobalCommands_insert( "DownFloor", makeCallbackF(Camera_ChangeFloorDown), Accelerator( GDK_KEY_Next ) );
2050
2051         GlobalToggles_insert( "ToggleCamera", ToggleShown::ToggleCaller( g_camera_shown ), ToggleItem::AddCallbackCaller( g_camera_shown.m_item ), Accelerator( 'C', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
2052 //      GlobalCommands_insert( "LookThroughSelected", makeCallbackF(GlobalCamera_LookThroughSelected) );
2053 //      GlobalCommands_insert( "LookThroughCamera", makeCallbackF(GlobalCamera_LookThroughCamera) );
2054
2055         if ( g_pGameDescription->mGameType == "doom3" ) {
2056                 GlobalCommands_insert( "TogglePreview", makeCallbackF(CamWnd_TogglePreview), Accelerator( GDK_KEY_F3 ) );
2057         }
2058
2059         GlobalCommands_insert( "FOVInc", makeCallbackF(FOV_increase), Accelerator( GDK_KEY_KP_Multiply, (GdkModifierType)GDK_SHIFT_MASK ) );
2060         GlobalCommands_insert( "FOVDec", makeCallbackF(FOV_decrease), Accelerator( GDK_KEY_KP_Divide, (GdkModifierType)GDK_SHIFT_MASK ) );
2061
2062         GlobalCommands_insert( "CameraSpeedInc", makeCallbackF(CameraSpeed_increase), Accelerator( GDK_KEY_KP_Add, (GdkModifierType)GDK_SHIFT_MASK ) );
2063         GlobalCommands_insert( "CameraSpeedDec", makeCallbackF(CameraSpeed_decrease), Accelerator( GDK_KEY_KP_Subtract, (GdkModifierType)GDK_SHIFT_MASK ) );
2064
2065         GlobalShortcuts_insert( "CameraForward", Accelerator( GDK_KEY_Up ) );
2066         GlobalShortcuts_insert( "CameraBack", Accelerator( GDK_KEY_Down ) );
2067         GlobalShortcuts_insert( "CameraLeft", Accelerator( GDK_KEY_Left ) );
2068         GlobalShortcuts_insert( "CameraRight", Accelerator( GDK_KEY_Right ) );
2069         GlobalShortcuts_insert( "CameraStrafeRight", Accelerator( 'D' ) );
2070         GlobalShortcuts_insert( "CameraStrafeLeft", Accelerator( 'A' ) );
2071
2072         GlobalShortcuts_insert( "CameraUp", accelerator_null() );
2073         GlobalShortcuts_insert( "CameraDown", accelerator_null() );
2074         GlobalShortcuts_insert( "CameraAngleUp", accelerator_null() );
2075         GlobalShortcuts_insert( "CameraAngleDown", accelerator_null() );
2076
2077         GlobalShortcuts_insert( "CameraFreeMoveForward", Accelerator( 'W' ) );
2078         GlobalShortcuts_insert( "CameraFreeMoveBack", Accelerator( 'S' ) );
2079         GlobalShortcuts_insert( "CameraFreeMoveLeft", Accelerator( 'A' ) );
2080         GlobalShortcuts_insert( "CameraFreeMoveRight", Accelerator( 'D' ) );
2081
2082         GlobalShortcuts_insert( "CameraFreeMoveForward2", Accelerator( GDK_KEY_Up ) );
2083         GlobalShortcuts_insert( "CameraFreeMoveBack2", Accelerator( GDK_KEY_Down ) );
2084         GlobalShortcuts_insert( "CameraFreeMoveLeft2", Accelerator( GDK_KEY_Left ) );
2085         GlobalShortcuts_insert( "CameraFreeMoveRight2", Accelerator( GDK_KEY_Right ) );
2086
2087         GlobalShortcuts_insert( "CameraFreeMoveUp", accelerator_null() );
2088         GlobalShortcuts_insert( "CameraFreeMoveDown", accelerator_null() );
2089
2090         GlobalToggles_insert( "ShowStats", makeCallbackF(ShowStatsToggle), ToggleItem::AddCallbackCaller( g_show_stats ) );
2091
2092         GlobalPreferenceSystem().registerPreference( "ShowStats", make_property_string( g_camwindow_globals_private.m_showStats ) );
2093         GlobalPreferenceSystem().registerPreference( "FOV", make_property_string( g_camwindow_globals_private.m_nFOV ) );
2094         GlobalPreferenceSystem().registerPreference( "MoveSpeed", make_property_string( g_camwindow_globals_private.m_nMoveSpeed ) );
2095         GlobalPreferenceSystem().registerPreference( "CamLinkSpeed", make_property_string( g_camwindow_globals_private.m_bCamLinkSpeed ) );
2096         GlobalPreferenceSystem().registerPreference( "AngleSpeed", make_property_string( g_camwindow_globals_private.m_nAngleSpeed ) );
2097         GlobalPreferenceSystem().registerPreference( "CamInverseMouse", make_property_string( g_camwindow_globals_private.m_bCamInverseMouse ) );
2098         GlobalPreferenceSystem().registerPreference( "CamDiscrete", make_property_string<CamWnd_Move_Discrete>());
2099         GlobalPreferenceSystem().registerPreference( "CubicClipping", make_property_string( g_camwindow_globals_private.m_bCubicClipping ) );
2100         GlobalPreferenceSystem().registerPreference( "CubicScale", make_property_string( g_camwindow_globals.m_nCubicScale ) );
2101         GlobalPreferenceSystem().registerPreference( "SI_Colors4", make_property_string( g_camwindow_globals.color_cameraback ) );
2102         GlobalPreferenceSystem().registerPreference( "SI_Colors12", make_property_string( g_camwindow_globals.color_selbrushes3d ) );
2103         GlobalPreferenceSystem().registerPreference( "CameraRenderMode", make_property_string<RenderMode>() );
2104         GlobalPreferenceSystem().registerPreference( "StrafeMode", make_property_string( g_camwindow_globals_private.m_nStrafeMode ) );
2105         GlobalPreferenceSystem().registerPreference( "3DZoomInToPointer", make_property_string( g_camwindow_globals.m_bZoomInToPointer ) );
2106
2107         CamWnd_constructStatic();
2108
2109         Camera_registerPreferencesPage();
2110 }
2111 void CamWnd_Destroy(){
2112         CamWnd_destroyStatic();
2113 }