multimodel.cpp 7.35 KB
Newer Older
1 2 3 4 5 6 7 8 9
////////////////////////////////////////////////////////
//
// GEM - Graphics Environment for Multimedia
//
// zmoelnig@iem.kug.ac.at
//
// Implementation file
//
//    Copyright (c) 1997-1999 Mark Danks.
zmoelnig's avatar
zmoelnig committed
10
//    Copyright (c) Günther Geiger.
zmoelnig's avatar
zmoelnig committed
11
//    Copyright (c) 2001-2011 IOhannes m zmölnig. forum::für::umläute. IEM. zmoelnig@iem.at
12 13 14 15 16
//    For information on usage and redistribution, and for a DISCLAIMER OF ALL
//    WARRANTIES, see the file, "GEM.LICENSE.TERMS" in this distribution.
//
/////////////////////////////////////////////////////////
#include "multimodel.h"
17
#include "Gem/State.h"
18 19
#include <stdio.h>

20
CPPEXTERN_NEW_WITH_FOUR_ARGS(multimodel, t_symbol *, A_DEFSYM, t_floatarg, A_DEFFLOAT, t_floatarg, A_DEFFLOAT, t_floatarg, A_DEFFLOAT);
21 22 23 24 25 26 27 28 29 30

/////////////////////////////////////////////////////////
//
// multimodel
//
/////////////////////////////////////////////////////////
// Constructor
//
/////////////////////////////////////////////////////////
multimodel :: multimodel(t_symbol *filename, t_floatarg baseModel,
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
31
                         t_floatarg topModel, t_floatarg skipRate)
32
  : m_curModel(-1),
zmoelnig's avatar
zmoelnig committed
33
    m_currentH(1.f), m_currentW(1.f)
34 35
{
  inlet_new(this->x_obj, &this->x_obj->ob_pd, &s_float, gensym("mdl_num"));
36
  
37
  // make sure that there are some characters
38 39
  if (filename->s_name[0])
    openMess(filename->s_name, baseModel, topModel, skipRate);
40 41
}

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
42
////////////////////////////////////////////////////////
43 44 45 46 47
// Destructor
//
/////////////////////////////////////////////////////////
multimodel :: ~multimodel()
{
48
  cleanMultimodel();
49 50
}

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
void multimodel :: openMess(const std::string&filename, 
                            float baseModel, float topModel, float skipRate)
{
  int skipRatei=static_cast<int>(skipRate);
  int topModeli=static_cast<int>(topModel);
  int baseModeli=static_cast<int>(baseModel);
  if (skipRatei == 0) {
    if (topModeli == 0)
      open(filename, 0, baseModeli, 1);
    else
      open(filename, baseModeli, topModeli, 1);
  }
  else open(filename, baseModeli, topModeli, skipRatei);
}


67 68 69 70
/////////////////////////////////////////////////////////
// cleanMultimodel
//
/////////////////////////////////////////////////////////
71
void multimodel :: cleanMultimodel(void)
72
{
73 74 75 76 77
  unsigned int i;
  for(i=0; i<m_loaders.size(); i++) {
    if (m_loaders[i])
      delete m_loaders[i];
    m_loaders[i]=NULL;
78
  }
79
  m_loaders.clear();
80 81 82
}

/////////////////////////////////////////////////////////
83
// open
84 85
//
/////////////////////////////////////////////////////////
86
void multimodel :: open(const std::string&filename, int baseModel, int topModel, int skipRate)
87
{
88
  std::vector<gem::plugins::modelloader*>loaders;
89

90
  if (!topModel) {
91
    error("requires an int for number of models");
92 93 94
    return;
  }
  if (baseModel > topModel) {
95
    error("top range less than base model");
96 97 98 99 100
    return;
  }
  if (skipRate < 1) skipRate = 1;
  char preName[256];
  char postName[256];
101

102
  int i = 0;
103
  const char *strPtr = filename.c_str();
104 105 106 107
  while (strPtr[i] && strPtr[i] != '*') {
    preName[i] = strPtr[i];
    i++;
  }
108

109
  if (!strPtr[i]) {
110
    error("unable to find * in file name");
111 112 113
    return;
  }

114
  preName[i] = '\0';
115
  strcpy(postName, &(strPtr[i+1]));
116

117
  // need to figure out how many filenames there are to load
118
  int numModels = (topModel + 1 - baseModel) / skipRate;
119 120 121

  int realNum = baseModel;
  char bufName[MAXPDSTRING];
zmoelnig's avatar
zmoelnig committed
122
  canvas_makefilename(const_cast<t_canvas*>(getCanvas()), preName, bufName, MAXPDSTRING);
123

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
124 125
  char newName[MAXPDSTRING];
  newName[0]=0;
126

127
  for (i = 0; i < numModels; i++, realNum += skipRate) {
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
128 129 130
    snprintf(newName, MAXPDSTRING, "%s%d%s", bufName, realNum, postName);
    newName[MAXPDSTRING-1]=0;
    verbose(1, "trying to load '%s'", newName);
131 132 133 134 135 136 137 138

    gem::plugins::modelloader*loader=gem::plugins::modelloader::getInstance();
    if(!loader) break;

    if(loader->open(newName, m_properties))
      loaders.push_back(loader);
    else {
      delete loader;
139
      break;
140
    }
141 142
  }

143 144
  if(loaders.size()!=numModels) {
    /* outch, something went wrong! */
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
145
    error("failed to load model#%d of %d (%s)...resetting to original models", i, numModels, newName);
146

147 148 149 150 151 152 153
    for(i=0; i<loaders.size(); i++) {
      if(loaders[i])
        delete loaders[i];
      loaders[i]=NULL;
    }
    loaders.clear();
    return;
154 155
  }

156 157 158 159
  cleanMultimodel();
  m_loaders=loaders;
  m_curModel = 0;

160
  post("loaded models: %s %s from %d to %d skipping %d",
161
       bufName, postName, baseModel, topModel, skipRate);
162
  setModified();
163 164
}

