-
Notifications
You must be signed in to change notification settings - Fork 0
/
shaders.py
160 lines (139 loc) · 4.4 KB
/
shaders.py
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
from render import *
from Texture import Texture
def stars(r):
for i in range(0,r.width-1):
for j in range(0,r.height-1):
if(random.randint(0,i)==i and i>(0.01*j + 400)):
r.pixels[i][j] = color(255,255,255)
def water(r):
for i in range(0,r.width-1):
for j in range(0,r.height-1):
if(i>(0.01*j + 400)):
pixels = r.pixels[i][j]
red = pixels[0]+5 if pixels[0]<250 else 255
green = pixels[1]+2 if pixels[1]<253 else 255
blue = pixels[2]+12 if pixels[2]<235 else 255
r.pixels[400-(i-400)][j] = bytes([blue,green,red])
def moon(render, **kwargs):
# barycentric
w, v, u = kwargs['bar']
# texture
tx, ty = kwargs['texture_coords']
tcolor = render.active_texture.get_color(tx, ty)
# normals
nA, nB, nC = kwargs['varying_normals']
# light intensity
iA, iB, iC = [ dot(n, render.light) for n in (nA, nB, nC) ]
intensity = w*iA + v*iB + u*iC
intensity = intensity**3
if(tcolor):
return color(
int(tcolor[2] * intensity) if tcolor[0] * intensity > 0 else 0,
int(tcolor[1] * intensity) if tcolor[1] * intensity > 0 else 0,
int(tcolor[0] * intensity) if tcolor[2] * intensity > 0 else 0
)
else:
return color(0,0,0)
def mountain(render, **kwargs):
# barycentric
w, v, u = kwargs['bar']
# texture
tx, ty = kwargs['texture_coords']
tcolor = render.active_texture.get_color(tx, ty)
# normals
nA, nB, nC = kwargs['varying_normals']
# light intensity
iA, iB, iC = [ dot(n, render.light) for n in (nA, nB, nC) ]
intensity = w*iA + v*iB + u*iC
intensity = intensity**3
if(tcolor):
return color(
int(tcolor[2] * intensity) if tcolor[0] * intensity > 0 else 0,
int(tcolor[1] * intensity) if tcolor[1] * intensity > 0 else 0,
int(tcolor[0] * intensity) if tcolor[2] * intensity > 0 else 0
)
else:
return color(0,0,0)
def planet(render, **kwargs):
# barycentric
w, v, u = kwargs['bar']
# texture
tx, ty = kwargs['texture_coords']
tcolor = render.active_texture.get_color(tx, ty)
# normals
nA, nB, nC = kwargs['varying_normals']
# light intensity
iA, iB, iC = [ dot(n, render.light) for n in (nA, nB, nC) ]
intensity = w*iA + v*iB + u*iC
intensity = intensity**3
if(tcolor):
return color(
int(tcolor[2] * intensity) if tcolor[0] * intensity > 0 else 0,
int(tcolor[1] * intensity) if tcolor[1] * intensity > 0 else 0,
int(tcolor[0] * intensity) if tcolor[2] * intensity > 0 else 0
)
else:
return color(0,0,0)
import random
def fragment(render, **kwargs):
# barycentric
w, v, u = kwargs['bar']
# texture
tx, ty = kwargs['texture_coords']
# tcolor = render.active_texture.get_color(tx, ty)
grey = int(ty * 100)
tcolor = color(grey, 50, 15)
# normals
nA, nB, nC = kwargs['varying_normals']
# light intensity
iA, iB, iC = [ dot(n, render.light) for n in (nA, nB, nC) ]
intensity = w*iA + v*iB + u*iC
if (intensity>0.85):
intensity = 1
elif (intensity>0.60):
intensity = 0.80
elif (intensity>0.45):
intensity = 0.60
elif (intensity>0.30):
intensity = 0.45
elif (intensity>0.15):
intensity = 0.30
else:
intensity = 0
return color(
int(tcolor[2] * intensity) if tcolor[0] * intensity > 0 else 0,
int(tcolor[1] * intensity) if tcolor[1] * intensity > 0 else 0,
int(tcolor[0] * intensity) if tcolor[2] * intensity > 0 else 0
)
def ground(render, **kwargs):
# barycentric
w, v, u = kwargs['bar']
# texture
tx, ty = kwargs['texture_coords']
tcolor = render.active_texture.get_color(tx, ty)
# normals
nA, nB, nC = kwargs['varying_normals']
# light intensity
iA, iB, iC = [ dot(n, render.light) for n in (nA, nB, nC) ]
intensity = w*iA + v*iB + u*iC
intensity = intensity/4
if(tcolor):
return color(
int(tcolor[2] * intensity) if tcolor[0] * intensity > 0 else 0,
int(tcolor[1] * intensity) if tcolor[1] * intensity > 0 else 0,
int(tcolor[0] * intensity) if tcolor[2] * intensity > 0 else 0
)
else:
return color(0,0,0)
def table(render, **kwargs):
tx, ty = kwargs['texture_coords']
return color(255,255,255)
r = Render(800, 800)
t = Texture('./models/model.bmp')
r.light = V3(0, 1, 1)
r.active_texture = t
r.active_shader = fragment
r.lookAt(V3(1, 0, 5), V3(0, 0, 0), V3(0, 1, 0))
r.load('./models/model.obj', translate=(0, 0, 0), scale=(1, 1, 1), rotate=(0, 0, 0))
r.draw_arrays('TRIANGLES')
r.display('out.bmp')