Skip to content

Commit 437d6b7

Browse files
committed
Octree improvment / Mesh scaling positioning
filter duplicate objects
1 parent a9ee60c commit 437d6b7

File tree

5 files changed

+20
-12
lines changed

5 files changed

+20
-12
lines changed

read_obj.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
// DO NOT change the content of this file except for implementing the computeNormals() function
33
// to load a mesh you need to call: var myMesh = readOBJ('./data/mesh.obj');
44

5-
function readOBJ(path) {
5+
function readOBJ(path, position, scale) {
66
console.log("Reading OBJ file: " + path);
7-
var obj = new Mesh();
7+
var obj = new Mesh(position, scale);
88
var req = new XMLHttpRequest();
99
req.open('GET', path, false);
1010

src/objects/mesh.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var Mesh = function() {
1+
var Mesh = function(_position, _scale) {
22
this.V = new Array(); // array of vertices
33
this.F = new Array(); // array of triangles
44
this.N = new Array(); // array of normals
@@ -7,8 +7,8 @@ var Mesh = function() {
77

88
this.material = null;
99

10-
this.position = null;
11-
this.scale = null;
10+
this.position = _position;
11+
this.scale = _scale;
1212

1313
this.octree = new Octree (null, 0);
1414
};
@@ -21,9 +21,9 @@ Mesh.prototype.generateTriangles = function () {
2121
for (var i = 0; i < this.F.length; i++) {
2222
var face = this.F[i];
2323
var triangle = new Triangle (
24-
this.V[face.e(1)],
25-
this.V[face.e(2)],
26-
this.V[face.e(3)],
24+
this.V[face.e(1)].multiply(this.scale).add(this.position),
25+
this.V[face.e(2)].multiply(this.scale).add(this.position),
26+
this.V[face.e(3)].multiply(this.scale).add(this.position),
2727
null
2828
);
2929
this.triangles[i] = triangle;
@@ -37,6 +37,11 @@ Mesh.prototype.intersects = function (ray) {
3737
var min_intersection = null;
3838

3939
var objects = RayConfig.octree ? this.octree.getIntersectionObjects(ray) : this.triangles;
40+
//console.rlog("triangle intersection tests (before): " + objects.length);
41+
objects = objects.filter (function (elem, pos) {
42+
return objects.indexOf(elem) == pos;
43+
});
44+
//console.rlog("triangle intersection tests (after): " + objects.length);
4045
for (var i = 0; i < objects.length; i++) {
4146

4247
var intersection = objects[i].intersects(ray);

src/objects/octree.js

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Octree.prototype.loadOctree = function (objects) {
6363
}
6464
}
6565

66+
// http://www.brandonpelfrey.com/blog/coding-a-simple-octree/
6667
Octree.prototype.insertObject = function (object) {
6768

6869
if (this.depth >= RayConfig.octree_depth) {

src/raytracer.js

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ function intersect (ray) {
66

77
// loop through all objects in the scene
88
var objects = RayConfig.octree ? scene.octree.getIntersectionObjects(ray) : scene.objects;
9+
//console.rlog("object intersection tests (before): " + objects.length);
10+
objects = objects.filter (function (elem, pos) {
11+
return objects.indexOf(elem) == pos;
12+
});
13+
//console.rlog("object intersection tests (after): " + objects.length);
914
for (var i = 0; i < objects.length; i++) {
1015
// intersect with object -> returns distance
1116
var intersection = objects[i].intersects(ray);

src/scene/scene.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ var Scene = function (_globalAmbientIntensity) {
55
this.camera = null;
66

77
this.octree = new Octree(null, 0);
8-
this.bounding = null;
98
};
109

1110
Scene.prototype.addLight = function (_light) {
@@ -46,9 +45,7 @@ function loadScene() {
4645
loadA1();
4746
}
4847

49-
if (RayConfig.octree) {
50-
scene.octree.loadOctree (scene.objects);
51-
}
48+
if (RayConfig.octree) scene.octree.loadOctree (scene.objects);
5249

5350
console.log("scene loaded");
5451
}

0 commit comments

Comments
 (0)