# Creating a camera for my ray tracer

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

## Recommended Posts

Hi everyone,

I'm trying to create a class to manage my camera for my ray tracer. I've got field of view, aspect ratio, a look direction, an up direction and a right direction, but I don't know how to translate this into ray directions which I can shoot from my camera position - can anyone help? I think some example code would help me best!

Thanks :)

##### Share on other sites

dir=right*x+up*y+lookdir;


x and y are -1 to 1, mapped from the screen pixel coordinate. if you want to apply the fov, simply multiply x and y by the horizontal respectably vertical fov.

yes, it's that easy to start :)

##### Share on other sites
i was using

       float x_ = ((float)(-pixels_x/2 + x))/pixels_x;
float y_ = ((float)(-pixels_y/2 + y))/pixels_y;

float dx =  x_*physical_window_width;
float dy =  y_*physical_window_height;
float dz =  z_physical_distance_to_screen;

ray.origin.x = cameraPos.x + cameraLeft.x*dx + cameraUp.x*dy;
ray.origin.y = cameraPos.y + cameraLeft.y*dx + cameraUp.y*dy;
ray.origin.z = cameraPos.z + cameraLeft.z*dx + cameraUp.z*dy;

ray.direction.x = cameraLeft.x*dx + cameraUp.x*dy + cameraDir.x*dz;
ray.direction.y = cameraLeft.y*dx + cameraUp.y*dy + cameraDir.y*dz;
ray.direction.z = cameraLeft.z*dx + cameraUp.z*dy + cameraDir.z*dz;

normalize(&ray.direction);


ps could your maybe say a bit about your intersection routines or lightning (I mean pixel color) calculation routine .. i wonder how people do that (though im f**n tired today)
Edited by fir

##### Share on other sites

OK, it's working :) I'm also interested in the proper way to shade pixels - do people use the phong model? What's the proper way to combine refraction, reflection and surface colours?

##### Share on other sites

Hey this is a great topic, I was searching for the something thans for your help

Edited by Gerretke

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634134
• Total Posts
3015743
×