Click here to Skip to main content
15,891,902 members
Articles / Programming Languages / C#

Yet Another RayTracer for .NET

Rate me:
Please Sign up or sign in to vote.
4.87/5 (56 votes)
29 Mar 2007CPOL15 min read 129.9K   3.9K   124  
This article is meant as an introduction to raytracing and explains the basic techniques to raytrace a scene.
// Copyright 2006 Herre Kuijpers - <herre@xs4all.nl>
//
// This source file(s) may be redistributed, altered and customized
// by any means PROVIDING the authors name and all copyright
// notices remain intact.
// THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED. USE IT AT YOUR OWN RISK. THE AUTHOR ACCEPTS NO
// LIABILITY FOR ANY DATA DAMAGE/LOSS THAT THIS PRODUCT MAY CAUSE.
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;

namespace RayTracer
{
    public class Camera
    {
        public Vector Position;
        public Vector LookAt;
        public Vector Equator;
        public Vector Up; // defines the tilt of the camera
        public Vector Screen; // defines the center position of the viewport/screen in 3D space

        public Camera(Vector position, Vector lookat) : this(position, lookat, new Vector(0,1,0))
        {
        }

        public Camera(Vector position, Vector lookat, Vector up)
        {
            Up = up.Normalize();
            Position = position;
            LookAt = lookat;
            Equator = LookAt.Normalize().Cross(Up);
            Screen = Position + LookAt;
        }

        /// <summary>
        /// returns the ray as it passes through the viewport form the camera perspective
        /// it assumes that the viewport is scaled down to (1,1)-(-1,-1)
        /// </summary>
        /// <param name="vx">x position on the viewport must be between [-1,1]</param>
        /// <param name="vy">y position on the viewport must be between [-1,1]</param>
        /// <returns></returns>
        public Ray GetRay(double vx, double vy)
        {
            Vector pos = Screen - Up * vy - Equator * vx;
            Vector dir = pos - Position;

            Ray ray = new Ray(pos, dir.Normalize());
            return ray;
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Rubicon
Netherlands Netherlands
Currently Herre Kuijpers is employed at Rubicon. During his career he developed skills with all kinds of technologies, methodologies and programming languages such as c#, ASP.Net, .Net Core, VC++, Javascript, SQL, Agile, Scrum, DevOps, ALM. Currently he fulfills the role of software architect in various projects.

Herre Kuijpers is a very experienced software architect with deep knowledge of software design and development on the Microsoft .Net platform. He has a broad knowledge of Microsoft products and knows how these, in combination with custom software, can be optimally implemented in the often complex environment of the customer.

Comments and Discussions