-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhongShader.cc
164 lines (122 loc) · 4.05 KB
/
PhongShader.cc
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
#include "PhongShader.hh"
#include <math.h>
void PhongShader::Reflect(Vec3f &newDir, Vec3f &normal, Vec3f &oldDir)
{
// Escreva o codigo aqui
}
Vec3f PhongShader::Shade(Ray &ray){
const Vec3f la = Vec3f(1,1,1);
const Vec3f ca = color;
const Vec3f cd = ca;
const Vec3f cs(1,1,1);
Vec3f incident(ray.dir);
Vec3f normal(ray.hit->GetNormal(ray));
float cosinus = Dot(incident,normal);
if (cosinus > 0) normal = -normal; else cosinus = -cosinus;
Vec3f reflected = (2*cosinus)*normal + incident;
Vec3f ambient = ka*Product(ca,la);
Vec3f sumDiffuse(0,0,0);
Vec3f sumSpecular(0,0,0);
Vec3f intensidade;
Vec3f dir;
for(int i=0;i < scene->lights.size();i++){
intensidade = scene->lights[i]->Illuminate(dir,ray.getHitPoint(0),0);
intensidade.assertInterval(0,1);
sumDiffuse = sumDiffuse + intensidade*std::max(0.0f,Dot(dir,normal));
float specularPower = std::max(0.0f,Dot(dir,reflected));
//cout << specularPower << endl;
if (specularPower > 0)
specularPower = pow(specularPower,ke);
sumSpecular = intensidade * specularPower;
sumSpecular.assertInterval(0,1);
}
Vec3f diffuse = kd * Product(cd, sumDiffuse);
diffuse.assertInterval(0,1);
Vec3f specular = ks * Product(cs, sumSpecular);
specular.assertInterval(0,1);
Vec3f result = ambient + diffuse +specular;
result.assertInterval(0,1);
return result;//* fabs(Dot(ray.dir,ray.hit->GetNormal(ray)));
}
/*
Vec3f PhongShader::Shade(Ray &ray){
const Vec3f la(1,1,1);
const Vec3f ca = color;
const Vec3f cd = ca;
const Vec3f cs(1,1,1);
Vec3f normal = ray.hit->GetNormal(ray);
if(Dot(normal,ray.dir) > 0.0f)
normal = - normal;
//Normalize(normal);
// Vetor de reflexao!!!
Vec3f reflect = ray.dir - 2.0f * Dot(normal,ray.dir) * normal;
//Normalize(reflect);
Vec3f termo1 = ka*Product(ca,la);
Vec3f termo2(0,0,0);
Vec3f termo3(0,0,0);
for(int i=0;i < scene->lights.size();i++){
Vec3f dir;
Vec3f intensidade = scene->lights[i]->Illuminate(dir,ray.getHitPoint(0),0);
float cosLightNormal = Dot(dir,normal);
if(cosLightNormal <= 0)
continue;
termo2 = termo2 + intensidade*cosLightNormal;
float cosLightReflect = Dot(dir,reflect);
if(cosLightReflect <= 0)
continue;
termo3 = termo3 + intensidade*powf(cosLightReflect,ke);
}
termo2 = Product(termo2,cd)*kd;
termo3 = Product(termo3,cs)*ks;
Vec3f result = termo1 + termo2 + termo3;
//result.assertInterval(0,1);
return result;
}
*/
/*
Vec3f PhongShader::Shade(Ray &ray)
{
Vec3f act = color;
Vec3f col;
Vec3f normal = ray.hit->GetNormal(ray);
if(Dot(normal,ray.dir) > 0.0f)
normal = - normal;
// Vetor de reflecao!!!
Vec3f reflect = ray.dir - 2.0f * Dot(normal,ray.dir) * normal;
// Dados do ambiente
Vec3f ambienteIntensidade(1.0f,1.0f,1.0f);
Vec3f amienteCor = ka*color;
Vec3f result = Product(amienteCor,ambienteIntensidade);
// Na direcao da luz
Ray shadow;
shadow.org = ray.org + ray.t * ray.dir - Vec3f(1.0f,1.0f,1.0f)*Epsilon; // -episilon
// Para cara funte luz
for(int i=0;i < scene->lights.size();i++){
Vec3f intensidadeLuz;
Vec3f result_local(0,0,0);
//para o numero de raios que SAEM do ponto de luz!!! no meu caso é 1
if(scene->lights[i]->Illuminate(shadow,intensidadeLuz)){
//distancis pra fonte de luz
float distancia = shadow.t;
float cosLightNormal = Dot(shadow.dir,normal);
if(cosLightNormal > 0.0f){
if(scene->primitives.Intersect(shadow)){
if(shadow.t < distancia)
continue;
}
// termo difuso
Vec3f corDifusa = kd*color;
result_local = result_local + Product(corDifusa * cosLightNormal,intensidadeLuz);
//termo specular
float cosLightReflect = Dot(shadow.dir,reflect);
if(cosLightReflect > 0.0f){
Vec3f corSpecular = ks * Vec3f(1.0f,1.0f,1.0f); // branca
result_local = result_local + Product(corSpecular * powf(cosLightReflect,ke),intensidadeLuz);
}
}
}
result = result + result_local;
}
return result;
}
*/