-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask2.2(final).py
More file actions
159 lines (132 loc) · 4.05 KB
/
task2.2(final).py
File metadata and controls
159 lines (132 loc) · 4.05 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
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
import random
W_Width, W_Height = 500, 500
speed = 0.05
blink = False
frozen = False
points = []
def convert_coordinate(x, y):
global W_Width, W_Height
a = x - (W_Width / 2)
b = (W_Height / 2) - y
return a, b
def draw_points():
global points, color, blink
glPointSize(5)
glBegin(GL_POINTS)
for i in points:
x, y, dx, dy, color, visible = i
if visible or not blink:
glColor3f(*color)
glVertex2f(x, y)
glEnd()
# def drawAxes():
# glLineWidth(1)
# glBegin(GL_LINES)
# glColor3f(1.0, 0.0, 0.0)
# glVertex2f(250, 0)
# glVertex2f(-250, 0)
# glColor3f(0.0, 0.0, 1.0)
# glVertex2f(0, 250)
# glVertex2f(0, -250)
# glEnd()
def drawBox():
glBegin(GL_LINES)
glColor3f(1.0, 1.0, 0.0)
glVertex2d(-200, -150)
glVertex2d(-200, 150)
glVertex2d(200, -150)
glVertex2d(200, 150)
glVertex2d(-200, -150)
glVertex2d(200, -150)
glVertex2d(-200, 150)
glVertex2d(200, 150)
glEnd()
def display():
#//clear the display
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glClearColor(0,0,0,0); #//color black
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
#//load the correct matrix -- MODEL-VIEW matrix
glMatrixMode(GL_MODELVIEW)
#//initialize the matrix
glLoadIdentity()
#//now give three info
#//1. where is the camera (viewer)?
#//2. where is the camera looking?
#//3. Which direction is the camera's UP direction?
gluLookAt(0,0,200, 0,0,0, 0,1,0)
glMatrixMode(GL_MODELVIEW)
# drawAxes()
drawBox()
draw_points()
glutSwapBuffers()
def animate():
glutPostRedisplay()
global points, speed, blink, frozen
if frozen:
return
for i in range(len(points)):
points[i][0] += points[i][2] * speed
points[i][1] += points[i][3] * speed
if points[i][0] >= 200 or points[i][0] <= -200:
points[i][2] *= -1
if points[i][1] >= 150 or points[i][1] <= -150:
points[i][3] *= -1
if blink:
for i in range(len(points)):
points[i][5] = not points[i][5]
def mouseListener(button, state, x, y): #/#/x, y is the x-y of the screen (2D)
global points, speed, blink, frozen
if frozen:
return
if button == GLUT_RIGHT_BUTTON:
if (state == GLUT_DOWN):
if 30<x<470 and 100<y<400:
print(x,y)
pointx, pointy = convert_coordinate(x,y)
dirx, diry = random.choice([(1, 1), (-1, 1), (1, -1), (-1, -1)])
color = [random.random(), random.random(), random.random()]
points.append([pointx, pointy, dirx, diry, color, True])
print(points)
elif button == GLUT_LEFT_BUTTON:
if state == GLUT_DOWN:
blink = not blink
def keyboardListener(key, x, y):
global frozen
if key == b' ': # Spacebar
frozen = not frozen
def specialKeyListener(key, x, y):
global speed, frozen
if frozen:
return
if key == GLUT_KEY_UP:
speed += 0.001
elif key == GLUT_KEY_DOWN:
speed -= 0.001
def init():
#//clear the screen
glClearColor(0,0,0,0)
#//load the PROJECTION matrix
glMatrixMode(GL_PROJECTION)
#//initialize the matrix
glLoadIdentity()
#//give PERSPECTIVE parameters
gluPerspective(104, 1, 1, 1000.0)
# **(important)**aspect ratio that determines the field of view in the X direction (horizontally). The bigger this angle is, the more you can see of the world - but at the same time, the objects you can see will become smaller.
#//near distance
#//far distance
glutInit()
glutInitWindowSize(W_Width, W_Height)
glutInitWindowPosition(0, 0)
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB)
wind = glutCreateWindow(b"Task - 2")
init()
glutDisplayFunc(display)
glutIdleFunc(animate)
glutMouseFunc(mouseListener)
glutKeyboardFunc(keyboardListener)
glutSpecialFunc(specialKeyListener)
glutMainLoop()