Game.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. #include <cmath>
  2. #include <vector>
  3. #include <fstream>
  4. #include "client/Game.h"
  5. #include "client/utils/Utils.h"
  6. #include "rendering/Renderer.h"
  7. #include "common/utils/String.h"
  8. #include "common/utils/Random.h"
  9. #include "math/Quaternion.h"
  10. #include "rendering/wrapper/GLWrapper.h"
  11. static float readFloat(std::ifstream& in) {
  12. float f;
  13. in >> f;
  14. in.get();
  15. return f;
  16. }
  17. static Vector3 readVector3(std::ifstream& in) {
  18. float x = readFloat(in);
  19. float y = readFloat(in);
  20. float z = readFloat(in);
  21. return Vector3(x, y, z);
  22. }
  23. static Vector2 readVector2(std::ifstream& in) {
  24. float x = readFloat(in);
  25. float y = readFloat(in);
  26. return Vector2(x, y);
  27. }
  28. Game::Game(const Control& control, const Clock& fps, const Clock& tps, RenderSettings& renderSettings,
  29. const WindowSize& size, const char* file) :
  30. control(control), fps(fps), tps(tps), renderSettings(renderSettings), size(size), world(blockRegistry),
  31. worldRenderer(world), pointIndex(0), moveSpeed(0.125f), movedLength(0.0f), mode(Mode::AUTO) {
  32. Random r(0);
  33. float h = World::WORLD_SIZE * 0.6f;
  34. float mid = World::WORLD_SIZE * 0.5f;
  35. float randLength = World::WORLD_SIZE * 0.125f * 0.25f;
  36. pos.set(0, h, 0);
  37. lastPos = pos;
  38. rotation = Quaternion(Vector3(1, 0, 0), -80);
  39. lastRotation = rotation;
  40. Quaternion q;
  41. for(uint i = 0; i < cameraPoints.getCapacity(); i++) {
  42. Vector3 offset(mid, h, mid);
  43. offset += Vector3(r.nextFloat(randLength), r.nextFloat(randLength), r.nextFloat(randLength));
  44. Vector3 v(i * 360.0f / cameraPoints.getCapacity(), 0.0f);
  45. v *= mid * 0.5f;
  46. v += offset;
  47. q.mul(Quaternion(Vector3(r.nextFloat() * 360.0f, r.nextFloat() * -90.0f), -10.0f));
  48. cameraPoints.add({v, q, 0.0f});
  49. }
  50. updateDistances();
  51. std::vector<KDTree::Triangle> data;
  52. (void) readFloat;
  53. std::ifstream in;
  54. in.open(file);
  55. if(in.good()) {
  56. while(true) {
  57. Vector3 a = readVector3(in);
  58. Vector2 ta = readVector2(in);
  59. Vector3 b = readVector3(in);
  60. Vector2 tb = readVector2(in);
  61. Vector3 c = readVector3(in);
  62. Vector2 tc = readVector2(in);
  63. if(in.eof()) {
  64. break;
  65. }
  66. data.push_back(KDTree::Triangle(a, b, c));
  67. treeData.add(Triangle(Vertex(a, ta), Vertex(b, tb), Vertex(c, tc)));
  68. }
  69. }
  70. //generateSphere(data);
  71. //generateRandom(data);
  72. /*for(KDTree::Triangle& t : data) {
  73. treeData.add(Triangle(
  74. Vertex(t[0], Vector2(8.0f / 16.0f, 0.0f)),
  75. Vertex(t[1], Vector2(9.0f / 16.0f, 0.0f)),
  76. Vertex(t[2], Vector2(9.0f / 16.0f, 1.0f / 16.0f))
  77. ));
  78. }*/
  79. treeData.build();
  80. kdTree.build(data);
  81. kdTree.fillLines(lines);
  82. }
  83. void Game::tick() {
  84. lastRotation = rotation;
  85. lastPos = pos;
  86. Matrix m = rotation.toMatrix();
  87. Vector3 right = m * Vector3(1.0f, 0.0f, 0.0f);
  88. Vector3 up = m * Vector3(0.0f, 1.0f, 0.0f);
  89. Vector3 back = m * Vector3(0.0f, 0.0f, -1.0f);
  90. if(mode == Mode::PLAYER) {
  91. const float speed = 1.0f;
  92. if(control.keys.down.isDown()) {
  93. pos += back * speed;
  94. }
  95. if(control.keys.up.isDown()) {
  96. pos -= back * speed;
  97. }
  98. if(control.keys.left.isDown()) {
  99. pos -= right * speed;
  100. }
  101. if(control.keys.right.isDown()) {
  102. pos += right * speed;
  103. }
  104. if(control.keys.jump.isDown()) {
  105. pos += up * speed;
  106. }
  107. if(control.keys.sneak.isDown()) {
  108. pos -= up * speed;
  109. }
  110. const float rotationSpeed = 5.0f;
  111. if(control.keys.camLeft.isDown()) {
  112. rotation.mul(Quaternion(up, rotationSpeed));
  113. }
  114. if(control.keys.camRight.isDown()) {
  115. rotation.mul(Quaternion(up, -rotationSpeed));
  116. }
  117. if(control.keys.camUp.isDown()) {
  118. rotation.mul(Quaternion(right, rotationSpeed));
  119. }
  120. if(control.keys.camDown.isDown()) {
  121. rotation.mul(Quaternion(right, -rotationSpeed));
  122. }
  123. if(control.keys.test3.getDownTime() == 1) {
  124. cameraPoints.add({pos, rotation, 0.0f});
  125. }
  126. } else if(mode == Mode::AUTO) {
  127. movedLength += moveSpeed;
  128. if(control.keys.camUp.isDown()) {
  129. moveSpeed += 0.0125f;
  130. if(moveSpeed > 1.0f) {
  131. moveSpeed = 1.0f;
  132. }
  133. }
  134. if(control.keys.camDown.isDown()) {
  135. moveSpeed -= 0.0125f;
  136. if(moveSpeed < 0.0f) {
  137. moveSpeed = 0.0f;
  138. }
  139. }
  140. if(control.keys.test3.isDown()) {
  141. mode = Mode::PLAYER;
  142. cameraPoints.clear();
  143. }
  144. }
  145. if(control.keys.test.isDown()) {
  146. mode = Mode::PLAYER;
  147. }
  148. if(control.keys.test2.isDown() && cameraPoints.getLength() >= 3) {
  149. mode = Mode::AUTO;
  150. movedLength = 0.0f;
  151. updateDistances();
  152. }
  153. if(control.keys.test4.getDownTime() == 1) {
  154. renderSettings.shadows = !renderSettings.shadows;
  155. }
  156. if(control.keys.test5.getDownTime() == 1) {
  157. renderSettings.ssao = !renderSettings.ssao;
  158. }
  159. if(control.keys.test6.getDownTime() == 1) {
  160. renderSettings.bump += 0.05f;
  161. if(renderSettings.bump > 1.0f) {
  162. renderSettings.bump = 0.0f;
  163. }
  164. }
  165. if(control.keys.factor.getDownTime() == 1) {
  166. if(renderSettings.factor == 1) {
  167. renderSettings.factor = 2;
  168. } else if(renderSettings.factor == 2) {
  169. renderSettings.factor = 3;
  170. } else {
  171. renderSettings.factor = 1;
  172. }
  173. renderSettings.dirtyFactor = true;
  174. }
  175. if(control.buttons.primary.getDownTime() == 1) {
  176. float hWidth = size.width * 0.5f;
  177. float hHeight = size.height * 0.5f;
  178. float x = (control.buttons.getX() - hWidth) / hWidth;
  179. float y = -(control.buttons.getY() - hHeight) / hHeight;
  180. float aspect = hWidth / hHeight;
  181. float tan = tanf((0.5f * 60.0f) * M_PI / 180.0f);
  182. float q = 1.0f / tan;
  183. Vector3 direction(x / (q / aspect), y / q, 1.0f);
  184. direction.normalize();
  185. direction = m * direction;
  186. clickLine.clear();
  187. clickLine.add(pos, pos + direction * 100.0f, 0xFF00FF);
  188. if(kdTree.findIntersection(pos, direction)) {
  189. KDTree::Triangle t = kdTree.getIntersectedTriangle();
  190. clickLine.add(t[0], t[1], 0xFFFF00);
  191. clickLine.add(t[1], t[2], 0xFFFF00);
  192. clickLine.add(t[2], t[0], 0xFFFF00);
  193. Vector3 hit = kdTree.getIntersection();
  194. float diff = 0.2f;
  195. clickLine.add(hit - Vector3(diff, 0.0f, 0.0f), hit + Vector3(diff, 0.0f, 0.0f), 0x00FF00);
  196. clickLine.add(hit - Vector3(0.0f, diff, 0.0f), hit + Vector3(0.0f, diff, 0.0f), 0x00FF00);
  197. clickLine.add(hit - Vector3(0.0f, 0.0f, diff), hit + Vector3(0.0f, 0.0f, diff), 0x00FF00);
  198. }
  199. clickLine.build();
  200. kdTree.fillLines(lines);
  201. }
  202. }
  203. void Game::renderWorld(float lag, Renderer& renderer) const {
  204. if(mode == Mode::AUTO) {
  205. float leftLength = (movedLength - moveSpeed) + moveSpeed * lag;
  206. uint index = 0;
  207. while(leftLength >= cameraPoints[index].distance) {
  208. leftLength -= cameraPoints[index].distance;
  209. index = (index + 1) % cameraPoints.getLength();
  210. }
  211. float t = leftLength / cameraPoints[index].distance;
  212. Vector3 interpolatedPos = pointUntilDistance(leftLength, index, 4000);
  213. uint a = index == 0 ? cameraPoints.getLength() - 1 : index - 1;
  214. uint b = (a + 1) % cameraPoints.getLength();
  215. uint c = (a + 2) % cameraPoints.getLength();
  216. uint d = (a + 3) % cameraPoints.getLength();
  217. renderer.update(interpolatedPos, cameraPoints[b].q.squad(t, cameraPoints[a].q, cameraPoints[c].q, cameraPoints[d].q));
  218. pos = interpolatedPos;
  219. } else if(mode == Mode::PLAYER) {
  220. Vector3 v = lastPos + (pos - lastPos) * lag;
  221. renderer.update(v, lastRotation.slerp(lag, rotation));
  222. }
  223. worldRenderer.render(lag, renderer);
  224. treeData.draw();
  225. }
  226. void Game::renderWorldLines(float lag, Renderer& renderer) const {
  227. (void) lag;
  228. renderer.translateTo(0.0f, 0.0f, 0.0f);
  229. renderer.update();
  230. if(control.keys.kdTree.isDown()) {
  231. GLWrapper::setLineThickness(1.0f);
  232. lines.draw();
  233. }
  234. GLWrapper::setLineThickness(5.0f);
  235. clickLine.draw();
  236. }
  237. void Game::renderTextOverlay(float lag, Renderer& renderer, FontRenderer& fr) const {
  238. (void) lag;
  239. renderer.scale(2.0f).update();
  240. String s;
  241. fr.drawString(10, 10, s.append("FPS: ").append(fps.getUpdatesPerSecond()).append(" TPS: ").append(tps.getUpdatesPerSecond()));
  242. fr.drawString(10, 19, s.clear().append("Speed: ").append(moveSpeed));
  243. s.clear();
  244. s += pos;
  245. fr.drawString(10, 28, s);
  246. for(uint i = 0; i < cameraPoints.getLength(); i++) {
  247. s.clear().append(i + 1).append(": ");
  248. s += cameraPoints[i].pos;
  249. fr.drawString(10, i * 9 + 37, s);
  250. }
  251. }
  252. bool Game::isRunning() const {
  253. return true;
  254. }
  255. Vector3 Game::splineTangent(const Vector3& prev, const Vector3& current, const Vector3& next) const {
  256. (void) current;
  257. //Vector3 v(current);
  258. //v.sub(prev).mul(0.5f).addMul(next, 0.5f).addMul(current, -0.5f);
  259. return (next - prev) * 0.5f;
  260. }
  261. Vector3 Game::interpolate(const Vector3& a, const Vector3& b, const Vector3& tanA, const Vector3& tanB, float t) const {
  262. float t2 = t * t;
  263. float t3 = t2 * t;
  264. return a * (2.0f * t3 - 3.0f * t2 + 1.0f) +
  265. b * (-2.0f * t3 + 3.0f * t2) +
  266. tanA * (t3 - 2.0f * t2 + t) +
  267. tanB * (t3 - t2);
  268. }
  269. float Game::distance(uint index, uint splits) const {
  270. Vector3 a;
  271. Vector3 b;
  272. Vector3 tanA;
  273. Vector3 tanB;
  274. getPointsAndTangents(index, a, b, tanA, tanB);
  275. Vector3 currentPos;
  276. Vector3 currentNext = interpolate(a, b, tanA, tanB, 0.0f);
  277. float sum = 0.0f;
  278. for(uint i = 0; i <= splits; i++) {
  279. currentPos = currentNext;
  280. float t = (i + 1.0f) / (splits + 1.0f);
  281. currentNext = interpolate(a, b, tanA, tanB, t);
  282. float l = static_cast<Vector3> (currentPos - currentNext).length();
  283. sum += l;
  284. }
  285. return sum;
  286. }
  287. Vector3 Game::pointUntilDistance(float leftDistance, uint index, uint splits) const {
  288. Vector3 a;
  289. Vector3 b;
  290. Vector3 tanA;
  291. Vector3 tanB;
  292. getPointsAndTangents(index, a, b, tanA, tanB);
  293. Vector3 currentPos;
  294. Vector3 currentNext = interpolate(a, b, tanA, tanB, 0.0f);
  295. float sum = 0.0f;
  296. uint i = 0;
  297. while(leftDistance > sum) {
  298. currentPos = currentNext;
  299. float t = (i + 1.0f) / (splits + 1.0f);
  300. currentNext = interpolate(a, b, tanA, tanB, t);
  301. float l = static_cast<Vector3> (currentPos - currentNext).length();
  302. sum += l;
  303. i++;
  304. }
  305. return currentNext;
  306. }
  307. void Game::getPointsAndTangents(uint index, Vector3& a, Vector3& b, Vector3& tanA, Vector3& tanB) const {
  308. uint prev = index == 0 ? cameraPoints.getLength() - 1 : index - 1;
  309. uint currentA = (prev + 1) % cameraPoints.getLength();
  310. uint currentB = (prev + 2) % cameraPoints.getLength();
  311. uint next = (prev + 3) % cameraPoints.getLength();
  312. a = cameraPoints[currentA].pos;
  313. b = cameraPoints[currentB].pos;
  314. tanA = splineTangent(cameraPoints[prev].pos, a, b);
  315. tanB = splineTangent(a, b, cameraPoints[next].pos);
  316. }
  317. void Game::updateDistances() {
  318. for(uint i = 0; i < cameraPoints.getLength(); i++) {
  319. cameraPoints[i].distance = distance(i, 10000);
  320. }
  321. }
  322. void Game::generateSphere(std::vector<KDTree::Triangle>& data) {
  323. int fieldSize = 8;
  324. int quality = 3;
  325. float radius = 30;
  326. int triangles = fieldSize * quality;
  327. int layers = (2 + fieldSize) * quality;
  328. for(int l = 0; l < layers; l++) {
  329. float high1 = cosf((M_PI * l) / layers);
  330. float high2 = cosf((M_PI * (l + 1)) / layers);
  331. float r1 = sqrtf(1 - high1 * high1) * radius;
  332. float r2 = sqrtf(1 - high2 * high2) * radius;
  333. high1 *= radius;
  334. high2 *= radius;
  335. for(int i = 0; i < triangles; i++) {
  336. float first = 2 * M_PI * i / triangles;
  337. float second = 2 * M_PI * (i + 1) / triangles;
  338. data.push_back(KDTree::Triangle(
  339. Vector3(r2 * cosf(first), high2, r2 * sinf(first)),
  340. Vector3(r1 * cosf(first), high1, r1 * sinf(first)),
  341. Vector3(r1 * cosf(second), high1, r1 * sinf(second))));
  342. data.push_back(KDTree::Triangle(
  343. Vector3(r2 * cosf(first), high2, r2 * sinf(first)),
  344. Vector3(r1 * cosf(second), high1, r1 * sinf(second)),
  345. Vector3(r2 * cosf(second), high2, r2 * sinf(second))));
  346. }
  347. }
  348. }
  349. void Game::generateRandom(std::vector<KDTree::Triangle>& data) {
  350. float radius = 25.0f;
  351. float diff = 5.0f;
  352. Random r(0);
  353. for(int i = 0; i < 10000; i++) {
  354. Vector3 a(r.nextFloat() * radius, r.nextFloat() * radius, r.nextFloat() * radius);
  355. Vector3 b = a + Vector3(r.nextFloat() * diff, r.nextFloat() * diff, r.nextFloat() * diff);
  356. Vector3 c = a + Vector3(r.nextFloat() * diff, r.nextFloat() * diff, r.nextFloat() * diff);
  357. data.push_back(KDTree::Triangle(a, b, c));
  358. }
  359. }