diff --git a/engines/grim/gfx_opengl.cpp b/engines/grim/gfx_opengl.cpp index 5a09354c8fa..319c46c887e 100644 --- a/engines/grim/gfx_opengl.cpp +++ b/engines/grim/gfx_opengl.cpp @@ -35,6 +35,8 @@ #include "graphics/surface.h" #include "graphics/pixelbuffer.h" +#include "math/glmath.h" + #include "engines/grim/actor.h" #include "engines/grim/colormap.h" #include "engines/grim/font.h" @@ -249,7 +251,9 @@ void GfxOpenGL::positionCamera(const Math::Vector3d &pos, const Math::Vector3d & if (pos.x() == interest.x() && pos.y() == interest.y()) up_vec = Math::Vector3d(0, 1, 0); - gluLookAt(pos.x(), pos.y(), pos.z(), interest.x(), interest.y(), interest.z(), up_vec.x(), up_vec.y(), up_vec.z()); + Math::Matrix4 lookMatrix = Math::makeLookAtMatrix(pos, interest, up_vec); + glMultMatrixf(lookMatrix.getData()); + glTranslated(-pos.x(), -pos.y(), -pos.z()); } } @@ -364,10 +368,9 @@ void GfxOpenGL::getBoundingBoxPos(const Mesh *model, int *x1, int *y1, int *x2, GLdouble right = -1000; GLdouble left = 1000; GLdouble bottom = -1000; - GLdouble winX, winY, winZ; for (int i = 0; i < model->_numFaces; i++) { - Math::Vector3d v; + Math::Vector3d obj; float *pVertices; for (int j = 0; j < model->_faces[i].getNumVertices(); j++) { @@ -380,18 +383,19 @@ void GfxOpenGL::getBoundingBoxPos(const Mesh *model, int *x1, int *y1, int *x2, pVertices = model->_vertices + 3 * model->_faces[i].getVertex(j); - v.set(*(pVertices), *(pVertices + 1), *(pVertices + 2)); + obj.set(*(pVertices), *(pVertices + 1), *(pVertices + 2)); - gluProject(v.x(), v.y(), v.z(), modelView, projection, viewPort, &winX, &winY, &winZ); + Math::Vector3d win; + Math::gluMathProject(obj, modelView, projection, viewPort, win); - if (winX > right) - right = winX; - if (winX < left) - left = winX; - if (winY < top) - top = winY; - if (winY > bottom) - bottom = winY; + if (win.x() > right) + right = win.x(); + if (win.x() < left) + left = win.x(); + if (win.y() < top) + top = win.y(); + if (win.y() > bottom) + bottom = win.y(); } } @@ -435,7 +439,6 @@ void GfxOpenGL::getBoundingBoxPos(const EMIModel *model, int *x1, int *y1, int * GLdouble right = -1000; GLdouble left = 1000; GLdouble bottom = -1000; - GLdouble winX, winY, winZ; GLdouble modelView[16], projection[16]; GLint viewPort[4]; @@ -449,18 +452,18 @@ void GfxOpenGL::getBoundingBoxPos(const EMIModel *model, int *x1, int *y1, int * for (uint j = 0; j < model->_faces[i]._faceLength * 3; j++) { int index = indices[j]; - Math::Vector3d v = model->_drawVertices[index]; - - gluProject(v.x(), v.y(), v.z(), modelView, projection, viewPort, &winX, &winY, &winZ); - - if (winX > right) - right = winX; - if (winX < left) - left = winX; - if (winY < top) - top = winY; - if (winY > bottom) - bottom = winY; + Math::Vector3d obj = model->_drawVertices[index]; + Math::Vector3d win; + Math::gluMathProject(obj, modelView, projection, viewPort, win); + + if (win.x() > right) + right = win.x(); + if (win.x() < left) + left = win.x(); + if (win.y() < top) + top = win.y(); + if (win.y() > bottom) + bottom = win.y(); } } diff --git a/engines/grim/gfx_tinygl.cpp b/engines/grim/gfx_tinygl.cpp index 0816df9b0ea..6329b623312 100644 --- a/engines/grim/gfx_tinygl.cpp +++ b/engines/grim/gfx_tinygl.cpp @@ -26,6 +26,8 @@ #include "graphics/surface.h" #include "graphics/colormasks.h" +#include "math/glmath.h" + #include "engines/grim/actor.h" #include "engines/grim/colormap.h" #include "engines/grim/material.h" @@ -317,7 +319,9 @@ void GfxTinyGL::positionCamera(const Math::Vector3d &pos, const Math::Vector3d & if (pos.x() == interest.x() && pos.y() == interest.y()) up_vec = Math::Vector3d(0, 1, 0); - lookAt(pos.x(), pos.y(), pos.z(), interest.x(), interest.y(), interest.z(), up_vec.x(), up_vec.y(), up_vec.z()); + Math::Matrix4 lookMatrix = Math::makeLookAtMatrix(pos, interest, up_vec); + tglMultMatrixf(lookMatrix.getData()); + tglTranslatef(-pos.x(), -pos.y(), -pos.z()); } } @@ -480,10 +484,9 @@ void GfxTinyGL::getBoundingBoxPos(const Mesh *model, int *x1, int *y1, int *x2, TGLfloat right = -1000; TGLfloat left = 1000; TGLfloat bottom = -1000; - TGLfloat winX, winY, winZ; for (int i = 0; i < model->_numFaces; i++) { - Math::Vector3d v; + Math::Vector3d obj; float *pVertices; for (int j = 0; j < model->_faces[i].getNumVertices(); j++) { @@ -496,18 +499,19 @@ void GfxTinyGL::getBoundingBoxPos(const Mesh *model, int *x1, int *y1, int *x2, pVertices = model->_vertices + 3 * model->_faces[i].getVertex(j); - v.set(*(pVertices), *(pVertices + 1), *(pVertices + 2)); + obj.set(*(pVertices), *(pVertices + 1), *(pVertices + 2)); - tgluProject(v.x(), v.y(), v.z(), modelView, projection, viewPort, &winX, &winY, &winZ); + Math::Vector3d win; + Math::gluMathProject(obj, modelView, projection, viewPort, win); - if (winX > right) - right = winX; - if (winX < left) - left = winX; - if (winY < top) - top = winY; - if (winY > bottom) - bottom = winY; + if (win.x() > right) + right = win.x(); + if (win.x() < left) + left = win.x(); + if (win.y() < top) + top = win.y(); + if (win.y() > bottom) + bottom = win.y(); } } @@ -551,7 +555,6 @@ void GfxTinyGL::getBoundingBoxPos(const EMIModel *model, int *x1, int *y1, int * TGLfloat right = -1000; TGLfloat left = 1000; TGLfloat bottom = -1000; - TGLfloat winX, winY, winZ; TGLfloat modelView[16], projection[16]; TGLint viewPort[4]; @@ -565,18 +568,19 @@ void GfxTinyGL::getBoundingBoxPos(const EMIModel *model, int *x1, int *y1, int * for (uint j = 0; j < model->_faces[i]._faceLength * 3; j++) { int index = indices[j]; - Math::Vector3d v = model->_drawVertices[index]; - - tgluProject(v.x(), v.y(), v.z(), modelView, projection, viewPort, &winX, &winY, &winZ); - - if (winX > right) - right = winX; - if (winX < left) - left = winX; - if (winY < top) - top = winY; - if (winY > bottom) - bottom = winY; + + Math::Vector3d obj = model->_drawVertices[index]; + Math::Vector3d win; + Math::gluMathProject(obj, modelView, projection, viewPort, win); + + if (win.x() > right) + right = win.x(); + if (win.x() < left) + left = win.x(); + if (win.y() < top) + top = win.y(); + if (win.y() > bottom) + bottom = win.y(); } } diff --git a/engines/myst3/gfx_opengl.cpp b/engines/myst3/gfx_opengl.cpp index 93780ce053d..128f0f90272 100644 --- a/engines/myst3/gfx_opengl.cpp +++ b/engines/myst3/gfx_opengl.cpp @@ -35,6 +35,7 @@ #include "graphics/surface.h" #include "math/vector2d.h" +#include "math/glmath.h" #include "engines/myst3/gfx.h" #include "engines/myst3/gfx_opengl.h" @@ -134,7 +135,7 @@ void OpenGLRenderer::setupCameraOrtho2D() { glViewport(0, 0, kOriginalWidth, kOriginalHeight); glMatrixMode(GL_PROJECTION); glLoadIdentity(); - gluOrtho2D(0.0, kOriginalWidth, kOriginalHeight, 0.0); + glOrtho(0.0, kOriginalWidth, kOriginalHeight, 0.0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); @@ -151,7 +152,8 @@ void OpenGLRenderer::setupCameraPerspective(float pitch, float heading, float fo glViewport(0, kBottomBorderHeight, kOriginalWidth, kFrameHeight); glMatrixMode(GL_PROJECTION); glLoadIdentity(); - gluPerspective(glFOV, (GLfloat)kOriginalWidth / (GLfloat)kFrameHeight, 1.0, 10000.0); + Math::Matrix4 m = Math::makePerspectiveMatrix(glFOV, (GLfloat)kOriginalWidth / (GLfloat)kFrameHeight, 1.0, 10000.0); + glMultMatrixf(m.getData()); // Rotate the model to simulate the rotation of the camera glMatrixMode(GL_MODELVIEW); @@ -353,19 +355,18 @@ Graphics::Surface *OpenGLRenderer::getScreenshot() { } void OpenGLRenderer::screenPosToDirection(const Common::Point screen, float &pitch, float &heading) { - double x, y, z; - // Screen coords to 3D coords - gluUnProject(screen.x, kOriginalHeight - screen.y, 0.9, _cubeModelViewMatrix, _cubeProjectionMatrix, (GLint *)_cubeViewport, &x, &y, &z); + Math::Vector3d obj; + Math::gluMathUnProject(Math::Vector3d(screen.x, kOriginalHeight - screen.y, 0.9), + _cubeModelViewMatrix, _cubeProjectionMatrix, _cubeViewport, obj); // 3D coords to polar coords - Math::Vector3d v = Math::Vector3d(x, y, z); - v.normalize(); + obj.normalize(); - Math::Vector2d horizontalProjection = Math::Vector2d(v.x(), v.z()); + Math::Vector2d horizontalProjection = Math::Vector2d(obj.x(), obj.z()); horizontalProjection.normalize(); - pitch = 90 - Math::Angle::arcCosine(v.y()).getDegrees(); + pitch = 90 - Math::Angle::arcCosine(obj.y()).getDegrees(); heading = Math::Angle::arcCosine(horizontalProjection.getY()).getDegrees(); if (horizontalProjection.getX() > 0.0) diff --git a/engines/myst3/gfx_tinygl.cpp b/engines/myst3/gfx_tinygl.cpp index 7394d357200..7b8c5ab4ea2 100644 --- a/engines/myst3/gfx_tinygl.cpp +++ b/engines/myst3/gfx_tinygl.cpp @@ -33,6 +33,7 @@ #include "graphics/surface.h" #include "math/vector2d.h" +#include "math/glmath.h" #include "engines/myst3/gfx.h" #include "engines/myst3/gfx_tinygl.h" @@ -131,7 +132,7 @@ void TinyGLRenderer::setupCameraOrtho2D() { tglViewport(0, 0, kOriginalWidth, kOriginalHeight); tglMatrixMode(TGL_PROJECTION); tglLoadIdentity(); - tgluOrtho2D(0.0, kOriginalWidth, kOriginalHeight, 0.0); + tglOrtho(0.0, kOriginalWidth, kOriginalHeight, 0.0, -1.0, 1.0); tglMatrixMode(TGL_MODELVIEW); tglLoadIdentity(); @@ -149,7 +150,8 @@ void TinyGLRenderer::setupCameraPerspective(float pitch, float heading, float fo tglViewport(0, kTopBorderHeight, kOriginalWidth, kFrameHeight); tglMatrixMode(TGL_PROJECTION); tglLoadIdentity(); - tgluPerspective(glFOV, (TGLfloat)kOriginalWidth / (TGLfloat)kFrameHeight, 1.0, 10000.0); + Math::Matrix4 m = Math::makePerspectiveMatrix(glFOV, (TGLfloat)kOriginalWidth / (TGLfloat)kFrameHeight, 1.0, 10000.0); + tglMultMatrixf(m.getData()); // Rotate the model to simulate the rotation of the camera tglMatrixMode(TGL_MODELVIEW); @@ -157,14 +159,8 @@ void TinyGLRenderer::setupCameraPerspective(float pitch, float heading, float fo tglRotatef(pitch, -1.0f, 0.0f, 0.0f); tglRotatef(heading - 180.0f, 0.0f, 1.0f, 0.0f); - float modelView[16], projection[16]; - tglGetFloatv(TGL_MODELVIEW_MATRIX, modelView); - tglGetFloatv(TGL_PROJECTION_MATRIX, projection); - for (int i = 0; i < 16; i++) { - _cubeModelViewMatrix[i] = modelView[i]; - _cubeProjectionMatrix[i] = projection[i]; - } - + tglGetFloatv(TGL_MODELVIEW_MATRIX, _cubeModelViewMatrix); + tglGetFloatv(TGL_PROJECTION_MATRIX, _cubeProjectionMatrix); tglGetIntegerv(TGL_VIEWPORT, (TGLint *)_cubeViewport); } @@ -328,19 +324,18 @@ Graphics::Surface *TinyGLRenderer::getScreenshot() { } void TinyGLRenderer::screenPosToDirection(const Common::Point screen, float &pitch, float &heading) { - double x, y, z; - // Screen coords to 3D coords - tgluUnProject(screen.x, kOriginalHeight - screen.y, 0.9, _cubeModelViewMatrix, _cubeProjectionMatrix, (TGLint *)_cubeViewport, &x, &y, &z); + Math::Vector3d obj; + Math::gluMathUnProject(Math::Vector3d(screen.x, kOriginalHeight - screen.y, 0.9), + _cubeModelViewMatrix, _cubeProjectionMatrix, _cubeViewport, obj); // 3D coords to polar coords - Math::Vector3d v = Math::Vector3d(x, y, z); - v.normalize(); + obj.normalize(); - Math::Vector2d horizontalProjection = Math::Vector2d(v.x(), v.z()); + Math::Vector2d horizontalProjection = Math::Vector2d(obj.x(), obj.z()); horizontalProjection.normalize(); - pitch = 90 - Math::Angle::arcCosine(v.y()).getDegrees(); + pitch = 90 - Math::Angle::arcCosine(obj.y()).getDegrees(); heading = Math::Angle::arcCosine(horizontalProjection.getY()).getDegrees(); if (horizontalProjection.getX() > 0.0) diff --git a/engines/myst3/gfx_tinygl.h b/engines/myst3/gfx_tinygl.h index 7ff0f455bf4..af058cf8f40 100644 --- a/engines/myst3/gfx_tinygl.h +++ b/engines/myst3/gfx_tinygl.h @@ -66,8 +66,8 @@ private: TinyGL::FrameBuffer *_fb; int _cubeViewport[4]; - double _cubeProjectionMatrix[16]; - double _cubeModelViewMatrix[16]; + float _cubeProjectionMatrix[16]; + float _cubeModelViewMatrix[16]; bool _nonPowerOfTwoTexSupport; };