-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.py
More file actions
128 lines (99 loc) · 2.84 KB
/
window.py
File metadata and controls
128 lines (99 loc) · 2.84 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
import OpenGL
# OpenGL.ERROR_CHECKING = False
from OpenGL.GL import *
import glfw
from OpenGL.GLU import *
class Window ():
def __init__(self, width, height, initFrame=None):
'''
Handles the window element.
Inputs:
- width (int)
- height (int)
- initFrame (bytearray)
'''
self.displayMode = GL_RGB
self.width = width
self.height = height
self.currFrame = initFrame
if not glfw.init():
raise Exception("GLFW could not be instantiated")
self.window = glfw.create_window(self.width, self.height, "Video Player", None, None)
if not self.window:
glfw.terminate()
raise Exception ("Window could not be created")
glfw.make_context_current(self.window)
def createTexture(self):
# I have no clue what I'm doing help
texture_id = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, texture_id)
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
glPixelStorei(GL_PACK_ALIGNMENT, 1)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
glTexImage2D(GL_TEXTURE_2D, 0, self.displayMode, self.width, self.height, 0, self.displayMode, GL_UNSIGNED_BYTE, self.currFrame)
return texture_id
def createFrame(self):
'''Bind texture to quad'''
glEnable(GL_TEXTURE_2D)
texture_id = self.createTexture()
glBindTexture(GL_TEXTURE_2D, texture_id)
glBegin(GL_QUADS)
glTexCoord2f(0,0)
glVertex2f(0,0)
glTexCoord2f(0, 500)
glVertex2f(0, 500)
glTexCoord2f(500, 500)
glVertex2f(500, 500)
glTexCoord2f(500, 0)
glVertex2f(500,0)
glEnd()
glDisable(GL_TEXTURE_2D)
def setFrame(self):
'''Define border for textures'''
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluOrtho2D(0, 500, 0, 500)
glMatrixMode(GL_TEXTURE)
glLoadIdentity()
gluOrtho2D(0, 1000, 0, 1000)
glMatrixMode (GL_MODELVIEW)
glLoadIdentity()
def renderScreen(self):
if (not self.currFrame):
raise ValueError("Frame is empty")
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
self.setFrame()
self.createFrame()
glfw.swap_buffers(self.window)
if __name__ == "__main__":
# Testing
import colorsys
import random
import time
offset = 0
def createFrame():
frame = bytearray(600*500*3)
for i in range(0, 600*500*3, 3):
col = (i//3) % 600 + offset
row = (i//3) // 600
r,g,b = colorsys.hsv_to_rgb(col/600, (row/500) % 1, 1)
# print (g * 255)
frame[i] = int(r*255)
frame[i+1] = int(g*255)
frame[i+2] = int(b*255)
return frame
win = Window(600, 500)
frame = createFrame()
offset += 10
frame2 = createFrame()
lastFrameTime = time.time()
while not glfw.window_should_close(win.window):
win.currFrame = createFrame()
win.renderScreen()
glfw.poll_events()
offset += 10
print ("Frame time: " + str(time.time() - lastFrameTime))
lastFrameTime = time.time()
glfw.terminate()