Skip to content

Commit

Permalink
I fixed the camera class so naturally frustrum culling broke
Browse files Browse the repository at this point in the history
  • Loading branch information
HunterBarclay committed Dec 5, 2023
1 parent 7107dfb commit d57267b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
4 changes: 4 additions & 0 deletions js/app.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { testCullingFrustrum } from "./renderer/culling-frustrum.mjs";
import startHelloWebGL from "./renderer/renderer.mjs";
import { mat4 } from "./util/glMatrix_util.mjs";

window.addEventListener("load", (_) => {

testCullingFrustrum();

startHelloWebGL();
});
8 changes: 5 additions & 3 deletions js/renderer/culling-frustrum.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ export function testCullingFrustrum() {
var cam = new Camera(0.1, 5, 45, 16.0 / 9.0);
var maze = generateMaze(20, 20, 0.1);
maze.print();
cam.setRotation([0.0, 60.0, 0.0]);
// cam.setPosition([5.0, 0.0, 5.0]);
cam.setPosition(maze.startPosition);
// cam.setRotation([0.0, 60.0, 0.0]);
cam.setPosition([5.0, 0.0, 5.0]);
// cam.setPosition(maze.startPosition);
var cullFrus = new CullingFrustrum(cam);

cam.getFrustrumPlanes().forEach(x => x.printEquation());

var layout = maze.getArrayLayout();

function layoutToCoord(x, z) {
Expand Down
13 changes: 11 additions & 2 deletions js/renderer/renderer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,24 @@ function drawScene() {

testCubeMaterial.camera.setRotation([xRot, yRot, 0.0]);

console.log(testCubeMaterial.camera.up.map(x => x.toFixed(2)));

var f = (keys['w'] ? 1.0 : 0.0) + (keys['s'] ? -1.0 : 0.0);
var r = (keys['a'] ? 1.0 : 0.0) + (keys['d'] ? -1.0 : 0.0);
cameraPosition = addVector(cameraPosition, multVector(testCubeMaterial.camera.forward, f * 0.2));
cameraPosition = addVector(cameraPosition, multVector(testCubeMaterial.camera.right, r * 0.2));

testCubeMaterial.camera.setPosition(cameraPosition);

batchMesh.geometry.batchInstances = mazeWalls.filter(x => {
const mat = mat4.create(testCubeMaterial.mvMatrix);
const pos = new Float32Array(3);
pos[0] = x.position[0];
pos[1] = x.position[1];
pos[2] = x.position[2];
const matRes = mat4.create();
const instancePos = mat4.multiplyVec3(mat, pos, matRes);
return cullingFrustrum.testBoundingSphere(instancePos, Math.sqrt(3));
});

setCameraPositionUI(testCubeMaterial.camera.position);
batchMesh.draw(gl);
}
Expand Down

0 comments on commit d57267b

Please sign in to comment.