# An old, obfuscated raytracer

During my undergraduate days, I had a module in programming. We were taught the basics of C++, but I had already been programming in it for many years so I got very little out of it.

We had to do a small project so I chose to do a raytracer. I am of the very strong opinion that writing a raytracing is an amazing way to learn not just graphics, but maths and general programming too.

The beauty of writing a raytracer is that very quickly you get to see something. The joy of seeing a perfect white circle on a black background (which means your ray set up and sphere intersection functions are working properly) for the first time is indescribable. Not only that, but it’s great for visual debugging. You can easily see if your normals are inverted or your camera matrix is skewed. Very convenient.

I will be talking about raytracing more on this blog, perhaps writing a tutorial and hopefully developing a simple raytracer from scratch.

But for now, I will just dump my old (nearly 20 year old) obfuscated raytracer here. It includes fake soft shadows which deserves a post all on its own.

```#include <stdio.h>
#include <math.h>
typedef struct{float x,y,z;}v;const int S=1024,NS=5,NL=2;float T,Tm,A,sh;float s[]={{-12.73,-12.73,50,1,0,0,8,50,.3},{-12.73,12.73,50,0,1,0,8,50,.3},{12.73,12.73,50,0,0,1,8,50,.3},{12.73,-12.73,50,1,1,0,8,50,.3},{0,0,50,1,1,1,10,50,.6}};float l[]={{-150,400,-20,.7,.7,.7,50},{350,100,-400,.4,.3,.35,50}};float dot(v a,v b){return a.x*b.x+a.y*b.y+a.z*b.z;}float norm(v *V){T=sqrt(dot(*V,*V));V->x/=T,V->y/=T,V->z/=T;return T;}int sect(int j,v o,v d,float &t){o.x-=s[j],o.y-=s[j],o.z-=s[j];t=-dot(o,d);t-=sqrt(s[j]*s[j]-dot(o,o)+t*t);if(t>.01)return 1;return 0;}v trace(v o,v d,int D){int h,i,j,k;v c,n,L,J;c.x=.3,c.y=.5,c.z=.8;for(Tm=999,h=0,i=NS;i--; )if(sect(i,o,d,T))if(T<Tm)Tm=T,h=i+1;if(h--){o.x+=d.x*Tm,o.y+=d.y*Tm,o.z+=d.z*Tm;n.x=o.x-s[h],n.y=o.y-s[h],n.z=o.z-s[h],norm(&n);T=dot(n,d)*2,d.x-=n.x*T,d.y-=n.y*T,d.z-=n.z*T;if(D--)c=trace(o,d,D);c.x*=s[h],c.y*=s[h],c.z*=s[h];c.x+=.1,c.y+=.1,c.z+=.1;for(j=NL;j--; ){L.x=l[j]-o.x,L.y=l[j]-o.y,L.z=l[j]-o.z,A=norm(&L);for(sh=1,i=NS;i--; ){J.x=s[i]-o.x,J.y=s[i]-o.y,J.z=s[i]-o.z;T=dot(L,J);if(T>0&&T<A){J.x-=L.x*T,J.y-=L.y*T,J.z-=L.z*T,T=l[j]*T/A;T=(sqrt(dot(J,J))-s[i]+T)/(T+T);T=(T<0)?0:(T>1)?1:T;sh*=T;}}T=dot(L,n),T=(T>0)?T*(1-s[h])*sh:0;c.x+=l[j]*s[h]*T,c.y+=l[j]*s[h]*T,c.z+=l[j]*s[h]*T;T=dot(L,d),T=(T>0)?pow(T,s[h]):0;T*=sh;c.x+=l[j]*T,c.y+=l[j]*T,c.z+=l[j]*T;}}return c;}int main(){char P;v o,d,c;short h[]={0,2,0,0,0,0,S,S,24};FILE *f=fopen("1.tga","wb");fwrite(h,18,1,f);for(int i=S*S;i--; ){o.x=o.y=o.z=0,d.x=S/2-i%S,d.y=S/2-i/S,d.z=S,norm(&d);c=trace(o,d,5);P=(c.z>1)?255:(char)(c.z*255),P=(c.y>1)?255:(char)(c.y*255),P=(c.x>1)?255:(char)(c.x*255);fwrite(&P,3,1,f);if(!(i%S))printf("%d ",i/S);}fclose(f);return 0;}
```

This code, when compiled and run, will spit out a TGA file that looks like this:

If you are learning programming, I highly recommend pulling the above code apart. Start off by making an empty C++ project and copying the code into main.cpp. Get it building and running and spitting out an image (in the same directory as the project). Then start putting in sane whitespace, separating out structures and functions, and also changing the names of variables into something more meaningful once you’ve worked out what they are for.

The code is not great, I was still learning a lot myself when I wrote it, but you might learn a trick or two. Let me know how you get on, or any problems with compilation.

Have fun! 