2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
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.
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.
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
25 // Leonardo Zide (leo@lokigames.com)
28 #include "camwindow.h"
31 #include <gdk/gdkkeysyms.h>
33 #include "debugging/debugging.h"
35 #include "iscenegraph.h"
40 #include "renderable.h"
41 #include "preferencesystem.h"
43 #include "signal/signal.h"
44 #include "container/array.h"
48 #include "math/frustum.h"
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"
56 #include "selection.h"
57 #include "mainframe.h"
58 #include "preferences.h"
61 #include "windowobservers.h"
62 #include "renderstate.h"
66 Signal0 g_cameraMoved_callbacks;
68 void AddCameraMovedCallback( const SignalHandler& handler ){
69 g_cameraMoved_callbacks.connectLast( handler );
72 void CameraMovedNotify(){
73 g_cameraMoved_callbacks();
77 struct camwindow_globals_private_t
83 bool m_bCamInverseMouse;
85 bool m_bCubicClipping;
89 camwindow_globals_private_t() :
92 m_bCamLinkSpeed( true ),
94 m_bCamInverseMouse( false ),
95 m_bCamDiscrete( true ),
96 m_bCubicClipping( false ),
103 camwindow_globals_private_t g_camwindow_globals_private;
106 const Matrix4 g_opengl2radiant(
113 const Matrix4 g_radiant2opengl(
121 void Camera_mouseMove( camera_t& camera, int x, int y );
123 enum camera_draw_mode
140 Vector3 color; // background
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)
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
152 unsigned int movementflags; // movement flags
153 Timer m_keycontrol_timer;
154 guint m_keymove_handler;
157 DeferredMotionDelta m_mouseMove;
159 static void motionDelta( int x, int y, void* data ){
160 Camera_mouseMove( *reinterpret_cast<camera_t*>( data ), x, y );
164 Callback<void()> m_update;
166 static camera_draw_mode draw_mode;
168 camera_t( View* view, const Callback<void()>& update )
175 projection( g_matrix4_identity ),
176 modelview( g_matrix4_identity ),
178 m_keycontrol_timer(),
179 m_keymove_handler( 0 ),
180 m_mouseMove( motionDelta, this ),
186 camera_draw_mode camera_t::draw_mode = cd_texture;
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 ) );
192 return matrix4_frustum(
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;
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 );
210 camera.m_view->Construct( camera.projection, camera.modelview, camera.width, camera.height );
213 void Camera_updateVectors( camera_t& camera ){
214 for ( int i = 0 ; i < 3 ; i++ )
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];
222 void Camera_updateModelview( camera_t& camera ){
223 camera.modelview = g_matrix4_identity;
226 Vector3 radiant_eulerXYZ( 0, -camera.angles[CAMERA_PITCH], camera.angles[CAMERA_YAW] );
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 );
233 Camera_updateVectors( camera );
235 camera.m_view->Construct( camera.projection, camera.modelview, camera.width, camera.height );
239 void Camera_Move_updateAxes( camera_t& camera ){
240 double ya = degrees_to_radians( camera.angles[CAMERA_YAW] );
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];
250 void Camera_Freemove_updateAxes( camera_t& camera ){
251 camera.right = camera.vright;
252 camera.forward = vector3_negated( camera.vpn );
255 const Vector3& Camera_getOrigin( camera_t& camera ){
256 return camera.origin;
259 void Camera_setOrigin( camera_t& camera, const Vector3& origin ){
260 camera.origin = origin;
261 Camera_updateModelview( camera );
266 const Vector3& Camera_getAngles( camera_t& camera ){
267 return camera.angles;
270 void Camera_setAngles( camera_t& camera, const Vector3& angles ){
271 camera.angles = angles;
272 Camera_updateModelview( camera );
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;
283 if ( g_camwindow_globals_private.m_bCamLinkSpeed ) {
284 strafespeed = (float)g_camwindow_globals_private.m_nMoveSpeed / 100;
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 );
292 camera.origin += camera.vup * strafespeed * dy;
295 else // free rotation
297 const float dtime = 0.1f;
299 if ( g_camwindow_globals_private.m_bCamInverseMouse ) {
300 camera.angles[CAMERA_PITCH] -= dy * dtime * g_camwindow_globals_private.m_nAngleSpeed;
303 camera.angles[CAMERA_PITCH] += dy * dtime * g_camwindow_globals_private.m_nAngleSpeed;
306 camera.angles[CAMERA_YAW] += dx * dtime * g_camwindow_globals_private.m_nAngleSpeed;
308 if ( camera.angles[CAMERA_PITCH] > 90 ) {
309 camera.angles[CAMERA_PITCH] = 90;
311 else if ( camera.angles[CAMERA_PITCH] < -90 ) {
312 camera.angles[CAMERA_PITCH] = -90;
315 if ( camera.angles[CAMERA_YAW] >= 360 ) {
316 camera.angles[CAMERA_YAW] -= 360;
318 else if ( camera.angles[CAMERA_YAW] <= 0 ) {
319 camera.angles[CAMERA_YAW] += 360;
323 Camera_updateModelview( camera );
324 Camera_Freemove_updateAxes( camera );
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 );
331 xf *= 1.0f - fabsf( yf );
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;
349 Camera_updateModelview( camera );
352 void Camera_mouseMove( camera_t& camera, int x, int y ){
353 //globalOutputStream() << "mousemove... ";
354 Camera_FreeMove( camera, -x, -y );
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;
372 void Cam_KeyControl( camera_t& camera, float dtime ){
374 if ( camera.movementflags & MOVE_ROTLEFT ) {
375 camera.angles[CAMERA_YAW] += 15 * dtime * g_camwindow_globals_private.m_nAngleSpeed;
377 if ( camera.movementflags & MOVE_ROTRIGHT ) {
378 camera.angles[CAMERA_YAW] -= 15 * dtime * g_camwindow_globals_private.m_nAngleSpeed;
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;
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;
393 Camera_updateModelview( camera );
394 Camera_Freemove_updateAxes( camera );
397 if ( camera.movementflags & MOVE_FORWARD ) {
398 vector3_add( camera.origin, vector3_scaled( camera.forward, dtime * g_camwindow_globals_private.m_nMoveSpeed ) );
400 if ( camera.movementflags & MOVE_BACK ) {
401 vector3_add( camera.origin, vector3_scaled( camera.forward, -dtime * g_camwindow_globals_private.m_nMoveSpeed ) );
403 if ( camera.movementflags & MOVE_STRAFELEFT ) {
404 vector3_add( camera.origin, vector3_scaled( camera.right, -dtime * g_camwindow_globals_private.m_nMoveSpeed ) );
406 if ( camera.movementflags & MOVE_STRAFERIGHT ) {
407 vector3_add( camera.origin, vector3_scaled( camera.right, dtime * g_camwindow_globals_private.m_nMoveSpeed ) );
409 if ( camera.movementflags & MOVE_UP ) {
410 vector3_add( camera.origin, vector3_scaled( g_vector3_axis_z, dtime * g_camwindow_globals_private.m_nMoveSpeed ) );
412 if ( camera.movementflags & MOVE_DOWN ) {
413 vector3_add( camera.origin, vector3_scaled( g_vector3_axis_z, -dtime * g_camwindow_globals_private.m_nMoveSpeed ) );
416 Camera_updateModelview( camera );
419 void Camera_keyMove( camera_t& camera ){
420 camera.m_mouseMove.flush();
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
428 Cam_KeyControl( camera, time_seconds * 5.0f );
434 gboolean camera_keymove( gpointer data ){
435 Camera_keyMove( *reinterpret_cast<camera_t*>( data ) );
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 );
443 camera.movementflags |= mask;
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;
450 camera.movementflags &= ~mask;
453 void Camera_MoveForward_KeyDown( camera_t& camera ){
454 Camera_setMovementFlags( camera, MOVE_FORWARD );
456 void Camera_MoveForward_KeyUp( camera_t& camera ){
457 Camera_clearMovementFlags( camera, MOVE_FORWARD );
459 void Camera_MoveBack_KeyDown( camera_t& camera ){
460 Camera_setMovementFlags( camera, MOVE_BACK );
462 void Camera_MoveBack_KeyUp( camera_t& camera ){
463 Camera_clearMovementFlags( camera, MOVE_BACK );
466 void Camera_MoveLeft_KeyDown( camera_t& camera ){
467 Camera_setMovementFlags( camera, MOVE_STRAFELEFT );
469 void Camera_MoveLeft_KeyUp( camera_t& camera ){
470 Camera_clearMovementFlags( camera, MOVE_STRAFELEFT );
472 void Camera_MoveRight_KeyDown( camera_t& camera ){
473 Camera_setMovementFlags( camera, MOVE_STRAFERIGHT );
475 void Camera_MoveRight_KeyUp( camera_t& camera ){
476 Camera_clearMovementFlags( camera, MOVE_STRAFERIGHT );
479 void Camera_MoveUp_KeyDown( camera_t& camera ){
480 Camera_setMovementFlags( camera, MOVE_UP );
482 void Camera_MoveUp_KeyUp( camera_t& camera ){
483 Camera_clearMovementFlags( camera, MOVE_UP );
485 void Camera_MoveDown_KeyDown( camera_t& camera ){
486 Camera_setMovementFlags( camera, MOVE_DOWN );
488 void Camera_MoveDown_KeyUp( camera_t& camera ){
489 Camera_clearMovementFlags( camera, MOVE_DOWN );
492 void Camera_RotateLeft_KeyDown( camera_t& camera ){
493 Camera_setMovementFlags( camera, MOVE_ROTLEFT );
495 void Camera_RotateLeft_KeyUp( camera_t& camera ){
496 Camera_clearMovementFlags( camera, MOVE_ROTLEFT );
498 void Camera_RotateRight_KeyDown( camera_t& camera ){
499 Camera_setMovementFlags( camera, MOVE_ROTRIGHT );
501 void Camera_RotateRight_KeyUp( camera_t& camera ){
502 Camera_clearMovementFlags( camera, MOVE_ROTRIGHT );
505 void Camera_PitchUp_KeyDown( camera_t& camera ){
506 Camera_setMovementFlags( camera, MOVE_PITCHUP );
508 void Camera_PitchUp_KeyUp( camera_t& camera ){
509 Camera_clearMovementFlags( camera, MOVE_PITCHUP );
511 void Camera_PitchDown_KeyDown( camera_t& camera ){
512 Camera_setMovementFlags( camera, MOVE_PITCHDOWN );
514 void Camera_PitchDown_KeyUp( camera_t& camera ){
515 Camera_clearMovementFlags( camera, MOVE_PITCHDOWN );
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;
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;
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 ) ) );
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 ) ) );
551 void Camera_MoveUp_Discrete( camera_t& camera ){
552 Vector3 origin( Camera_getOrigin( camera ) );
553 origin[2] += SPEED_MOVE;
554 Camera_setOrigin( camera, origin );
556 void Camera_MoveDown_Discrete( camera_t& camera ){
557 Vector3 origin( Camera_getOrigin( camera ) );
558 origin[2] -= SPEED_MOVE;
559 Camera_setOrigin( camera, origin );
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 ) ) );
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 ) ) );
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 );
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 );
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;
588 Camera_setAngles( camera, angles );
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;
596 Camera_setAngles( camera, angles );
600 class RadiantCameraView : public CameraView
604 Callback<void()> m_update;
606 RadiantCameraView( camera_t& camera, View* view, const Callback<void()>& update ) : m_camera( camera ), m_view( view ), m_update( update ){
609 m_view->Construct( m_camera.projection, m_camera.modelview, m_camera.width, m_camera.height );
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 );
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 );
627 void Camera_motionDelta( int x, int y, unsigned int state, void* data ){
628 camera_t* cam = reinterpret_cast<camera_t*>( data );
630 cam->m_mouseMove.motion_delta( x, y, state );
632 cam->m_strafe_forward_invert = false;
634 switch ( g_camwindow_globals_private.m_nStrafeMode )
637 cam->m_strafe = false;
640 cam->m_strafe = ( state & GDK_CONTROL_MASK ) != 0 && ( state & GDK_SHIFT_MASK ) == 0;
641 cam->m_strafe_forward = false;
644 cam->m_strafe = ( state & GDK_CONTROL_MASK ) != 0 && ( state & GDK_SHIFT_MASK ) == 0;
645 cam->m_strafe_forward = cam->m_strafe;
648 cam->m_strafe_forward_invert = true;
650 cam->m_strafe = ( state & GDK_CONTROL_MASK ) != 0;
651 if ( cam->m_strafe ) {
652 cam->m_strafe_forward = ( state & GDK_SHIFT_MASK ) != 0;
655 cam->m_strafe_forward = false;
669 RadiantCameraView m_cameraview;
671 int m_PositionDragCursorX;
672 int m_PositionDragCursorY;
675 guint m_freemove_handle_focusout;
677 static Shader* m_state_select1;
678 static Shader* m_state_select2;
680 FreezePointer m_freezePointer;
683 ui::GLArea m_gl_widget;
684 ui::Window m_parent{ui::null};
686 SelectionSystemWindowObserver* m_window_observer;
687 XORRectangle m_XORRectangle;
689 DeferredDraw m_deferredDraw;
690 DeferredMotion m_deferred_motion;
692 guint m_selection_button_press_handler;
693 guint m_selection_button_release_handler;
694 guint m_selection_motion_handler;
696 guint m_freelook_button_press_handler;
699 guint m_exposeHandler;
706 //ASSERT_MESSAGE(!m_drawing, "CamWnd::queue_draw(): called while draw is already in progress");
710 //globalOutputStream() << "queue... ";
711 m_deferredDraw.draw();
715 static void captureStates(){
716 m_state_select1 = GlobalShaderCache().capture( "$CAM_HIGHLIGHT" );
717 m_state_select2 = GlobalShaderCache().capture( "$CAM_OVERLAY" );
719 static void releaseStates(){
720 GlobalShaderCache().release( "$CAM_HIGHLIGHT" );
721 GlobalShaderCache().release( "$CAM_OVERLAY" );
724 camera_t& getCamera(){
729 void Cam_ChangeFloor( bool up );
731 void DisableFreeMove();
732 void EnableFreeMove();
735 CameraView& getCameraView(){
743 typedef MemberCaller<CamWnd, void(), &CamWnd::queue_draw> CamWndQueueDraw;
745 Shader* CamWnd::m_state_select1 = 0;
746 Shader* CamWnd::m_state_select2 = 0;
751 void DeleteCamWnd( CamWnd* camwnd ){
755 void CamWnd_constructStatic(){
756 CamWnd::captureStates();
759 void CamWnd_destroyStatic(){
760 CamWnd::releaseStates();
763 static CamWnd* g_camwnd = 0;
765 void GlobalCamera_setCamWnd( CamWnd& camwnd ){
770 ui::GLArea CamWnd_getWidget( CamWnd& camwnd ){
771 return camwnd.m_gl_widget;
774 ui::Window CamWnd_getParent( CamWnd& camwnd ){
775 return camwnd.m_parent;
778 ToggleShown g_camera_shown( true );
780 void CamWnd_setParent( CamWnd& camwnd, ui::Window parent ){
781 camwnd.m_parent = parent;
782 g_camera_shown.connect( camwnd.m_parent );
785 void CamWnd_Update( CamWnd& camwnd ){
791 camwindow_globals_t g_camwindow_globals;
793 const Vector3& Camera_getOrigin( CamWnd& camwnd ){
794 return Camera_getOrigin( camwnd.getCamera() );
797 void Camera_setOrigin( CamWnd& camwnd, const Vector3& origin ){
798 Camera_setOrigin( camwnd.getCamera(), origin );
801 const Vector3& Camera_getAngles( CamWnd& camwnd ){
802 return Camera_getAngles( camwnd.getCamera() );
805 void Camera_setAngles( CamWnd& camwnd, const Vector3& angles ){
806 Camera_setAngles( camwnd.getCamera(), angles );
810 // =============================================================================
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();
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();
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 );
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 ) );
845 gboolean selection_button_press( ui::Widget widget, GdkEventButton* event, WindowObserver* observer ){
846 if ( event->type == GDK_BUTTON_PRESS ) {
847 gtk_widget_grab_focus( widget );
848 observer->onMouseDown( WindowVector_forDouble( event->x, event->y ), button_for_button( event->button ), modifiers_for_state( event->state ) );
853 gboolean selection_button_release( ui::Widget widget, GdkEventButton* event, WindowObserver* observer ){
854 if ( event->type == GDK_BUTTON_RELEASE ) {
855 observer->onMouseUp( WindowVector_forDouble( event->x, event->y ), button_for_button( event->button ), modifiers_for_state( event->state ) );
860 void selection_motion( gdouble x, gdouble y, guint state, void* data ){
861 //globalOutputStream() << "motion... ";
862 reinterpret_cast<WindowObserver*>( data )->onMouseMotion( WindowVector_forDouble( x, y ), modifiers_for_state( state ) );
865 inline WindowVector windowvector_for_widget_centre( ui::Widget widget ){
866 auto allocation = widget.dimensions();
867 return WindowVector( static_cast<float>( allocation.width / 2 ), static_cast<float>(allocation.height / 2 ) );
870 gboolean selection_button_press_freemove( ui::Widget widget, GdkEventButton* event, WindowObserver* observer ){
871 if ( event->type == GDK_BUTTON_PRESS ) {
872 observer->onMouseDown( windowvector_for_widget_centre( widget ), button_for_button( event->button ), modifiers_for_state( event->state ) );
877 gboolean selection_button_release_freemove( ui::Widget widget, GdkEventButton* event, WindowObserver* observer ){
878 if ( event->type == GDK_BUTTON_RELEASE ) {
879 observer->onMouseUp( windowvector_for_widget_centre( widget ), button_for_button( event->button ), modifiers_for_state( event->state ) );
884 gboolean selection_motion_freemove( ui::Widget widget, GdkEventMotion *event, WindowObserver* observer ){
885 observer->onMouseMotion( windowvector_for_widget_centre( widget ), modifiers_for_state( event->state ) );
889 gboolean wheelmove_scroll( ui::Widget widget, GdkEventScroll* event, CamWnd* camwnd ){
890 //gtk_window_set_focus( camwnd->m_parent, camwnd->m_gl_widget );
891 gtk_widget_grab_focus( camwnd->m_gl_widget );
892 if( !gtk_window_is_active( camwnd->m_parent ) )
893 gtk_window_present( camwnd->m_parent );
895 if ( event->direction == GDK_SCROLL_UP ) {
896 Camera_Freemove_updateAxes( camwnd->getCamera() );
897 if( camwnd->m_bFreeMove || !g_camwindow_globals.m_bZoomInToPointer ){
898 Camera_setOrigin( *camwnd, vector3_added( Camera_getOrigin( *camwnd ), vector3_scaled( camwnd->getCamera().forward, static_cast<float>( g_camwindow_globals_private.m_nMoveSpeed ) ) ) );
901 //Matrix4 maa = matrix4_multiplied_by_matrix4( camwnd->getCamera().projection, camwnd->getCamera().modelview );
902 Matrix4 maa = camwnd->getCamera().m_view->GetViewMatrix();
903 matrix4_affine_invert( maa );
905 float x = static_cast<float>( event->x );
906 float y = static_cast<float>( event->y );
909 normalized[0] = 2.0f * ( x ) / static_cast<float>( camwnd->getCamera().width ) - 1.0f;
910 normalized[1] = 2.0f * ( y )/ static_cast<float>( camwnd->getCamera().height ) - 1.0f;
911 normalized[1] *= -1.f;
915 //globalOutputStream() << normalized << " normalized ";
916 matrix4_transform_point( maa, normalized );
917 //globalOutputStream() << normalized << "\n";
918 Vector3 norm = vector3_normalised( normalized - Camera_getOrigin( *camwnd ) );
919 //globalOutputStream() << normalized - Camera_getOrigin( *camwnd ) << " normalized - Camera_getOrigin( *camwnd )\n";
920 //globalOutputStream() << norm << " norm\n";
921 Camera_setOrigin( *camwnd, vector3_added( Camera_getOrigin( *camwnd ), vector3_scaled( norm, static_cast<float>( g_camwindow_globals_private.m_nMoveSpeed ) ) ) );
924 else if ( event->direction == GDK_SCROLL_DOWN ) {
925 Camera_Freemove_updateAxes( camwnd->getCamera() );
926 Camera_setOrigin( *camwnd, vector3_added( Camera_getOrigin( *camwnd ), vector3_scaled( camwnd->getCamera().forward, -static_cast<float>( g_camwindow_globals_private.m_nMoveSpeed ) ) ) );
932 gboolean camera_size_allocate( ui::Widget widget, GtkAllocation* allocation, CamWnd* camwnd ){
933 camwnd->getCamera().width = allocation->width;
934 camwnd->getCamera().height = allocation->height;
935 Camera_updateProjection( camwnd->getCamera() );
936 camwnd->m_window_observer->onSizeChanged( camwnd->getCamera().width, camwnd->getCamera().height );
937 camwnd->queue_draw();
941 gboolean camera_expose( ui::Widget widget, GdkEventExpose* event, gpointer data ){
942 reinterpret_cast<CamWnd*>( data )->draw();
946 void KeyEvent_connect( const char* name ){
947 const KeyEvent& keyEvent = GlobalKeyEvents_find( name );
948 keydown_accelerators_add( keyEvent.m_accelerator, keyEvent.m_keyDown );
949 keyup_accelerators_add( keyEvent.m_accelerator, keyEvent.m_keyUp );
952 void KeyEvent_disconnect( const char* name ){
953 const KeyEvent& keyEvent = GlobalKeyEvents_find( name );
954 keydown_accelerators_remove( keyEvent.m_accelerator );
955 keyup_accelerators_remove( keyEvent.m_accelerator );
958 void CamWnd_registerCommands( CamWnd& camwnd ){
959 GlobalKeyEvents_insert( "CameraForward", accelerator_null(),
960 ReferenceCaller<camera_t, void(), Camera_MoveForward_KeyDown>( camwnd.getCamera() ),
961 ReferenceCaller<camera_t, void(), Camera_MoveForward_KeyUp>( camwnd.getCamera() )
963 GlobalKeyEvents_insert( "CameraBack", accelerator_null(),
964 ReferenceCaller<camera_t, void(), Camera_MoveBack_KeyDown>( camwnd.getCamera() ),
965 ReferenceCaller<camera_t, void(), Camera_MoveBack_KeyUp>( camwnd.getCamera() )
967 GlobalKeyEvents_insert( "CameraLeft", accelerator_null(),
968 ReferenceCaller<camera_t, void(), Camera_RotateLeft_KeyDown>( camwnd.getCamera() ),
969 ReferenceCaller<camera_t, void(), Camera_RotateLeft_KeyUp>( camwnd.getCamera() )
971 GlobalKeyEvents_insert( "CameraRight", accelerator_null(),
972 ReferenceCaller<camera_t, void(), Camera_RotateRight_KeyDown>( camwnd.getCamera() ),
973 ReferenceCaller<camera_t, void(), Camera_RotateRight_KeyUp>( camwnd.getCamera() )
975 GlobalKeyEvents_insert( "CameraStrafeRight", accelerator_null(),
976 ReferenceCaller<camera_t, void(), Camera_MoveRight_KeyDown>( camwnd.getCamera() ),
977 ReferenceCaller<camera_t, void(), Camera_MoveRight_KeyUp>( camwnd.getCamera() )
979 GlobalKeyEvents_insert( "CameraStrafeLeft", accelerator_null(),
980 ReferenceCaller<camera_t, void(), Camera_MoveLeft_KeyDown>( camwnd.getCamera() ),
981 ReferenceCaller<camera_t, void(), Camera_MoveLeft_KeyUp>( camwnd.getCamera() )
983 GlobalKeyEvents_insert( "CameraUp", accelerator_null(),
984 ReferenceCaller<camera_t, void(), Camera_MoveUp_KeyDown>( camwnd.getCamera() ),
985 ReferenceCaller<camera_t, void(), Camera_MoveUp_KeyUp>( camwnd.getCamera() )
987 GlobalKeyEvents_insert( "CameraDown", accelerator_null(),
988 ReferenceCaller<camera_t, void(), Camera_MoveDown_KeyDown>( camwnd.getCamera() ),
989 ReferenceCaller<camera_t, void(), Camera_MoveDown_KeyUp>( camwnd.getCamera() )
991 GlobalKeyEvents_insert( "CameraAngleUp", accelerator_null(),
992 ReferenceCaller<camera_t, void(), Camera_PitchUp_KeyDown>( camwnd.getCamera() ),
993 ReferenceCaller<camera_t, void(), Camera_PitchUp_KeyUp>( camwnd.getCamera() )
995 GlobalKeyEvents_insert( "CameraAngleDown", accelerator_null(),
996 ReferenceCaller<camera_t, void(), Camera_PitchDown_KeyDown>( camwnd.getCamera() ),
997 ReferenceCaller<camera_t, void(), Camera_PitchDown_KeyUp>( camwnd.getCamera() )
1001 GlobalKeyEvents_insert( "CameraFreeMoveForward", accelerator_null(),
1002 FreeMoveCameraMoveForwardKeyDownCaller( camwnd.getCamera() ),
1003 FreeMoveCameraMoveForwardKeyUpCaller( camwnd.getCamera() )
1005 GlobalKeyEvents_insert( "CameraFreeMoveBack", accelerator_null(),
1006 FreeMoveCameraMoveBackKeyDownCaller( camwnd.getCamera() ),
1007 FreeMoveCameraMoveBackKeyUpCaller( camwnd.getCamera() )
1009 GlobalKeyEvents_insert( "CameraFreeMoveLeft", accelerator_null(),
1010 FreeMoveCameraMoveLeftKeyDownCaller( camwnd.getCamera() ),
1011 FreeMoveCameraMoveLeftKeyUpCaller( camwnd.getCamera() )
1013 GlobalKeyEvents_insert( "CameraFreeMoveRight", accelerator_null(),
1014 FreeMoveCameraMoveRightKeyDownCaller( camwnd.getCamera() ),
1015 FreeMoveCameraMoveRightKeyUpCaller( camwnd.getCamera() )
1018 GlobalKeyEvents_insert( "CameraFreeMoveForward2", accelerator_null(),
1019 FreeMoveCameraMoveForwardKeyDownCaller( camwnd.getCamera() ),
1020 FreeMoveCameraMoveForwardKeyUpCaller( camwnd.getCamera() )
1022 GlobalKeyEvents_insert( "CameraFreeMoveBack2", accelerator_null(),
1023 FreeMoveCameraMoveBackKeyDownCaller( camwnd.getCamera() ),
1024 FreeMoveCameraMoveBackKeyUpCaller( camwnd.getCamera() )
1026 GlobalKeyEvents_insert( "CameraFreeMoveLeft2", accelerator_null(),
1027 FreeMoveCameraMoveLeftKeyDownCaller( camwnd.getCamera() ),
1028 FreeMoveCameraMoveLeftKeyUpCaller( camwnd.getCamera() )
1030 GlobalKeyEvents_insert( "CameraFreeMoveRight2", accelerator_null(),
1031 FreeMoveCameraMoveRightKeyDownCaller( camwnd.getCamera() ),
1032 FreeMoveCameraMoveRightKeyUpCaller( camwnd.getCamera() )
1035 GlobalKeyEvents_insert( "CameraFreeMoveUp", accelerator_null(),
1036 FreeMoveCameraMoveUpKeyDownCaller( camwnd.getCamera() ),
1037 FreeMoveCameraMoveUpKeyUpCaller( camwnd.getCamera() )
1039 GlobalKeyEvents_insert( "CameraFreeMoveDown", accelerator_null(),
1040 FreeMoveCameraMoveDownKeyDownCaller( camwnd.getCamera() ),
1041 FreeMoveCameraMoveDownKeyUpCaller( camwnd.getCamera() )
1044 GlobalCommands_insert( "CameraForward", ReferenceCaller<camera_t, void(), Camera_MoveForward_Discrete>( camwnd.getCamera() ) );
1045 GlobalCommands_insert( "CameraBack", ReferenceCaller<camera_t, void(), Camera_MoveBack_Discrete>( camwnd.getCamera() ) );
1046 GlobalCommands_insert( "CameraLeft", ReferenceCaller<camera_t, void(), Camera_RotateLeft_Discrete>( camwnd.getCamera() ) );
1047 GlobalCommands_insert( "CameraRight", ReferenceCaller<camera_t, void(), Camera_RotateRight_Discrete>( camwnd.getCamera() ) );
1048 GlobalCommands_insert( "CameraStrafeRight", ReferenceCaller<camera_t, void(), Camera_MoveRight_Discrete>( camwnd.getCamera() ) );
1049 GlobalCommands_insert( "CameraStrafeLeft", ReferenceCaller<camera_t, void(), Camera_MoveLeft_Discrete>( camwnd.getCamera() ) );
1051 GlobalCommands_insert( "CameraUp", ReferenceCaller<camera_t, void(), Camera_MoveUp_Discrete>( camwnd.getCamera() ) );
1052 GlobalCommands_insert( "CameraDown", ReferenceCaller<camera_t, void(), Camera_MoveDown_Discrete>( camwnd.getCamera() ) );
1053 GlobalCommands_insert( "CameraAngleUp", ReferenceCaller<camera_t, void(), Camera_PitchUp_Discrete>( camwnd.getCamera() ) );
1054 GlobalCommands_insert( "CameraAngleDown", ReferenceCaller<camera_t, void(), Camera_PitchDown_Discrete>( camwnd.getCamera() ) );
1057 void CamWnd_Move_Enable( CamWnd& camwnd ){
1058 KeyEvent_connect( "CameraForward" );
1059 KeyEvent_connect( "CameraBack" );
1060 KeyEvent_connect( "CameraLeft" );
1061 KeyEvent_connect( "CameraRight" );
1062 KeyEvent_connect( "CameraStrafeRight" );
1063 KeyEvent_connect( "CameraStrafeLeft" );
1064 KeyEvent_connect( "CameraUp" );
1065 KeyEvent_connect( "CameraDown" );
1066 KeyEvent_connect( "CameraAngleUp" );
1067 KeyEvent_connect( "CameraAngleDown" );
1070 void CamWnd_Move_Disable( CamWnd& camwnd ){
1071 KeyEvent_disconnect( "CameraForward" );
1072 KeyEvent_disconnect( "CameraBack" );
1073 KeyEvent_disconnect( "CameraLeft" );
1074 KeyEvent_disconnect( "CameraRight" );
1075 KeyEvent_disconnect( "CameraStrafeRight" );
1076 KeyEvent_disconnect( "CameraStrafeLeft" );
1077 KeyEvent_disconnect( "CameraUp" );
1078 KeyEvent_disconnect( "CameraDown" );
1079 KeyEvent_disconnect( "CameraAngleUp" );
1080 KeyEvent_disconnect( "CameraAngleDown" );
1083 void CamWnd_Move_Discrete_Enable( CamWnd& camwnd ){
1084 command_connect_accelerator( "CameraForward" );
1085 command_connect_accelerator( "CameraBack" );
1086 command_connect_accelerator( "CameraLeft" );
1087 command_connect_accelerator( "CameraRight" );
1088 command_connect_accelerator( "CameraStrafeRight" );
1089 command_connect_accelerator( "CameraStrafeLeft" );
1090 command_connect_accelerator( "CameraUp" );
1091 command_connect_accelerator( "CameraDown" );
1092 command_connect_accelerator( "CameraAngleUp" );
1093 command_connect_accelerator( "CameraAngleDown" );
1096 void CamWnd_Move_Discrete_Disable( CamWnd& camwnd ){
1097 command_disconnect_accelerator( "CameraForward" );
1098 command_disconnect_accelerator( "CameraBack" );
1099 command_disconnect_accelerator( "CameraLeft" );
1100 command_disconnect_accelerator( "CameraRight" );
1101 command_disconnect_accelerator( "CameraStrafeRight" );
1102 command_disconnect_accelerator( "CameraStrafeLeft" );
1103 command_disconnect_accelerator( "CameraUp" );
1104 command_disconnect_accelerator( "CameraDown" );
1105 command_disconnect_accelerator( "CameraAngleUp" );
1106 command_disconnect_accelerator( "CameraAngleDown" );
1109 struct CamWnd_Move_Discrete {
1110 static void Export(const Callback<void(bool)> &returnz) {
1111 returnz(g_camwindow_globals_private.m_bCamDiscrete);
1114 static void Import(bool value) {
1116 Import_(*g_camwnd, value);
1118 g_camwindow_globals_private.m_bCamDiscrete = value;
1122 static void Import_(CamWnd &camwnd, bool value) {
1123 if ( g_camwindow_globals_private.m_bCamDiscrete ) {
1124 CamWnd_Move_Discrete_Disable( camwnd );
1126 CamWnd_Move_Disable( camwnd );
1129 g_camwindow_globals_private.m_bCamDiscrete = value;
1131 if ( g_camwindow_globals_private.m_bCamDiscrete ) {
1132 CamWnd_Move_Discrete_Enable( camwnd );
1134 CamWnd_Move_Enable( camwnd );
1140 void CamWnd_Add_Handlers_Move( CamWnd& camwnd ){
1141 camwnd.m_selection_button_press_handler = camwnd.m_gl_widget.connect( "button_press_event", G_CALLBACK( selection_button_press ), camwnd.m_window_observer );
1142 camwnd.m_selection_button_release_handler = camwnd.m_gl_widget.connect( "button_release_event", G_CALLBACK( selection_button_release ), camwnd.m_window_observer );
1143 camwnd.m_selection_motion_handler = camwnd.m_gl_widget.connect( "motion_notify_event", G_CALLBACK( DeferredMotion::gtk_motion ), &camwnd.m_deferred_motion );
1145 camwnd.m_freelook_button_press_handler = camwnd.m_gl_widget.connect( "button_press_event", G_CALLBACK( enable_freelook_button_press ), &camwnd );
1147 if ( g_camwindow_globals_private.m_bCamDiscrete ) {
1148 CamWnd_Move_Discrete_Enable( camwnd );
1152 CamWnd_Move_Enable( camwnd );
1156 void CamWnd_Remove_Handlers_Move( CamWnd& camwnd ){
1157 g_signal_handler_disconnect( G_OBJECT( camwnd.m_gl_widget ), camwnd.m_selection_button_press_handler );
1158 g_signal_handler_disconnect( G_OBJECT( camwnd.m_gl_widget ), camwnd.m_selection_button_release_handler );
1159 g_signal_handler_disconnect( G_OBJECT( camwnd.m_gl_widget ), camwnd.m_selection_motion_handler );
1161 g_signal_handler_disconnect( G_OBJECT( camwnd.m_gl_widget ), camwnd.m_freelook_button_press_handler );
1163 if ( g_camwindow_globals_private.m_bCamDiscrete ) {
1164 CamWnd_Move_Discrete_Disable( camwnd );
1168 CamWnd_Move_Disable( camwnd );
1172 void CamWnd_Add_Handlers_FreeMove( CamWnd& camwnd ){
1173 camwnd.m_selection_button_press_handler = camwnd.m_gl_widget.connect( "button_press_event", G_CALLBACK( selection_button_press_freemove ), camwnd.m_window_observer );
1174 camwnd.m_selection_button_release_handler = camwnd.m_gl_widget.connect( "button_release_event", G_CALLBACK( selection_button_release_freemove ), camwnd.m_window_observer );
1175 camwnd.m_selection_motion_handler = camwnd.m_gl_widget.connect( "motion_notify_event", G_CALLBACK( selection_motion_freemove ), camwnd.m_window_observer );
1177 camwnd.m_freelook_button_press_handler = camwnd.m_gl_widget.connect( "button_press_event", G_CALLBACK( disable_freelook_button_press ), &camwnd );
1179 KeyEvent_connect( "CameraFreeMoveForward" );
1180 KeyEvent_connect( "CameraFreeMoveBack" );
1181 KeyEvent_connect( "CameraFreeMoveLeft" );
1182 KeyEvent_connect( "CameraFreeMoveRight" );
1184 KeyEvent_connect( "CameraFreeMoveForward2" );
1185 KeyEvent_connect( "CameraFreeMoveBack2" );
1186 KeyEvent_connect( "CameraFreeMoveLeft2" );
1187 KeyEvent_connect( "CameraFreeMoveRight2" );
1189 KeyEvent_connect( "CameraFreeMoveUp" );
1190 KeyEvent_connect( "CameraFreeMoveDown" );
1193 void CamWnd_Remove_Handlers_FreeMove( CamWnd& camwnd ){
1194 KeyEvent_disconnect( "CameraFreeMoveForward" );
1195 KeyEvent_disconnect( "CameraFreeMoveBack" );
1196 KeyEvent_disconnect( "CameraFreeMoveLeft" );
1197 KeyEvent_disconnect( "CameraFreeMoveRight" );
1199 KeyEvent_disconnect( "CameraFreeMoveForward2" );
1200 KeyEvent_disconnect( "CameraFreeMoveBack2" );
1201 KeyEvent_disconnect( "CameraFreeMoveLeft2" );
1202 KeyEvent_disconnect( "CameraFreeMoveRight2" );
1204 KeyEvent_disconnect( "CameraFreeMoveUp" );
1205 KeyEvent_disconnect( "CameraFreeMoveDown" );
1207 g_signal_handler_disconnect( G_OBJECT( camwnd.m_gl_widget ), camwnd.m_selection_button_press_handler );
1208 g_signal_handler_disconnect( G_OBJECT( camwnd.m_gl_widget ), camwnd.m_selection_button_release_handler );
1209 g_signal_handler_disconnect( G_OBJECT( camwnd.m_gl_widget ), camwnd.m_selection_motion_handler );
1211 g_signal_handler_disconnect( G_OBJECT( camwnd.m_gl_widget ), camwnd.m_freelook_button_press_handler );
1216 m_Camera( &m_view, CamWndQueueDraw( *this ) ),
1217 m_cameraview( m_Camera, &m_view, ReferenceCaller<CamWnd, void(), CamWnd_Update>( *this ) ),
1218 m_gl_widget( glwidget_new( TRUE ) ),
1219 m_window_observer( NewWindowObserver() ),
1220 m_XORRectangle( m_gl_widget ),
1221 m_deferredDraw( WidgetQueueDrawCaller( m_gl_widget ) ),
1222 m_deferred_motion( selection_motion, m_window_observer ),
1223 m_selection_button_press_handler( 0 ),
1224 m_selection_button_release_handler( 0 ),
1225 m_selection_motion_handler( 0 ),
1226 m_freelook_button_press_handler( 0 ),
1228 m_bFreeMove = false;
1230 GlobalWindowObservers_add( m_window_observer );
1231 GlobalWindowObservers_connectWidget( m_gl_widget );
1233 m_window_observer->setRectangleDrawCallback( ReferenceCaller<CamWnd, void(rect_t), camwnd_update_xor_rectangle>( *this ) );
1234 m_window_observer->setView( m_view );
1236 g_object_ref( m_gl_widget._handle );
1238 gtk_widget_set_events( m_gl_widget, GDK_DESTROY | GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_SCROLL_MASK );
1239 gtk_widget_set_can_focus( m_gl_widget, true );
1241 m_sizeHandler = m_gl_widget.connect( "size_allocate", G_CALLBACK( camera_size_allocate ), this );
1242 m_exposeHandler = m_gl_widget.on_render( G_CALLBACK( camera_expose ), this );
1244 Map_addValidCallback( g_map, DeferredDrawOnMapValidChangedCaller( m_deferredDraw ) );
1246 CamWnd_registerCommands( *this );
1248 CamWnd_Add_Handlers_Move( *this );
1250 m_gl_widget.connect( "scroll_event", G_CALLBACK( wheelmove_scroll ), this );
1252 AddSceneChangeCallback( ReferenceCaller<CamWnd, void(), CamWnd_Update>( *this ) );
1254 PressedButtons_connect( g_pressedButtons, m_gl_widget );
1258 if ( m_bFreeMove ) {
1262 CamWnd_Remove_Handlers_Move( *this );
1264 g_signal_handler_disconnect( G_OBJECT( m_gl_widget ), m_sizeHandler );
1265 g_signal_handler_disconnect( G_OBJECT( m_gl_widget ), m_exposeHandler );
1267 m_gl_widget.unref();
1269 m_window_observer->release();
1272 class FloorHeightWalker : public scene::Graph::Walker
1278 FloorHeightWalker( float current, float& bestUp, float& bestDown ) :
1279 m_current( current ), m_bestUp( bestUp ), m_bestDown( bestDown ){
1280 bestUp = g_MaxWorldCoord;
1281 bestDown = -g_MaxWorldCoord;
1283 bool pre( const scene::Path& path, scene::Instance& instance ) const {
1284 if ( path.top().get().visible()
1285 && Node_isBrush( path.top() ) ) { // this node is a floor
1286 const AABB& aabb = instance.worldAABB();
1287 float floorHeight = aabb.origin.z() + aabb.extents.z();
1288 if ( floorHeight > m_current && floorHeight < m_bestUp ) {
1289 m_bestUp = floorHeight;
1291 if ( floorHeight < m_current && floorHeight > m_bestDown ) {
1292 m_bestDown = floorHeight;
1295 else if( !path.top().get().visible() ){
1302 void CamWnd::Cam_ChangeFloor( bool up ){
1303 float current = m_Camera.origin[2] - 48;
1306 GlobalSceneGraph().traverse( FloorHeightWalker( current, bestUp, bestDown ) );
1308 if ( up && bestUp != g_MaxWorldCoord ) {
1311 if ( !up && bestDown != -g_MaxWorldCoord ) {
1315 m_Camera.origin[2] = current + 48;
1316 Camera_updateModelview( getCamera() );
1317 CamWnd_Update( *this );
1318 CameraMovedNotify();
1325 Sys_GetCursorPos( &m_PositionDragCursorX, &m_PositionDragCursorY );
1328 if ( ( m_bFreeMove && ( buttons == ( RAD_CONTROL | RAD_SHIFT ) ) )
1329 || ( !m_bFreeMove && ( buttons == ( RAD_RBUTTON | RAD_CONTROL ) ) ) ) {
1331 CamWnd_Update( camwnd );
1332 CameraMovedNotify();
1336 void CamWnd::Cam_PositionDrag(){
1339 Sys_GetCursorPos( m_gl_widget, &x, &y );
1340 if ( x != m_PositionDragCursorX || y != m_PositionDragCursorY ) {
1341 x -= m_PositionDragCursorX;
1342 vector3_add( m_Camera.origin, vector3_scaled( m_Camera.vright, x ) );
1343 y -= m_PositionDragCursorY;
1344 m_Camera.origin[2] -= y;
1345 Camera_updateModelview();
1346 CamWnd_Update( camwnd );
1347 CameraMovedNotify();
1349 Sys_SetCursorPos( m_gl_widget, m_PositionDragCursorX, m_PositionDragCursorY );
1355 // NOTE TTimo if there's an OS-level focus out of the application
1356 // then we can release the camera cursor grab
1357 static gboolean camwindow_freemove_focusout( ui::Widget widget, GdkEventFocus* event, gpointer data ){
1358 reinterpret_cast<CamWnd*>( data )->DisableFreeMove();
1362 void CamWnd::EnableFreeMove(){
1363 //globalOutputStream() << "EnableFreeMove\n";
1365 ASSERT_MESSAGE( !m_bFreeMove, "EnableFreeMove: free-move was already enabled" );
1367 Camera_clearMovementFlags( getCamera(), MOVE_ALL );
1369 CamWnd_Remove_Handlers_Move( *this );
1370 CamWnd_Add_Handlers_FreeMove( *this );
1372 gtk_window_set_focus( m_parent, m_gl_widget );
1373 m_freemove_handle_focusout = m_gl_widget.connect( "focus_out_event", G_CALLBACK( camwindow_freemove_focusout ), this );
1374 /* We chose to replace m_parent by m_gl_widget but NetRadiantCustom does:
1375 m_freezePointer.freeze_pointer( m_parent, m_gl_widget, Camera_motionDelta, &m_Camera ); */
1376 m_freezePointer.freeze_pointer( m_gl_widget, Camera_motionDelta, &m_Camera );
1378 CamWnd_Update( *this );
1381 void CamWnd::DisableFreeMove(){
1382 //globalOutputStream() << "DisableFreeMove\n";
1384 ASSERT_MESSAGE( m_bFreeMove, "DisableFreeMove: free-move was not enabled" );
1385 m_bFreeMove = false;
1386 Camera_clearMovementFlags( getCamera(), MOVE_ALL );
1388 CamWnd_Remove_Handlers_FreeMove( *this );
1389 CamWnd_Add_Handlers_Move( *this );
1391 m_freezePointer.unfreeze_pointer( m_gl_widget, true );
1393 g_signal_handler_disconnect( G_OBJECT( m_gl_widget ), m_freemove_handle_focusout );
1395 CamWnd_Update( *this );
1399 #include "renderer.h"
1401 class CamRenderer : public Renderer
1405 state_type() : m_highlight( 0 ), m_state( 0 ), m_lights( 0 ){
1407 unsigned int m_highlight;
1409 const LightList* m_lights;
1412 std::vector<state_type> m_state_stack;
1413 RenderStateFlags m_globalstate;
1414 Shader* m_state_select0;
1415 Shader* m_state_select1;
1416 const Vector3& m_viewer;
1419 CamRenderer( RenderStateFlags globalstate, Shader* select0, Shader* select1, const Vector3& viewer ) :
1420 m_globalstate( globalstate ),
1421 m_state_select0( select0 ),
1422 m_state_select1( select1 ),
1424 ASSERT_NOTNULL( select0 );
1425 ASSERT_NOTNULL( select1 );
1426 m_state_stack.push_back( state_type() );
1429 void SetState( Shader* state, EStyle style ){
1430 ASSERT_NOTNULL( state );
1431 if ( style == eFullMaterials ) {
1432 m_state_stack.back().m_state = state;
1435 EStyle getStyle() const {
1436 return eFullMaterials;
1439 m_state_stack.push_back( m_state_stack.back() );
1442 ASSERT_MESSAGE( !m_state_stack.empty(), "popping empty stack" );
1443 m_state_stack.pop_back();
1445 void Highlight( EHighlightMode mode, bool bEnable = true ){
1447 m_state_stack.back().m_highlight |= mode;
1449 m_state_stack.back().m_highlight &= ~mode;
1452 void setLights( const LightList& lights ){
1453 m_state_stack.back().m_lights = &lights;
1455 void addRenderable( const OpenGLRenderable& renderable, const Matrix4& world ){
1456 if ( m_state_stack.back().m_highlight & ePrimitive ) {
1457 m_state_select0->addRenderable( renderable, world, m_state_stack.back().m_lights );
1459 if ( m_state_stack.back().m_highlight & eFace ) {
1460 m_state_select1->addRenderable( renderable, world, m_state_stack.back().m_lights );
1463 m_state_stack.back().m_state->addRenderable( renderable, world, m_state_stack.back().m_lights );
1466 void render( const Matrix4& modelview, const Matrix4& projection ){
1467 GlobalShaderCache().render( m_globalstate, modelview, projection, m_viewer );
1477 void ShowStatsToggle(){
1478 g_camwindow_globals_private.m_showStats ^= 1;
1481 void ShowStatsExport( const Callback<void(bool)> &importer ){
1482 importer( g_camwindow_globals_private.m_showStats );
1485 FreeCaller<void(const Callback<void(bool)>&), ShowStatsExport> g_show_stats_caller;
1486 Callback<void(const Callback<void(bool)> &)> g_show_stats_callback( g_show_stats_caller );
1487 ToggleItem g_show_stats( g_show_stats_callback );
1490 void ShowStatsToggle(){
1491 g_camwindow_globals_private.m_showStats ^= 1;
1492 // g_show_stats.update();
1495 typedef FreeCaller<void(), ShowStatsToggle> ShowStatsToggleCaller;
1496 void ShowStatsExport( const Callback<void(bool)> & importer ){
1497 importer( g_camwindow_globals_private.m_showStats );
1499 typedef FreeCaller<void(const Callback<void(bool)> &), ShowStatsExport> ShowStatsExportCaller;
1501 ShowStatsExportCaller g_show_stats_caller;
1502 Callback<void(const Callback<void(bool)> &)> g_show_stats_callback( g_show_stats_caller );
1503 ToggleItem g_show_stats( g_show_stats_callback );
1505 void CamWnd::Cam_Draw(){
1506 glViewport( 0, 0, m_Camera.width, m_Camera.height );
1509 glGetIntegerv( GL_VIEWPORT, viewprt );
1512 // enable depth buffer writes
1513 glDepthMask( GL_TRUE );
1514 glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
1516 Vector3 clearColour( 0, 0, 0 );
1517 if ( m_Camera.draw_mode != cd_lighting ) {
1518 clearColour = g_camwindow_globals.color_cameraback;
1521 glClearColor( clearColour[0], clearColour[1], clearColour[2], 0 );
1522 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
1524 extern void Renderer_ResetStats();
1525 Renderer_ResetStats();
1526 extern void Cull_ResetStats();
1529 glMatrixMode( GL_PROJECTION );
1530 glLoadMatrixf( reinterpret_cast<const float*>( &m_Camera.projection ) );
1532 glMatrixMode( GL_MODELVIEW );
1533 glLoadMatrixf( reinterpret_cast<const float*>( &m_Camera.modelview ) );
1536 // one directional light source directly behind the viewer
1538 GLfloat inverse_cam_dir[4], ambient[4], diffuse[4]; //, material[4];
1540 ambient[0] = ambient[1] = ambient[2] = 0.4f;
1542 diffuse[0] = diffuse[1] = diffuse[2] = 0.4f;
1544 //material[0] = material[1] = material[2] = 0.8f;
1545 //material[3] = 1.0f;
1547 inverse_cam_dir[0] = m_Camera.vpn[0];
1548 inverse_cam_dir[1] = m_Camera.vpn[1];
1549 inverse_cam_dir[2] = m_Camera.vpn[2];
1550 inverse_cam_dir[3] = 0;
1552 glLightfv( GL_LIGHT0, GL_POSITION, inverse_cam_dir );
1554 glLightfv( GL_LIGHT0, GL_AMBIENT, ambient );
1555 glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuse );
1557 glEnable( GL_LIGHT0 );
1561 unsigned int globalstate = RENDER_DEPTHTEST | RENDER_COLOURWRITE | RENDER_DEPTHWRITE | RENDER_ALPHATEST | RENDER_BLEND | RENDER_CULLFACE | RENDER_COLOURARRAY | RENDER_OFFSETLINE | RENDER_POLYGONSMOOTH | RENDER_LINESMOOTH | RENDER_FOG | RENDER_COLOURCHANGE;
1562 switch ( m_Camera.draw_mode )
1567 globalstate |= RENDER_FILL
1573 globalstate |= RENDER_FILL
1580 globalstate |= RENDER_FILL
1594 // if ( !g_xywindow_globals.m_bNoStipple ) {
1595 globalstate |= RENDER_LINESTIPPLE | RENDER_POLYGONSTIPPLE;
1599 CamRenderer renderer( globalstate, m_state_select2, m_state_select1, m_view.getViewer() );
1601 Scene_Render( renderer, m_view );
1603 renderer.render( m_Camera.modelview, m_Camera.projection );
1606 // prepare for 2d stuff
1607 glColor4f( 1, 1, 1, 1 );
1608 glDisable( GL_BLEND );
1609 glMatrixMode( GL_PROJECTION );
1611 glOrtho( 0, (float)m_Camera.width, 0, (float)m_Camera.height, -100, 100 );
1612 glScalef( 1, -1, 1 );
1613 glTranslatef( 0, -(float)m_Camera.height, 0 );
1614 glMatrixMode( GL_MODELVIEW );
1617 if ( GlobalOpenGL().GL_1_3() ) {
1618 glClientActiveTexture( GL_TEXTURE0 );
1619 glActiveTexture( GL_TEXTURE0 );
1622 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1623 glDisableClientState( GL_NORMAL_ARRAY );
1624 glDisableClientState( GL_COLOR_ARRAY );
1626 glDisable( GL_TEXTURE_2D );
1627 glDisable( GL_LIGHTING );
1628 glDisable( GL_COLOR_MATERIAL );
1629 glDisable( GL_DEPTH_TEST );
1630 glColor3f( 1.f, 1.f, 1.f );
1633 // draw the crosshair
1634 if ( m_bFreeMove ) {
1635 glBegin( GL_LINES );
1636 glVertex2f( (float)m_Camera.width / 2.f, (float)m_Camera.height / 2.f + 6 );
1637 glVertex2f( (float)m_Camera.width / 2.f, (float)m_Camera.height / 2.f + 2 );
1638 glVertex2f( (float)m_Camera.width / 2.f, (float)m_Camera.height / 2.f - 6 );
1639 glVertex2f( (float)m_Camera.width / 2.f, (float)m_Camera.height / 2.f - 2 );
1640 glVertex2f( (float)m_Camera.width / 2.f + 6, (float)m_Camera.height / 2.f );
1641 glVertex2f( (float)m_Camera.width / 2.f + 2, (float)m_Camera.height / 2.f );
1642 glVertex2f( (float)m_Camera.width / 2.f - 6, (float)m_Camera.height / 2.f );
1643 glVertex2f( (float)m_Camera.width / 2.f - 2, (float)m_Camera.height / 2.f );
1647 if ( g_camwindow_globals_private.m_showStats ) {
1648 glRasterPos3f( 1.0f, static_cast<float>( m_Camera.height ) - GlobalOpenGL().m_font->getPixelDescent(), 0.0f );
1649 extern const char* Renderer_GetStats();
1650 GlobalOpenGL().drawString( Renderer_GetStats() );
1652 glRasterPos3f( 1.0f, static_cast<float>( m_Camera.height ) - GlobalOpenGL().m_font->getPixelDescent() - GlobalOpenGL().m_font->getPixelHeight(), 0.0f );
1653 extern const char* Cull_GetStats();
1654 GlobalOpenGL().drawString( Cull_GetStats() );
1657 // bind back to the default texture so that we don't have problems
1658 // elsewhere using/modifying texture maps between contexts
1659 glBindTexture( GL_TEXTURE_2D, 0 );
1662 void CamWnd::draw(){
1665 //globalOutputStream() << "draw...\n";
1666 if ( glwidget_make_current( m_gl_widget ) != FALSE ) {
1667 if ( Map_Valid( g_map ) && ScreenUpdates_Enabled() ) {
1668 GlobalOpenGL_debugAssertNoErrors();
1670 GlobalOpenGL_debugAssertNoErrors();
1673 m_XORRectangle.set( rectangle_t() );
1676 glwidget_swap_buffers( m_gl_widget );
1682 void CamWnd::BenchMark(){
1683 double dStart = Sys_DoubleTime();
1684 for ( int i = 0 ; i < 100 ; i++ )
1687 angles[CAMERA_ROLL] = 0;
1688 angles[CAMERA_PITCH] = 0;
1689 angles[CAMERA_YAW] = i * 360.0f / 100.0f;
1690 Camera_setAngles( *this, angles );
1692 double dEnd = Sys_DoubleTime();
1693 globalOutputStream() << FloatFormat( dEnd - dStart, 5, 2 ) << " seconds\n";
1697 void GlobalCamera_ResetAngles(){
1698 CamWnd& camwnd = *g_camwnd;
1700 angles[CAMERA_ROLL] = angles[CAMERA_PITCH] = 0;
1701 angles[CAMERA_YAW] = 22.5f * floorf( ( Camera_getAngles( camwnd )[CAMERA_YAW] + 11 ) / 22.5f );
1702 Camera_setAngles( camwnd, angles );
1707 void GlobalCamera_FocusOnSelected(){
1708 CamWnd& camwnd = *g_camwnd;
1710 Vector3 angles( Camera_getAngles( camwnd ) );
1711 Vector3 radangles( degrees_to_radians( angles[0] ), degrees_to_radians( angles[1] ), degrees_to_radians( angles[2] ) );
1713 viewvector[0] = cos( radangles[1] ) * cos( radangles[0] );
1714 viewvector[1] = sin( radangles[1] ) * cos( radangles[0] );
1715 viewvector[2] = sin( radangles[0] );
1717 Vector3 camorigin( Camera_getOrigin( camwnd ) );
1719 AABB aabb( aabb_for_minmax( Select_getWorkZone().d_work_min, Select_getWorkZone().d_work_max ) );
1721 View& view = *( camwnd.getCamera().m_view );
1723 Plane3 frustumPlanes[4];
1724 frustumPlanes[0] = plane3_translated( view.getFrustum().left, camorigin - aabb.origin );
1725 frustumPlanes[1] = plane3_translated( view.getFrustum().right, camorigin - aabb.origin );
1726 frustumPlanes[2] = plane3_translated( view.getFrustum().top, camorigin - aabb.origin );
1727 frustumPlanes[3] = plane3_translated( view.getFrustum().bottom, camorigin - aabb.origin );
1729 float offset = 64.0f;
1732 aabb_corners( aabb, corners );
1734 for ( size_t i = 0; i < 4; ++i ){
1735 for ( size_t j = 0; j < 8; ++j ){
1736 Ray ray( aabb.origin, -viewvector );
1737 //Plane3 newplane( frustumPlanes[i].normal(), vector3_dot( frustumPlanes[i].normal(), corners[j] - frustumPlanes[i].normal() * 16.0f ) );
1738 Plane3 newplane( frustumPlanes[i].normal(), vector3_dot( frustumPlanes[i].normal(), corners[j] ) );
1739 float d = vector3_dot( ray.direction, newplane.normal() );
1741 float s = vector3_dot( newplane.normal() * newplane.dist() - ray.origin, newplane.normal() ) / d;
1742 offset = std::max( offset, s );
1746 Camera_setOrigin( camwnd, aabb.origin - viewvector * offset );
1749 void Camera_ChangeFloorUp(){
1750 CamWnd& camwnd = *g_camwnd;
1751 camwnd.Cam_ChangeFloor( true );
1754 void Camera_ChangeFloorDown(){
1755 CamWnd& camwnd = *g_camwnd;
1756 camwnd.Cam_ChangeFloor( false );
1759 void Camera_CubeIn(){
1760 CamWnd& camwnd = *g_camwnd;
1761 g_camwindow_globals.m_nCubicScale--;
1762 if ( g_camwindow_globals.m_nCubicScale < 1 ) {
1763 g_camwindow_globals.m_nCubicScale = 1;
1765 Camera_updateProjection( camwnd.getCamera() );
1766 CamWnd_Update( camwnd );
1767 g_pParentWnd->SetGridStatus();
1770 void Camera_CubeOut(){
1771 CamWnd& camwnd = *g_camwnd;
1772 g_camwindow_globals.m_nCubicScale++;
1773 if ( g_camwindow_globals.m_nCubicScale > 46 ) {
1774 g_camwindow_globals.m_nCubicScale = 46;
1776 Camera_updateProjection( camwnd.getCamera() );
1777 CamWnd_Update( camwnd );
1778 g_pParentWnd->SetGridStatus();
1781 bool Camera_GetFarClip(){
1782 return g_camwindow_globals_private.m_bCubicClipping;
1785 ConstReferenceCaller<bool, void(const Callback<void(bool)> &), PropertyImpl<bool>::Export> g_getfarclip_caller( g_camwindow_globals_private.m_bCubicClipping );
1786 ToggleItem g_getfarclip_item( g_getfarclip_caller );
1788 void Camera_SetFarClip( bool value ){
1789 CamWnd& camwnd = *g_camwnd;
1790 g_camwindow_globals_private.m_bCubicClipping = value;
1791 g_getfarclip_item.update();
1792 Camera_updateProjection( camwnd.getCamera() );
1793 CamWnd_Update( camwnd );
1796 struct Camera_FarClip {
1797 static void Export(const Callback<void(bool)> &returnz) {
1798 returnz(g_camwindow_globals_private.m_bCubicClipping);
1801 static void Import(bool value) {
1802 Camera_SetFarClip(value);
1806 void Camera_ToggleFarClip(){
1807 Camera_SetFarClip( !Camera_GetFarClip() );
1811 void CamWnd_constructToolbar( ui::Toolbar toolbar ){
1812 toolbar_append_toggle_button( toolbar, "Cubic clip the camera view (Ctrl + \\)", "view_cubicclipping.png", "ToggleCubicClip" );
1815 void CamWnd_registerShortcuts(){
1816 toggle_add_accelerator( "ToggleCubicClip" );
1818 if ( g_pGameDescription->mGameType == "doom3" ) {
1819 command_connect_accelerator( "TogglePreview" );
1822 command_connect_accelerator( "FOVInc" );
1823 command_connect_accelerator( "FOVDec" );
1824 command_connect_accelerator( "CameraSpeedInc" );
1825 command_connect_accelerator( "CameraSpeedDec" );
1829 void GlobalCamera_Benchmark(){
1830 CamWnd& camwnd = *g_camwnd;
1834 void GlobalCamera_Update(){
1835 CamWnd& camwnd = *g_camwnd;
1836 CamWnd_Update( camwnd );
1839 camera_draw_mode CamWnd_GetMode(){
1840 return camera_t::draw_mode;
1842 void CamWnd_SetMode( camera_draw_mode mode ){
1843 ShaderCache_setBumpEnabled( mode == cd_lighting );
1844 camera_t::draw_mode = mode;
1845 if ( g_camwnd != 0 ) {
1846 CamWnd_Update( *g_camwnd );
1850 void CamWnd_TogglePreview( void ){
1851 // gametype must be doom3 for this function to work
1852 // if the gametype is not doom3 something is wrong with the
1853 // global command list or somebody else calls this function.
1854 ASSERT_MESSAGE( g_pGameDescription->mGameType == "doom3", "CamWnd_TogglePreview called although mGameType is not doom3 compatible" );
1856 // switch between textured and lighting mode
1857 CamWnd_SetMode( ( CamWnd_GetMode() == cd_lighting ) ? cd_texture : cd_lighting );
1861 CameraModel* g_camera_model = 0;
1863 void CamWnd_LookThroughCamera( CamWnd& camwnd ){
1864 if ( g_camera_model != 0 ) {
1865 CamWnd_Add_Handlers_Move( camwnd );
1866 g_camera_model->setCameraView( 0, Callback<void()>() );
1868 Camera_updateModelview( camwnd.getCamera() );
1869 Camera_updateProjection( camwnd.getCamera() );
1870 CamWnd_Update( camwnd );
1874 inline CameraModel* Instance_getCameraModel( scene::Instance& instance ){
1875 return InstanceTypeCast<CameraModel>::cast( instance );
1878 void CamWnd_LookThroughSelected( CamWnd& camwnd ){
1879 if ( g_camera_model != 0 ) {
1880 CamWnd_LookThroughCamera( camwnd );
1883 if ( GlobalSelectionSystem().countSelected() != 0 ) {
1884 scene::Instance& instance = GlobalSelectionSystem().ultimateSelected();
1885 CameraModel* cameraModel = Instance_getCameraModel( instance );
1886 if ( cameraModel != 0 ) {
1887 CamWnd_Remove_Handlers_Move( camwnd );
1888 g_camera_model = cameraModel;
1889 g_camera_model->setCameraView( &camwnd.getCameraView(), ReferenceCaller<CamWnd, void(), CamWnd_LookThroughCamera>( camwnd ) );
1894 void GlobalCamera_LookThroughSelected(){
1895 CamWnd_LookThroughSelected( *g_camwnd );
1898 void GlobalCamera_LookThroughCamera(){
1899 CamWnd_LookThroughCamera( *g_camwnd );
1903 static void Export(const Callback<void(int)> &returnz) {
1904 switch (CamWnd_GetMode()) {
1920 static void Import(int value) {
1923 CamWnd_SetMode( cd_wire );
1926 CamWnd_SetMode( cd_solid );
1929 CamWnd_SetMode( cd_texture );
1932 CamWnd_SetMode( cd_lighting );
1935 CamWnd_SetMode( cd_texture );
1940 void Camera_constructPreferences( PreferencesPage& page ){
1941 page.appendSlider( "FOV", g_camwindow_globals_private.m_nFOV, TRUE, 0, 0, 100, MIN_FOV, MAX_FOV, 1, 10 );
1942 page.appendSlider( "Movement Speed", g_camwindow_globals_private.m_nMoveSpeed, TRUE, 0, 0, 100, MIN_CAM_SPEED, MAX_CAM_SPEED, 1, 10 );
1943 page.appendCheckBox( "", "Link strafe speed to movement speed", g_camwindow_globals_private.m_bCamLinkSpeed );
1944 page.appendSlider( "Rotation Speed", g_camwindow_globals_private.m_nAngleSpeed, TRUE, 0, 0, 3, 1, 180, 1, 10 );
1945 page.appendCheckBox( "", "Invert mouse vertical axis", g_camwindow_globals_private.m_bCamInverseMouse );
1946 page.appendCheckBox( "", "Zoom In to Mouse pointer", g_camwindow_globals.m_bZoomInToPointer );
1947 page.appendCheckBox(
1948 "", "Discrete movement",
1949 make_property<CamWnd_Move_Discrete>()
1951 page.appendCheckBox(
1952 "", "Enable far-clip plane",
1953 make_property<Camera_FarClip>()
1956 if ( g_pGameDescription->mGameType == "doom3" ) {
1957 const char* render_mode[] = { "Wireframe", "Flatshade", "Textured", "Lighting" };
1961 STRING_ARRAY_RANGE( render_mode ),
1962 make_property<RenderMode>()
1967 const char* render_mode[] = { "Wireframe", "Flatshade", "Textured" };
1971 STRING_ARRAY_RANGE( render_mode ),
1972 make_property<RenderMode>()
1976 const char* strafe_mode[] = { "None", "Up", "Forward", "Both", "Both Inverted" };
1980 g_camwindow_globals_private.m_nStrafeMode,
1981 STRING_ARRAY_RANGE( strafe_mode )
1984 void Camera_constructPage( PreferenceGroup& group ){
1985 PreferencesPage page( group.createPage( "Camera", "Camera View Preferences" ) );
1986 Camera_constructPreferences( page );
1988 void Camera_registerPreferencesPage(){
1989 PreferencesDialog_addSettingsPage( makeCallbackF(Camera_constructPage) );
1992 #include "preferencesystem.h"
1993 #include "stringio.h"
1996 void FOV_increase(){
1997 CamWnd& camwnd = *g_camwnd;
1998 if ( g_camwindow_globals_private.m_nFOV <= ( MAX_FOV - FOV_STEP - 10 ) ) {
1999 g_camwindow_globals_private.m_nFOV += FOV_STEP;
2002 g_camwindow_globals_private.m_nFOV = MAX_FOV - 10;
2004 Camera_updateProjection( camwnd.getCamera() );
2005 CamWnd_Update( camwnd );
2008 void FOV_decrease(){
2009 CamWnd& camwnd = *g_camwnd;
2010 if ( g_camwindow_globals_private.m_nFOV >= ( MIN_FOV + FOV_STEP ) ) {
2011 g_camwindow_globals_private.m_nFOV -= FOV_STEP;
2014 g_camwindow_globals_private.m_nFOV = MIN_FOV;
2016 Camera_updateProjection( camwnd.getCamera() );
2017 CamWnd_Update( camwnd );
2021 void CameraSpeed_increase(){
2022 if ( g_camwindow_globals_private.m_nMoveSpeed <= ( MAX_CAM_SPEED - CAM_SPEED_STEP - 10 ) ) {
2023 g_camwindow_globals_private.m_nMoveSpeed += CAM_SPEED_STEP;
2026 g_camwindow_globals_private.m_nMoveSpeed = MAX_CAM_SPEED - 10;
2030 void CameraSpeed_decrease(){
2031 if ( g_camwindow_globals_private.m_nMoveSpeed >= ( MIN_CAM_SPEED + CAM_SPEED_STEP ) ) {
2032 g_camwindow_globals_private.m_nMoveSpeed -= CAM_SPEED_STEP;
2035 g_camwindow_globals_private.m_nMoveSpeed = MIN_CAM_SPEED;
2039 /// \brief Initialisation for things that have the same lifespan as this module.
2040 void CamWnd_Construct(){
2041 GlobalCommands_insert( "CenterView", makeCallbackF(GlobalCamera_ResetAngles), Accelerator( GDK_KEY_End ) );
2042 GlobalCommands_insert( "CameraFocusOnSelected", makeCallbackF( GlobalCamera_FocusOnSelected ), Accelerator( GDK_Tab ) );
2044 GlobalToggles_insert( "ToggleCubicClip", makeCallbackF(Camera_ToggleFarClip), ToggleItem::AddCallbackCaller( g_getfarclip_item ), Accelerator( '\\', (GdkModifierType)GDK_CONTROL_MASK ) );
2045 GlobalCommands_insert( "CubicClipZoomIn", makeCallbackF(Camera_CubeIn), Accelerator( '[', (GdkModifierType)GDK_CONTROL_MASK ) );
2046 GlobalCommands_insert( "CubicClipZoomOut", makeCallbackF(Camera_CubeOut), Accelerator( ']', (GdkModifierType)GDK_CONTROL_MASK ) );
2048 GlobalCommands_insert( "UpFloor", makeCallbackF(Camera_ChangeFloorUp), Accelerator( GDK_KEY_Prior ) );
2049 GlobalCommands_insert( "DownFloor", makeCallbackF(Camera_ChangeFloorDown), Accelerator( GDK_KEY_Next ) );
2051 GlobalToggles_insert( "ToggleCamera", ToggleShown::ToggleCaller( g_camera_shown ), ToggleItem::AddCallbackCaller( g_camera_shown.m_item ), Accelerator( 'C', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
2052 // GlobalCommands_insert( "LookThroughSelected", makeCallbackF(GlobalCamera_LookThroughSelected) );
2053 // GlobalCommands_insert( "LookThroughCamera", makeCallbackF(GlobalCamera_LookThroughCamera) );
2055 if ( g_pGameDescription->mGameType == "doom3" ) {
2056 GlobalCommands_insert( "TogglePreview", makeCallbackF(CamWnd_TogglePreview), Accelerator( GDK_KEY_F3 ) );
2059 GlobalCommands_insert( "FOVInc", makeCallbackF(FOV_increase), Accelerator( GDK_KEY_KP_Multiply, (GdkModifierType)GDK_SHIFT_MASK ) );
2060 GlobalCommands_insert( "FOVDec", makeCallbackF(FOV_decrease), Accelerator( GDK_KEY_KP_Divide, (GdkModifierType)GDK_SHIFT_MASK ) );
2062 GlobalCommands_insert( "CameraSpeedInc", makeCallbackF(CameraSpeed_increase), Accelerator( GDK_KEY_KP_Add, (GdkModifierType)GDK_SHIFT_MASK ) );
2063 GlobalCommands_insert( "CameraSpeedDec", makeCallbackF(CameraSpeed_decrease), Accelerator( GDK_KEY_KP_Subtract, (GdkModifierType)GDK_SHIFT_MASK ) );
2065 GlobalShortcuts_insert( "CameraForward", Accelerator( GDK_KEY_Up ) );
2066 GlobalShortcuts_insert( "CameraBack", Accelerator( GDK_KEY_Down ) );
2067 GlobalShortcuts_insert( "CameraLeft", Accelerator( GDK_KEY_Left ) );
2068 GlobalShortcuts_insert( "CameraRight", Accelerator( GDK_KEY_Right ) );
2069 GlobalShortcuts_insert( "CameraStrafeRight", Accelerator( 'D' ) );
2070 GlobalShortcuts_insert( "CameraStrafeLeft", Accelerator( 'A' ) );
2072 GlobalShortcuts_insert( "CameraUp", accelerator_null() );
2073 GlobalShortcuts_insert( "CameraDown", accelerator_null() );
2074 GlobalShortcuts_insert( "CameraAngleUp", accelerator_null() );
2075 GlobalShortcuts_insert( "CameraAngleDown", accelerator_null() );
2077 GlobalShortcuts_insert( "CameraFreeMoveForward", Accelerator( 'W' ) );
2078 GlobalShortcuts_insert( "CameraFreeMoveBack", Accelerator( 'S' ) );
2079 GlobalShortcuts_insert( "CameraFreeMoveLeft", Accelerator( 'A' ) );
2080 GlobalShortcuts_insert( "CameraFreeMoveRight", Accelerator( 'D' ) );
2082 GlobalShortcuts_insert( "CameraFreeMoveForward2", Accelerator( GDK_KEY_Up ) );
2083 GlobalShortcuts_insert( "CameraFreeMoveBack2", Accelerator( GDK_KEY_Down ) );
2084 GlobalShortcuts_insert( "CameraFreeMoveLeft2", Accelerator( GDK_KEY_Left ) );
2085 GlobalShortcuts_insert( "CameraFreeMoveRight2", Accelerator( GDK_KEY_Right ) );
2087 GlobalShortcuts_insert( "CameraFreeMoveUp", accelerator_null() );
2088 GlobalShortcuts_insert( "CameraFreeMoveDown", accelerator_null() );
2090 GlobalToggles_insert( "ShowStats", makeCallbackF(ShowStatsToggle), ToggleItem::AddCallbackCaller( g_show_stats ) );
2092 GlobalPreferenceSystem().registerPreference( "ShowStats", make_property_string( g_camwindow_globals_private.m_showStats ) );
2093 GlobalPreferenceSystem().registerPreference( "FOV", make_property_string( g_camwindow_globals_private.m_nFOV ) );
2094 GlobalPreferenceSystem().registerPreference( "MoveSpeed", make_property_string( g_camwindow_globals_private.m_nMoveSpeed ) );
2095 GlobalPreferenceSystem().registerPreference( "CamLinkSpeed", make_property_string( g_camwindow_globals_private.m_bCamLinkSpeed ) );
2096 GlobalPreferenceSystem().registerPreference( "AngleSpeed", make_property_string( g_camwindow_globals_private.m_nAngleSpeed ) );
2097 GlobalPreferenceSystem().registerPreference( "CamInverseMouse", make_property_string( g_camwindow_globals_private.m_bCamInverseMouse ) );
2098 GlobalPreferenceSystem().registerPreference( "CamDiscrete", make_property_string<CamWnd_Move_Discrete>());
2099 GlobalPreferenceSystem().registerPreference( "CubicClipping", make_property_string( g_camwindow_globals_private.m_bCubicClipping ) );
2100 GlobalPreferenceSystem().registerPreference( "CubicScale", make_property_string( g_camwindow_globals.m_nCubicScale ) );
2101 GlobalPreferenceSystem().registerPreference( "SI_Colors4", make_property_string( g_camwindow_globals.color_cameraback ) );
2102 GlobalPreferenceSystem().registerPreference( "SI_Colors12", make_property_string( g_camwindow_globals.color_selbrushes3d ) );
2103 GlobalPreferenceSystem().registerPreference( "CameraRenderMode", make_property_string<RenderMode>() );
2104 GlobalPreferenceSystem().registerPreference( "StrafeMode", make_property_string( g_camwindow_globals_private.m_nStrafeMode ) );
2105 GlobalPreferenceSystem().registerPreference( "3DZoomInToPointer", make_property_string( g_camwindow_globals.m_bZoomInToPointer ) );
2107 CamWnd_constructStatic();
2109 Camera_registerPreferencesPage();
2111 void CamWnd_Destroy(){
2112 CamWnd_destroyStatic();