💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

461 lines
12 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. /*
  2. * Copyright (C) 2017 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper Source Code.
  5. *
  6. * Antkeeper Source Code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper Source Code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper Source Code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "material-loader.hpp"
  20. #include <algorithm>
  21. #include <fstream>
  22. #include <iostream>
  23. #include <sstream>
  24. #include <vector>
  25. MaterialLoader::MaterialLoader()
  26. {
  27. textureLoader.setGamma(1.0f);
  28. textureLoader.setMipmapChain(false);
  29. textureLoader.setMaxAnisotropy(1.0f);
  30. }
  31. MaterialLoader::~MaterialLoader()
  32. {
  33. unload();
  34. }
  35. void MaterialLoader::unload()
  36. {
  37. for (auto it = materialCache.begin(); it != materialCache.end(); ++it)
  38. {
  39. delete it->second;
  40. }
  41. materialCache.clear();
  42. for (auto it = texture2DCache.begin(); it != texture2DCache.end(); ++it)
  43. {
  44. delete it->second;
  45. }
  46. texture2DCache.clear();
  47. for (auto it = textureCubeCache.begin(); it != textureCubeCache.end(); ++it)
  48. {
  49. delete it->second;
  50. }
  51. textureCubeCache.clear();
  52. }
  53. Material* MaterialLoader::load(const std::string& filename)
  54. {
  55. // Check if material exists in cache
  56. auto it = materialCache.find(filename);
  57. if (it != materialCache.end())
  58. {
  59. return it->second;
  60. }
  61. // Allocate new material
  62. Material* material = new Material();
  63. // Open file
  64. std::ifstream file(filename.c_str(), std::ifstream::in);
  65. if (!file.is_open())
  66. {
  67. std::cerr << "MaterialLoader::load(): Failed to open material file \"" << filename << "\"" << std::endl;
  68. delete material;
  69. return nullptr;
  70. }
  71. // Parse lines
  72. std::string line;
  73. while (file.good() && std::getline(file, line))
  74. {
  75. const std::string whitespace = " \t";
  76. // Skip empty lines
  77. if (line.empty())
  78. {
  79. continue;
  80. }
  81. // Find position of first character in variable name
  82. std::size_t variableNamePosition = line.find_first_not_of(whitespace, 0);
  83. if (variableNamePosition == std::string::npos)
  84. {
  85. // Skip whitespace-only lines
  86. continue;
  87. }
  88. // Find position of equals sign
  89. std::size_t equalsSignPosition = line.find_first_of("=", variableNamePosition);
  90. if (equalsSignPosition == std::string::npos)
  91. {
  92. // Skip lines with no equals sign
  93. continue;
  94. }
  95. // Find position of first character in variable type
  96. std::size_t variableTypePosition = line.find_first_not_of(whitespace, equalsSignPosition + 1);
  97. if (variableTypePosition == std::string::npos)
  98. {
  99. // Skip lines with no variable type definition
  100. continue;
  101. }
  102. // Count parentheses
  103. std::size_t leftParenthesisCount = std::count(line.begin() + variableNamePosition, line.end(), '(');
  104. std::size_t rightParenthesisCount = std::count(line.begin() + variableNamePosition, line.end(), ')');
  105. if (leftParenthesisCount != rightParenthesisCount || leftParenthesisCount == 0)
  106. {
  107. // Skip lines with invalid number of parentheses
  108. continue;
  109. }
  110. std::string variableName = line.substr(variableNamePosition, line.find_first_of(" \t=", variableNamePosition) - variableNamePosition);
  111. std::string variableType = line.substr(variableTypePosition, line.find_first_of(" \t[(", variableTypePosition) - variableTypePosition);
  112. std::size_t elementCount = leftParenthesisCount;
  113. std::size_t currentPosition = variableTypePosition;
  114. std::vector<std::vector<std::string>> elements;
  115. bool invalid = false;
  116. for (std::size_t i = 0; i < elementCount; ++i)
  117. {
  118. std::size_t leftParenthesisPosition = line.find_first_of("(", currentPosition);
  119. std::size_t rightParenthesisPosition = line.find_first_of(")", leftParenthesisPosition + 1);
  120. if (leftParenthesisPosition == std::string::npos || rightParenthesisPosition == std::string::npos)
  121. {
  122. invalid = true;
  123. break;
  124. }
  125. currentPosition = leftParenthesisPosition + 1;
  126. std::size_t argumentCount = std::count(line.begin() + leftParenthesisPosition + 1, line.begin() + rightParenthesisPosition, ',') + 1;
  127. std::vector<std::string> arguments;
  128. for (std::size_t j = 0; j < argumentCount; ++j)
  129. {
  130. std::size_t argumentStart = line.find_first_not_of(whitespace, currentPosition);
  131. std::size_t argumentEnd = line.find_first_of(" \t,)", argumentStart + 1);
  132. if (argumentStart == std::string::npos || argumentEnd == std::string::npos)
  133. {
  134. // Unable to parse argument
  135. invalid = true;
  136. break;
  137. }
  138. std::string argument = line.substr(argumentStart, argumentEnd - argumentStart);
  139. arguments.push_back(argument);
  140. currentPosition = argumentEnd + 1;
  141. }
  142. if (invalid)
  143. {
  144. // Unable to parse element
  145. break;
  146. }
  147. elements.push_back(arguments);
  148. currentPosition = rightParenthesisPosition + 1;
  149. }
  150. if (invalid)
  151. {
  152. // Unable to parse line
  153. continue;
  154. }
  155. if (variableType == "int")
  156. {
  157. ShaderInt* variable = material->addVariable<int>(variableName, elements.size());
  158. loadShaderInt(variable, elements);
  159. }
  160. else if (variableType == "float")
  161. {
  162. ShaderFloat* variable = material->addVariable<float>(variableName, elements.size());
  163. loadShaderFloat(variable, elements);
  164. }
  165. else if (variableType == "vec2")
  166. {
  167. ShaderVector2* variable = material->addVariable<Vector2>(variableName, elements.size());
  168. loadShaderVector2(variable, elements);
  169. }
  170. else if (variableType == "vec3")
  171. {
  172. ShaderVector3* variable = material->addVariable<Vector3>(variableName, elements.size());
  173. loadShaderVector3(variable, elements);
  174. }
  175. else if (variableType == "vec4")
  176. {
  177. ShaderVector4* variable = material->addVariable<Vector4>(variableName, elements.size());
  178. loadShaderVector4(variable, elements);
  179. }
  180. else if (variableType == "mat3")
  181. {
  182. ShaderMatrix3* variable = material->addVariable<Matrix3>(variableName, elements.size());
  183. loadShaderMatrix3(variable, elements);
  184. }
  185. else if (variableType == "mat4")
  186. {
  187. ShaderMatrix4* variable = material->addVariable<Matrix4>(variableName, elements.size());
  188. loadShaderMatrix4(variable, elements);
  189. }
  190. else if (variableType == "texture")
  191. {
  192. ShaderTexture2D* variable = material->addVariable<const Texture2D*>(variableName, elements.size());
  193. loadShaderTexture2D(variable, elements);
  194. }
  195. else if (variableType == "textureCube")
  196. {
  197. ShaderTextureCube* variable = material->addVariable<const TextureCube*>(variableName, elements.size());
  198. loadShaderTextureCube(variable, elements);
  199. }
  200. }
  201. // Close file
  202. file.close();
  203. // Add material to cache
  204. materialCache[filename] = material;
  205. return material;
  206. }
  207. Texture2D* MaterialLoader::loadTexture2D(const std::string& filename)
  208. {
  209. // Check if texture exists in cache
  210. auto it = texture2DCache.find(filename);
  211. if (it != texture2DCache.end())
  212. {
  213. return it->second;
  214. }
  215. std::string fullFilename = std::string("data/textures/") + filename;
  216. // Load texture
  217. Texture2D* texture = textureLoader.load2D(fullFilename);
  218. if (!texture)
  219. {
  220. std::cerr << "MaterialLoader::loadTexture2D(): Failed to load texture file \"" << fullFilename << "\"" << std::endl;
  221. return nullptr;
  222. }
  223. // Add texture to cache
  224. texture2DCache[filename] = texture;
  225. return texture;
  226. }
  227. TextureCube* MaterialLoader::loadTextureCube(const std::string& filename)
  228. {
  229. // Check if texture exists in cache
  230. auto it = textureCubeCache.find(filename);
  231. if (it != textureCubeCache.end())
  232. {
  233. return it->second;
  234. }
  235. std::string fullFilename = std::string("data/textures/") + filename;
  236. // Load texture
  237. TextureCube* texture = textureLoader.loadCube(fullFilename);
  238. if (!texture)
  239. {
  240. std::cerr << "MaterialLoader::loadTextureCube(): Failed to load texture file \"" << fullFilename << "\"" << std::endl;
  241. return nullptr;
  242. }
  243. // Add texture to cache
  244. textureCubeCache[filename] = texture;
  245. return texture;
  246. }
  247. bool MaterialLoader::loadShaderInt(ShaderInt* variable, const std::vector<std::vector<std::string>>& elements)
  248. {
  249. for (int i = 0; i < elements.size(); ++i)
  250. {
  251. int value;
  252. std::stringstream stream;
  253. stream << elements[i][0];
  254. stream >> value;
  255. variable->setValue(i, value);
  256. }
  257. return true;
  258. }
  259. bool MaterialLoader::loadShaderFloat(ShaderFloat* variable, const std::vector<std::vector<std::string>>& elements)
  260. {
  261. for (int i = 0; i < elements.size(); ++i)
  262. {
  263. float value;
  264. std::stringstream stream;
  265. stream << elements[i][0];
  266. stream >> value;
  267. variable->setValue(i, value);
  268. }
  269. return true;
  270. }
  271. bool MaterialLoader::loadShaderVector2(ShaderVector2* variable, const std::vector<std::vector<std::string>>& elements)
  272. {
  273. for (int i = 0; i < elements.size(); ++i)
  274. {
  275. Vector2 value;
  276. for (int j = 0; j < 2; ++j)
  277. {
  278. std::stringstream stream;
  279. stream << elements[i][j];
  280. stream >> value[j];
  281. }
  282. variable->setValue(i, value);
  283. }
  284. return true;
  285. }
  286. bool MaterialLoader::loadShaderVector3(ShaderVector3* variable, const std::vector<std::vector<std::string>>& elements)
  287. {
  288. for (int i = 0; i < elements.size(); ++i)
  289. {
  290. Vector3 value;
  291. for (int j = 0; j < 3; ++j)
  292. {
  293. std::stringstream stream;
  294. stream << elements[i][j];
  295. stream >> value[j];
  296. }
  297. variable->setValue(i, value);
  298. }
  299. return true;
  300. }
  301. bool MaterialLoader::loadShaderVector4(ShaderVector4* variable, const std::vector<std::vector<std::string>>& elements)
  302. {
  303. for (int i = 0; i < elements.size(); ++i)
  304. {
  305. Vector4 value;
  306. for (int j = 0; j < 4; ++j)
  307. {
  308. std::stringstream stream;
  309. stream << elements[i][j];
  310. stream >> value[j];
  311. }
  312. variable->setValue(i, value);
  313. }
  314. return true;
  315. }
  316. bool MaterialLoader::loadShaderMatrix3(ShaderMatrix3* variable, const std::vector<std::vector<std::string>>& elements)
  317. {
  318. for (int i = 0; i < elements.size(); ++i)
  319. {
  320. Matrix3 value;
  321. for (int j = 0; j < 3; ++j)
  322. {
  323. for (int k = 0; k < 3; ++k)
  324. {
  325. std::stringstream stream;
  326. stream << elements[i][k * 3 + j];
  327. stream >> value[j][k];
  328. }
  329. }
  330. variable->setValue(i, value);
  331. }
  332. return true;
  333. }
  334. bool MaterialLoader::loadShaderMatrix4(ShaderMatrix4* variable, const std::vector<std::vector<std::string>>& elements)
  335. {
  336. for (int i = 0; i < elements.size(); ++i)
  337. {
  338. Matrix4 value;
  339. for (int j = 0; j < 4; ++j)
  340. {
  341. for (int k = 0; k < 4; ++k)
  342. {
  343. std::stringstream stream;
  344. stream << elements[i][k * 4 + j];
  345. stream >> value[j][k];
  346. }
  347. }
  348. variable->setValue(i, value);
  349. }
  350. return true;
  351. }
  352. bool MaterialLoader::loadShaderTexture2D(ShaderTexture2D* variable, const std::vector<std::vector<std::string>>& elements)
  353. {
  354. for (int i = 0; i < elements.size(); ++i)
  355. {
  356. std::string filename;
  357. std::stringstream stream;
  358. stream << elements[i][0];
  359. stream >> filename;
  360. Texture2D* value = loadTexture2D(filename);
  361. if (!value)
  362. {
  363. std::cerr << "MaterialLoader::loadShaderTexture2D(): Failed to load 2D texture \"" << filename << "\"" << std::endl;
  364. return false;
  365. }
  366. variable->setValue(i, value);
  367. }
  368. return true;
  369. }
  370. bool MaterialLoader::loadShaderTextureCube(ShaderTextureCube* variable, const std::vector<std::vector<std::string>>& elements)
  371. {
  372. for (int i = 0; i < elements.size(); ++i)
  373. {
  374. std::string filename;
  375. std::stringstream stream;
  376. stream << elements[i][0];
  377. stream >> filename;
  378. TextureCube* value = loadTextureCube(filename);
  379. if (!value)
  380. {
  381. std::cerr << "MaterialLoader::loadShaderTextureCube(): Failed to load cube texture \"" << filename << "\"" << std::endl;
  382. return false;
  383. }
  384. variable->setValue(i, value);
  385. }
  386. return true;
  387. }