STARK: Correct the projection matrix to match the original
This commit is contained in:
parent
9a7b846301
commit
32c487f143
5 changed files with 33 additions and 34 deletions
|
@ -41,6 +41,8 @@ public:
|
||||||
|
|
||||||
virtual void setupScreen(int screenW, int screenH, bool fullscreen) = 0;
|
virtual void setupScreen(int screenW, int screenH, bool fullscreen) = 0;
|
||||||
|
|
||||||
|
virtual void setGameViewport() = 0;
|
||||||
|
|
||||||
virtual void setupPerspective(const Math::Matrix4 &projectionMatrix) = 0;
|
virtual void setupPerspective(const Math::Matrix4 &projectionMatrix) = 0;
|
||||||
virtual void setupCamera(const Math::Vector3d &position, const Math::Matrix4 &lookAt) = 0;
|
virtual void setupCamera(const Math::Vector3d &position, const Math::Matrix4 &lookAt) = 0;
|
||||||
|
|
||||||
|
@ -51,6 +53,9 @@ public:
|
||||||
|
|
||||||
virtual void set3DMode() = 0;
|
virtual void set3DMode() = 0;
|
||||||
|
|
||||||
|
static const int32 kTopBorderHeight = 36;
|
||||||
|
static const int32 kGameViewportHeight = 365;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
int _screenWidth;
|
int _screenWidth;
|
||||||
int _screenHeight;
|
int _screenHeight;
|
||||||
|
|
|
@ -55,6 +55,10 @@ void OpenGLGfxDriver::setupScreen(int screenW, int screenH, bool fullscreen) {
|
||||||
_screenHeight = screenH;
|
_screenHeight = screenH;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void OpenGLGfxDriver::setGameViewport() {
|
||||||
|
glViewport(0, _screenHeight - kGameViewportHeight - kTopBorderHeight, _screenWidth, kGameViewportHeight);
|
||||||
|
}
|
||||||
|
|
||||||
void OpenGLGfxDriver::setupPerspective(const Math::Matrix4 &projectionMatrix) {
|
void OpenGLGfxDriver::setupPerspective(const Math::Matrix4 &projectionMatrix) {
|
||||||
glMatrixMode(GL_PROJECTION);
|
glMatrixMode(GL_PROJECTION);
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
|
@ -81,8 +85,6 @@ void OpenGLGfxDriver::drawSurface(const Graphics::Surface *surface, Common::Poin
|
||||||
if (rect.isEmpty())
|
if (rect.isEmpty())
|
||||||
rect = Common::Rect(surface->w, surface->h);
|
rect = Common::Rect(surface->w, surface->h);
|
||||||
|
|
||||||
dest += Common::Point(0, 36); // Top border
|
|
||||||
|
|
||||||
start2DMode();
|
start2DMode();
|
||||||
|
|
||||||
glDisable(GL_TEXTURE_2D);
|
glDisable(GL_TEXTURE_2D);
|
||||||
|
|
|
@ -41,6 +41,8 @@ public:
|
||||||
|
|
||||||
void setupScreen(int screenW, int screenH, bool fullscreen);
|
void setupScreen(int screenW, int screenH, bool fullscreen);
|
||||||
|
|
||||||
|
void setGameViewport() override;
|
||||||
|
|
||||||
void setupPerspective(const Math::Matrix4 &projectionMatrix) override;
|
void setupPerspective(const Math::Matrix4 &projectionMatrix) override;
|
||||||
void setupCamera(const Math::Vector3d &position, const Math::Matrix4 &lookAt) override;
|
void setupCamera(const Math::Vector3d &position, const Math::Matrix4 &lookAt) override;
|
||||||
|
|
||||||
|
|
|
@ -33,9 +33,7 @@ Scene::Scene(GfxDriver *gfx) :
|
||||||
_gfx(gfx),
|
_gfx(gfx),
|
||||||
_fov(45.0),
|
_fov(45.0),
|
||||||
_nearClipPlane(100.0),
|
_nearClipPlane(100.0),
|
||||||
_farClipPlane(64000.0),
|
_farClipPlane(64000.0) {
|
||||||
_scollXFactor(0.0),
|
|
||||||
_scollYFactor(0.0) {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Scene::~Scene() {
|
Scene::~Scene() {
|
||||||
|
@ -50,44 +48,38 @@ void Scene::initCamera(const Math::Vector3d &position, const Math::Vector3d &loo
|
||||||
_nearClipPlane = nearClipPlane;
|
_nearClipPlane = nearClipPlane;
|
||||||
_farClipPlane = farClipPlane;
|
_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));
|
_lookAtMatrix = Math::makeLookAtMatrix(_cameraPosition, _cameraPosition + _cameraLookDirection, Math::Vector3d(0.0, 0.0, 1.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Scene::scrollCamera(const Common::Rect &viewport) {
|
void Scene::scrollCamera(const Common::Rect &viewport) {
|
||||||
_viewport = 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;
|
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;
|
// The amounts by which translate to clipping planes to account for one pixel
|
||||||
int32 distanceToCenterY = _viewport.top + _viewport.height() / 2 - _viewSize.height() / 2;
|
// of camera scrolling movement
|
||||||
|
float scollXFactor = (xmax - xmin) / _viewport.width();
|
||||||
|
float scollYFactor = (ymax - ymin) / _viewport.height();
|
||||||
|
|
||||||
xmin += distanceToCenterX * _scollXFactor;
|
int32 distanceToRight = _viewport.right - _viewSize.width();
|
||||||
xmax += distanceToCenterX * _scollXFactor;
|
int32 distanceToBottom = _viewport.bottom - _viewSize.height();
|
||||||
ymin += distanceToCenterY * _scollYFactor;
|
|
||||||
ymax += distanceToCenterY * _scollYFactor;
|
xmin += distanceToRight * scollXFactor;
|
||||||
|
xmax += distanceToRight * scollXFactor;
|
||||||
|
ymin += distanceToBottom * scollYFactor;
|
||||||
|
ymax += distanceToBottom * scollYFactor;
|
||||||
|
|
||||||
_perspectiveMatrix = Math::makeFrustumMatrix(xmin, xmax, ymin, ymax, _nearClipPlane, _farClipPlane);
|
_perspectiveMatrix = Math::makeFrustumMatrix(xmin, xmax, ymin, ymax, _nearClipPlane, _farClipPlane);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Scene::computeSymmetricPerspectiveRect(float *xmin, float *xmax, float *ymin, float *ymax) {
|
void Scene::computeClippingRect(float *xmin, float *xmax, float *ymin, float *ymax) {
|
||||||
float aspectRatio = 1.0;
|
float aspectRatio = _viewSize.width() / (float) _viewSize.height();
|
||||||
float ymaxValue = _nearClipPlane * tan(_fov * M_PI / 360.0);
|
float xmaxValue = _nearClipPlane * tan(_fov * M_PI / 360.0);
|
||||||
float yminValue = -ymaxValue;
|
float ymaxValue = xmaxValue / aspectRatio;
|
||||||
float xminValue = yminValue / aspectRatio;
|
|
||||||
float xmaxValue = ymaxValue / 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 (xmin) *xmin = xminValue;
|
||||||
if (xmax) *xmax = xmaxValue;
|
if (xmax) *xmax = xmaxValue;
|
||||||
|
@ -97,6 +89,7 @@ void Scene::computeSymmetricPerspectiveRect(float *xmin, float *xmax, float *ymi
|
||||||
|
|
||||||
void Scene::render(RenderEntryArray renderEntries) {
|
void Scene::render(RenderEntryArray renderEntries) {
|
||||||
// setup cam
|
// setup cam
|
||||||
|
_gfx->setGameViewport();
|
||||||
_gfx->setupPerspective(_perspectiveMatrix);
|
_gfx->setupPerspective(_perspectiveMatrix);
|
||||||
_gfx->setupCamera(_cameraPosition, _lookAtMatrix);
|
_gfx->setupCamera(_cameraPosition, _lookAtMatrix);
|
||||||
|
|
||||||
|
|
|
@ -57,7 +57,7 @@ public:
|
||||||
void scrollCamera(const Common::Rect &viewport);
|
void scrollCamera(const Common::Rect &viewport);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void computeSymmetricPerspectiveRect(float *xmin, float *xmax, float *ymin, float *ymax);
|
void computeClippingRect(float *xmin, float *xmax, float *ymin, float *ymax);
|
||||||
|
|
||||||
GfxDriver *_gfx;
|
GfxDriver *_gfx;
|
||||||
|
|
||||||
|
@ -69,9 +69,6 @@ private:
|
||||||
float _nearClipPlane;
|
float _nearClipPlane;
|
||||||
float _farClipPlane;
|
float _farClipPlane;
|
||||||
|
|
||||||
float _scollXFactor;
|
|
||||||
float _scollYFactor;
|
|
||||||
|
|
||||||
Math::Matrix4 _perspectiveMatrix;
|
Math::Matrix4 _perspectiveMatrix;
|
||||||
Math::Matrix4 _lookAtMatrix;
|
Math::Matrix4 _lookAtMatrix;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue