JURVANOERLE.nl

This C++ application compiled with DevCPP creates a camera, some lights, a ground plain and a few reflecting bowls. It then fires rays from the camera into the world and where-ever they get obstructed, is where an object resides and the ray cannot pass. The same is done for light and if the objects are shining, this effect will reoccur until the ray is completely absorbed.
This is a very costly method in CPU time as thousands of rays need to be shot and calculated but the effects are amazing.

Here is the source code.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cmath>
#include <limits>
#include <vector>

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>

#define MAX_ ( X, Y )( ( ( X ) > ( Y ) ) ? ( X ) : ( Y ) )
#define MIN_ ( X, Y )( ( ( X ) < ( Y ) ) ? ( X ) : ( Y ) )

#include "Color.h"
#include "Vect.h"
#include "Camera.h"
#include "Ray.h"
#include "Light.h"
#include "Shere.h"
#include "Plane.h"
#include "Object.h"
#include "Source.h"

using namespace std;



struct RGBType
{
	double r;
	double g;
	double b;
};

void saveBitmap( const char *filename, int width, int height, int dpi, RGBType *pxs )
{
	FILE *file;
	int	totalPxs	= width * height;
	int size		= totalPxs * 4; //the size of the image that we need to reserve in bytes.
	int totalSize	= 54 + size;
	
	double factor	= 39.375;
	int m			= static_cast<int>( factor );
	int ppm			= dpi * m;
	unsigned char bmpFileHeader[14]	= { 'B', 'M', 0,0,0,0, 0,0,0,0, 54,0,0,0 };												  
	unsigned char bmpInfoHeader[40]	= { 40,0,0,0, 0,0,0,0, 0,0,0,0, 1,0,24,0 };
	
	bmpFileHeader[2]		= ( unsigned char )( totalSize );
	bmpFileHeader[3]		= ( unsigned char )( totalSize >> 8 );
	bmpFileHeader[4]		= ( unsigned char )( totalSize >> 16 );
	bmpFileHeader[5]		= ( unsigned char )( totalSize >> 24 );
	
	bmpInfoHeader[4]		= ( unsigned char )( width );
	bmpInfoHeader[5]		= ( unsigned char )( width >> 8 );
	bmpInfoHeader[6]		= ( unsigned char )( width >> 16 );
	bmpInfoHeader[7]		= ( unsigned char )( width >> 24 );
	
	bmpInfoHeader[8]		= ( unsigned char )( height );
	bmpInfoHeader[9]		= ( unsigned char )( height >> 8 );
	bmpInfoHeader[10]		= ( unsigned char )( height >> 16 );
	bmpInfoHeader[11]		= ( unsigned char )( height >> 24 );
	
	bmpInfoHeader[21]		= ( unsigned char )( size );
	bmpInfoHeader[22]		= ( unsigned char )( size >> 8 );
	bmpInfoHeader[23]		= ( unsigned char )( size >> 16 );
	bmpInfoHeader[24]		= ( unsigned char )( size >> 24 );
	
	bmpInfoHeader[25]		= ( unsigned char )( ppm );
	bmpInfoHeader[26]		= ( unsigned char )( ppm >> 8 );
	bmpInfoHeader[27]		= ( unsigned char )( ppm >> 16 );
	bmpInfoHeader[28]		= ( unsigned char )( ppm >> 24 );
	
	bmpInfoHeader[29]		= ( unsigned char )( ppm );
	bmpInfoHeader[30]		= ( unsigned char )( ppm >> 8 );
	bmpInfoHeader[31]		= ( unsigned char )( ppm >> 16 );
	bmpInfoHeader[32]		= ( unsigned char )( ppm >> 24 );
	
	file					= fopen( filename, "wb" );
	fwrite( bmpFileHeader, 1, 14, file );
	fwrite( bmpInfoHeader, 1, 40, file );
	
	for ( int i = 0; i < totalPxs; ++i )
	{
		RGBType rgb		= pxs[i];
		double red 		= pxs[i].r * 255;
		double green	= pxs[i].g * 255;
		double blue 	= pxs[i].b * 255;
		
		unsigned char color[3]	= { ( int )floor( blue ), 
									( int )floor( green ),  
									( int )floor( red  ) };
		
		fwrite( color, 1, 3, file );
	}
	fclose( file );
}

