This repository has been archived by the owner on Nov 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
phone.py
117 lines (100 loc) · 4.1 KB
/
phone.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
import numpy as np
from glumpy import app, gl, glm, gloo, data
from os.path import abspath
vertex = """
uniform mat4 u_model; // Model matrix
uniform mat4 u_view; // View matrix
uniform mat4 u_projection; // Projection matrix
attribute vec3 a_position;
attribute vec3 a_normal;
varying vec3 v_texcoord;
varying vec3 v_normal;
varying vec3 v_position; // Interpolated position (out)
void main()
{
gl_Position = u_projection * u_view * u_model * vec4(a_position,1.0);
v_normal = a_normal;
v_texcoord = a_position;
v_position = a_position;
}
"""
fragment = """
uniform mat4 u_model; // Model matrix
uniform mat4 u_view; // View matrix
uniform mat4 u_normal; // Normal matrix
uniform vec3 u_light_position; // Light position
uniform vec3 u_light_intensity; // Light intensity
uniform samplerCube texture; // Texture
varying vec3 v_normal; // Interpolated normal (out)
varying vec3 v_position; // Interpolated position (out)
varying vec3 v_texcoord; // Interpolated fragment texture coordinates (out)
void main()
{
// Calculate normal in world coordinates
vec3 normal = normalize(u_normal * vec4(v_normal,1.0)).xyz;
// Calculate the location of this fragment (pixel) in world coordinates
vec3 position = vec3(u_view*u_model * vec4(v_position, 1));
// Calculate the vector from this pixels surface to the light source
vec3 surfaceToLight = u_light_position - position;
// Calculate the cosine of the angle of incidence (brightness)
float brightness = dot(normal, surfaceToLight) /
(length(surfaceToLight) * length(normal));
brightness = max(min(brightness,1.0),0.0);
// Calculate final color of the pixel, based on:
// 1. The angle of incidence: brightness
// 2. The color/intensities of the light: light.intensities
// 3. The texture and texture coord: texture(tex, fragTexCoord)
// Get texture color
vec4 t_color = textureCube(texture, v_texcoord);
gl_FragColor = t_color * (0.1 + 0.9*brightness * vec4(u_light_intensity, 1));
} """
window = app.Window(width=1024, height=1024)
@window.event
def on_draw(dt):
global phi, theta, beta
window.clear()
program.draw(gl.GL_QUADS)
# Rotate cube
glm.rotate(model, theta, 0, 0, 1)
glm.rotate(model, phi, 0, 1, 0)
glm.rotate(model, beta, 1, 0, 0)
program['u_model'] = model
program['u_normal'] = np.array(np.matrix(np.dot(view, model)).I.T)
@window.event
def on_init():
gl.glEnable(gl.GL_DEPTH_TEST)
@window.event
def on_resize(width, height):
program['u_projection'] = glm.perspective(45.0, width / float(height), 2.0, 100.0)
p = np.array([[+1,+1,+1], [-1,+1,+1], [-1,-1,+1], [+1,-1,+1],
[+1,-1,-1], [+1,+1,-1], [-1,+1,-1], [-1,-1,-1]])
n = np.array([[0, 0, 1], [1, 0, 0], [0, 1, 0],
[-1, 0, 1], [0, -1, 0], [0, 0, -1]])
faces_p = [0, 1, 2, 3, 0, 3, 4, 5, 0, 5, 6, 1,
1, 6, 7, 2, 7, 4, 3, 2, 4, 7, 6, 5]
faces_n = [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5]
program = gloo.Program(vertex, fragment)
program['a_position'] = [p[i] for i in faces_p]
program['a_normal'] = [n[i] for i in faces_n]
texture = np.zeros((6,1024,1024,3),dtype=np.float32).view(gloo.TextureCube)
texture.interpolation = gl.GL_LINEAR
texture[1] = data.get(abspath("Left_t4.png"))/255.
texture[0] = data.get(abspath("Right_t4.png"))/255.
texture[4] = data.get(abspath("Front_t4.png"))/255.
texture[5] = data.get(abspath("Back_t4.png"))/255.
texture[2] = data.get(abspath("Top_t4.png"))/255.
texture[3] = data.get(abspath("Bottom_t4.png"))/255.
model = np.eye(4,dtype=np.float32)
view = np.eye(4,dtype=np.float32)
glm.translate(view, 0, 0, -5)
glm.scale(model, 0.5, 1, 0.1)
program['texture'] = texture
program['u_model'] = model
program['u_view'] = view
program["u_light_position"] = 0,2,-2
program["u_light_intensity"] = 2,2,2
phi = 1
theta = 0.1
beta = 0.5
app.run()