9 #include <boost/tokenizer.hpp>
16 #include <ngl/Colour.h>
49 std::string &_text_file,
60 std::vector<Light*> &_scene_lights,
61 std::vector<geo::Shape*> &_scene_objects)
63 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
64 boost::char_separator<char> quote{
"\""};
67 std::ifstream sceneFile(_text_file);
69 sceneFile.seekg(0, std::ios::end);
70 text.reserve(sceneFile.tellg());
71 sceneFile.seekg(0, std::ios::beg);
73 text.assign((std::istreambuf_iterator<char>(sceneFile)), std::istreambuf_iterator<char>());
75 tokenizer tok{text,quote};
76 tokenizer::iterator it = tok.begin();
79 std::cout <<
"Parsing filename...\n"; ++it;
83 std::cout <<
"OK! Filename: " << _image_name << std::endl; ++it; ++it;
84 std::cout <<
"Parsing file dimensions...\n";
86 tmp = *it; _width = atoi(tmp.c_str()); ++it; ++it;
87 tmp = *it; _height = atoi(tmp.c_str());
89 std::cout <<
"OK! " << _width <<
"x" << _height <<
" image specified.\n";
91 std::cout <<
"Parsing camera...\n"; ++it; ++it;
92 tmp = *it; _camPosX = atof(tmp.c_str()); ++it; ++it;
93 tmp = *it; _camPosY = atof(tmp.c_str()); ++it; ++it;
94 tmp = *it; _camPosZ = atof(tmp.c_str()); ++it; ++it;
95 tmp = *it; _lookAtX = atof(tmp.c_str()); ++it; ++it;
96 tmp = *it; _lookAtY = atof(tmp.c_str()); ++it; ++it;
97 tmp = *it; _lookAtZ = atof(tmp.c_str());
99 std::cout <<
"OK! Camera parsed with pos["<<_camPosX <<
","<<_camPosY<<
","<<_camPosZ<<
"] and lookup["<<_lookAtX<<
","
100 <<_lookAtY<<
","<<_lookAtZ<<
"]\n"; ++it; ++it;
102 tmp = *it; _max_depth = atoi(tmp.c_str()); ++it; ++it;
103 tmp = *it; _anti_aliasing = atoi(tmp.c_str()); ++it; ++it;
105 std::cout <<
"Parsing lights...\n";
108 std::cout <<
"Parsing objects...\n";
120 void parseObjects(std::string& _text, std::vector<geo::Shape*> &_scene_objects)
122 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
123 boost::char_separator<char> dollar{
"$"};
124 tokenizer tok{_text,dollar};
125 tokenizer::iterator it = tok.begin();
130 while(it != tok.end())
133 if (tmp.at(0) ==
'}')
break;
134 if (tmp.at(0) ==
'P')
139 else if (tmp.at(0) ==
'S')
152 void parseLights(std::string& _text, std::vector<Light*> &_scene_lights)
154 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
155 boost::char_separator<char> plus{
"+"};
156 tokenizer tok{_text,plus};
157 tokenizer::iterator it = tok.begin();
162 while(it != tok.end())
165 if (tmp.at(0) ==
'}')
break;
177 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
178 boost::char_separator<char> space{
" "};
179 tokenizer tok{_text_light,space};
180 tokenizer::iterator it = tok.begin();
182 std::string light_name;
185 ngl::Colour diffuse_col, specular_col;
186 float diff_int, spec_int, falloff;
188 light_name = *it; it++;it++;
190 tmp = *it; position.m_x = atof(tmp.c_str()); it++;
191 tmp = *it; position.m_y = atof(tmp.c_str()); it++;
192 tmp = *it; position.m_z = atof(tmp.c_str()); it++;it++;it++;
193 tmp = *it; diffuse_col.m_r = atof(tmp.c_str()); it++;
194 tmp = *it; diffuse_col.m_g = atof(tmp.c_str()); it++;
195 tmp = *it; diffuse_col.m_b = atof(tmp.c_str()); it++;it++;it++;
196 tmp = *it; specular_col.m_r = atof(tmp.c_str()); it++;
197 tmp = *it; specular_col.m_g = atof(tmp.c_str()); it++;
198 tmp = *it; specular_col.m_b = atof(tmp.c_str()); it++;it++;
199 tmp = *it; diff_int = atof(tmp.c_str()); it++;
200 tmp = *it; spec_int = atof(tmp.c_str()); it++;
201 tmp = *it; falloff = atof(tmp.c_str());
203 PointLight* light =
new PointLight(position, diffuse_col, specular_col, diff_int, spec_int, falloff);
204 _scene_lights.push_back(light);
205 std::cout <<
"OK! Light with name " << light_name <<
" has been parsed.\n";
212 void parsePlane(std::string& _text, std::vector<geo::Shape*> &_scene_objects)
214 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
215 boost::char_separator<char> space{
" "};
216 tokenizer tok{_text,space};
217 tokenizer::iterator it = tok.begin();
224 std::string plane_name = *it; it++;
226 tmp = *it; distance = atof(tmp.c_str()); it++;it++;
227 tmp = *it; nX = atof(tmp.c_str()); it++;
228 tmp = *it; nY = atof(tmp.c_str()); it++;
229 tmp = *it; nZ = atof(tmp.c_str()); it++;it++;
231 if (tmp ==
"@checker")
233 float col1_r, col1_g, col1_b, col2_r, col2_g, col2_b; it++;it++;
235 tmp = *it; col1_r = atof(tmp.c_str());it++;
236 tmp = *it; col1_g = atof(tmp.c_str());it++;
237 tmp = *it; col1_b = atof(tmp.c_str());it++; it++; it++;
238 tmp = *it; col2_r = atof(tmp.c_str());it++;
239 tmp = *it; col2_g = atof(tmp.c_str());it++;
240 tmp = *it; col2_b = atof(tmp.c_str());it++; it++;
243 if (tmp ==
"@specularHardness")
246 float specularHardness;
247 tmp = *it; specularHardness = atof(tmp.c_str());
249 geo::Shape* plane =
new geo::Plane(distance,ngl::Vec3(nX,nY,nZ),ngl::Colour(col1_r,col1_g,col1_b,1), ngl::Colour(col2_r,col2_g,col2_b,1));
250 plane->getMaterial()->m_spec_hardness = specularHardness;
251 _scene_objects.push_back(plane);
253 std::cout <<
"OK! Plane " << plane_name <<
" has been parsed successfully.\n";
257 geo::Shape* plane =
new geo::Plane(distance,ngl::Vec3(nX,nY,nZ),ngl::Colour(col1_r,col1_g,col1_b,1), ngl::Colour(col2_r,col2_g,col2_b,1));
258 _scene_objects.push_back(plane);
260 std::cout <<
"OK! Plane " << plane_name <<
" has been parsed successfully.\n";
265 float col_r, col_g, col_b;
267 tmp = *it; col_r = atof(tmp.c_str()); it++;
268 tmp = *it; col_g = atof(tmp.c_str()); it++;
269 tmp = *it; col_b = atof(tmp.c_str()); it++; it++;
272 if(tmp ==
"@specularHardness")
275 float specularHardness;
276 tmp = *it; specularHardness = atof(tmp.c_str());
278 geo::Shape* plane =
new geo::Plane(distance,ngl::Vec3(nX,nY,nZ),ngl::Colour(col_r,col_g,col_b,1));
279 plane->getMaterial()->m_spec_hardness = specularHardness;
280 _scene_objects.push_back(plane);
282 std::cout <<
"OK! Plane " << plane_name <<
" has been parsed successfully.\n";
286 geo::Shape* plane =
new geo::Plane(distance,ngl::Vec3(nX,nY,nZ),ngl::Colour(col_r,col_g,col_b,1));
287 _scene_objects.push_back(plane);
288 std::cout <<
"OK! Plane " << plane_name <<
" has been parsed successfully.\n";
297 void parseSphere(std::string& _text, std::vector<geo::Shape*> &_scene_objects)
299 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
300 boost::char_separator<char> space{
" "};
301 tokenizer tok{_text,space};
302 tokenizer::iterator it = tok.begin();
307 float posX, posY, posZ;
308 float colX, colY, colZ;
311 std::string sphere_name = *it;
314 tmp = *it; posX = atof(tmp.c_str()); it++;
315 tmp = *it; posY = atof(tmp.c_str()); it++;
316 tmp = *it; posZ = atof(tmp.c_str()); it++;it++;
317 tmp = *it; radius = atof(tmp.c_str()); it++;it++;
318 tmp = *it; colX = atof(tmp.c_str()); it++;
319 tmp = *it; colY = atof(tmp.c_str()); it++;
320 tmp = *it; colZ = atof(tmp.c_str()); it++;it++;
323 if(tmp ==
"@specularHardness") {
327 float specularHardness = atof(tmp.c_str());
329 geo::Shape* sphere =
new geo::Sphere(ngl::Vec3(posX,posY,posZ),radius, ngl::Colour(colX,colY,colZ,1));
330 sphere->getMaterial()->m_spec_hardness = specularHardness;
331 _scene_objects.push_back(sphere);
333 std::cout <<
"OK! Sphere " << sphere_name <<
" has been parsed successfully.\n";
335 else if(tmp ==
"@reflective") {
337 tmp = *it;
float reflection_ratio = atof(tmp.c_str()) / 100.0f;
339 geo::Shape* sphere =
new geo::Sphere(ngl::Vec3(posX,posY,posZ),radius, ngl::Colour(colX,colY,colZ,1));
340 sphere->hasReflection(reflection_ratio, 1.0f-reflection_ratio);
341 _scene_objects.push_back(sphere);
343 std::cout <<
"OK! Sphere " << sphere_name <<
" has been parsed successfully.\n";
345 else if(tmp ==
"@refractive") {
347 tmp = *it;
float ior = atof(tmp.c_str()); it++;
348 tmp = *it;
float refraction_ratio = atof(tmp.c_str()) / 100.0f;
350 geo::Shape* sphere =
new geo::Sphere(ngl::Vec3(posX,posY,posZ),radius, ngl::Colour(colX,colY,colZ,1));
351 sphere->hasRefraction(ior, refraction_ratio, 1.0f-refraction_ratio);
352 _scene_objects.push_back(sphere);
354 std::cout <<
"OK! Sphere " << sphere_name <<
" has been parsed successfully.\n";
357 geo::Shape* sphere =
new geo::Sphere(ngl::Vec3(posX,posY,posZ),radius, ngl::Colour(colX,colY,colZ,1));
358 _scene_objects.push_back(sphere);
void parseSingleLight(std::string &_text_light, std::vector< Light * > &_scene_lights)
Read the text line that defines a light and push it into the vector container.
Class that hold an implicit definition of a sphere through center and radius parameters. It also implements a method for finding intersections.
Implements the interface for creating a plane shape and the methods for finding its intersections...
A class for implicit sphere definitions.
~Parser()
Default destructor for the parser.
A class for the lights in the scene. There is no sepparate .cpp file due to its simplicity.
void parsePlane(std::string &_text, std::vector< geo::Shape * > &_scene_objects)
Read the text line that defines a plane and push it into the vector container.
Inherits from shape. Implements functionability for plane shape and its materials. and the ray-plane intersections algorithms.
void parseLights(std::string &_text, std::vector< Light * > &_scene_lights)
Read objects snippet of text and push elements to the object vector on the go.
Parser(std::string &_image_name, std::string &_text_file, int &_width, int &_height, float &_camPosX, float &_camPosY, float &_camPosZ, float &_lookAtX, float &_lookAtY, float &_lookAtZ, int &_max_depth, int &_anti_aliasing, std::vector< Light * > &_scene_lights, std::vector< geo::Shape * > &_scene_objects)
Default constructor for the parser.
void parseSphere(std::string &_text, std::vector< geo::Shape * > &_scene_objects)
Read the text line that defines a sphere and push it into the vector container.
void parseObjects(std::string &_text, std::vector< geo::Shape * > &_scene_objects)
Read objects snippet of text and push elements to the object vector on the go.
Semi abstract class with virtual methods that holds all the calls for getting intersections, getting normals, also for specifying the properties of the material that will be hold by the Material member of this class.
Reads from text file and iterates using boost tokenizer to "pickup" and store values based on syntax...