VoxelNormals.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. FinalSun/FinalAlert 2 Mission Editor
  3. Copyright (C) 1999-2024 Electronic Arts, Inc.
  4. Authored by Matthias Wagner
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. */
  16. #include "stdafx.h"
  17. #include "VoxelNormals.h"
  18. #include <array>
  19. #include <math.h>
  20. template<class T>
  21. T read(std::istream& s)
  22. {
  23. T t;
  24. auto p1 = s.tellg();
  25. auto n = sizeof(T);
  26. s.read(reinterpret_cast<char*>(&t), sizeof(T));
  27. auto p2 = s.tellg();
  28. auto p3 = p2 - p1;
  29. //s >> t;
  30. return t;
  31. };
  32. template<class T, int N>
  33. std::array<T, N> read(std::istream& s)
  34. {
  35. std::array<T, N> t;
  36. s.read(reinterpret_cast<char*>(t.data()), sizeof(T) * N);
  37. return t;
  38. };
  39. VoxelNormalTable::VoxelNormalTable(std::istream& f)
  40. {
  41. auto oldEx = f.exceptions();
  42. f.exceptions(f.failbit | f.badbit);
  43. try
  44. {
  45. load(f);
  46. }
  47. catch (const std::exception& ex)
  48. {
  49. f.exceptions(oldEx);
  50. throw ex;
  51. }
  52. }
  53. void VoxelNormalTable::load(std::istream& f)
  54. {
  55. auto count = read<std::uint8_t>(f);
  56. m_normals.clear();
  57. m_normals.reserve(count);
  58. for (auto i = 0; i < count; ++i)
  59. {
  60. auto v = read<float, 3>(f);
  61. m_normals.push_back(Vec3f(v.data()));
  62. }
  63. }
  64. VoxelNormalTables::VoxelNormalTables(std::istream& f)
  65. {
  66. auto oldEx = f.exceptions();
  67. f.exceptions(f.failbit | f.badbit);
  68. try
  69. {
  70. load(f);
  71. }
  72. catch(const std::exception& ex)
  73. {
  74. f.exceptions(oldEx);
  75. throw ex;
  76. }
  77. }
  78. void VoxelNormalTables::load(std::istream& f)
  79. {
  80. auto tableCount = read<std::uint8_t>(f);
  81. for (std::uint32_t i = 0; i < tableCount; ++i)
  82. {
  83. auto normalClass = read<std::uint8_t>(f);
  84. VoxelNormalTable table(f);
  85. auto normalIndex = normalClass - 1;
  86. m_tables.resize(std::max(m_tables.size(), static_cast<std::size_t>(normalClass)));
  87. m_tables[normalIndex] = std::move(table);
  88. }
  89. }