-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
95 lines (70 loc) · 3.1 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
from __future__ import division
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GL import shaders
from sys import exit as exitsystem
from numpy import array
def ReadFile(filename):
data = ""
with open(filename, 'r') as f:
data = f.read()
return data
VERTEX_SHADER = ReadFile("./vertexShader.glsl")
FRAGMENT_SHADER = ReadFile("./fragmentShader.glsl")
class Main(object):
def __init__(self):
pygame.init()
self.resolution = 1920, 1080
pygame.display.set_mode(self.resolution, DOUBLEBUF | OPENGL)
pygame.display.set_caption('Voronoi diagram')
# setting my Shaders
self.vertex_shader = shaders.compileShader(VERTEX_SHADER, GL_VERTEX_SHADER)
self.fragment_shader = shaders.compileShader(FRAGMENT_SHADER, GL_FRAGMENT_SHADER)
self.shader = shaders.compileProgram(self.vertex_shader, self.fragment_shader)
# Get the uniform locations
self.uni_mouse = glGetUniformLocation(self.shader, 'iMouse')
self.uni_ticks = glGetUniformLocation(self.shader, 'iTime')
glUseProgram(self.shader) # Need to be enabled before sending uniform variables
# Resolution doesn't change. Send it once
glUniform2f(glGetUniformLocation(self.shader, 'iResolution'), *self.resolution)
#to create a quad
self.vertices = array([-1.0, -1.0, 0.0,
1.0, -1.0, 0.0,
1.0, 1.0, 0.0,
-1.0, 1.0, 0.0], dtype='float32')
#generate VBO and VAO
self.vao = glGenVertexArrays(1)
glBindVertexArray(self.vao)
self.vbo = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
glBufferData(GL_ARRAY_BUFFER, self.vertices, GL_STATIC_DRAW)
glEnableVertexAttribArray(0)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None)
self.clock = pygame.time.Clock()
def mainloop(self):
while 1:
delta = self.clock.tick(8192)
glClearColor(0.0, 0.0, 0.0, 1.0)
glClear(GL_COLOR_BUFFER_BIT)
for event in pygame.event.get():
if (event.type == QUIT) or (event.type == KEYUP and event.key == K_ESCAPE):
pygame.quit()
exitsystem()
elif event.type == MOUSEBUTTONDOWN:
if event.button == 4:
pass
glUseProgram(self.shader)
# Map mouse coordinates between -1 and 1 range
mx, my = pygame.mouse.get_pos()
mx = (1.0 / self.resolution[0] * mx) * 2.0 - 1.0
my = (1.0 / self.resolution[1] * my) * 2.0 - 1.0
glUniform2f(self.uni_mouse, mx, my)
glUniform1f(self.uni_ticks, pygame.time.get_ticks() / 1000.0)
# Bind the vao (which stores the VBO with all the vertices)
glBindVertexArray(self.vao)
glDrawArrays(GL_QUADS, 0, 4)
pygame.display.set_caption("FPS: {}".format(self.clock.get_fps()))
pygame.display.flip()
if __name__ == '__main__':
Main().mainloop()