marksurnin / computer-graphics-nyuad Goto Github PK
View Code? Open in Web Editor NEWCoursework for Computer Graphics at NYU Abu Dhabi.
Coursework for Computer Graphics at NYU Abu Dhabi.
This issue is created to address the problem of converting a recursive ray tracing algorithm to an iterative one. GLSL for WebGL does not support recursive function calls.
We have a recursive trace function in JavaScript:
//RayTracerStatic.js line 59
function trace(ray, depth){
var color = background_color;
var h = hit(ray);
if(h.obj!==null){
var normal = computeNormal(h.obj, h.point);
var material = Materials[h.obj.material_id];
var viewDir = scale(-1, ray.d);
color = shade(h.point, normal, viewDir, material);
if(depth === 0) return color;
var reflected_dir = add(ray.d, scale(-2*dot(ray.d, normal),normal));
var reflected_ray = {e:h.point, d:reflected_dir};
var reflected_col = trace( reflected_ray, depth-1);
color = add(color, scale(material.reflectivity, reflected_col));
}
return color;
}
Note the role of the last few lines: they recursively calculate the color of the object hit by a reflected ray, multiply reflectivity and add it as a color component of the current object.
var reflected_col = trace( reflected_ray, depth-1);
color = add(color, scale(material.reflectivity, reflected_col));
If we simplify this algorithm into a simple mathematical formula, it is the following:
c_0
= c_1 + r_1 * (c_2 + r_2 * (c_3 + r_3 * (...( c_n ))))
= (c_1 * 1) + (c_2 * r_1) + (c_3 * r_1 * r_2) + ... + (c_n * r_1 * r_2 * ... * r_n-1)
The second expression of c_0
can be evaluated using iteration. We iterate calculating c_i. We keep track of the product of all r_i.
The following is a demonstrative JavaScript code of the above method. The general outline is the same, except that we removed recursion. We are keeping track of the product of all reflectivities. Refer to the comments in the code:
function trace(ray, depth){
var finalColor = vec3(0,0,0);
var noHit = true;
var r = 1;
for (var i = 0; i < depth; i++) {
var h = hit(ray);
if(h.obj!==null){
noHit = false;
var normal = computeNormal(h.obj, h.point);
var material = Materials[h.obj.material_id];
var viewDir = scale(-1, ray.d);
color = shade(h.point, normal, viewDir, material);
var reflected_dir = add(ray.d, scale(-2*dot(ray.d, normal),normal));
ray = {e:h.point, d:reflected_dir}; // now we consider the reflected ray
finalColor = add(finalColor, scale(r, color)); // c += r x c_i
r *= material.reflectivity; // r *= r_i
}
}
return noHit ? background_color : finalColor; // if ray never hit any objects, return bg color
}
Now we have to translate this into GLSL.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.