Model.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #include "Model.h"
  2. #define STB_IMAGE_IMPLEMENTATION
  3. #include "stb_image.h"
  4. #include <iostream>
  5. #include <fstream>
  6. #include <string>
  7. #include <algorithm>
  8. //Prototypes
  9. std::vector<std::string> split(std::string str, std::string sep);
  10. std::string replace(std::string str, std::string toReplace, std::string replacement);
  11. std::string toLower(std::string data);
  12. Model::Model(std::string fileName)
  13. {
  14. std::string dirName = fileName;
  15. if (dirName.rfind("/") != std::string::npos)
  16. dirName = dirName.substr(0, dirName.rfind("/"));
  17. if (dirName.rfind("\\") != std::string::npos)
  18. dirName = dirName.substr(0, dirName.rfind("\\"));
  19. if (fileName == dirName)
  20. dirName = "";
  21. std::ifstream pFile(fileName.c_str());
  22. if (!pFile.is_open())
  23. {
  24. std::cout << "Could not open file " << fileName << std::endl;
  25. return;
  26. }
  27. ObjGroup* currentGroup = new ObjGroup();
  28. currentGroup->materialIndex = -1;
  29. while (!pFile.eof())
  30. {
  31. std::string line;
  32. std::getline(pFile, line);
  33. line = replace(line, "\t", " ");
  34. while (line.find(" ") != std::string::npos)
  35. line = replace(line, " ", " ");
  36. if (line == "")
  37. continue;
  38. if (line[0] == ' ')
  39. line = line.substr(1);
  40. if (line == "")
  41. continue;
  42. if (line[line.length() - 1] == ' ')
  43. line = line.substr(0, line.length() - 1);
  44. if (line == "")
  45. continue;
  46. if (line[0] == '#')
  47. continue;
  48. std::vector<std::string> params = split(line, " ");
  49. params[0] = toLower(params[0]);
  50. if (params[0] == "v")
  51. vertices.push_back(Vec3f((float)atof(params[1].c_str()), (float)atof(params[2].c_str()), (float)atof(params[3].c_str())));
  52. else if (params[0] == "vn")
  53. normals.push_back(Vec3f((float)atof(params[1].c_str()), (float)atof(params[2].c_str()), (float)atof(params[3].c_str())));
  54. else if (params[0] == "vt")
  55. texcoords.push_back(Vec2f((float)atof(params[1].c_str()), (float)atof(params[2].c_str())));
  56. else if (params[0] == "f")
  57. {
  58. for (size_t ii = 4; ii <= params.size(); ii++)
  59. {
  60. Face face;
  61. for (size_t i = ii - 3; i < ii; i++) //magische forlus om van quads triangles te maken ;)
  62. {
  63. Vertex vertex;
  64. std::vector<std::string> indices = split(params[i == (ii - 3) ? 1 : i], "/");
  65. if (indices.size() >= 1) //er is een positie
  66. vertex.position = atoi(indices[0].c_str()) - 1;
  67. if (indices.size() == 2) //alleen texture
  68. vertex.texcoord = atoi(indices[1].c_str()) - 1;
  69. if (indices.size() == 3) //v/t/n of v//n
  70. {
  71. if (indices[1] != "")
  72. vertex.texcoord = atoi(indices[1].c_str()) - 1;
  73. vertex.normal = atoi(indices[2].c_str()) - 1;
  74. }
  75. face.vertices.push_back(vertex);
  76. }
  77. currentGroup->faces.push_back(face);
  78. }
  79. }
  80. else if (params[0] == "s")
  81. {//smoothing
  82. }
  83. else if (params[0] == "mtllib")
  84. {
  85. loadMaterialFile(dirName + "/" + params[1], dirName);
  86. }
  87. else if (params[0] == "usemtl")
  88. {
  89. if (currentGroup->faces.size() != 0)
  90. groups.push_back(currentGroup);
  91. currentGroup = new ObjGroup();
  92. currentGroup->materialIndex = -1;
  93. for (size_t i = 0; i < materials.size(); i++)
  94. {
  95. MaterialInfo* info = materials[i];
  96. if (info->name == params[1])
  97. {
  98. currentGroup->materialIndex = i;
  99. break;
  100. }
  101. }
  102. if (currentGroup->materialIndex == -1)
  103. std::cout << "Could not find material name " << params[1] << std::endl;
  104. }
  105. }
  106. groups.push_back(currentGroup);
  107. }
  108. Model::~Model(void)
  109. {
  110. }
  111. void Model::draw()
  112. {
  113. for (auto &g : groups)
  114. {
  115. if (materials[g->materialIndex]->hasTexture)
  116. {
  117. glEnable(GL_TEXTURE_2D);
  118. materials[g->materialIndex]->texture->bind();
  119. }
  120. else
  121. {
  122. glDisable(GL_TEXTURE_2D);
  123. float color[4] = { 1, 0, 0, 1 };
  124. if (materials[g->materialIndex]->hasDiffuse)
  125. {
  126. memcpy(color, materials[g->materialIndex]->diffuseColor.v, 3 * sizeof(float));
  127. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color);
  128. }
  129. else if (materials[g->materialIndex]->hasAmbient)
  130. {
  131. memcpy(color, materials[g->materialIndex]->ambientColor.v, 3 * sizeof(float));
  132. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color);
  133. }
  134. else if (materials[g->materialIndex]->hasSpecular)
  135. {
  136. memcpy(color, materials[g->materialIndex]->specularColor.v, 3 * sizeof(float));
  137. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, color);
  138. }
  139. }
  140. glBegin(GL_TRIANGLES);
  141. for (auto &f : g->faces)
  142. {
  143. for (auto &v : f.vertices)
  144. {
  145. glNormal3f(normals[v.normal].x, normals[v.normal].y, normals[v.normal].z);
  146. glTexCoord2f(texcoords[v.texcoord].x, texcoords[v.texcoord].y);
  147. glVertex3f(vertices[v.position].x, vertices[v.position].y, vertices[v.position].z);
  148. }
  149. }
  150. glEnd();
  151. }
  152. }
  153. void Model::loadMaterialFile(std::string fileName, std::string dirName)
  154. {
  155. std::ifstream pFile(fileName.c_str());
  156. if (!pFile.is_open())
  157. {
  158. std::cout << "Could not open file " << fileName << std::endl;
  159. return;
  160. }
  161. MaterialInfo* currentMaterial = NULL;
  162. while (!pFile.eof())
  163. {
  164. std::string line;
  165. std::getline(pFile, line);
  166. line = replace(line, "\t", " ");
  167. while (line.find(" ") != std::string::npos)
  168. line = replace(line, " ", " ");
  169. if (line == "")
  170. continue;
  171. if (line[0] == ' ')
  172. line = line.substr(1);
  173. if (line == "")
  174. continue;
  175. if (line[line.length() - 1] == ' ')
  176. line = line.substr(0, line.length() - 1);
  177. if (line == "")
  178. continue;
  179. if (line[0] == '#')
  180. continue;
  181. std::vector<std::string> params = split(line, " ");
  182. params[0] = toLower(params[0]);
  183. if (params[0] == "newmtl")
  184. {
  185. if (currentMaterial != NULL)
  186. {
  187. materials.push_back(currentMaterial);
  188. }
  189. currentMaterial = new MaterialInfo();
  190. currentMaterial->name = params[1];
  191. }
  192. else if (params[0] == "map_kd")
  193. {
  194. currentMaterial->hasTexture = true;
  195. currentMaterial->texture = new Texture(dirName + "/" + params[1]);
  196. }
  197. else if (params[0] == "kd")
  198. {
  199. currentMaterial->hasDiffuse = true;
  200. currentMaterial->diffuseColor = Vec3f(atof(params[1].c_str()), atof(params[2].c_str()), atof(params[3].c_str()));
  201. }
  202. else if (params[0] == "ka")
  203. {
  204. currentMaterial->hasAmbient = true;
  205. currentMaterial->ambientColor = Vec3f(atof(params[1].c_str()), atof(params[2].c_str()), atof(params[3].c_str()));
  206. }
  207. else if (params[0] == "ks")
  208. {
  209. currentMaterial->hasSpecular = true;
  210. currentMaterial->specularColor = Vec3f(atof(params[1].c_str()), atof(params[2].c_str()), atof(params[3].c_str()));
  211. }
  212. else
  213. std::cout << "Didn't parse " << params[0] << " in material file" << std::endl;
  214. }
  215. if (currentMaterial != NULL)
  216. materials.push_back(currentMaterial);
  217. }
  218. Model::MaterialInfo::MaterialInfo()
  219. {
  220. hasTexture = false;
  221. }
  222. Model::Texture::Texture(const std::string & fileName)
  223. {
  224. int width, height, bpp;
  225. stbi_set_flip_vertically_on_load(true);
  226. unsigned char* imgData = stbi_load(fileName.c_str(), &width, &height, &bpp, 4);
  227. glGenTextures(1, &index);
  228. glBindTexture(GL_TEXTURE_2D, index);
  229. glTexImage2D(GL_TEXTURE_2D,
  230. 0, //level
  231. GL_RGBA, //internal format
  232. width, //width
  233. height, //height
  234. 0, //border
  235. GL_RGBA, //data format
  236. GL_UNSIGNED_BYTE, //data type
  237. imgData); //data
  238. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  239. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  240. stbi_image_free(imgData);
  241. }
  242. void Model::Texture::bind()
  243. {
  244. glBindTexture(GL_TEXTURE_2D, index);
  245. }
  246. std::string replace(std::string str, std::string toReplace, std::string replacement)
  247. {
  248. size_t index = 0;
  249. while (true)
  250. {
  251. index = str.find(toReplace, index);
  252. if (index == std::string::npos)
  253. break;
  254. str.replace(index, toReplace.length(), replacement);
  255. ++index;
  256. }
  257. return str;
  258. }
  259. std::vector<std::string> split(std::string str, std::string sep)
  260. {
  261. std::vector<std::string> ret;
  262. size_t index;
  263. while (true)
  264. {
  265. index = str.find(sep);
  266. if (index == std::string::npos)
  267. break;
  268. ret.push_back(str.substr(0, index));
  269. str = str.substr(index + 1);
  270. }
  271. ret.push_back(str);
  272. return ret;
  273. }
  274. inline std::string toLower(std::string data)
  275. {
  276. std::transform(data.begin(), data.end(), data.begin(), ::tolower);
  277. return data;
  278. }
  279. std::map<std::string, std::pair<Model*, int>> Model::cache;
  280. Model* Model::load(const std::string &fileName)
  281. {
  282. if (cache.find(fileName) == cache.end())
  283. cache[fileName] = std::pair<Model*, int>(new Model(fileName), 0);
  284. cache[fileName].second++;
  285. return cache[fileName].first;
  286. }
  287. void Model::unload(const std::string & fileName)
  288. {
  289. assert(cache.find(fileName) != cache.end());
  290. cache[fileName].second--;
  291. if (cache[fileName].second == 0)
  292. {
  293. delete cache[fileName].first;
  294. cache.erase(cache.find(fileName));
  295. }
  296. }