165

166 167 168 169 170 171
void multimodel :: applyProperties(void)
{
  unsigned int i;
  for(i=0; i<m_loaders.size(); i++) {
    if(m_loaders[i])
      m_loaders[i]->setProperties(m_properties);
172 173 174
  }
}

175

176 177 178 179 180 181
/////////////////////////////////////////////////////////
// materialMess
//
/////////////////////////////////////////////////////////
void multimodel :: textureMess(int state)
{
182
  std::string textype;
183
  switch(state) {
184
  case 0:
185
    textype="linear";
186
    break;
187
  case 1:
188
    textype="spheremap";
189 190
    break;
  case 2:
191
    textype="UV";
192 193
    break;
  default:
194
    break;
195
  }
196 197 198 199 200 201 202
  if(textype.empty()) {
    m_properties.erase("textype");
  } else {
    gem::any value=textype;
    m_properties.set("textype", value);
  }
  applyProperties();
203 204
}

205 206 207 208 209 210 211 212 213
/////////////////////////////////////////////////////////
// smoothMess
//
/////////////////////////////////////////////////////////
void multimodel :: smoothMess(float fsmooth)
{  
  m_properties.set("smooth", fsmooth);
  applyProperties();
}
214

215 216 217 218 219
/////////////////////////////////////////////////////////
// matrialMess
//
/////////////////////////////////////////////////////////
void multimodel :: rescaleMess(bool state)
220
{
221 222 223
  gem::any value=state;
  m_properties.set("rescale", value);
  applyProperties();
224
}
225

226 227 228 229
/////////////////////////////////////////////////////////
// render
//
/////////////////////////////////////////////////////////
230
void multimodel :: render(GemState *state)
231
{
232 233
  if (0==m_loaders.size() || m_curModel<0 || m_curModel>=m_loaders.size())
    return;
234

235 236 237
  if (state && (m_currentH != state->texCoordX(2) || m_currentW != state->texCoordY(2))) {
    m_currentH = state->texCoordX(2);
    m_currentW = state->texCoordY(2);
238

239 240 241 242
    m_properties.set("texwidth", m_currentW);
    m_properties.set("texheight", m_currentH);
    applyProperties();
  }
243

244
  m_loaders[m_curModel]->render();
245 246 247 248 249 250 251 252
}

/////////////////////////////////////////////////////////
// changeModel
//
/////////////////////////////////////////////////////////
void multimodel :: changeModel(int modelNum)
{
253 254 255 256
  if (modelNum < 0 || modelNum >= m_loaders.size()) {
    error("selection %d out of range: 0..%d", modelNum, m_loaders.size()-1);
    return;
  }
257
  m_curModel = modelNum;
258
  //  setModified();
259 260 261 262 263 264 265 266
}

/////////////////////////////////////////////////////////
// static member function
//
/////////////////////////////////////////////////////////
void multimodel :: obj_setupCallback(t_class *classPtr)
{
zmoelnig's avatar
zmoelnig committed
267
  class_addmethod(classPtr, reinterpret_cast<t_method>(&multimodel::openMessCallback),
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
268
                  gensym("open"), A_SYMBOL, A_FLOAT, A_DEFFLOAT, A_DEFFLOAT, A_NULL);
269 270 271 272 273

  CPPEXTERN_MSG1(classPtr, "mdl_num", changeModel, int);
  CPPEXTERN_MSG1(classPtr, "rescale", rescaleMess, bool);
  CPPEXTERN_MSG1(classPtr, "texture", textureMess, int);
  CPPEXTERN_MSG1(classPtr, "smooth", smoothMess, float);
274
}
275
void multimodel :: openMessCallback(void *data, t_symbol *filesymbol, t_floatarg baseModel,
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
276
                                    t_floatarg topModel, t_floatarg skipRate)
277
{
278
  GetMyClass(data)->openMess(filesymbol->s_name, baseModel, topModel, skipRate);
279
}