NormalMapTool.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Context.h"
  24. #include "File.h"
  25. #include "Image.h"
  26. #include "StringUtils.h"
  27. #include <cstdlib>
  28. #include <iostream>
  29. #include <string>
  30. #include <stb_image.h>
  31. #include <stb_image_write.h>
  32. #include "DebugNew.h"
  33. SharedPtr<Context> context_(new Context());
  34. int main(int argc, char** argv);
  35. void Run(const std::vector<std::string>& arguments);
  36. void ErrorExit(const std::string& error);
  37. int main(int argc, char** argv)
  38. {
  39. std::vector<std::string> arguments;
  40. for (int i = 1; i < argc; ++i)
  41. arguments.push_back(std::string(argv[i]));
  42. Run(arguments);
  43. return 0;
  44. }
  45. void Run(const std::vector<std::string>& arguments)
  46. {
  47. if (arguments.size() < 1)
  48. {
  49. ErrorExit(
  50. "Usage: NormalMapTool <inputfile>\n\n"
  51. "Output file will be saved as inputfile.dds\n"
  52. );
  53. }
  54. File source(context_);
  55. source.Open(arguments[0]);
  56. Image image(context_);
  57. if (!image.Load(source))
  58. ErrorExit("Could not load input file " + arguments[0]);
  59. unsigned comp = image.GetComponents();
  60. if ((comp < 3) && (comp > 4))
  61. ErrorExit("Image must contain 3 or 4 components");
  62. SharedArrayPtr<unsigned char> buffer(new unsigned char[image.GetWidth() * image.GetHeight() * 4]);
  63. unsigned char* srcData = image.GetData();
  64. unsigned char* destData = buffer.GetPtr();
  65. for (int y = 0; y < image.GetHeight(); ++y)
  66. {
  67. for (int x = 0; x < image.GetWidth(); ++x)
  68. {
  69. unsigned char r = *srcData++;
  70. unsigned char g = *srcData++;
  71. unsigned char b = *srcData++;
  72. if (comp == 4)
  73. srcData++;
  74. // Store X as alpha, and Y as color. Z can be reconstructed
  75. *destData++ = g;
  76. *destData++ = g;
  77. *destData++ = g;
  78. *destData++ = r;
  79. }
  80. }
  81. std::string tempDestName = Split(arguments[0], '.')[0] + ".tga";
  82. stbi_write_tga(tempDestName.c_str(), image.GetWidth(), image.GetHeight(), 4, buffer.GetPtr());
  83. std::string command = "texconv -f DXT5 -ft DDS -if NONE " + tempDestName;
  84. int ret = system(command.c_str());
  85. if (ret)
  86. ErrorExit("Failed to convert to DXT5");
  87. remove(tempDestName.c_str());
  88. }
  89. void ErrorExit(const std::string& error)
  90. {
  91. std::cout << error;
  92. exit(1);
  93. }