/
renderer.cpp
178 lines (153 loc) · 5.06 KB
/
renderer.cpp
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "renderer.hpp"
#include "vektoroperations.hpp"
#include <iostream>
#include "camera.hpp"
#include <math.h>
Renderer::Renderer(Scene const& scene, unsigned w, unsigned h, std::string const& file):
scene_(scene),
width_(w),
height_(h),
colorbuffer_(w*h, Color(0.0, 0.0, 0.0)),
filename_(file),
ppm_(width_, height_)
{}
void Renderer::render()
{
if(scene_.sizeShape > 0)
{
for (unsigned y = 0; y < height_; ++y)
{
for (unsigned x = 0; x < width_; ++x)
{
std::vector<Ray> rays;
rays.push_back({scene_.cam->calculateRay(x, y)});
rays.push_back({scene_.cam->calculateRay(x + 0.5, y)});
rays.push_back({scene_.cam->calculateRay(x, y + 0.5)});
rays.push_back({scene_.cam->calculateRay(x + 0.5, y + 0.5)});
Color colorAA(0.0f,0.0f,0.0f);
for (auto ronny : rays) {
colorAA += raytrace(ronny, 1);
}
Pixel p(x,y);
p.color = 1.0f / rays.size() * colorAA;
//std::cout<<"Pixel ["<<x<<","<<y<<"]"<<" RGB ["<<p.color.r<<","<<p.color.g<<","<<p.color.b<<"]"<<std::endl;
write(p);
}
}//hier ist der colorbuffer geladen
//antialias();
}
else
{
std::cout<<"Es wurden keine Objekte geladen."<<std::endl;
}
ppm_.save(filename_);
}
Color Renderer::raytrace(Ray const& ronny,unsigned int depth) const
{
depth --;
Hit hit = calculateHit(ronny);
if (hit.impact)
{
Color c{1,0,0};
float ia = 0.10;
float ip = 0, LN = 0, RV = 0;
Material mat{hit.shape->getmat()};
c.r = ia * mat.ka_.r; //die abmienten
c.g = ia * mat.ka_.g; //terme werden
c.b = ia * mat.ka_.b; //zugewiesen
for (unsigned int i = 0 ; i < scene_.sizeLight ; i++)
{
if(illuminate(hit, scene_.lights[i]->pos))
{
ip = scene_.lights[i]->intensity; //intensität des lichts
LN = skalar(glm::normalize(scene_.lights[i]->pos - hit.point) , glm::normalize(hit.normal)); //winkel normale / blickwinkel
RV = skalar(glm::normalize(mirror(scene_.lights[i]->pos , Ray{hit.point, hit.normal})) , glm::normalize(ronny.origin - hit.point));
if(LN < 0) LN = 0;
if(RV < 0) RV = 0;
c.r += ip * (LN * mat.kd_.r + mat.ks_.r * pow(RV,mat.m_));
c.g += ip * (LN * mat.kd_.g + mat.ks_.g * pow(RV,mat.m_));
c.b += ip * (LN * mat.kd_.b + mat.ks_.b * pow(RV,mat.m_));
}
}
return c;
}
}
Hit Renderer::calculateHit(Ray const& rafa) const
{
Hit nearest;
for(auto const& shape: scene_.shapes)
{
if(shape != nullptr)
{
Ray ray{rafa.origin,glm::normalize(rafa.direction)};
Hit newHit = shape->intersect(ray);
if(newHit.impact && 0.0001 < newHit.distance && newHit.distance < nearest.distance)
{
nearest = newHit;
}
}
else std::cout<< "shape == nullptr"<<std::endl;
}
return nearest;
}
bool Renderer::illuminate(Hit const& hit, glm::vec3 const& lightPos) const
{
float epschilom = 0.001f;
glm::vec3 point = hit.point + (epschilom * glm::normalize(hit.normal));
Hit shadow = calculateHit(Ray{ point , lightPos - point});
return (!shadow.impact or glm::length( point - lightPos) < glm::length( point - shadow. point) );
}
Color Renderer::tonemap(Color c){
float r,g,b;
float l = 0.2126 * c.r + 0.7512 * c.g + 0.0722 * c.b;
/*
//John Hable Method
r=std::max(0.0f,c.r-0.004f);
g=std::max(0.0f,c.g-0.004f);
b=std::max(0.0f,c.b-0.004f);
r=(r*(6.2*r+0.5))/(r*(6.2*r+1.7)+0.06);
g=(g*(6.2*g+0.5))/(g*(6.2*g+1.7)+0.06);
b=(b*(6.2*b+0.5))/(b*(6.2*b+1.7)+0.06);
return {r,g,b};
*/
//Simple Operator
return{c.r+0.1f,c.g+0.1f,c.b+0.1f};
/*
//Filmic Operator (http://filmicgames.com/archives/75)
float A = 0.15; //shoulder strength
float B = 0.50; //linear strength
float C = 0.10; //linear angle
float D = 0.20; //toe strength
float E = 0.02; //toe numerator
float F = 0.30; //toe denominator
float W = 11.2; //linear hitepoint
r=((2*c.r * (A * 2*c.r + C * B) + D * E) / (2*c.r * (A * 2*c.r + B) + D * F)) - E / F;
g=((2*c.g * (A * 2*c.g + C * B) + D * E) / (2*c.g * (A * 2*c.g + B) + D * F)) - E / F;
b=((2*c.b * (A * 2*c.b + C * B) + D * E) / (2*c.b * (A * 2*c.b + B) + D * F)) - E / F;
r = r * (1 / ((W * (A * W + C * B) + D * E) / (W * (A * W + B) + D * F)) - E / F);
g = g * (1 / ((W * (A * W + C * B) + D * E) / (W * (A * W + B) + D * F)) - E / F);
b = b * (1 / ((W * (A * W + C * B) + D * E) / (W * (A * W + B) + D * F)) - E / F);
return {r,g,b};
*/
}
void Renderer::write(Pixel const& p)
{
// flip pixels, because of opengl glDrawPixels
size_t buf_pos = (width_*p.y + p.x);
if (buf_pos >= colorbuffer_.size() or (int)buf_pos < 0)
{
std::cerr << "Fatal Error Renderer::write(Pixel p) : "
<< "pixel out of ppm_ : "
<< (int)p.x << "," << (int)p.y
<< std::endl;
}
else
{
colorbuffer_[buf_pos] = p.color;
}
ppm_.write(p);
}
void Renderer::antialias()
{
//http://paulbourke.net/miscellaneous/aliasing/
}