Advertisement Jump to content
Sign in to follow this  

making rays

This topic is 1803 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am doing some basic raytracer but got some problem with simple question

of generating starting 'eye' - ray for each pixel of the viewport


lets assume I got a viewport bitmap of size pixels_x pixels_y (for example 320x200)

for each pixel (x,y) i need "float3 RayOrigin " and "float3 RayDirection" for this,

how formula to use to get this?



Share this post

Link to post
Share on other sites

This depends on the kind of projection. In raytracing the common kinds perspective and orthogonal projection are possible, but also less common ones like oblique projections, and even exotic things like panoramic projections. Furthermore is it possible to generate the rays in view space and to transform it into world space, or else to generate them in world space directly; the former one is usually easier. You further need to define along which of the 6 principle axis directions you want to be the forward looking direction. Oh, and over-sampling and jitter plays a role, too (forgotten these in my first write). Without defining such things, the possibilities are too many for a detailed description here.


In general you have 2 things to consider: The eye point, if the projection uses one, and the sample point on the view plane. From these you can define the both parts required for a ray r, an "origin" r0 and a direction rd:

    r( t ) := r0 + t * rd


The eye point (used by e.g. perspective projection) defines a point trough which each ray passes, and as such gives a fine origin r0 for the rays. In view space the eye point is (0,0,0), while in world space the eye point is defined as the positional part of the observer's world transform.


Parallel projections have rays that do not pass through a common point, but they make a defined angle with the view plane. The orthogonal projection, for example, makes a 90° angle with the view plane (in other words, the direction rd is equal to the normal (or perhaps its inverse) of the view plane at the position of the sample).


EDIT: ((Forgotten to tell about sampling)) The view is usually rectangular with a a resolution of (Vx, Vy) many pixels. If the view plane is also rectangular, then is has a size of say (w,h). (If the view plane is not rectangular, then you need to map it to a rectangle.) Without over-sampling or jittering, each ray passes through a center of a rectangular portion of the view plane. There are (Vx, Vy) many such portions, each one being w/Vx by h/Vy in size. To find the centers, you need to add half of that size to the sample positions. Hence the sample point in local view plane co-ordinates are at

   ( -w/2 + w/2/Vx + i * w/V, -h/2 + h/2/Vy + j * h/Vy ) for 0 <= i < Vx and 0 <= j < Vy

assuming a symmetric viewport.

Edited by haegarr

Share this post

Link to post
Share on other sites

allright tnx, i resolved this problem by simply taking the screen width size (38cmx30cmx100cm far to eye) and making a ray from this,  this showed to be easy

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!