]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/camwindow.cpp
Merge commit '19c67e1114f894604c3346fe1af9b015184328be' 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                 observer->onMouseDown( WindowVector_forDouble( event->x, event->y ), button_for_button( event->button ), modifiers_for_state( event->state ) );
848         }
849         return FALSE;
850 }
851
852 gboolean selection_button_release( ui::Widget widget, GdkEventButton* event, WindowObserver* observer ){
853         if ( event->type == GDK_BUTTON_RELEASE ) {
854                 observer->onMouseUp( WindowVector_forDouble( event->x, event->y ), button_for_button( event->button ), modifiers_for_state( event->state ) );
855         }
856         return FALSE;
857 }
858
859 void selection_motion( gdouble x, gdouble y, guint state, void* data ){
860         //globalOutputStream() << "motion... ";
861         reinterpret_cast<WindowObserver*>( data )->onMouseMotion( WindowVector_forDouble( x, y ), modifiers_for_state( state ) );
862 }
863
864 inline WindowVector windowvector_for_widget_centre( ui::Widget widget ){
865         auto allocation = widget.dimensions();
866         return WindowVector( static_cast<float>( allocation.width / 2 ), static_cast<float>(allocation.height / 2 ) );
867 }
868
869 gboolean selection_button_press_freemove( ui::Widget widget, GdkEventButton* event, WindowObserver* observer ){
870         if ( event->type == GDK_BUTTON_PRESS ) {
871                 observer->onMouseDown( windowvector_for_widget_centre( widget ), button_for_button( event->button ), modifiers_for_state( event->state ) );
872         }
873         return FALSE;
874 }
875
876 gboolean selection_button_release_freemove( ui::Widget widget, GdkEventButton* event, WindowObserver* observer ){
877         if ( event->type == GDK_BUTTON_RELEASE ) {
878                 observer->onMouseUp( windowvector_for_widget_centre( widget ), button_for_button( event->button ), modifiers_for_state( event->state ) );
879         }
880         return FALSE;
881 }
882
883 gboolean selection_motion_freemove( ui::Widget widget, GdkEventMotion *event, WindowObserver* observer ){
884         observer->onMouseMotion( windowvector_for_widget_centre( widget ), modifiers_for_state( event->state ) );
885         return FALSE;
886 }
887
888 gboolean wheelmove_scroll( ui::Widget widget, GdkEventScroll* event, CamWnd* camwnd ){
889         if ( event->direction == GDK_SCROLL_UP ) {
890                 Camera_Freemove_updateAxes( camwnd->getCamera() );
891                 if( camwnd->m_bFreeMove || !g_camwindow_globals.m_bZoomInToPointer ){
892                         Camera_setOrigin( *camwnd, vector3_added( Camera_getOrigin( *camwnd ), vector3_scaled( camwnd->getCamera().forward, static_cast<float>( g_camwindow_globals_private.m_nMoveSpeed ) ) ) );
893                 }
894                 else{
895                         //Matrix4 maa = matrix4_multiplied_by_matrix4( camwnd->getCamera().projection, camwnd->getCamera().modelview );
896                         Matrix4 maa = camwnd->getCamera().m_view->GetViewMatrix();
897                         matrix4_affine_invert( maa );
898
899                         float x = static_cast<float>( event->x );
900                         float y = static_cast<float>( event->y );
901                         Vector3 normalized;
902
903                         normalized[0] = 2.0f * ( x ) / static_cast<float>( camwnd->getCamera().width ) - 1.0f;
904                         normalized[1] = 2.0f * ( y )/ static_cast<float>( camwnd->getCamera().height ) - 1.0f;
905                         normalized[1] *= -1.f;
906                         normalized[2] = 0.f;
907
908                         normalized *= 16.0f;
909                                 //globalOutputStream() << normalized << " normalized    ";
910                         matrix4_transform_point( maa, normalized );
911                                 //globalOutputStream() << normalized << "\n";
912                         Vector3 norm = vector3_normalised( normalized - Camera_getOrigin( *camwnd ) );
913                                 //globalOutputStream() << normalized - Camera_getOrigin( *camwnd ) << "  normalized - Camera_getOrigin( *camwnd )\n";
914                                 //globalOutputStream() << norm << "  norm\n";
915                         Camera_setOrigin( *camwnd, vector3_added( Camera_getOrigin( *camwnd ), vector3_scaled( norm, static_cast<float>( g_camwindow_globals_private.m_nMoveSpeed ) ) ) );
916                 }
917         }
918         else if ( event->direction == GDK_SCROLL_DOWN ) {
919                 Camera_Freemove_updateAxes( camwnd->getCamera() );
920                 Camera_setOrigin( *camwnd, vector3_added( Camera_getOrigin( *camwnd ), vector3_scaled( camwnd->getCamera().forward, -static_cast<float>( g_camwindow_globals_private.m_nMoveSpeed ) ) ) );
921         }
922
923         return FALSE;
924 }
925
926 gboolean camera_size_allocate( ui::Widget widget, GtkAllocation* allocation, CamWnd* camwnd ){
927         camwnd->getCamera().width = allocation->width;
928         camwnd->getCamera().height = allocation->height;
929         Camera_updateProjection( camwnd->getCamera() );
930         camwnd->m_window_observer->onSizeChanged( camwnd->getCamera().width, camwnd->getCamera().height );
931         camwnd->queue_draw();
932         return FALSE;
933 }
934
935 gboolean camera_expose( ui::Widget widget, GdkEventExpose* event, gpointer data ){
936         reinterpret_cast<CamWnd*>( data )->draw();
937         return FALSE;
938 }
939
940 void KeyEvent_connect( const char* name ){
941         const KeyEvent& keyEvent = GlobalKeyEvents_find( name );
942         keydown_accelerators_add( keyEvent.m_accelerator, keyEvent.m_keyDown );
943         keyup_accelerators_add( keyEvent.m_accelerator, keyEvent.m_keyUp );
944 }
945
946 void KeyEvent_disconnect( const char* name ){
947         const KeyEvent& keyEvent = GlobalKeyEvents_find( name );
948         keydown_accelerators_remove( keyEvent.m_accelerator );
949         keyup_accelerators_remove( keyEvent.m_accelerator );
950 }
951
952 void CamWnd_registerCommands( CamWnd& camwnd ){
953         GlobalKeyEvents_insert( "CameraForward", accelerator_null(),
954                                                         ReferenceCaller<camera_t, void(), Camera_MoveForward_KeyDown>( camwnd.getCamera() ),
955                                                         ReferenceCaller<camera_t, void(), Camera_MoveForward_KeyUp>( camwnd.getCamera() )
956                                                         );
957         GlobalKeyEvents_insert( "CameraBack", accelerator_null(),
958                                                         ReferenceCaller<camera_t, void(), Camera_MoveBack_KeyDown>( camwnd.getCamera() ),
959                                                         ReferenceCaller<camera_t, void(), Camera_MoveBack_KeyUp>( camwnd.getCamera() )
960                                                         );
961         GlobalKeyEvents_insert( "CameraLeft", accelerator_null(),
962                                                         ReferenceCaller<camera_t, void(), Camera_RotateLeft_KeyDown>( camwnd.getCamera() ),
963                                                         ReferenceCaller<camera_t, void(), Camera_RotateLeft_KeyUp>( camwnd.getCamera() )
964                                                         );
965         GlobalKeyEvents_insert( "CameraRight", accelerator_null(),
966                                                         ReferenceCaller<camera_t, void(), Camera_RotateRight_KeyDown>( camwnd.getCamera() ),
967                                                         ReferenceCaller<camera_t, void(), Camera_RotateRight_KeyUp>( camwnd.getCamera() )
968                                                         );
969         GlobalKeyEvents_insert( "CameraStrafeRight", accelerator_null(),
970                                                         ReferenceCaller<camera_t, void(), Camera_MoveRight_KeyDown>( camwnd.getCamera() ),
971                                                         ReferenceCaller<camera_t, void(), Camera_MoveRight_KeyUp>( camwnd.getCamera() )
972                                                         );
973         GlobalKeyEvents_insert( "CameraStrafeLeft", accelerator_null(),
974                                                         ReferenceCaller<camera_t, void(), Camera_MoveLeft_KeyDown>( camwnd.getCamera() ),
975                                                         ReferenceCaller<camera_t, void(), Camera_MoveLeft_KeyUp>( camwnd.getCamera() )
976                                                         );
977         GlobalKeyEvents_insert( "CameraUp", accelerator_null(),
978                                                         ReferenceCaller<camera_t, void(), Camera_MoveUp_KeyDown>( camwnd.getCamera() ),
979                                                         ReferenceCaller<camera_t, void(), Camera_MoveUp_KeyUp>( camwnd.getCamera() )
980                                                         );
981         GlobalKeyEvents_insert( "CameraDown", accelerator_null(),
982                                                         ReferenceCaller<camera_t, void(), Camera_MoveDown_KeyDown>( camwnd.getCamera() ),
983                                                         ReferenceCaller<camera_t, void(), Camera_MoveDown_KeyUp>( camwnd.getCamera() )
984                                                         );
985         GlobalKeyEvents_insert( "CameraAngleUp", accelerator_null(),
986                                                         ReferenceCaller<camera_t, void(), Camera_PitchUp_KeyDown>( camwnd.getCamera() ),
987                                                         ReferenceCaller<camera_t, void(), Camera_PitchUp_KeyUp>( camwnd.getCamera() )
988                                                         );
989         GlobalKeyEvents_insert( "CameraAngleDown", accelerator_null(),
990                                                         ReferenceCaller<camera_t, void(), Camera_PitchDown_KeyDown>( camwnd.getCamera() ),
991                                                         ReferenceCaller<camera_t, void(), Camera_PitchDown_KeyUp>( camwnd.getCamera() )
992                                                         );
993
994
995         GlobalKeyEvents_insert( "CameraFreeMoveForward", accelerator_null(),
996                                                         FreeMoveCameraMoveForwardKeyDownCaller( camwnd.getCamera() ),
997                                                         FreeMoveCameraMoveForwardKeyUpCaller( camwnd.getCamera() )
998                                                         );
999         GlobalKeyEvents_insert( "CameraFreeMoveBack", accelerator_null(),
1000                                                         FreeMoveCameraMoveBackKeyDownCaller( camwnd.getCamera() ),
1001                                                         FreeMoveCameraMoveBackKeyUpCaller( camwnd.getCamera() )
1002                                                         );
1003         GlobalKeyEvents_insert( "CameraFreeMoveLeft", accelerator_null(),
1004                                                         FreeMoveCameraMoveLeftKeyDownCaller( camwnd.getCamera() ),
1005                                                         FreeMoveCameraMoveLeftKeyUpCaller( camwnd.getCamera() )
1006                                                         );
1007         GlobalKeyEvents_insert( "CameraFreeMoveRight", accelerator_null(),
1008                                                         FreeMoveCameraMoveRightKeyDownCaller( camwnd.getCamera() ),
1009                                                         FreeMoveCameraMoveRightKeyUpCaller( camwnd.getCamera() )
1010                                                         );
1011
1012         GlobalKeyEvents_insert( "CameraFreeMoveForward2", accelerator_null(),
1013                                                         FreeMoveCameraMoveForwardKeyDownCaller( camwnd.getCamera() ),
1014                                                         FreeMoveCameraMoveForwardKeyUpCaller( camwnd.getCamera() )
1015                                                         );
1016         GlobalKeyEvents_insert( "CameraFreeMoveBack2", accelerator_null(),
1017                                                         FreeMoveCameraMoveBackKeyDownCaller( camwnd.getCamera() ),
1018                                                         FreeMoveCameraMoveBackKeyUpCaller( camwnd.getCamera() )
1019                                                         );
1020         GlobalKeyEvents_insert( "CameraFreeMoveLeft2", accelerator_null(),
1021                                                         FreeMoveCameraMoveLeftKeyDownCaller( camwnd.getCamera() ),
1022                                                         FreeMoveCameraMoveLeftKeyUpCaller( camwnd.getCamera() )
1023                                                         );
1024         GlobalKeyEvents_insert( "CameraFreeMoveRight2", accelerator_null(),
1025                                                         FreeMoveCameraMoveRightKeyDownCaller( camwnd.getCamera() ),
1026                                                         FreeMoveCameraMoveRightKeyUpCaller( camwnd.getCamera() )
1027                                                         );
1028
1029         GlobalKeyEvents_insert( "CameraFreeMoveUp", accelerator_null(),
1030                                                         FreeMoveCameraMoveUpKeyDownCaller( camwnd.getCamera() ),
1031                                                         FreeMoveCameraMoveUpKeyUpCaller( camwnd.getCamera() )
1032                                                         );
1033         GlobalKeyEvents_insert( "CameraFreeMoveDown", accelerator_null(),
1034                                                         FreeMoveCameraMoveDownKeyDownCaller( camwnd.getCamera() ),
1035                                                         FreeMoveCameraMoveDownKeyUpCaller( camwnd.getCamera() )
1036                                                         );
1037
1038         GlobalCommands_insert( "CameraForward", ReferenceCaller<camera_t, void(), Camera_MoveForward_Discrete>( camwnd.getCamera() ) );
1039         GlobalCommands_insert( "CameraBack", ReferenceCaller<camera_t, void(), Camera_MoveBack_Discrete>( camwnd.getCamera() ) );
1040         GlobalCommands_insert( "CameraLeft", ReferenceCaller<camera_t, void(), Camera_RotateLeft_Discrete>( camwnd.getCamera() ) );
1041         GlobalCommands_insert( "CameraRight", ReferenceCaller<camera_t, void(), Camera_RotateRight_Discrete>( camwnd.getCamera() ) );
1042         GlobalCommands_insert( "CameraStrafeRight", ReferenceCaller<camera_t, void(), Camera_MoveRight_Discrete>( camwnd.getCamera() ) );
1043         GlobalCommands_insert( "CameraStrafeLeft", ReferenceCaller<camera_t, void(), Camera_MoveLeft_Discrete>( camwnd.getCamera() ) );
1044
1045         GlobalCommands_insert( "CameraUp", ReferenceCaller<camera_t, void(), Camera_MoveUp_Discrete>( camwnd.getCamera() ) );
1046         GlobalCommands_insert( "CameraDown", ReferenceCaller<camera_t, void(), Camera_MoveDown_Discrete>( camwnd.getCamera() ) );
1047         GlobalCommands_insert( "CameraAngleUp", ReferenceCaller<camera_t, void(), Camera_PitchUp_Discrete>( camwnd.getCamera() ) );
1048         GlobalCommands_insert( "CameraAngleDown", ReferenceCaller<camera_t, void(), Camera_PitchDown_Discrete>( camwnd.getCamera() ) );
1049 }
1050
1051 void CamWnd_Move_Enable( CamWnd& camwnd ){
1052         KeyEvent_connect( "CameraForward" );
1053         KeyEvent_connect( "CameraBack" );
1054         KeyEvent_connect( "CameraLeft" );
1055         KeyEvent_connect( "CameraRight" );
1056         KeyEvent_connect( "CameraStrafeRight" );
1057         KeyEvent_connect( "CameraStrafeLeft" );
1058         KeyEvent_connect( "CameraUp" );
1059         KeyEvent_connect( "CameraDown" );
1060         KeyEvent_connect( "CameraAngleUp" );
1061         KeyEvent_connect( "CameraAngleDown" );
1062 }
1063
1064 void CamWnd_Move_Disable( CamWnd& camwnd ){
1065         KeyEvent_disconnect( "CameraForward" );
1066         KeyEvent_disconnect( "CameraBack" );
1067         KeyEvent_disconnect( "CameraLeft" );
1068         KeyEvent_disconnect( "CameraRight" );
1069         KeyEvent_disconnect( "CameraStrafeRight" );
1070         KeyEvent_disconnect( "CameraStrafeLeft" );
1071         KeyEvent_disconnect( "CameraUp" );
1072         KeyEvent_disconnect( "CameraDown" );
1073         KeyEvent_disconnect( "CameraAngleUp" );
1074         KeyEvent_disconnect( "CameraAngleDown" );
1075 }
1076
1077 void CamWnd_Move_Discrete_Enable( CamWnd& camwnd ){
1078         command_connect_accelerator( "CameraForward" );
1079         command_connect_accelerator( "CameraBack" );
1080         command_connect_accelerator( "CameraLeft" );
1081         command_connect_accelerator( "CameraRight" );
1082         command_connect_accelerator( "CameraStrafeRight" );
1083         command_connect_accelerator( "CameraStrafeLeft" );
1084         command_connect_accelerator( "CameraUp" );
1085         command_connect_accelerator( "CameraDown" );
1086         command_connect_accelerator( "CameraAngleUp" );
1087         command_connect_accelerator( "CameraAngleDown" );
1088 }
1089
1090 void CamWnd_Move_Discrete_Disable( CamWnd& camwnd ){
1091         command_disconnect_accelerator( "CameraForward" );
1092         command_disconnect_accelerator( "CameraBack" );
1093         command_disconnect_accelerator( "CameraLeft" );
1094         command_disconnect_accelerator( "CameraRight" );
1095         command_disconnect_accelerator( "CameraStrafeRight" );
1096         command_disconnect_accelerator( "CameraStrafeLeft" );
1097         command_disconnect_accelerator( "CameraUp" );
1098         command_disconnect_accelerator( "CameraDown" );
1099         command_disconnect_accelerator( "CameraAngleUp" );
1100         command_disconnect_accelerator( "CameraAngleDown" );
1101 }
1102
1103 struct CamWnd_Move_Discrete {
1104         static void Export(const Callback<void(bool)> &returnz) {
1105                 returnz(g_camwindow_globals_private.m_bCamDiscrete);
1106         }
1107
1108         static void Import(bool value) {
1109                 if (g_camwnd) {
1110                         Import_(*g_camwnd, value);
1111                 } else {
1112                         g_camwindow_globals_private.m_bCamDiscrete = value;
1113                 }
1114         }
1115
1116         static void Import_(CamWnd &camwnd, bool value) {
1117         if ( g_camwindow_globals_private.m_bCamDiscrete ) {
1118                 CamWnd_Move_Discrete_Disable( camwnd );
1119                 } else {
1120                 CamWnd_Move_Disable( camwnd );
1121         }
1122
1123         g_camwindow_globals_private.m_bCamDiscrete = value;
1124
1125         if ( g_camwindow_globals_private.m_bCamDiscrete ) {
1126                 CamWnd_Move_Discrete_Enable( camwnd );
1127                 } else {
1128                 CamWnd_Move_Enable( camwnd );
1129         }
1130 }
1131 };
1132
1133
1134 void CamWnd_Add_Handlers_Move( CamWnd& camwnd ){
1135         camwnd.m_selection_button_press_handler = camwnd.m_gl_widget.connect( "button_press_event", G_CALLBACK( selection_button_press ), camwnd.m_window_observer );
1136         camwnd.m_selection_button_release_handler = camwnd.m_gl_widget.connect( "button_release_event", G_CALLBACK( selection_button_release ), camwnd.m_window_observer );
1137         camwnd.m_selection_motion_handler = camwnd.m_gl_widget.connect( "motion_notify_event", G_CALLBACK( DeferredMotion::gtk_motion ), &camwnd.m_deferred_motion );
1138
1139         camwnd.m_freelook_button_press_handler = camwnd.m_gl_widget.connect( "button_press_event", G_CALLBACK( enable_freelook_button_press ), &camwnd );
1140
1141         if ( g_camwindow_globals_private.m_bCamDiscrete ) {
1142                 CamWnd_Move_Discrete_Enable( camwnd );
1143         }
1144         else
1145         {
1146                 CamWnd_Move_Enable( camwnd );
1147         }
1148 }
1149
1150 void CamWnd_Remove_Handlers_Move( CamWnd& camwnd ){
1151         g_signal_handler_disconnect( G_OBJECT( camwnd.m_gl_widget ), camwnd.m_selection_button_press_handler );
1152         g_signal_handler_disconnect( G_OBJECT( camwnd.m_gl_widget ), camwnd.m_selection_button_release_handler );
1153         g_signal_handler_disconnect( G_OBJECT( camwnd.m_gl_widget ), camwnd.m_selection_motion_handler );
1154
1155         g_signal_handler_disconnect( G_OBJECT( camwnd.m_gl_widget ), camwnd.m_freelook_button_press_handler );
1156
1157         if ( g_camwindow_globals_private.m_bCamDiscrete ) {
1158                 CamWnd_Move_Discrete_Disable( camwnd );
1159         }
1160         else
1161         {
1162                 CamWnd_Move_Disable( camwnd );
1163         }
1164 }
1165
1166 void CamWnd_Add_Handlers_FreeMove( CamWnd& camwnd ){
1167         camwnd.m_selection_button_press_handler = camwnd.m_gl_widget.connect( "button_press_event", G_CALLBACK( selection_button_press_freemove ), camwnd.m_window_observer );
1168         camwnd.m_selection_button_release_handler = camwnd.m_gl_widget.connect( "button_release_event", G_CALLBACK( selection_button_release_freemove ), camwnd.m_window_observer );
1169         camwnd.m_selection_motion_handler = camwnd.m_gl_widget.connect( "motion_notify_event", G_CALLBACK( selection_motion_freemove ), camwnd.m_window_observer );
1170
1171         camwnd.m_freelook_button_press_handler = camwnd.m_gl_widget.connect( "button_press_event", G_CALLBACK( disable_freelook_button_press ), &camwnd );
1172
1173         KeyEvent_connect( "CameraFreeMoveForward" );
1174         KeyEvent_connect( "CameraFreeMoveBack" );
1175         KeyEvent_connect( "CameraFreeMoveLeft" );
1176         KeyEvent_connect( "CameraFreeMoveRight" );
1177
1178         KeyEvent_connect( "CameraFreeMoveForward2" );
1179         KeyEvent_connect( "CameraFreeMoveBack2" );
1180         KeyEvent_connect( "CameraFreeMoveLeft2" );
1181         KeyEvent_connect( "CameraFreeMoveRight2" );
1182
1183         KeyEvent_connect( "CameraFreeMoveUp" );
1184         KeyEvent_connect( "CameraFreeMoveDown" );
1185 }
1186
1187 void CamWnd_Remove_Handlers_FreeMove( CamWnd& camwnd ){
1188         KeyEvent_disconnect( "CameraFreeMoveForward" );
1189         KeyEvent_disconnect( "CameraFreeMoveBack" );
1190         KeyEvent_disconnect( "CameraFreeMoveLeft" );
1191         KeyEvent_disconnect( "CameraFreeMoveRight" );
1192
1193         KeyEvent_disconnect( "CameraFreeMoveForward2" );
1194         KeyEvent_disconnect( "CameraFreeMoveBack2" );
1195         KeyEvent_disconnect( "CameraFreeMoveLeft2" );
1196         KeyEvent_disconnect( "CameraFreeMoveRight2" );
1197
1198         KeyEvent_disconnect( "CameraFreeMoveUp" );
1199         KeyEvent_disconnect( "CameraFreeMoveDown" );
1200
1201         g_signal_handler_disconnect( G_OBJECT( camwnd.m_gl_widget ), camwnd.m_selection_button_press_handler );
1202         g_signal_handler_disconnect( G_OBJECT( camwnd.m_gl_widget ), camwnd.m_selection_button_release_handler );
1203         g_signal_handler_disconnect( G_OBJECT( camwnd.m_gl_widget ), camwnd.m_selection_motion_handler );
1204
1205         g_signal_handler_disconnect( G_OBJECT( camwnd.m_gl_widget ), camwnd.m_freelook_button_press_handler );
1206 }
1207
1208 CamWnd::CamWnd() :
1209         m_view( true ),
1210         m_Camera( &m_view, CamWndQueueDraw( *this ) ),
1211         m_cameraview( m_Camera, &m_view, ReferenceCaller<CamWnd, void(), CamWnd_Update>( *this ) ),
1212         m_gl_widget( glwidget_new( TRUE ) ),
1213         m_window_observer( NewWindowObserver() ),
1214         m_XORRectangle( m_gl_widget ),
1215         m_deferredDraw( WidgetQueueDrawCaller( m_gl_widget ) ),
1216         m_deferred_motion( selection_motion, m_window_observer ),
1217         m_selection_button_press_handler( 0 ),
1218         m_selection_button_release_handler( 0 ),
1219         m_selection_motion_handler( 0 ),
1220         m_freelook_button_press_handler( 0 ),
1221         m_drawing( false ){
1222         m_bFreeMove = false;
1223
1224         GlobalWindowObservers_add( m_window_observer );
1225         GlobalWindowObservers_connectWidget( m_gl_widget );
1226
1227         m_window_observer->setRectangleDrawCallback( ReferenceCaller<CamWnd, void(rect_t), camwnd_update_xor_rectangle>( *this ) );
1228         m_window_observer->setView( m_view );
1229
1230         g_object_ref( m_gl_widget._handle );
1231
1232         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 );
1233         gtk_widget_set_can_focus( m_gl_widget, true );
1234
1235         m_sizeHandler = m_gl_widget.connect( "size_allocate", G_CALLBACK( camera_size_allocate ), this );
1236         m_exposeHandler = m_gl_widget.on_render( G_CALLBACK( camera_expose ), this );
1237
1238         Map_addValidCallback( g_map, DeferredDrawOnMapValidChangedCaller( m_deferredDraw ) );
1239
1240         CamWnd_registerCommands( *this );
1241
1242         CamWnd_Add_Handlers_Move( *this );
1243
1244         m_gl_widget.connect( "scroll_event", G_CALLBACK( wheelmove_scroll ), this );
1245
1246         AddSceneChangeCallback( ReferenceCaller<CamWnd, void(), CamWnd_Update>( *this ) );
1247
1248         PressedButtons_connect( g_pressedButtons, m_gl_widget );
1249 }
1250
1251 CamWnd::~CamWnd(){
1252         if ( m_bFreeMove ) {
1253                 DisableFreeMove();
1254         }
1255
1256         CamWnd_Remove_Handlers_Move( *this );
1257
1258         g_signal_handler_disconnect( G_OBJECT( m_gl_widget ), m_sizeHandler );
1259         g_signal_handler_disconnect( G_OBJECT( m_gl_widget ), m_exposeHandler );
1260
1261         m_gl_widget.unref();
1262
1263         m_window_observer->release();
1264 }
1265
1266 class FloorHeightWalker : public scene::Graph::Walker
1267 {
1268 float m_current;
1269 float& m_bestUp;
1270 float& m_bestDown;
1271 public:
1272 FloorHeightWalker( float current, float& bestUp, float& bestDown ) :
1273         m_current( current ), m_bestUp( bestUp ), m_bestDown( bestDown ){
1274         bestUp = g_MaxWorldCoord;
1275         bestDown = -g_MaxWorldCoord;
1276 }
1277 bool pre( const scene::Path& path, scene::Instance& instance ) const {
1278         if ( path.top().get().visible()
1279                  && Node_isBrush( path.top() ) ) { // this node is a floor
1280                 const AABB& aabb = instance.worldAABB();
1281                 float floorHeight = aabb.origin.z() + aabb.extents.z();
1282                 if ( floorHeight > m_current && floorHeight < m_bestUp ) {
1283                         m_bestUp = floorHeight;
1284                 }
1285                 if ( floorHeight < m_current && floorHeight > m_bestDown ) {
1286                         m_bestDown = floorHeight;
1287                 }
1288         }
1289         else if( !path.top().get().visible() ){
1290                 return false;
1291         }
1292         return true;
1293 }
1294 };
1295
1296 void CamWnd::Cam_ChangeFloor( bool up ){
1297         float current = m_Camera.origin[2] - 48;
1298         float bestUp;
1299         float bestDown;
1300         GlobalSceneGraph().traverse( FloorHeightWalker( current, bestUp, bestDown ) );
1301
1302         if ( up && bestUp != g_MaxWorldCoord ) {
1303                 current = bestUp;
1304         }
1305         if ( !up && bestDown != -g_MaxWorldCoord ) {
1306                 current = bestDown;
1307         }
1308
1309         m_Camera.origin[2] = current + 48;
1310         Camera_updateModelview( getCamera() );
1311         CamWnd_Update( *this );
1312         CameraMovedNotify();
1313 }
1314
1315
1316 #if 0
1317
1318 // button_press
1319 Sys_GetCursorPos( &m_PositionDragCursorX, &m_PositionDragCursorY );
1320
1321 // motion
1322 if ( ( m_bFreeMove && ( buttons == ( RAD_CONTROL | RAD_SHIFT ) ) )
1323          || ( !m_bFreeMove && ( buttons == ( RAD_RBUTTON | RAD_CONTROL ) ) ) ) {
1324         Cam_PositionDrag();
1325         CamWnd_Update( camwnd );
1326         CameraMovedNotify();
1327         return;
1328 }
1329
1330 void CamWnd::Cam_PositionDrag(){
1331         int x, y;
1332
1333         Sys_GetCursorPos( m_gl_widget, &x, &y );
1334         if ( x != m_PositionDragCursorX || y != m_PositionDragCursorY ) {
1335                 x -= m_PositionDragCursorX;
1336                 vector3_add( m_Camera.origin, vector3_scaled( m_Camera.vright, x ) );
1337                 y -= m_PositionDragCursorY;
1338                 m_Camera.origin[2] -= y;
1339                 Camera_updateModelview();
1340                 CamWnd_Update( camwnd );
1341                 CameraMovedNotify();
1342
1343                 Sys_SetCursorPos( m_gl_widget, m_PositionDragCursorX, m_PositionDragCursorY );
1344         }
1345 }
1346 #endif
1347
1348
1349 // NOTE TTimo if there's an OS-level focus out of the application
1350 //   then we can release the camera cursor grab
1351 static gboolean camwindow_freemove_focusout( ui::Widget widget, GdkEventFocus* event, gpointer data ){
1352         reinterpret_cast<CamWnd*>( data )->DisableFreeMove();
1353         return FALSE;
1354 }
1355
1356 void CamWnd::EnableFreeMove(){
1357         //globalOutputStream() << "EnableFreeMove\n";
1358
1359         ASSERT_MESSAGE( !m_bFreeMove, "EnableFreeMove: free-move was already enabled" );
1360         m_bFreeMove = true;
1361         Camera_clearMovementFlags( getCamera(), MOVE_ALL );
1362
1363         CamWnd_Remove_Handlers_Move( *this );
1364         CamWnd_Add_Handlers_FreeMove( *this );
1365
1366         gtk_window_set_focus( m_parent, m_gl_widget );
1367         m_freemove_handle_focusout = m_gl_widget.connect( "focus_out_event", G_CALLBACK( camwindow_freemove_focusout ), this );
1368         /* We chose to replace m_parent by m_gl_widget but NetRadiantCustom does:
1369         m_freezePointer.freeze_pointer( m_parent, m_gl_widget, Camera_motionDelta, &m_Camera ); */
1370         m_freezePointer.freeze_pointer( m_gl_widget, Camera_motionDelta, &m_Camera );
1371
1372         CamWnd_Update( *this );
1373 }
1374
1375 void CamWnd::DisableFreeMove(){
1376         //globalOutputStream() << "DisableFreeMove\n";
1377
1378         ASSERT_MESSAGE( m_bFreeMove, "DisableFreeMove: free-move was not enabled" );
1379         m_bFreeMove = false;
1380         Camera_clearMovementFlags( getCamera(), MOVE_ALL );
1381
1382         CamWnd_Remove_Handlers_FreeMove( *this );
1383         CamWnd_Add_Handlers_Move( *this );
1384
1385         m_freezePointer.unfreeze_pointer( m_gl_widget, true );
1386
1387         g_signal_handler_disconnect( G_OBJECT( m_gl_widget ), m_freemove_handle_focusout );
1388
1389         CamWnd_Update( *this );
1390 }
1391
1392
1393 #include "renderer.h"
1394
1395 class CamRenderer : public Renderer
1396 {
1397 struct state_type
1398 {
1399         state_type() : m_highlight( 0 ), m_state( 0 ), m_lights( 0 ){
1400         }
1401         unsigned int m_highlight;
1402         Shader* m_state;
1403         const LightList* m_lights;
1404 };
1405
1406 std::vector<state_type> m_state_stack;
1407 RenderStateFlags m_globalstate;
1408 Shader* m_state_select0;
1409 Shader* m_state_select1;
1410 const Vector3& m_viewer;
1411
1412 public:
1413 CamRenderer( RenderStateFlags globalstate, Shader* select0, Shader* select1, const Vector3& viewer ) :
1414         m_globalstate( globalstate ),
1415         m_state_select0( select0 ),
1416         m_state_select1( select1 ),
1417         m_viewer( viewer ){
1418         ASSERT_NOTNULL( select0 );
1419         ASSERT_NOTNULL( select1 );
1420         m_state_stack.push_back( state_type() );
1421 }
1422
1423 void SetState( Shader* state, EStyle style ){
1424         ASSERT_NOTNULL( state );
1425         if ( style == eFullMaterials ) {
1426                 m_state_stack.back().m_state = state;
1427         }
1428 }
1429 EStyle getStyle() const {
1430         return eFullMaterials;
1431 }
1432 void PushState(){
1433         m_state_stack.push_back( m_state_stack.back() );
1434 }
1435 void PopState(){
1436         ASSERT_MESSAGE( !m_state_stack.empty(), "popping empty stack" );
1437         m_state_stack.pop_back();
1438 }
1439 void Highlight( EHighlightMode mode, bool bEnable = true ){
1440         if ( bEnable ) {
1441                 m_state_stack.back().m_highlight |= mode;
1442         } else {
1443                 m_state_stack.back().m_highlight &= ~mode;
1444         }
1445 }
1446 void setLights( const LightList& lights ){
1447         m_state_stack.back().m_lights = &lights;
1448 }
1449 void addRenderable( const OpenGLRenderable& renderable, const Matrix4& world ){
1450         if ( m_state_stack.back().m_highlight & ePrimitive ) {
1451                 m_state_select0->addRenderable( renderable, world, m_state_stack.back().m_lights );
1452         }
1453         if ( m_state_stack.back().m_highlight & eFace ) {
1454                 m_state_select1->addRenderable( renderable, world, m_state_stack.back().m_lights );
1455         }
1456
1457         m_state_stack.back().m_state->addRenderable( renderable, world, m_state_stack.back().m_lights );
1458 }
1459
1460 void render( const Matrix4& modelview, const Matrix4& projection ){
1461         GlobalShaderCache().render( m_globalstate, modelview, projection, m_viewer );
1462 }
1463 };
1464
1465 /*
1466    ==============
1467    Cam_Draw
1468    ==============
1469  */
1470 /*
1471 void ShowStatsToggle(){
1472         g_camwindow_globals_private.m_showStats ^= 1;
1473 }
1474
1475 void ShowStatsExport( const Callback<void(bool)> &importer ){
1476         importer( g_camwindow_globals_private.m_showStats );
1477 }
1478
1479 FreeCaller<void(const Callback<void(bool)>&), ShowStatsExport> g_show_stats_caller;
1480 Callback<void(const Callback<void(bool)> &)> g_show_stats_callback( g_show_stats_caller );
1481 ToggleItem g_show_stats( g_show_stats_callback );
1482 */
1483
1484 void ShowStatsToggle(){
1485         g_camwindow_globals_private.m_showStats ^= 1;
1486 //      g_show_stats.update();
1487         UpdateAllWindows();
1488 }
1489 typedef FreeCaller<void(), ShowStatsToggle> ShowStatsToggleCaller;
1490 void ShowStatsExport( const Callback<void(bool)> & importer ){
1491         importer( g_camwindow_globals_private.m_showStats );
1492 }
1493 typedef FreeCaller<void(const Callback<void(bool)> &), ShowStatsExport> ShowStatsExportCaller;
1494
1495 ShowStatsExportCaller g_show_stats_caller;
1496 Callback<void(const Callback<void(bool)> &)> g_show_stats_callback( g_show_stats_caller );
1497 ToggleItem g_show_stats( g_show_stats_callback );
1498
1499 void CamWnd::Cam_Draw(){
1500         glViewport( 0, 0, m_Camera.width, m_Camera.height );
1501 #if 0
1502         GLint viewprt[4];
1503         glGetIntegerv( GL_VIEWPORT, viewprt );
1504 #endif
1505
1506         // enable depth buffer writes
1507         glDepthMask( GL_TRUE );
1508         glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
1509
1510         Vector3 clearColour( 0, 0, 0 );
1511         if ( m_Camera.draw_mode != cd_lighting ) {
1512                 clearColour = g_camwindow_globals.color_cameraback;
1513         }
1514
1515         glClearColor( clearColour[0], clearColour[1], clearColour[2], 0 );
1516         glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
1517
1518         extern void Renderer_ResetStats();
1519         Renderer_ResetStats();
1520         extern void Cull_ResetStats();
1521         Cull_ResetStats();
1522
1523         glMatrixMode( GL_PROJECTION );
1524         glLoadMatrixf( reinterpret_cast<const float*>( &m_Camera.projection ) );
1525
1526         glMatrixMode( GL_MODELVIEW );
1527         glLoadMatrixf( reinterpret_cast<const float*>( &m_Camera.modelview ) );
1528
1529
1530         // one directional light source directly behind the viewer
1531         {
1532                 GLfloat inverse_cam_dir[4], ambient[4], diffuse[4]; //, material[4];
1533
1534                 ambient[0] = ambient[1] = ambient[2] = 0.4f;
1535                 ambient[3] = 1.0f;
1536                 diffuse[0] = diffuse[1] = diffuse[2] = 0.4f;
1537                 diffuse[3] = 1.0f;
1538                 //material[0] = material[1] = material[2] = 0.8f;
1539                 //material[3] = 1.0f;
1540
1541                 inverse_cam_dir[0] = m_Camera.vpn[0];
1542                 inverse_cam_dir[1] = m_Camera.vpn[1];
1543                 inverse_cam_dir[2] = m_Camera.vpn[2];
1544                 inverse_cam_dir[3] = 0;
1545
1546                 glLightfv( GL_LIGHT0, GL_POSITION, inverse_cam_dir );
1547
1548                 glLightfv( GL_LIGHT0, GL_AMBIENT, ambient );
1549                 glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuse );
1550
1551                 glEnable( GL_LIGHT0 );
1552         }
1553
1554
1555         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;
1556         switch ( m_Camera.draw_mode )
1557         {
1558         case cd_wire:
1559                 break;
1560         case cd_solid:
1561                 globalstate |= RENDER_FILL
1562                                            | RENDER_LIGHTING
1563                                            | RENDER_SMOOTH
1564                                            | RENDER_SCALED;
1565                 break;
1566         case cd_texture:
1567                 globalstate |= RENDER_FILL
1568                                            | RENDER_LIGHTING
1569                                            | RENDER_TEXTURE
1570                                            | RENDER_SMOOTH
1571                                            | RENDER_SCALED;
1572                 break;
1573         case cd_lighting:
1574                 globalstate |= RENDER_FILL
1575                                            | RENDER_LIGHTING
1576                                            | RENDER_TEXTURE
1577                                            | RENDER_SMOOTH
1578                                            | RENDER_SCALED
1579                                            | RENDER_BUMP
1580                                            | RENDER_PROGRAM
1581                                            | RENDER_SCREEN;
1582                 break;
1583         default:
1584                 globalstate = 0;
1585                 break;
1586         }
1587
1588 //      if ( !g_xywindow_globals.m_bNoStipple ) {
1589                 globalstate |= RENDER_LINESTIPPLE | RENDER_POLYGONSTIPPLE;
1590 //      }
1591
1592         {
1593                 CamRenderer renderer( globalstate, m_state_select2, m_state_select1, m_view.getViewer() );
1594
1595                 Scene_Render( renderer, m_view );
1596
1597                 renderer.render( m_Camera.modelview, m_Camera.projection );
1598         }
1599
1600         // prepare for 2d stuff
1601         glColor4f( 1, 1, 1, 1 );
1602         glDisable( GL_BLEND );
1603         glMatrixMode( GL_PROJECTION );
1604         glLoadIdentity();
1605         glOrtho( 0, (float)m_Camera.width, 0, (float)m_Camera.height, -100, 100 );
1606         glScalef( 1, -1, 1 );
1607         glTranslatef( 0, -(float)m_Camera.height, 0 );
1608         glMatrixMode( GL_MODELVIEW );
1609         glLoadIdentity();
1610
1611         if ( GlobalOpenGL().GL_1_3() ) {
1612                 glClientActiveTexture( GL_TEXTURE0 );
1613                 glActiveTexture( GL_TEXTURE0 );
1614         }
1615
1616         glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1617         glDisableClientState( GL_NORMAL_ARRAY );
1618         glDisableClientState( GL_COLOR_ARRAY );
1619
1620         glDisable( GL_TEXTURE_2D );
1621         glDisable( GL_LIGHTING );
1622         glDisable( GL_COLOR_MATERIAL );
1623         glDisable( GL_DEPTH_TEST );
1624         glColor3f( 1.f, 1.f, 1.f );
1625         glLineWidth( 1 );
1626
1627         // draw the crosshair
1628         if ( m_bFreeMove ) {
1629                 glBegin( GL_LINES );
1630                 glVertex2f( (float)m_Camera.width / 2.f, (float)m_Camera.height / 2.f + 6 );
1631                 glVertex2f( (float)m_Camera.width / 2.f, (float)m_Camera.height / 2.f + 2 );
1632                 glVertex2f( (float)m_Camera.width / 2.f, (float)m_Camera.height / 2.f - 6 );
1633                 glVertex2f( (float)m_Camera.width / 2.f, (float)m_Camera.height / 2.f - 2 );
1634                 glVertex2f( (float)m_Camera.width / 2.f + 6, (float)m_Camera.height / 2.f );
1635                 glVertex2f( (float)m_Camera.width / 2.f + 2, (float)m_Camera.height / 2.f );
1636                 glVertex2f( (float)m_Camera.width / 2.f - 6, (float)m_Camera.height / 2.f );
1637                 glVertex2f( (float)m_Camera.width / 2.f - 2, (float)m_Camera.height / 2.f );
1638                 glEnd();
1639         }
1640
1641         if ( g_camwindow_globals_private.m_showStats ) {
1642                 glRasterPos3f( 1.0f, static_cast<float>( m_Camera.height ) - GlobalOpenGL().m_font->getPixelDescent(), 0.0f );
1643                 extern const char* Renderer_GetStats();
1644                 GlobalOpenGL().drawString( Renderer_GetStats() );
1645
1646                 glRasterPos3f( 1.0f, static_cast<float>( m_Camera.height ) - GlobalOpenGL().m_font->getPixelDescent() - GlobalOpenGL().m_font->getPixelHeight(), 0.0f );
1647                 extern const char* Cull_GetStats();
1648                 GlobalOpenGL().drawString( Cull_GetStats() );
1649         }
1650
1651         // bind back to the default texture so that we don't have problems
1652         // elsewhere using/modifying texture maps between contexts
1653         glBindTexture( GL_TEXTURE_2D, 0 );
1654 }
1655
1656 void CamWnd::draw(){
1657         m_drawing = true;
1658
1659         //globalOutputStream() << "draw...\n";
1660         if ( glwidget_make_current( m_gl_widget ) != FALSE ) {
1661                 if ( Map_Valid( g_map ) && ScreenUpdates_Enabled() ) {
1662                         GlobalOpenGL_debugAssertNoErrors();
1663                         Cam_Draw();
1664                         GlobalOpenGL_debugAssertNoErrors();
1665                         //qglFinish();
1666
1667                         m_XORRectangle.set( rectangle_t() );
1668                 }
1669
1670                 glwidget_swap_buffers( m_gl_widget );
1671         }
1672
1673         m_drawing = false;
1674 }
1675
1676 void CamWnd::BenchMark(){
1677         double dStart = Sys_DoubleTime();
1678         for ( int i = 0 ; i < 100 ; i++ )
1679         {
1680                 Vector3 angles;
1681                 angles[CAMERA_ROLL] = 0;
1682                 angles[CAMERA_PITCH] = 0;
1683                 angles[CAMERA_YAW] = i * 360.0f / 100.0f;
1684                 Camera_setAngles( *this, angles );
1685         }
1686         double dEnd = Sys_DoubleTime();
1687         globalOutputStream() << FloatFormat( dEnd - dStart, 5, 2 ) << " seconds\n";
1688 }
1689
1690
1691 void GlobalCamera_ResetAngles(){
1692         CamWnd& camwnd = *g_camwnd;
1693         Vector3 angles;
1694         angles[CAMERA_ROLL] = angles[CAMERA_PITCH] = 0;
1695         angles[CAMERA_YAW] = 22.5f * floorf( ( Camera_getAngles( camwnd )[CAMERA_YAW] + 11 ) / 22.5f );
1696         Camera_setAngles( camwnd, angles );
1697 }
1698
1699 #include "select.h"
1700
1701 void GlobalCamera_FocusOnSelected(){
1702         CamWnd& camwnd = *g_camwnd;
1703
1704         Vector3 angles( Camera_getAngles( camwnd ) );
1705         Vector3 radangles( degrees_to_radians( angles[0] ), degrees_to_radians( angles[1] ), degrees_to_radians( angles[2] ) );
1706         Vector3 viewvector;
1707         viewvector[0] = cos( radangles[1] ) * cos( radangles[0] );
1708         viewvector[1] = sin( radangles[1] ) * cos( radangles[0] );
1709         viewvector[2] = sin( radangles[0] );
1710
1711         Vector3 camorigin( Camera_getOrigin( camwnd ) );
1712
1713         AABB aabb( aabb_for_minmax( Select_getWorkZone().d_work_min, Select_getWorkZone().d_work_max ) );
1714
1715         View& view = *( camwnd.getCamera().m_view );
1716
1717         Plane3 frustumPlanes[4];
1718         frustumPlanes[0] = plane3_translated( view.getFrustum().left, camorigin - aabb.origin );
1719         frustumPlanes[1] = plane3_translated( view.getFrustum().right, camorigin - aabb.origin );
1720         frustumPlanes[2] = plane3_translated( view.getFrustum().top, camorigin - aabb.origin );
1721         frustumPlanes[3] = plane3_translated( view.getFrustum().bottom, camorigin - aabb.origin );
1722
1723         float offset = 64.0f;
1724
1725         Vector3 corners[8];
1726         aabb_corners( aabb, corners );
1727
1728         for ( size_t i = 0; i < 4; ++i ){
1729                 for ( size_t j = 0; j < 8; ++j ){
1730                         Ray ray( aabb.origin, -viewvector );
1731                         //Plane3 newplane( frustumPlanes[i].normal(), vector3_dot( frustumPlanes[i].normal(), corners[j] - frustumPlanes[i].normal() * 16.0f ) );
1732                         Plane3 newplane( frustumPlanes[i].normal(), vector3_dot( frustumPlanes[i].normal(), corners[j] ) );
1733                         float d = vector3_dot( ray.direction, newplane.normal() );
1734                         if( d != 0 ){
1735                                 float s = vector3_dot( newplane.normal() * newplane.dist() - ray.origin, newplane.normal() ) / d;
1736                                 offset = std::max( offset, s );
1737                         }
1738                 }
1739         }
1740         Camera_setOrigin( camwnd, aabb.origin - viewvector * offset );
1741 }
1742
1743 void Camera_ChangeFloorUp(){
1744         CamWnd& camwnd = *g_camwnd;
1745         camwnd.Cam_ChangeFloor( true );
1746 }
1747
1748 void Camera_ChangeFloorDown(){
1749         CamWnd& camwnd = *g_camwnd;
1750         camwnd.Cam_ChangeFloor( false );
1751 }
1752
1753 void Camera_CubeIn(){
1754         CamWnd& camwnd = *g_camwnd;
1755         g_camwindow_globals.m_nCubicScale--;
1756         if ( g_camwindow_globals.m_nCubicScale < 1 ) {
1757                 g_camwindow_globals.m_nCubicScale = 1;
1758         }
1759         Camera_updateProjection( camwnd.getCamera() );
1760         CamWnd_Update( camwnd );
1761         g_pParentWnd->SetGridStatus();
1762 }
1763
1764 void Camera_CubeOut(){
1765         CamWnd& camwnd = *g_camwnd;
1766         g_camwindow_globals.m_nCubicScale++;
1767         if ( g_camwindow_globals.m_nCubicScale > 46 ) {
1768                 g_camwindow_globals.m_nCubicScale = 46;
1769         }
1770         Camera_updateProjection( camwnd.getCamera() );
1771         CamWnd_Update( camwnd );
1772         g_pParentWnd->SetGridStatus();
1773 }
1774
1775 bool Camera_GetFarClip(){
1776         return g_camwindow_globals_private.m_bCubicClipping;
1777 }
1778
1779 ConstReferenceCaller<bool, void(const Callback<void(bool)> &), PropertyImpl<bool>::Export> g_getfarclip_caller( g_camwindow_globals_private.m_bCubicClipping );
1780 ToggleItem g_getfarclip_item( g_getfarclip_caller );
1781
1782 void Camera_SetFarClip( bool value ){
1783         CamWnd& camwnd = *g_camwnd;
1784         g_camwindow_globals_private.m_bCubicClipping = value;
1785         g_getfarclip_item.update();
1786         Camera_updateProjection( camwnd.getCamera() );
1787         CamWnd_Update( camwnd );
1788 }
1789
1790 struct Camera_FarClip {
1791         static void Export(const Callback<void(bool)> &returnz) {
1792                 returnz(g_camwindow_globals_private.m_bCubicClipping);
1793         }
1794
1795         static void Import(bool value) {
1796                 Camera_SetFarClip(value);
1797         }
1798 };
1799
1800 void Camera_ToggleFarClip(){
1801         Camera_SetFarClip( !Camera_GetFarClip() );
1802 }
1803
1804
1805 void CamWnd_constructToolbar( ui::Toolbar toolbar ){
1806         toolbar_append_toggle_button( toolbar, "Cubic clip the camera view (Ctrl + \\)", "view_cubicclipping.png", "ToggleCubicClip" );
1807 }
1808
1809 void CamWnd_registerShortcuts(){
1810         toggle_add_accelerator( "ToggleCubicClip" );
1811
1812         if ( g_pGameDescription->mGameType == "doom3" ) {
1813                 command_connect_accelerator( "TogglePreview" );
1814         }
1815
1816         command_connect_accelerator( "FOVInc" );
1817         command_connect_accelerator( "FOVDec" );
1818         command_connect_accelerator( "CameraSpeedInc" );
1819         command_connect_accelerator( "CameraSpeedDec" );
1820 }
1821
1822
1823 void GlobalCamera_Benchmark(){
1824         CamWnd& camwnd = *g_camwnd;
1825         camwnd.BenchMark();
1826 }
1827
1828 void GlobalCamera_Update(){
1829         CamWnd& camwnd = *g_camwnd;
1830         CamWnd_Update( camwnd );
1831 }
1832
1833 camera_draw_mode CamWnd_GetMode(){
1834         return camera_t::draw_mode;
1835 }
1836 void CamWnd_SetMode( camera_draw_mode mode ){
1837         ShaderCache_setBumpEnabled( mode == cd_lighting );
1838         camera_t::draw_mode = mode;
1839         if ( g_camwnd != 0 ) {
1840                 CamWnd_Update( *g_camwnd );
1841         }
1842 }
1843
1844 void CamWnd_TogglePreview( void ){
1845         // gametype must be doom3 for this function to work
1846         // if the gametype is not doom3 something is wrong with the
1847         // global command list or somebody else calls this function.
1848         ASSERT_MESSAGE( g_pGameDescription->mGameType == "doom3", "CamWnd_TogglePreview called although mGameType is not doom3 compatible" );
1849
1850         // switch between textured and lighting mode
1851         CamWnd_SetMode( ( CamWnd_GetMode() == cd_lighting ) ? cd_texture : cd_lighting );
1852 }
1853
1854
1855 CameraModel* g_camera_model = 0;
1856
1857 void CamWnd_LookThroughCamera( CamWnd& camwnd ){
1858         if ( g_camera_model != 0 ) {
1859                 CamWnd_Add_Handlers_Move( camwnd );
1860                 g_camera_model->setCameraView( 0, Callback<void()>() );
1861                 g_camera_model = 0;
1862                 Camera_updateModelview( camwnd.getCamera() );
1863                 Camera_updateProjection( camwnd.getCamera() );
1864                 CamWnd_Update( camwnd );
1865         }
1866 }
1867
1868 inline CameraModel* Instance_getCameraModel( scene::Instance& instance ){
1869         return InstanceTypeCast<CameraModel>::cast( instance );
1870 }
1871
1872 void CamWnd_LookThroughSelected( CamWnd& camwnd ){
1873         if ( g_camera_model != 0 ) {
1874                 CamWnd_LookThroughCamera( camwnd );
1875         }
1876
1877         if ( GlobalSelectionSystem().countSelected() != 0 ) {
1878                 scene::Instance& instance = GlobalSelectionSystem().ultimateSelected();
1879                 CameraModel* cameraModel = Instance_getCameraModel( instance );
1880                 if ( cameraModel != 0 ) {
1881                         CamWnd_Remove_Handlers_Move( camwnd );
1882                         g_camera_model = cameraModel;
1883                         g_camera_model->setCameraView( &camwnd.getCameraView(), ReferenceCaller<CamWnd, void(), CamWnd_LookThroughCamera>( camwnd ) );
1884                 }
1885         }
1886 }
1887
1888 void GlobalCamera_LookThroughSelected(){
1889         CamWnd_LookThroughSelected( *g_camwnd );
1890 }
1891
1892 void GlobalCamera_LookThroughCamera(){
1893         CamWnd_LookThroughCamera( *g_camwnd );
1894 }
1895
1896 struct RenderMode {
1897         static void Export(const Callback<void(int)> &returnz) {
1898                 switch (CamWnd_GetMode()) {
1899                         case cd_wire:
1900                                 returnz(0);
1901                                 break;
1902                         case cd_solid:
1903                                 returnz(1);
1904                                 break;
1905                         case cd_texture:
1906                                 returnz(2);
1907                                 break;
1908                         case cd_lighting:
1909                                 returnz(3);
1910                                 break;
1911                 }
1912         }
1913
1914         static void Import(int value) {
1915                 switch (value) {
1916         case 0:
1917                 CamWnd_SetMode( cd_wire );
1918                 break;
1919         case 1:
1920                 CamWnd_SetMode( cd_solid );
1921                 break;
1922         case 2:
1923                 CamWnd_SetMode( cd_texture );
1924                 break;
1925         case 3:
1926                 CamWnd_SetMode( cd_lighting );
1927                 break;
1928         default:
1929                 CamWnd_SetMode( cd_texture );
1930         }
1931 }
1932 };
1933
1934 void Camera_constructPreferences( PreferencesPage& page ){
1935         page.appendSlider( "FOV", g_camwindow_globals_private.m_nFOV, TRUE, 0, 0, 100, MIN_FOV, MAX_FOV, 1, 10 );
1936         page.appendSlider( "Movement Speed", g_camwindow_globals_private.m_nMoveSpeed, TRUE, 0, 0, 100, MIN_CAM_SPEED, MAX_CAM_SPEED, 1, 10 );
1937         page.appendCheckBox( "", "Link strafe speed to movement speed", g_camwindow_globals_private.m_bCamLinkSpeed );
1938         page.appendSlider( "Rotation Speed", g_camwindow_globals_private.m_nAngleSpeed, TRUE, 0, 0, 3, 1, 180, 1, 10 );
1939         page.appendCheckBox( "", "Invert mouse vertical axis", g_camwindow_globals_private.m_bCamInverseMouse );
1940         page.appendCheckBox( "", "Zoom In to Mouse pointer", g_camwindow_globals.m_bZoomInToPointer );
1941         page.appendCheckBox(
1942                 "", "Discrete movement",
1943                 make_property<CamWnd_Move_Discrete>()
1944                 );
1945         page.appendCheckBox(
1946                 "", "Enable far-clip plane",
1947                 make_property<Camera_FarClip>()
1948                 );
1949
1950         if ( g_pGameDescription->mGameType == "doom3" ) {
1951                 const char* render_mode[] = { "Wireframe", "Flatshade", "Textured", "Lighting" };
1952
1953                 page.appendCombo(
1954                         "Render Mode",
1955                         STRING_ARRAY_RANGE( render_mode ),
1956                         make_property<RenderMode>()
1957                         );
1958         }
1959         else
1960         {
1961                 const char* render_mode[] = { "Wireframe", "Flatshade", "Textured" };
1962
1963                 page.appendCombo(
1964                         "Render Mode",
1965                         STRING_ARRAY_RANGE( render_mode ),
1966                         make_property<RenderMode>()
1967                         );
1968         }
1969
1970         const char* strafe_mode[] = { "None", "Up", "Forward", "Both", "Both Inverted" };
1971
1972         page.appendCombo(
1973                 "Strafe Mode",
1974                 g_camwindow_globals_private.m_nStrafeMode,
1975                 STRING_ARRAY_RANGE( strafe_mode )
1976                 );
1977 }
1978 void Camera_constructPage( PreferenceGroup& group ){
1979         PreferencesPage page( group.createPage( "Camera", "Camera View Preferences" ) );
1980         Camera_constructPreferences( page );
1981 }
1982 void Camera_registerPreferencesPage(){
1983         PreferencesDialog_addSettingsPage( makeCallbackF(Camera_constructPage) );
1984 }
1985
1986 #include "preferencesystem.h"
1987 #include "stringio.h"
1988 #include "dialog.h"
1989
1990 void FOV_increase(){
1991         CamWnd& camwnd = *g_camwnd;
1992         if ( g_camwindow_globals_private.m_nFOV <= ( MAX_FOV - FOV_STEP - 10 ) ) {
1993                 g_camwindow_globals_private.m_nFOV += FOV_STEP;
1994         }
1995         else {
1996                 g_camwindow_globals_private.m_nFOV = MAX_FOV - 10;
1997         }
1998         Camera_updateProjection( camwnd.getCamera() );
1999         CamWnd_Update( camwnd );
2000 }
2001
2002 void FOV_decrease(){
2003         CamWnd& camwnd = *g_camwnd;
2004         if ( g_camwindow_globals_private.m_nFOV >= ( MIN_FOV + FOV_STEP ) ) {
2005                 g_camwindow_globals_private.m_nFOV -= FOV_STEP;
2006         }
2007         else {
2008                 g_camwindow_globals_private.m_nFOV = MIN_FOV;
2009         }
2010         Camera_updateProjection( camwnd.getCamera() );
2011         CamWnd_Update( camwnd );
2012 }
2013
2014
2015 void CameraSpeed_increase(){
2016         if ( g_camwindow_globals_private.m_nMoveSpeed <= ( MAX_CAM_SPEED - CAM_SPEED_STEP - 10 ) ) {
2017                 g_camwindow_globals_private.m_nMoveSpeed += CAM_SPEED_STEP;
2018         }
2019         else {
2020                 g_camwindow_globals_private.m_nMoveSpeed = MAX_CAM_SPEED - 10;
2021         }
2022 }
2023
2024 void CameraSpeed_decrease(){
2025         if ( g_camwindow_globals_private.m_nMoveSpeed >= ( MIN_CAM_SPEED + CAM_SPEED_STEP ) ) {
2026                 g_camwindow_globals_private.m_nMoveSpeed -= CAM_SPEED_STEP;
2027         }
2028         else {
2029                 g_camwindow_globals_private.m_nMoveSpeed = MIN_CAM_SPEED;
2030         }
2031 }
2032
2033 /// \brief Initialisation for things that have the same lifespan as this module.
2034 void CamWnd_Construct(){
2035         GlobalCommands_insert( "CenterView", makeCallbackF(GlobalCamera_ResetAngles), Accelerator( GDK_KEY_End ) );
2036         GlobalCommands_insert( "CameraFocusOnSelected", makeCallbackF( GlobalCamera_FocusOnSelected ), Accelerator( GDK_Tab ) );
2037
2038         GlobalToggles_insert( "ToggleCubicClip", makeCallbackF(Camera_ToggleFarClip), ToggleItem::AddCallbackCaller( g_getfarclip_item ), Accelerator( '\\', (GdkModifierType)GDK_CONTROL_MASK ) );
2039         GlobalCommands_insert( "CubicClipZoomIn", makeCallbackF(Camera_CubeIn), Accelerator( '[', (GdkModifierType)GDK_CONTROL_MASK ) );
2040         GlobalCommands_insert( "CubicClipZoomOut", makeCallbackF(Camera_CubeOut), Accelerator( ']', (GdkModifierType)GDK_CONTROL_MASK ) );
2041
2042         GlobalCommands_insert( "UpFloor", makeCallbackF(Camera_ChangeFloorUp), Accelerator( GDK_KEY_Prior ) );
2043         GlobalCommands_insert( "DownFloor", makeCallbackF(Camera_ChangeFloorDown), Accelerator( GDK_KEY_Next ) );
2044
2045         GlobalToggles_insert( "ToggleCamera", ToggleShown::ToggleCaller( g_camera_shown ), ToggleItem::AddCallbackCaller( g_camera_shown.m_item ), Accelerator( 'C', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
2046 //      GlobalCommands_insert( "LookThroughSelected", makeCallbackF(GlobalCamera_LookThroughSelected) );
2047 //      GlobalCommands_insert( "LookThroughCamera", makeCallbackF(GlobalCamera_LookThroughCamera) );
2048
2049         if ( g_pGameDescription->mGameType == "doom3" ) {
2050                 GlobalCommands_insert( "TogglePreview", makeCallbackF(CamWnd_TogglePreview), Accelerator( GDK_KEY_F3 ) );
2051         }
2052
2053         GlobalCommands_insert( "FOVInc", makeCallbackF(FOV_increase), Accelerator( GDK_KEY_KP_Multiply, (GdkModifierType)GDK_SHIFT_MASK ) );
2054         GlobalCommands_insert( "FOVDec", makeCallbackF(FOV_decrease), Accelerator( GDK_KEY_KP_Divide, (GdkModifierType)GDK_SHIFT_MASK ) );
2055
2056         GlobalCommands_insert( "CameraSpeedInc", makeCallbackF(CameraSpeed_increase), Accelerator( GDK_KEY_KP_Add, (GdkModifierType)GDK_SHIFT_MASK ) );
2057         GlobalCommands_insert( "CameraSpeedDec", makeCallbackF(CameraSpeed_decrease), Accelerator( GDK_KEY_KP_Subtract, (GdkModifierType)GDK_SHIFT_MASK ) );
2058
2059         GlobalShortcuts_insert( "CameraForward", Accelerator( GDK_KEY_Up ) );
2060         GlobalShortcuts_insert( "CameraBack", Accelerator( GDK_KEY_Down ) );
2061         GlobalShortcuts_insert( "CameraLeft", Accelerator( GDK_KEY_Left ) );
2062         GlobalShortcuts_insert( "CameraRight", Accelerator( GDK_KEY_Right ) );
2063         GlobalShortcuts_insert( "CameraStrafeRight", Accelerator( 'D' ) );
2064         GlobalShortcuts_insert( "CameraStrafeLeft", Accelerator( 'A' ) );
2065
2066         GlobalShortcuts_insert( "CameraUp", accelerator_null() );
2067         GlobalShortcuts_insert( "CameraDown", accelerator_null() );
2068         GlobalShortcuts_insert( "CameraAngleUp", accelerator_null() );
2069         GlobalShortcuts_insert( "CameraAngleDown", accelerator_null() );
2070
2071         GlobalShortcuts_insert( "CameraFreeMoveForward", Accelerator( 'W' ) );
2072         GlobalShortcuts_insert( "CameraFreeMoveBack", Accelerator( 'S' ) );
2073         GlobalShortcuts_insert( "CameraFreeMoveLeft", Accelerator( 'A' ) );
2074         GlobalShortcuts_insert( "CameraFreeMoveRight", Accelerator( 'D' ) );
2075
2076         GlobalShortcuts_insert( "CameraFreeMoveForward2", Accelerator( GDK_KEY_Up ) );
2077         GlobalShortcuts_insert( "CameraFreeMoveBack2", Accelerator( GDK_KEY_Down ) );
2078         GlobalShortcuts_insert( "CameraFreeMoveLeft2", Accelerator( GDK_KEY_Left ) );
2079         GlobalShortcuts_insert( "CameraFreeMoveRight2", Accelerator( GDK_KEY_Right ) );
2080
2081         GlobalShortcuts_insert( "CameraFreeMoveUp", accelerator_null() );
2082         GlobalShortcuts_insert( "CameraFreeMoveDown", accelerator_null() );
2083
2084         GlobalToggles_insert( "ShowStats", makeCallbackF(ShowStatsToggle), ToggleItem::AddCallbackCaller( g_show_stats ) );
2085
2086         GlobalPreferenceSystem().registerPreference( "ShowStats", make_property_string( g_camwindow_globals_private.m_showStats ) );
2087         GlobalPreferenceSystem().registerPreference( "FOV", make_property_string( g_camwindow_globals_private.m_nFOV ) );
2088         GlobalPreferenceSystem().registerPreference( "MoveSpeed", make_property_string( g_camwindow_globals_private.m_nMoveSpeed ) );
2089         GlobalPreferenceSystem().registerPreference( "CamLinkSpeed", make_property_string( g_camwindow_globals_private.m_bCamLinkSpeed ) );
2090         GlobalPreferenceSystem().registerPreference( "AngleSpeed", make_property_string( g_camwindow_globals_private.m_nAngleSpeed ) );
2091         GlobalPreferenceSystem().registerPreference( "CamInverseMouse", make_property_string( g_camwindow_globals_private.m_bCamInverseMouse ) );
2092         GlobalPreferenceSystem().registerPreference( "CamDiscrete", make_property_string<CamWnd_Move_Discrete>());
2093         GlobalPreferenceSystem().registerPreference( "CubicClipping", make_property_string( g_camwindow_globals_private.m_bCubicClipping ) );
2094         GlobalPreferenceSystem().registerPreference( "CubicScale", make_property_string( g_camwindow_globals.m_nCubicScale ) );
2095         GlobalPreferenceSystem().registerPreference( "SI_Colors4", make_property_string( g_camwindow_globals.color_cameraback ) );
2096         GlobalPreferenceSystem().registerPreference( "SI_Colors12", make_property_string( g_camwindow_globals.color_selbrushes3d ) );
2097         GlobalPreferenceSystem().registerPreference( "CameraRenderMode", make_property_string<RenderMode>() );
2098         GlobalPreferenceSystem().registerPreference( "StrafeMode", make_property_string( g_camwindow_globals_private.m_nStrafeMode ) );
2099         GlobalPreferenceSystem().registerPreference( "3DZoomInToPointer", make_property_string( g_camwindow_globals.m_bZoomInToPointer ) );
2100
2101         CamWnd_constructStatic();
2102
2103         Camera_registerPreferencesPage();
2104 }
2105 void CamWnd_Destroy(){
2106         CamWnd_destroyStatic();
2107 }