Main.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. #include <math.h>
  2. #include "Vector3D.h"
  3. #include "GameEngine.h"
  4. #include "Control.h"
  5. long tickCounter = 0;
  6. long renderTickCounter = 0;
  7. long tickSum = 0;
  8. long renderTickSum = 0;
  9. long tickTime = -1;
  10. long renderTickTime = -1;
  11. typedef struct GameField
  12. {
  13. int id;
  14. float r;
  15. float g;
  16. float b;
  17. float a;
  18. } GameField;
  19. int amountGameFields = -1;
  20. GameField* gameFields = NULL;
  21. GLuint vbo = -1;
  22. GLuint vba = -1;
  23. int fieldSize = 0;
  24. int quality = 0;
  25. int vertices = 0;
  26. GLsizeiptr size = 0;
  27. GLfloat* data = NULL;
  28. Vector3D oldCamera;
  29. Vector3D camera;
  30. float oldLengthAngle;
  31. float lengthAngle;
  32. float oldWidthAngle;
  33. float widthAngle;
  34. float mouseX = 0.0f;
  35. float mouseY = 0.0f;
  36. Vector3D front;
  37. Vector3D back;
  38. Vector3D right;
  39. Vector3D left;
  40. Vector3D up;
  41. Vector3D down;
  42. int selectedIndex = -1;
  43. int selectedLayer = -1;
  44. GameField* getGameField(int layer, int index)
  45. {
  46. if(layer == 0)
  47. {
  48. return &gameFields[0];
  49. }
  50. else if(layer == 1 + fieldSize)
  51. {
  52. return &gameFields[1];
  53. }
  54. index += (layer - 1) * fieldSize + 2;
  55. if(index < 2 || index >= amountGameFields)
  56. {
  57. return NULL;
  58. }
  59. return &gameFields[index];
  60. }
  61. float interpolate(float lag, float old, float new)
  62. {
  63. return old + lag * (new - old);
  64. }
  65. void colorField(GameField* field)
  66. {
  67. if(field->id)
  68. {
  69. field->r = 1.0f;
  70. field->g = 0.0f;
  71. field->b = 0.0f;
  72. field->a = 1.0f;
  73. }
  74. else
  75. {
  76. field->r = 1.0f;
  77. field->g = 1.0f;
  78. field->b = 1.0f;
  79. field->a = 1.0f;
  80. }
  81. }
  82. void generateSphere()
  83. {
  84. int triangles = fieldSize * quality;
  85. int layers = (2 + fieldSize) * quality;
  86. if(gameFields == NULL)
  87. {
  88. amountGameFields = 2 + fieldSize * fieldSize;
  89. gameFields = malloc(sizeof(GameField) * amountGameFields);
  90. for(int i = 0; i < amountGameFields; i++)
  91. {
  92. gameFields[i].id = (rand() < RAND_MAX / 2) ? 1 : 0;
  93. colorField(&gameFields[i]);
  94. }
  95. }
  96. if(data == NULL)
  97. {
  98. vertices = triangles * layers * 3 * 2;
  99. size = sizeof(GLfloat) * 9 * vertices;
  100. data = malloc(size);
  101. }
  102. for(int l = 0; l < layers; l++)
  103. {
  104. float high1 = cos((M_PI * l) / layers);
  105. float high2 = cos((M_PI * (l + 1)) / layers);
  106. float texHigh1 = (l % quality) * (1.0f / quality);
  107. float texHigh2 = texHigh1 + (1.0f / quality);
  108. float r1 = sqrt(1 - high1 * high1);
  109. float r2 = sqrt(1 - high2 * high2);
  110. int flag = (l / quality) == 0;
  111. if(flag)
  112. {
  113. texHigh1 = 1 - texHigh1;
  114. texHigh2 = 1 - texHigh2;
  115. }
  116. flag |= (l / quality) >= fieldSize + 1;
  117. for(int i = 0; i < triangles; i++)
  118. {
  119. float first = 2 * M_PI * i / triangles;
  120. float second = 2 * M_PI * (i + 1) / triangles;
  121. int off = 27 * 2 * i + l * triangles * 27 * 2;
  122. float texWidth1 = (i % quality) * (1.0f / quality);
  123. float texWidth2 = texWidth1 + (1.0f / quality);
  124. float r = 0.0f;
  125. float g = 0.0f;
  126. float b = 0.0f;
  127. float a = 0.0f;
  128. GameField* field = getGameField(l / quality, i / quality);
  129. if(field != NULL)
  130. {
  131. r = field->r;
  132. g = field->g;
  133. b = field->b;
  134. a = field->a;
  135. }
  136. for(int j = 0; j < 54; j += 9)
  137. {
  138. data[3 + off + j] = r;
  139. data[4 + off + j] = g;
  140. data[5 + off + j] = b;
  141. data[6 + off + j] = a;
  142. }
  143. data[0 + off] = r2 * cos(first);
  144. data[1 + off] = high2;
  145. data[2 + off] = r2 * sin(first);
  146. data[9 + off] = r1 * cos(first);
  147. data[10 + off] = high1;
  148. data[11 + off] = r1 * sin(first);
  149. data[18 + off] = r1 * cos(second);
  150. data[19 + off] = high1;
  151. data[20 + off] = r1 * sin(second);
  152. if(flag)
  153. {
  154. data[7 + off] = texHigh2 * 0.5f;
  155. data[8 + off] = texHigh2 * 0.5f;
  156. data[16 + off] = texHigh1 * 0.5f;
  157. data[17 + off] = texHigh1 * 0.5f;
  158. data[25 + off] = texHigh1 * 0.5f;
  159. data[26 + off] = texHigh1 * 0.5f;
  160. }
  161. else
  162. {
  163. data[7 + off] = texWidth1;
  164. data[8 + off] = texHigh2;
  165. data[16 + off] = texWidth1;
  166. data[17 + off] = texHigh1;
  167. data[25 + off] = texWidth2;
  168. data[26 + off] = texHigh1;
  169. }
  170. data[27 + off] = r2 * cos(first);
  171. data[28 + off] = high2;
  172. data[29 + off] = r2 * sin(first);
  173. data[36 + off] = r1 * cos(second);
  174. data[37 + off] = high1;
  175. data[38 + off] = r1 * sin(second);
  176. data[45 + off] = r2 * cos(second);
  177. data[46 + off] = high2;
  178. data[47 + off] = r2 * sin(second);
  179. if(flag)
  180. {
  181. data[34 + off] = texHigh2 * 0.5f;
  182. data[35 + off] = texHigh2 * 0.5f;
  183. data[43 + off] = texHigh1 * 0.5f;
  184. data[44 + off] = texHigh1 * 0.5f;
  185. data[52 + off] = texHigh2 * 0.5f;
  186. data[53 + off] = texHigh2 * 0.5f;
  187. }
  188. else
  189. {
  190. data[34 + off] = texWidth1;
  191. data[35 + off] = texHigh2;
  192. data[43 + off] = texWidth2;
  193. data[44 + off] = texHigh1;
  194. data[52 + off] = texWidth2;
  195. data[53 + off] = texHigh2;
  196. }
  197. }
  198. }
  199. glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
  200. }
  201. void updateProjection(float aspect, int program)
  202. {
  203. float fovY = 60.0f;
  204. float nearClip = 0.1f;
  205. float farClip = 1000;
  206. float q = 1.0f / (float) tan((0.5f * fovY) * M_PI / 180.0f);
  207. float a = q / aspect;
  208. float b = (nearClip + farClip) / (nearClip - farClip);
  209. float c = (2.0f * nearClip * farClip) / (nearClip - farClip);
  210. GLfloat data[16];
  211. data[0] = a;
  212. data[1] = 0.0f;
  213. data[2] = 0.0f;
  214. data[3] = 0.0f;
  215. data[4] = 0.0f;
  216. data[5] = q;
  217. data[6] = 0.0f;
  218. data[7] = 0.0f;
  219. data[8] = 0.0f;
  220. data[9] = 0.0f;
  221. data[10] = b;
  222. data[11] = -1.0f;
  223. data[12] = 0.0f;
  224. data[13] = 0.0f;
  225. data[14] = c;
  226. data[15] = 0;
  227. GLint loc = glGetUniformLocation(program, "projMatrix");
  228. glUniformMatrix4fv(loc, 1, 0, data);
  229. }
  230. float chooseSmallerPositive(float a, float b)
  231. {
  232. if(a < 0)
  233. {
  234. return b;
  235. }
  236. else if(b < 0 || b > a)
  237. {
  238. return a;
  239. }
  240. return b;
  241. }
  242. void updateView(int program, float lag)
  243. {
  244. // front
  245. vectorSetAngles(&front, interpolate(lag, oldLengthAngle, lengthAngle), interpolate(lag, oldWidthAngle, widthAngle));
  246. // calculate selected tile
  247. float a = camera.x * camera.x + camera.y * camera.y + camera.z * camera.z - 1;
  248. float b = 2 * (camera.x * front.x + camera.y * front.y + camera.z * front.z);
  249. float c = front.x * front.x + front.y * front.y + front.z * front.z;
  250. float p = b / c;
  251. float q = a / c;
  252. float det = p * p * 0.25f - q;
  253. if(det >= 0)
  254. {
  255. float k = sqrt(det);
  256. k = chooseSmallerPositive(-0.5f * p + k, -0.5f * p - k);
  257. if(k >= 0)
  258. {
  259. Vector3D v;
  260. vectorSetTo(&v, &camera);
  261. vectorAddMul(&v, &front, k);
  262. selectedLayer = (asin(-v.y) + M_PI_2) * M_1_PI * (2 + fieldSize);
  263. if(selectedLayer == 2 + fieldSize)
  264. {
  265. selectedLayer = fieldSize + 1;
  266. }
  267. float tan = atan2(v.z, v.x) * M_1_PI * 0.5;
  268. tan += (tan < 0);
  269. selectedIndex = tan * fieldSize;
  270. }
  271. }
  272. // back
  273. vectorSetToInverse(&back, &front);
  274. // right
  275. vectorSetTo(&right, &front);
  276. vectorCross(&right, 0.0f, 1.0f, 0.0f);
  277. vectorNormalize(&right);
  278. // left
  279. vectorSetToInverse(&left, &right);
  280. // up
  281. vectorSetTo(&up, &front);
  282. vectorCrossWith(&up, &left);
  283. vectorNormalize(&up);
  284. // down
  285. vectorSetToInverse(&down, &up);
  286. GLfloat data[16];
  287. data[0] = right.x;
  288. data[1] = up.x;
  289. data[2] = back.x;
  290. data[3] = 0.0f;
  291. data[4] = right.y;
  292. data[5] = up.y;
  293. data[6] = back.y;
  294. data[7] = 0.0f;
  295. data[8] = right.z;
  296. data[9] = up.z;
  297. data[10] = back.z;
  298. data[11] = 0.0f;
  299. Vector3D interCam;
  300. vectorSetTo(&interCam, &oldCamera);
  301. vectorAddMul(&interCam, &camera, lag);
  302. vectorAddMul(&interCam, &oldCamera, -lag);
  303. data[12] = vectorDotInverse(&right, &interCam);
  304. data[13] = vectorDotInverse(&up, &interCam);
  305. data[14] = vectorDotInverse(&back, &interCam);
  306. data[15] = 1.0f;
  307. //printf("%f %f %f %f\n", data[0], data[4], data[8], data[12]);
  308. //printf("%f %f %f %f\n", data[1], data[5], data[9], data[13]);
  309. //printf("%f %f %f %f\n", data[2], data[6], data[10], data[14]);
  310. //printf("%f %f %f %f\n", data[3], data[7], data[11], data[15]);
  311. // front
  312. vectorSetAngles(&front, interpolate(lag, oldLengthAngle, lengthAngle), interpolate(lag, oldWidthAngle, widthAngle));
  313. front.y = 0;
  314. vectorNormalize(&front);
  315. // back
  316. vectorSetToInverse(&back, &front);
  317. // right
  318. vectorSetTo(&right, &front);
  319. vectorCross(&right, 0.0f, 1.0f, 0.0f);
  320. vectorNormalize(&right);
  321. // left
  322. vectorSetToInverse(&left, &right);
  323. // up
  324. vectorSet(&up, 0.0f, 1.0f, 0.0f);
  325. // down
  326. vectorSetToInverse(&down, &up);
  327. GLint loc = glGetUniformLocation(program, "viewMatrix");
  328. glUniformMatrix4fv(loc, 1, 0, data);
  329. }
  330. void init(int program)
  331. {
  332. printf("Init\n");
  333. glGenBuffers(1, &vbo);
  334. glGenVertexArrays(1, &vba);
  335. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  336. glBindVertexArray(vba);
  337. glVertexAttribPointer(0, 3, GL_FLOAT, 0, 36, (GLvoid*) 0);
  338. glEnableVertexAttribArray(0);
  339. glVertexAttribPointer(1, 4, GL_FLOAT, 0, 36, (GLvoid*) 12);
  340. glEnableVertexAttribArray(1);
  341. glVertexAttribPointer(2, 2, GL_FLOAT, 0, 36, (GLvoid*) 28);
  342. glEnableVertexAttribArray(2);
  343. generateSphere();
  344. vectorSet(&camera, 0.0f, 0.0f, -5.0f);
  345. lengthAngle = 0.0f;
  346. widthAngle = 0.0f;
  347. updateProjection(4.0f / 3.0f, program);
  348. }
  349. void invertField(int layer, int index)
  350. {
  351. GameField* field = getGameField(layer, index);
  352. if(field != NULL)
  353. {
  354. field->id = 1 - field->id;
  355. colorField(field);
  356. }
  357. }
  358. void invertAt(int layer, int index)
  359. {
  360. if(layer == 0)
  361. {
  362. invertField(layer, index);
  363. layer++;
  364. for(int i = 0; i < fieldSize ; i++)
  365. {
  366. invertField(layer, i);
  367. }
  368. }
  369. else if(layer == fieldSize + 1)
  370. {
  371. invertField(layer, index);
  372. layer--;
  373. for(int i = 0; i < fieldSize ; i++)
  374. {
  375. invertField(layer, i);
  376. }
  377. }
  378. else
  379. {
  380. invertField(layer, index);
  381. invertField(layer, (index + 1) % fieldSize);
  382. invertField(layer, (index - 1 + fieldSize) % fieldSize);
  383. invertField(layer + 1, index);
  384. invertField(layer - 1, index);
  385. }
  386. }
  387. void tick(int program)
  388. {
  389. if(tickTime == -1)
  390. {
  391. tickTime = glfwGetTimerValue();
  392. }
  393. else
  394. {
  395. long time = glfwGetTimerValue();
  396. tickSum += time - tickTime;
  397. tickTime = time;
  398. tickCounter++;
  399. }
  400. vectorSetTo(&oldCamera, &camera);
  401. oldLengthAngle = lengthAngle;
  402. oldWidthAngle = widthAngle;
  403. float factor = 0.05f;
  404. if(keyIsDown(GLFW_KEY_A))
  405. {
  406. vectorAddMul(&camera, &left, factor);
  407. }
  408. if(keyIsDown(GLFW_KEY_D))
  409. {
  410. vectorAddMul(&camera, &right, factor);
  411. }
  412. if(keyIsDown(GLFW_KEY_W))
  413. {
  414. vectorAddMul(&camera, &front, factor);
  415. }
  416. if(keyIsDown(GLFW_KEY_S))
  417. {
  418. vectorAddMul(&camera, &back, factor);
  419. }
  420. if(keyIsDown(GLFW_KEY_SPACE))
  421. {
  422. vectorAddMul(&camera, &up, factor);
  423. }
  424. if(keyIsDown(GLFW_KEY_LEFT_SHIFT))
  425. {
  426. vectorAddMul(&camera, &down, factor);
  427. }
  428. if(mouseIsJustDown(GLFW_MOUSE_BUTTON_1) && selectedLayer != -1 && selectedIndex != -1)
  429. {
  430. invertAt(selectedLayer, selectedIndex);
  431. generateSphere();
  432. selectedLayer = -1;
  433. selectedIndex = -1;
  434. }
  435. widthAngle -= mouseY * 0.1f;
  436. lengthAngle -= mouseX * 0.1f;
  437. if(widthAngle >= 89.5)
  438. {
  439. widthAngle = 89.5f;
  440. }
  441. else if(widthAngle <= -89.5)
  442. {
  443. widthAngle = -89.5f;
  444. }
  445. mouseX = 0.0f;
  446. mouseY = 0.0f;
  447. }
  448. void renderTick(int program, float lag)
  449. {
  450. if(renderTickTime == -1)
  451. {
  452. renderTickTime = glfwGetTimerValue();
  453. }
  454. else
  455. {
  456. long time = glfwGetTimerValue();
  457. renderTickSum += time - renderTickTime;
  458. renderTickTime = time;
  459. renderTickCounter++;
  460. }
  461. updateView(program, lag);
  462. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  463. glBindVertexArray(vba);
  464. glDrawArrays(GL_TRIANGLES, 0, vertices);
  465. }
  466. void onWindowResize(int width, int height)
  467. {
  468. updateProjection((float) width / height, getProgram());
  469. }
  470. void onMouseMove(float x, float y)
  471. {
  472. mouseX += x;
  473. mouseY += y;
  474. }
  475. int readPositiveInt(int from, int to, char* message, int error)
  476. {
  477. fputs(message, stdout);
  478. fflush(stdout);
  479. int size = 16;
  480. int index = 0;
  481. char* buffer = malloc(sizeof(char) * size);
  482. while(1)
  483. {
  484. int c = fgetc(stdin);
  485. if(c == EOF)
  486. {
  487. free(buffer);
  488. return error;
  489. }
  490. else if(c == '\n')
  491. {
  492. int number = 0;
  493. int i = 0;
  494. while(i < index)
  495. {
  496. int n = buffer[i] - '0';
  497. if(n < 0 || n > 9)
  498. {
  499. break;
  500. }
  501. number = number * 10 + n;
  502. if(number > to)
  503. {
  504. break;
  505. }
  506. i++;
  507. }
  508. if(i >= index && number >= from)
  509. {
  510. free(buffer);
  511. return number;
  512. }
  513. fputs(message, stdout);
  514. fflush(stdout);
  515. index = 0;
  516. continue;
  517. }
  518. if(index >= size)
  519. {
  520. size *= 2;
  521. buffer = realloc(buffer, sizeof(char) * size);
  522. }
  523. buffer[index] = c;
  524. index++;
  525. }
  526. }
  527. int main()
  528. {
  529. /*fieldSize = readPositiveInt(5, 20, "Select a game size from 5 to 20:\n", 0);
  530. if(fieldSize == 0)
  531. {
  532. printf("Cannot read from stdin\n");
  533. return EXIT_SUCCESS;
  534. }
  535. quality = readPositiveInt(1, 9, "Select a rendering quality from 1 to 9:\n", 0);
  536. if(quality == 0)
  537. {
  538. printf("Cannot read from stdin\n");
  539. return EXIT_SUCCESS;
  540. }*/
  541. fieldSize = 5;
  542. quality = 8;
  543. if(startGame("Test Game", init, tick, renderTick, onWindowResize, onMouseMove))
  544. {
  545. fprintf(stderr, "Exited with error\n");
  546. //return EXIT_FAILURE;
  547. }
  548. if(data != NULL)
  549. {
  550. free(data);
  551. }
  552. if(gameFields != NULL)
  553. {
  554. free(gameFields);
  555. }
  556. glDeleteBuffers(1, &vbo);
  557. glDeleteVertexArrays(1, &vba);
  558. printf("_______________TPS_______________\n");
  559. printf("%ld %ld %ld\n", tickCounter, tickSum, tickTime);
  560. printf("%lf\n", (double) tickSum / tickCounter);
  561. printf("%lf\n", 1000000000.0 * tickCounter / tickSum);
  562. printf("_______________FPS_______________\n");
  563. printf("%ld %ld %ld\n", renderTickCounter, renderTickSum, renderTickTime);
  564. printf("%lf\n", (double) renderTickSum / renderTickCounter);
  565. printf("%lf\n", 1000000000.0 * renderTickCounter / renderTickSum);
  566. return EXIT_SUCCESS;
  567. }