From 4c50b94f6aaf1a2d0770f3aacbfcf80056afac2e Mon Sep 17 00:00:00 2001 From: Lucas Dower Date: Sat, 3 Jul 2021 15:20:55 +0100 Subject: [PATCH] Refactor --- src/camera.js | 2 +- src/client.js | 16 +++++++++--- src/hash_map.js | 60 ++++++++++++++++++++++++++++++++++++++++++++ src/renderer.js | 37 ++++++++++++++++++++++++--- src/vector.js | 11 ++++++++ src/voxel_manager.js | 31 ++++++----------------- 6 files changed, 125 insertions(+), 32 deletions(-) create mode 100644 src/hash_map.js diff --git a/src/camera.js b/src/camera.js index 9ff1e71..031f399 100644 --- a/src/camera.js +++ b/src/camera.js @@ -10,7 +10,7 @@ class ArcballCamera { this.zNear = zNear; this.zFar = zFar; - this.actualDistance = 10.0; + this.actualDistance = 15.0; this.actualAzimuth = 0.6; this.actualElevation = 1.3; diff --git a/src/client.js b/src/client.js index 857db89..9e6f187 100644 --- a/src/client.js +++ b/src/client.js @@ -3,14 +3,22 @@ const { Vector3 } = require('./src/vector.js'); const { Triangle } = require('./src/triangle.js'); const { VoxelManager } = require('./src/voxel_manager.js'); -const renderer = new Renderer(); -const voxelManager = new VoxelManager(0.25); +const voxelSize = 0.1; +const renderer = new Renderer(voxelSize); +const voxelManager = new VoxelManager(voxelSize); const triangle = new Triangle(new Vector3(0, 0, 0), new Vector3(4, 3, 1), new Vector3(2, -3, -2)); +const triangle2 = new Triangle(new Vector3(5, 2, -1), new Vector3(-2, 3, -1), new Vector3(0, 3, 2)); +console.log(triangle2); renderer.setStroke(new Vector3(1.0, 0.0, 0.0)); -renderer.registerTriangle(triangle.v0, triangle.v1, triangle.v2); +renderer.registerTriangle(triangle); +renderer.registerTriangle(triangle2); -voxelManager.voxeliseTriangle(triangle, renderer); +voxelManager.voxeliseTriangle(triangle); +voxelManager.voxeliseTriangle(triangle2); + +renderer.setStroke(new Vector3(1.0, 1.0, 1.0)); +renderer.registerVoxels(voxelManager.voxels); renderer.compileRegister(); diff --git a/src/hash_map.js b/src/hash_map.js new file mode 100644 index 0000000..d064944 --- /dev/null +++ b/src/hash_map.js @@ -0,0 +1,60 @@ +const { Vector3 } = require('./vector.js'); + +class HashMap { + + constructor(numBins) { + this.numBins = numBins; + this.bins = new Array(numBins); + } + + _getBin(key) { + const hash = key.hash(); // A bit naughty + return Math.abs(hash) % this.numBins; + } + + add(key, value) { + const binIndex = this._getBin(key); + console.log(binIndex); + + if (!this.bins[binIndex]) { + this.bins[binIndex] = [ {key: key, value: value} ]; + } else { + this.bins[binIndex].push({key: key, value: value}); + } + } + + get(key) { + const binIndex = this._getBin(key); + + if (!this.bins[binIndex]) { + return; + } + + const list = this.bins[binIndex]; + for (const item of list) { + if (item.key.equals(key)) { + return item.value; + } + } + } +} + +const hashMap = new HashMap(4); +const v = new Vector3(2.0, 2.0, 3.0); +const v2 = new Vector3(2.0, 2.0, 5.0); +const v3 = new Vector3(-2.0, -2.0, -5.0); +const v4 = new Vector3(-2.0, -5.0, -7.0); + +hashMap.add(v, true); +hashMap.add(v2, true); +hashMap.add(v3, true); + +console.log(hashMap.bins[0]); +console.log(hashMap.bins[1]); +console.log(hashMap.bins[2]); +console.log(hashMap.bins[3]); + +console.log(hashMap.get(v)); +console.log(hashMap.get(v2)); +console.log(hashMap.get(v3)); +console.log(hashMap.get(v4)); \ No newline at end of file diff --git a/src/renderer.js b/src/renderer.js index 92bb910..c5687e2 100644 --- a/src/renderer.js +++ b/src/renderer.js @@ -7,7 +7,7 @@ const shaderManager = require('./shaders.js'); class Renderer { - constructor() { + constructor(voxelSize) { this._gl = document.querySelector("#c").getContext("webgl"); this._fov = 30; @@ -25,6 +25,9 @@ class Renderer { }; this._maxIndex = 0; + //this._voxelSize = voxelSize; + this._voxelSizeVector = new Vector3(voxelSize, voxelSize, voxelSize); + this._registerOpen = true; } @@ -129,6 +132,7 @@ class Renderer { }; } + /* // Use when immediate drawing drawBox(centre, size) { const data = this._getBoxData(centre, size); @@ -140,18 +144,43 @@ class Renderer { const data = this._getTriangleData(a, b, c); this._drawData(data); } + */ // Use when drawing the same thing each frame - registerBox(centre, size) { - const data = this._getBoxData(centre, size); - this._addDataToRegister(data); + registerBox(centre, size, debug) { + if (debug) { + const data = this._getBoxData(centre, size); + this._addDataToRegister(data); + } else { + // TODO + } } // Use when drawing the same triangle each frame + /* registerTriangle(a, b, c) { const data = this._getTriangleData(a, b, c); this._addDataToRegister(data); + }*/ + registerTriangle(triangle, debug) { + if (debug) { + const data = this._getTriangleData(triangle.v0, triangle.v1, triangle.v2); + this._addDataToRegister(data); + } else { + // TODO + } + } + + registerVoxel(centre) { + const data = this._getBoxData(centre, this._voxelSizeVector); + this._addDataToRegister(data); + } + + registerVoxels(voxelCentres) { + for (const voxelCentre of voxelCentres) { + this.registerVoxel(voxelCentre); + } } _addDataToRegister(data) { diff --git a/src/vector.js b/src/vector.js index 7f94585..b65dbbb 100644 --- a/src/vector.js +++ b/src/vector.js @@ -84,6 +84,17 @@ class Vector3 { ); } + hash() { + const p0 = 73856093; + const p1 = 19349663; + const p2 = 83492791; + return (this.x * p0) ^ (this.y * p1) ^ (this.z * p2); + } + + equals(vec) { + return this.x == vec.x && this.y == vec.y && this.z == vec.z; + } + } module.exports.Vector3 = Vector3; \ No newline at end of file diff --git a/src/voxel_manager.js b/src/voxel_manager.js index a5483e6..7d0650d 100644 --- a/src/voxel_manager.js +++ b/src/voxel_manager.js @@ -5,26 +5,7 @@ class VoxelManager { constructor(voxelSize) { this._voxelSize = voxelSize; - } - - cubeifyAABB(aabb, gridSnap) { - const size = aabb.size; - const maxDimension = Math.max(size.x, size.y, size.z); - let newSize = new Vector3(maxDimension, maxDimension, maxDimension); - - if (gridSnap) { - let newCentre = Vector3.divScalar(aabb.centre, this._voxelSize); - newCentre = Vector3.round(newCentre); - newCentre = Vector3.mulScalar(newCentre, this._voxelSize); - - const offset = Vector3.sub(aabb.centre, newCentre); - console.log(offset); - - //let newSize - - return new AABB(newCentre, newSize); - } - return new AABB(aabb.centre, newSize); + this.voxels = []; } _getTriangleCubeAABB(triangle) { @@ -44,10 +25,11 @@ class VoxelManager { return cubeAABB; } - voxeliseTriangle(triangle, renderer) { + voxeliseTriangle(triangle) { const cubeAABB = this._getTriangleCubeAABB(triangle); - renderer.setStroke(new Vector3(1.0, 1.0, 1.0)); + //renderer.setStroke(new Vector3(1.0, 1.0, 1.0)); + //let voxels = []; let queue = [cubeAABB]; while (queue.length > 0) { @@ -58,10 +40,13 @@ class VoxelManager { queue.push(...aabb.subdivide()); } else { // We've reached the voxel level, stop - renderer.registerBox(aabb.centre, aabb.size); + //renderer.registerBox(aabb.centre, aabb.size); + this.voxels.push(aabb.centre); } } } + + //return voxels; } }