Skip to content

Instantly share code, notes, and snippets.

@Darthfett
Created December 1, 2011 22:14
Show Gist options
  • Save Darthfett/1420284 to your computer and use it in GitHub Desktop.
Save Darthfett/1420284 to your computer and use it in GitHub Desktop.
Camera camera = m_Scene.m_Camera;
Vector cameraPosition = camera.GetPosition();
// Camera Coordinates directions
Vector negZ = (camera.GetTarget() - cameraPosition).Normalize();
Vector posY = camera.GetUp().Normalize();
Vector posX = negZ.Cross(posY).Normalize();
float FOV = camera.GetFOV();
float nearZ = camera.GetNearClip();
float farZ = camera.GetFarClip();
float aspectRatio = ((float) WINDOW_WIDTH / (float) WINDOW_HEIGHT);
float halfScreenHeight = nearZ * tan(FOV * PI / 180);
float halfScreenWidth = aspectRatio * halfScreenHeight;
// Position of pixel -- Screen pixels go from X = [-halfScreenWidth , halfScreenWidth], Y = [-halfScreenHeight, halfScreenHeight]
Vector relativePosition = posX * (screenX - halfScreenWidth) + posY * (screenY - halfScreenHeight) + negZ * nearZ;
Vector pixelPosition = cameraPosition + relativePosition;
Vector rayDirection = (pixelPosition - cameraPosition).Normalize();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment