base.h 4.5 KB

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