Skip to content

Commit

Permalink
Added sphere intersection code. Looks like an ellipse for some reason…
Browse files Browse the repository at this point in the history
…, need some fix
  • Loading branch information
muraliavarma committed Feb 20, 2013
1 parent c1dd031 commit 3dcaf81
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 16 deletions.
4 changes: 4 additions & 0 deletions core/primitive/Plane.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ Plane.prototype.getIntersection = function(ray) {
return ray.pointAtDistance(t);
}

Plane.prototype.getNormal = function(point) {
return this.normal;
}

module.exports = Plane;
2 changes: 1 addition & 1 deletion core/primitive/Rectangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Rectangle.prototype.getIntersection = function(ray) {
}

Rectangle.prototype.getNormal = function(point) {
return this.normal;
return Plane.prototype.getNormal.call(this, point);
}

module.exports = Rectangle;
28 changes: 25 additions & 3 deletions core/primitive/Sphere.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
function Sphere(x, y, z, r) {
var Vector = require("../common/Vector");

function Sphere(x, y, z, r) {
this.x = x;
this.y = y;
this.z = z;
this.r = r;
}

Sphere.prototype.getIntersection = function(ray) {

}
var a = ray.direction.dot(ray.direction);
var oc = new Vector(ray.origin.x - this.x, ray.origin.y - this.y, ray.origin.z - this.z);
var b = 2 * (oc.dot(ray.direction));
var c = oc.dot(oc) - this.r * this.r;
var detSq = b * b - 4 * a * c;
if (detSq < 0) {
return null;
}
var det = Math.sqrt(detSq);
var t0 = (-b - det) / (2 * a);
var t1 = (-b + det) / (2 * a);
return ray.pointAtDistance(t0); //this is the closer of the two intersection points. the other will be useful for refraction maybe?
}

Sphere.prototype.getNormal = function(point) {
return new Vector(point.x - this.x, point.y - this.y, point.z - this.z);
}

module.exports = Sphere;
21 changes: 14 additions & 7 deletions core/renderer/SimpleRenderer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
var Point = require('../common/Point');
var Ray = require('../common/Ray');
var Vector = require('../common/Vector');

var Plane = require('../primitive/Plane');
var Rectangle = require('../primitive/Rectangle');
var Sphere = require('../primitive/Sphere');

var Color = require('../image/Color');
var PNG = require("pngjs").PNG;
var fs = require('fs');
Expand All @@ -25,6 +28,11 @@ exports.render = function(data, socket) {
var x = aspectRatio * ((w / data.camera.imageWidth) - 0.5);
for (var h = 0; h < data.camera.imageHeight; h++) {
var idx = (png.width * (png.height - h) + w) << 2;
png.data[idx] = 0;
png.data[idx+1] = 0;
png.data[idx+2] = 0;
png.data[idx+3] = 255;

var y = (h / data.camera.imageHeight) - 0.5;
var cameraPlaneIntersectionPoint = cameraPlaneCenter.add(cameraRight.multiply(x)).add(cameraUp.multiply(y));
var ray = new Ray(cameraPos, cameraPos.vectorTo(cameraPlaneIntersectionPoint));
Expand All @@ -40,23 +48,22 @@ exports.render = function(data, socket) {
primitive.height
);
}
else if (primitive.type == "sphere") {
shape = new Sphere(primitive.center.x, primitive.center.y, primitive.center.z, primitive.radius);
}
else {
continue;
}
var intersectionPoint = shape.getIntersection(ray);
if (!intersectionPoint) {
png.data[idx] = 0;
png.data[idx+1] = 0;
png.data[idx+2] = 0;
png.data[idx+3] = 255;
}
else {
if (intersectionPoint) {
var color = _getColor(data.lights, primitive.material, intersectionPoint, shape.getNormal(intersectionPoint));
png.data[idx] = color.r;
png.data[idx+1] = color.g;
png.data[idx+2] = color.b;
png.data[idx+3] = 255;
}
else {
}
}
}
var percent = (100.0 * w/data.camera.imageWidth) | 0;
Expand Down
10 changes: 5 additions & 5 deletions public/js/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ function doRender() {
},{
type: 'sphere',
center: {
x: 10, y: 10, z: 10
x: 5, y: -5, z: 35
},
radius: 5,
radius: 1,
material: {
type: 'diffuse',
color: {
r: 255, g: 0, b: 0
r: 255, g: 0, b: 255
}
}
}],
Expand All @@ -62,8 +62,8 @@ function doRender() {
x: 0, y: 1, z: 0
},
zoom: 1,
imageWidth: 300,
imageHeight: 400
imageWidth: 600,
imageHeight: 800
}
};
socket.emit('doRender', sceneData);
Expand Down

0 comments on commit 3dcaf81

Please sign in to comment.