From: James Turner Date: Mon, 19 Sep 2011 08:00:23 +0000 (+0100) Subject: Push aspect-ratio handling into CameraGroup, so renderer doesn't need to resize viewp... X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=5a17ccf9eb7f76ef830fc364ee242c6fec3bda40;p=flightgear.git Push aspect-ratio handling into CameraGroup, so renderer doesn't need to resize viewports each update. --- diff --git a/src/GUI/gui_funcs.cxx b/src/GUI/gui_funcs.cxx index 9713b45c6..538eb755d 100644 --- a/src/GUI/gui_funcs.cxx +++ b/src/GUI/gui_funcs.cxx @@ -322,7 +322,7 @@ void fgHiResDump() int curColumn = trGet(tr, TR_CURRENT_COLUMN); // int curRow = trGet(tr, TR_CURRENT_ROW); - renderer->update( false ); + renderer->update(); // OSGFIXME // if ( do_hud ) // fgUpdateHUD( curColumn*hud_col_step, curRow*hud_row_step, diff --git a/src/Main/CameraGroup.cxx b/src/Main/CameraGroup.cxx index 78a27891b..3e70e9fe1 100644 --- a/src/Main/CameraGroup.cxx +++ b/src/Main/CameraGroup.cxx @@ -263,6 +263,22 @@ void CameraGroup::setCameraParameters(float vfov, float aspectRatio) 1.0f / aspectRatio, _zNear, _zFar); } + +double CameraGroup::getMasterAspectRatio() const +{ + if (_cameras.empty()) + return 0.0; + + const CameraInfo* info = _cameras.front(); + + const osg::Viewport* viewport = info->camera->getViewport(); + if (!viewport) { + return 0.0; + } + + return static_cast(viewport->height()) / viewport->width(); +} + } namespace diff --git a/src/Main/CameraGroup.hxx b/src/Main/CameraGroup.hxx index 1e18e28e1..e0808ce47 100644 --- a/src/Main/CameraGroup.hxx +++ b/src/Main/CameraGroup.hxx @@ -183,6 +183,11 @@ public: void buildDistortionCamera(const SGPropertyNode* psNode, osg::Camera* camera); + + /** + * get aspect ratio of master camera's viewport + */ + double getMasterAspectRatio() const; protected: CameraList _cameras; osg::ref_ptr _viewer; diff --git a/src/Main/fg_os_osgviewer.cxx b/src/Main/fg_os_osgviewer.cxx index 764f90735..6952195f5 100644 --- a/src/Main/fg_os_osgviewer.cxx +++ b/src/Main/fg_os_osgviewer.cxx @@ -281,7 +281,7 @@ int fgOSMainLoop() fgIdleHandler idleFunc = manipulator->getIdleHandler(); if (idleFunc) (*idleFunc)(); - globals->get_renderer()->update(true); + globals->get_renderer()->update(); viewer->frame(); } diff --git a/src/Main/renderer.cxx b/src/Main/renderer.cxx index 6aa796d69..57980c785 100644 --- a/src/Main/renderer.cxx +++ b/src/Main/renderer.cxx @@ -597,7 +597,7 @@ FGRenderer::setupView( void ) // Update all Visuals (redraws anything graphics related) void -FGRenderer::update( bool refresh_camera_settings ) { +FGRenderer::update( ) { if (!(_scenery_loaded->getBoolValue() || _scenery_override->getBoolValue())) { @@ -649,12 +649,7 @@ FGRenderer::update( bool refresh_camera_settings ) { FGViewer *current__view = globals->get_current_view(); // Force update of center dependent values ... current__view->set_dirty(); - - if ( refresh_camera_settings ) { - // update view port - resize( _xsize->getIntValue(), - _ysize->getIntValue() ); - } + osg::Camera *camera = viewer->getCamera(); bool skyblend = _skyblend->getBoolValue(); @@ -771,22 +766,9 @@ FGRenderer::update( bool refresh_camera_settings ) { CameraGroup::getDefault()->setCameraCullMasks(cullMask); } - - -// options.cxx needs to see this for toggle_panel() -// Handle new window size or exposure void -FGRenderer::resize( int width, int height ) { - -// the following breaks aspect-ratio of the main 3D scenery window when 2D panels are moved -// in y direction - causing issues for aircraft with 2D panels (/sim/virtual_cockpit=false). -// Disabling for now. Seems this useful for the pre-OSG time only. -// if ( (!_virtual_cockpit->getBoolValue()) -// && fgPanelVisible() && idle_state == 1000 ) { -// view_h = (int)(height * (globals->get_current_panel()->getViewHeight() - -// globals->get_current_panel()->getYOffset()) / 768.0); -// } - +FGRenderer::resize( int width, int height ) +{ int curWidth = _xsize->getIntValue(), curHeight = _ysize->getIntValue(); if ((curHeight != height) || (curWidth != width)) { @@ -794,18 +776,6 @@ FGRenderer::resize( int width, int height ) { _xsize->setIntValue(width); _ysize->setIntValue(height); } - - // must set view aspect ratio each frame, or initial values are wrong. - // should probably be fixed 'smarter' during view setup. - double aspect = height / (double) width; - - // for all views - FGViewMgr *viewmgr = globals->get_viewmgr(); - if (viewmgr) { - for ( int i = 0; i < viewmgr->size(); ++i ) { - viewmgr->get_view(i)->set_aspect_ratio(aspect); - } - } } bool diff --git a/src/Main/renderer.hxx b/src/Main/renderer.hxx index 67b9ff926..84b03d87e 100644 --- a/src/Main/renderer.hxx +++ b/src/Main/renderer.hxx @@ -51,10 +51,7 @@ public: void resize(int width, int height ); - // calling update( refresh_camera_settings = false ) will not - // touch window or camera settings. This is useful for the tiled - // renderer which needs to set the view frustum itself. - void update( bool refresh_camera_settings); + void update(); /** Just pick into the scene and return the pick callbacks on the way ... */ diff --git a/src/Main/viewer.cxx b/src/Main/viewer.cxx index 57e60f481..49d77cf5b 100644 --- a/src/Main/viewer.cxx +++ b/src/Main/viewer.cxx @@ -65,7 +65,6 @@ FGViewer::FGViewer( fgViewType Type, bool from_model, int from_model_index, _pitch_deg(0), _heading_deg(0), _scaling_type(FG_SCALING_MAX), - _aspect_ratio(0), _cameraGroup(CameraGroup::getDefault()) { _absolute_view_pos = SGVec3d(0, 0, 0); @@ -102,7 +101,7 @@ FGViewer::FGViewer( fgViewType Type, bool from_model, int from_model_index, } else { _fov_deg = 55; } - _aspect_ratio = 1; + _aspect_ratio_multiplier = aspect_ratio_multiplier; _target_offset_m.x() = target_x_offset_m; _target_offset_m.y() = target_y_offset_m; @@ -539,18 +538,19 @@ FGViewer::updateDampOutput(double dt) double FGViewer::get_h_fov() { + double aspectRatio = _cameraGroup->getMasterAspectRatio(); switch (_scaling_type) { case FG_SCALING_WIDTH: // h_fov == fov return _fov_deg; case FG_SCALING_MAX: - if (_aspect_ratio < 1.0) { + if (aspectRatio < 1.0) { // h_fov == fov return _fov_deg; } else { // v_fov == fov return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) - / (_aspect_ratio*_aspect_ratio_multiplier)) + / (aspectRatio*_aspect_ratio_multiplier)) * SG_RADIANS_TO_DEGREES * 2; } default: @@ -564,18 +564,19 @@ FGViewer::get_h_fov() double FGViewer::get_v_fov() { + double aspectRatio = _cameraGroup->getMasterAspectRatio(); switch (_scaling_type) { case FG_SCALING_WIDTH: // h_fov == fov return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) - * (_aspect_ratio*_aspect_ratio_multiplier)) + * (aspectRatio*_aspect_ratio_multiplier)) * SG_RADIANS_TO_DEGREES * 2; case FG_SCALING_MAX: - if (_aspect_ratio < 1.0) { + if (aspectRatio < 1.0) { // h_fov == fov return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) - * (_aspect_ratio*_aspect_ratio_multiplier)) + * (aspectRatio*_aspect_ratio_multiplier)) * SG_RADIANS_TO_DEGREES * 2; } else { // v_fov == fov @@ -671,3 +672,8 @@ FGViewer::update (double dt) _cameraGroup->setCameraParameters(get_v_fov(), get_aspect_ratio()); } } + +double FGViewer::get_aspect_ratio() const +{ + return _cameraGroup->getMasterAspectRatio(); +} diff --git a/src/Main/viewer.hxx b/src/Main/viewer.hxx index 7cd58adae..ef5df5049 100644 --- a/src/Main/viewer.hxx +++ b/src/Main/viewer.hxx @@ -215,10 +215,7 @@ public: virtual double get_h_fov(); // Get horizontal fov, in degrees. virtual double get_v_fov(); // Get vertical fov, in degrees. - virtual void set_aspect_ratio( double r ) { - _aspect_ratio = r; - } - virtual double get_aspect_ratio() const { return _aspect_ratio; } + virtual double get_aspect_ratio() const; virtual void set_aspect_ratio_multiplier( double m ) { _aspect_ratio_multiplier = m; @@ -305,11 +302,6 @@ private: // the nominal field of view (angle, in degrees) double _fov_deg; - // Ratio of window width and height; height = width * - // aspect_ratio. This value is automatically calculated based on - // window dimentions. - double _aspect_ratio; - // default = 1.0, this value is user configurable and is // multiplied into the aspect_ratio to get the actual vertical fov double _aspect_ratio_multiplier;