scummvm/engine/model.cpp

511 lines
15 KiB
C++
Raw Normal View History

2006-04-02 14:20:45 +00:00
/* Residual - Virtual machine to run LucasArts' 3D adventure games
* Copyright (C) 2003-2006 The ScummVM-Residual Team (www.scummvm.org)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
* $URL$
* $Id$
*
*/
2003-08-15 18:00:22 +00:00
#include "common/sys.h"
#include "common/endian.h"
#include "common/debug.h"
#include "engine/model.h"
#include "engine/resource.h"
#include "engine/material.h"
#include "engine/textsplit.h"
#include "engine/backend/driver.h"
2005-01-01 12:27:57 +00:00
2003-08-15 18:00:22 +00:00
#include <cstring>
Model::Model(const char *filename, const char *data, int len, const CMap &cmap) :
Resource(filename), _numMaterials(0), _numGeosets(0) {
if (len >= 4 && std::memcmp(data, "LDOM", 4) == 0)
loadBinary(data, cmap);
else {
TextSplitter ts(data, len);
loadText(ts, cmap);
}
2003-08-15 18:00:22 +00:00
}
void Model::reload(const CMap &cmap) {
// Load the new colormap
for (int i = 0; i < _numMaterials; i++)
_materials[i] = g_resourceloader->loadMaterial(_materialNames[i], cmap);
for (int i = 0; i < _numGeosets; i++)
_geosets[i].changeMaterials(_materials);
}
void Model::loadBinary(const char *data, const CMap &cmap) {
2004-12-09 23:55:43 +00:00
_numMaterials = READ_LE_UINT32(data + 4);
data += 8;
2004-12-09 23:55:43 +00:00
_materials = new ResPtr<Material>[_numMaterials];
_materialNames = new char[_numMaterials][32];
2004-12-09 23:55:43 +00:00
for (int i = 0; i < _numMaterials; i++) {
strcpy(_materialNames[i], data);
_materials[i] = g_resourceloader->loadMaterial(_materialNames[i], cmap);
data += 32;
}
data += 32; // skip name
2004-12-09 23:55:43 +00:00
_numGeosets = READ_LE_UINT32(data + 4);
data += 8;
2004-12-09 23:55:43 +00:00
_geosets = new Geoset[_numGeosets];
for (int i = 0; i < _numGeosets; i++)
_geosets[i].loadBinary(data, _materials);
_numHierNodes = READ_LE_UINT32(data + 4);
data += 8;
2004-12-09 23:55:43 +00:00
_rootHierNode = new HierNode[_numHierNodes];
for (int i = 0; i < _numHierNodes; i++)
_rootHierNode[i].loadBinary(data, _rootHierNode, _geosets[0]);
_radius = get_float(data);
_insertOffset = get_vector3d(data + 40);
2003-08-15 18:00:22 +00:00
}
Model::~Model() {
2004-12-09 23:55:43 +00:00
delete[] _materials;
delete[] _materialNames;
2004-12-09 23:55:43 +00:00
delete[] _geosets;
delete[] _rootHierNode;
2003-08-15 18:00:22 +00:00
}
void Model::Geoset::loadBinary(const char *&data, ResPtr<Material> *materials) {
2004-12-09 23:55:43 +00:00
_numMeshes = READ_LE_UINT32(data);
data += 4;
2004-12-09 23:55:43 +00:00
_meshes = new Mesh[_numMeshes];
for (int i = 0; i < _numMeshes; i++)
_meshes[i].loadBinary(data, materials);
2003-08-15 18:00:22 +00:00
}
Model::Geoset::~Geoset() {
2004-12-09 23:55:43 +00:00
delete[] _meshes;
2003-08-15 18:00:22 +00:00
}
void Model::Mesh::loadBinary(const char *&data, ResPtr<Material> *materials) {
2004-12-09 23:55:43 +00:00
memcpy(_name, data, 32);
_geometryMode = READ_LE_UINT32(data + 36);
_lightingMode = READ_LE_UINT32(data + 40);
_textureMode = READ_LE_UINT32(data + 44);
_numVertices = READ_LE_UINT32(data + 48);
_numTextureVerts = READ_LE_UINT32(data + 52);
_numFaces = READ_LE_UINT32(data + 56);
_vertices = new float[3 * _numVertices];
_verticesI = new float[_numVertices];
_vertNormals = new float[3 * _numVertices];
_textureVerts = new float[2 * _numTextureVerts];
data += 60;
2004-12-09 23:55:43 +00:00
for (int i = 0; i < 3 * _numVertices; i++) {
_vertices[i] = get_float(data);
data += 4;
}
2004-12-09 23:55:43 +00:00
for (int i = 0; i < 2 * _numTextureVerts; i++) {
_textureVerts[i] = get_float(data);
data += 4;
}
2004-12-09 23:55:43 +00:00
for (int i = 0; i < _numVertices; i++) {
_verticesI[i] = get_float(data);
data += 4;
}
2004-12-09 23:55:43 +00:00
data += _numVertices * 4;
_faces = new Face[_numFaces];
_materialid = new int[_numFaces];
2004-12-09 23:55:43 +00:00
for (int i = 0; i < _numFaces; i++)
_materialid[i] = _faces[i].loadBinary(data, materials);
2004-12-09 23:55:43 +00:00
_vertNormals = new float[3 * _numVertices];
for (int i = 0; i < 3 * _numVertices; i++) {
_vertNormals[i] = get_float(data);
data += 4;
}
2004-12-09 23:55:43 +00:00
_shadow = READ_LE_UINT32(data);
_radius = get_float(data + 8);
data += 36;
2003-08-15 18:00:22 +00:00
}
Model::Mesh::~Mesh() {
2004-12-09 23:55:43 +00:00
delete[] _vertices;
delete[] _verticesI;
delete[] _vertNormals;
delete[] _textureVerts;
delete[] _faces;
2003-08-15 18:00:22 +00:00
}
2003-08-31 13:20:28 +00:00
void Model::Mesh::update() {
}
void Model::Face::changeMaterial(ResPtr<Material> material) {
_material = material;
}
int Model::Face::loadBinary(const char *&data, ResPtr<Material> *materials) {
2004-12-09 23:55:43 +00:00
_type = READ_LE_UINT32(data + 4);
_geo = READ_LE_UINT32(data + 8);
_light = READ_LE_UINT32(data + 12);
_tex = READ_LE_UINT32(data + 16);
_numVertices = READ_LE_UINT32(data + 20);
int texPtr = READ_LE_UINT32(data + 28);
int materialPtr = READ_LE_UINT32(data + 32);
2004-12-09 23:55:43 +00:00
_extraLight = get_float(data + 48);
_normal = get_vector3d(data + 64);
data += 76;
2004-12-09 23:55:43 +00:00
_vertices = new int[_numVertices];
for (int i = 0; i < _numVertices; i++) {
_vertices[i] = READ_LE_UINT32(data);
data += 4;
}
2005-01-12 23:28:47 +00:00
if (texPtr == 0)
2004-12-09 23:55:43 +00:00
_texVertices = NULL;
else {
2004-12-09 23:55:43 +00:00
_texVertices = new int[_numVertices];
for (int i = 0; i < _numVertices; i++) {
_texVertices[i] = READ_LE_UINT32(data);
data += 4;
}
}
if (materialPtr == 0)
2004-12-09 23:55:43 +00:00
_material = 0;
else {
2004-12-09 23:55:43 +00:00
_material = materials[READ_LE_UINT32(data)];
materialPtr = READ_LE_UINT32(data);
data += 4;
}
return materialPtr;
2003-08-15 18:00:22 +00:00
}
Model::Face::~Face() {
2004-12-09 23:55:43 +00:00
delete[] _vertices;
delete[] _texVertices;
2003-08-15 18:00:22 +00:00
}
2004-04-19 09:56:34 +00:00
void Model::HierNode::loadBinary(const char *&data, Model::HierNode *hierNodes, const Geoset &g) {
2004-12-09 23:55:43 +00:00
memcpy(_name, data, 64);
_flags = READ_LE_UINT32(data + 64);
_type = READ_LE_UINT32(data + 72);
2004-04-19 09:56:34 +00:00
int meshNum = READ_LE_UINT32(data + 76);
if (meshNum < 0)
2004-12-09 23:55:43 +00:00
_mesh = NULL;
2004-04-19 09:56:34 +00:00
else
2004-12-09 23:55:43 +00:00
_mesh = g._meshes + meshNum;
_depth = READ_LE_UINT32(data + 80);
2004-04-19 09:56:34 +00:00
int parentPtr = READ_LE_UINT32(data + 84);
2004-12-09 23:55:43 +00:00
_numChildren = READ_LE_UINT32(data + 88);
2004-04-19 09:56:34 +00:00
int childPtr = READ_LE_UINT32(data + 92);
int siblingPtr = READ_LE_UINT32(data + 96);
2004-12-09 23:55:43 +00:00
_pivot = get_vector3d(data + 100);
_pos = get_vector3d(data + 112);
_pitch = get_float(data + 124);
_yaw = get_float(data + 128);
_roll = get_float(data + 132);
_animPos = _pos;
_animPitch = _pitch;
_animYaw = _yaw;
_animRoll = _roll;
_priority = -1;
_totalWeight = 1;
2004-04-19 09:56:34 +00:00
data += 184;
if (parentPtr != 0) {
2004-12-09 23:55:43 +00:00
_parent = hierNodes + READ_LE_UINT32(data);
2004-04-19 09:56:34 +00:00
data += 4;
} else
2004-12-09 23:55:43 +00:00
_parent = NULL;
2004-04-19 09:56:34 +00:00
if (childPtr != 0) {
2004-12-09 23:55:43 +00:00
_child = hierNodes + READ_LE_UINT32(data);
2004-04-19 09:56:34 +00:00
data += 4;
} else
2004-12-09 23:55:43 +00:00
_child = NULL;
2004-04-19 09:56:34 +00:00
if (siblingPtr != 0) {
2004-12-09 23:55:43 +00:00
_sibling = hierNodes + READ_LE_UINT32(data);
2004-04-19 09:56:34 +00:00
data += 4;
} else
2004-12-09 23:55:43 +00:00
_sibling = NULL;
2004-12-09 23:55:43 +00:00
_meshVisible = true;
_hierVisible = true;
_totalWeight = 1;
_initialized = true;
2004-04-19 09:56:34 +00:00
}
2003-08-15 18:00:22 +00:00
void Model::draw() const {
2004-12-09 23:55:43 +00:00
_rootHierNode->draw();
2003-08-15 18:00:22 +00:00
}
Model::HierNode *Model::copyHierarchy() {
2004-12-09 23:55:43 +00:00
HierNode *result = new HierNode[_numHierNodes];
std::memcpy(result, _rootHierNode, _numHierNodes * sizeof(HierNode));
// Now adjust pointers
2004-12-09 23:55:43 +00:00
for (int i = 0; i < _numHierNodes; i++) {
if (result[i]._parent != NULL)
result[i]._parent = result + (_rootHierNode[i]._parent - _rootHierNode);
if (result[i]._child != NULL)
result[i]._child = result + (_rootHierNode[i]._child - _rootHierNode);
if (result[i]._sibling != NULL)
result[i]._sibling = result + (_rootHierNode[i]._sibling - _rootHierNode);
}
return result;
2003-08-15 18:00:22 +00:00
}
void Model::loadText(TextSplitter &ts, const CMap &cmap) {
ts.expectString("section: header");
int major, minor;
ts.scanString("3do %d.%d", 2, &major, &minor);
ts.expectString("section: modelresource");
2004-12-09 23:55:43 +00:00
ts.scanString("materials %d", 1, &_numMaterials);
_materials = new ResPtr<Material>[_numMaterials];
_materialNames = new char[_numMaterials][32];
2004-12-09 23:55:43 +00:00
for (int i = 0; i < _numMaterials; i++) {
char materialName[32];
int num;
ts.scanString("%d: %32s", 2, &num, materialName);
_materials[num] = g_resourceloader->loadMaterial(materialName, cmap);
strcpy(_materialNames[num], materialName);
}
ts.expectString("section: geometrydef");
2004-12-09 23:55:43 +00:00
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);
_geosets = new Geoset[_numGeosets];
for (int i = 0; i < _numGeosets; i++) {
int num;
ts.scanString("geoset %d", 1, &num);
2004-12-09 23:55:43 +00:00
_geosets[num].loadText(ts, _materials);
}
ts.expectString("section: hierarchydef");
2004-12-09 23:55:43 +00:00
ts.scanString("hierarchy nodes %d", 1, &_numHierNodes);
_rootHierNode = new HierNode[_numHierNodes];
for (int i = 0; i < _numHierNodes; i++) {
int num, flags, type, mesh, parent, child, sibling, numChildren;
float x, y, z, pitch, yaw, roll, pivotx, pivoty, pivotz;
char name[64];
ts.scanString(" %d: %i %i %d %d %d %d %d %f %f %f %f %f %f %f %f %f %64s",
18, &num, &flags, &type, &mesh, &parent, &child, &sibling,
2004-12-09 23:55:43 +00:00
&numChildren, &x, &y, &z, &pitch, &yaw, &roll, &pivotx, &pivoty, &pivotz, name);
_rootHierNode[num]._flags = flags;
_rootHierNode[num]._type = type;
if (mesh < 0)
2004-12-09 23:55:43 +00:00
_rootHierNode[num]._mesh = NULL;
else
2004-12-09 23:55:43 +00:00
_rootHierNode[num]._mesh = _geosets[0]._meshes + mesh;
if (parent >= 0) {
2004-12-09 23:55:43 +00:00
_rootHierNode[num]._parent = _rootHierNode + parent;
_rootHierNode[num]._depth = _rootHierNode[parent]._depth + 1;
} else {
2004-12-09 23:55:43 +00:00
_rootHierNode[num]._parent = NULL;
_rootHierNode[num]._depth = 0;
}
if (child >= 0)
2004-12-09 23:55:43 +00:00
_rootHierNode[num]._child = _rootHierNode + child;
else
2004-12-09 23:55:43 +00:00
_rootHierNode[num]._child = NULL;
if (sibling >= 0)
2004-12-09 23:55:43 +00:00
_rootHierNode[num]._sibling = _rootHierNode + sibling;
else
2004-12-09 23:55:43 +00:00
_rootHierNode[num]._sibling = NULL;
_rootHierNode[num]._numChildren = numChildren;
_rootHierNode[num]._pos = Vector3d(x, y, z);
_rootHierNode[num]._pitch = pitch;
_rootHierNode[num]._yaw = yaw;
_rootHierNode[num]._roll = roll;
_rootHierNode[num]._pivot = Vector3d(pivotx, pivoty, pivotz);
_rootHierNode[num]._meshVisible = true;
_rootHierNode[num]._hierVisible = true;
_rootHierNode[num]._totalWeight = 1;
}
2004-04-19 09:56:34 +00:00
if (!ts.eof() && (debugLevel == DEBUG_WARN || debugLevel == DEBUG_ALL))
2004-04-19 09:56:34 +00:00
warning("Unexpected junk at end of model text\n");
2003-08-15 18:00:22 +00:00
}
void Model::Geoset::changeMaterials(ResPtr<Material> *materials) {
for (int i = 0; i < _numMeshes; i++)
_meshes[i].changeMaterials(materials);
}
2003-08-15 18:00:22 +00:00
void Model::Geoset::loadText(TextSplitter &ts, ResPtr<Material> *materials) {
2004-12-09 23:55:43 +00:00
ts.scanString("meshes %d", 1, &_numMeshes);
_meshes = new Mesh[_numMeshes];
for (int i = 0; i < _numMeshes; i++) {
int num;
ts.scanString("mesh %d", 1, &num);
2004-12-09 23:55:43 +00:00
_meshes[num].loadText(ts, materials);
}
2003-08-15 18:00:22 +00:00
}
void Model::Mesh::changeMaterials(ResPtr<Material> *materials) {
for (int i = 0; i < _numFaces; i++)
_faces[i].changeMaterial(materials[_materialid[i]]);
}
2003-08-15 18:00:22 +00:00
void Model::Mesh::loadText(TextSplitter &ts, ResPtr<Material> *materials) {
2004-12-09 23:55:43 +00:00
ts.scanString("name %32s", 1, _name);
ts.scanString("radius %f", 1, &_radius);
// In data001/rope_scale.3do, the shadow line is missing
2004-12-09 23:55:43 +00:00
if (std::sscanf(ts.currentLine(), "shadow %d", &_shadow) < 1) {
_shadow = 0;
if (debugLevel == DEBUG_WARN || debugLevel == DEBUG_ALL)
warning("Missing shadow directive in model\n");
} else
ts.nextLine();
2004-12-09 23:55:43 +00:00
ts.scanString("geometrymode %d", 1, &_geometryMode);
ts.scanString("lightingmode %d", 1, &_lightingMode);
ts.scanString("texturemode %d", 1, &_textureMode);
ts.scanString("vertices %d", 1, &_numVertices);
_vertices = new float[3 * _numVertices];
_verticesI = new float[_numVertices];
_vertNormals = new float[3 * _numVertices];
for (int i = 0; i < _numVertices; i++) {
int num;
float x, y, z, ival;
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-12-09 23:55:43 +00:00
ts.scanString("texture vertices %d", 1, &_numTextureVerts);
_textureVerts = new float[2 * _numTextureVerts];
2004-12-09 23:55:43 +00:00
for (int i = 0; i < _numTextureVerts; i++) {
int num;
float x, y;
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;
}
ts.expectString("vertex normals");
2004-12-09 23:55:43 +00:00
for (int i = 0; i < _numVertices; i++) {
int num;
float x, y, z;
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-12-09 23:55:43 +00:00
ts.scanString("faces %d", 1, &_numFaces);
_faces = new Face[_numFaces];
_materialid = new int[_numFaces];
2004-12-09 23:55:43 +00:00
for (int i = 0; i < _numFaces; i++) {
int num, materialid, type, geo, light, tex, verts;
float extralight;
int readlen;
2004-12-09 23:55:43 +00:00
if (ts.eof())
error("Expected face data, got EOF\n");
2004-12-09 23:55:43 +00:00
if (std::sscanf(ts.currentLine(), " %d: %d %i %d %d %d %f %d%n", &num, &materialid, &type, &geo, &light, &tex, &extralight, &verts, &readlen) < 8)
2004-12-09 23:55:43 +00:00
error("Expected face data, got `%s'\n", ts.currentLine());
_materialid[num] = materialid;
_faces[num]._material = materials[_materialid[num]];
2004-12-09 23:55:43 +00:00
_faces[num]._type = type;
_faces[num]._geo = geo;
_faces[num]._light = light;
_faces[num]._tex = tex;
_faces[num]._extraLight = extralight;
_faces[num]._numVertices = verts;
_faces[num]._vertices = new int[verts];
_faces[num]._texVertices = new int[verts];
for (int j = 0; j < verts; j++) {
int readlen2;
2004-12-09 23:55:43 +00:00
if (std::sscanf(ts.currentLine() + readlen, " %d, %d%n", _faces[num]._vertices + j, _faces[num]._texVertices + j, &readlen2) < 2)
2004-12-09 23:55:43 +00:00
error("Could not read vertex indices in line `%s'\n",
ts.currentLine());
readlen += readlen2;
}
ts.nextLine();
}
ts.expectString("face normals");
2004-12-09 23:55:43 +00:00
for (int i = 0; i < _numFaces; i++) {
int num;
float x, y, z;
ts.scanString(" %d: %f %f %f", 4, &num, &x, &y, &z);
2004-12-09 23:55:43 +00:00
_faces[num]._normal = Vector3d(x, y, z);
}
2003-08-15 18:00:22 +00:00
}
void Model::HierNode::draw() const {
2004-04-19 09:56:34 +00:00
g_driver->drawHierachyNode(this);
2003-08-15 18:00:22 +00:00
}
void Model::HierNode::addChild(HierNode *child) {
2004-12-09 23:55:43 +00:00
HierNode **childPos = &_child;
while (*childPos != NULL)
2004-12-09 23:55:43 +00:00
childPos = &(*childPos)->_sibling;
*childPos = child;
2004-12-09 23:55:43 +00:00
child->_parent = this;
2003-08-15 18:00:22 +00:00
}
void Model::HierNode::removeChild(HierNode *child) {
2004-12-09 23:55:43 +00:00
HierNode **childPos = &_child;
while (*childPos != NULL && *childPos != child)
2004-12-09 23:55:43 +00:00
childPos = &(*childPos)->_sibling;
if (*childPos != NULL) {
2004-12-09 23:55:43 +00:00
*childPos = child->_sibling;
child->_parent = NULL;
}
2003-08-15 18:00:22 +00:00
}
void Model::HierNode::setMatrix(Matrix4 matrix) {
2004-12-09 23:55:43 +00:00
_matrix = matrix;
}
void Model::HierNode::update() {
if (!_initialized)
return;
2004-12-09 23:55:43 +00:00
_localMatrix._pos.set(_animPos.x() / _totalWeight, _animPos.y() / _totalWeight, _animPos.z() / _totalWeight);
_localMatrix._rot.buildFromPitchYawRoll(_animPitch / _totalWeight, _animYaw / _totalWeight, _animRoll / _totalWeight);
2004-12-09 23:55:43 +00:00
_matrix *= _localMatrix;
2004-12-09 23:55:43 +00:00
_pivotMatrix = _matrix;
2005-01-11 19:46:47 +00:00
_pivotMatrix.translate(_pivot.x(), _pivot.y(), _pivot.z());
if (_mesh != NULL ) {
_mesh->_matrix = _pivotMatrix;
}
2005-01-11 19:46:47 +00:00
if (_child != NULL) {
_child->setMatrix(_matrix);
_child->update();
}
}
2003-08-15 18:00:22 +00:00
void Model::Mesh::draw() const {
2004-12-09 23:55:43 +00:00
for (int i = 0; i < _numFaces; i++)
_faces[i].draw(_vertices, _vertNormals, _textureVerts);
2004-04-19 09:56:34 +00:00
}
2003-08-15 18:00:22 +00:00
void Model::Face::draw(float *vertices, float *vertNormals, float *textureVerts) const {
2004-12-09 23:55:43 +00:00
_material->select();
2004-04-19 09:56:34 +00:00
g_driver->drawModelFace(this, vertices, vertNormals, textureVerts);
2003-08-15 18:00:22 +00:00
}