From 32c487f143353d014aaded2f4065fab1b016ff38 Mon Sep 17 00:00:00 2001 From: Bastien Bouclet Date: Thu, 12 Feb 2015 21:56:00 +0100 Subject: [PATCH] STARK: Correct the projection matrix to match the original --- engines/stark/gfx/driver.h | 5 ++++ engines/stark/gfx/opengl.cpp | 6 +++-- engines/stark/gfx/opengl.h | 2 ++ engines/stark/scene.cpp | 49 ++++++++++++++++-------------------- engines/stark/scene.h | 5 +--- 5 files changed, 33 insertions(+), 34 deletions(-) diff --git a/engines/stark/gfx/driver.h b/engines/stark/gfx/driver.h index 53d961a7499..6ab033c9224 100644 --- a/engines/stark/gfx/driver.h +++ b/engines/stark/gfx/driver.h @@ -41,6 +41,8 @@ public: virtual void setupScreen(int screenW, int screenH, bool fullscreen) = 0; + virtual void setGameViewport() = 0; + virtual void setupPerspective(const Math::Matrix4 &projectionMatrix) = 0; virtual void setupCamera(const Math::Vector3d &position, const Math::Matrix4 &lookAt) = 0; @@ -51,6 +53,9 @@ public: virtual void set3DMode() = 0; + static const int32 kTopBorderHeight = 36; + static const int32 kGameViewportHeight = 365; + protected: int _screenWidth; int _screenHeight; diff --git a/engines/stark/gfx/opengl.cpp b/engines/stark/gfx/opengl.cpp index 212b28bd689..1c8c7d59d2b 100644 --- a/engines/stark/gfx/opengl.cpp +++ b/engines/stark/gfx/opengl.cpp @@ -55,6 +55,10 @@ void OpenGLGfxDriver::setupScreen(int screenW, int screenH, bool fullscreen) { _screenHeight = screenH; } +void OpenGLGfxDriver::setGameViewport() { + glViewport(0, _screenHeight - kGameViewportHeight - kTopBorderHeight, _screenWidth, kGameViewportHeight); +} + void OpenGLGfxDriver::setupPerspective(const Math::Matrix4 &projectionMatrix) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); @@ -81,8 +85,6 @@ void OpenGLGfxDriver::drawSurface(const Graphics::Surface *surface, Common::Poin if (rect.isEmpty()) rect = Common::Rect(surface->w, surface->h); - dest += Common::Point(0, 36); // Top border - start2DMode(); glDisable(GL_TEXTURE_2D); diff --git a/engines/stark/gfx/opengl.h b/engines/stark/gfx/opengl.h index 9c18d6add76..d584996d860 100644 --- a/engines/stark/gfx/opengl.h +++ b/engines/stark/gfx/opengl.h @@ -41,6 +41,8 @@ public: void setupScreen(int screenW, int screenH, bool fullscreen); + void setGameViewport() override; + void setupPerspective(const Math::Matrix4 &projectionMatrix) override; void setupCamera(const Math::Vector3d &position, const Math::Matrix4 &lookAt) override; diff --git a/engines/stark/scene.cpp b/engines/stark/scene.cpp index 6175b513e83..717e194d241 100644 --- a/engines/stark/scene.cpp +++ b/engines/stark/scene.cpp @@ -33,9 +33,7 @@ Scene::Scene(GfxDriver *gfx) : _gfx(gfx), _fov(45.0), _nearClipPlane(100.0), - _farClipPlane(64000.0), - _scollXFactor(0.0), - _scollYFactor(0.0) { + _farClipPlane(64000.0) { } Scene::~Scene() { @@ -50,44 +48,38 @@ void Scene::initCamera(const Math::Vector3d &position, const Math::Vector3d &loo _nearClipPlane = nearClipPlane; _farClipPlane = farClipPlane; - float xmax, ymax; - computeSymmetricPerspectiveRect(nullptr, &xmax, nullptr, &ymax); - - // The amounts by which translate to clipping planes to account for one pixel - // of camera scrolling movement - _scollXFactor = xmax / 640.0 * 2; - _scollYFactor = ymax / 365.0 * 2; - _lookAtMatrix = Math::makeLookAtMatrix(_cameraPosition, _cameraPosition + _cameraLookDirection, Math::Vector3d(0.0, 0.0, 1.0)); } void Scene::scrollCamera(const Common::Rect &viewport) { _viewport = viewport; - // The perspective matrix is a symmetric perspective matrix as returned - // by Math::makePerspectiveMatrix with the clipping rect translated - // to account for the camera scrolling. - float xmin, xmax, ymin, ymax; - computeSymmetricPerspectiveRect(&xmin, &xmax, &ymin, &ymax); + computeClippingRect(&xmin, &xmax, &ymin, &ymax); - int32 distanceToCenterX = _viewport.left + _viewport.width() / 2 - _viewSize.width() / 2; - int32 distanceToCenterY = _viewport.top + _viewport.height() / 2 - _viewSize.height() / 2; + // The amounts by which translate to clipping planes to account for one pixel + // of camera scrolling movement + float scollXFactor = (xmax - xmin) / _viewport.width(); + float scollYFactor = (ymax - ymin) / _viewport.height(); - xmin += distanceToCenterX * _scollXFactor; - xmax += distanceToCenterX * _scollXFactor; - ymin += distanceToCenterY * _scollYFactor; - ymax += distanceToCenterY * _scollYFactor; + int32 distanceToRight = _viewport.right - _viewSize.width(); + int32 distanceToBottom = _viewport.bottom - _viewSize.height(); + + xmin += distanceToRight * scollXFactor; + xmax += distanceToRight * scollXFactor; + ymin += distanceToBottom * scollYFactor; + ymax += distanceToBottom * scollYFactor; _perspectiveMatrix = Math::makeFrustumMatrix(xmin, xmax, ymin, ymax, _nearClipPlane, _farClipPlane); } -void Scene::computeSymmetricPerspectiveRect(float *xmin, float *xmax, float *ymin, float *ymax) { - float aspectRatio = 1.0; - float ymaxValue = _nearClipPlane * tan(_fov * M_PI / 360.0); - float yminValue = -ymaxValue; - float xminValue = yminValue / aspectRatio; - float xmaxValue = ymaxValue / aspectRatio; +void Scene::computeClippingRect(float *xmin, float *xmax, float *ymin, float *ymax) { + float aspectRatio = _viewSize.width() / (float) _viewSize.height(); + float xmaxValue = _nearClipPlane * tan(_fov * M_PI / 360.0); + float ymaxValue = xmaxValue / aspectRatio; + + float xminValue = xmaxValue - 2 * xmaxValue * (_viewport.width() / (float) _viewSize.width()); + float yminValue = ymaxValue - 2 * ymaxValue * (_viewport.height() / (float) _viewSize.height()); if (xmin) *xmin = xminValue; if (xmax) *xmax = xmaxValue; @@ -97,6 +89,7 @@ void Scene::computeSymmetricPerspectiveRect(float *xmin, float *xmax, float *ymi void Scene::render(RenderEntryArray renderEntries) { // setup cam + _gfx->setGameViewport(); _gfx->setupPerspective(_perspectiveMatrix); _gfx->setupCamera(_cameraPosition, _lookAtMatrix); diff --git a/engines/stark/scene.h b/engines/stark/scene.h index 8ee67385872..e84d37747cb 100644 --- a/engines/stark/scene.h +++ b/engines/stark/scene.h @@ -57,7 +57,7 @@ public: void scrollCamera(const Common::Rect &viewport); private: - void computeSymmetricPerspectiveRect(float *xmin, float *xmax, float *ymin, float *ymax); + void computeClippingRect(float *xmin, float *xmax, float *ymin, float *ymax); GfxDriver *_gfx; @@ -69,9 +69,6 @@ private: float _nearClipPlane; float _farClipPlane; - float _scollXFactor; - float _scollYFactor; - Math::Matrix4 _perspectiveMatrix; Math::Matrix4 _lookAtMatrix;