Dark Bit Factory & Gravity

PROGRAMMING => General coding questions => Topic started by: frea on February 04, 2008

Title: creating rays, deformed image [ 99% solved ;p ]
Post by: frea on February 04, 2008
hi!
I've got some werid trouble with creating rays.

Let E be the eye position, L the look at position. From that and the FOV angles i create two vectors UP and RIGHT, in such way that the viewing screen is defined by
V = L + a*UP + b * RIGHT, |a|,|b| <= 1.

( UP + E and RIGHT + E span the plane in which the screen lies )
Now the ray position is E, and ray direction is V - E.

I try to raytrace few spheres, which lie on the OX axis, with eye position 0,0,-2 and lookat 0,0,0
and fov's enough big to see them all.
that what i do not understand is, why the further from 0,0,0 the more the spheres are deformed.
This looks like i would be doing a fish-eye like efect.

Image:
http://img163.imageshack.us/my.php?image=testhi4.png

i am sure, that the Up and RIGHT vectors are correct, and the ray-sphere intersection code was written using two different algos, both giving the same result.

i think i am making some kind of fundamental mistake, but when i looked and other people's code it was moreless the same.
Title: Re: creating rays, deformed image
Post by: taj on February 05, 2008
Did you remember to normalise your vectors? Specifically L, UP and RIGHT?
Title: Re: creating rays, deformed image
Post by: frea on February 05, 2008
if UP and RIGHT were normalized then the screen size would be always 2x2.
Why normalize L? That would just move the place where the screen is.

Although i could normalize them, and choose a,b according to FOV'S, but is there any diffrence?
Title: Re: creating rays, deformed image
Post by: frea on February 05, 2008
could it be that, for big fov's this disortion appears?
Title: Re: creating rays, deformed image
Post by: taj on February 05, 2008
Ah so L is on the "screen". That wasnt a given before. Well, you may be right but as I dont know how you create your vectors, my guess is that you dont have an orthonormal basis from which to trace which is the most obvious reason for distortion. So either you need to normalise or your up, right are not orthonormal with look in the first place due to some dodgy maths.



Title: Re: creating rays, deformed image
Post by: frea on February 05, 2008
You are right, i forgot to mention that L is on the screen.

i printed my vectors from the test scene:
0 1 -0 UP
-1 0 0 RIGHT
0 0 1 L
orthonormal.

I just discovered that my fov was equal to PI ;). if fov is around PI/6, i get only 1 pixel off for furthest spheres, which is acceptable, but i still think that something is wrong, with my code, or my understanding of things :).



edit: ok i am sure, that i make some kind of stupid understanding mistake. I'll try to find out what exactly, but unfortunatly later ( exams ~~ ).


edit2: couldn't stop thinking :). That what i did was to traverse the world using a moving plane ( d[ 2 ] was the same for all rays ), while i should've traversed with a sphere with increasing radius and fixed origin.
Title: Re: creating rays, deformed image
Post by: taj on February 05, 2008
Quote from: frea on February 05, 2008
could it be that, for big fov's this disortion appears?


Definitely.
Title: Re: creating rays, deformed image [ 99% solved ;p ]
Post by: frea on February 05, 2008
:). So in order to avoid the disortion for high fov's i should generate the rays in an another way? Not as lines going through eye->pixel but as lines going through eye->sphere, where the sphere and screen have 4 common points? I don't know how else i could define this sphere, sorry ;). I'll try to make a draing of that in paint but, i don't know how it will look like :).


http://img210.imageshack.us/my.php?image=beztytuu4do3.jpg
previously i choosed rays so, they would be equaly spaced on the screen quad.
the solution would be to choose rays so that they would be equaly spaced on the screen 'mapped' along the lines eye->sphere on the sphere. I dunno whether that image helps :P.
Title: Re: creating rays, deformed image [ 99% solved ;p ]
Post by: Jim on February 06, 2008
I dunno - the further you stray from a 90degree angle the more weird things are going to look.