vector<double> :: iterator closestObjectIndex( vector<double> *intersections )
{
	if ( intersections -> size( ) == 0 )
		return intersections -> end( );
		
	if ( intersections -> size( ) == 1 && intersections -> at( 0 ) >= 0 )
		return intersections -> begin( );
	
	double min	= INFINITY;
	vector<double> :: iterator iterator = intersections -> end( );
	for ( vector<double> :: iterator i = intersections -> begin( ); i != intersections -> end( ); ++i )
	{
		if ( min > *i && *i > 0 )
		{
			iterator	= i;
			min 		= *i;
		}
	}	
	//return the index of the closest object unless it is smaller than 0 in that case return 
	//intersections -> end( ), which is a null value.
	return iterator;
}

Color *getColorAt( Vect *intersectionOrigin, Vect *intersectionDirection, vector<Object*> *objects, vector<Source*> *lights, int index, double accuracy, double ambientLight )
{
	Color *objColor	= objects -> at( index ) -> getColor( );
	Vect *objNormal	= objects -> at( index ) -> getNormalAt( intersectionOrigin );
	
	if ( objColor -> getSpecial( ) == 2 )
	{
		int square	= ( int )floor( intersectionOrigin -> getX( ) ) + ( int )floor( intersectionOrigin -> getZ( ) );
		if ( ( square % 2 ) == 0 )
		{
			objColor -> setColor( 0, 0, 0 );
		} else
		{
			objColor -> setColor( 1, 1, 1 );
		}
	}
	
	Color *color 	= objColor -> getColorScalar( ambientLight );
	
	if ( objColor -> getSpecial( ) > 0 && objColor -> getSpecial( ) <= 1 )
	{
		double dot1 	= objNormal -> getDotProd( intersectionDirection -> negate( ) );
		Vect *scalar1	= objNormal -> mult( dot1 );
		Vect *addDir1	= scalar1 	-> add( intersectionDirection );
		
		Vect *scalar2	= addDir1	-> mult( 2 );
		Vect *addDir2	= intersectionDirection -> negate( ) -> add( scalar2 );
		
		Vect *reflectionDir	= addDir2 	-> getNormalized( );		
		Ray * reflectionRay	= new Ray( intersectionOrigin, reflectionDir );
		
		vector<double> *reflectionIntersections = new vector<double>( );
		
		for ( vector<Object*> :: iterator i = objects -> begin( ); i < objects -> end( ); ++i )
		{
			reflectionIntersections -> push_back( ( *i ) -> findIntersection( reflectionRay ) );
		}
		vector<double> :: iterator it = closestObjectIndex( reflectionIntersections );
		if ( it != reflectionIntersections -> end( ) )
		{
			if ( *it > accuracy )
			{
				Vect *reflectionIntersectionOrigin		= intersectionOrigin -> add( ( reflectionDir -> mult( *it ) ) );
				Vect *reflectionIntersectionDirection	= reflectionDir;
				int reflectionIndex 					= it - reflectionIntersections 	-> begin( );
				Color *reflectionColor					= getColorAt( reflectionIntersectionOrigin, reflectionIntersectionDirection, objects, lights, reflectionIndex, accuracy, ambientLight );
				color = color -> add( reflectionColor -> getColorScalar( objColor -> getSpecial( ) ) );
			}
		}
		
	}
	
	for ( vector<Source*> :: iterator i = lights -> begin( ); i != lights -> end( ); ++i )
	{
		Vect *lightDir	= ( *i ) -> getOrigin( ) -> add( intersectionOrigin -> negate( ) ) -> getNormalized( );
		double cosAngle	= objNormal -> getDotProd( lightDir );
		
		if ( cosAngle > 0 )
		{
			//test for shadows. 
			bool shadowed			= false;
			Vect *distToLight		= ( *i ) -> getOrigin( ) -> add( intersectionOrigin -> negate( ) ) -> getNormalized( );
			double distToLightMag	= distToLight -> getMagnitude( );
			
			Ray *shadowRay 			= new Ray( intersectionOrigin, ( *i ) -> getOrigin( ) -> add( intersectionOrigin -> negate( ) ) -> getNormalized( ) );
			vector<double> *intersections	= new vector<double>( );
			for ( vector<Object*> :: iterator j = objects -> begin( ); j != objects -> end( ) && !shadowed; ++j )
			{
				intersections -> push_back( ( *j ) -> findIntersection( shadowRay ) );
			}
			for ( vector<double> :: iterator k = intersections -> begin( ); k != intersections -> end( ); ++k )
			{
				if ( *k > accuracy )
				{
					shadowed = true;
				}
			}
			
			if ( !shadowed )
			{
				color	= color -> add( objColor -> mult( ( *i ) -> getColor( ) ) -> getColorScalar( cosAngle ) );
				if ( objColor -> getSpecial( ) > 0 && objColor -> getSpecial( ) <= 1 )
				{
					double dot1 	= objNormal -> getDotProd( intersectionDirection -> negate( ) );
					Vect *scalar1	= objNormal -> mult( dot1 );
					Vect *add1		= scalar1	-> add( intersectionDirection );
					
					Vect *scalar2	= add1 		-> mult( 2 );
					Vect *add2		= intersectionDirection -> negate( ) -> add( scalar2 );
					Vect *refDir	= add2 		-> getNormalized( );
					
					double specular	= refDir 	-> getDotProd( lightDir );
					if ( specular > 0 )
					{
						specular 			= pow( specular, 10 );
						color				= color -> add( ( *i ) -> getColor( ) -> getColorScalar( specular * objColor -> getSpecial( ) ) );
					}
				}
			}
		}		
	}
	return color -> getClipped( );
}

