You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

191 lines
5.5 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. import { mat4, vec3, vec4 } from "gl-matrix";
  2. import { Corner, GAT, Tile } from "../format/gat";
  3. import { createProgram, createShader, ShaderType } from "../util/render";
  4. /* @ts-ignore */
  5. import fragmentSource from "./gat.frag";
  6. /* @ts-ignore */
  7. import vertexSource from "./gat.vert";
  8. const ORIGIN = vec4.fromValues(0, 0, 0, 1);
  9. const UP = vec3.fromValues(0, 1, 0);
  10. const printMat4 = (name: string, m: mat4) => {
  11. console.log(name);
  12. console.log([m[0], m[1], m[2], m[3]]);
  13. console.log([m[4], m[5], m[6], m[7]]);
  14. console.log([m[8], m[9], m[10], m[11]]);
  15. console.log([m[12], m[13], m[14], m[15]]);
  16. };
  17. // Function to download data to a file
  18. function download(data: BlobPart, filename: string, type: string) {
  19. var file = new Blob([data], { type: type });
  20. // Others
  21. var a = document.createElement("a"),
  22. url = URL.createObjectURL(file);
  23. a.href = url;
  24. a.download = filename;
  25. document.body.appendChild(a);
  26. a.click();
  27. setTimeout(function () {
  28. document.body.removeChild(a);
  29. window.URL.revokeObjectURL(url);
  30. }, 0);
  31. }
  32. const exportObj = (vertices: number[]): string => {
  33. let obj = "";
  34. for (let i = 0; i < vertices.length / 3; ++i) {
  35. obj += `v ${vertices[i]} ${vertices[i + 1]} ${vertices[i + 2]}\n`;
  36. }
  37. for (let i = 0; i < vertices.length / (3 * 2); ++i) {
  38. obj += `f ${i + 1} ${i + 2} ${i + 3}\n`;
  39. obj += `f ${i + 4} ${i + 5} ${i + 6}\n`;
  40. }
  41. return obj;
  42. };
  43. export const renderGAT = (gl: WebGLRenderingContext, gat: GAT) => {
  44. console.log("gat", gat);
  45. const fragmentShader = createShader(gl, ShaderType.FRAGMENT, fragmentSource);
  46. const vertexShader = createShader(gl, ShaderType.VERTEX, vertexSource);
  47. const program = createProgram(gl, vertexShader, fragmentShader);
  48. gl.useProgram(program);
  49. gl.enable(gl.DEPTH_TEST);
  50. const maxAltitude = getMaxAltitude(gat);
  51. console.log("altitude", maxAltitude);
  52. const camera = vec3.fromValues(
  53. gat.width / 2,
  54. maxAltitude + 10,
  55. gat.height / 2
  56. );
  57. console.log("camera", camera);
  58. const centerX = Math.floor(gat.width / 2);
  59. const centerZ = Math.floor(gat.height / 2);
  60. const centerTile = gat.tiles[gat.width * centerZ + centerX];
  61. const center = vec3.fromValues(centerX, avgTileHeight(centerTile), centerZ);
  62. console.log("center", center);
  63. const model = mat4.create();
  64. mat4.identity(model);
  65. mat4.translate(
  66. model,
  67. model,
  68. vec3.fromValues(-centerX, -maxAltitude - 10, -centerZ)
  69. );
  70. const view = mat4.create();
  71. mat4.identity(view);
  72. mat4.rotateX(view, view, -Math.PI / 2);
  73. // mat4.lookAt(view, camera, center, UP);
  74. const perspective = mat4.create();
  75. const aspect = gl.canvas.clientWidth / gl.canvas.clientHeight;
  76. mat4.perspective(perspective, Math.PI / 4, aspect, 0.5, 2000);
  77. const matrix = mat4.create();
  78. mat4.multiply(matrix, view, model);
  79. printMat4("view * model", matrix);
  80. mat4.multiply(matrix, perspective, matrix);
  81. const matrixLoc = gl.getUniformLocation(program, "u_matrix");
  82. gl.uniformMatrix4fv(matrixLoc, false, matrix);
  83. printMat4("model", model);
  84. printMat4("view", view);
  85. printMat4("projection", perspective);
  86. printMat4("matrix", matrix);
  87. const origin = vec4.create();
  88. vec4.transformMat4(origin, ORIGIN, matrix);
  89. console.log("origin", origin);
  90. const vertices: number[] = [];
  91. gat.tiles.forEach((tile, i) => {
  92. const x = i % gat.width;
  93. const z = Math.floor(i / gat.width);
  94. const topLeft = [x, tile.altitude[Corner.TOP_LEFT], z];
  95. const topRight = [x + 1, tile.altitude[Corner.TOP_RIGHT], z];
  96. const bottomLeft = [x, tile.altitude[Corner.BOTTOM_LEFT], z + 1];
  97. const bottomRight = [x + 1, tile.altitude[Corner.BOTTOM_RIGHT], z + 1];
  98. vertices.push(
  99. ...topLeft,
  100. ...bottomLeft,
  101. ...topRight,
  102. ...topRight,
  103. ...bottomLeft,
  104. ...bottomRight
  105. );
  106. });
  107. console.log("# vertices", vertices.length / 3);
  108. // download(exportObj(vertices), "heightmap.obj", "text/plain");
  109. // let failCount = 0;
  110. // for (let i = 0; i < vertices.length / 3; ++i) {
  111. // const vec = vec3.fromValues(vertices[i], vertices[i + 1], vertices[i + 2]);
  112. // const result = checkPixelCoord(vec, matrix);
  113. // if (result) {
  114. // if (failCount < 10) {
  115. // // console.log(vec);
  116. // // console.log(result);
  117. // }
  118. // failCount++;
  119. // }
  120. // }
  121. // console.log("failCount", failCount);
  122. // console.log("totalCount", vertices.length / 3);
  123. const vertexBuffer = gl.createBuffer();
  124. gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
  125. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
  126. const positionLoc = gl.getAttribLocation(program, "a_position");
  127. gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
  128. gl.vertexAttribPointer(positionLoc, 3, gl.FLOAT, false, 0, 0);
  129. gl.enableVertexAttribArray(positionLoc);
  130. gl.clearColor(0, 0, 0, 1);
  131. gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
  132. gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
  133. gl.drawArrays(gl.TRIANGLES, 0, vertices.length / 3);
  134. };
  135. const checkPixelCoord = (x: vec3, mat: mat4): vec4 | null => {
  136. const vec = vec4.fromValues(x[0], x[1], x[2], 1);
  137. vec4.transformMat4(vec, vec, mat);
  138. for (let i = 0; i < vec.length; ++i) {
  139. if (Math.abs(vec[i]) >= 1) {
  140. return vec;
  141. }
  142. }
  143. return null;
  144. };
  145. const getMaxAltitude = (gat: GAT): number =>
  146. gat.tiles.reduce(
  147. (max, tile) =>
  148. Object.values(tile.altitude).reduce(
  149. (max1, altitude) => Math.max(max1, altitude),
  150. max
  151. ),
  152. 0
  153. );
  154. const avgTileHeight = (tile: Tile): number => {
  155. const heights = [...Object.values(tile.altitude)];
  156. const s = heights.reduce((x, y) => x + y, 0);
  157. return s / heights.length;
  158. };