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

stark/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         _maxShadowLength(0.075f) {
00042 }
00043 
00044 Scene::~Scene() {
00045 }
00046 
00047 void Scene::initCamera(const Math::Vector3d &position, const Math::Vector3d &lookDirection,
00048         float fov, Common::Rect viewSize, float nearClipPlane, float farClipPlane) {
00049     _cameraPosition = position;
00050     _cameraLookDirection = lookDirection;
00051     _fov = fov;
00052     _viewSize = viewSize;
00053     _nearClipPlane = nearClipPlane;
00054     _farClipPlane = farClipPlane;
00055 
00056     _viewMatrix = Math::makeLookAtMatrix(_cameraPosition, _cameraPosition + _cameraLookDirection, Math::Vector3d(0.0, 0.0, 1.0));
00057     _viewMatrix.transpose(); // Math::makeLookAtMatrix outputs transposed matrices ...
00058     _viewMatrix.translate(-_cameraPosition);
00059 
00060     setSwayAngle(0);
00061     setFadeLevel(1.0);
00062     setFloatOffset(0);
00063 }
00064 
00065 void Scene::scrollCamera(const Common::Rect &viewport) {
00066     _viewport = viewport;
00067 
00068     float xmin, xmax, ymin, ymax;
00069     computeClippingRect(&xmin, &xmax, &ymin, &ymax);
00070 
00071     // The amounts by which translate to clipping planes to account for one pixel
00072     // of camera scrolling movement
00073     float scollXFactor = (xmax - xmin) / _viewport.width();
00074     float scollYFactor = (ymax - ymin) / _viewport.height();
00075 
00076     int32 distanceToRight = _viewport.right - _viewSize.width();
00077     int32 distanceToTop = -_viewport.top;
00078 
00079     xmin += distanceToRight * scollXFactor;
00080     xmax += distanceToRight * scollXFactor;
00081     ymin += distanceToTop * scollYFactor;
00082     ymax += distanceToTop * scollYFactor;
00083 
00084     _projectionMatrix = Math::makeFrustumMatrix(xmin, xmax, ymin, ymax, _nearClipPlane, _farClipPlane);
00085     _projectionMatrix.transpose(); // Math::makeFrustumMatrix outputs transposed matrices ...
00086 }
00087 
00088 void Scene::computeClippingRect(float *xmin, float *xmax, float *ymin, float *ymax) {
00089     float aspectRatio = _viewSize.width() / (float) _viewSize.height();
00090     float xmaxValue = _nearClipPlane * tan(_fov * M_PI / 360.0);
00091     float ymaxValue = xmaxValue / aspectRatio;
00092 
00093     float xminValue = xmaxValue - 2 * xmaxValue * (_viewport.width() / (float) _viewSize.width());
00094     float yminValue = ymaxValue - 2 * ymaxValue * (_viewport.height() / (float) _viewSize.height());
00095 
00096     if (xmin) *xmin = xminValue;
00097     if (xmax) *xmax = xmaxValue;
00098     if (ymin) *ymin = yminValue;
00099     if (ymax) *ymax = ymaxValue;
00100 }
00101 
00102 Math::Ray Scene::makeRayFromMouse(const Common::Point &mouse) const {
00103     Common::Rect gameViewport = _gfx->gameViewport();
00104 
00105     Math::Vector4d in;
00106     in.x() = (mouse.x - gameViewport.left) * 2 / (float) gameViewport.width() - 1.0;
00107     in.y() = (gameViewport.bottom - mouse.y) * 2 / (float) gameViewport.height() - 1.0;
00108     in.z() = 1.0;
00109     in.w() = 1.0;
00110 
00111     Math::Matrix4 view = _viewMatrix;
00112     view.translate(_cameraPosition);
00113 
00114     Math::Matrix4 A = _projectionMatrix * view;
00115     A.inverse();
00116 
00117     Math::Vector4d out = A * in;
00118 
00119     Math::Vector3d origin = _cameraPosition;
00120     Math::Vector3d direction = Math::Vector3d(out.x(), out.y(), out.z());
00121     direction.normalize();
00122 
00123     return Math::Ray(origin, direction);
00124 }
00125 
00126 Common::Point Scene::convertPosition3DToGameScreenOriginal(const Math::Vector3d &obj) const {
00127     Math::Vector4d in;
00128     in.set(obj.x(), obj.y(), obj.z(), 1.0);
00129 
00130     Math::Vector4d out = _projectionMatrix * _viewMatrix * in;
00131 
00132     out.x() /= out.w();
00133     out.y() /= out.w();
00134 
00135     Common::Point point;
00136     point.x = (1 + out.x()) * Gfx::Driver::kGameViewportWidth / 2;
00137     point.y = -Gfx::Driver::kTopBorderHeight + Gfx::Driver::kOriginalHeight - (1 + out.y()) * Gfx::Driver::kGameViewportHeight / 2;
00138 
00139     return point;
00140 }
00141 
00142 void Scene::setFadeLevel(float fadeLevel) {
00143     _fadeLevel = fadeLevel;
00144 }
00145 
00146 float Scene::getFadeLevel() const {
00147     return _fadeLevel;
00148 }
00149 
00150 void Scene::setSwayAngle(const Math::Angle &angle) {
00151     _swayAngle = angle;
00152 }
00153 
00154 Math::Angle Scene::getSwayAngle() const {
00155     return _swayAngle;
00156 }
00157 
00158 Math::Vector3d Scene::getSwayDirection() const {
00159     // Actor sway is always along the camera direction, so that
00160     // the rotation is not affected by the direction they are facing.
00161     return _cameraLookDirection;
00162 }
00163 
00164 void Scene::setFloatOffset(float floatOffset) {
00165     _floatOffset = floatOffset;
00166 }
00167 
00168 float Scene::getFloatOffset() const {
00169     return _floatOffset;
00170 }
00171 
00172 } // End of namespace Stark


Generated on Sat May 18 2019 05:01:19 for ResidualVM by doxygen 1.7.1
curved edge   curved edge