-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain2.cpp
More file actions
139 lines (123 loc) · 6.28 KB
/
main2.cpp
File metadata and controls
139 lines (123 loc) · 6.28 KB
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
#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;
}
/** A wrapper class for size_t that prints out comma-separated numbers */
struct Size_t
{
Size_t( size_t v=0 ) : value(v){}
size_t value;
size_t &operator()( void ){ return value; }
const size_t &operator()( void ) const { return value; }
protected:
static void _Write( std::ostream &stream , size_t top , size_t bottom )
{
if( !top ) stream << bottom;
else
{
if( top<1000 ) stream << top;
else _Write( stream , top/1000 , top%1000 );
stream << ",";
if ( bottom<1 ) stream << "000";
else if( bottom<10 ) stream << "00";
else if( bottom<100 ) stream << "0";
stream << bottom;
}
}
friend std::ostream &operator << ( std::ostream &stream , const Size_t &s );
};
std::ostream &operator << ( std::ostream &stream , const Size_t &s )
{
if( s.value<10000 ) return stream << s.value;
Size_t::_Write( stream , s.value/1000 , s.value%1000 );
return stream;
}
int main( int argc , char *argv[] )
{
CmdLineParse( argc-1 , argv+1 , params );
if( !InputRayFile.set ){ ShowUsage( argv[0] ) ; return EXIT_FAILURE; }
Scene::BaseDir = GetFileDirectory( InputRayFile.value );
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() );
Timer timer;
istream >> scene;
std::cout << "\tRead: " << timer.elapsed() << " seconds" << std::endl;
timer.reset();
RayTracingStats::Reset();
Image32 img = scene.rayTrace( ImageWidth.value , ImageHeight.value , RecursionLimit.value , CutOffThreshold.value );
std::cout << "\tRay-traced: " << timer.elapsed() << " seconds" << std::endl;
std::cout << "\tPixels: " << Size_t( ImageWidth.value ) << " x " << Size_t( ImageHeight.value ) << std::endl;
std::cout << "\tPrimitives: " << Size_t( scene.primitiveNum() ) << std::endl;
std::cout << "\tRays: " << Size_t( RayTracingStats::RayNum() ) << " (" << (double)RayTracingStats::RayNum()/(ImageWidth.value*ImageHeight.value) << " rays/pixel)" << std::endl;
std::cout << "\tPrimitive intersections: " << Size_t( RayTracingStats::RayPrimitiveIntersectionNum() ) << " (" << (double)RayTracingStats::RayPrimitiveIntersectionNum()/RayTracingStats::RayNum() << " intersections/ray)" << std::endl;
std::cout << "\tBounding-box intersections: " << Size_t( RayTracingStats::RayBoundingBoxIntersectionNum() ) << " (" << (double)RayTracingStats::RayBoundingBoxIntersectionNum()/RayTracingStats::RayNum() << " intersections/ray)" << 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;
}