Skip to content

Commit

Permalink
complete lesson 2
Browse files Browse the repository at this point in the history
  • Loading branch information
EC2 Default User committed May 20, 2018
1 parent 00b8939 commit 32a1503
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ As of lesson 1, there is no `webpack.config.js`, just run webpack since
## Progress

- [x] Lesson 1
- [ ] Lesson 2
- [x] Lesson 2
- [ ] Lesson 3
207 changes: 206 additions & 1 deletion dist/main.js

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/geometry/sphere.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Sphere {

intersection(ray) {

let cPrime = ray.direction.origin._minus(this.center);
let cPrime = ray.origin._minus(this.center);

// quadratic variables
let a = ray.direction.length() * ray.direction.length();
Expand All @@ -39,4 +39,4 @@ class Sphere {
}
}

export default Geometry;
export default Sphere;
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

import main from './lessons/lesson01';
import main from './lessons/lesson02';

main();
15 changes: 15 additions & 0 deletions src/lessons/lesson02.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

import Scene from '../scenes/scene';
import View from '../view';

function main() {

const view = View.createDefaultView();

let scene = new Scene();
scene.generateRandomSpheres();
let image = view.viewScene(scene);

image.renderInto(document.querySelector('body'));
}
export default main;
35 changes: 33 additions & 2 deletions src/view/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,41 @@ class View {

let pointray = this._bilinearInterpolation(x, y);
let color = scene.backgroundColor;
// loop through scene objects getting T-values
// add color for the lowest to value or add default color
let objects = scene.getObjects();
let bestT = null;
let bestO = null;

for (let i = 0; i < objects.length; ++i) {

let o = objects[i];
let t = o.intersection(pointray.ray);

if (t !== null) {

// console.log(t);

if (bestT === null) {
bestT = t;
bestO = o;
}
else {
if (t < bestT) {
bestT = t;
bestO = o;
}
}
}
}

if (bestO !== null) {
// console.log(bestO.color);
color = bestO.color.normalized();
}
image.putPixel(x, y, color);
}
}

return image;
}
};

Expand Down

0 comments on commit 32a1503

Please sign in to comment.