multimodel.cpp 7.28 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
  for (i = 0; i < numModels; i++, realNum += skipRate) {
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
127 128
    snprintf(newName, MAXPDSTRING, "%s%d%s", bufName, realNum, postName);
    newName[MAXPDSTRING-1]=0;
129

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
130
    verbose(1, "trying to load '%s'", newName);
131 132 133 134 135
    loaders.push_back(gem::plugins::modelloader::getInstance());
    if(!loaders[i])
      break;
    if(!loaders[i]->open(newName, m_properties))
      break;
136 137
  }

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

142 143 144 145 146 147 148
    for(i=0; i<loaders.size(); i++) {
      if(loaders[i])
        delete loaders[i];
      loaders[i]=NULL;
    }
    loaders.clear();
    return;
149 150
  }

151 152 153 154
  cleanMultimodel();
  m_loaders=loaders;
  m_curModel = 0;

155
  post("loaded models: %s %s from %d to %d skipping %d",
156
       bufName, postName, baseModel, topModel, skipRate);
157
  setModified();
158 159
}

160

161 162 163 164 165 166
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);
167 168 169
  }
}

170

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

200 201 202 203 204 205 206 207 208
/////////////////////////////////////////////////////////
// smoothMess
//
/////////////////////////////////////////////////////////
void multimodel :: smoothMess(float fsmooth)
{  
  m_properties.set("smooth", fsmooth);
  applyProperties();
}
209

210 211 212 213 214
/////////////////////////////////////////////////////////
// matrialMess
//
/////////////////////////////////////////////////////////
void multimodel :: rescaleMess(bool state)
215
{
216 217 218
  gem::any value=state;
  m_properties.set("rescale", value);
  applyProperties();
219
}
220

221 222 223 224
/////////////////////////////////////////////////////////
// render
//
/////////////////////////////////////////////////////////
225
void multimodel :: render(GemState *state)
226
{
227 228
  if (0==m_loaders.size() || m_curModel<0 || m_curModel>=m_loaders.size())
    return;
229

230 231 232
  if (state && (m_currentH != state->texCoordX(2) || m_currentW != state->texCoordY(2))) {
    m_currentH = state->texCoordX(2);
    m_currentW = state->texCoordY(2);
233

234 235 236 237
    m_properties.set("texwidth", m_currentW);
    m_properties.set("texheight", m_currentH);
    applyProperties();
  }
238

239
  m_loaders[m_curModel]->render();
240 241 242 243 244 245 246 247
}

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

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

  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);
269
}
270
void multimodel :: openMessCallback(void *data, t_symbol *filesymbol, t_floatarg baseModel,
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
271
                                    t_floatarg topModel, t_floatarg skipRate)
272
{
273
  GetMyClass(data)->openMess(filesymbol->s_name, baseModel, topModel, skipRate);
274
}