[Scummvm-git-logs] scummvm master -> 87cb0325eb0a87095d2590b1eef1a62ea3bca991
aquadran
noreply at scummvm.org
Sun Nov 22 06:38:14 UTC 2026
This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://api.github.com/repos/scummvm/scummvm .
Summary:
87cb0325eb GRIM: Move screen bounding box calculation out of graphics
Commit: 87cb0325eb0a87095d2590b1eef1a62ea3bca991
https://github.com/scummvm/scummvm/commit/87cb0325eb0a87095d2590b1eef1a62ea3bca991
Author: Joel Teichroeb (joel at teichroeb.net)
Date: 2026-11-22T07:38:08+01:00
Commit Message:
GRIM: Move screen bounding box calculation out of graphics
The calculations were done on CPU regardless, move them into the Actor
and model classes
This fixes speech placement on grim with shaders
Fixes #11898
Changed paths:
engines/grim/actor.cpp
engines/grim/actor.h
engines/grim/costume.cpp
engines/grim/costume.h
engines/grim/costume/model_component.cpp
engines/grim/costume/model_component.h
engines/grim/emi/costume/emimesh_component.cpp
engines/grim/emi/costume/emimesh_component.h
engines/grim/emi/modelemi.cpp
engines/grim/emi/modelemi.h
engines/grim/gfx_base.h
engines/grim/gfx_opengl.cpp
engines/grim/gfx_opengl.h
engines/grim/gfx_opengl_shaders.cpp
engines/grim/gfx_opengl_shaders.h
engines/grim/gfx_tinygl.cpp
engines/grim/gfx_tinygl.h
engines/grim/model.cpp
engines/grim/model.h
diff --git a/engines/grim/actor.cpp b/engines/grim/actor.cpp
index 7656e2541f9..2e7b94f8f97 100644
--- a/engines/grim/actor.cpp
+++ b/engines/grim/actor.cpp
@@ -1779,6 +1779,37 @@ bool Actor::updateTalk() {
return false;
}
+ScreenBounds Actor::getScreenBounds() const {
+ Math::Matrix4 viewProjMatrix = g_driver->getProjection() * g_driver->getModelView();
+
+ ScreenBounds bounds;
+ if (g_grim->getGameType() == GType_GRIM) {
+ if (!_costumeStack.empty())
+ _costumeStack.back()->getBoundingBox(viewProjMatrix * getGrimModelMatrix(), bounds);
+ } else {
+ Math::Vector3d bboxPos, bboxSize;
+ getBBoxInfo(bboxPos, bboxSize);
+
+ Math::Matrix4 m = getFinalMatrix();
+ bboxPos = bboxPos + getWorldPos();
+
+ for (int x = 0; x < 2; x++) {
+ for (int y = 0; y < 2; y++) {
+ for (int z = 0; z < 2; z++) {
+ Math::Vector3d added(bboxSize.x() * 0.5f * (x * 2 - 1), bboxSize.y() * 0.5f * (y * 2 - 1), bboxSize.z() * 0.5f * (z * 2 - 1));
+ m.transform(&added, false);
+ bounds.addPoint(viewProjMatrix, bboxPos + added);
+ }
+ }
+ }
+
+ if (!bounds.overlapsScreen())
+ return {};
+ }
+
+ return bounds;
+}
+
void Actor::draw() {
// FIXME: if isAttached(), factor in the joint rotation as well.
const Math::Vector3d &absPos = getWorldPos();
@@ -1796,31 +1827,16 @@ void Actor::draw() {
}
if (_mustPlaceText) {
- Common::Point p1, p2;
- if (g_grim->getGameType() == GType_GRIM) {
- if (!_costumeStack.empty()) {
- int x1 = 1000, y1 = 1000, x2 = -1000, y2 = -1000;
- g_driver->startActorDraw(this);
- _costumeStack.back()->getBoundingBox(&x1, &y1, &x2, &y2);
- g_driver->finishActorDraw();
- p1.x = x1;
- p1.y = y1;
- p2.x = x2;
- p2.y = y2;
- }
- } else {
- g_driver->getActorScreenBBox(this, p1, p2);
- }
-
TextObject *textObject = TextObject::getPool().getObject(_sayLineText);
if (textObject) {
- if (p1.x == 1000 || p2.x == -1000 || p2.x == -1000) {
+ const ScreenBounds bounds = getScreenBounds();
+ if (bounds.isEmpty()) {
const Common::Point &pos = g_grim->getOffscreenTextPos();
textObject->setX(pos.x);
textObject->setY(pos.y);
} else {
- textObject->setX((p1.x + p2.x) / 2);
- textObject->setY(p1.y);
+ textObject->setX((bounds.getLeft() + bounds.getRight()) / 2);
+ textObject->setY(bounds.getTop());
}
// Deletes the original text and rebuilds it with the newly placed text
textObject->reset();
@@ -2370,6 +2386,15 @@ const Math::Matrix4 Actor::getFinalMatrix() const {
return m;
}
+Math::Matrix4 Actor::getGrimModelMatrix() const {
+ Math::Matrix4 m = getRotationQuat().toMatrix();
+ m *= getScale();
+ m.setValue(3, 3, 1.0f);
+ m.transpose();
+ m.setPosition(getWorldPos());
+ return m;
+}
+
Math::Vector3d Actor::getWorldPos() const {
if (! isAttached())
return getPos();
diff --git a/engines/grim/actor.h b/engines/grim/actor.h
index 609724bfca8..a6d0715b36f 100644
--- a/engines/grim/actor.h
+++ b/engines/grim/actor.h
@@ -41,6 +41,7 @@ class Set;
class Material;
struct SetShadow;
struct Joint;
+struct ScreenBounds;
class EMIModel;
struct Plane {
@@ -541,6 +542,7 @@ public:
void detach();
Math::Quaternion getRotationQuat() const;
const Math::Matrix4 getFinalMatrix() const;
+ Math::Matrix4 getGrimModelMatrix() const;
Math::Vector3d getHeadPos() const;
void setInOverworld(bool inOverworld) { _inOverworld = inOverworld; }
@@ -573,6 +575,7 @@ public:
private:
void costumeMarkerCallback(int marker);
+ ScreenBounds getScreenBounds() const;
void collisionHandlerCallback(Actor *other) const;
void updateWalk();
void addShadowPlane(const char *n, Set *scene, int shadowId);
diff --git a/engines/grim/costume.cpp b/engines/grim/costume.cpp
index c62d447e7a3..78d513f38da 100644
--- a/engines/grim/costume.cpp
+++ b/engines/grim/costume.cpp
@@ -32,7 +32,6 @@
#include "engines/grim/costume/chore.h"
#include "engines/grim/costume/head.h"
#include "engines/grim/emi/costume/emianim_component.h"
-#include "engines/grim/emi/costume/emimesh_component.h"
#include "engines/grim/emi/costume/emiskel_component.h"
#include "engines/grim/costume/main_model_component.h"
#include "engines/grim/costume/colormap_component.h"
@@ -422,17 +421,12 @@ void Costume::draw() {
_components[i]->draw();
}
-void Costume::getBoundingBox(int *x1, int *y1, int *x2, int *y2) {
+void Costume::getBoundingBox(const Math::Matrix4 &matrix, ScreenBounds &bounds) {
for (int i = 0; i < _numComponents; i++) {
if (_components[i] &&(_components[i]->isComponentType('M','M','D','L') ||
_components[i]->isComponentType('M','O','D','L'))) {
ModelComponent *c = static_cast<ModelComponent *>(_components[i]);
- c->getBoundingBox(x1, y1, x2, y2);
- }
-
- if (_components[i] &&(_components[i]->isComponentType('m','e','s','h'))) {
- EMIMeshComponent *c = static_cast<EMIMeshComponent *>(_components[i]);
- c->getBoundingBox(x1, y1, x2, y2);
+ c->getBoundingBox(matrix, bounds);
}
}
}
diff --git a/engines/grim/costume.h b/engines/grim/costume.h
index 1420308a89e..f53b9f61e15 100644
--- a/engines/grim/costume.h
+++ b/engines/grim/costume.h
@@ -33,6 +33,7 @@ namespace Grim {
typedef uint32 tag32;
class CMap;
+struct ScreenBounds;
class Model;
class ModelNode;
class TextSplitter;
@@ -83,7 +84,7 @@ public:
virtual int update(uint frameTime);
void animate();
virtual void draw();
- void getBoundingBox(int *x1, int *y1, int *x2, int *y2);
+ void getBoundingBox(const Math::Matrix4 &matrix, ScreenBounds &bounds);
void setPosRotate(const Math::Vector3d &pos, const Math::Angle &pitch,
const Math::Angle &yaw, const Math::Angle &roll);
Math::Matrix4 getMatrix() const;
diff --git a/engines/grim/costume/model_component.cpp b/engines/grim/costume/model_component.cpp
index 1b1ffe4f067..f716db764d0 100644
--- a/engines/grim/costume/model_component.cpp
+++ b/engines/grim/costume/model_component.cpp
@@ -191,20 +191,17 @@ void ModelComponent::draw() {
translateObject(true);
}
-void ModelComponent::getBoundingBox(int *x1, int *y1, int *x2, int *y2) {
- // If the object was drawn by being a component
- // of it's parent then don't draw it
+static Math::Matrix4 getNodeChainMatrix(const ModelNode *node) {
+ if (!node)
+ return {};
+ return getNodeChainMatrix(node->_parent) * node->getAnimMatrix();
+}
+void ModelComponent::getBoundingBox(const Math::Matrix4 &matrix, ScreenBounds &bounds) {
if (_parent && _parent->isVisible())
return;
- // Need to translate object to be in accordance
- // with the setup of the parent
- translateObject(false);
-
- _hier->getBoundingBox(x1, y1, x2, y2);
- // Need to un-translate when done
- translateObject(true);
+ _hier->getBoundingBox(matrix * getNodeChainMatrix(_hier->_parent), bounds);
}
} // end of namespace Grim
diff --git a/engines/grim/costume/model_component.h b/engines/grim/costume/model_component.h
index 73949482c72..e35da40c040 100644
--- a/engines/grim/costume/model_component.h
+++ b/engines/grim/costume/model_component.h
@@ -28,6 +28,7 @@ namespace Grim {
class Model;
class AnimManager;
+struct ScreenBounds;
class ModelComponent : public Component {
public:
@@ -49,7 +50,7 @@ public:
int getNumNodes();
Model *getModel() { return _obj; }
void draw() override;
- void getBoundingBox(int *x1, int *y1, int *x2, int *y2);
+ void getBoundingBox(const Math::Matrix4 &matrix, ScreenBounds &bounds);
protected:
Model *_obj;
diff --git a/engines/grim/emi/costume/emimesh_component.cpp b/engines/grim/emi/costume/emimesh_component.cpp
index 61d8a5bae34..5d29d81bdc1 100644
--- a/engines/grim/emi/costume/emimesh_component.cpp
+++ b/engines/grim/emi/costume/emimesh_component.cpp
@@ -85,15 +85,4 @@ void EMIMeshComponent::draw() {
//translateObject(true);
}
-void EMIMeshComponent::getBoundingBox(int *x1, int *y1, int *x2, int *y2) const {
- // If the object was drawn by being a component
- // of it's parent then don't draw it
-
- if (_parent && _parent->isVisible())
- return;
-
- if (_obj)
- _obj->getBoundingBox(x1, y1, x2, y2);
-}
-
} // end of namespace Grim
diff --git a/engines/grim/emi/costume/emimesh_component.h b/engines/grim/emi/costume/emimesh_component.h
index 8b6565d5c7f..84c4e70297f 100644
--- a/engines/grim/emi/costume/emimesh_component.h
+++ b/engines/grim/emi/costume/emimesh_component.h
@@ -37,7 +37,6 @@ public:
int update(uint time) override;
void reset() override;
void draw() override;
- void getBoundingBox(int *x1, int *y1, int *x2, int *y2) const;
public:
EMICostume *_costume;
diff --git a/engines/grim/emi/modelemi.cpp b/engines/grim/emi/modelemi.cpp
index 6c428d0230e..f1ee4c59a77 100644
--- a/engines/grim/emi/modelemi.cpp
+++ b/engines/grim/emi/modelemi.cpp
@@ -403,17 +403,6 @@ void EMIModel::updateLighting(const Math::Matrix4 &modelToWorld) {
}
}
-void EMIModel::getBoundingBox(int *x1, int *y1, int *x2, int *y2) const {
- int winX1, winY1, winX2, winY2;
- g_driver->getScreenBoundingBox(this, &winX1, &winY1, &winX2, &winY2);
- if (winX1 != -1 && winY1 != -1 && winX2 != -1 && winY2 != -1) {
- *x1 = MIN(*x1, winX1);
- *y1 = MIN(*y1, winY1);
- *x2 = MAX(*x2, winX2);
- *y2 = MAX(*y2, winY2);
- }
-}
-
Math::AABB EMIModel::calculateWorldBounds(const Math::Matrix4 &matrix) const {
Math::AABB bounds;
for (int i = 0; i < _numVertices; i++) {
diff --git a/engines/grim/emi/modelemi.h b/engines/grim/emi/modelemi.h
index 3c76581e34e..69af4a92ce2 100644
--- a/engines/grim/emi/modelemi.h
+++ b/engines/grim/emi/modelemi.h
@@ -139,7 +139,6 @@ public:
void prepareTextures();
void draw();
void updateLighting(const Math::Matrix4 &modelToWorld);
- void getBoundingBox(int *x1, int *y1, int *x2, int *y2) const;
Math::AABB calculateWorldBounds(const Math::Matrix4 &matrix) const;
};
diff --git a/engines/grim/gfx_base.h b/engines/grim/gfx_base.h
index a9173febc99..9f3a0f213c7 100644
--- a/engines/grim/gfx_base.h
+++ b/engines/grim/gfx_base.h
@@ -105,13 +105,6 @@ public:
*/
virtual void flipBuffer(bool opportunistic = false) = 0;
- /**
- * FIXME: The implementations of these functions (for Grim and EMI, respectively)
- * are very similar. Needs refactoring. See issue #789.
- */
- virtual void getScreenBoundingBox(const Mesh *mesh, int *x1, int *y1, int *x2, int *y2) = 0;
- virtual void getScreenBoundingBox(const EMIModel *mesh, int *x1, int *y1, int *x2, int *y2) = 0;
- virtual void getActorScreenBBox(const Actor *actor, Common::Point &p1, Common::Point &p2) = 0;
virtual void startActorDraw(const Actor *act) = 0;
virtual void finishActorDraw() = 0;
virtual void setShadow(Shadow *shadow) = 0;
diff --git a/engines/grim/gfx_opengl.cpp b/engines/grim/gfx_opengl.cpp
index 9ba6ef8010c..2f29972875d 100644
--- a/engines/grim/gfx_opengl.cpp
+++ b/engines/grim/gfx_opengl.cpp
@@ -302,208 +302,6 @@ static void shadowProjection(const Math::Vector3d &light, const Math::Vector3d &
glMultMatrixf((GLfloat *)mat);
}
-void GfxOpenGL::getScreenBoundingBox(const Mesh *model, int *x1, int *y1, int *x2, int *y2) {
- if (_currentShadowArray) {
- *x1 = -1;
- *y1 = -1;
- *x2 = -1;
- *y2 = -1;
- return;
- }
-
- GLdouble top = 1000;
- GLdouble right = -1000;
- GLdouble left = 1000;
- GLdouble bottom = -1000;
-
- for (int i = 0; i < model->_numFaces; i++) {
- Math::Vector3d obj;
- float *pVertices;
-
- for (int j = 0; j < model->_faces[i].getNumVertices(); j++) {
- GLdouble modelView[16], projection[16];
- GLint viewPort[4];
-
- glGetDoublev(GL_MODELVIEW_MATRIX, modelView);
- glGetDoublev(GL_PROJECTION_MATRIX, projection);
- glGetIntegerv(GL_VIEWPORT, viewPort);
-
- pVertices = model->_vertices + 3 * model->_faces[i].getVertex(j);
-
- obj.set(*(pVertices), *(pVertices + 1), *(pVertices + 2));
-
- Math::Vector3d win;
- Math::gluMathProject<GLdouble, GLint>(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();
- }
- }
-
- double t = bottom;
- bottom = _gameHeight - top;
- top = _gameHeight - t;
-
- if (left < 0)
- left = 0;
- if (right >= _gameWidth)
- right = _gameWidth - 1;
- if (top < 0)
- top = 0;
- if (bottom >= _gameHeight)
- bottom = _gameHeight - 1;
-
- if (top >= _gameHeight || left >= _gameWidth || bottom < 0 || right < 0) {
- *x1 = -1;
- *y1 = -1;
- *x2 = -1;
- *y2 = -1;
- return;
- }
-
- *x1 = (int)left;
- *y1 = (int)top;
- *x2 = (int)right;
- *y2 = (int)bottom;
-}
-
-void GfxOpenGL::getScreenBoundingBox(const EMIModel *model, int *x1, int *y1, int *x2, int *y2) {
- if (_currentShadowArray) {
- *x1 = -1;
- *y1 = -1;
- *x2 = -1;
- *y2 = -1;
- return;
- }
-
- GLdouble top = 1000;
- GLdouble right = -1000;
- GLdouble left = 1000;
- GLdouble bottom = -1000;
-
- GLdouble modelView[16], projection[16];
- GLint viewPort[4];
-
- glGetDoublev(GL_MODELVIEW_MATRIX, modelView);
- glGetDoublev(GL_PROJECTION_MATRIX, projection);
- glGetIntegerv(GL_VIEWPORT, viewPort);
-
- for (uint i = 0; i < model->_numFaces; i++) {
- uint16 *indices = (uint16 *)model->_faces[i]._indexes;
-
- for (uint j = 0; j < model->_faces[i]._faceLength * 3; j++) {
- uint16 index = indices[j];
- Math::Vector3d obj = model->_drawVertices[index];
- Math::Vector3d win;
- Math::gluMathProject<GLdouble, GLint>(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();
- }
- }
-
- double t = bottom;
- bottom = _gameHeight - top;
- top = _gameHeight - t;
-
- if (left < 0)
- left = 0;
- if (right >= _gameWidth)
- right = _gameWidth - 1;
- if (top < 0)
- top = 0;
- if (bottom >= _gameHeight)
- bottom = _gameHeight - 1;
-
- if (top >= _gameHeight || left >= _gameWidth || bottom < 0 || right < 0) {
- *x1 = -1;
- *y1 = -1;
- *x2 = -1;
- *y2 = -1;
- return;
- }
-
- *x1 = (int)left;
- *y1 = (int)top;
- *x2 = (int)right;
- *y2 = (int)bottom;
-}
-
-void GfxOpenGL::getActorScreenBBox(const Actor *actor, Common::Point &p1, Common::Point &p2) {
- // Get the actor's bounding box information (describes a 3D box)
- Math::Vector3d bboxPos, bboxSize;
- actor->getBBoxInfo(bboxPos, bboxSize);
-
- // Translate the bounding box to the actor's position
- Math::Matrix4 m = actor->getFinalMatrix();
- bboxPos = bboxPos + actor->getWorldPos();
-
- // Set up the camera coordinate system
- glMatrixMode(GL_MODELVIEW);
- glPushMatrix();
-
- // Apply the view transform.
- Math::Matrix4 worldRot = _currentRot;
- glMultMatrixf(worldRot.getData());
- glTranslatef(-_currentPos.x(), -_currentPos.y(), -_currentPos.z());
-
- // Get the current OpenGL state
- GLdouble modelView[16], projection[16];
- GLint viewPort[4];
- glGetDoublev(GL_MODELVIEW_MATRIX, modelView);
- glGetDoublev(GL_PROJECTION_MATRIX, projection);
- glGetIntegerv(GL_VIEWPORT, viewPort);
-
- // Set values outside of the screen range
- p1.x = 1000;
- p1.y = 1000;
- p2.x = -1000;
- p2.y = -1000;
-
- // Project all of the points in the 3D bounding box
- Math::Vector3d p, projected;
- for (int x = 0; x < 2; x++) {
- for (int y = 0; y < 2; y++) {
- for (int z = 0; z < 2; z++) {
- Math::Vector3d added(bboxSize.x() * 0.5f * (x * 2 - 1), bboxSize.y() * 0.5f * (y * 2 - 1), bboxSize.z() * 0.5f * (z * 2 - 1));
- m.transform(&added, false);
- p = bboxPos + added;
- Math::gluMathProject<GLdouble, GLint>(p, modelView, projection, viewPort, projected);
-
- // Find the points
- if (projected.x() < p1.x)
- p1.x = projected.x();
- if (projected.y() < p1.y)
- p1.y = projected.y();
- if (projected.x() > p2.x)
- p2.x = projected.x();
- if (projected.y() > p2.y)
- p2.y = projected.y();
- }
- }
- }
-
- // Swap the p1/p2 y coorindates
- int16 tmp = p1.y;
- p1.y = 480 - p2.y;
- p2.y = 480 - tmp;
-
- // Restore the state
- glPopMatrix();
-}
-
void GfxOpenGL::startActorDraw(const Actor *actor) {
_currentActor = actor;
glEnable(GL_TEXTURE_2D);
diff --git a/engines/grim/gfx_opengl.h b/engines/grim/gfx_opengl.h
index 35b0dc01fa7..e3ff4778b08 100644
--- a/engines/grim/gfx_opengl.h
+++ b/engines/grim/gfx_opengl.h
@@ -57,10 +57,6 @@ public:
bool isHardwareAccelerated() override;
bool supportsShaders() override;
- void getScreenBoundingBox(const Mesh *model, int *x1, int *y1, int *x2, int *y2) override;
- void getScreenBoundingBox(const EMIModel *model, int *x1, int *y1, int *x2, int *y2) override;
- void getActorScreenBBox(const Actor *actor, Common::Point &p1, Common::Point &p2) override;
-
void startActorDraw(const Actor *actor) override;
void finishActorDraw() override;
void setShadow(Shadow *shadow) override;
diff --git a/engines/grim/gfx_opengl_shaders.cpp b/engines/grim/gfx_opengl_shaders.cpp
index 7aa6e9e7f0a..af6ca784280 100644
--- a/engines/grim/gfx_opengl_shaders.cpp
+++ b/engines/grim/gfx_opengl_shaders.cpp
@@ -455,9 +455,6 @@ void GfxOpenGLS::positionCamera(const Math::Vector3d &pos, const Math::Vector3d
}
void GfxOpenGLS::positionCamera(const Math::Vector3d &pos, const Math::Matrix4 &rot) {
- Math::Matrix4 projMatrix = _projMatrix;
- projMatrix.transpose();
-
_currentPos = pos;
_currentRot = rot;
@@ -473,7 +470,6 @@ void GfxOpenGLS::positionCamera(const Math::Vector3d &pos, const Math::Matrix4 &
camPos(2, 3) = -_currentPos.z();
_viewMatrix = invertZ * viewMatrix * camPos;
- _mvpMatrix = projMatrix * _viewMatrix;
_viewMatrix.transpose();
}
@@ -494,7 +490,9 @@ Math::Matrix4 GfxOpenGLS::getModelView() {
Math::Matrix4 modelView = invertZ * viewMatrix * camPos;
return modelView;
} else {
- return _mvpMatrix;
+ Math::Matrix4 modelView = _viewMatrix;
+ modelView.transpose();
+ return modelView;
}
}
@@ -527,215 +525,10 @@ void GfxOpenGLS::flipBuffer(bool opportunistic) {
g_system->updateScreen();
}
-void GfxOpenGLS::getScreenBoundingBox(const Mesh *model, int *x1, int *y1, int *x2, int *y2) {
- if (_currentShadowArray) {
- *x1 = -1;
- *y1 = -1;
- *x2 = -1;
- *y2 = -1;
- return;
- }
-
- Math::Matrix4 modelMatrix = _currentActor->getFinalMatrix();
- Math::Matrix4 mvpMatrix = _mvpMatrix * modelMatrix;
-
- double top = 1000;
- double right = -1000;
- double left = 1000;
- double bottom = -1000;
-
- Math::Vector3d obj;
- float *pVertices = nullptr;
-
- for (int i = 0; i < model->_numFaces; i++) {
- for (int j = 0; j < model->_faces[i].getNumVertices(); j++) {
- pVertices = model->_vertices + 3 * model->_faces[i].getVertex(j);
-
- obj.set(*(pVertices), *(pVertices + 1), *(pVertices + 2));
-
- Math::Vector4d v = Math::Vector4d(obj.x(), obj.y(), obj.z(), 1.0f);
- v = mvpMatrix * v;
- v /= v.w();
-
- double winX = (1 + v.x()) / 2.0f * _gameWidth;
- double winY = (1 + v.y()) / 2.0f * _gameHeight;
-
- if (winX > right)
- right = winX;
- if (winX < left)
- left = winX;
- if (winY < top)
- top = winY;
- if (winY > bottom)
- bottom = winY;
- }
- }
-
- double t = bottom;
- bottom = _gameHeight - top;
- top = _gameHeight - t;
-
- if (left < 0)
- left = 0;
- if (right >= _gameWidth)
- right = _gameWidth - 1;
- if (top < 0)
- top = 0;
- if (bottom >= _gameHeight)
- bottom = _gameHeight - 1;
-
- if (top >= _gameHeight || left >= _gameWidth || bottom < 0 || right < 0) {
- *x1 = -1;
- *y1 = -1;
- *x2 = -1;
- *y2 = -1;
- return;
- }
-
- *x1 = (int)left;
- *y1 = (int)(_gameHeight - bottom);
- *x2 = (int)right;
- *y2 = (int)(_gameHeight - top);
-}
-
-void GfxOpenGLS::getScreenBoundingBox(const EMIModel *model, int *x1, int *y1, int *x2, int *y2) {
- if (_currentShadowArray) {
- *x1 = -1;
- *y1 = -1;
- *x2 = -1;
- *y2 = -1;
- return;
- }
-
- Math::Matrix4 modelMatrix = _currentActor->getFinalMatrix();
- Math::Matrix4 mvpMatrix = _mvpMatrix * modelMatrix;
-
- double top = 1000;
- double right = -1000;
- double left = 1000;
- double bottom = -1000;
-
- for (uint i = 0; i < model->_numFaces; i++) {
- uint16 *indices = (uint16 *)model->_faces[i]._indexes;
-
- for (uint j = 0; j < model->_faces[i]._faceLength * 3; j++) {
- uint16 index = indices[j];
- const Math::Vector3d &dv = model->_drawVertices[index];
-
- Math::Vector4d v = Math::Vector4d(dv.x(), dv.y(), dv.z(), 1.0f);
- v = mvpMatrix * v;
- v /= v.w();
-
- double winX = (1 + v.x()) / 2.0f * _gameWidth;
- double winY = (1 + v.y()) / 2.0f * _gameHeight;
-
- if (winX > right)
- right = winX;
- if (winX < left)
- left = winX;
- if (winY < top)
- top = winY;
- if (winY > bottom)
- bottom = winY;
- }
- }
-
- double t = bottom;
- bottom = _gameHeight - top;
- top = _gameHeight - t;
-
- if (left < 0)
- left = 0;
- if (right >= _gameWidth)
- right = _gameWidth - 1;
- if (top < 0)
- top = 0;
- if (bottom >= _gameHeight)
- bottom = _gameHeight - 1;
-
- if (top >= _gameHeight || left >= _gameWidth || bottom < 0 || right < 0) {
- *x1 = -1;
- *y1 = -1;
- *x2 = -1;
- *y2 = -1;
- return;
- }
-
- *x1 = (int)left;
- *y1 = (int)(_gameHeight - bottom);
- *x2 = (int)right;
- *y2 = (int)(_gameHeight - top);
-}
-
-void GfxOpenGLS::getActorScreenBBox(const Actor *actor, Common::Point &p1, Common::Point &p2) {
- // Get the actor's bounding box information (describes a 3D box)
- Math::Vector3d bboxPos, bboxSize;
- actor->getBBoxInfo(bboxPos, bboxSize);
-
- // Translate the bounding box to the actor's position
- Math::Matrix4 m = actor->getFinalMatrix();
- bboxPos = bboxPos + actor->getWorldPos();
-
- // Set up the camera coordinate system
- Math::Matrix4 modelView = _currentRot;
- Math::Matrix4 zScale;
- zScale.setValue(2, 2, -1.0);
- modelView = modelView * zScale;
- modelView.transpose();
- modelView.translate(-_currentPos);
- modelView.transpose();
-
- // Set values outside of the screen range
- p1.x = 1000;
- p1.y = 1000;
- p2.x = -1000;
- p2.y = -1000;
-
- // Project all of the points in the 3D bounding box
- Math::Vector3d p, projected;
- for (int x = 0; x < 2; x++) {
- for (int y = 0; y < 2; y++) {
- for (int z = 0; z < 2; z++) {
- Math::Vector3d added(bboxSize.x() * 0.5f * (x * 2 - 1), bboxSize.y() * 0.5f * (y * 2 - 1), bboxSize.z() * 0.5f * (z * 2 - 1));
- m.transform(&added, false);
- p = bboxPos + added;
-
- Math::Vector4d v = Math::Vector4d(p.x(), p.y(), p.z(), 1.0f);
- v = _projMatrix.transform(modelView.transform(v));
- if (v.w() == 0.0)
- return;
- v /= v.w();
-
- double winX = (1 + v.x()) / 2.0f * _gameWidth;
- double winY = (1 + v.y()) / 2.0f * _gameHeight;
-
- // Find the points
- if (winX < p1.x)
- p1.x = winX;
- if (winY < p1.y)
- p1.y = winY;
- if (winX > p2.x)
- p2.x = winX;
- if (winY > p2.y)
- p2.y = winY;
- }
- }
- }
-
- // Swap the p1/p2 y coorindates
- int16 tmp = p1.y;
- p1.y = 480 - p2.y;
- p2.y = 480 - tmp;
-}
-
void GfxOpenGLS::startActorDraw(const Actor *actor) {
_currentActor = actor;
glEnable(GL_DEPTH_TEST);
- const Math::Vector3d &pos = actor->getWorldPos();
- const Math::Quaternion &quat = actor->getRotationQuat();
- const float scale = actor->getScale();
-
Math::Matrix4 viewMatrix = _viewMatrix;
viewMatrix.transpose();
@@ -777,18 +570,13 @@ void GfxOpenGLS::startActorDraw(const Actor *actor) {
shaders[i]->setUniform1f("meshAlpha", 1.0f);
}
} else {
- Math::Matrix4 modelMatrix = quat.toMatrix();
- modelMatrix *= scale;
- modelMatrix.setValue(3, 3, 1.0f);
+ Math::Matrix4 modelMatrix = actor->getGrimModelMatrix();
+ modelMatrix.transpose();
bool hasZBuffer = g_grim->getCurrSet()->getCurrSetup()->_bkgndZBm;
Math::Matrix4 extraMatrix;
_matrixStack.top() = extraMatrix;
- modelMatrix.transpose();
- modelMatrix.setPosition(pos);
- modelMatrix.transpose();
-
for (int i = 0; i < 3; i++) {
shaders[i]->use();
shaders[i]->setUniform("modelMatrix", modelMatrix);
diff --git a/engines/grim/gfx_opengl_shaders.h b/engines/grim/gfx_opengl_shaders.h
index 3fd94156aa5..9e5fff716b3 100644
--- a/engines/grim/gfx_opengl_shaders.h
+++ b/engines/grim/gfx_opengl_shaders.h
@@ -67,9 +67,6 @@ public:
*/
void flipBuffer(bool opportunistic = false) override;
- void getScreenBoundingBox(const Mesh *mesh, int *x1, int *y1, int *x2, int *y2) override;
- void getScreenBoundingBox(const EMIModel *model, int *x1, int *y1, int *x2, int *y2) override;
- void getActorScreenBBox(const Actor *actor, Common::Point &p1, Common::Point &p2) override;
void startActorDraw(const Actor *actor) override;
void finishActorDraw() override;
@@ -252,7 +249,6 @@ private:
float _fclip;
Math::Matrix4 _projMatrix;
Math::Matrix4 _viewMatrix;
- Math::Matrix4 _mvpMatrix;
Math::Matrix4 _overworldProjMatrix;
void setupTexturedCenteredQuad();
diff --git a/engines/grim/gfx_tinygl.cpp b/engines/grim/gfx_tinygl.cpp
index ce1717031c5..04fd4bec0e0 100644
--- a/engines/grim/gfx_tinygl.cpp
+++ b/engines/grim/gfx_tinygl.cpp
@@ -262,208 +262,6 @@ static void shadowProjection(const Math::Vector3d &light, const Math::Vector3d &
tglMultMatrixf(mat);
}
-void GfxTinyGL::getScreenBoundingBox(const Mesh *model, int *x1, int *y1, int *x2, int *y2) {
- if (_currentShadowArray) {
- *x1 = -1;
- *y1 = -1;
- *x2 = -1;
- *y2 = -1;
- return;
- }
-
- TGLfloat top = 1000;
- TGLfloat right = -1000;
- TGLfloat left = 1000;
- TGLfloat bottom = -1000;
-
- for (int i = 0; i < model->_numFaces; i++) {
- Math::Vector3d obj;
- float *pVertices;
-
- for (int j = 0; j < model->_faces[i].getNumVertices(); j++) {
- TGLfloat modelView[16], projection[16];
- TGLint viewPort[4];
-
- tglGetFloatv(TGL_MODELVIEW_MATRIX, modelView);
- tglGetFloatv(TGL_PROJECTION_MATRIX, projection);
- tglGetIntegerv(TGL_VIEWPORT, viewPort);
-
- pVertices = model->_vertices + 3 * model->_faces[i].getVertex(j);
-
- obj.set(*(pVertices), *(pVertices + 1), *(pVertices + 2));
-
- Math::Vector3d win;
- Math::gluMathProject<TGLfloat, TGLint>(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();
- }
- }
-
- float t = bottom;
- bottom = _gameHeight - top;
- top = _gameHeight - t;
-
- if (left < 0)
- left = 0;
- if (right >= _gameWidth)
- right = _gameWidth - 1;
- if (top < 0)
- top = 0;
- if (bottom >= _gameHeight)
- bottom = _gameHeight - 1;
-
- if (top >= _gameHeight || left >= _gameWidth || bottom < 0 || right < 0) {
- *x1 = -1;
- *y1 = -1;
- *x2 = -1;
- *y2 = -1;
- return;
- }
-
- *x1 = (int)left;
- *y1 = (int)top;
- *x2 = (int)right;
- *y2 = (int)bottom;
-}
-
-void GfxTinyGL::getScreenBoundingBox(const EMIModel *model, int *x1, int *y1, int *x2, int *y2) {
- if (_currentShadowArray) {
- *x1 = -1;
- *y1 = -1;
- *x2 = -1;
- *y2 = -1;
- return;
- }
-
- TGLfloat top = 1000;
- TGLfloat right = -1000;
- TGLfloat left = 1000;
- TGLfloat bottom = -1000;
-
- TGLfloat modelView[16], projection[16];
- TGLint viewPort[4];
-
- tglGetFloatv(TGL_MODELVIEW_MATRIX, modelView);
- tglGetFloatv(TGL_PROJECTION_MATRIX, projection);
- tglGetIntegerv(TGL_VIEWPORT, viewPort);
-
- for (uint i = 0; i < model->_numFaces; i++) {
- uint16 *indices = (uint16 *)model->_faces[i]._indexes;
-
- for (uint j = 0; j < model->_faces[i]._faceLength * 3; j++) {
- uint16 index = indices[j];
- Math::Vector3d obj = model->_drawVertices[index];
- Math::Vector3d win;
- Math::gluMathProject<TGLfloat, TGLint>(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();
- }
- }
-
- float t = bottom;
- bottom = _gameHeight - top;
- top = _gameHeight - t;
-
- if (left < 0)
- left = 0;
- if (right >= _gameWidth)
- right = _gameWidth - 1;
- if (top < 0)
- top = 0;
- if (bottom >= _gameHeight)
- bottom = _gameHeight - 1;
-
- if (top >= _gameHeight || left >= _gameWidth || bottom < 0 || right < 0) {
- *x1 = -1;
- *y1 = -1;
- *x2 = -1;
- *y2 = -1;
- return;
- }
-
- *x1 = (int)left;
- *y1 = (int)(_gameHeight - bottom);
- *x2 = (int)right;
- *y2 = (int)(_gameHeight - top);
-}
-
-void GfxTinyGL::getActorScreenBBox(const Actor *actor, Common::Point &p1, Common::Point &p2) {
- // Get the actor's bounding box information (describes a 3D box)
- Math::Vector3d bboxPos, bboxSize;
- actor->getBBoxInfo(bboxPos, bboxSize);
-
- // Translate the bounding box to the actor's position
- Math::Matrix4 m = actor->getFinalMatrix();
- bboxPos = bboxPos + actor->getWorldPos();
-
- // Set up the camera coordinate system
- tglMatrixMode(TGL_MODELVIEW);
- tglPushMatrix();
-
- // Apply the view transform.
- Math::Matrix4 worldRot = _currentRot;
- tglMultMatrixf(worldRot.getData());
- tglTranslatef(-_currentPos.x(), -_currentPos.y(), -_currentPos.z());
-
- // Get the current OpenGL state
- TGLfloat modelView[16], projection[16];
- TGLint viewPort[4];
- tglGetFloatv(TGL_MODELVIEW_MATRIX, modelView);
- tglGetFloatv(TGL_PROJECTION_MATRIX, projection);
- tglGetIntegerv(TGL_VIEWPORT, viewPort);
-
- // Set values outside of the screen range
- p1.x = 1000;
- p1.y = 1000;
- p2.x = -1000;
- p2.y = -1000;
-
- // Project all of the points in the 3D bounding box
- Math::Vector3d p, projected;
- for (int x = 0; x < 2; x++) {
- for (int y = 0; y < 2; y++) {
- for (int z = 0; z < 2; z++) {
- Math::Vector3d added(bboxSize.x() * 0.5f * (x * 2 - 1), bboxSize.y() * 0.5f * (y * 2 - 1), bboxSize.z() * 0.5f * (z * 2 - 1));
- m.transform(&added, false);
- p = bboxPos + added;
- Math::gluMathProject<TGLfloat, TGLint>(p, modelView, projection, viewPort, projected);
-
- // Find the points
- if (projected.x() < p1.x)
- p1.x = projected.x();
- if (projected.y() < p1.y)
- p1.y = projected.y();
- if (projected.x() > p2.x)
- p2.x = projected.x();
- if (projected.y() > p2.y)
- p2.y = projected.y();
- }
- }
- }
-
- // Swap the p1/p2 y coorindates
- int16 tmp = p1.y;
- p1.y = 480 - p2.y;
- p2.y = 480 - tmp;
-
- // Restore the state
- tglPopMatrix();
-}
-
void GfxTinyGL::startActorDraw(const Actor *actor) {
_currentActor = actor;
tglEnable(TGL_TEXTURE_2D);
diff --git a/engines/grim/gfx_tinygl.h b/engines/grim/gfx_tinygl.h
index 730b25747c2..92daa46368c 100644
--- a/engines/grim/gfx_tinygl.h
+++ b/engines/grim/gfx_tinygl.h
@@ -59,10 +59,6 @@ public:
bool isHardwareAccelerated() override;
bool supportsShaders() override;
- void getScreenBoundingBox(const Mesh *model, int *x1, int *y1, int *x2, int *y2) override;
- void getScreenBoundingBox(const EMIModel *model, int *x1, int *y1, int *x2, int *y2) override;
- void getActorScreenBBox(const Actor *actor, Common::Point &p1, Common::Point &p2) override;
-
void startActorDraw(const Actor *actor) override;
void finishActorDraw() override;
void setShadow(Shadow *shadow) override;
diff --git a/engines/grim/model.cpp b/engines/grim/model.cpp
index a347b9442c0..de90ecf99d5 100644
--- a/engines/grim/model.cpp
+++ b/engines/grim/model.cpp
@@ -35,6 +35,60 @@
namespace Grim {
+void ScreenBounds::addPoint(const Math::Matrix4 &mvpMatrix, const Math::Vector3d &point) {
+ Math::Vector4d v = mvpMatrix * Math::Vector4d(point.x(), point.y(), point.z(), 1.0f);
+ if (v.w() <= 0.0f)
+ return;
+ v /= v.w();
+
+ float x = (1 + v.x()) / 2.0f * 640;
+ float y = (1 - v.y()) / 2.0f * 480;
+ extend(x, y, x, y);
+}
+
+void ScreenBounds::addBounds(const ScreenBounds &other) {
+ if (!other.isEmpty())
+ extend(other._left, other._top, other._right, other._bottom);
+}
+
+void ScreenBounds::extend(float left, float top, float right, float bottom) {
+ if (_empty) {
+ _left = left;
+ _top = top;
+ _right = right;
+ _bottom = bottom;
+ _empty = false;
+ return;
+ }
+
+ _left = MIN(_left, left);
+ _top = MIN(_top, top);
+ _right = MAX(_right, right);
+ _bottom = MAX(_bottom, bottom);
+}
+
+bool ScreenBounds::clipToScreen() {
+ if (isEmpty() || _top >= 480 || _left >= 640 || _bottom < 0 || _right < 0)
+ return false;
+
+ _left = MAX(_left, 0.0f);
+ _right = MIN(_right, 640 - 1.0f);
+ _top = MAX(_top, 0.0f);
+ _bottom = MIN(_bottom, 480 - 1.0f);
+ return true;
+}
+
+bool ScreenBounds::overlapsScreen() const {
+ if (isEmpty())
+ return false;
+
+ int x = getLeft();
+ int y = getTop();
+ int w = toPixel(_right - _left);
+ int h = toPixel(_bottom - _top);
+ return x + w >= 0 && x <= 640 && y + h >= 0 && y <= 480;
+}
+
/**
* @class Model
*/
@@ -568,17 +622,20 @@ void Mesh::draw() const {
g_driver->enableLights();
}
-void Mesh::getBoundingBox(int *x1, int *y1, int *x2, int *y2) const {
- int winX1, winY1, winX2, winY2;
- g_driver->getScreenBoundingBox(this, &winX1, &winY1, &winX2, &winY2);
- if (winX1 != -1 && winY1 != -1 && winX2 != -1 && winY2 != -1) {
- *x1 = MIN(*x1, winX1);
- *y1 = MIN(*y1, winY1);
- *x2 = MAX(*x2, winX2);
- *y2 = MAX(*y2, winY2);
+void Mesh::getBoundingBox(const Math::Matrix4 &mvpMatrix, ScreenBounds &bounds) const {
+ ScreenBounds meshBounds;
+ for (int i = 0; i < _numFaces; i++) {
+ for (int j = 0; j < _faces[i].getNumVertices(); j++) {
+ const float *v = _vertices + 3 * _faces[i].getVertex(j);
+ meshBounds.addPoint(mvpMatrix, Math::Vector3d(v[0], v[1], v[2]));
+ }
}
+
+ if (meshBounds.clipToScreen())
+ bounds.addBounds(meshBounds);
}
+
/**
* @class ModelNode
*/
@@ -681,35 +738,32 @@ void ModelNode::draw() const {
}
}
-void ModelNode::getBoundingBox(int *x1, int *y1, int *x2, int *y2) const {
- if (_sibling || _child) {
- translateViewpointStart();
- }
- translateViewpoint();
+void ModelNode::getBoundingBox(const Math::Matrix4 &matrix, ScreenBounds &bounds) const {
if (_hierVisible) {
- if (_child) {
- translateViewpointStart();
- }
- g_driver->translateViewpoint(_pivot);
+ Math::Matrix4 nodeMatrix = matrix * getAnimMatrix();
if (_mesh && _meshVisible) {
- _mesh->getBoundingBox(x1, y1, x2, y2);
+ Math::Matrix4 pivotMatrix;
+ pivotMatrix.setPosition(_pivot);
+ _mesh->getBoundingBox(nodeMatrix * pivotMatrix, bounds);
}
if (_child) {
- translateViewpointFinish();
- _child->getBoundingBox(x1, y1, x2, y2);
+ _child->getBoundingBox(nodeMatrix, bounds);
}
}
- if (_sibling || _child) {
- translateViewpointFinish();
- }
if (_sibling) {
- _sibling->getBoundingBox(x1, y1, x2, y2);
+ _sibling->getBoundingBox(matrix, bounds);
}
}
+Math::Matrix4 ModelNode::getAnimMatrix() const {
+ Math::Matrix4 matrix = _animRot.toMatrix();
+ matrix.setPosition(_animPos);
+ return matrix;
+}
+
void ModelNode::addChild(ModelNode *child) {
ModelNode **childPos = &_child;
while (*childPos)
@@ -739,8 +793,7 @@ void ModelNode::update() {
return;
if (_hierVisible && _needsUpdate) {
- _localMatrix = _animRot.toMatrix();
- _localMatrix.setPosition(_animPos);
+ _localMatrix = getAnimMatrix();
_matrix = _matrix * _localMatrix;
diff --git a/engines/grim/model.h b/engines/grim/model.h
index 1f8936c6d70..ba56357f4a0 100644
--- a/engines/grim/model.h
+++ b/engines/grim/model.h
@@ -40,6 +40,30 @@ class ModelNode;
class CMap;
class Sprite;
+struct ScreenBounds {
+ void addPoint(const Math::Matrix4 &mvpMatrix, const Math::Vector3d &point);
+ void addBounds(const ScreenBounds &other);
+ bool clipToScreen();
+ bool overlapsScreen() const;
+ bool isEmpty() const { return _empty; }
+
+ int getLeft() const { return toPixel(_left); }
+ int getTop() const { return toPixel(_top); }
+ int getRight() const { return toPixel(_right); }
+ int getBottom() const { return toPixel(_bottom); }
+
+ float _left = 0.0f;
+ float _top = 0.0f;
+ float _right = 0.0f;
+ float _bottom = 0.0f;
+
+private:
+ void extend(float left, float top, float right, float bottom);
+ static int toPixel(float v) { return (int)CLIP(v, -32768.0f, 32767.0f); }
+
+ bool _empty = true;
+};
+
class Model : public Object {
public:
// Construct a 3D model from the given data.
@@ -128,7 +152,7 @@ public:
void loadText(TextSplitter *ts, Material *materials[]);
void changeMaterials(Material *materials[]);
void draw() const;
- void getBoundingBox(int *x1, int *y1, int *x2, int *y2) const;
+ void getBoundingBox(const Math::Matrix4 &mvpMatrix, ScreenBounds &bounds) const;
void update();
Mesh();
~Mesh();
@@ -162,7 +186,8 @@ public:
~ModelNode();
void loadBinary(Common::SeekableReadStream *data, ModelNode *hierNodes, const Model::Geoset *g);
void draw() const;
- void getBoundingBox(int *x1, int *y1, int *x2, int *y2) const;
+ void getBoundingBox(const Math::Matrix4 &matrix, ScreenBounds &bounds) const;
+ Math::Matrix4 getAnimMatrix() const;
void addChild(ModelNode *child);
void removeChild(ModelNode *child);
void setMatrix(const Math::Matrix4 &matrix);
More information about the Scummvm-git-logs
mailing list