int current	= 0;
int main ( int argc, char * args[] )
{
    clock_t now = clock( );
	cout << "rendering..." << endl;
	int dpi			= 72;
	int width 		= 640, height = 480;
	double aspectRatio	= ( double )width / ( double )height;
	double ambientLight	= 0.2;
	double accuracy		= 0.000001;
	
	RGBType *pxs	= new RGBType[width * height];
	
	int aaDepth			= 2;
	double aaThreshold	= 0.1;
	Vect camPos( 1, 3.25, 6 );
	//Vect camPos( 0.5, 0.5, -0.5 );
	Vect x( 1, 0, 0 );
	Vect y( 0, 1, 0 );
	Vect z( 0, 0, 1 );
	
	Vect lookAt( 1, 2, 1 );
	Vect *diffBetween	= lookAt - camPos;
	Vect *camDir		= diffBetween -> getNormalized( );
	Vect *camRight		= y.getCrossProd( camDir ) 	-> getNormalized( );
	Vect *camDown		= camRight -> getCrossProd( camDir );
	Camera *cam			= new Camera( &camPos, camDir, camRight, camDown );
	
	Color *white 		= new Color( 1.0, 1.0, 1.0 );
	Color *green		= new Color( 0.5, 1.0, 0.5, 1 );
	Color *red			= new Color( 1.0, 0.25, 0.35, 1 );
	Color *blue			= new Color( 0.1, 0.25, 1.0, 1 );
	Color *tileFloor	= new Color( 1, 1, 1, 2 );
	
	Vect *lightPos		= new Vect( 10.0, 10.0, 10.0 );
	Light *light		= new Light( lightPos, white );
		
	Plane *plane		= new Plane( &y, tileFloor, -1 );
	vector<Source*> *lights		= new vector<Source*>( );
	vector<Object*> *objects	= new vector<Object*>( );
	
	objects				-> push_back( new Sphere( new Vect( 1, 1, 1 ), red, 1.0 ) );
	objects				-> push_back( new Sphere( new Vect( 1, 2.5, 0 ), blue, 0.5 ) );
	objects				-> push_back( new Sphere( new Vect( 1, 3.5, 2 ), green, 0.5 ) );
	objects				-> push_back( plane );
	
	lights				-> push_back( light );
	lights				-> push_back( new Light( new Vect( -1.0, 10.0, -10.0 ), new Color( 0, 0, 0 ) )	);

	int aaIndex;
	double x_ant, y_ant;
	double tempRed, tempGreen, tempBlue;
	
	for ( int x = 0; x < width; ++x )
	{
		for ( int y = 0; y < height; ++y )
		{
			current	= x + y * width;
			
			double tempRed[aaDepth * aaDepth];
			double tempGreen[aaDepth * aaDepth];
			double tempBlue[aaDepth * aaDepth];
			
			for ( int aaX = 0; aaX < aaDepth; ++aaX )
			{
				for ( int aaY = 0; aaY < aaDepth; ++aaY )
				{
					if ( aaDepth == 1 )
					{
						if ( width > height )
						{
							x_ant	= ( ( x + 0.5 ) / width ) * aspectRatio - ( ( ( width - height ) / ( double )height ) / 2 );
							y_ant 	= ( ( height - y ) + 0.5 ) / height;
						} else if ( height > width )
						{
							x_ant	= ( x + 0.5 ) / width;
							y_ant	= ( ( (  height - y ) + 0.5 ) / height ) / aspectRatio - ( ( ( height - width ) / ( double ) width ) / 2 );
						} else 
						{
							x_ant = ( x + 0.5 ) / width;
							y_ant = ( ( height - y ) + 0.5 ) / height;
						}
					} else
					{
						aaIndex	= aaY * aaDepth + aaX;
						srand( time( NULL ) );
						
						if ( width > height )
						{
							x_ant	= ( ( x + ( double )aaX / ( ( double ) aaDepth - 1 ) ) / width ) * aspectRatio - ( ( ( width - height ) / ( double )height ) / 2 );
							y_ant 	= ( ( height - y ) + ( double )aaX / ( ( double ) aaDepth - 1 ) ) / height;
						} else if ( height > width )
						{
							x_ant	= ( x + ( double )aaX / ( ( double ) aaDepth - 1 ) ) / width;
							y_ant	= ( ( (  height - y ) + ( double )aaX / ( ( double ) aaDepth - 1 ) ) / height ) / aspectRatio - ( ( ( height - width ) / ( double ) width ) / 2 );
						} else 
						{
							x_ant = ( x + ( double )aaX / ( ( double ) aaDepth - 1 ) ) / width;
							y_ant = ( ( height - y ) + ( double )aaX / ( ( double ) aaDepth - 1 ) ) / height;
						}
					}
					Vect *camRayOrigin		= cam -> getOrigin( );
					Vect *camRayDirection	= camDir -> add( camRight -> mult( x_ant - 0.5 ) -> add( camDown -> mult( y_ant - 0.5 ) ) ) -> getNormalized( );
					
					Ray *camRay 			= new Ray( camRayOrigin, camRayDirection );
					vector<double> *intersections = new vector<double>( );
					
					for ( vector<Object*> :: iterator i = objects -> begin( ); i < objects -> end( ); ++i )
					{
						Object* obj	= *i;
						intersections	-> push_back( obj -> findIntersection( camRay ) );
					}
					
					vector<double> :: iterator it = closestObjectIndex( intersections );
					if ( it == intersections -> end( ) )
					{
						tempRed[aaIndex]		= 0;
						tempGreen[aaIndex]		= 0;
						tempBlue[aaIndex]		= 0;
					} else
					{
						if ( *it > accuracy )
						{
							int index = it - intersections 	-> begin( );
							
							Vect *intersectionOrigin	= camRayOrigin -> add( ( camRayDirection -> mult( *it ) ) );
							Vect *intersectionDirection	= camRayDirection;
							
							
							Color *color 	= getColorAt( intersectionOrigin, intersectionDirection, objects, lights, index, accuracy, ambientLight );
							tempRed[aaIndex]  = color 		-> getRed( );
							tempGreen[aaIndex] 	= color 		-> getGreen( );
							tempBlue[aaIndex] 	= color 		-> getBlue( );
						}
					}
				}
			}
			double totalRed		= 0;
			double totalGreen	= 0;
			double totalBlue	= 0;
			
			for ( int i	= 0; i < aaDepth * aaDepth; ++i )
			{
				totalRed 	+= tempRed[i];
				totalGreen	+= tempGreen[i];
				totalBlue	+= tempBlue[i];
			}
			
			double avgRed 	= totalRed / ( aaDepth * aaDepth );
			double avgGreen 	= totalGreen / ( aaDepth * aaDepth );
			double avgBlue 	= totalBlue / ( aaDepth * aaDepth );
			
			pxs[current].r 	= avgRed;
			pxs[current].g 	= avgGreen;
			pxs[current].b 	= avgBlue;
		}
	}
	
	saveBitmap( "raytracing_example_1.bmp", width, height, dpi, pxs );	
	delete pxs, tempRed, tempGreen, tempBlue;
	cout << "done: " << ( double )( ( double )( clock( ) - now ) / ( double )1000 ) << "s.";
	return 0;
}