-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgmath.py
More file actions
165 lines (129 loc) · 4.97 KB
/
gmath.py
File metadata and controls
165 lines (129 loc) · 4.97 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
import math
from display import *
# IMPORANT NOTE
# Ambient light is represeneted by a color value
# Point light sources are 2D arrays of doubles.
# - The fist index (LOCATION) represents the vector to the light.
# - The second index (COLOR) represents the color.
# Reflection constants (ka, kd, ks) are represened as arrays of
# doubles (red, green, blue)
'''SCALING USED FOR MESH FILE'''
SCALING = 30
AMBIENT = 0
DIFFUSE = 1
SPECULAR = 2
LOCATION = 0
COLOR = 1
SPECULAR_EXP = 4
# estimate for float point error
def estimate(vertex):
est = [ int( "%.0f" % (v * 10000) ) for v in vertex ]
return est
#return dictionary of average vector normals for polygon matrix
def calculate_vertex_norms(polygons):
# norms = { k: [ avg_normal, num_elements ] }
norms = {}
for point in polygons:
norms[ str(estimate(point)) ] = [0,0,0]
i = 0
while i < len(polygons) - 2:
tri_normal = calculate_normal(polygons, i)
normalize(tri_normal)
# points = [ (polygons[i][0], polygons[i][1], polygons[i][2]),
# (polygons[i+1][0], polygons[i+1][1], polygons[i+1][2]),
# (polygons[i+2][0], polygons[i+2][1], polygons[i+2][2]) ]
points = [ (polygons[i][0], polygons[i][1], polygons[i][2], 1),
(polygons[i+1][0], polygons[i+1][1], polygons[i+1][2], 1),
(polygons[i+2][0], polygons[i+2][1], polygons[i+2][2], 1) ]
# print tri_normal
point_keys = [ str(estimate(p)) for p in points ]
for pk in point_keys:
# print pk in norms
norms[pk][0] += tri_normal[0]
norms[pk][1] += tri_normal[1]
norms[pk][2] += tri_normal[2]
#if pk not in norms:
# norms[pk] = [tri_normal, 0]
#else:
# # counter
# norms[pk][1] += 1
# # average
# norms[pk][0] = [ (norms[pk][0][index] + tri_normal[index]) for index in range(len(tri_normal))]
i += 3
for key in norms:
normalize(norms[key])
return norms
#lighting functions
def get_lighting(normal, view, ambient, light, symbols, reflect ):
n = normal[:]
normalize(n)
normalize(light[LOCATION])
normalize(view)
r = symbols[reflect][1]
a = calculate_ambient(ambient, r)
d = calculate_diffuse(light, r, n)
s = calculate_specular(light, r, view, n)
i = [0, 0, 0]
i[RED] = int(a[RED] + d[RED] + s[RED])
i[GREEN] = int(a[GREEN] + d[GREEN] + s[GREEN])
i[BLUE] = int(a[BLUE] + d[BLUE] + s[BLUE])
return i
def calculate_ambient(alight, reflect):
a = [0, 0, 0]
a[RED] = alight[RED] * reflect['red'][AMBIENT]
a[GREEN] = alight[GREEN] * reflect['green'][AMBIENT]
a[BLUE] = alight[BLUE] * reflect['blue'][AMBIENT]
return a
def calculate_diffuse(light, reflect, normal):
d = [0, 0, 0]
dot = dot_product( light[LOCATION], normal)
dot = dot if dot > 0 else 0
d[RED] = light[COLOR][RED] * reflect['red'][DIFFUSE] * dot
d[GREEN] = light[COLOR][GREEN] * reflect['green'][DIFFUSE] * dot
d[BLUE] = light[COLOR][BLUE] * reflect['blue'][DIFFUSE] * dot
return d
def calculate_specular(light, reflect, view, normal):
s = [0, 0, 0]
n = [0, 0, 0]
result = 2 * dot_product(light[LOCATION], normal)
n[0] = (normal[0] * result) - light[LOCATION][0]
n[1] = (normal[1] * result) - light[LOCATION][1]
n[2] = (normal[2] * result) - light[LOCATION][2]
result = dot_product(n, view)
result = result if result > 0 else 0
result = pow( result, SPECULAR_EXP )
s[RED] = light[COLOR][RED] * reflect['red'][SPECULAR] * result
s[GREEN] = light[COLOR][GREEN] * reflect['green'][SPECULAR] * result
s[BLUE] = light[COLOR][BLUE] * reflect['blue'][SPECULAR] * result
return s
def limit_color(color):
color[RED] = 255 if color[RED] > 255 else color[RED]
color[GREEN] = 255 if color[GREEN] > 255 else color[GREEN]
color[BLUE] = 255 if color[BLUE] > 255 else color[BLUE]
#vector functions
#normalize vetor, should modify the parameter
def normalize(vector):
magnitude = math.sqrt( vector[0] * vector[0] +
vector[1] * vector[1] +
vector[2] * vector[2])
for i in range(3):
vector[i] = vector[i] / magnitude if magnitude != 0 else 1
#Return the dot porduct of a . b
def dot_product(a, b):
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
#Calculate the surface normal for the triangle whose first
#point is located at index i in polygons
def calculate_normal(polygons, i):
A = [0, 0, 0]
B = [0, 0, 0]
N = [0, 0, 0]
A[0] = polygons[i+1][0] - polygons[i][0]
A[1] = polygons[i+1][1] - polygons[i][1]
A[2] = polygons[i+1][2] - polygons[i][2]
B[0] = polygons[i+2][0] - polygons[i][0]
B[1] = polygons[i+2][1] - polygons[i][1]
B[2] = polygons[i+2][2] - polygons[i][2]
N[0] = A[1] * B[2] - A[2] * B[1]
N[1] = A[2] * B[0] - A[0] * B[2]
N[2] = A[0] * B[1] - A[1] * B[0]
return N