2011-02-23 10:52:22 +00:00
|
|
|
/*
|
2008-04-16 22:40:48 +00:00
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
2008-04-16 22:40:48 +00:00
|
|
|
* of the License, or (at your option) any later version.
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
2010-02-12 13:34:04 +00:00
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
|
|
|
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* The Original Code is: all of this file.
|
|
|
|
*
|
|
|
|
* Contributor(s): none yet.
|
|
|
|
*
|
2008-04-16 22:40:48 +00:00
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
2002-10-12 11:37:38 +00:00
|
|
|
*/
|
|
|
|
|
2011-02-25 13:38:24 +00:00
|
|
|
/** \file gameengine/Rasterizer/RAS_FramingManager.cpp
|
|
|
|
* \ingroup bgerast
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
#include "RAS_FramingManager.h"
|
|
|
|
#include "RAS_Rect.h"
|
|
|
|
|
|
|
|
void
|
|
|
|
RAS_FramingManager::
|
|
|
|
ComputeDefaultFrustum(
|
|
|
|
const float camnear,
|
|
|
|
const float camfar,
|
|
|
|
const float lens,
|
2011-11-04 14:36:06 +00:00
|
|
|
const float sensor_x, const float sensor_y,
|
|
|
|
const short sensor_fit,
|
2002-10-12 11:37:38 +00:00
|
|
|
const float design_aspect_ratio,
|
|
|
|
RAS_FrameFrustum & frustum
|
2012-09-16 04:58:18 +00:00
|
|
|
) {
|
2011-11-04 14:36:06 +00:00
|
|
|
float halfSize;
|
2002-10-12 11:37:38 +00:00
|
|
|
float sizeX;
|
|
|
|
float sizeY;
|
|
|
|
|
2012-03-24 07:52:14 +00:00
|
|
|
if (sensor_fit==RAS_SENSORFIT_AUTO) {
|
2011-11-04 14:36:06 +00:00
|
|
|
halfSize = (sensor_x / 2.f) * camnear / lens;
|
|
|
|
|
|
|
|
if (design_aspect_ratio > 1.f) {
|
|
|
|
// halfsize defines the width
|
|
|
|
sizeX = halfSize;
|
|
|
|
sizeY = halfSize/design_aspect_ratio;
|
|
|
|
} else {
|
|
|
|
// halfsize defines the height
|
|
|
|
sizeX = halfSize * design_aspect_ratio;
|
|
|
|
sizeY = halfSize;
|
|
|
|
}
|
|
|
|
}
|
2012-03-24 07:52:14 +00:00
|
|
|
else if (sensor_fit==RAS_SENSORFIT_HOR) {
|
2011-11-04 14:36:06 +00:00
|
|
|
halfSize = (sensor_x / 2.f) * camnear / lens;
|
2002-10-12 11:37:38 +00:00
|
|
|
sizeX = halfSize;
|
|
|
|
sizeY = halfSize/design_aspect_ratio;
|
2011-11-04 14:36:06 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
halfSize = (sensor_y / 2.f) * camnear / lens;
|
2002-10-12 11:37:38 +00:00
|
|
|
sizeX = halfSize * design_aspect_ratio;
|
|
|
|
sizeY = halfSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
frustum.x2 = sizeX;
|
|
|
|
frustum.x1 = -frustum.x2;
|
|
|
|
frustum.y2 = sizeY;
|
|
|
|
frustum.y1 = -frustum.y2;
|
|
|
|
frustum.camnear = camnear;
|
|
|
|
frustum.camfar = camfar;
|
|
|
|
}
|
|
|
|
|
2009-04-26 12:23:30 +00:00
|
|
|
void
|
|
|
|
RAS_FramingManager::
|
|
|
|
ComputeDefaultOrtho(
|
|
|
|
const float camnear,
|
|
|
|
const float camfar,
|
|
|
|
const float scale,
|
|
|
|
const float design_aspect_ratio,
|
2011-11-04 14:36:06 +00:00
|
|
|
const short sensor_fit,
|
2009-04-26 12:23:30 +00:00
|
|
|
RAS_FrameFrustum & frustum
|
|
|
|
)
|
|
|
|
{
|
|
|
|
float halfSize = scale*0.5f;
|
|
|
|
float sizeX;
|
|
|
|
float sizeY;
|
|
|
|
|
2012-03-24 07:52:14 +00:00
|
|
|
if (sensor_fit==RAS_SENSORFIT_AUTO) {
|
2011-11-04 14:36:06 +00:00
|
|
|
if (design_aspect_ratio > 1.f) {
|
|
|
|
// halfsize defines the width
|
|
|
|
sizeX = halfSize;
|
|
|
|
sizeY = halfSize/design_aspect_ratio;
|
|
|
|
} else {
|
|
|
|
// halfsize defines the height
|
|
|
|
sizeX = halfSize * design_aspect_ratio;
|
|
|
|
sizeY = halfSize;
|
|
|
|
}
|
|
|
|
}
|
2012-03-24 07:52:14 +00:00
|
|
|
else if (sensor_fit==RAS_SENSORFIT_HOR) {
|
2009-04-26 12:23:30 +00:00
|
|
|
sizeX = halfSize;
|
|
|
|
sizeY = halfSize/design_aspect_ratio;
|
2011-11-04 14:36:06 +00:00
|
|
|
}
|
|
|
|
else {
|
2009-04-26 12:23:30 +00:00
|
|
|
sizeX = halfSize * design_aspect_ratio;
|
|
|
|
sizeY = halfSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
frustum.x2 = sizeX;
|
|
|
|
frustum.x1 = -frustum.x2;
|
|
|
|
frustum.y2 = sizeY;
|
|
|
|
frustum.y1 = -frustum.y2;
|
2011-10-20 06:38:45 +00:00
|
|
|
frustum.camnear = camnear;
|
2009-04-26 12:23:30 +00:00
|
|
|
frustum.camfar = camfar;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
void
|
|
|
|
RAS_FramingManager::
|
|
|
|
ComputeBestFitViewRect(
|
|
|
|
const RAS_Rect &availableViewport,
|
|
|
|
const float design_aspect_ratio,
|
|
|
|
RAS_Rect &viewport
|
2012-03-28 05:03:24 +00:00
|
|
|
) {
|
2002-10-12 11:37:38 +00:00
|
|
|
// try and honour the aspect ratio when setting the
|
|
|
|
// drawable area. If we don't do this we are liable
|
|
|
|
// to get a lot of distortion in the rendered image.
|
|
|
|
|
|
|
|
int width = availableViewport.GetWidth();
|
|
|
|
int height = availableViewport.GetHeight();
|
|
|
|
float window_aspect = float(width)/float(height);
|
|
|
|
|
|
|
|
if (window_aspect < design_aspect_ratio) {
|
|
|
|
int v_height = (int)(width / design_aspect_ratio);
|
|
|
|
int left_over = (height - v_height) / 2;
|
|
|
|
|
|
|
|
viewport.SetLeft(availableViewport.GetLeft());
|
|
|
|
viewport.SetBottom(availableViewport.GetBottom() + left_over);
|
|
|
|
viewport.SetRight(availableViewport.GetLeft() + width);
|
|
|
|
viewport.SetTop(availableViewport.GetBottom() + left_over + v_height);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
int v_width = (int)(height * design_aspect_ratio);
|
|
|
|
int left_over = (width - v_width) / 2;
|
|
|
|
|
|
|
|
viewport.SetLeft(availableViewport.GetLeft() + left_over);
|
|
|
|
viewport.SetBottom(availableViewport.GetBottom());
|
|
|
|
viewport.SetRight(availableViewport.GetLeft() + v_width + left_over);
|
|
|
|
viewport.SetTop(availableViewport.GetBottom() + height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RAS_FramingManager::
|
|
|
|
ComputeViewport(
|
|
|
|
const RAS_FrameSettings &settings,
|
|
|
|
const RAS_Rect &availableViewport,
|
|
|
|
RAS_Rect &viewport
|
2012-03-28 05:03:24 +00:00
|
|
|
) {
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
RAS_FrameSettings::RAS_FrameType type = settings.FrameType();
|
|
|
|
const int winx = availableViewport.GetWidth();
|
|
|
|
const int winy = availableViewport.GetHeight();
|
|
|
|
|
|
|
|
const float design_width = float(settings.DesignAspectWidth());
|
|
|
|
const float design_height = float(settings.DesignAspectHeight());
|
|
|
|
|
|
|
|
float design_aspect_ratio = float(1);
|
|
|
|
|
|
|
|
if (design_height == float(0)) {
|
|
|
|
// well this is ill defined
|
|
|
|
// lets just scale the thing
|
|
|
|
|
|
|
|
type = RAS_FrameSettings::e_frame_scale;
|
|
|
|
} else {
|
|
|
|
design_aspect_ratio = design_width/design_height;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
|
|
|
|
case RAS_FrameSettings::e_frame_scale :
|
|
|
|
case RAS_FrameSettings::e_frame_extend:
|
|
|
|
{
|
|
|
|
viewport.SetLeft(availableViewport.GetLeft());
|
|
|
|
viewport.SetBottom(availableViewport.GetBottom());
|
|
|
|
viewport.SetRight(availableViewport.GetLeft() + int(winx));
|
|
|
|
viewport.SetTop(availableViewport.GetBottom() + int(winy));
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case RAS_FrameSettings::e_frame_bars:
|
|
|
|
{
|
|
|
|
ComputeBestFitViewRect(
|
|
|
|
availableViewport,
|
2012-09-16 04:58:18 +00:00
|
|
|
design_aspect_ratio,
|
2002-10-12 11:37:38 +00:00
|
|
|
viewport
|
|
|
|
);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default :
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RAS_FramingManager::
|
|
|
|
ComputeFrustum(
|
|
|
|
const RAS_FrameSettings &settings,
|
|
|
|
const RAS_Rect &availableViewport,
|
|
|
|
const RAS_Rect &viewport,
|
|
|
|
const float lens,
|
2011-11-04 14:36:06 +00:00
|
|
|
const float sensor_x, const float sensor_y, const short sensor_fit,
|
2002-10-12 11:37:38 +00:00
|
|
|
const float camnear,
|
|
|
|
const float camfar,
|
|
|
|
RAS_FrameFrustum &frustum
|
2012-03-28 05:03:24 +00:00
|
|
|
) {
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
RAS_FrameSettings::RAS_FrameType type = settings.FrameType();
|
|
|
|
|
|
|
|
const float design_width = float(settings.DesignAspectWidth());
|
|
|
|
const float design_height = float(settings.DesignAspectHeight());
|
|
|
|
|
|
|
|
float design_aspect_ratio = float(1);
|
|
|
|
|
|
|
|
if (design_height == float(0)) {
|
|
|
|
// well this is ill defined
|
|
|
|
// lets just scale the thing
|
|
|
|
|
|
|
|
type = RAS_FrameSettings::e_frame_scale;
|
|
|
|
} else {
|
|
|
|
design_aspect_ratio = design_width/design_height;
|
|
|
|
}
|
|
|
|
|
|
|
|
ComputeDefaultFrustum(
|
|
|
|
camnear,
|
|
|
|
camfar,
|
|
|
|
lens,
|
2011-11-04 14:36:06 +00:00
|
|
|
sensor_x,
|
|
|
|
sensor_y,
|
|
|
|
sensor_fit,
|
2002-10-12 11:37:38 +00:00
|
|
|
design_aspect_ratio,
|
|
|
|
frustum
|
|
|
|
);
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
|
|
|
|
case RAS_FrameSettings::e_frame_extend:
|
|
|
|
{
|
2013-04-18 23:34:32 +00:00
|
|
|
float x_scale, y_scale;
|
|
|
|
switch (sensor_fit) {
|
|
|
|
case RAS_SENSORFIT_HOR:
|
|
|
|
{
|
|
|
|
x_scale = 1.0;
|
|
|
|
y_scale = float(viewport.GetHeight()) / float(viewport.GetWidth());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RAS_SENSORFIT_VERT:
|
|
|
|
{
|
|
|
|
x_scale = float(viewport.GetWidth()) / float(viewport.GetHeight());
|
|
|
|
y_scale = 1.0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RAS_SENSORFIT_AUTO:
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
RAS_Rect vt;
|
|
|
|
ComputeBestFitViewRect(
|
|
|
|
availableViewport,
|
|
|
|
design_aspect_ratio,
|
|
|
|
vt
|
|
|
|
);
|
|
|
|
|
|
|
|
// now scale the calculated frustum by the difference
|
|
|
|
// between vt and the viewport in each axis.
|
|
|
|
// These are always > 1
|
|
|
|
|
|
|
|
x_scale = float(viewport.GetWidth())/float(vt.GetWidth());
|
|
|
|
y_scale = float(viewport.GetHeight())/float(vt.GetHeight());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
frustum.x1 *= x_scale;
|
|
|
|
frustum.x2 *= x_scale;
|
|
|
|
frustum.y1 *= y_scale;
|
|
|
|
frustum.y2 *= y_scale;
|
|
|
|
|
|
|
|
break;
|
2012-09-16 04:58:18 +00:00
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
case RAS_FrameSettings::e_frame_scale :
|
|
|
|
case RAS_FrameSettings::e_frame_bars:
|
|
|
|
default :
|
|
|
|
break;
|
|
|
|
}
|
2012-09-16 04:58:18 +00:00
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2009-04-26 12:23:30 +00:00
|
|
|
void
|
|
|
|
RAS_FramingManager::
|
|
|
|
ComputeOrtho(
|
|
|
|
const RAS_FrameSettings &settings,
|
|
|
|
const RAS_Rect &availableViewport,
|
|
|
|
const RAS_Rect &viewport,
|
|
|
|
const float scale,
|
|
|
|
const float camnear,
|
|
|
|
const float camfar,
|
2011-11-04 14:36:06 +00:00
|
|
|
const short sensor_fit,
|
2009-04-26 12:23:30 +00:00
|
|
|
RAS_FrameFrustum &frustum
|
|
|
|
)
|
|
|
|
{
|
|
|
|
RAS_FrameSettings::RAS_FrameType type = settings.FrameType();
|
|
|
|
|
|
|
|
const float design_width = float(settings.DesignAspectWidth());
|
|
|
|
const float design_height = float(settings.DesignAspectHeight());
|
|
|
|
|
|
|
|
float design_aspect_ratio = float(1);
|
|
|
|
|
|
|
|
if (design_height == float(0)) {
|
|
|
|
// well this is ill defined
|
|
|
|
// lets just scale the thing
|
|
|
|
type = RAS_FrameSettings::e_frame_scale;
|
|
|
|
} else {
|
|
|
|
design_aspect_ratio = design_width/design_height;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ComputeDefaultOrtho(
|
|
|
|
camnear,
|
|
|
|
camfar,
|
|
|
|
scale,
|
|
|
|
design_aspect_ratio,
|
2011-11-04 14:36:06 +00:00
|
|
|
sensor_fit,
|
2009-04-26 12:23:30 +00:00
|
|
|
frustum
|
|
|
|
);
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
|
|
|
|
case RAS_FrameSettings::e_frame_extend:
|
|
|
|
{
|
2013-04-18 23:34:32 +00:00
|
|
|
float x_scale, y_scale;
|
|
|
|
switch (sensor_fit) {
|
|
|
|
case RAS_SENSORFIT_HOR:
|
|
|
|
{
|
|
|
|
x_scale = 1.0;
|
|
|
|
y_scale = float(viewport.GetHeight()) / float(viewport.GetWidth());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RAS_SENSORFIT_VERT:
|
|
|
|
{
|
|
|
|
x_scale = float(viewport.GetWidth()) / float(viewport.GetHeight());
|
|
|
|
y_scale = 1.0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RAS_SENSORFIT_AUTO:
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
RAS_Rect vt;
|
|
|
|
ComputeBestFitViewRect(
|
|
|
|
availableViewport,
|
|
|
|
design_aspect_ratio,
|
|
|
|
vt
|
|
|
|
);
|
|
|
|
|
|
|
|
// now scale the calculated frustum by the difference
|
|
|
|
// between vt and the viewport in each axis.
|
|
|
|
// These are always > 1
|
|
|
|
|
|
|
|
x_scale = float(viewport.GetWidth())/float(vt.GetWidth());
|
|
|
|
y_scale = float(viewport.GetHeight())/float(vt.GetHeight());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-04-26 12:23:30 +00:00
|
|
|
|
|
|
|
frustum.x1 *= x_scale;
|
|
|
|
frustum.x2 *= x_scale;
|
|
|
|
frustum.y1 *= y_scale;
|
|
|
|
frustum.y2 *= y_scale;
|
|
|
|
|
|
|
|
break;
|
2012-09-16 04:58:18 +00:00
|
|
|
}
|
2009-04-26 12:23:30 +00:00
|
|
|
case RAS_FrameSettings::e_frame_scale :
|
|
|
|
case RAS_FrameSettings::e_frame_bars:
|
|
|
|
default :
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
|