-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.py
113 lines (93 loc) · 2.81 KB
/
4.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
import glfw
from OpenGL.GL import *
import numpy as np
global gComposedM
gComposedM = np.identity(3)
def render(T):
glClear(GL_COLOR_BUFFER_BIT)
glLoadIdentity()
# draw cooridnate
glBegin(GL_LINES)
glColor3ub(255, 0, 0)
glVertex2fv(np.array([0.,0.]))
glVertex2fv(np.array([1.,0.]))
glColor3ub(0, 255, 0)
glVertex2fv(np.array([0.,0.]))
glVertex2fv(np.array([0.,1.]))
glEnd()
# draw triangle
glBegin(GL_TRIANGLES)
glColor3ub(255, 255, 255)
glVertex2fv( (T @ np.array([.0,.5,1.]))[:-1] )
glVertex2fv( (T @ np.array([.0,.0,1.]))[:-1] )
glVertex2fv( (T @ np.array([.5,.0,1.]))[:-1] )
glEnd()
def key_callback(window, key, scancode, action, mods):
global gComposedM
if key==glfw.KEY_W:
if (action==glfw.PRESS or action==glfw.REPEAT):
T = np.array([[1, 0, 0],
[0, 0.9, 0],
[0, 0, 1]])
gComposedM = T @ gComposedM
if key==glfw.KEY_E:
if (action==glfw.PRESS or action==glfw.REPEAT):
T = np.array([[1, 0, 0],
[0, 1.1, 0],
[0, 0, 1]])
gComposedM = T @ gComposedM
if key==glfw.KEY_S:
if (action==glfw.PRESS or action==glfw.REPEAT):
th = np.radians(10)
T = np.array([[np.cos(th), -np.sin(th), 0],
[np.sin(th), np.cos(th), 0],
[0, 0, 1]])
gComposedM = T @ gComposedM
if key==glfw.KEY_D:
if (action==glfw.PRESS or action==glfw.REPEAT):
th = np.radians(10)
T = np.array([[np.cos(th), np.sin(th), 0],
[-np.sin(th), np.cos(th), 0],
[0, 0, 1]])
gComposedM = T @ gComposedM
if key==glfw.KEY_X:
if (action==glfw.PRESS or action==glfw.REPEAT):
T = np.array([[1, 0, 0.1],
[0, 1, 0],
[0, 0, 1]])
gComposedM = T @ gComposedM
if key==glfw.KEY_C:
if (action==glfw.PRESS or action==glfw.REPEAT):
T = np.array([[1, 0, -0.1],
[0, 1, 0],
[0, 0, 1]])
gComposedM = T @ gComposedM
if key==glfw.KEY_R:
if (action==glfw.PRESS or action==glfw.REPEAT):
T = np.array([[-1, 0, 0],
[0, -1, 0],
[0, 0, 1]])
gComposedM = T @ gComposedM
if key==glfw.KEY_1:
if (action==glfw.PRESS or action==glfw.REPEAT):
T = np.identity(3)
gComposedM = T
def main():
if not glfw.init():
return
window = glfw.create_window(480,480,"A", None,None)
if not window:
glfw.terminate()
return
glfw.set_key_callback(window, key_callback)
glfw.make_context_current(window)
glfw.swap_interval(1)
global gComposedM
gComposedM = np.identity(3)
while not glfw.window_should_close(window):
glfw.poll_events()
render(gComposedM)
glfw.swap_buffers(window)
glfw.terminate()
if __name__ == "__main__":
main()