2021-12-26 20:19:38 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
2008-06-13 14:57:47 +00:00
|
|
|
*
|
2021-12-26 20:19:38 +00:00
|
|
|
* ScummVM is the legal property of its developers, whose names
|
2011-04-16 12:12:44 +00:00
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
2008-06-13 14:57:47 +00:00
|
|
|
* file distributed with this source distribution.
|
2006-04-02 14:20:45 +00:00
|
|
|
*
|
2021-12-26 17:47:58 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2014-04-05 16:18:42 +00:00
|
|
|
*
|
2012-12-19 22:15:43 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2006-04-02 14:20:45 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2012-12-19 22:15:43 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2014-04-05 16:18:42 +00:00
|
|
|
*
|
2012-12-19 22:15:43 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
2021-12-26 17:47:58 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2006-04-02 14:20:45 +00:00
|
|
|
*
|
|
|
|
*/
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2013-02-20 23:50:13 +00:00
|
|
|
#include "common/algorithm.h"
|
2008-06-12 12:08:15 +00:00
|
|
|
#include "common/endian.h"
|
2013-02-20 23:50:13 +00:00
|
|
|
#include "common/func.h"
|
2008-01-26 11:47:23 +00:00
|
|
|
|
2011-07-23 13:37:14 +00:00
|
|
|
#include "engines/grim/debug.h"
|
2009-06-18 11:52:26 +00:00
|
|
|
#include "engines/grim/grim.h"
|
2009-05-24 19:13:58 +00:00
|
|
|
#include "engines/grim/model.h"
|
|
|
|
#include "engines/grim/material.h"
|
|
|
|
#include "engines/grim/textsplit.h"
|
|
|
|
#include "engines/grim/gfx_base.h"
|
2011-05-14 00:55:14 +00:00
|
|
|
#include "engines/grim/resource.h"
|
2011-12-17 23:34:37 +00:00
|
|
|
#include "engines/grim/colormap.h"
|
2013-07-08 00:18:30 +00:00
|
|
|
#include "engines/grim/sprite.h"
|
2005-01-01 12:27:57 +00:00
|
|
|
|
2009-05-25 06:49:57 +00:00
|
|
|
namespace Grim {
|
|
|
|
|
2011-07-22 20:20:48 +00:00
|
|
|
/**
|
|
|
|
* @class Model
|
|
|
|
*/
|
2011-12-19 17:26:48 +00:00
|
|
|
Model::Model(const Common::String &filename, Common::SeekableReadStream *data, CMap *cmap, Model *parent) :
|
2014-01-07 11:27:18 +00:00
|
|
|
Object(), _parent(parent), _numMaterials(0), _numGeosets(0), _cmap(cmap), _fname(filename) {
|
2012-04-25 00:22:16 +00:00
|
|
|
if (data->readUint32BE() == MKTAG('L','D','O','M'))
|
2012-04-25 00:21:18 +00:00
|
|
|
loadBinary(data);
|
2004-02-24 22:43:32 +00:00
|
|
|
else {
|
2011-12-19 17:26:48 +00:00
|
|
|
data->seek(0, SEEK_SET);
|
2013-02-04 20:36:38 +00:00
|
|
|
TextSplitter ts(_fname, data);
|
2012-04-25 00:21:18 +00:00
|
|
|
loadText(&ts);
|
2004-02-24 22:43:32 +00:00
|
|
|
}
|
2011-08-01 20:16:15 +00:00
|
|
|
|
2011-09-10 15:46:07 +00:00
|
|
|
Math::Vector3d max;
|
2011-08-01 20:16:15 +00:00
|
|
|
|
2011-08-02 17:17:32 +00:00
|
|
|
_rootHierNode->update();
|
2011-08-01 20:16:15 +00:00
|
|
|
bool first = true;
|
2011-08-02 17:17:32 +00:00
|
|
|
for (int i = 0; i < _numHierNodes; ++i) {
|
|
|
|
ModelNode &node = _rootHierNode[i];
|
|
|
|
if (node._mesh) {
|
2014-07-10 01:08:01 +00:00
|
|
|
g_driver->createMesh(node._mesh);
|
2011-08-02 17:17:32 +00:00
|
|
|
Mesh &mesh = *node._mesh;
|
2011-10-18 15:42:12 +00:00
|
|
|
Math::Vector3d p = mesh._matrix.getPosition();
|
2011-08-02 17:17:32 +00:00
|
|
|
float x = p.x();
|
|
|
|
float y = p.y();
|
|
|
|
float z = p.z();
|
|
|
|
for (int k = 0; k < mesh._numVertices * 3; k += 3) {
|
|
|
|
if (first || mesh._vertices[k] + x < _bboxPos.x())
|
|
|
|
_bboxPos.x() = mesh._vertices[k] + x;
|
2011-10-18 15:42:12 +00:00
|
|
|
if (first || mesh._vertices[k + 1] + y < _bboxPos.y())
|
2011-08-02 17:17:32 +00:00
|
|
|
_bboxPos.y() = mesh._vertices[k + 1] + y;
|
2011-10-18 15:42:12 +00:00
|
|
|
if (first || mesh._vertices[k + 2] + z < _bboxPos.z())
|
2011-08-02 17:17:32 +00:00
|
|
|
_bboxPos.z() = mesh._vertices[k + 2] + z;
|
|
|
|
|
|
|
|
if (first || mesh._vertices[k] + x > max.x())
|
|
|
|
max.x() = mesh._vertices[k] + x;
|
2011-10-18 15:42:12 +00:00
|
|
|
if (first || mesh._vertices[k + 1] + y > max.y())
|
2011-08-02 17:17:32 +00:00
|
|
|
max.y() = mesh._vertices[k + 1] + y;
|
2011-10-18 15:42:12 +00:00
|
|
|
if (first || mesh._vertices[k + 2] + z > max.z())
|
2011-08-02 17:17:32 +00:00
|
|
|
max.z() = mesh._vertices[k + 2] + z;
|
2011-08-01 20:16:15 +00:00
|
|
|
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_bboxSize = max - _bboxPos;
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
2011-07-22 20:20:48 +00:00
|
|
|
Model::~Model() {
|
2011-12-11 22:28:22 +00:00
|
|
|
for (int i = 0; i < _numMaterials; ++i) {
|
|
|
|
if (!_materialsShared[i]) {
|
2014-12-31 11:09:22 +00:00
|
|
|
delete _materials[i];
|
2011-12-11 22:28:22 +00:00
|
|
|
}
|
|
|
|
}
|
2011-07-22 20:20:48 +00:00
|
|
|
delete[] _materials;
|
|
|
|
delete[] _materialNames;
|
2011-12-11 22:28:22 +00:00
|
|
|
delete[] _materialsShared;
|
2011-07-22 20:20:48 +00:00
|
|
|
delete[] _geosets;
|
|
|
|
delete[] _rootHierNode;
|
|
|
|
g_resourceloader->uncacheModel(this);
|
2005-07-10 18:57:27 +00:00
|
|
|
}
|
|
|
|
|
2012-04-25 00:21:18 +00:00
|
|
|
void Model::loadBinary(Common::SeekableReadStream *data) {
|
2011-12-19 17:26:48 +00:00
|
|
|
_numMaterials = data->readUint32LE();
|
2011-07-18 20:16:40 +00:00
|
|
|
_materials = new Material*[_numMaterials];
|
2005-07-10 18:57:27 +00:00
|
|
|
_materialNames = new char[_numMaterials][32];
|
2011-07-18 21:43:01 +00:00
|
|
|
_materialsShared = new bool[_numMaterials];
|
2004-12-09 23:55:43 +00:00
|
|
|
for (int i = 0; i < _numMaterials; i++) {
|
2011-12-19 17:26:48 +00:00
|
|
|
data->read(_materialNames[i], 32);
|
2011-07-18 21:43:01 +00:00
|
|
|
_materialsShared[i] = false;
|
2014-05-29 22:35:46 +00:00
|
|
|
_materials[i] = nullptr;
|
2012-04-26 00:32:45 +00:00
|
|
|
loadMaterial(i, _cmap);
|
2004-02-24 22:43:32 +00:00
|
|
|
}
|
2011-12-19 17:26:48 +00:00
|
|
|
data->seek(32, SEEK_CUR); // skip name
|
|
|
|
data->seek(4, SEEK_CUR);
|
|
|
|
_numGeosets = data->readUint32LE();
|
2004-12-09 23:55:43 +00:00
|
|
|
_geosets = new Geoset[_numGeosets];
|
|
|
|
for (int i = 0; i < _numGeosets; i++)
|
2011-07-18 20:16:40 +00:00
|
|
|
_geosets[i].loadBinary(data, _materials);
|
2011-12-19 17:26:48 +00:00
|
|
|
data->seek(4, SEEK_CUR);
|
|
|
|
_numHierNodes = data->readUint32LE();
|
2011-07-22 20:09:49 +00:00
|
|
|
_rootHierNode = new ModelNode[_numHierNodes];
|
2011-07-18 20:23:18 +00:00
|
|
|
for (int i = 0; i < _numHierNodes; i++) {
|
|
|
|
_rootHierNode[i].loadBinary(data, _rootHierNode, &_geosets[0]);
|
|
|
|
}
|
2018-04-14 09:29:34 +00:00
|
|
|
_radius = data->readFloatLE();
|
2011-12-19 17:26:48 +00:00
|
|
|
data->seek(36, SEEK_CUR);
|
2018-04-14 09:29:34 +00:00
|
|
|
_insertOffset.readFromStream(data);
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
2012-04-25 00:21:18 +00:00
|
|
|
void Model::loadText(TextSplitter *ts) {
|
2009-06-26 16:13:11 +00:00
|
|
|
ts->expectString("section: header");
|
2004-02-24 22:43:32 +00:00
|
|
|
int major, minor;
|
2009-06-26 16:13:11 +00:00
|
|
|
ts->scanString("3do %d.%d", 2, &major, &minor);
|
|
|
|
ts->expectString("section: modelresource");
|
|
|
|
ts->scanString("materials %d", 1, &_numMaterials);
|
2011-07-18 20:16:40 +00:00
|
|
|
_materials = new Material*[_numMaterials];
|
2005-07-10 18:57:27 +00:00
|
|
|
_materialNames = new char[_numMaterials][32];
|
2011-07-18 21:43:01 +00:00
|
|
|
_materialsShared = new bool[_numMaterials];
|
2004-12-09 23:55:43 +00:00
|
|
|
for (int i = 0; i < _numMaterials; i++) {
|
2005-07-17 23:40:22 +00:00
|
|
|
char materialName[32];
|
2004-02-24 22:43:32 +00:00
|
|
|
int num;
|
2011-07-18 21:43:01 +00:00
|
|
|
_materialsShared[i] = false;
|
2014-05-29 22:35:46 +00:00
|
|
|
_materials[i] = nullptr;
|
2008-07-30 07:04:32 +00:00
|
|
|
|
2009-06-26 16:13:11 +00:00
|
|
|
ts->scanString("%d: %32s", 2, &num, materialName);
|
2022-09-25 09:34:23 +00:00
|
|
|
Common::strcpy_s(_materialNames[num], materialName);
|
2012-04-26 00:32:45 +00:00
|
|
|
loadMaterial(num, _cmap);
|
2004-02-24 22:43:32 +00:00
|
|
|
}
|
|
|
|
|
2009-06-26 16:13:11 +00:00
|
|
|
ts->expectString("section: geometrydef");
|
|
|
|
ts->scanString("radius %f", 1, &_radius);
|
|
|
|
ts->scanString("insert offset %f %f %f", 3, &_insertOffset.x(), &_insertOffset.y(), &_insertOffset.z());
|
|
|
|
ts->scanString("geosets %d", 1, &_numGeosets);
|
2004-12-09 23:55:43 +00:00
|
|
|
_geosets = new Geoset[_numGeosets];
|
|
|
|
for (int i = 0; i < _numGeosets; i++) {
|
2004-02-24 22:43:32 +00:00
|
|
|
int num;
|
2009-06-26 16:13:11 +00:00
|
|
|
ts->scanString("geoset %d", 1, &num);
|
2011-07-18 20:16:40 +00:00
|
|
|
_geosets[num].loadText(ts, _materials);
|
2004-02-24 22:43:32 +00:00
|
|
|
}
|
|
|
|
|
2009-06-26 16:13:11 +00:00
|
|
|
ts->expectString("section: hierarchydef");
|
|
|
|
ts->scanString("hierarchy nodes %d", 1, &_numHierNodes);
|
2011-07-22 20:09:49 +00:00
|
|
|
_rootHierNode = new ModelNode[_numHierNodes];
|
2004-12-09 23:55:43 +00:00
|
|
|
for (int i = 0; i < _numHierNodes; i++) {
|
2009-06-07 14:34:48 +00:00
|
|
|
int num, mesh, parent, child, sibling, numChildren;
|
|
|
|
unsigned int flags, type;
|
2004-02-24 22:43:32 +00:00
|
|
|
float x, y, z, pitch, yaw, roll, pivotx, pivoty, pivotz;
|
|
|
|
char name[64];
|
2009-06-26 16:13:11 +00:00
|
|
|
ts->scanString(" %d: %x %x %d %d %d %d %d %f %f %f %f %f %f %f %f %f %64s",
|
2011-07-22 20:20:48 +00:00
|
|
|
18, &num, &flags, &type, &mesh, &parent, &child, &sibling,
|
|
|
|
&numChildren, &x, &y, &z, &pitch, &yaw, &roll, &pivotx, &pivoty, &pivotz, name);
|
2009-06-07 14:34:48 +00:00
|
|
|
_rootHierNode[num]._flags = (int)flags;
|
|
|
|
_rootHierNode[num]._type = (int)type;
|
2004-02-24 22:43:32 +00:00
|
|
|
if (mesh < 0)
|
2014-05-29 22:35:46 +00:00
|
|
|
_rootHierNode[num]._mesh = nullptr;
|
2004-02-24 22:43:32 +00:00
|
|
|
else
|
2009-06-26 16:13:11 +00:00
|
|
|
_rootHierNode[num]._mesh = &_geosets[0]._meshes[mesh];
|
2004-02-24 22:43:32 +00:00
|
|
|
if (parent >= 0) {
|
2009-06-26 16:13:11 +00:00
|
|
|
_rootHierNode[num]._parent = &_rootHierNode[parent];
|
2004-12-09 23:55:43 +00:00
|
|
|
_rootHierNode[num]._depth = _rootHierNode[parent]._depth + 1;
|
2004-02-24 22:43:32 +00:00
|
|
|
} else {
|
2014-05-29 22:35:46 +00:00
|
|
|
_rootHierNode[num]._parent = nullptr;
|
2004-12-09 23:55:43 +00:00
|
|
|
_rootHierNode[num]._depth = 0;
|
2004-02-24 22:43:32 +00:00
|
|
|
}
|
|
|
|
if (child >= 0)
|
2009-06-26 16:13:11 +00:00
|
|
|
_rootHierNode[num]._child = &_rootHierNode[child];
|
2004-02-24 22:43:32 +00:00
|
|
|
else
|
2014-05-29 22:35:46 +00:00
|
|
|
_rootHierNode[num]._child = nullptr;
|
2004-02-24 22:43:32 +00:00
|
|
|
if (sibling >= 0)
|
2009-06-26 16:13:11 +00:00
|
|
|
_rootHierNode[num]._sibling = &_rootHierNode[sibling];
|
2004-02-24 22:43:32 +00:00
|
|
|
else
|
2014-05-29 22:35:46 +00:00
|
|
|
_rootHierNode[num]._sibling = nullptr;
|
2004-12-09 23:55:43 +00:00
|
|
|
|
|
|
|
_rootHierNode[num]._numChildren = numChildren;
|
2011-09-10 15:46:07 +00:00
|
|
|
_rootHierNode[num]._pos = Math::Vector3d(x, y, z);
|
2014-12-26 22:30:09 +00:00
|
|
|
_rootHierNode[num]._rot = Math::Quaternion::fromEuler(yaw, pitch, roll, Math::EO_ZXY);
|
|
|
|
_rootHierNode[num]._animRot = _rootHierNode[num]._rot;
|
|
|
|
_rootHierNode[num]._animPos = _rootHierNode[num]._pos;
|
2011-09-10 15:46:07 +00:00
|
|
|
_rootHierNode[num]._pivot = Math::Vector3d(pivotx, pivoty, pivotz);
|
2004-12-09 23:55:43 +00:00
|
|
|
_rootHierNode[num]._meshVisible = true;
|
|
|
|
_rootHierNode[num]._hierVisible = true;
|
2014-05-29 22:35:46 +00:00
|
|
|
_rootHierNode[num]._sprite = nullptr;
|
2013-01-19 17:35:36 +00:00
|
|
|
_rootHierNode[num]._initialized = true;
|
2004-02-24 22:43:32 +00:00
|
|
|
}
|
2004-04-19 09:56:34 +00:00
|
|
|
|
2011-10-07 21:05:20 +00:00
|
|
|
if (!ts->isEof())
|
|
|
|
Debug::warning(Debug::Models, "Unexpected junk at end of model text");
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
2011-07-22 20:20:48 +00:00
|
|
|
void Model::draw() const {
|
2011-12-12 17:57:17 +00:00
|
|
|
_rootHierNode->draw();
|
2011-07-22 20:20:48 +00:00
|
|
|
}
|
|
|
|
|
2011-12-12 18:18:33 +00:00
|
|
|
ModelNode *Model::getHierarchy() const {
|
|
|
|
return _rootHierNode;
|
2011-07-22 20:20:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Model::reload(CMap *cmap) {
|
|
|
|
// Load the new colormap
|
|
|
|
for (int i = 0; i < _numMaterials; i++) {
|
2012-04-26 00:32:45 +00:00
|
|
|
loadMaterial(i, cmap);
|
2011-07-22 20:20:48 +00:00
|
|
|
}
|
|
|
|
for (int i = 0; i < _numGeosets; i++)
|
|
|
|
_geosets[i].changeMaterials(_materials);
|
2012-04-26 00:32:45 +00:00
|
|
|
_cmap = cmap;
|
2011-07-22 20:20:48 +00:00
|
|
|
}
|
|
|
|
|
2012-04-26 00:32:45 +00:00
|
|
|
void Model::loadMaterial(int index, CMap *cmap) {
|
2014-05-29 22:35:46 +00:00
|
|
|
Material *mat = nullptr;
|
2011-07-18 21:43:01 +00:00
|
|
|
if (!_materialsShared[index]) {
|
2011-07-24 15:40:50 +00:00
|
|
|
mat = _materials[index];
|
2011-07-18 21:43:01 +00:00
|
|
|
}
|
2014-05-29 22:35:46 +00:00
|
|
|
_materials[index] = nullptr;
|
2011-07-18 21:43:01 +00:00
|
|
|
if (_parent) {
|
2012-04-26 00:32:45 +00:00
|
|
|
_materials[index] = _parent->findMaterial(_materialNames[index], cmap);
|
2011-07-24 15:40:50 +00:00
|
|
|
if (_materials[index]) {
|
|
|
|
_materialsShared[index] = true;
|
|
|
|
}
|
2011-07-18 21:43:01 +00:00
|
|
|
}
|
|
|
|
if (!_materials[index]) {
|
2012-04-26 00:32:45 +00:00
|
|
|
if (mat && cmap->getFilename() == _cmap->getFilename()) {
|
2011-07-24 15:40:50 +00:00
|
|
|
_materials[index] = mat;
|
|
|
|
} else {
|
2014-06-18 01:58:21 +00:00
|
|
|
_materials[index] = g_resourceloader->loadMaterial(_materialNames[index], cmap, false);
|
2011-07-24 15:40:50 +00:00
|
|
|
}
|
2011-07-18 21:43:01 +00:00
|
|
|
_materialsShared[index] = false;
|
|
|
|
}
|
2011-07-24 15:40:50 +00:00
|
|
|
if (mat != _materials[index]) {
|
|
|
|
delete mat;
|
|
|
|
}
|
2011-07-18 21:43:01 +00:00
|
|
|
}
|
|
|
|
|
2011-07-27 16:07:35 +00:00
|
|
|
Material *Model::findMaterial(const char *name, CMap *cmap) const {
|
2011-07-18 21:43:01 +00:00
|
|
|
for (int i = 0; i < _numMaterials; ++i) {
|
|
|
|
if (scumm_stricmp(name, _materialNames[i]) == 0) {
|
2011-07-27 16:07:35 +00:00
|
|
|
if (cmap->getFilename() != _cmap->getFilename())
|
|
|
|
_materials[i]->reload(cmap);
|
2011-07-18 21:43:01 +00:00
|
|
|
return _materials[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-29 22:35:46 +00:00
|
|
|
return nullptr;
|
2011-07-18 21:43:01 +00:00
|
|
|
}
|
|
|
|
|
2011-07-22 20:20:48 +00:00
|
|
|
/**
|
|
|
|
* @class Model::Geoset
|
|
|
|
*/
|
|
|
|
Model::Geoset::~Geoset() {
|
|
|
|
delete[] _meshes;
|
|
|
|
}
|
|
|
|
|
2011-12-19 17:26:48 +00:00
|
|
|
void Model::Geoset::loadBinary(Common::SeekableReadStream *data, Material *materials[]) {
|
|
|
|
_numMeshes = data->readUint32LE();
|
2011-07-22 20:20:48 +00:00
|
|
|
_meshes = new Mesh[_numMeshes];
|
2005-07-10 18:57:27 +00:00
|
|
|
for (int i = 0; i < _numMeshes; i++)
|
2011-07-22 20:20:48 +00:00
|
|
|
_meshes[i].loadBinary(data, materials);
|
2005-07-10 18:57:27 +00:00
|
|
|
}
|
|
|
|
|
2009-08-27 19:01:09 +00:00
|
|
|
void Model::Geoset::loadText(TextSplitter *ts, Material *materials[]) {
|
2009-06-26 16:13:11 +00:00
|
|
|
ts->scanString("meshes %d", 1, &_numMeshes);
|
2004-12-09 23:55:43 +00:00
|
|
|
_meshes = new Mesh[_numMeshes];
|
|
|
|
for (int i = 0; i < _numMeshes; i++) {
|
2004-02-24 22:43:32 +00:00
|
|
|
int num;
|
2009-06-26 16:13:11 +00:00
|
|
|
ts->scanString("mesh %d", 1, &num);
|
2004-12-09 23:55:43 +00:00
|
|
|
_meshes[num].loadText(ts, materials);
|
2004-02-24 22:43:32 +00:00
|
|
|
}
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
2011-07-22 20:20:48 +00:00
|
|
|
void Model::Geoset::changeMaterials(Material *materials[]) {
|
|
|
|
for (int i = 0; i < _numMeshes; i++)
|
|
|
|
_meshes[i].changeMaterials(materials);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @class MeshFace
|
|
|
|
*/
|
2014-03-30 21:02:32 +00:00
|
|
|
MeshFace::MeshFace() :
|
|
|
|
_material(nullptr), _type(0), _geo(0), _light(0), _tex(0),
|
|
|
|
_extraLight(0), _numVertices(0), _vertices(nullptr),
|
|
|
|
_texVertices(nullptr), _userData(nullptr) {
|
|
|
|
}
|
|
|
|
|
2011-07-22 20:20:48 +00:00
|
|
|
MeshFace::~MeshFace() {
|
|
|
|
delete[] _vertices;
|
|
|
|
delete[] _texVertices;
|
|
|
|
}
|
|
|
|
|
2014-03-30 22:33:24 +00:00
|
|
|
void MeshFace::stealData(MeshFace &other) {
|
|
|
|
*this = other;
|
|
|
|
other._vertices = nullptr;
|
|
|
|
other._texVertices = nullptr;
|
2014-03-30 21:20:47 +00:00
|
|
|
}
|
|
|
|
|
2011-12-19 17:26:48 +00:00
|
|
|
int MeshFace::loadBinary(Common::SeekableReadStream *data, Material *materials[]) {
|
|
|
|
data->seek(4, SEEK_CUR);
|
|
|
|
_type = data->readUint32LE();
|
|
|
|
_geo = data->readUint32LE();
|
|
|
|
_light = data->readUint32LE();
|
|
|
|
_tex = data->readUint32LE();
|
|
|
|
_numVertices = data->readUint32LE();
|
|
|
|
data->seek(4, SEEK_CUR);
|
|
|
|
int texPtr = data->readUint32LE();
|
|
|
|
int materialPtr = data->readUint32LE();
|
|
|
|
data->seek(12, SEEK_CUR);
|
2018-04-14 09:29:34 +00:00
|
|
|
_extraLight = data->readFloatLE();
|
2011-12-19 17:26:48 +00:00
|
|
|
data->seek(12, SEEK_CUR);
|
2018-04-14 09:29:34 +00:00
|
|
|
_normal.readFromStream(data);
|
2011-07-22 20:20:48 +00:00
|
|
|
|
|
|
|
_vertices = new int[_numVertices];
|
2014-03-30 21:02:32 +00:00
|
|
|
for (int i = 0; i < _numVertices; i++) {
|
2011-12-19 17:26:48 +00:00
|
|
|
_vertices[i] = data->readUint32LE();
|
2014-03-30 21:02:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (texPtr != 0) {
|
2011-07-22 20:20:48 +00:00
|
|
|
_texVertices = new int[_numVertices];
|
2014-03-30 21:02:32 +00:00
|
|
|
for (int i = 0; i < _numVertices; i++) {
|
2011-12-19 17:26:48 +00:00
|
|
|
_texVertices[i] = data->readUint32LE();
|
2014-03-30 21:02:32 +00:00
|
|
|
}
|
2011-07-22 20:20:48 +00:00
|
|
|
}
|
2014-03-30 21:02:32 +00:00
|
|
|
|
|
|
|
if (materialPtr != 0) {
|
2011-12-19 17:26:48 +00:00
|
|
|
materialPtr = data->readUint32LE();
|
|
|
|
_material = materials[materialPtr];
|
2011-07-22 20:20:48 +00:00
|
|
|
}
|
2014-03-30 21:02:32 +00:00
|
|
|
|
2011-07-22 20:20:48 +00:00
|
|
|
return materialPtr;
|
|
|
|
}
|
|
|
|
|
2014-03-30 21:02:32 +00:00
|
|
|
int MeshFace::loadText(TextSplitter *ts, Material *materials[], int offset) {
|
|
|
|
int readlen, materialid;
|
|
|
|
|
|
|
|
if (ts->isEof())
|
|
|
|
error("Expected face data, got EOF");
|
|
|
|
|
|
|
|
ts->scanStringAtOffsetNoNewLine(offset, "%d %x %d %d %d %f %d%n", 7, &materialid, &_type, &_geo, &_light, &_tex, &_extraLight, &_numVertices, &readlen);
|
|
|
|
readlen += offset;
|
|
|
|
|
|
|
|
assert(materialid != -1);
|
|
|
|
_material = materials[materialid];
|
|
|
|
_vertices = new int[_numVertices];
|
|
|
|
_texVertices = new int[_numVertices];
|
|
|
|
for (int i = 0; i < _numVertices; ++i) {
|
|
|
|
int readlen2;
|
|
|
|
|
|
|
|
ts->scanStringAtOffsetNoNewLine(readlen, " %d, %d%n", 2, &_vertices[i], &_texVertices[i], &readlen2);
|
|
|
|
readlen += readlen2;
|
|
|
|
}
|
|
|
|
ts->nextLine();
|
|
|
|
|
|
|
|
return materialid;
|
|
|
|
}
|
|
|
|
|
2011-07-22 20:20:48 +00:00
|
|
|
void MeshFace::changeMaterial(Material *material) {
|
|
|
|
_material = material;
|
|
|
|
}
|
|
|
|
|
2012-12-27 18:08:46 +00:00
|
|
|
void MeshFace::draw(const Mesh *mesh) const {
|
2012-04-08 14:53:03 +00:00
|
|
|
if (_light == 0 && !g_driver->isShadowModeActive())
|
|
|
|
g_driver->disableLights();
|
|
|
|
|
2011-07-22 20:20:48 +00:00
|
|
|
_material->select();
|
2012-12-27 18:08:46 +00:00
|
|
|
g_driver->drawModelFace(mesh, this);
|
2012-04-08 14:53:03 +00:00
|
|
|
|
|
|
|
if (_light == 0 && !g_driver->isShadowModeActive())
|
|
|
|
g_driver->enableLights();
|
2011-07-22 20:20:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @class Mesh
|
|
|
|
*/
|
2013-10-26 20:57:55 +00:00
|
|
|
Mesh::Mesh() :
|
|
|
|
_numFaces(0), _radius(0.0f), _shadow(0), _geometryMode(0),
|
2014-05-29 22:35:46 +00:00
|
|
|
_lightingMode(0), _textureMode(0), _numVertices(0), _materialid(nullptr),
|
|
|
|
_vertices(nullptr), _verticesI(nullptr), _vertNormals(nullptr),
|
|
|
|
_numTextureVerts(0), _textureVerts(nullptr), _faces(nullptr), _userData(nullptr) {
|
2013-04-21 14:05:32 +00:00
|
|
|
_name[0] = '\0';
|
2013-04-21 13:39:39 +00:00
|
|
|
}
|
|
|
|
|
2011-07-22 20:20:48 +00:00
|
|
|
Mesh::~Mesh() {
|
2014-07-10 01:17:09 +00:00
|
|
|
g_driver->destroyMesh(this);
|
|
|
|
|
2011-07-22 20:20:48 +00:00
|
|
|
delete[] _vertices;
|
|
|
|
delete[] _verticesI;
|
|
|
|
delete[] _vertNormals;
|
|
|
|
delete[] _textureVerts;
|
|
|
|
delete[] _faces;
|
|
|
|
delete[] _materialid;
|
|
|
|
}
|
|
|
|
|
2011-12-19 17:26:48 +00:00
|
|
|
void Mesh::loadBinary(Common::SeekableReadStream *data, Material *materials[]) {
|
|
|
|
data->read(_name, 32);
|
|
|
|
data->seek(4, SEEK_CUR);
|
|
|
|
_geometryMode = data->readUint32LE();
|
|
|
|
_lightingMode = data->readUint32LE();
|
|
|
|
_textureMode = data->readUint32LE();
|
|
|
|
_numVertices = data->readUint32LE();
|
|
|
|
_numTextureVerts = data->readUint32LE();
|
|
|
|
_numFaces = data->readUint32LE();
|
2011-07-22 20:20:48 +00:00
|
|
|
_vertices = new float[3 * _numVertices];
|
|
|
|
_verticesI = new float[_numVertices];
|
|
|
|
_vertNormals = new float[3 * _numVertices];
|
|
|
|
_textureVerts = new float[2 * _numTextureVerts];
|
|
|
|
_faces = new MeshFace[_numFaces];
|
|
|
|
_materialid = new int[_numFaces];
|
|
|
|
for (int i = 0; i < 3 * _numVertices; i++) {
|
2018-04-14 09:29:34 +00:00
|
|
|
_vertices[i] = data->readFloatLE();
|
2011-07-22 20:20:48 +00:00
|
|
|
}
|
|
|
|
for (int i = 0; i < 2 * _numTextureVerts; i++) {
|
2018-04-14 09:29:34 +00:00
|
|
|
_textureVerts[i] = data->readFloatLE();
|
2011-07-22 20:20:48 +00:00
|
|
|
}
|
|
|
|
for (int i = 0; i < _numVertices; i++) {
|
2018-04-14 09:29:34 +00:00
|
|
|
_verticesI[i] = data->readFloatLE();
|
2011-07-22 20:20:48 +00:00
|
|
|
}
|
2011-12-19 17:26:48 +00:00
|
|
|
data->seek(_numVertices * 4, SEEK_CUR);
|
2005-07-10 18:57:27 +00:00
|
|
|
for (int i = 0; i < _numFaces; i++)
|
2011-07-22 20:20:48 +00:00
|
|
|
_materialid[i] = _faces[i].loadBinary(data, materials);
|
|
|
|
for (int i = 0; i < 3 * _numVertices; i++) {
|
2018-04-14 09:29:34 +00:00
|
|
|
_vertNormals[i] = data->readFloatLE();
|
2011-07-22 20:20:48 +00:00
|
|
|
}
|
2011-12-19 17:26:48 +00:00
|
|
|
_shadow = data->readUint32LE();
|
|
|
|
data->seek(4, SEEK_CUR);
|
2018-04-14 09:29:34 +00:00
|
|
|
_radius = data->readFloatLE();
|
2011-12-19 17:26:48 +00:00
|
|
|
data->seek(24, SEEK_CUR);
|
2013-02-20 23:50:13 +00:00
|
|
|
sortFaces();
|
2005-07-10 18:57:27 +00:00
|
|
|
}
|
|
|
|
|
2013-07-09 19:12:55 +00:00
|
|
|
void Mesh::loadText(TextSplitter *ts, Material *materials[]) {
|
2009-06-26 16:13:11 +00:00
|
|
|
ts->scanString("name %32s", 1, _name);
|
|
|
|
ts->scanString("radius %f", 1, &_radius);
|
2004-02-24 22:43:32 +00:00
|
|
|
|
|
|
|
// In data001/rope_scale.3do, the shadow line is missing
|
2011-05-05 09:56:36 +00:00
|
|
|
if (sscanf(ts->getCurrentLine(), "shadow %d", &_shadow) < 1) {
|
2004-12-09 23:55:43 +00:00
|
|
|
_shadow = 0;
|
2004-02-24 22:43:32 +00:00
|
|
|
} else
|
2009-06-26 16:13:11 +00:00
|
|
|
ts->nextLine();
|
|
|
|
ts->scanString("geometrymode %d", 1, &_geometryMode);
|
|
|
|
ts->scanString("lightingmode %d", 1, &_lightingMode);
|
|
|
|
ts->scanString("texturemode %d", 1, &_textureMode);
|
|
|
|
ts->scanString("vertices %d", 1, &_numVertices);
|
2004-12-09 23:55:43 +00:00
|
|
|
_vertices = new float[3 * _numVertices];
|
|
|
|
_verticesI = new float[_numVertices];
|
|
|
|
_vertNormals = new float[3 * _numVertices];
|
|
|
|
|
|
|
|
for (int i = 0; i < _numVertices; i++) {
|
2004-02-24 22:43:32 +00:00
|
|
|
int num;
|
|
|
|
float x, y, z, ival;
|
2009-06-26 16:13:11 +00:00
|
|
|
ts->scanString(" %d: %f %f %f %f", 5, &num, &x, &y, &z, &ival);
|
2004-12-09 23:55:43 +00:00
|
|
|
_vertices[3 * num] = x;
|
|
|
|
_vertices[3 * num + 1] = y;
|
|
|
|
_vertices[3 * num + 2] = z;
|
|
|
|
_verticesI[num] = ival;
|
2004-02-24 22:43:32 +00:00
|
|
|
}
|
|
|
|
|
2009-06-26 16:13:11 +00:00
|
|
|
ts->scanString("texture vertices %d", 1, &_numTextureVerts);
|
2004-12-09 23:55:43 +00:00
|
|
|
_textureVerts = new float[2 * _numTextureVerts];
|
2004-02-24 22:43:32 +00:00
|
|
|
|
2004-12-09 23:55:43 +00:00
|
|
|
for (int i = 0; i < _numTextureVerts; i++) {
|
2004-02-24 22:43:32 +00:00
|
|
|
int num;
|
|
|
|
float x, y;
|
2009-06-26 16:13:11 +00:00
|
|
|
ts->scanString(" %d: %f %f", 3, &num, &x, &y);
|
2004-12-09 23:55:43 +00:00
|
|
|
_textureVerts[2 * num] = x;
|
|
|
|
_textureVerts[2 * num + 1] = y;
|
2004-02-24 22:43:32 +00:00
|
|
|
}
|
|
|
|
|
2009-06-26 16:13:11 +00:00
|
|
|
ts->expectString("vertex normals");
|
2004-12-09 23:55:43 +00:00
|
|
|
for (int i = 0; i < _numVertices; i++) {
|
2004-02-24 22:43:32 +00:00
|
|
|
int num;
|
|
|
|
float x, y, z;
|
2009-06-26 16:13:11 +00:00
|
|
|
ts->scanString(" %d: %f %f %f", 4, &num, &x, &y, &z);
|
2004-12-09 23:55:43 +00:00
|
|
|
_vertNormals[3 * num] = x;
|
|
|
|
_vertNormals[3 * num + 1] = y;
|
|
|
|
_vertNormals[3 * num + 2] = z;
|
2004-02-24 22:43:32 +00:00
|
|
|
}
|
|
|
|
|
2009-06-26 16:13:11 +00:00
|
|
|
ts->scanString("faces %d", 1, &_numFaces);
|
2011-07-22 20:09:49 +00:00
|
|
|
_faces = new MeshFace[_numFaces];
|
2005-07-10 18:57:27 +00:00
|
|
|
_materialid = new int[_numFaces];
|
2004-12-09 23:55:43 +00:00
|
|
|
for (int i = 0; i < _numFaces; i++) {
|
2014-03-30 21:02:32 +00:00
|
|
|
int num, readlen;
|
|
|
|
ts->scanStringNoNewLine(" %d:%n ", 1, &num, &readlen);
|
|
|
|
_materialid[num] = _faces[num].loadText(ts, materials, readlen);
|
2004-02-24 22:43:32 +00:00
|
|
|
}
|
|
|
|
|
2009-06-26 16:13:11 +00:00
|
|
|
ts->expectString("face normals");
|
2004-12-09 23:55:43 +00:00
|
|
|
for (int i = 0; i < _numFaces; i++) {
|
2004-02-24 22:43:32 +00:00
|
|
|
int num;
|
|
|
|
float x, y, z;
|
2009-06-26 16:13:11 +00:00
|
|
|
ts->scanString(" %d: %f %f %f", 4, &num, &x, &y, &z);
|
2014-03-30 21:38:52 +00:00
|
|
|
_faces[num].setNormal(Math::Vector3d(x, y, z));
|
2004-02-24 22:43:32 +00:00
|
|
|
}
|
2013-02-20 23:50:13 +00:00
|
|
|
sortFaces();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mesh::sortFaces() {
|
|
|
|
if (_numFaces < 2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
MeshFace *newFaces = new MeshFace[_numFaces];
|
|
|
|
int *newMaterialid = new int[_numFaces];
|
|
|
|
bool *copied = new bool[_numFaces];
|
|
|
|
for (int i = 0; i < _numFaces; ++i)
|
|
|
|
copied[i] = false;
|
|
|
|
|
|
|
|
for (int cur = 0, writeIdx = 0; cur < _numFaces; ++cur) {
|
|
|
|
if (copied[cur])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (int other = cur; other < _numFaces; ++other) {
|
2014-03-30 21:38:52 +00:00
|
|
|
if (_faces[cur].getMaterial() == _faces[other].getMaterial() && !copied[other]) {
|
2013-02-20 23:50:13 +00:00
|
|
|
copied[other] = true;
|
2014-03-30 22:33:24 +00:00
|
|
|
newFaces[writeIdx].stealData(_faces[other]);
|
2013-02-20 23:50:13 +00:00
|
|
|
newMaterialid[writeIdx] = _materialid[other];
|
|
|
|
writeIdx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete[] _faces;
|
|
|
|
_faces = newFaces;
|
|
|
|
delete[] _materialid;
|
|
|
|
_materialid = newMaterialid;
|
|
|
|
delete[] copied;
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
2011-07-22 20:20:48 +00:00
|
|
|
void Mesh::update() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mesh::changeMaterials(Material *materials[]) {
|
|
|
|
for (int i = 0; i < _numFaces; i++)
|
|
|
|
_faces[i].changeMaterial(materials[_materialid[i]]);
|
|
|
|
}
|
|
|
|
|
2011-12-12 17:57:17 +00:00
|
|
|
void Mesh::draw() const {
|
2011-07-22 20:20:48 +00:00
|
|
|
if (_lightingMode == 0)
|
|
|
|
g_driver->disableLights();
|
|
|
|
|
2013-02-20 22:02:37 +00:00
|
|
|
g_driver->drawMesh(this);
|
2011-07-22 20:20:48 +00:00
|
|
|
|
|
|
|
if (_lightingMode == 0)
|
|
|
|
g_driver->enableLights();
|
|
|
|
}
|
|
|
|
|
2011-12-12 17:57:17 +00:00
|
|
|
void Mesh::getBoundingBox(int *x1, int *y1, int *x2, int *y2) const {
|
|
|
|
int winX1, winY1, winX2, winY2;
|
2014-07-22 23:13:29 +00:00
|
|
|
g_driver->getScreenBoundingBox(this, &winX1, &winY1, &winX2, &winY2);
|
2011-12-12 17:57:17 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-22 20:20:48 +00:00
|
|
|
/**
|
|
|
|
* @class ModelNode
|
|
|
|
*/
|
2013-10-25 01:26:01 +00:00
|
|
|
ModelNode::ModelNode() :
|
2013-10-26 20:57:55 +00:00
|
|
|
_initialized(false), _needsUpdate(true), _mesh(nullptr), _flags(0), _type(0),
|
|
|
|
_depth(0), _numChildren(0), _parent(nullptr), _child(nullptr), _sprite(nullptr),
|
|
|
|
_sibling(nullptr), _meshVisible(false), _hierVisible(false) {
|
2013-10-25 01:26:01 +00:00
|
|
|
_name[0] = '\0';
|
|
|
|
}
|
|
|
|
|
2011-07-22 20:20:48 +00:00
|
|
|
ModelNode::~ModelNode() {
|
|
|
|
ModelNode *child = _child;
|
|
|
|
while (child) {
|
2014-05-29 22:35:46 +00:00
|
|
|
child->_parent = nullptr;
|
2011-07-22 20:20:48 +00:00
|
|
|
child = child->_sibling;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-19 17:26:48 +00:00
|
|
|
void ModelNode::loadBinary(Common::SeekableReadStream *data, ModelNode *hierNodes, const Model::Geoset *g) {
|
|
|
|
data->read(_name, 64);
|
|
|
|
_flags = data->readUint32LE();
|
|
|
|
data->seek(4, SEEK_CUR);
|
|
|
|
_type = data->readUint32LE();
|
|
|
|
int meshNum = data->readUint32LE();
|
2011-07-22 20:20:48 +00:00
|
|
|
if (meshNum < 0)
|
2014-05-29 22:35:46 +00:00
|
|
|
_mesh = nullptr;
|
2011-07-22 20:20:48 +00:00
|
|
|
else
|
|
|
|
_mesh = g->_meshes + meshNum;
|
2011-12-19 17:26:48 +00:00
|
|
|
_depth = data->readUint32LE();
|
|
|
|
int parentPtr = data->readUint32LE();
|
|
|
|
_numChildren = data->readUint32LE();
|
|
|
|
int childPtr = data->readUint32LE();
|
|
|
|
int siblingPtr = data->readUint32LE();
|
2018-04-14 09:29:34 +00:00
|
|
|
_pivot.readFromStream(data);
|
|
|
|
_pos.readFromStream(data);
|
|
|
|
float pitch = data->readFloatLE();
|
|
|
|
float yaw = data->readFloatLE();
|
|
|
|
float roll = data->readFloatLE();
|
2014-12-26 22:21:56 +00:00
|
|
|
_rot = Math::Quaternion::fromEuler(yaw, pitch, roll, Math::EO_ZXY);
|
|
|
|
_animRot = _rot;
|
2014-06-25 14:15:41 +00:00
|
|
|
_animPos = _pos;
|
2014-05-29 22:35:46 +00:00
|
|
|
_sprite = nullptr;
|
2011-07-22 20:20:48 +00:00
|
|
|
|
2011-12-19 17:26:48 +00:00
|
|
|
data->seek(48, SEEK_CUR);
|
2011-07-22 20:20:48 +00:00
|
|
|
|
2011-12-19 17:26:48 +00:00
|
|
|
if (parentPtr != 0)
|
|
|
|
_parent = hierNodes + data->readUint32LE();
|
|
|
|
else
|
2014-05-29 22:35:46 +00:00
|
|
|
_parent = nullptr;
|
2011-12-19 17:26:48 +00:00
|
|
|
|
|
|
|
if (childPtr != 0)
|
|
|
|
_child = hierNodes + data->readUint32LE();
|
|
|
|
else
|
2014-05-29 22:35:46 +00:00
|
|
|
_child = nullptr;
|
2011-12-19 17:26:48 +00:00
|
|
|
|
|
|
|
if (siblingPtr != 0)
|
|
|
|
_sibling = hierNodes + data->readUint32LE();
|
|
|
|
else
|
2014-05-29 22:35:46 +00:00
|
|
|
_sibling = nullptr;
|
2011-07-22 20:20:48 +00:00
|
|
|
|
|
|
|
_meshVisible = true;
|
|
|
|
_hierVisible = true;
|
|
|
|
_initialized = true;
|
|
|
|
}
|
|
|
|
|
2011-12-12 17:57:17 +00:00
|
|
|
void ModelNode::draw() const {
|
2017-11-28 00:39:55 +00:00
|
|
|
if (_sibling || _child) {
|
|
|
|
translateViewpointStart();
|
|
|
|
}
|
2011-12-12 17:57:17 +00:00
|
|
|
translateViewpoint();
|
|
|
|
if (_hierVisible) {
|
2017-11-28 00:39:55 +00:00
|
|
|
if (_child) {
|
|
|
|
translateViewpointStart();
|
|
|
|
}
|
2011-12-12 17:57:17 +00:00
|
|
|
g_driver->translateViewpoint(_pivot);
|
|
|
|
|
|
|
|
if (!g_driver->isShadowModeActive()) {
|
|
|
|
Sprite *sprite = _sprite;
|
|
|
|
while (sprite) {
|
|
|
|
sprite->draw();
|
|
|
|
sprite = sprite->_next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_mesh && _meshVisible) {
|
|
|
|
_mesh->draw();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_child) {
|
2017-11-28 00:39:55 +00:00
|
|
|
translateViewpointFinish();
|
2011-12-12 17:57:17 +00:00
|
|
|
_child->draw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:39:55 +00:00
|
|
|
if (_sibling || _child) {
|
|
|
|
translateViewpointFinish();
|
|
|
|
}
|
2011-12-12 17:57:17 +00:00
|
|
|
if (_sibling) {
|
|
|
|
_sibling->draw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModelNode::getBoundingBox(int *x1, int *y1, int *x2, int *y2) const {
|
2017-11-28 00:39:55 +00:00
|
|
|
if (_sibling || _child) {
|
|
|
|
translateViewpointStart();
|
|
|
|
}
|
2011-12-12 17:57:17 +00:00
|
|
|
translateViewpoint();
|
|
|
|
if (_hierVisible) {
|
2017-11-28 00:39:55 +00:00
|
|
|
if (_child) {
|
|
|
|
translateViewpointStart();
|
|
|
|
}
|
2011-12-12 17:57:17 +00:00
|
|
|
g_driver->translateViewpoint(_pivot);
|
|
|
|
|
|
|
|
if (_mesh && _meshVisible) {
|
|
|
|
_mesh->getBoundingBox(x1, y1, x2, y2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_child) {
|
2017-11-28 00:39:55 +00:00
|
|
|
translateViewpointFinish();
|
2011-12-12 17:57:17 +00:00
|
|
|
_child->getBoundingBox(x1, y1, x2, y2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:39:55 +00:00
|
|
|
if (_sibling || _child) {
|
|
|
|
translateViewpointFinish();
|
|
|
|
}
|
2011-12-12 17:57:17 +00:00
|
|
|
if (_sibling) {
|
|
|
|
_sibling->getBoundingBox(x1, y1, x2, y2);
|
|
|
|
}
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
2011-07-22 20:09:49 +00:00
|
|
|
void ModelNode::addChild(ModelNode *child) {
|
|
|
|
ModelNode **childPos = &_child;
|
2008-07-30 07:04:32 +00:00
|
|
|
while (*childPos)
|
2004-12-09 23:55:43 +00:00
|
|
|
childPos = &(*childPos)->_sibling;
|
2004-02-24 22:43:32 +00:00
|
|
|
*childPos = child;
|
2004-12-09 23:55:43 +00:00
|
|
|
child->_parent = this;
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
2011-07-22 20:09:49 +00:00
|
|
|
void ModelNode::removeChild(ModelNode *child) {
|
|
|
|
ModelNode **childPos = &_child;
|
2008-07-30 07:04:32 +00:00
|
|
|
while (*childPos && *childPos != child)
|
2004-12-09 23:55:43 +00:00
|
|
|
childPos = &(*childPos)->_sibling;
|
2008-07-30 07:04:32 +00:00
|
|
|
if (*childPos) {
|
2004-12-09 23:55:43 +00:00
|
|
|
*childPos = child->_sibling;
|
2014-05-29 22:35:46 +00:00
|
|
|
child->_parent = nullptr;
|
2004-02-24 22:43:32 +00:00
|
|
|
}
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
2012-02-05 02:07:24 +00:00
|
|
|
void ModelNode::setMatrix(const Math::Matrix4 &matrix) {
|
2004-12-09 23:55:43 +00:00
|
|
|
_matrix = matrix;
|
2011-10-18 15:42:12 +00:00
|
|
|
if (_sibling)
|
|
|
|
_sibling->setMatrix(matrix);
|
2003-08-28 01:55:48 +00:00
|
|
|
}
|
|
|
|
|
2011-07-22 20:09:49 +00:00
|
|
|
void ModelNode::update() {
|
2005-07-21 15:16:35 +00:00
|
|
|
if (!_initialized)
|
|
|
|
return;
|
2009-05-09 17:47:28 +00:00
|
|
|
|
2012-04-08 09:54:42 +00:00
|
|
|
if (_hierVisible && _needsUpdate) {
|
2014-06-25 14:15:41 +00:00
|
|
|
_localMatrix = _animRot.toMatrix();
|
|
|
|
_localMatrix.setPosition(_animPos);
|
2003-08-28 01:55:48 +00:00
|
|
|
|
2011-10-18 15:42:12 +00:00
|
|
|
_matrix = _matrix * _localMatrix;
|
2003-08-28 01:55:48 +00:00
|
|
|
|
2011-10-18 15:42:12 +00:00
|
|
|
_pivotMatrix = _matrix;
|
|
|
|
_pivotMatrix.translate(_pivot);
|
2003-08-28 01:55:48 +00:00
|
|
|
|
2011-10-18 15:42:12 +00:00
|
|
|
if (_mesh) {
|
|
|
|
_mesh->_matrix = _pivotMatrix;
|
|
|
|
}
|
2005-01-11 19:46:47 +00:00
|
|
|
|
2011-10-18 15:42:12 +00:00
|
|
|
if (_child) {
|
|
|
|
_child->setMatrix(_matrix);
|
|
|
|
_child->update();
|
|
|
|
}
|
2012-04-08 09:54:42 +00:00
|
|
|
|
|
|
|
_needsUpdate = false;
|
2005-01-11 19:46:47 +00:00
|
|
|
}
|
2003-08-28 01:55:48 +00:00
|
|
|
|
2011-10-18 15:42:12 +00:00
|
|
|
if (_sibling) {
|
|
|
|
_sibling->update();
|
2005-01-11 19:46:47 +00:00
|
|
|
}
|
2003-08-28 01:55:48 +00:00
|
|
|
}
|
|
|
|
|
2011-07-22 20:09:49 +00:00
|
|
|
void ModelNode::addSprite(Sprite *sprite) {
|
2011-05-15 13:47:09 +00:00
|
|
|
sprite->_next = _sprite;
|
|
|
|
_sprite = sprite;
|
|
|
|
}
|
|
|
|
|
2013-10-25 14:40:30 +00:00
|
|
|
void ModelNode::removeSprite(const Sprite *sprite) {
|
2013-07-09 19:12:55 +00:00
|
|
|
Sprite *curr = _sprite;
|
2014-05-29 22:35:46 +00:00
|
|
|
Sprite *prev = nullptr;
|
2011-05-25 20:01:25 +00:00
|
|
|
while (curr) {
|
|
|
|
if (curr == sprite) {
|
|
|
|
if (prev)
|
|
|
|
prev->_next = curr->_next;
|
|
|
|
else
|
|
|
|
_sprite = curr->_next;
|
2011-05-15 13:47:09 +00:00
|
|
|
}
|
2011-05-25 20:01:25 +00:00
|
|
|
prev = curr;
|
|
|
|
curr = curr->_next;
|
2011-05-15 13:47:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-12 17:57:17 +00:00
|
|
|
void ModelNode::translateViewpoint() const {
|
2014-06-25 14:15:41 +00:00
|
|
|
g_driver->translateViewpoint(_animPos);
|
|
|
|
|
|
|
|
Math::Matrix4 rot = _animRot.toMatrix();
|
|
|
|
rot.transpose();
|
|
|
|
g_driver->rotateViewpoint(rot);
|
2011-12-12 17:57:17 +00:00
|
|
|
}
|
|
|
|
|
2017-11-28 00:39:55 +00:00
|
|
|
void ModelNode::translateViewpointStart() const {
|
|
|
|
g_driver->translateViewpointStart();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModelNode::translateViewpointFinish() const {
|
2011-12-12 17:57:17 +00:00
|
|
|
g_driver->translateViewpointFinish();
|
|
|
|
}
|
|
|
|
|
2009-05-25 06:49:57 +00:00
|
|
|
} // end of namespace Grim
|