import { mat4, vec3, vec4 } from "gl-matrix";
|
|
|
|
import { Corner, GAT, Tile } from "../format/gat";
|
|
import { createProgram, createShader, ShaderType } from "../util/render";
|
|
|
|
/* @ts-ignore */
|
|
import fragmentSource from "./gat.frag";
|
|
/* @ts-ignore */
|
|
import vertexSource from "./gat.vert";
|
|
|
|
const UP = vec3.fromValues(0, 1, 0);
|
|
|
|
export const renderGAT = (gl: WebGLRenderingContext, gat: GAT) => {
|
|
console.log("gat", gat);
|
|
|
|
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
|
|
|
|
gl.clearColor(0, 0, 0, 1);
|
|
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
|
|
const fragmentShader = createShader(gl, ShaderType.FRAGMENT, fragmentSource);
|
|
const vertexShader = createShader(gl, ShaderType.VERTEX, vertexSource);
|
|
const program = createProgram(gl, vertexShader, fragmentShader);
|
|
|
|
gl.useProgram(program);
|
|
gl.enable(gl.DEPTH_TEST);
|
|
|
|
const maxAltitude = getMaxAltitude(gat);
|
|
console.log("altitude", maxAltitude);
|
|
|
|
const camera = vec3.fromValues(
|
|
gat.width / 2,
|
|
maxAltitude + 10,
|
|
gat.height / 2
|
|
);
|
|
console.log("camera", camera);
|
|
|
|
const centerX = Math.floor(gat.width / 2);
|
|
const centerZ = Math.floor(gat.height / 2);
|
|
const centerTile = gat.tiles[gat.width * centerZ + centerX];
|
|
const center = vec3.fromValues(centerX, avgTileHeight(centerTile), centerZ);
|
|
console.log("center", center);
|
|
|
|
const model = mat4.create();
|
|
mat4.identity(model);
|
|
mat4.translate(model, model, vec3.fromValues(-centerX, 0, -centerZ));
|
|
|
|
const view = mat4.create();
|
|
mat4.lookAt(view, camera, center, UP);
|
|
|
|
const perspective = mat4.create();
|
|
const aspect = gl.canvas.clientWidth / gl.canvas.clientHeight;
|
|
mat4.perspective(perspective, Math.PI / 4, aspect, 0.5, 2000);
|
|
|
|
const matrix = mat4.create();
|
|
mat4.multiply(matrix, view, model);
|
|
mat4.multiply(matrix, perspective, matrix);
|
|
|
|
const matrixLoc = gl.getUniformLocation(program, "u_matrix");
|
|
gl.uniformMatrix4fv(matrixLoc, false, matrix);
|
|
|
|
console.log("model", model);
|
|
console.log("view", view);
|
|
console.log("projection", perspective);
|
|
console.log("matrix", matrix);
|
|
|
|
const vertices: number[] = [];
|
|
|
|
gat.tiles.forEach((tile, i) => {
|
|
const x = i % gat.width;
|
|
const z = Math.floor(i / gat.width);
|
|
|
|
const topLeft = [x, tile.altitude[Corner.TOP_LEFT], z];
|
|
const topRight = [x + 1, tile.altitude[Corner.TOP_RIGHT], z];
|
|
const bottomLeft = [x, tile.altitude[Corner.BOTTOM_LEFT], z + 1];
|
|
const bottomRight = [x + 1, tile.altitude[Corner.BOTTOM_RIGHT], z + 1];
|
|
|
|
vertices.push(
|
|
...topLeft,
|
|
...bottomLeft,
|
|
...topRight,
|
|
...topRight,
|
|
...bottomLeft,
|
|
...bottomRight
|
|
);
|
|
});
|
|
|
|
let failCount = 0;
|
|
for (let i = 0; i < vertices.length / 3; ++i) {
|
|
const vec = vec3.fromValues(vertices[i], vertices[i + 1], vertices[i + 2]);
|
|
const result = checkPixelCoord(vec, matrix);
|
|
if (result) {
|
|
if (failCount < 10) {
|
|
console.log(vec);
|
|
console.log(result);
|
|
}
|
|
failCount++;
|
|
}
|
|
}
|
|
console.log("failCount", failCount);
|
|
console.log("totalCount", vertices.length / 3);
|
|
|
|
const positionLoc = gl.getAttribLocation(program, "a_position");
|
|
|
|
const vertexBuffer = gl.createBuffer();
|
|
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
|
|
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
|
|
|
|
gl.enableVertexAttribArray(positionLoc);
|
|
|
|
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
|
|
gl.vertexAttribPointer(positionLoc, 3, gl.FLOAT, false, 0, 0);
|
|
|
|
gl.drawArrays(gl.TRIANGLES, 0, vertices.length / 3);
|
|
};
|
|
|
|
const checkPixelCoord = (x: vec3, mat: mat4): vec4 | null => {
|
|
const vec = vec4.fromValues(x[0], x[1], x[2], 1);
|
|
vec4.transformMat4(vec, vec, mat);
|
|
for (let i = 0; i < vec.length; ++i) {
|
|
if (Math.abs(vec[i]) >= 1) {
|
|
return vec;
|
|
}
|
|
}
|
|
return null;
|
|
};
|
|
|
|
const getMaxAltitude = (gat: GAT): number =>
|
|
gat.tiles.reduce(
|
|
(max, tile) =>
|
|
Object.values(tile.altitude).reduce(
|
|
(max1, altitude) => Math.max(max1, altitude),
|
|
max
|
|
),
|
|
0
|
|
);
|
|
|
|
const avgTileHeight = (tile: Tile): number => {
|
|
const heights = [...Object.values(tile.altitude)];
|
|
const s = heights.reduce((x, y) => x + y, 0);
|
|
return s / heights.length;
|
|
};
|