-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathobjetos.py
More file actions
255 lines (204 loc) · 7.91 KB
/
objetos.py
File metadata and controls
255 lines (204 loc) · 7.91 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
"""
Classe que presenta cada objeto a ser mostrado na cena
"""
from Algebra import Dot, Vector3D, Normal, Normalize, Cross, Parallelogram_Area
from math import sqrt
class Objeto:
area = 0.0
def __init__(self, A, B, C, color, ka, kd,ks, kt, n, trans_difusa, trans_especular):
"""
:param vertices - lista de vertices:
:param faces: lista de Faces
:param cor: cor do objeto (R,G,B):
:param ka: coeficiente ambiental
:param kd: coeficiente difuso
:param ks: coeficiente especular
:param kt: coeficiente de transparencia
:param n: expoente de reflexão especular
"""
self.A = A # Vertice A
self.B = B # Vertice B
self.C = C # Vertice C
self.normal = Normal(A, B, C)
self.color = color
self.ka = ka
self.kd = kd
self.ks = ks
self.kt = kt
self.n = n
self.trans_difusa = trans_difusa
self.trans_especular = trans_especular
def intersect(self, ray):
r = Normalize(ray.d)
# Checando se o triangulo e o raio são paralelos
if Dot(r, self.normal) == 0.0:
# Raio nao intersecta o triangulo
hit = False
distance = 0.0
hit_point = Vector3D(0.0, 0.0, 0.0)
return (hit, distance, hit_point, self.normal)
ray_dir = Normalize(ray.d)
# Calculando o ponto que pode está no plano do triangulo
t = Dot(self.normal, (self.A - ray.o)) / Dot(ray_dir, self.normal)
hit_point = ray.get_hitpoint(t)
if t < 0.0001:
# Raio nao intersecta o triangulo
hit = False
distance = 0.0
hit_point = Vector3D(0.0, 0.0, 0.0)
return (hit, distance, hit_point, self.normal)
# Checando se o Ponto está dentro do triangulo
# Inside-OutSide Test
vectorAB = self.B - self.A;
vectorBC = self.C - self.B;
vectorCA = self.A - self.C;
C0 = hit_point - self.A
C1 = hit_point - self.B
C2 = hit_point - self.C
if (Dot(self.normal, Cross(vectorAB, C0)) > 0
and Dot(self.normal, Cross(vectorBC, C1)) > 0
and Dot(self.normal, Cross(vectorCA, C2))) > 0:
#print("Acertou: " + str (vectorAB))
hit = True
distance = t
hit_point = ray.get_hitpoint(t)
return (hit, distance, hit_point, self.normal) # tuple
if (Dot(self.normal, Cross(vectorAB, C0)) < 0
and Dot(self.normal, Cross(vectorBC, C1)) < 0
and Dot(self.normal, Cross(vectorCA, C2))) < 0:
#print("Acertou")
hit = True
distance = t
hit_point = ray.get_hitpoint(t)
return (hit, distance, hit_point, self.normal) # tuple
# Didn't hit the triangule
hit = False
distance = 0.0
hit_point = Vector3D(0.0, 0.0, 0.0)
return (hit, distance, hit_point, self.normal)
class Light():
def __init__(self, A, B, C, color, lp):
self.A = A # Vertice A
self.B = B # Vertice B
self.C = C # Vertice C
self.normal = Normal(A, B, C)
self.color = color
self.lp = lp
def intersect(self, ray):
r = Normalize(ray.d)
# Checando se o triangulo e o raio são paralelos
if Dot(r, self.normal) == 0.0:
# Raio nao intersecta o triangulo
hit = False
distance = 0.0
hit_point = Vector3D(0.0, 0.0, 0.0)
return (hit, distance, hit_point, self.normal)
ray_dir = Normalize(ray.d)
# Calculando o ponto que pode está no plano do triangulo
t = Dot(self.normal, (self.A - ray.o)) / Dot(ray_dir, self.normal)
hit_point = ray.get_hitpoint(t)
if t < 0.0001:
# Raio nao intersecta o triangulo
hit = False
distance = 0.0
hit_point = Vector3D(0.0, 0.0, 0.0)
return (hit, distance, hit_point, self.normal)
# Checando se o Ponto está dentro do triangulo
# Inside-OutSide Test
vectorAB = self.B - self.A;
vectorBC = self.C - self.B;
vectorCA = self.A - self.C;
C0 = hit_point - self.A
C1 = hit_point - self.B
C2 = hit_point - self.C
if (Dot(self.normal, Cross(vectorAB, C0)) > 0
and Dot(self.normal, Cross(vectorBC, C1)) > 0
and Dot(self.normal, Cross(vectorCA, C2))) > 0:
#print("Acertou: " + str (vectorAB))
hit = True
distance = t
hit_point = ray.get_hitpoint(t)
return (hit, distance, hit_point, self.normal) # tuple
if (Dot(self.normal, Cross(vectorAB, C0)) < 0
and Dot(self.normal, Cross(vectorBC, C1)) < 0
and Dot(self.normal, Cross(vectorCA, C2))) < 0:
#print("Acertou")
hit = True
distance = t
hit_point = ray.get_hitpoint(t)
return (hit, distance, hit_point, self.normal) # tuple
# Didn't hit the triangule
hit = False
distance = 0.0
hit_point = Vector3D(0.0, 0.0, 0.0)
return (hit, distance, hit_point, self.normal)
class ObjectQuadric():
def __init__(self, a, b, c, d, e, f, g, h, j, k, color, ka,
kd, ks, kt, n, trans_difusa, trans_especular):
self.a = a
self.b = b
self.c = c
self.d = d
self.e = e
self.f = f
self.g = g
self.h = h
self.j = j
self.k = k
self.color = color
self.ka = ka
self.kd = kd
self.ks = ks
self.kt = kt
self.n = n
self.trans_difusa = trans_difusa
self.trans_especular = trans_especular
def intersect(self, ray):
d = ray.d - ray.o
dx = d[0]
dy = d[1]
dz = d[2]
x0 = ray.o[0]
y0 = ray.o[1]
z0 = ray.o[2]
acoef = 2 * self.f * dx * dz + 2 * self.e * dy * dz + self.c * dz * dz + self.b * dy * dy + self.a * dx * dx + 2 * d * dx * dy
bcoef = (2 * self.b * y0 * dy + 2 * self.a * x0 * dx + 2 * self.c * z0 * dz) + \
(2 * self.h * dy + 2 * self.g * dx + 2 * self.j * dz + 2 * d * x0 * dy) + \
(2 * self.e * y0 * dz + 2 * self.e * z0 * dy + 2 * d * y0 * dx) + \
(2 * self.f * x0 * dz + 2 * self.f * z0 * dx)
ccoef = (self.a * x0 * x0 + 2 * self.g * x0 + 2 * self.f * x0 * z0 + self.b * y0 * y0) + \
(2 * self.e * y0 * z0 + 2 * d * x0 * y0 + self.c * z0 * z0 + 2 * self.h * y0) + \
(2 * self.j * z0 + self.k)
## The following was modified by David J. Brandow to allow for planar
## quadrics
if 1.0 + acoef == 1.0:
if 1.0 + bcoef == 1.0:
hit = False
distance = 0.0
hit_point = Vector3D(0.0, 0.0, 0.0)
return (hit, distance, hit_point, self.normal)
t = (-ccoef) / (bcoef)
else:
disc = bcoef * bcoef - 4 * acoef * ccoef
if 1.0 + disc < 1.0:
hit = False
distance = 0.0
hit_point = Vector3D(0.0, 0.0, 0.0)
return (hit, distance, hit_point, self.normal)
root = sqrt(disc)
t = (-bcoef - root) / (acoef + acoef)
if t < 0.0:
t = (-bcoef + root) / (acoef + acoef)
if (t < 0.001):
hit = False
distance = 0.0
hit_point = Vector3D(0.0, 0.0, 0.0)
return (hit, distance, hit_point, self.normal)
hit_point = ray.get_hitpoint(t)
normal = hit_point - Vector3D(self.a, self.b, self.c)
return (True, t, hit_point, normal)
# Didn't hit the Quadradic
#hit = False
#distance = 0.0
#hit_point = Vector3D(0.0, 0.0, 0.0)
#return (hit, distance, hit_point, self.normal)