Jim
Title: Re: creating rays, deformed image [ 99% solved ;p ]
Post by: rain_storm on February 06, 2008
from what I learnt from simple raycasting using no projection plane will cause a fish-eye distortion. This is because the source of all rays will become a single point in space. I would imagine that using a sphere instead of a flat projection plane will cause this same artifact. To increase the feild of view beyond 90 degrees and below 180 degrees you should move the projection plane closer to the camera source. so that the angle made between the edges and the camera make up the required feild of view.
Title: Re: creating rays, deformed image [ 99% solved ;p ]
Post by: Stonemonkey on February 06, 2008
Had a couple of thoughts about this, one is to interpolate angles across the screen and calculate the vectors using trig and the other is something like this although this might be crap:


//eye_radius>sqr((screen_width*.5)^2+(screen_height*.5)^2)

vx=pixel_x-screen_width*0.5
vy=pixel_y-screen_height*0.5
vz=sqr(eye_radius*eye_radius-vx*vx-vy*vy)

//normalise result
d=1.0/sqr(vx*vx+vy*vy+vz*vz)
vx*=d
vy*=d
vz*=d

Title: Re: creating rays, deformed image [ 99% solved ;p ]
Post by: frea on February 06, 2008
using angles is exacly that what i meant :). although it took me some time to discover this.

rain_storm - i hope you are wrong, but i guess i need to code it and see the results :).
Title: Re: creating rays, deformed image [ 99% solved ;p ]
Post by: taj on February 07, 2008
Quote from: frea on February 06, 2008
using angles is exacly that what i meant :). although it took me some time to discover this.

rain_storm - i hope you are wrong, but i guess i need to code it and see the results :).

Post the results frea. I think rain storm is right - but still your results might be interesting for small FOVs.
Title: Re: creating rays, deformed image [ 99% solved ;p ]
Post by: p01 on February 07, 2008
Using a projection plane is fine as long as you normalize the vectors.
Title: Re: creating rays, deformed image [ 99% solved ;p ]
Post by: frea on February 07, 2008
i normalized them, the same effect.
later i even tried something like

d[ 0 ] = ((float)i)/512.0f;
d[ 1 ] = ((float)j)/512.0f;
d[ 2 ] = 1;
normalize( d );

this chooses evenly from a plane, and this gives incorrect results. i,j were from -256 to 256.

if d[ 2 ] = sqrt( 1 - d[ 0 ] * d[ 0 ] - d[ 1 ] * d[ 1 ] )
everything was correct.

taj, i have for now exams, i'll try in weekend :).
Title: Re: creating rays, deformed image [ 99% solved ;p ]
Post by: taj on February 07, 2008
Quote from: frea on February 07, 2008
i normalized them, the same effect.
later i even tried something like

d[ 0 ] = ((float)i)/512.0f;
d[ 1 ] = ((float)j)/512.0f;
d[ 2 ] = 1;
normalize( d );

this chooses evenly from a plane, and this gives incorrect results. i,j were from -256 to 256.

if d[ 2 ] = sqrt( 1 - d[ 0 ] * d[ 0 ] - d[ 1 ] * d[ 1 ] )
everything was correct.

taj, i have for now exams, i'll try in weekend :).

This is crazy...unless I'm going nuts, the first maths is correct, normalised or not, it should work. Which means one of two things:
a) The bug is somewhere else
b) There is no bug (the image is right).

I think it may be b) there is no bug. Spheres dont have to project as spheres. Maybe your code is 100% correct. Here is an image to show what I mean. Note the sphere in the top left : it doesnt project to a circle, but an off angle ellipse.

At college I once setup forward raytracing (from light to eye). I put a prism on a plane and I got a beautiful rainbow except in the middle of the rainbow was a dark line. I debugged and debugged and couldn't get it to work. Finally assuming I wasn't stupid, I went to the physics lab and borrowed a prism. What do you know...an optical phenomenon of prisms is a dark line in the middle of the rainbow. There was no bug.

I say , move on.
Title: Re: creating rays, deformed image [ 99% solved ;p ]
Post by: rain_storm on February 07, 2008
That looks perfectly normal to me since often times in the real world a perfect sphere such as a soccer ball will cast an eliptical shadow. Out of interest try moving things much further away. If the distortion is not reduced by increased distance than you can infer that your code for calculating distance and for converting the distance to a flat screen co-ordinate are not at fault.
Title: Re: creating rays, deformed image [ 99% solved ;p ]
Post by: frea on February 10, 2008
shooting through a sphere is even worse. for high fov's the middle one is streched on the whole height of screen.
for now the only solution i found is to rotate the rays from the code sample i gave.