Skip to content

Commit

Permalink
Fix bug in Lambert shading
Browse files Browse the repository at this point in the history
  • Loading branch information
staceytay committed Apr 20, 2016
1 parent 4ad980a commit 5a933f2
Showing 1 changed file with 20 additions and 24 deletions.
44 changes: 20 additions & 24 deletions raytracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,9 @@ let createKernel = (mode) => {
let rayVz = unitVectorZ (sumVx, sumVy, sumVz)

// 2. Get first intersection, if any.
var closest = this.constants.THINGSCOUNT
var closestDistance = this.constants.INFINITY
for (var i = 0; i < this.constants.THINGSCOUNT; i++) {
let closest = this.constants.THINGSCOUNT
let closestDistance = this.constants.INFINITY
for (let i = 0; i < this.constants.THINGSCOUNT; i++) {
let distance = sphereIntersectionDistance (
things[i][9], things[i][10], things[i][11], things[i][12],
rayPx, rayPy, rayPz, rayVx, rayVy, rayVz
Expand Down Expand Up @@ -305,7 +305,7 @@ let createKernel = (mode) => {
// 3a. Compute Lambert shading.
let lambertAmount = 0
if (lambertianReflectance > 0 && lambert > 0) {
for (var i = 0; i < this.constants.LIGHTSCOUNT; i++) {
for (let i = 0; i < this.constants.LIGHTSCOUNT; i++) {
// Check is if light is visible on this point.
let LPx = px - lights[i][0]
let LPy = py - lights[i][1]
Expand All @@ -314,16 +314,15 @@ let createKernel = (mode) => {
let uLPy = unitVectorY (LPx, LPy, LPz)
let uLPz = unitVectorZ (LPx, LPy, LPz)

// var closest = this.constants.THINGSCOUNT
var closestDistance = this.constants.INFINITY
for (var i = 0; i < this.constants.THINGSCOUNT; i++) {
let closestDistance = this.constants.INFINITY
for (let j = 0; j < this.constants.THINGSCOUNT; j++) {
// Find sphere intersection distance from light source
let distance = this.constants.INFINITY
let EOx = things[i][9] - px
let EOy = things[i][10] - py
let EOz = things[i][11] - pz
let EOx = things[j][9] - px
let EOy = things[j][10] - py
let EOz = things[j][11] - pz
let v = (EOx * uLPx) + (EOy * uLPy) + (EOz * uLPz)
let radius = things[i][12]
let radius = things[j][12]
let discriminant = (radius * radius)
- ((EOx * EOx) + (EOy * EOy) + (EOz * EOz))
+ (v * v)
Expand All @@ -333,7 +332,6 @@ let createKernel = (mode) => {
}

if (distance < closestDistance) {
// closest = i
closestDistance = distance
}
}
Expand Down Expand Up @@ -374,9 +372,9 @@ let createKernel = (mode) => {

// Trace, again, to calculate reflection colour.
// Get first intersection, if any.
var closest = this.constants.THINGSCOUNT
var closestDistance = this.constants.INFINITY
for (var i = 0; i < this.constants.THINGSCOUNT; i++) {
let closest = this.constants.THINGSCOUNT
let closestDistance = this.constants.INFINITY
for (let i = 0; i < this.constants.THINGSCOUNT; i++) {
let distance = sphereIntersectionDistance (
things[i][9], things[i][10], things[i][11], things[i][12],
rRayPx, rRayPy, rRayPz, rRayVx, rRayVy, rRayVz
Expand Down Expand Up @@ -420,7 +418,7 @@ let createKernel = (mode) => {
// 3a. Compute Lambert shading.
let rlambertAmount = 0
if (lambertianReflectance > 0 && rlambert > 0) {
for (var i = 0; i < this.constants.LIGHTSCOUNT; i++) {
for (let i = 0; i < this.constants.LIGHTSCOUNT; i++) {
// Check is if light is visible on this point.
let LPx = px - lights[i][0]
let LPy = py - lights[i][1]
Expand All @@ -429,16 +427,15 @@ let createKernel = (mode) => {
let uLPy = unitVectorY (LPx, LPy, LPz)
let uLPz = unitVectorZ (LPx, LPy, LPz)

var closest = this.constants.THINGSCOUNT
var closestDistance = this.constants.INFINITY
for (var i = 0; i < this.constants.THINGSCOUNT; i++) {
let closestDistance = this.constants.INFINITY
for (let j = 0; j < this.constants.THINGSCOUNT; j++) {
// Find sphere intersection distance from light source
let distance = this.constants.INFINITY
let EOx = things[i][9] - px
let EOy = things[i][10] - py
let EOz = things[i][11] - pz
let EOx = things[j][9] - px
let EOy = things[j][10] - py
let EOz = things[j][11] - pz
let v = (EOx * uLPx) + (EOy * uLPy) + (EOz * uLPz)
let radius = things[i][12]
let radius = things[j][12]
let discriminant = (radius * radius)
- ((EOx * EOx) + (EOy * EOy) + (EOz * EOz))
+ (v * v)
Expand All @@ -448,7 +445,6 @@ let createKernel = (mode) => {
}

if (distance < closestDistance) {
closest = i
closestDistance = distance
}
}
Expand Down

0 comments on commit 5a933f2

Please sign in to comment.