RampGenerator.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "MathDefs.h"
  24. #include "SharedArrayPtr.h"
  25. #include "StringUtils.h"
  26. #include <stb_image.h>
  27. #include <stb_image_write.h>
  28. #include <iostream>
  29. #include <string>
  30. #include "DebugNew.h"
  31. int main(int argc, char** argv);
  32. void Run(const std::vector<std::string>& arguments);
  33. void ErrorExit(const std::string& error);
  34. int main(int argc, char** argv)
  35. {
  36. std::vector<std::string> arguments;
  37. for (int i = 1; i < argc; ++i)
  38. arguments.push_back(std::string(argv[i]));
  39. Run(arguments);
  40. return 0;
  41. }
  42. void Run(const std::vector<std::string>& arguments)
  43. {
  44. if (arguments.size() < 3)
  45. ErrorExit("Usage: RampGenerator <output file> <width> <power> [dimensions]\n");
  46. int width = ToInt(arguments[1]);
  47. float power = ToFloat(arguments[2]);
  48. int dimensions = 1;
  49. if (arguments.size() > 3)
  50. dimensions = ToInt(arguments[3]);
  51. if (width < 2)
  52. ErrorExit("Width must be at least 2");
  53. if ((dimensions < 1) || (dimensions > 2))
  54. ErrorExit("Dimensions must be 1 or 2");
  55. std::string tempDestName = Split(arguments[0], '.')[0] + ".tga";
  56. if (dimensions == 1)
  57. {
  58. SharedArrayPtr<unsigned char> data(new unsigned char[width]);
  59. for (int i = 0; i < width; ++i)
  60. {
  61. float x = ((float)i) / ((float)(width - 1));
  62. data[i] = (unsigned char)((1.0f - pow(x, power)) * 255.0f);
  63. }
  64. // Ensure start is full bright & end is completely black
  65. data[0] = 255;
  66. data[width - 1] = 0;
  67. stbi_write_tga(tempDestName.c_str(), width, 1, 1, data.GetPtr());
  68. }
  69. if (dimensions == 2)
  70. {
  71. SharedArrayPtr<unsigned char> data(new unsigned char[width * width]);
  72. for (int y = 0; y < width; ++y)
  73. {
  74. for (int x = 0; x < width; ++x)
  75. {
  76. unsigned i = y * width + x;
  77. float halfWidth = width * 0.5f;
  78. float xf = (x - halfWidth + 0.5f) / (halfWidth - 0.5f);
  79. float yf = (y - halfWidth + 0.5f) / (halfWidth - 0.5f);
  80. float dist = sqrtf(xf * xf + yf * yf);
  81. if (dist > 1.0f)
  82. dist = 1.0f;
  83. data[i] = (unsigned char)((1.0f - pow(dist, power)) * 255.0f);
  84. }
  85. }
  86. // Ensure the border is completely black
  87. for (int x = 0; x < width; ++x)
  88. {
  89. data[x] = 0;
  90. data[(width - 1) * width + x] = 0;
  91. data[x * width] = 0;
  92. data[x * width + (width - 1)] = 0;
  93. }
  94. stbi_write_tga(tempDestName.c_str(), width, width, 1, data.GetPtr());
  95. }
  96. std::string command = "texconv -f R8G8B8 -ft PNG -if NONE " + tempDestName;
  97. int ret = system(command.c_str());
  98. if (ret)
  99. ErrorExit("Failed to convert to PNG");
  100. remove(tempDestName.c_str());
  101. }
  102. void ErrorExit(const std::string& error)
  103. {
  104. std::cout << error;
  105. exit(1);
  106. }