Skip to main content
gamedev.net
🔒 Locked

Eye ray!

Started by edwinnie 1 reply 550+ views
Original Post
edwinnie
edwinnie
hihi! i was trying to compute the eye ray direction. I was having some difficulty computing the right intersection coordinates with the scene bounding box, so i decided to fall back on the eye ray direction... i was using the forumla given somewhere in this forum: //x = xleft + (0.5 + i)dx //y = ytop + (0.5 + j)dy i suppose u all used the near view plane as a start, compute its world coords for its corners, and use the above forumla to determine the ray directions znear is 1.0f origin is "camera eye position" at (0,0,20) IN.position(WPOS) gives the window screen coords near viewplane world coord corners: //topleft(-1.33,1.56,19.99) //topright(2.08,1.56,19.99) //bottomleft(-1.33,-1.56,19.99) //bottomright(2.08,-1.56,19.99)
float3 ComputeRayDirection(Fragment IN, Viewplane viewplane, float2 viewport)
{	
  float3 dir = float3(0,0,0);
  float dx = (viewplane.topright.x - viewplane.topleft.x) / viewport.x;
  float dy = (viewplane.topright.y - viewplane.bottomright.y) / viewport.y;
  dir.x = viewplane.bottomleft.x + (0.5 + IN.position.x) * dx;
  dir.y = viewplane.bottomleft.y + (0.5 + IN.position.y) * dy;
  dir.z = 0;
  dir = normalize(dir - origin);
  return dir;
}
the resulting direction happened to be quite small numbers eg (0.014,0.008,-0.99) does that make sense?? thx! Edwinz
Nik02
Nik02
Your example (0.014,0.008,-0.99) is very close to an unit vector, that is, signifying only direction and not magnitude.
For that part, it makes perfect sense.

-Nik

EDIT: With "very close", I meant "exact, in practice".

[edited by - Nik02 on June 1, 2004 3:18:50 AM]
Niko Suni
edwinnie
edwinnie
hi there!
thx fer replying!

assuming the above is correct, i was trying to use the ray box intersection algo to test fot hits and calculate the actual intersection point.

unfortunately, i could only sense out of 1 coord, the other 2 were gibberish i think.

i am using a uniform cube, with min (-1,-1,-1) and max (1,1,1)
as the bounding box (the cube itself is the bounding box basically)

the camera is at (0,0,5) looking straight at (0,0,-1)
however, i could only get the Z intersection coord correct.

i am using this article as a reference:
RayBox Intersect article</html>

this is the start of the intersection calling function,
which make uses of a helper function to simplifiy code...

int IntersectBoxCoord(CGRay ray, float3 ray_dir, out float3 hit_coord)
{
int result = 1;
float Tnear = -999999.0f;
float Tfar = 999999.0f;

//X plane

result = IntersectHelper(Tnear,Tfar,ray_dir.x,ray.origin.x,float2(box_min.x,box_max.x));

//Y plane

if(result == 1)
result = IntersectHelper(Tnear,Tfar,ray_dir.y,ray.origin.y,float2(box_min.y,box_max.y));

//Z plane

if(result == 1)
result = IntersectHelper(Tnear,Tfar,ray_dir.z,ray.origin.z,float2(box_min.z,box_max.z));

if(result == 1)
hit_coord = ray.origin + Tnear * ray_dir;
else
hit_coord = float3(0,0,0);

return result;
}


the helper fn:

int IntersectHelper(out float Tnear, out float Tfar, float ray_dir, float origin,float2 planeMinMax)
{
int result = 1;

//if ray dir is parallel to plane

if(ray_dir == 0.0f)
{
//planeMinMax could be float2(box_min.x,box_max.x)

// float2(box_min.y,box_max.y)

// float2(box_min.z,box_max.z)

if(origin < planeMinMax.x || origin > planeMinMax.y)
result = 0;
}
else
//ray dir is not parallel to plane

{
//ray: P = O + D.t

//plane: P.N = d

//--> O.N + t * (D.N) = d

//--> t = (d - O.N) / (D.N)

//X plane so N = (-1,0,0) & (1,0,0)


//for each min/max plane

float t1 = (planeMinMax.x - origin) / ray_dir;
float t2 = (planeMinMax.y - origin) / ray_dir;

//swap, always make t1 < t2

if(t1 > t2)
{
float temp = t1;
t1 = t2;
t2 = temp;
}

//set the Tnear & Tfar points

if(t1 > Tnear)
Tnear = t1;
if(t2 < Tfar)
Tfar = t2;

//missed

if(Tnear > Tfar)
result = 0;
else
//behind

if(Tfar < 0)
result = 0;
}
return result;
}


hopefully someone can point out some silly mistakes...
thx!
Edwinz

[edited by - edwinnie on June 1, 2004 5:39:16 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.