CalCoreModel *CharacterModel::loadConfig(std::string filename) { PathHandler ph; std::string pfile; ph.setBaseFile(filename.c_str()); // open the model configuration file std::ifstream file; file.open(filename.c_str(), std::ios::in | std::ios::binary); if(!file) { SWARNING << "Failed to open model configuration file '" << filename << "'." << endLog; return NULL; } CalCoreModel *model = new CalCoreModel("dummy"); // parse all lines from the model configuration file int line; for(line = 1; ; line++) { // read the next model configuration line std::string strBuffer; std::getline(file, strBuffer); // stop if we reached the end of file if(file.eof()) break; // check if an error happend while reading from the file if(!file) { SWARNING << "Error while reading from the model configuration file '" << filename << "'." << endLog; return NULL; } // find the first non-whitespace character std::string::size_type pos; pos = strBuffer.find_first_not_of(" \t"); // check for empty lines if((pos == std::string::npos) || (strBuffer[pos] == '\n') || (strBuffer[pos] == '\r') || (strBuffer[pos] == 0)) continue; // check for comment lines if(strBuffer[pos] == '#') continue; // get the key std::string strKey; strKey = strBuffer.substr(pos, strBuffer.find_first_of(" =\t\n\r", pos) - pos); pos += strKey.size(); // get the '=' character pos = strBuffer.find_first_not_of(" \t", pos); if((pos == std::string::npos) || (strBuffer[pos] != '=')) { SWARNING << filename << "(" << line << "): Invalid syntax." << endLog; return false; } // find the first non-whitespace character after the '=' character pos = strBuffer.find_first_not_of(" \t", pos + 1); // get the data std::string strData; strData = strBuffer.substr(pos, strBuffer.find_first_of("\n\r", pos) - pos); // handle the model creation if(strKey == "scale") { // set rendering scale factor //m_scale = atof(strData.c_str()); } else if(strKey == "skeleton") { pfile = ph.findFile(strData.c_str()); // load core skeleton SINFO << "Loading skeleton '" << pfile << "'..." << endLog; if(!model->loadCoreSkeleton(pfile.c_str())) { CalError::printLastError(); return false; } } else if(strKey == "animation") { pfile = ph.findFile(strData.c_str()); // load core animation SINFO << "Loading animation '" << pfile << "'..." << endLog; if(model->loadCoreAnimation(pfile.c_str()) == -1) { CalError::printLastError(); return false; } } else if(strKey == "mesh") { pfile = ph.findFile(strData.c_str()); // load core mesh SINFO << "Loading mesh '" << pfile << "'..." << endLog; if(model->loadCoreMesh(pfile.c_str()) == -1) { CalError::printLastError(); return false; } } else if(strKey == "material") { pfile = ph.findFile(strData.c_str()); // load core material SINFO << "Loading material '" << pfile << "'..." << endLog; if(model->loadCoreMaterial(pfile.c_str()) == -1) { CalError::printLastError(); return false; } } else { // everything else triggers an error message, but is ignored SWARNING << filename << "(" << line << "): Invalid syntax." << endLog; } } // create material threads int mid; for(mid = 0; mid < model->getCoreMaterialCount(); mid++) { model->createCoreMaterialThread(mid); model->setCoreMaterialId(mid, 0, mid); } file.close(); return model; }
void CharacterModel::convertMaterials(std::string configfile) { getMaterials().clear(); UInt32 mcnt = 0; PathHandler ph; ph.setBaseFile(configfile.c_str()); for(int mid = 0; mid < _coreModel->getCoreMaterialCount(); mid++) { CalCoreMaterial *coremat = _coreModel->getCoreMaterial(mid); SimpleMaterialPtr mat = SimpleMaterial::create(); beginEditCP(mat); CalCoreMaterial::Color &calamb = coremat->getAmbientColor(); CalCoreMaterial::Color &caldif = coremat->getDiffuseColor(); CalCoreMaterial::Color &calspec = coremat->getSpecularColor(); mat->setAmbient(Color3f(calamb.red / 255.0f, calamb.green / 255.0f, calamb.blue / 255.0f)); mat->setDiffuse(Color3f(caldif.red / 255.0f, caldif.green / 255.0f, caldif.blue / 255.0f)); mat->setSpecular(Color3f(calspec.red / 255.0f, calspec.green / 255.0f, calspec.blue / 255.0f)); mat->setShininess(coremat->getShininess() * 100.f); mat->setLit(true); mat->setColorMaterial(GL_NONE); for(int mapId = 0; mapId < coremat->getMapCount(); mapId++) { std::string file = coremat->getMapFilename(mapId); std::string pfile = ph.findFile(file.c_str()); SINFO << "Loading texture '" << pfile << "'..." << endLog; ImagePtr img = Image::create(); if(!img->read(pfile.c_str())) { SWARNING << "CharacterModel::convertMaterials: error " << "loading image " << file << endLog; } else { // amz with my test scene paladin.cfg all textures were // upside down so I disabled the vertical flipping perhaps // they fixed the bug in Cal3D? #if 0 beginEditCP(img); { // For some reason Cal3D expects textures upside down ??? UInt32 bpl = img->getBpp() * img->getWidth(); UChar8 *t = img->getData(), *b = t + (img->getHeight() - 1) * bpl, dum; for(UInt32 y = img->getHeight() / 2; y > 0; --y) { for(UInt32 x = bpl; x > 0; --x, ++t, ++b) { dum = *t; *t = *b; *b = dum; } b -= bpl * 2; } } endEditCP(img); #endif TextureChunkPtr tex = TextureChunk::create(); beginEditCP(tex); tex->setImage(img); tex->setEnvMode(GL_MODULATE); endEditCP(tex); mat->addChunk(tex); } } endEditCP(mat); coremat->setUserData((Cal::UserData)mcnt); getMaterials().push_back(mat); mcnt ++; } }
void TiledImageBlockAccessor::open(const Char8 *szFilename) { fprintf(stderr, "Open Tiled %s\n", szFilename); FILE *pFile = fopen(szFilename, "r"); Char8 szRow[1024]; const Char8 *szDelim = " \t\n "; std::string szBuffer; if(pFile != NULL) { while(!feof(pFile)) { fgets(szRow, 1024, pFile); szBuffer.append(szRow); } PathHandler *pOrgHandler = ImageFileHandler::the()->getPathHandler(); PathHandler tmpHandler; tmpHandler.setBaseFile(szFilename); ImageFileHandler::the()->setPathHandler(&tmpHandler); fprintf(stderr, "got %s\n", szBuffer.c_str()); string_token_iterator cIt (szBuffer, szDelim); string_token_iterator cEnd; _uiColumns = TypeTraits<UInt32>::getFromCString((*cIt).c_str()); fprintf(stderr, "C: %d\n", _uiColumns); ++cIt; _uiRows = TypeTraits<UInt32>::getFromCString((*cIt).c_str()); fprintf(stderr, "R: %d\n", _uiRows); ++cIt; for(UInt32 i = 0; i < _uiRows * _uiColumns; ++i) { if(cIt == cEnd) { break; } fprintf(stderr, "File -%s-\n", (*cIt).c_str()); ImageBlockAccessorPtr pAccess; if(strncmp((*cIt).c_str(), "default:", 7) == 0) { DefaultBlockAccessorPtr pDefAccess( new DefaultBlockAccessor()); pAccess = pDefAccess; pDefAccess->open((*cIt).c_str()); } else { pAccess = ImageFileHandler::the()->open((*cIt).c_str()); } _vImages.push_back(pAccess); ++cIt; } ImageFileHandler::the()->setPathHandler(pOrgHandler); if(_vImages.size() != _uiRows * _uiColumns || _vImages.size() == 0) { fprintf(stderr, "Images missing %" PRISize " %d\n", _vImages.size(), _uiRows * _uiColumns); _vImages.clear(); return; } _vSampleDescs.resize(_vImages.size()); UInt32 uiIdx = 0; UInt32 uiOtherIdx = 0; _vSampleDescs[0].setBounds( 0, 0, _vImages[0]->getSize()[0], _vImages[0]->getSize()[1]); for(UInt32 i = 1; i < _uiRows; ++i) { uiIdx = i * _uiColumns; uiOtherIdx = uiIdx - _uiColumns; _vSampleDescs[uiIdx].setBounds( _vSampleDescs[uiOtherIdx].x0, _vSampleDescs[uiOtherIdx].y1, _vImages [uiIdx ]->getSize()[0], _vImages [uiIdx ]->getSize()[1]); } for(UInt32 i = 1; i < _uiColumns; ++i) { _vSampleDescs[i].setBounds( _vSampleDescs[i - 1].x1, _vSampleDescs[i - 1].y0, _vImages [i ]->getSize()[0], _vImages [i ]->getSize()[1]); } for(UInt32 i = 1; i < _uiRows; ++i) { for(UInt32 j = 1; j < _uiColumns; ++j) { uiIdx = i * _uiColumns + j; uiOtherIdx = (i - 1) * _uiColumns + (j - 1); _vSampleDescs[uiIdx].setBounds( _vSampleDescs[uiOtherIdx].x1, _vSampleDescs[uiOtherIdx].y1, _vImages [uiIdx ]->getSize()[0], _vImages [uiIdx ]->getSize()[1]); } } for(UInt32 i = 0; i < _uiRows; ++i) { for(UInt32 j = 0; j < _uiColumns; ++j) { uiIdx = i * _uiColumns + j; fprintf(stderr, "(%d)(%d %d) | %d %d %d %d\n", uiIdx, i, j, _vSampleDescs[uiIdx].x0, _vSampleDescs[uiIdx].y0, _vSampleDescs[uiIdx].x1, _vSampleDescs[uiIdx].y1); } } _vSize.setValues(_vSampleDescs.back().x1, _vSampleDescs.back().y1); GeoReferenceAttachment *pFirstRef = _vImages.front()->getGeoRef(); if(pFirstRef != NULL) { _pGeoRef = GeoReferenceAttachment::create(); _pGeoRef->setOrigin (pFirstRef->getOrigin ()); _pGeoRef->setPixelSize (pFirstRef->getPixelSize ()); _pGeoRef->setEllipsoidAxis(pFirstRef->getEllipsoidAxis()); _pGeoRef->setDatum (pFirstRef->getDatum ()); _pGeoRef->setNoDataValue (pFirstRef->getNoDataValue ()); } _eImgType = _vImages.front()->getType (); _eImgFormat = _vImages.front()->getFormat(); _fNoDataValue = _vImages.front()->getNoDataValue(); fclose(pFile); } }