// A font is specified by two files: a TGA file with the rendered // chars for the font, and a XML file which contains global info // about the font and the texture coordinates and width of each char // The parameter fontName is the filename without extension. // It is assumed that the files are "fontName.xml" and "fontName.tga" bool VSFontLib::load(std::string fontName) { // Test if image file exists FILE *fp; std::string s; s = fontName + ".tga"; fp = fopen(s.c_str(),"r"); if (fp == NULL) { VSResourceLib::sLogError.addMessage("Unable to find font texture: %s", s.c_str()); return false; } mFontTex = loadRGBATexture(s); s = fontName + ".xml"; TiXmlDocument doc(s.c_str()); bool loadOK = doc.LoadFile(); if (!loadOK) { VSResourceLib::sLogError.addMessage("Problem reading the XML font definition file: %s", s.c_str()); return false; } TiXmlHandle hDoc(&doc); TiXmlHandle hRoot(0); TiXmlElement *pElem; pElem = hDoc.FirstChildElement().Element(); if (0 == pElem) return false; hRoot = TiXmlHandle(pElem); pElem->QueryIntAttribute("numchars",&mNumChars); if (mNumChars == 0) return false; hRoot = hRoot.FirstChild("characters"); pElem = hRoot.FirstChild("chardata").Element(); if (pElem) pElem->QueryIntAttribute("hgt",&mHeight); VSFLChar aChar; int charCode, numChars = 0; for(; 0 != pElem; pElem = pElem->NextSiblingElement(), ++numChars) { pElem->QueryIntAttribute("char",&charCode); pElem->QueryIntAttribute("wid",&(aChar.width)); pElem->QueryFloatAttribute("X1", &(aChar.x1)); pElem->QueryFloatAttribute("X2", &(aChar.x2)); pElem->QueryFloatAttribute("Y1", &(aChar.y1)); pElem->QueryFloatAttribute("Y2", &(aChar.y2)); pElem->QueryIntAttribute("A", &(aChar.A)); pElem->QueryIntAttribute("C", &(aChar.C)); mChars[(unsigned char)charCode] = aChar; } VSResourceLib::sLogInfo.addMessage("Font has %d chars", numChars); return true; }
void VSResSurfRevLib::addTexture(unsigned int unit, std::string filename) { int textID = loadRGBATexture(filename, true); mMyMesh[objId].texUnits[unit] = textID; mMyMesh[objId].texTypes[unit] = GL_TEXTURE_2D; mMyMesh[objId].mat.texCount = 1; }
void VSResModelLib::addTexture(unsigned int unit, std::string filename) { int textID = loadRGBATexture(filename, true); for (unsigned int i = 0; i < mMyMeshes.size(); ++i) { mMyMeshes[i].texUnits[unit] = textID; mMyMeshes[i].texTypes[unit] = GL_TEXTURE_2D; mMyMeshes[i].mat.texCount = 1; } }
// Load model textures bool VSResModelLib::loadTextures(const aiScene* scene, std::string prefix) { VSLOG(mLogInfo, "Loading Textures from %s", prefix.c_str()); /* scan scene's materials for textures */ for (unsigned int m=0; m<scene->mNumMaterials; ++m) { int texIndex = 0; aiString path; // filename aiReturn texFound = scene->mMaterials[m]-> GetTexture(aiTextureType_DIFFUSE, texIndex, &path); while (texFound == AI_SUCCESS) { //fill map with textures, OpenGL image ids set to 0 pTextureIdMap[path.data] = 0; // more textures? texIndex++; texFound = scene->mMaterials[m]-> GetTexture(aiTextureType_DIFFUSE, texIndex, &path); } } int numTextures = pTextureIdMap.size(); /* get iterator */ std::map<std::string, GLuint>::iterator itr = pTextureIdMap.begin(); for (int i= 0; itr != pTextureIdMap.end(); ++i, ++itr) { // get filename std::string filename = (*itr).first; filename = prefix + filename; // save texture id for filename in map (*itr).second = loadRGBATexture(filename, true,true); VSLOG(mLogInfo, "Texture %s loaded with name %d", filename.c_str(), (int)(*itr).second); } return true; }