-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
316 lines (281 loc) · 10 KB
/
main.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import pygame
import sys
from OpenGL.GL import *
from OpenGL.GL.shaders import *
from pygame.locals import *
from threading import Thread
from tkinter import *
from tkinter import filedialog
from numpy import *
def datawindow():
root= Tk()
root.title('Rotate Testerv0.1')
root.geometry('252x352')
def selectx():
global xa
xtext = "X:" + str(vx.get())
labelx.config(text = xtext)
xa = float(vx.get())/100
vx = DoubleVar()
scale = Scale(root, variable = vx, from_ = -100, to = 100, orient = HORIZONTAL)
scale.pack(anchor=CENTER)
btnx = Button(root, text="X", command=selectx)
btnx.pack(anchor=CENTER)
labelx = Label(root)
labelx.pack()
def selecty():
global ya
ytext = "Y:" + str(vy.get())
labely.config(text = ytext)
ya = float(vy.get())/100
vy = DoubleVar()
scale = Scale(root, variable = vy, from_ = -100, to = 100, orient = HORIZONTAL)
scale.pack(anchor=CENTER)
btny = Button(root, text="Y", command=selecty)
btny.pack(anchor=CENTER)
labely = Label(root)
labely.pack()
def selectz():
global za
ztext = "X:" + str(vz.get())
labelz.config(text = ztext)
za = float(vz.get())/100
vz = DoubleVar()
scale = Scale(root, variable = vz, from_ = -100, to = 100, orient = HORIZONTAL)
scale.pack(anchor=CENTER)
btnz = Button(root, text="Z", command=selectz)
btnz.pack(anchor=CENTER)
labelz = Label(root)
labelz.pack()
def get_name():
global matrix_name
matrix_name = nameinput.get()
def save_matrices():
global go_to_save
get_name()
go_to_save = True
def load_matrices():
global go_to_load
go_to_load = True
nametext = Label(root,text='Input Save Name')
nametext.pack()
NAME=StringVar()
nameinput = Entry(root,width=16,textvariable=NAME)
nameinput.pack()
s = Button(root, text='save matrix', command=save_matrices)
s.pack()
s = Button(root, text='load matrix', command=load_matrices)
s.pack()
root.mainloop()
def getFileContent(file):
content = open(file, 'r').read()
return content
def init():
vertices = [-1, -1,
-1, 1,
1, 1,
1, -1]
texcoords = [0.0, 0.0,
0.0, 1.0,
1.0, 1.0,
1.0, 0.0]
pygame.init()
screen_size = (1080, 720)
window_title = "pyopengl rotate test"
pygame.display.set_mode(screen_size, HWSURFACE | OPENGL | DOUBLEBUF)
pygame.display.set_caption(window_title)
glEnable(GL_CULL_FACE)
glViewport(0, 0, screen_size[0], screen_size[1])
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
global img, img_rect, img2, img2_rect
img = pygame.image.load("images/image.png")
img_rect = img.get_rect(center = (screen_size[0]/2, screen_size[1]/2))
img2 = pygame.image.load("images/back2.png")
img2_rect = img.get_rect(center = (screen_size[0]/2, screen_size[1]/2))
global screen
screen = pygame.Surface(screen_size, SRCALPHA)
# screen.set_alpha(0)
# screen.blit(img, img_rect)
picture_exchange()
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
vertexShader = compileShader(getFileContent("shaders/sprite_vert.glsl"), GL_VERTEX_SHADER)
fragmentShader = compileShader(getFileContent("shaders/sprite_frag.glsl"), GL_FRAGMENT_SHADER)
global shaderProgram
shaderProgram = glCreateProgram()
glAttachShader(shaderProgram, vertexShader)
glAttachShader(shaderProgram, fragmentShader)
glLinkProgram(shaderProgram)
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, vertices)
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, texcoords)
glEnableVertexAttribArray(0)
glEnableVertexAttribArray(1)
global texLocation
texLocation = glGetUniformLocation(shaderProgram, "textureObj")
light_angle = glGetUniformLocation(shaderProgram, "angle")
glUseProgram(shaderProgram)
glUniform1i(texLocation, 0)
glUniform1f(light_angle, 0)
def clear(color=(0, 0, 0)):
pygame.display.flip()
glClearColor(color[0] / 255, color[1] / 255, color[2] / 255, color[3] / 255 if len(color) > 3 else 1)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
def picture_exchange():
textureData = pygame.image.tostring(screen, "RGB", True)
width = screen.get_width()
height = screen.get_height()
glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, textureData)
def render():
picture_exchange()
glDrawArrays(GL_QUADS, 0, 4)
def rotatewindow():
init()
# variables
clock = pygame.time.Clock()
fps = 240
global move,angle,xa,ya,za
move_z = False
move_xz = False
angle = 0
test = False
flip_twice = False
flip_time = 0
flip_direction = 1
global go_to_save, go_to_load
go_to_save = False
go_to_load = False
spd_collect = 0
angle_spd = 2
flip_spd = 10
move_spd = 0.1
quick_save = False
has_save = False
global save_matrix
correct_matrix = glGetDoublev(GL_MODELVIEW_MATRIX)
save_matrix = glGetDoublev(GL_MODELVIEW_MATRIX)
def save_matrix_file():
global matrix_name
save_matrix = glGetDoublev(GL_MODELVIEW_MATRIX)
print(save_matrix)
if matrix_name:
save('matrices/' + matrix_name + '.npy', save_matrix)
def load_matrix_file():
save_path = filedialog.askopenfilename()
if save_path:
save_matrix = load(save_path)
glLoadMatrixf(save_matrix)
while True:
clock.tick(fps)
for e in pygame.event.get():
if e.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif e.type == pygame.KEYDOWN:
if e.key == pygame.K_RETURN:
move = not(move)
elif e.key == pygame.K_q:
spd_collect = 0
move_z = not(move_z)
elif e.key == pygame.K_e:
spd_collect = 0
move_xz = not(move_xz)
elif e.key == pygame.K_t:
spd_collect = 0
test = not(test)
elif e.key == pygame.K_r:
spd_collect = 0
flip_time = 0
flip_direction = 1
flip_twice = not(flip_twice)
elif e.key == pygame.K_w:
glRotatef(flip_spd, 1, 0, 0)
elif e.key == pygame.K_s:
glRotatef(flip_spd, -1, 0, 0)
elif e.key == pygame.K_a:
glRotatef(flip_spd, 0, 1, 0)
elif e.key == pygame.K_d:
glRotatef(flip_spd, 0, -1, 0)
elif e.key == pygame.K_z:
glRotatef(flip_spd, 0, 0, 1)
elif e.key == pygame.K_x:
glRotatef(flip_spd, 0, 0, -1)
elif e.key == pygame.K_RIGHT:
glTranslatef(move_spd, 0, 0)
elif e.key == pygame.K_LEFT:
glTranslatef(-move_spd, 0, 0)
elif e.key == pygame.K_UP:
glTranslatef(0, move_spd, 0)
elif e.key == pygame.K_DOWN:
glTranslatef(0, -move_spd, 0)
elif e.key == pygame.K_0:
if not(quick_save):
glPushMatrix()
quick_save = True
elif e.key == pygame.K_9:
if quick_save:
glPopMatrix()
quick_save = False
elif e.key == pygame.K_2:
if not(has_save):
save_matrix = glGetDoublev(GL_MODELVIEW_MATRIX)
has_save = True
elif e.key == pygame.K_3:
if has_save:
glLoadMatrixf(save_matrix)
# has_save = False
elif e.key == pygame.K_1:
glLoadMatrixf(correct_matrix)
if go_to_save:
go_to_save = False
save_matrix_file()
elif go_to_load:
go_to_load = False
load_matrix_file()
if move:
spd_collect += angle_spd
glRotatef(angle_spd, xa, ya, za)
if spd_collect >= 360:
spd_collect = 0
move = False
if move_z:
spd_collect += angle_spd
glRotatef(angle_spd, 0, 0, 1)
if spd_collect >= 360:
spd_collect = 0
move_z = False
if move_xz:
spd_collect += angle_spd
glRotatef(angle_spd, 1, 0, 1)
if spd_collect >= 360:
spd_collect = 0
move_xz = False
if test:
spd_collect += angle_spd
glRotatef(angle_spd, 0.15, 0.5, 0.02)
if spd_collect >= 360:
spd_collect = 0
test = False
if flip_twice:
spd_collect += angle_spd
glRotatef(angle_spd, 1*flip_direction, 0, 1*flip_direction)
if spd_collect >= 360:
flip_direction *= -1
flip_time+=1
spd_collect = 0
if flip_time >=2:
flip_twice = False
clear()
glCullFace(GL_BACK)
screen.blit(img2, img_rect)
render()
glCullFace(GL_FRONT)
screen.blit(img, img_rect)
render()
if __name__ == '__main__':
move = False
angle = 0
xa=ya=za=0.0
thread1 = Thread(target=datawindow)
thread1.setDaemon(True)
thread1.start()
rotatewindow()