Advertisement Jump to content
Sign in to follow this  

Ray Caster - What Ray to Use?

This topic is 4878 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

So I'm working on a ray caster. I want everything to be contained in a 1x1x1 box. Why? I want it. :) So my problem is I'm given the viewplane position in the center and the vectors that represent the half width and half height of the viewplace. i.e. viewplane is centered at (0, .5, 0). The "right" vector is (.5, 0, 0). The "up" vector is (.5, 0, 0). So in the end, the viewport is 1x1. I'm iterating my nested loops from -height to height and -width to width. How do I find a ray out of them?

Share this post

Link to post
Share on other sites
I wrote a LOT about generating primary rays in a previous thread. this is all for prospective views and not orthographic views tho. orthographic primary rays VERY simple. also your up vector and right vector are equal, which is impossible. I'm assuming you meant up = (0, .5, 0)

anyway here is the post

conceptually you're breaking up your view planes into H, W boxes where H, W are the hieght and width of your screen in pixels. through each box we send a ray to estimate the color of the pixel at that point.

Given a ray equation

r = e + t*v (where e is eye point and v is the direction of the ray)

the direction of the ray is calcualted by finding the point on the view plane that the ray would go through and subtracting the eye from that point.


Share this post

Link to post
Share on other sites
You have to determine your pixel height and width in world units, then find the center of each pixel. That point - the eye point is your ray vector.

The following should help somewhat. Given a direction and an up vector, it sets up a matrix to convert from camera to world coordinates.

It then finds the direction of the ray in camera coordinates, and converts it into world coordinates based on the above conversion matrix.

void RayTracer::generateRays()

//create resX x resY number of rays
rays = new Ray*[camera.m_resY];
for(int r = 0; r < camera.m_resY; r++)
rays[r] = new Ray[camera.m_resX];

//setup view matrix to convert from camera coords to world coords
Vector3D w = camera.m_direction.normalize();
Vector3D u = (w % camera.m_up).normalize();
Vector3D v = u % w;

float near = 1; //puts near plane 1 unit away from camera origin
float aspect = (float)camera.m_resX /(float)camera.m_resY; //image width / image height

float sceneHeight = (2 * near * (float)tan(0.5*(camera.m_fovX*3.14159/180)));
float sceneWidth = aspect*sceneHeight;

//generate rays
for(int y = 0; y < camera.m_resY; y++)
for(int x = 0; x < camera.m_resX; x++)

rays[y][x].o = camera.m_origin;

//this generates a point that is the center of the pixel at location (x,y) on the image plane
//in camera coordinates
Vector3D direction((sceneWidth*((x+0.5)/camera.m_resX - 0.5)),
(sceneHeight*(0.5 - (y+0.5)/camera.m_resY)),
Vector3D d(u.x*direction.x + v.x*direction.y + w.x*direction.z,
u.y*direction.x + v.y*direction.y + w.y*direction.z,
u.z*direction.x + v.z*direction.y + w.z*direction.z);
rays[y][x].d = d.normalize();
rays[y][x].ax = 1/rays[y][x].d.x;
rays[y][x].ay = 1/rays[y][x].d.y;
rays[y][x].az = 1/rays[y][x].d.z;
rays[y][x].shadow = false;

The important thing to note is that the ray origin (rays[][].o) is equal to the eye's location in space, and that rays[][].d represents the direction of the ray. You can ignore the .ax,.ay,.az and .shadow parameters. That was used by bounding volume hierarchy and shadowing.

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!