1 // Copyright 2012 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 #ifndef WEBGL_LOADER_BOUNDS_H_
16 #define WEBGL_LOADER_BOUNDS_H_
22 namespace webgl_loader {
24 // TODO: arbitrary vertex formats.
31 for (size_t i = 0; i < 8; ++i) {
37 void EncloseAttrib(const float* attribs) {
38 for (size_t i = 0; i < 8; ++i) {
39 const float attrib = attribs[i];
40 if (mins[i] > attrib) {
43 if (maxes[i] < attrib) {
49 void Enclose(const AttribList& attribs) {
50 for (size_t i = 0; i < attribs.size(); i += 8) {
51 EncloseAttrib(&attribs[i]);
55 float UniformScale() const {
56 const float x = maxes[0] - mins[0];
57 const float y = maxes[1] - mins[1];
58 const float z = maxes[2] - mins[2];
59 return (x > y) // TODO: max3
65 // TODO: make maxPosition et. al. configurable.
67 static BoundsParams FromBounds(const Bounds& bounds) {
69 const float scale = bounds.UniformScale();
70 // Position. Use a uniform scale.
71 for (size_t i = 0; i < 3; ++i) {
72 const int maxPosition = (1 << 14) - 1; // 16383;
73 ret.mins[i] = bounds.mins[i];
74 ret.scales[i] = scale;
75 ret.outputMaxes[i] = maxPosition;
76 ret.decodeOffsets[i] = maxPosition * bounds.mins[i] / scale;
77 ret.decodeScales[i] = scale / maxPosition;
80 // TODO: get bounds-dependent texcoords working!
81 for (size_t i = 3; i < 5; ++i) {
82 // const float texScale = bounds.maxes[i] - bounds.mins[i];
83 const int maxTexcoord = (1 << 10) - 1; // 1023
84 ret.mins[i] = 0; //bounds.mins[i];
85 ret.scales[i] = 1; //texScale;
86 ret.outputMaxes[i] = maxTexcoord;
87 ret.decodeOffsets[i] = 0; //maxTexcoord * bounds.mins[i] / texScale;
88 ret.decodeScales[i] = 1.0f / maxTexcoord; // texScale / maxTexcoord;
90 // Normal. Always uniform range.
91 for (size_t i = 5; i < 8; ++i) {
94 ret.outputMaxes[i] = (1 << 10) - 1; // 1023
95 ret.decodeOffsets[i] = 1 - (1 << 9); // -511
96 ret.decodeScales[i] = 1.0 / 511;
101 void DumpJson(FILE* out = stdout) {
102 // TODO: use JsonSink.
104 fprintf(out, " \"decodeOffsets\": [%d,%d,%d,%d,%d,%d,%d,%d],\n",
105 decodeOffsets[0], decodeOffsets[1], decodeOffsets[2],
106 decodeOffsets[3], decodeOffsets[4], decodeOffsets[5],
107 decodeOffsets[6], decodeOffsets[7]);
108 fprintf(out, " \"decodeScales\": [%f,%f,%f,%f,%f,%f,%f,%f]\n",
109 decodeScales[0], decodeScales[1], decodeScales[2], decodeScales[3],
110 decodeScales[4], decodeScales[5], decodeScales[6], decodeScales[7]);
117 int decodeOffsets[8];
118 float decodeScales[8];
121 } // namespace webgl_loader
123 #endif // WEBGL_LOADER_BOUNDS_H_