ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

scene.cpp

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the AUTHORS
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "engines/stark/scene.h"
00024 
00025 #include "engines/stark/gfx/driver.h"
00026 #include "engines/stark/gfx/renderentry.h"
00027 
00028 #include "common/system.h"
00029 
00030 #include "math/glmath.h"
00031 
00032 namespace Stark {
00033 
00034 Scene::Scene(Gfx::Driver *gfx) :
00035         _gfx(gfx),
00036         _fov(45.0),
00037         _nearClipPlane(100.0),
00038         _farClipPlane(64000.0),
00039         _fadeLevel(1.0),
00040         _floatOffset(0.0),
00041         _shouldRenderShadows(true),
00042         _maxShadowLength(0.075f) {
00043 }
00044 
00045 Scene::~Scene() {
00046 }
00047 
00048 void Scene::initCamera(const Math::Vector3d &position, const Math::Vector3d &lookDirection,
00049         float fov, Common::Rect viewSize, float nearClipPlane, float farClipPlane) {
00050     _cameraPosition = position;
00051     _cameraLookDirection = lookDirection;
00052     _fov = fov;
00053     _viewSize = viewSize;
00054     _nearClipPlane = nearClipPlane;
00055     _farClipPlane = farClipPlane;
00056 
00057     _viewMatrix = Math::makeLookAtMatrix(_cameraPosition, _cameraPosition + _cameraLookDirection, Math::Vector3d(0.0, 0.0, 1.0));
00058     _viewMatrix.transpose(); // Math::makeLookAtMatrix outputs transposed matrices ...
00059     _viewMatrix.translate(-_cameraPosition);
00060 
00061     setSwayAngle(0);
00062     setFadeLevel(1.0);
00063     setFloatOffset(0);
00064 }
00065 
00066 void Scene::scrollCamera(const Common::Rect &viewport) {
00067     _viewport = viewport;
00068 
00069     float xmin, xmax, ymin, ymax;
00070     computeClippingRect(&xmin, &xmax, &ymin, &ymax);
00071 
00072     // The amounts by which translate to clipping planes to account for one pixel
00073     // of camera scrolling movement
00074     float scollXFactor = (xmax - xmin) / _viewport.width();
00075     float scollYFactor = (ymax - ymin) / _viewport.height();
00076 
00077     int32 distanceToRight = _viewport.right - _viewSize.width();
00078     int32 distanceToTop = -_viewport.top;
00079 
00080     xmin += distanceToRight * scollXFactor;
00081     xmax += distanceToRight * scollXFactor;
00082     ymin += distanceToTop * scollYFactor;
00083     ymax += distanceToTop * scollYFactor;
00084 
00085     _projectionMatrix = Math::makeFrustumMatrix(xmin, xmax, ymin, ymax, _nearClipPlane, _farClipPlane);
00086     _projectionMatrix.transpose(); // Math::makeFrustumMatrix outputs transposed matrices ...
00087 }
00088 
00089 void Scene::computeClippingRect(float *xmin, float *xmax, float *ymin, float *ymax) {
00090     float aspectRatio = _viewSize.width() / (float) _viewSize.height();
00091     float xmaxValue = _nearClipPlane * tan(_fov * M_PI / 360.0);
00092     float ymaxValue = xmaxValue / aspectRatio;
00093 
00094     float xminValue = xmaxValue - 2 * xmaxValue * (_viewport.width() / (float) _viewSize.width());
00095     float yminValue = ymaxValue - 2 * ymaxValue * (_viewport.height() / (float) _viewSize.height());
00096 
00097     if (xmin) *xmin = xminValue;
00098     if (xmax) *xmax = xmaxValue;
00099     if (ymin) *ymin = yminValue;
00100     if (ymax) *ymax = ymaxValue;
00101 }
00102 
00103 Math::Ray Scene::makeRayFromMouse(const Common::Point &mouse) const {
00104     Common::Rect gameViewport = _gfx->gameViewport();
00105 
00106     Math::Vector4d in;
00107     in.x() = (mouse.x - gameViewport.left) * 2 / (float) gameViewport.width() - 1.0;
00108     in.y() = (gameViewport.bottom - mouse.y) * 2 / (float) gameViewport.height() - 1.0;
00109     in.z() = 1.0;
00110     in.w() = 1.0;
00111 
00112     Math::Matrix4 view = _viewMatrix;
00113     view.translate(_cameraPosition);
00114 
00115     Math::Matrix4 A = _projectionMatrix * view;
00116     A.inverse();
00117 
00118     Math::Vector4d out = A * in;
00119 
00120     Math::Vector3d origin = _cameraPosition;
00121     Math::Vector3d direction = Math::Vector3d(out.x(), out.y(), out.z());
00122     direction.normalize();
00123 
00124     return Math::Ray(origin, direction);
00125 }
00126 
00127 Common::Point Scene::convertPosition3DToGameScreenOriginal(const Math::Vector3d &obj) const {
00128     Math::Vector4d in;
00129     in.set(obj.x(), obj.y(), obj.z(), 1.0);
00130 
00131     Math::Vector4d out = _projectionMatrix * _viewMatrix * in;
00132 
00133     out.x() /= out.w();
00134     out.y() /= out.w();
00135 
00136     Common::Point point;
00137     point.x = (1 + out.x()) * Gfx::Driver::kGameViewportWidth / 2;
00138     point.y = -Gfx::Driver::kTopBorderHeight + Gfx::Driver::kOriginalHeight - (1 + out.y()) * Gfx::Driver::kGameViewportHeight / 2;
00139 
00140     return point;
00141 }
00142 
00143 void Scene::setFadeLevel(float fadeLevel) {
00144     _fadeLevel = fadeLevel;
00145 }
00146 
00147 float Scene::getFadeLevel() const {
00148     return _fadeLevel;
00149 }
00150 
00151 void Scene::setSwayAngle(const Math::Angle &angle) {
00152     _swayAngle = angle;
00153 }
00154 
00155 Math::Angle Scene::getSwayAngle() const {
00156     return _swayAngle;
00157 }
00158 
00159 Math::Vector3d Scene::getSwayDirection() const {
00160     // Actor sway is always along the camera direction, so that
00161     // the rotation is not affected by the direction they are facing.
00162     return _cameraLookDirection;
00163 }
00164 
00165 void Scene::setFloatOffset(float floatOffset) {
00166     _floatOffset = floatOffset;
00167 }
00168 
00169 float Scene::getFloatOffset() const {
00170     return _floatOffset;
00171 }
00172 
00173 void Scene::setupShadows(bool enabled, float length) {
00174     _shouldRenderShadows = enabled;
00175     _maxShadowLength = length;
00176 }
00177 
00178 } // End of namespace Stark


Generated on Sat Oct 19 2019 05:01:07 for ResidualVM by doxygen 1.7.1
curved edge   curved edge