12 #include <ngl/Types.h>
24 if ( x % (n/r +1) != 0 )
return;
27 float ratio = x/(float)n;
31 printf(
"%3d%% [", (
int)(ratio*100) );
34 for (
int x=0; x<c; x++)
37 for (
int x=c; x<w; x++)
41 printf(
"]\n\033[F\033[J");
75 if(_interxs.size() == 0) {
return -1;}
76 else if(_interxs.size() == 1)
78 if(_interxs.at(0) > 0) {
return 0;}
84 for (
unsigned int i = 0; i < _interxs.size(); i++)
86 if(max < _interxs.at(i)) {max = _interxs.at(i);}
90 for (
unsigned int i = 0; i < _interxs.size(); i++)
92 if(_interxs.at(i) > 0 && _interxs.at(i) <= max)
109 bool shadowed =
false;
113 std::vector<double> intersections;
116 float distance = dir.length();
123 intersections.push_back(
m_scene->
m_objects.at(j)->getIntersection(fire_ray));
126 for(
unsigned int k = 0; k < intersections.size(); k++)
128 if(intersections.at(k) < 0.1)
continue;
129 if(intersections.at(k) < -1)
continue;
131 if (closest_index == -1 || closest_index == _avoid)
continue;
132 if(intersections.at(k) > distance)
continue;
146 std::vector<double> intersections;
153 intersections.push_back(
m_scene->
m_objects.at(i)->getIntersection(cam_ray));
160 if(closest_index == -1) {
return ngl::Colour(0,0,0,1);}
163 ngl::Vec3 pHit = _from + intersections.at(closest_index) * _direction;
168 if(_direction.dot(nHit) > 0)
176 bool isObscured =
raycast(pHit + nHit * bias, closest_index);
187 ngl::Colour crfr(0,0,0,1);
188 ngl::Colour crfl(0,0,0,1);
194 ngl::Vec3 refl_dir = _direction - nHit * 2 * _direction.dot(nHit);
195 refl_dir.normalize();
198 crfl =
trace(pHit + bias*nHit, refl_dir, depth+1);
208 float cosi = -nHit.dot(_direction);
219 float k = 1 - eta * eta * (1 - cosi * cosi);
220 ngl::Vec3 refr_dir = _direction * eta + nHit * (eta * cosi - sqrt(k));
221 refr_dir.normalize();
222 crfr =
trace(pHit - nHit * bias, refr_dir, depth+1);
225 ngl::Colour surfaceColor =
m_scene->
m_objects.at(closest_index)->getColour(pHit);
226 float cosineFactor = std::max(-nHit.dot(cam_ray.
getDirection()),(
float)0);
229 ngl::Colour Ka(1,0,0.4,1);
233 float ambient_intensity = 0.05;
235 ngl::Colour ambient_contrib = Ka * surfaceColor * ambient_intensity;
236 ngl::Colour diffuse_contrib(0,0,0,1);
237 ngl::Colour specular_contrib(0,0,0,1);
242 float distance = v_distance.length();
244 attenuation = 1 - pow(distance/radius,2);
252 ngl::Vec3 R = 2 * (L.dot(N) * N) - L;
255 diffuse_contrib += (surfaceColor * (Kd * pow(std::max(L.dot(N),(float)0),2) *
m_scene->
m_lights.at(m)->m_diff_int))*attenuation;
256 specular_contrib += ((Ks * pow(std::max(R.dot(-_direction),(float)0),900)*400 *
m_scene->
m_lights.at(m)->m_spec_int))*attenuation;
259 specular_contrib.clamp(0,0.8);
261 ngl::Colour s01 = crfl *
m_scene->
m_objects.at(closest_index)->getMaterial()->getReflIntensity();
262 ngl::Colour s02 = crfr *
m_scene->
m_objects.at(closest_index)->getMaterial()->getTransparency();
263 ngl::Colour s03 = s01 + s02;
264 ngl::Colour diffuseColor =
m_scene->
m_objects.at(closest_index)->getColour(pHit) * cosineFactor *
m_scene->
m_objects.at(closest_index)->getMaterial()->getDiffuseIntensity();
267 ngl::Colour outRadiance = diffuseColor + s03 + specular_contrib + ambient_contrib;
268 outRadiance.clamp(0,1);
270 return isObscured ? outRadiance * 0.7f : outRadiance;
276 ngl::Colour surfaceColor =
m_scene->
m_objects.at(closest_index)->getColour(pHit);
279 ngl::Colour Ka(1,0,0.4,1);
283 float ambient_intensity = 0.05;
285 ngl::Colour ambient_contrib = Ka * surfaceColor * ambient_intensity;
286 ngl::Colour diffuse_contrib(0,0,0,1);
287 ngl::Colour specular_contrib(0,0,0,1);
292 float distance = v_distance.length();
294 attenuation = 1 - pow(distance/radius,2);
302 ngl::Vec3 R = 2 * (L.dot(N) * N) - L;
305 float spec_hardness =
m_scene->
m_objects.at(closest_index)->getMaterial()->m_spec_hardness;
307 diffuse_contrib += (surfaceColor * (Kd * pow(std::max(L.dot(N),(float)0),2) *
m_scene->
m_lights.at(m)->m_diff_int))*attenuation;
308 specular_contrib += ((Ks * pow(std::max(R.dot(-_direction),(float)0),spec_hardness) *
m_scene->
m_lights.at(m)->m_spec_int))*attenuation;
311 diffuse_contrib.clamp(0,1);
312 specular_contrib.clamp(0,1);
314 ngl::Colour outRadiance = diffuse_contrib + specular_contrib + ambient_contrib;
316 outRadiance.clamp(0,1);
318 return isObscured ? outRadiance * 0.7f : outRadiance;
324 std::vector<ngl::Colour> colourStack;
334 loadBar(current_pixel, total_number_of_pixels, r, w);
336 ngl::Colour finalColour;
344 float x_amount = ( (float)x + ((
float)aax / (float)m_anti_aliasing) + 0.5f * ((float)aax / (
float)
m_anti_aliasing) ) / (float)
m_film->
m_width;
345 float y_amount = ( (
float)y + ((float)aay / (
float)
m_anti_aliasing) + 0.5f * ((
float)aay / (float)m_anti_aliasing) ) / (
float)
m_film->
m_width;
348 cam_ray_dir.normalize();
353 colourStack.push_back(col);
364 cRed += colourStack.at(i).m_r;
365 cGreen += colourStack.at(i).m_g;
366 cBlue += colourStack.at(i).m_b;
369 cRed /= (float)m_anti_aliasing;
370 cGreen /= (float)m_anti_aliasing;
371 cBlue /= (float)m_anti_aliasing;
373 finalColour = ngl::Colour(cRed, cGreen, cBlue, 1);
385 cam_ray_dir.normalize();
void render()
Will trigger the class and start doing all the calculations.
void writeFile(const char *_image_name)
Iterates over the pixel vector and writes into a file using basic output stream methods.
Camera * m_camera
Associated camera.
Film * m_film
Associated film.
int m_anti_aliasing
Antialiasing amount.
ngl::Vec3 m_down
Camera down vector.
ngl::Vec3 m_pos
Camera position vector.
This will be used to return the colour when queried from the Shape derived classes.
int m_height
Height of the image.
~Renderer()
Destructor, frees any possible memory from the heap.
Renderer()
Constructor for the renderer class.
std::vector< Light * > m_lights
Holds all the Light instances of the scene.
static void loadBar(int x, int n, int r, int w)
Implements a loading bar. This algorithm is taken from another person.
int m_width
Width of the image.
int m_max_depth
Maximum number of ray stack frames.
ngl::Vec3 m_dir
Camera aim vector.
ngl::Vec3 m_bg_colour
default background colour.
Scene * m_scene
Associated scene.
int getIndexClosest(std::vector< double > _intersections)
Given an intersections vector will return the closest to the camera, the one that it will read colour...
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.
std::string m_image_name
Name of the filename that will be written.
ngl::Colour trace(ngl::Vec3 _from, ngl::Vec3 _direction, int _depth)
Probably the most important algorithm of this class. This method is recursive. It will fire a ray fro...
bool raycast(ngl::Vec3 _from, int _avoid)
Will fire rays from a position, then iterate over the lights, and if any object is inbetweet will ret...
Holds all the operations regarding to input/output of colour information.
int m_width
Width of the Film's image.
void writePixel(ngl::Colour _colour)
Will create a pixel and push it to the pixel vector.
ngl::Vec3 m_right
Camera right vector.
Hold all the objects and lights of the scene.
int m_height
Height of the Film's image.
This class is the heart of my raytracer. It is the core, where all the camera rays are calculated and...
Holds camera functions which will be accessed by the Render class.
This class handles the implementation of ray: an object with an origin and a direction.
std::vector< geo::Shape * > m_objects
Holds all the Shape objects in the scene.
ngl::Vec3 getDirection()
Getter method for the direction.