Game.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. #include <cmath>
  2. #include <iostream>
  3. #include "Game.h"
  4. #include "gaming-core/utils/Array.h"
  5. #include "gaming-core/utils/List.h"
  6. #include "gaming-core/utils/Random.h"
  7. #include "gaming-core/utils/Utils.h"
  8. // static GLuint texture3d;
  9. static List<Vector3, 122880> list;
  10. static float tData[16][16][16];
  11. static Array<List<Array<Vector3, 3>, 4>, 256> table;
  12. static List<Vector3, 50> points;
  13. static int known[15] = {0b00000000, 0b10000000, 0b11000000, 0b10000100,
  14. 0b01110000, 0b11110000, 0b01111000, 0b10100101,
  15. 0b10110001, 0b01110001, 0b10000010, 0b11000010,
  16. 0b01001010, 0b10101010, 0b10110010};
  17. static Array<List<Array<Vector3, 3>, 4>, 15> knownTable;
  18. static void addCube(int x, int y, int z) {
  19. bool b1 = tData[x][y][z] < 0.5f;
  20. bool b2 = tData[x + 1][y][z] < 0.5f;
  21. bool b3 = tData[x + 1][y][z + 1] < 0.5f;
  22. bool b4 = tData[x][y][z + 1] < 0.5f;
  23. bool b5 = tData[x][y + 1][z] < 0.5f;
  24. bool b6 = tData[x + 1][y + 1][z] < 0.5f;
  25. bool b7 = tData[x + 1][y + 1][z + 1] < 0.5f;
  26. bool b8 = tData[x][y + 1][z + 1] < 0.5f;
  27. int index = (b1 << 7) | (b2 << 6) | (b3 << 5) | (b4 << 4) | (b5 << 3) |
  28. (b6 << 2) | (b7 << 1) | b8;
  29. Vector3 v(static_cast<float>(x), static_cast<float>(y),
  30. static_cast<float>(z));
  31. for(int i = 0; i < table[index].getLength(); i++) {
  32. list.add(table[index][i][0] + v);
  33. list.add(table[index][i][1] + v);
  34. list.add(table[index][i][2] + v);
  35. }
  36. // if(b1) {
  37. // list.add(Vector3(x - 0.1f, y + 0.0f, z + 0.0f));
  38. // list.add(Vector3(x + 0.0f, y - 0.1f, z + 0.0f));
  39. // list.add(Vector3(x + 0.0f, y + 0.0f, z - 0.1f));
  40. //}
  41. }
  42. static void addKnownTable(int index, const Vector3& a, const Vector3& b,
  43. const Vector3& c) {
  44. Array<Vector3, 3> array;
  45. array[0] = a;
  46. array[1] = b;
  47. array[2] = c;
  48. knownTable[index].add(array);
  49. }
  50. static int swapBits(int index, int a, int b) {
  51. int bitA = (index & (1 << a)) != 0;
  52. int bitB = (index & (1 << b)) != 0;
  53. index &= ~((1 << a) | (1 << b));
  54. index |= bitA << b;
  55. index |= bitB << a;
  56. return index;
  57. }
  58. static int flipYZ(int index) {
  59. index = swapBits(index, 0, 1);
  60. index = swapBits(index, 3, 2);
  61. index = swapBits(index, 4, 5);
  62. index = swapBits(index, 7, 6);
  63. return index;
  64. }
  65. static int flipXY(int index) {
  66. index = swapBits(index, 0, 3);
  67. index = swapBits(index, 1, 2);
  68. index = swapBits(index, 4, 7);
  69. index = swapBits(index, 5, 6);
  70. return index;
  71. }
  72. static int rotateY(int index) {
  73. index = swapBits(index, 0, 1);
  74. index = swapBits(index, 0, 3);
  75. index = swapBits(index, 3, 2);
  76. index = swapBits(index, 4, 5);
  77. index = swapBits(index, 4, 7);
  78. index = swapBits(index, 7, 6);
  79. return index;
  80. }
  81. static int rotateZ(int index) {
  82. index = swapBits(index, 0, 1);
  83. index = swapBits(index, 0, 4);
  84. index = swapBits(index, 4, 5);
  85. index = swapBits(index, 3, 2);
  86. index = swapBits(index, 3, 7);
  87. index = swapBits(index, 7, 6);
  88. return index;
  89. }
  90. static int rotateX(int index) {
  91. index = swapBits(index, 2, 1);
  92. index = swapBits(index, 1, 5);
  93. index = swapBits(index, 5, 6);
  94. index = swapBits(index, 3, 0);
  95. index = swapBits(index, 0, 4);
  96. index = swapBits(index, 4, 7);
  97. return index;
  98. }
  99. static void flipYZ(List<Array<Vector3, 3>, 4>& perm) {
  100. Matrix m;
  101. m.translateX(-0.5f);
  102. m.scale(Vector3(-1.0f, 1.0f, 1.0f));
  103. m.translateX(0.5f);
  104. for(int i = 0; i < perm.getLength(); i++) {
  105. perm[i][0] = m * perm[i][0];
  106. perm[i][1] = m * perm[i][1];
  107. perm[i][2] = m * perm[i][2];
  108. }
  109. }
  110. static void flipXY(List<Array<Vector3, 3>, 4>& perm) {
  111. Matrix m;
  112. m.translateZ(-0.5f);
  113. m.scale(Vector3(1.0f, 1.0f, -1.0f));
  114. m.translateZ(0.5f);
  115. for(int i = 0; i < perm.getLength(); i++) {
  116. perm[i][0] = m * perm[i][0];
  117. perm[i][1] = m * perm[i][1];
  118. perm[i][2] = m * perm[i][2];
  119. }
  120. }
  121. static void applyMatrix(List<Array<Vector3, 3>, 4>& perm, Matrix& m) {
  122. for(int i = 0; i < perm.getLength(); i++) {
  123. perm[i][0] = m * perm[i][0];
  124. perm[i][1] = m * perm[i][1];
  125. perm[i][2] = m * perm[i][2];
  126. }
  127. }
  128. static bool check(int index) {
  129. Matrix m;
  130. for(int i = 0; i < 15; i++) {
  131. List<Array<Vector3, 3>, 4> perm = knownTable[i];
  132. int normal = index;
  133. for(int h = 0; h < 4; h++) {
  134. for(int w = 0; w < 4; w++) {
  135. for(int k = 0; k < 4; k++) {
  136. for(int l = 0; l < 2; l++) {
  137. /*if(known[i] == flipXY(flipYZ((normal)))) {
  138. flipYZ(perm);
  139. flipXY(perm);
  140. table[index] = perm;
  141. return false;
  142. } else*/
  143. if(known[i] == normal) {
  144. Matrix m2;
  145. m2.translate(Vector3(-0.5f, -0.5f, -0.5f));
  146. m *= m2;
  147. m.translate(Vector3(0.5f, 0.5f, 0.5f));
  148. applyMatrix(perm, m);
  149. table[index] = perm;
  150. return false;
  151. }
  152. normal = ((~normal) & 0xFF);
  153. }
  154. normal = rotateY(normal);
  155. Matrix m2;
  156. m2.rotateY(-90.0f);
  157. m *= m2;
  158. }
  159. normal = rotateZ(normal);
  160. Matrix m2;
  161. m2.rotateZ(90.0f);
  162. m *= m2;
  163. }
  164. normal = rotateX(normal);
  165. Matrix m2;
  166. m2.rotateX(90.0f);
  167. m *= m2;
  168. }
  169. }
  170. return true;
  171. }
  172. static void generateTable() {
  173. addKnownTable(1, Vector3(0.5f, 0.0f, 0.0f), Vector3(0.0f, 0.5f, 0.0f),
  174. Vector3(0.0f, 0.0f, 0.5f));
  175. addKnownTable(2, Vector3(0.0f, 0.0f, 0.5f), Vector3(1.0f, 0.0f, 0.5f),
  176. Vector3(0.0f, 0.5f, 0.0f));
  177. addKnownTable(2, Vector3(1.0f, 0.0f, 0.5f), Vector3(0.0f, 0.5f, 0.0f),
  178. Vector3(1.0f, 0.5f, 0.0f));
  179. addKnownTable(3, Vector3(0.5f, 0.0f, 0.0f), Vector3(0.0f, 0.5f, 0.0f),
  180. Vector3(0.0f, 0.0f, 0.5f));
  181. addKnownTable(3, Vector3(0.5f, 1.0f, 0.0f), Vector3(1.0f, 0.5f, 0.0f),
  182. Vector3(1.0f, 1.0f, 0.5f));
  183. addKnownTable(4, Vector3(0.5f, 0.0f, 0.0f), Vector3(0.0f, 0.0f, 0.5f),
  184. Vector3(1.0f, 0.5f, 0.0f));
  185. addKnownTable(4, Vector3(0.0f, 0.5f, 1.0f), Vector3(0.0f, 0.0f, 0.5f),
  186. Vector3(1.0f, 0.5f, 0.0f));
  187. addKnownTable(4, Vector3(0.0f, 0.5f, 1.0f), Vector3(1.0f, 0.5f, 0.0f),
  188. Vector3(1.0f, 0.5f, 1.0f));
  189. addKnownTable(5, Vector3(0.0f, 0.5f, 0.0f), Vector3(1.0f, 0.5f, 0.0f),
  190. Vector3(0.0f, 0.5f, 1.0f));
  191. addKnownTable(5, Vector3(1.0f, 0.5f, 1.0f), Vector3(1.0f, 0.5f, 0.0f),
  192. Vector3(0.0f, 0.5f, 1.0f));
  193. addKnownTable(6, Vector3(0.5f, 0.0f, 0.0f), Vector3(0.0f, 0.0f, 0.5f),
  194. Vector3(1.0f, 0.5f, 0.0f));
  195. addKnownTable(6, Vector3(0.0f, 0.5f, 1.0f), Vector3(0.0f, 0.0f, 0.5f),
  196. Vector3(1.0f, 0.5f, 0.0f));
  197. addKnownTable(6, Vector3(0.0f, 0.5f, 1.0f), Vector3(0.0f, 0.0f, 0.5f),
  198. Vector3(1.0f, 0.5f, 1.0f));
  199. addKnownTable(6, Vector3(0.5f, 1.0f, 0.0f), Vector3(0.0f, 0.5f, 0.0f),
  200. Vector3(0.0f, 1.0f, 0.5f));
  201. addKnownTable(7, Vector3(0.5f, 0.0f, 0.0f), Vector3(0.0f, 0.5f, 0.0f),
  202. Vector3(0.0f, 0.0f, 0.5f));
  203. addKnownTable(7, Vector3(0.5f, 1.0f, 0.0f), Vector3(1.0f, 0.5f, 0.0f),
  204. Vector3(1.0f, 1.0f, 0.5f));
  205. addKnownTable(7, Vector3(0.5f, 1.0f, 1.0f), Vector3(0.0f, 0.5f, 1.0f),
  206. Vector3(0.0f, 1.0f, 0.5f));
  207. addKnownTable(7, Vector3(0.5f, 1.0f, 0.0f), Vector3(1.0f, 0.5f, 0.0f),
  208. Vector3(1.0f, 1.0f, 0.5f));
  209. addKnownTable(8, Vector3(0.0f, 0.5f, 0.0f), Vector3(0.0f, 1.0f, 0.5f),
  210. Vector3(0.5f, 1.0f, 1.0f));
  211. addKnownTable(8, Vector3(0.0f, 0.5f, 0.0f), Vector3(0.5f, 0.0f, 0.0f),
  212. Vector3(0.5f, 1.0f, 1.0f));
  213. addKnownTable(8, Vector3(1.0f, 0.5f, 1.0f), Vector3(0.5f, 0.0f, 0.0f),
  214. Vector3(0.5f, 1.0f, 1.0f));
  215. addKnownTable(8, Vector3(1.0f, 0.5f, 1.0f), Vector3(0.5f, 0.0f, 0.0f),
  216. Vector3(1.0f, 0.0f, 0.5f));
  217. addKnownTable(9, Vector3(0.5f, 0.0f, 0.0f), Vector3(1.0f, 0.5f, 1.0f),
  218. Vector3(0.0f, 1.0f, 0.5f));
  219. addKnownTable(9, Vector3(0.5f, 0.0f, 0.0f), Vector3(0.0f, 0.0f, 0.5f),
  220. Vector3(0.0f, 1.0f, 0.5f));
  221. addKnownTable(9, Vector3(0.5f, 1.0f, 1.0f), Vector3(1.0f, 0.5f, 1.0f),
  222. Vector3(0.0f, 1.0f, 0.5f));
  223. addKnownTable(9, Vector3(0.5f, 0.0f, 0.0f), Vector3(1.0f, 0.5f, 1.0f),
  224. Vector3(1.0f, 0.5f, 0.0f));
  225. addKnownTable(10, Vector3(0.5f, 0.0f, 0.0f), Vector3(0.0f, 0.5f, 0.0f),
  226. Vector3(0.0f, 0.0f, 0.5f));
  227. addKnownTable(10, Vector3(0.5f, 1.0f, 1.0f), Vector3(1.0f, 0.5f, 1.0f),
  228. Vector3(1.0f, 1.0f, 0.5f));
  229. addKnownTable(11, Vector3(0.0f, 0.0f, 0.5f), Vector3(1.0f, 0.0f, 0.5f),
  230. Vector3(0.0f, 0.5f, 0.0f));
  231. addKnownTable(11, Vector3(1.0f, 0.0f, 0.5f), Vector3(0.0f, 0.5f, 0.0f),
  232. Vector3(1.0f, 0.5f, 0.0f));
  233. addKnownTable(11, Vector3(0.5f, 1.0f, 1.0f), Vector3(1.0f, 0.5f, 1.0f),
  234. Vector3(1.0f, 1.0f, 0.5f));
  235. addKnownTable(12, Vector3(0.5f, 1.0f, 1.0f), Vector3(1.0f, 0.5f, 1.0f),
  236. Vector3(1.0f, 1.0f, 0.5f));
  237. addKnownTable(12, Vector3(0.5f, 0.0f, 0.0f), Vector3(1.0f, 0.5f, 0.0f),
  238. Vector3(1.0f, 0.0f, 0.5f));
  239. addKnownTable(12, Vector3(0.5f, 1.0f, 0.0f), Vector3(0.0f, 0.5f, 0.0f),
  240. Vector3(0.0f, 1.0f, 0.5f));
  241. addKnownTable(13, Vector3(0.5f, 0.0f, 0.0f), Vector3(0.0f, 0.0f, 0.5f),
  242. Vector3(0.0f, 1.0f, 0.5f));
  243. addKnownTable(13, Vector3(0.5f, 0.0f, 0.0f), Vector3(0.0f, 1.0f, 0.5f),
  244. Vector3(0.0f, 1.0f, 0.5f));
  245. addKnownTable(13, Vector3(1.0f, 0.0f, 0.5f), Vector3(0.5f, 0.0f, 1.0f),
  246. Vector3(0.5f, 1.0f, 1.0f));
  247. addKnownTable(13, Vector3(1.0f, 0.0f, 0.5f), Vector3(0.5f, 1.0f, 1.0f),
  248. Vector3(0.5f, 1.0f, 1.0f));
  249. addKnownTable(14, Vector3(0.0f, 0.5f, 0.0f), Vector3(0.5f, 0.0f, 0.0f),
  250. Vector3(0.0f, 0.5f, 1.0f));
  251. addKnownTable(14, Vector3(1.0f, 1.0f, 0.5f), Vector3(0.5f, 0.0f, 0.0f),
  252. Vector3(0.0f, 0.5f, 1.0f));
  253. addKnownTable(14, Vector3(1.0f, 1.0f, 0.5f), Vector3(0.5f, 0.0f, 0.0f),
  254. Vector3(1.0f, 0.0f, 0.5f));
  255. addKnownTable(14, Vector3(1.0f, 1.0f, 0.5f), Vector3(0.5f, 1.0f, 1.0f),
  256. Vector3(0.0f, 0.5f, 1.0f));
  257. int counter = 0;
  258. for(int i = 0; i < 256; i++) {
  259. counter += check(i);
  260. }
  261. std::cout << "Unsupported: " << counter << "\n";
  262. }
  263. Game::Game(Shader& shader, Buttons& buttons, const Size& size)
  264. : shader(shader), buttons(buttons), size(size),
  265. frustum(60, 0.1f, 1000.0f, size), up(buttons.add(GLFW_KEY_SPACE, "Up")),
  266. down(buttons.add(GLFW_KEY_LEFT_SHIFT, "Down")),
  267. left(buttons.add(GLFW_KEY_A, "left")),
  268. right(buttons.add(GLFW_KEY_D, "right")),
  269. front(buttons.add(GLFW_KEY_W, "front")),
  270. back(buttons.add(GLFW_KEY_S, "back")) {
  271. shader.use();
  272. vertexBuffer.setAttributes(Attributes().addFloat(3));
  273. generateTable();
  274. /*Matrix m;
  275. m.translate(Vector3(-0.5f, -0.5f, -0.5f));
  276. m.rotateX(-90);
  277. m.translate(Vector3(0.5f, 0.5f, 0.5f));
  278. Vector3 v(0.5f, 0.0f, 0.0f);
  279. v = m * v;
  280. std::cout << v[0] << " " << v[1] << " " << v[2] << "\n";
  281. v = m * v;
  282. std::cout << v[0] << " " << v[1] << " " << v[2] << "\n";
  283. v = m * v;
  284. std::cout << v[0] << " " << v[1] << " " << v[2] << "\n";
  285. v = m * v;
  286. std::cout << v[0] << " " << v[1] << " " << v[2] << "\n";*/
  287. Random r(0);
  288. for(int x = 0; x < 16; x++) {
  289. for(int y = 0; y < 16; y++) {
  290. for(int z = 0; z < 16; z++) {
  291. if(x == 0 || x == 15 || y == 0 || y == 15 || z == 0 ||
  292. z == 15) {
  293. tData[x][y][z] = 0.0f;
  294. } else {
  295. // tData[x][y][z] = 1.0f;
  296. float sinX = sinf(M_PI * x / 8.0f);
  297. float cosY = cosf(M_PI * y / 8.0f);
  298. float cosZ = cosf(M_PI * z / 8.0f);
  299. tData[x][y][z] = (sinX * sinX + cosY * cosY + cosZ * cosZ) *
  300. (1.0f / 3.0f);
  301. }
  302. }
  303. }
  304. }
  305. for(int x = 0; x < 15; x++) {
  306. for(int y = 0; y < 15; y++) {
  307. for(int z = 0; z < 15; z++) {
  308. addCube(x, y, z);
  309. }
  310. }
  311. }
  312. vertexBuffer.setStaticData(sizeof(Vector3) * list.getLength(),
  313. list.begin());
  314. /*glGenTextures(1, &texture3d);
  315. glBindTexture(GL_TEXTURE_3D, texture3d);
  316. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  317. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  318. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  319. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
  320. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  321. glTexImage3D(GL_TEXTURE_3D, 0, GL_R32F, 16, 16, 1, 0, GL_RED, GL_FLOAT,
  322. tData);
  323. glActiveTexture(GL_TEXTURE0);*/
  324. }
  325. void Game::render(float lag) {
  326. Vector3 interPos = Utils::interpolate(oldPosition, position, lag);
  327. shader.setMatrix("proj", frustum.updateProjection().getValues());
  328. Matrix m;
  329. m.translate(interPos);
  330. m.translateZ(-30.0f);
  331. m.translateX(-8.0f);
  332. // m.rotateY(-30.0f);
  333. // m.scale(Vector3(0.05f, 0.05f, 1.0f));
  334. shader.setMatrix("view", m.getValues());
  335. vertexBuffer.draw(list.getLength());
  336. }
  337. void Game::tick() {
  338. oldPosition = position;
  339. if(up.isDown()) {
  340. position -= Vector3(0.0f, 0.5f, 0.0f);
  341. }
  342. if(down.isDown()) {
  343. position += Vector3(0.0f, 0.5f, 0.0f);
  344. }
  345. if(left.isDown()) {
  346. position += Vector3(0.5f, 0.0f, 0.0f);
  347. }
  348. if(right.isDown()) {
  349. position -= Vector3(0.5f, 0.0f, 0.0f);
  350. }
  351. if(front.isDown()) {
  352. position += Vector3(0.0f, 0.0f, 0.5f);
  353. }
  354. if(back.isDown()) {
  355. position -= Vector3(0.0f, 0.0f, 0.5f);
  356. }
  357. }