Original Post
Greetings,
I bet the topic has been covered 1000 times, and even myself - I've coded the damn thing again and again through many interations of opengl versions and languages.
This time I am having a hard time debugging, since Javascript and Webgl aren't exactly debugging-friendly.
First: building the ray.
I am using the GLUUnproject function which I found here: https://github.com/fintler/webgl-unproject/blob/master/GLU.js
Simple and clean, not much to explain.
Second: the intersection itself
I've coded this from scratch, basing myself on this paper http://www.cs.virginia.edu/~gfx/Courses/2003/ImageSynthesis/papers/Acceleration/Fast%20MinimumStorage%20RayTriangle%20Intersection.pdf
The code looks like this
Then I drew the simplest triangle ever and checked against it, failing.
I've double checked all the values that I pass to the functions (both GluUnproject and Check), and they're correct.
Nonetheless Check() fails.
I think I may be doing something wrong in creating the ray, possibly because I am using a right-hand camera system.
Do you see anything that may be wrong?
Can someone tell me how to "build" manually a ray that goes straight from the center of the screen to the horizon?
Thanks in advance
andrea
I bet the topic has been covered 1000 times, and even myself - I've coded the damn thing again and again through many interations of opengl versions and languages.
This time I am having a hard time debugging, since Javascript and Webgl aren't exactly debugging-friendly.
First: building the ray.
I am using the GLUUnproject function which I found here: https://github.com/fintler/webgl-unproject/blob/master/GLU.js
Simple and clean, not much to explain.
Second: the intersection itself
I've coded this from scratch, basing myself on this paper http://www.cs.virginia.edu/~gfx/Courses/2003/ImageSynthesis/papers/Acceleration/Fast%20MinimumStorage%20RayTriangle%20Intersection.pdf
The code looks like this
function check(vert0, vert1, vert2, orig, dir)
{
// OUT
var t,u,v;
// VAR
var edge1 = vec3.create();
var edge2 = vec3.create();
var tvec = vec3.create();
var pvec = vec3.create();
var qvec = vec3.create();
var det, inv_det;
// find vectors for edges
vec3.subtract(vert1, vert0, edge1);
vec3.subtract(vert2, vert0, edge2);
// calculate determinant
vec3.cross(dir, edge2, pvec);
det = vec3.dot(edge1, pvec);
// calculations - CULLING ENABLED
vec3.subtract(orig, vert0, tvec);
u = vec3.dot(tvec,pvec);
if(u<0 || u>det) return null;
vec3.cross(tvec, edge1, qvec);
v = vec3.dot(dir, qvec);
if(v<0 || (u+v)>det) return null;
t = vec3.dot(edge2,qvec);
inv_det = 1/det;
t = t * inv_det;
u = u * inv_det;
v = v * inv_det;
return vec3.create([t,u,v]);
}
Then I drew the simplest triangle ever and checked against it, failing.
I've double checked all the values that I pass to the functions (both GluUnproject and Check), and they're correct.
Nonetheless Check() fails.
I think I may be doing something wrong in creating the ray, possibly because I am using a right-hand camera system.
Do you see anything that may be wrong?
Can someone tell me how to "build" manually a ray that goes straight from the center of the screen to the horizon?
Thanks in advance
andrea