INFO804 Introduction à l'informatique graphique
Loading...
Searching...
No Matches
Renderer.h
Go to the documentation of this file.
1/**
2@file Renderer.h
3@author JOL
4*/
5#pragma once
6#ifndef _RENDERER_H_
7#define _RENDERER_H_
8
9#include "Color.h"
10#include "Image2D.h"
11#include "Ray.h"
12
13/// Namespace RayTracer
14namespace rt {
15
16 inline void progressBar( std::ostream& output,
17 const double currentValue, const double maximumValue)
18 {
19 static const int PROGRESSBARWIDTH = 60;
20 static int myProgressBarRotation = 0;
21 static int myProgressBarCurrent = 0;
22 // how wide you want the progress meter to be
23 double fraction = currentValue /maximumValue;
24
25 // part of the progressmeter that's already "full"
26 int dotz = static_cast<int>(floor(fraction * PROGRESSBARWIDTH));
27 if (dotz > PROGRESSBARWIDTH) dotz = PROGRESSBARWIDTH;
28
29 // if the fullness hasn't changed skip display
30 if (dotz == myProgressBarCurrent) return;
31 myProgressBarCurrent = dotz;
32 myProgressBarRotation++;
33
34 // create the "meter"
35 int ii=0;
36 output << "[";
37 // part that's full already
38 for ( ; ii < dotz;ii++) output<< "#";
39 // remaining part (spaces)
40 for ( ; ii < PROGRESSBARWIDTH;ii++) output<< " ";
41 static const char* rotation_string = "|\\-/";
42 myProgressBarRotation %= 4;
43 output << "] " << rotation_string[myProgressBarRotation]
44 << " " << (int)(fraction*100)<<"/100\r";
45 output.flush();
46 }
47
48 /// This structure takes care of rendering a scene.
49 struct Renderer {
50
51 /// The scene to render
53 /// The origin of the camera in space.
55 /// (myOrigin, myOrigin+myDirUL) forms a ray going through the upper-left
56 /// corner pixel of the viewport, i.e. pixel (0,0)
58 /// (myOrigin, myOrigin+myDirUR) forms a ray going through the upper-right
59 /// corner pixel of the viewport, i.e. pixel (width,0)
61 /// (myOrigin, myOrigin+myDirLL) forms a ray going through the lower-left
62 /// corner pixel of the viewport, i.e. pixel (0,height)
64 /// (myOrigin, myOrigin+myDirLR) forms a ray going through the lower-right
65 /// corner pixel of the viewport, i.e. pixel (width,height)
67
68 int myWidth;
69 int myHeight;
70
71 Renderer() : ptrScene( 0 ) {}
72 Renderer( Scene& scene ) : ptrScene( &scene ) {}
73 void setScene( rt::Scene& aScene ) { ptrScene = &aScene; }
74
75 void setViewBox( Point3 origin,
76 Vector3 dirUL, Vector3 dirUR, Vector3 dirLL, Vector3 dirLR )
77 {
78 myOrigin = origin;
79 myDirUL = dirUL;
80 myDirUR = dirUR;
81 myDirLL = dirLL;
82 myDirLR = dirLR;
83 }
84
85 void setResolution( int width, int height )
86 {
87 myWidth = width;
88 myHeight = height;
89 }
90
91
92 /// The main rendering routine
93 void render( Image2D<Color>& image, int max_depth )
94 {
95 std::cout << "Rendering into image ... might take a while." << std::endl;
96 image = Image2D<Color>( myWidth, myHeight );
97 for ( int y = 0; y < myHeight; ++y )
98 {
99 Real ty = (Real) y / (Real)(myHeight-1);
100 progressBar( std::cout, ty, 1.0 );
101 Vector3 dirL = (1.0f - ty) * myDirUL + ty * myDirLL;
102 Vector3 dirR = (1.0f - ty) * myDirUR + ty * myDirLR;
103 dirL /= dirL.norm();
104 dirR /= dirR.norm();
105 for ( int x = 0; x < myWidth; ++x )
106 {
107 Real tx = (Real) x / (Real)(myWidth-1);
108 Vector3 dir = (1.0f - tx) * dirL + tx * dirR;
109 Ray eye_ray = Ray( myOrigin, dir, max_depth );
110 Color result = trace( eye_ray );
111 image.at( x, y ) = result.clamp();
112 }
113 }
114 std::cout << "Done." << std::endl;
115 }
116
117
118 /// The rendering routine for one ray.
119 /// @return the color for the given ray.
120 Color trace( const Ray& ray )
121 {
122 assert( ptrScene != 0 );
123 Color result = Color( 0.0, 0.0, 0.0 );
124 GraphicalObject* obj_i = 0; // pointer to intersected object
125 Point3 p_i; // point of intersection
126
127 // Look for intersection in this direction.
128 Real ri = ptrScene->rayIntersection( ray, obj_i, p_i );
129 // Nothing was intersected
130 if ( ri >= 0.0f ) return result; // some background color
131 return Color( 1.0, 1.0, 1.0 );
132 }
133
134 };
135
136} // namespace rt
137
138#endif // #define _RENDERER_H_
Classe générique pour représenter des images 2D.
Definition Image2D.h:10
Value at(int i, int j) const
Definition Image2D.h:185
Namespace RayTracer.
Definition Color.h:11
float Real
the type for representing a real number.
PointVector< Real, 3 > Point3
The type for representing a 3d vector;.
PointVector< Real, 3 > Vector3
The type for representing a 3d point.
Color & clamp()
Garantees that color channels are between 0 and 1.
Definition Color.h:27
Definition Ray.h:18
This structure takes care of rendering a scene.
Definition Renderer.h:49
Vector3 myDirUR
Definition Renderer.h:60
Vector3 myDirUL
Definition Renderer.h:57
Vector3 myDirLL
Definition Renderer.h:63
Scene * ptrScene
The scene to render.
Definition Renderer.h:52
Color trace(const Ray &ray)
Definition Renderer.h:120
Vector3 myDirLR
Definition Renderer.h:66
Point3 myOrigin
The origin of the camera in space.
Definition Renderer.h:54
void render(Image2D< Color > &image, int max_depth)
The main rendering routine.
Definition Renderer.h:93
Real rayIntersection(const Ray &ray, GraphicalObject *&object, Point3 &p)
Definition Scene.h:93