RampGenerator.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // Copyright (c) 2008-2016 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Urho3D/Container/ArrayPtr.h>
  23. #include <Urho3D/Core/ProcessUtils.h>
  24. #include <Urho3D/Core/StringUtils.h>
  25. #ifdef WIN32
  26. #include <windows.h>
  27. #endif
  28. #include <STB/stb_image_write.h>
  29. #include <Urho3D/DebugNew.h>
  30. using namespace Urho3D;
  31. int main(int argc, char** argv);
  32. void Run(const Vector<String>& arguments);
  33. int main(int argc, char** argv)
  34. {
  35. Vector<String> arguments;
  36. #ifdef WIN32
  37. arguments = ParseArguments(GetCommandLineW());
  38. #else
  39. arguments = ParseArguments(argc, argv);
  40. #endif
  41. Run(arguments);
  42. return 0;
  43. }
  44. void Run(const Vector<String>& arguments)
  45. {
  46. if (arguments.Size() < 3)
  47. ErrorExit("Usage: RampGenerator <output png file> <width> <power> [dimensions]\n");
  48. int width = ToInt(arguments[1]);
  49. float power = ToFloat(arguments[2]);
  50. int dimensions = 1;
  51. if (arguments.Size() > 3)
  52. dimensions = ToInt(arguments[3]);
  53. if (width < 2)
  54. ErrorExit("Width must be at least 2");
  55. if (dimensions < 1 || dimensions > 2)
  56. ErrorExit("Dimensions must be 1 or 2");
  57. if (dimensions == 1)
  58. {
  59. SharedArrayPtr<unsigned char> data(new unsigned char[width]);
  60. for (int i = 0; i < width; ++i)
  61. {
  62. float x = ((float)i) / ((float)(width - 1));
  63. data[i] = (unsigned char)((1.0f - pow(x, power)) * 255.0f);
  64. }
  65. // Ensure start is full bright & end is completely black
  66. data[0] = 255;
  67. data[width - 1] = 0;
  68. stbi_write_png(arguments[0].CString(), width, 1, 1, data.Get(), 0);
  69. }
  70. if (dimensions == 2)
  71. {
  72. SharedArrayPtr<unsigned char> data(new unsigned char[width * width]);
  73. for (int y = 0; y < width; ++y)
  74. {
  75. for (int x = 0; x < width; ++x)
  76. {
  77. unsigned i = y * width + x;
  78. float halfWidth = width * 0.5f;
  79. float xf = (x - halfWidth + 0.5f) / (halfWidth - 0.5f);
  80. float yf = (y - halfWidth + 0.5f) / (halfWidth - 0.5f);
  81. float dist = sqrtf(xf * xf + yf * yf);
  82. if (dist > 1.0f)
  83. dist = 1.0f;
  84. data[i] = (unsigned char)((1.0f - pow(dist, power)) * 255.0f);
  85. }
  86. }
  87. // Ensure the border is completely black
  88. for (int x = 0; x < width; ++x)
  89. {
  90. data[x] = 0;
  91. data[(width - 1) * width + x] = 0;
  92. data[x * width] = 0;
  93. data[x * width + (width - 1)] = 0;
  94. }
  95. stbi_write_png(arguments[0].CString(), width, width, 1, data.Get(), 0);
  96. }
  97. }