-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.cpp
96 lines (84 loc) · 4.42 KB
/
main2.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <Util/cmdLineParser.h>
#include <Util/timer.h>
#include <Ray/scene.h>
#include <Ray/box.h>
#include <Ray/cone.h>
#include <Ray/cylinder.h>
#include <Ray/sphere.h>
#include <Ray/torus.h>
#include <Ray/triangle.h>
#include <Ray/fileInstance.h>
#include <Ray/directionalLight.h>
#include <Ray/pointLight.h>
#include <Ray/spotLight.h>
using namespace std;
using namespace Ray;
using namespace Util;
using namespace Image;
#undef GLUT_NO_LIB_PRAGMA
CmdLineParameter< string > InputRayFile( "in" );
CmdLineParameter< string > OutputImageFile( "out" );
CmdLineParameter< int > ImageWidth( "width" , 640 );
CmdLineParameter< int > ImageHeight( "height" , 480 );
CmdLineParameter< int > RecursionLimit( "rLimit" , 5 );
CmdLineParameter< float > CutOffThreshold( "cutOff" , 0.0001f );
CmdLineReadable* params[] =
{
&InputRayFile , &OutputImageFile , &ImageWidth , &ImageHeight , &RecursionLimit , &CutOffThreshold ,
NULL
};
void ShowUsage( const string &ex )
{
cout << "Usage " << ex << ":" << endl;
cout << "\t --" << InputRayFile.name << " <input ray File>" << endl;
cout << "\t[--" << OutputImageFile.name << " <output image file>]" << endl;
cout << "\t[--" << ImageWidth.name << " <image width>=" << ImageWidth.value << "]" << endl;
cout << "\t[--" << ImageHeight.name << " <image height>=" << ImageHeight.value << "]" << endl;
cout << "\t[--" << RecursionLimit.name << " <recursion limit>=" << RecursionLimit.value << "]" << endl;
cout << "\t[--" << CutOffThreshold.name << " <cut-off threshold>=" << CutOffThreshold.value << "]" << endl;
}
int main( int argc , char *argv[] )
{
CmdLineParse( argc-1 , argv+1 , params );
if( !InputRayFile.set ){ ShowUsage( argv[0] ) ; return EXIT_FAILURE; }
Scene scene;
try
{
ShapeList::ShapeFactories[ Box ::Directive() ] = new DerivedFactory< Shape , Box >();
ShapeList::ShapeFactories[ Cone ::Directive() ] = new DerivedFactory< Shape , Cone >();
ShapeList::ShapeFactories[ Cylinder ::Directive() ] = new DerivedFactory< Shape , Cylinder >();
ShapeList::ShapeFactories[ Sphere ::Directive() ] = new DerivedFactory< Shape , Sphere >();
ShapeList::ShapeFactories[ Torus ::Directive() ] = new DerivedFactory< Shape , Torus >();
ShapeList::ShapeFactories[ Triangle ::Directive() ] = new DerivedFactory< Shape , Triangle >();
ShapeList::ShapeFactories[ FileInstance ::Directive() ] = new DerivedFactory< Shape , FileInstance >();
ShapeList::ShapeFactories[ ShapeList ::Directive() ] = new DerivedFactory< Shape , ShapeList >();
ShapeList::ShapeFactories[ TriangleList ::Directive() ] = new DerivedFactory< Shape , TriangleList >();
ShapeList::ShapeFactories[ StaticAffineShape::Directive() ] = new DerivedFactory< Shape , StaticAffineShape >();
ShapeList::ShapeFactories[ Union ::Directive() ] = new DerivedFactory< Shape , Union >();
ShapeList::ShapeFactories[ Intersection ::Directive() ] = new DerivedFactory< Shape , Intersection >();
ShapeList::ShapeFactories[ Difference ::Directive() ] = new DerivedFactory< Shape , Difference >();
GlobalSceneData::LightFactories[ DirectionalLight::Directive() ] = new DerivedFactory< Light , DirectionalLight >();
GlobalSceneData::LightFactories[ PointLight ::Directive() ] = new DerivedFactory< Light , PointLight >();
GlobalSceneData::LightFactories[ SpotLight ::Directive() ] = new DerivedFactory< Light , SpotLight >();
ifstream istream;
istream.open( InputRayFile.value );
if( !istream ) THROW( "Failed to open file for reading: %s\n" , InputRayFile.value.c_str() );
istream >> scene;
Timer timer;
Image32 img = scene.rayTrace( ImageWidth.value , ImageHeight.value , RecursionLimit.value , CutOffThreshold.value );
std::cout << "Ray-traced " << ImageWidth.value << "x" << ImageHeight.value << " image in " << timer.elapsed() << " seconds" << std::endl;
if( OutputImageFile.set ) img.write( OutputImageFile.value );
}
catch( const exception &e )
{
cerr << e.what() << endl;
return EXIT_FAILURE;
}
for( auto iter=ShapeList::ShapeFactories.begin() ; iter!=ShapeList::ShapeFactories.end() ; iter++ ) delete iter->second;
for( auto iter=GlobalSceneData::LightFactories.begin() ; iter!=GlobalSceneData::LightFactories.end() ; iter++ ) delete iter->second;
return EXIT_SUCCESS;
}