Phongo Clap RT  1.0
Simple Raytracing Renderer
main.cpp
Go to the documentation of this file.
1 
4 #include <iostream>
5 #include <stdlib.h>
6 #include <fstream>
7 #include <string>
8 #include <time.h>
9 #include <assert.h>
10 #include <ngl/Vec3.h>
11 #include <vector>
12 #include <boost/tokenizer.hpp>
13 
14 #include "Singleton.h"
15 #include "Parser.h"
16 #include "Scene.h"
17 #include "Film.h"
18 #include "Shape.h"
19 #include "Sphere.h"
20 #include "Plane.h"
21 #include "Renderer.h"
22 
23 
24 void printOutTime(float _seconds)
25 {
26  if (_seconds<60.0f)
27  {
28  std::cout << "Render time: " << (int)_seconds << " seconds\n";
29  }
30  else if (_seconds > 60.0f && _seconds < 3600.0f)
31  {
32  int minutes = (int)_seconds / (int)60;
33  float seconds = (int)_seconds % (int)60;
34 
35  std::cout << "Render time: " << minutes << "min " << seconds << "seconds\n";
36  }
37 }
38 
39 int main(int argc, char *argv[])
40 {
41  if (argc != 2)
42  {
43  std::cout << "ERROR, you must indicate the scene file you want to read from.\n"
44  "Usage: ./aidan [path_to_textfile]";
45  }
46  assert(argc == 2);
47 
48  // USER DEFINES THESE VARIABLES
49  int width = 0;
50  int height = 0;
51  float camPosX = 0;
52  float camPosY = 0;
53  float camPosZ = 0;
54  float lookAtX = 0;
55  float lookAtY = 0;
56  float lookAtZ = 0;
57  int max_depth = 0;
58  int anti_aliasing = 0;
59 
60  std::vector<Light*> scene_lights;
61  std::vector<geo::Shape*> scene_objects;
62  std::string text_file = argv[1];
63  std::string image_name;
64  Parser scene_parser(image_name,
65  text_file,
66  width,
67  height,
68  camPosX,
69  camPosY,
70  camPosZ,
71  lookAtX,
72  lookAtY,
73  lookAtZ,
74  max_depth,
75  anti_aliasing,
76  scene_lights,
77  scene_objects);
78 
79  if(anti_aliasing < 1)
80  {
81  std::cout << "ERROR: Anti-aliasing should be 1 (no-antialising) or more." << std::endl;
82  assert(anti_aliasing > 0);
83  }
84 
85  // instanciate a scene object
86  Scene* myScene_instance = Singleton<Scene>::Instance();
87 
88  // push all the object that the user specified into the scene
89  for(unsigned int i = 0; i < scene_objects.size(); i++)
90  {
91  myScene_instance->addObject(scene_objects.at(i));
92  }
93 
94  // push all the lights specified by the user into the scene
95  for(unsigned int i = 0; i < scene_lights.size(); i++)
96  {
97  myScene_instance->addLight(scene_lights.at(i));
98  }
99 
100  // instanciate film and set its dimensions
101  Film* myFilm_instance = Singleton<Film>::Instance();
102  myFilm_instance->setDimensions(width, height);
103 
104  // calculate camera right and down vectors for generating the camera rays properly in the renderer
105  ngl::Vec3 Y (0,1,0);
106  ngl::Vec3 campos(camPosX,camPosY,camPosZ);
107  ngl::Vec3 lookat(lookAtX,lookAtY,lookAtZ);
108  ngl::Vec3 diff_btw = campos - lookat; diff_btw.normalize();
109  ngl::Vec3 camdir = -diff_btw;
110  ngl::Vec3 camright = Y.cross(camdir);
111  ngl::Vec3 camdown = camright.cross(camdir);
112 
113  // initialise camera
114  Camera* myCamera_instance = Singleton<Camera>::Instance();
115  myCamera_instance->setParameters(campos,camdir,camright,camdown);
116 
117  // instanciate a renderer and bind film and camera to it
118  Renderer* renderer_instance = Singleton<Renderer>::Instance();
119  renderer_instance->bind(myScene_instance, myFilm_instance, myCamera_instance, max_depth, anti_aliasing, image_name);
120 
121  // clock in so that we can now what the render time is after the render process takes place
122  clock_t t;
123  t = clock();
124  std::cout << "Rendering...\n";
125 
126  // start the rendering process
127  renderer_instance->render();
128 
129  // Calculate render time
130  t = clock() - t;
131  float seconds = (float)t/CLOCKS_PER_SEC;
132  printOutTime(seconds);
133 
134  // destroy the renderer, camera, film and scene
139 
140  // Sucess message for the user and display the image
141  std::cout << image_name << ".ppm has been written successfully.\n";
142  std::string command = "display " + image_name + ".ppm";
143  system(command.c_str());
144 
145  return 0;
146 }
void render()
Will trigger the class and start doing all the calculations.
Definition: Renderer.cpp:322
void printOutTime(float _seconds)
Definition: main.cpp:24
Core of my program, central unit that manages all the other classes almost.
Definition: Renderer.h:23
void setParameters(ngl::Vec3 _pos, ngl::Vec3 _dir, ngl::Vec3 _right, ngl::Vec3 _down)
Initialises the camera settings.
Definition: Camera.cpp:11
Implements the interface for creating a plane shape and the methods for finding its intersections...
All the operations regarding to file output and file input belong to this class. It abstracts the ide...
A class for implicit sphere definitions.
This hold all the objects and lights that the parser interpred and later pushed into the scene...
Parses a text file that user passes as an argument when executing the program.
void setDimensions(int _w, int _h)
Sets the dimensions of the Film.
Definition: Film.cpp:11
Makes sure that no object is instanciated twice.
void bind(Scene *_scence, Film *_film, Camera *_camera, int _max_depth, int _anti_aliasing, std::string _image_name)
Grabs all the information and places it into the private interface.
Definition: Renderer.cpp:59
void addObject(geo::Shape *_object)
Adds an shape obejct to the scene.
Definition: Scene.cpp:12
Holds all the operations regarding to input/output of colour information.
Definition: Film.h:29
Hold all the objects and lights of the scene.
Definition: Scene.h:20
static void destroyInstance()
Destroys the instance thus freeing memory from the heap.
Definition: Singleton.h:27
void addLight(Light *_light)
Adds a light to the scene structure.
Definition: Scene.cpp:17
This class is the heart of my raytracer. It is the core, where all the camera rays are calculated and...
static T * Instance()
If the instance is null it creates one, otherwise just returns the current one.
Definition: Singleton.h:16
Holds camera functions which will be accessed by the Render class.
Definition: Camera.h:17
int main(int argc, char *argv[])
Definition: main.cpp:39
Reads from text file and iterates using boost tokenizer to "pickup" and store values based on syntax...
Definition: Parser.h:28