1 | // Copyright 2011 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_BASE_H_
|
---|
15 | #define WEBGL_LOADER_BASE_H_
|
---|
16 |
|
---|
17 | #include <ctype.h>
|
---|
18 | #include <float.h>
|
---|
19 | #include <stdio.h>
|
---|
20 | #include <stdlib.h>
|
---|
21 | #include <string.h>
|
---|
22 |
|
---|
23 | #include <string>
|
---|
24 | #include <vector>
|
---|
25 |
|
---|
26 | // TODO: consider using C99 spellings.
|
---|
27 | typedef unsigned char uint8;
|
---|
28 | typedef unsigned short uint16;
|
---|
29 | typedef short int16;
|
---|
30 | typedef unsigned int uint32;
|
---|
31 |
|
---|
32 | // printf format strings for size_t.
|
---|
33 | #ifdef _WIN32
|
---|
34 | # define PRIuS "%Iu"
|
---|
35 | #else // Most compilers use the C99 format string.
|
---|
36 | # define PRIuS "%zu"
|
---|
37 | #endif
|
---|
38 |
|
---|
39 | #ifndef isfinite
|
---|
40 | # define isfinite _finite
|
---|
41 | #endif
|
---|
42 |
|
---|
43 | typedef std::vector<float> AttribList;
|
---|
44 | typedef std::vector<int> IndexList;
|
---|
45 | typedef std::vector<uint16> QuantizedAttribList;
|
---|
46 | typedef std::vector<uint16> OptimizedIndexList;
|
---|
47 |
|
---|
48 | // TODO: these data structures ought to go elsewhere.
|
---|
49 | struct DrawMesh {
|
---|
50 | // Interleaved vertex format:
|
---|
51 | // 3-D Position
|
---|
52 | // 3-D Normal
|
---|
53 | // 2-D TexCoord
|
---|
54 | // Note that these
|
---|
55 | AttribList attribs;
|
---|
56 | // Indices are 0-indexed.
|
---|
57 | IndexList indices;
|
---|
58 | };
|
---|
59 |
|
---|
60 | struct WebGLMesh {
|
---|
61 | QuantizedAttribList attribs;
|
---|
62 | OptimizedIndexList indices;
|
---|
63 | };
|
---|
64 |
|
---|
65 | typedef std::vector<WebGLMesh> WebGLMeshList;
|
---|
66 |
|
---|
67 | static inline int strtoint(const char* str, const char** endptr) {
|
---|
68 | return static_cast<int>(strtol(str, const_cast<char**>(endptr), 10));
|
---|
69 | }
|
---|
70 |
|
---|
71 | static inline const char* StripLeadingWhitespace(const char* str) {
|
---|
72 | while (isspace(*str)) {
|
---|
73 | ++str;
|
---|
74 | }
|
---|
75 | return str;
|
---|
76 | }
|
---|
77 |
|
---|
78 | static inline char* StripLeadingWhitespace(char* str) {
|
---|
79 | while (isspace(*str)) {
|
---|
80 | ++str;
|
---|
81 | }
|
---|
82 | return str;
|
---|
83 | }
|
---|
84 |
|
---|
85 | // Like basename.
|
---|
86 | static inline const char* StripLeadingDir(const char* const str) {
|
---|
87 | const char* last_slash = NULL;
|
---|
88 | const char* pos = str;
|
---|
89 | while (const char ch = *pos) {
|
---|
90 | if (ch == '/' || ch == '\\') {
|
---|
91 | last_slash = pos;
|
---|
92 | }
|
---|
93 | ++pos;
|
---|
94 | }
|
---|
95 | return last_slash ? (last_slash + 1) : str;
|
---|
96 | }
|
---|
97 |
|
---|
98 | static inline void TerminateAtNewlineOrComment(char* str) {
|
---|
99 | char* newline = strpbrk(str, "#\r\n");
|
---|
100 | if (newline) {
|
---|
101 | *newline = '\0';
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | static inline const char* ConsumeFirstToken(const char* const line,
|
---|
106 | std::string* token) {
|
---|
107 | const char* curr = line;
|
---|
108 | while (char ch = *curr) {
|
---|
109 | if (isspace(ch)) {
|
---|
110 | token->assign(line, curr);
|
---|
111 | return curr + 1;
|
---|
112 | }
|
---|
113 | ++curr;
|
---|
114 | }
|
---|
115 | if (curr == line) {
|
---|
116 | return NULL;
|
---|
117 | }
|
---|
118 | token->assign(line, curr);
|
---|
119 | return curr;
|
---|
120 | }
|
---|
121 |
|
---|
122 | static inline void ToLower(const char* in, std::string* out) {
|
---|
123 | while (char ch = *in) {
|
---|
124 | out->push_back(tolower(ch));
|
---|
125 | ++in;
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | static inline void ToLowerInplace(std::string* in) {
|
---|
130 | std::string& s = *in;
|
---|
131 | for (size_t i = 0; i < s.size(); ++i) {
|
---|
132 | s[i] = tolower(s[i]);
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | // Jenkin's One-at-a-time Hash. Not the best, but simple and
|
---|
137 | // portable.
|
---|
138 | uint32 SimpleHash(char *key, size_t len, uint32 seed = 0) {
|
---|
139 | uint32 hash = seed;
|
---|
140 | for(size_t i = 0; i < len; ++i) {
|
---|
141 | hash += static_cast<unsigned char>(key[i]);
|
---|
142 | hash += (hash << 10);
|
---|
143 | hash ^= (hash >> 6);
|
---|
144 | }
|
---|
145 | hash += (hash << 3);
|
---|
146 | hash ^= (hash >> 11);
|
---|
147 | hash += (hash << 15);
|
---|
148 | return hash;
|
---|
149 | }
|
---|
150 |
|
---|
151 | void ToHex(uint32 w, char out[9]) {
|
---|
152 | const char kOffset0 = '0';
|
---|
153 | const char kOffset10 = 'a' - 10;
|
---|
154 | out[8] = '\0';
|
---|
155 | for (size_t i = 8; i > 0;) {
|
---|
156 | uint32 bits = w & 0xF;
|
---|
157 | out[--i] = bits + ((bits < 10) ? kOffset0 : kOffset10);
|
---|
158 | w >>= 4;
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | uint16 Quantize(float f, float in_min, float in_scale, uint16 out_max) {
|
---|
163 | return static_cast<uint16>(out_max * ((f-in_min) / in_scale));
|
---|
164 | }
|
---|
165 |
|
---|
166 | // TODO: Visual Studio calls this someting different.
|
---|
167 | #ifdef putc_unlocked
|
---|
168 | # define PutChar putc_unlocked
|
---|
169 | #else
|
---|
170 | # define PutChar putc
|
---|
171 | #endif // putc_unlocked
|
---|
172 |
|
---|
173 | #ifndef CHECK
|
---|
174 | # define CHECK(PRED) if (!(PRED)) { \
|
---|
175 | fprintf(stderr, "%s:%d CHECK failed: ", __FILE__, __LINE__); \
|
---|
176 | fputs(#PRED "\n", stderr); \
|
---|
177 | exit(-1); } else
|
---|
178 | #endif // CHECK
|
---|
179 |
|
---|
180 | #ifndef DCHECK
|
---|
181 | # ifdef DEBUG
|
---|
182 | # define DCHECK(PRED) CHECK(PRED)
|
---|
183 | # else
|
---|
184 | # define DCHECK(PRED)
|
---|
185 | # endif // DEBUG
|
---|
186 | #endif // DCHECK
|
---|
187 |
|
---|
188 | #endif // WEBGL_LOADER_BASE_H_
|
---|