Click here to Skip to main content
15,885,546 members
Articles / Mobile Apps

A Raytracer for the Compact Framework

Rate me:
Please Sign up or sign in to vote.
4.62/5 (38 votes)
22 Jun 2004CPOL9 min read 101.2K   541   46  
An implementation of a simple raytracer for the CF as an intro to graphics theory.
using System;
using System.Collections;

namespace raytracer
{
	/// <summary>
	/// raytracing class
	/// </summary>
	public class raytrace
	{
		const int maxobjs=16;

		public raytrace()
		{
			// allocate lists
			m_objects=new objbase [maxobjs];
			m_lightman=new lightmanager();
			m_pos=0;
		}

		public void addobject(objbase obj)
		{
			if(m_pos==maxobjs-1)
				return;

			m_objects[m_pos++]=obj;
		}

		public void addlight(light lgt)
		{
			m_lightman.addlight(lgt);
		}

		public colour getcolourpos(int x,int y)
		{
			vector pos=new vector((float)x,(float)y,100.0f);
			vector eye=new vector(0.0f,0.0f,0.0f);

			ray iray=new ray();
			iray.OriginValue=pos;
			iray.NormalValue=pos-eye;
			
			return shootray(iray);
		}

		private colour shootray(ray iray)
		{
			// allocate colour
			colour col=new colour();

			// closest object
			objbase obj=new objbase();
			IntersectInfo info=new IntersectInfo();

			// test whether ray intersects an object
			if(!intersectiontest(iray,ref obj,ref info))
			{
				col.toblack();
				return col;
			}

			// get the lit colour
			colour litcol=getlitcolour(obj,info,iray);

			// set starting colour
//			col=info.col;
			// light it
//			col*=litcol;

			// limit colour to sensible levels
			litcol.limit();

			return litcol;
		}

		private colour getlitcolour(objbase obj,IntersectInfo info,ray iray)
		{
			// run through the lights
			colour litcol=new colour();
			litcol.toblack();

			for(int lct=0; lct<m_lightman.getnumlights(); lct++)
			{
				// get ray from light to object
				ray lray=m_lightman.getray(lct,info.pos);

				objbase tempobj=new objbase();
				IntersectInfo tempinfo=new IntersectInfo();

				// is there any closer object?
				intersectiontest(lray,ref tempobj,ref tempinfo);

				// no
				if(obj.Equals(tempobj))
					litcol+=m_lightman.getlitcolour(lct,info.pos,info.col,info.normal,obj.ReflectValue,iray.OriginValue);
			}

			return litcol;
		}

		private bool intersectiontest(ray iray,ref objbase closest,ref IntersectInfo info)
		{
			// are there any objects?
			if(m_pos==0)
				return false;

			// set to a large value
			float dist=10000000.0f;

			for(int ct=0; ct<m_pos; ct++)
			{
				// get the present object
				objbase obj=m_objects[ct];

				// test intersection
				IntersectInfo test=obj.intersect(iray);

				// did the ray hit?
				if(test.hit==true)
				{
					// calculate the distance
					float objdist=iray.calcdistvalue(test.pos);

					// is it closer than the last?
					if(objdist<dist)
					{
						dist=objdist;
						closest=obj;
						info=test;
					}
				}
			}
			
			// did the ray hit?
			if(dist==10000000.0f)
				return false;
			else
				return true;
		}

		private int				m_pos;		// position in array
		private objbase	[]		m_objects;	// object list
		private lightmanager	m_lightman;	// light manager
	}
}

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
Software Developer (Senior)
United Kingdom United Kingdom
I was a Reseach Scientist working on pain relief strategies for intractable pain.
When not doing that I am busy trying to finish my PhD in Mathematical Physiology.
When not doing that I write computer gamesSmile | :)

Comments and Discussions