bounds.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2012 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"); you
  4. // may not use this file except in compliance with the License. You
  5. // may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  12. // implied. See the License for the specific language governing
  13. // permissions and limitations under the License.
  14. #ifndef WEBGL_LOADER_BOUNDS_H_
  15. #define WEBGL_LOADER_BOUNDS_H_
  16. #include <stdio.h>
  17. #include "base.h"
  18. namespace webgl_loader {
  19. // TODO: arbitrary vertex formats.
  20. struct Bounds {
  21. float mins[8];
  22. float maxes[8];
  23. void Clear() {
  24. for (size_t i = 0; i < 8; ++i) {
  25. mins[i] = FLT_MAX;
  26. maxes[i] = -FLT_MAX;
  27. }
  28. }
  29. void EncloseAttrib(const float* attribs) {
  30. for (size_t i = 0; i < 8; ++i) {
  31. const float attrib = attribs[i];
  32. if (mins[i] > attrib) {
  33. mins[i] = attrib;
  34. }
  35. if (maxes[i] < attrib) {
  36. maxes[i] = attrib;
  37. }
  38. }
  39. }
  40. void Enclose(const AttribList& attribs) {
  41. for (size_t i = 0; i < attribs.size(); i += 8) {
  42. EncloseAttrib(&attribs[i]);
  43. }
  44. }
  45. float UniformScale() const {
  46. const float x = maxes[0] - mins[0];
  47. const float y = maxes[1] - mins[1];
  48. const float z = maxes[2] - mins[2];
  49. return (x > y) // TODO: max3
  50. ? ((x > z) ? x : z)
  51. : ((y > z) ? y : z);
  52. }
  53. };
  54. // TODO: make maxPosition et. al. configurable.
  55. struct BoundsParams {
  56. static BoundsParams FromBounds(const Bounds& bounds) {
  57. BoundsParams ret;
  58. const float scale = bounds.UniformScale();
  59. // Position. Use a uniform scale.
  60. for (size_t i = 0; i < 3; ++i) {
  61. const int maxPosition = (1 << 14) - 1; // 16383;
  62. ret.mins[i] = bounds.mins[i];
  63. ret.scales[i] = scale;
  64. ret.outputMaxes[i] = maxPosition;
  65. ret.decodeOffsets[i] = maxPosition * bounds.mins[i] / scale;
  66. ret.decodeScales[i] = scale / maxPosition;
  67. }
  68. // TexCoord.
  69. // TODO: get bounds-dependent texcoords working!
  70. for (size_t i = 3; i < 5; ++i) {
  71. // const float texScale = bounds.maxes[i] - bounds.mins[i];
  72. const int maxTexcoord = (1 << 10) - 1; // 1023
  73. ret.mins[i] = 0; //bounds.mins[i];
  74. ret.scales[i] = 1; //texScale;
  75. ret.outputMaxes[i] = maxTexcoord;
  76. ret.decodeOffsets[i] = 0; //maxTexcoord * bounds.mins[i] / texScale;
  77. ret.decodeScales[i] = 1.0f / maxTexcoord; // texScale / maxTexcoord;
  78. }
  79. // Normal. Always uniform range.
  80. for (size_t i = 5; i < 8; ++i) {
  81. ret.mins[i] = -1;
  82. ret.scales[i] = 2.f;
  83. ret.outputMaxes[i] = (1 << 10) - 1; // 1023
  84. ret.decodeOffsets[i] = 1 - (1 << 9); // -511
  85. ret.decodeScales[i] = 1.0 / 511;
  86. }
  87. return ret;
  88. }
  89. void DumpJson(FILE* out = stdout) {
  90. // TODO: use JsonSink.
  91. fputs("{\n", out);
  92. fprintf(out, " \"decodeOffsets\": [%d,%d,%d,%d,%d,%d,%d,%d],\n",
  93. decodeOffsets[0], decodeOffsets[1], decodeOffsets[2],
  94. decodeOffsets[3], decodeOffsets[4], decodeOffsets[5],
  95. decodeOffsets[6], decodeOffsets[7]);
  96. fprintf(out, " \"decodeScales\": [%f,%f,%f,%f,%f,%f,%f,%f]\n",
  97. decodeScales[0], decodeScales[1], decodeScales[2], decodeScales[3],
  98. decodeScales[4], decodeScales[5], decodeScales[6], decodeScales[7]);
  99. fputs(" }", out);
  100. }
  101. float mins[8];
  102. float scales[8];
  103. int outputMaxes[8];
  104. int decodeOffsets[8];
  105. float decodeScales[8];
  106. };
  107. } // namespace webgl_loader
  108. #endif // WEBGL_LOADER_BOUNDS_H_