Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "engines/grim/debug.h"
00024 #include "engines/grim/model.h"
00025 #include "engines/grim/resource.h"
00026 #include "engines/grim/grim.h"
00027 #include "engines/grim/set.h"
00028 #include "engines/grim/gfx_base.h"
00029 #include "engines/grim/colormap.h"
00030 #include "engines/grim/animation.h"
00031
00032 #include "engines/grim/costume/model_component.h"
00033 #include "engines/grim/costume/main_model_component.h"
00034 #include "engines/grim/costume/mesh_component.h"
00035
00036 namespace Grim {
00037
00038 #define DEFAULT_COLORMAP "item.cmp"
00039
00040 ModelComponent::ModelComponent(Component *p, int parentID, const char *filename, Component *prevComponent, tag32 t) :
00041 Component(p, parentID, filename, t),
00042 _obj(nullptr), _hier(nullptr), _animation(nullptr), _animated(false) {
00043 const char *comma = strchr(filename, ',');
00044
00045
00046
00047
00048
00049 if (comma) {
00050 _name = Common::String(filename, comma);
00051 warning("Comma in model components not supported: %s", filename);
00052 }
00053 _prevComp = prevComponent;
00054 }
00055
00056 ModelComponent::~ModelComponent() {
00057 if (_hier && _hier->_parent) {
00058 _hier->_parent->removeChild(_hier);
00059 }
00060
00061 delete _obj;
00062 delete _animation;
00063 }
00064
00065 void ModelComponent::init() {
00066 if (_prevComp && _prevComp->isComponentType('M','M','D','L')) {
00067 _previousCmap = _prevComp->getCMap();
00068 }
00069
00070
00071
00072 if (!_obj) {
00073 CMapPtr cm = getCMap();
00074
00075
00076
00077 if (!cm && g_grim->getCurrSet())
00078 cm = g_grim->getCurrSet()->getCMap();
00079 if (!cm) {
00080 Debug::warning(Debug::Costumes, "No colormap specified for %s, using %s", _name.c_str(), DEFAULT_COLORMAP);
00081
00082 cm = g_resourceloader->getColormap(DEFAULT_COLORMAP);
00083 }
00084
00085
00086
00087 if (_parent) {
00088 MeshComponent *mc = static_cast<MeshComponent *>(_parent);
00089 _obj = g_resourceloader->loadModel(_name, cm, mc->getModel());
00090 _hier = _obj->getHierarchy();
00091 mc->getNode()->addChild(_hier);
00092 } else {
00093 _obj = g_resourceloader->loadModel(_name, cm);
00094 _hier = _obj->getHierarchy();
00095 Debug::warning(Debug::Costumes, "Parent of model %s wasn't a mesh", _name.c_str());
00096 }
00097
00098
00099
00100 if (_parent)
00101 setKey(0);
00102 else
00103 setKey(1);
00104 }
00105
00106 if (!_animation) {
00107 _animation = new AnimManager();
00108 }
00109 }
00110
00111 void ModelComponent::setKey(int val) {
00112 _visible = (val != 0);
00113 _hier->_hierVisible = _visible;
00114 }
00115
00116 void ModelComponent::reset() {
00117 _visible = false;
00118 _hier->_hierVisible = _visible;
00119 }
00120
00121 AnimManager *ModelComponent::getAnimManager() const {
00122 return _animation;
00123 }
00124
00125 int ModelComponent::update(uint time) {
00126
00127 for (int i = 0; i < getNumNodes(); i++) {
00128 _hier[i]._animPos = _hier[i]._pos;
00129 _hier[i]._animRot = _hier[i]._rot;
00130 }
00131
00132 _animated = false;
00133 return 0;
00134 }
00135
00136 void ModelComponent::animate() {
00137 if (_animated) {
00138 return;
00139 }
00140
00141 _animation->animate(_hier, getNumNodes());
00142 _animated = true;
00143 }
00144
00145 void ModelComponent::resetColormap() {
00146 CMap *cm;
00147
00148 cm = getCMap();
00149 if (_obj && cm)
00150 _obj->reload(cm);
00151 }
00152
00153 void ModelComponent::restoreState(SaveGame *state) {
00154 _hier->_hierVisible = _visible;
00155 }
00156
00157 int ModelComponent::getNumNodes() {
00158 return _obj->getNumNodes();
00159 }
00160
00161 void ModelComponent::translateObject(ModelNode *node, bool reset) {
00162 if (node->_parent)
00163 translateObject(node->_parent, reset);
00164
00165 if (reset) {
00166 node->translateViewpointFinish();
00167 } else {
00168 node->translateViewpointStart();
00169 node->translateViewpoint();
00170 }
00171 }
00172
00173 void ModelComponent::translateObject(bool res) {
00174 ModelNode *node = _hier->_parent;
00175 if (node) {
00176 translateObject(node, res);
00177 }
00178 }
00179
00180 void ModelComponent::draw() {
00181
00182
00183
00184 if (_parent && _parent->isVisible())
00185 return;
00186
00187
00188 translateObject(false);
00189
00190 _hier->draw();
00191
00192
00193 translateObject(true);
00194 }
00195
00196 void ModelComponent::getBoundingBox(int *x1, int *y1, int *x2, int *y2) {
00197
00198
00199
00200 if (_parent && _parent->isVisible())
00201 return;
00202
00203
00204 translateObject(false);
00205
00206 _hier->getBoundingBox(x1, y1, x2, y2);
00207
00208
00209 translateObject(true);
00210 }
00211
00212 }