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 |
|
---|
15 | #ifndef WEBGL_LOADER_BOUNDS_H_
|
---|
16 | #define WEBGL_LOADER_BOUNDS_H_
|
---|
17 |
|
---|
18 | #include <stdio.h>
|
---|
19 |
|
---|
20 | #include "base.h"
|
---|
21 |
|
---|
22 | namespace webgl_loader {
|
---|
23 |
|
---|
24 | // TODO: arbitrary vertex formats.
|
---|
25 |
|
---|
26 | struct Bounds {
|
---|
27 | float mins[8];
|
---|
28 | float maxes[8];
|
---|
29 |
|
---|
30 | void Clear() {
|
---|
31 | for (size_t i = 0; i < 8; ++i) {
|
---|
32 | mins[i] = FLT_MAX;
|
---|
33 | maxes[i] = -FLT_MAX;
|
---|
34 | }
|
---|
35 | }
|
---|
36 |
|
---|
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) {
|
---|
41 | mins[i] = attrib;
|
---|
42 | }
|
---|
43 | if (maxes[i] < attrib) {
|
---|
44 | maxes[i] = attrib;
|
---|
45 | }
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | void Enclose(const AttribList& attribs) {
|
---|
50 | for (size_t i = 0; i < attribs.size(); i += 8) {
|
---|
51 | EncloseAttrib(&attribs[i]);
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
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
|
---|
60 | ? ((x > z) ? x : z)
|
---|
61 | : ((y > z) ? y : z);
|
---|
62 | }
|
---|
63 | };
|
---|
64 |
|
---|
65 | // TODO: make maxPosition et. al. configurable.
|
---|
66 | struct BoundsParams {
|
---|
67 | static BoundsParams FromBounds(const Bounds& bounds) {
|
---|
68 | BoundsParams ret;
|
---|
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;
|
---|
78 | }
|
---|
79 | // TexCoord.
|
---|
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;
|
---|
89 | }
|
---|
90 | // Normal. Always uniform range.
|
---|
91 | for (size_t i = 5; i < 8; ++i) {
|
---|
92 | ret.mins[i] = -1;
|
---|
93 | ret.scales[i] = 2.f;
|
---|
94 | ret.outputMaxes[i] = (1 << 10) - 1; // 1023
|
---|
95 | ret.decodeOffsets[i] = 1 - (1 << 9); // -511
|
---|
96 | ret.decodeScales[i] = 1.0 / 511;
|
---|
97 | }
|
---|
98 | return ret;
|
---|
99 | }
|
---|
100 |
|
---|
101 | void DumpJson(FILE* out = stdout) {
|
---|
102 | // TODO: use JsonSink.
|
---|
103 | fputs("{\n", out);
|
---|
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]);
|
---|
111 | fputs(" }", out);
|
---|
112 | }
|
---|
113 |
|
---|
114 | float mins[8];
|
---|
115 | float scales[8];
|
---|
116 | int outputMaxes[8];
|
---|
117 | int decodeOffsets[8];
|
---|
118 | float decodeScales[8];
|
---|
119 | };
|
---|
120 |
|
---|
121 | } // namespace webgl_loader
|
---|
122 |
|
---|
123 | #endif // WEBGL_LOADER_BOUNDS_H_
|
---|