-
Notifications
You must be signed in to change notification settings - Fork 0
/
rit_window.py
117 lines (100 loc) · 4.03 KB
/
rit_window.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
import numpy as np
import signal
from cgI_engine import *
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
class RitWindow:
def __init__(self, width, height, title="CSCI 610"):
# window-related info
self.width = width
self.height = height
self.title = title
self.framebuffer = np.array([0 for i in range(self.width*self.height*4)])
def run (self, e):
self.myEngine = e
glutInit()
glutInitDisplayMode(GLUT_RGBA)
glutInitWindowSize(self.width, self.height)
glutInitWindowPosition(0, 0)
self.winid = glutCreateWindow(self.title)
glutDisplayFunc(e.go)
glutKeyboardUpFunc (self._keypress)
self.t_id = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, self.t_id)
glPixelStorei(GL_UNPACK_ALIGNMENT,1)
glTexImage2D(
GL_TEXTURE_2D, 0, 3, self.width, self.height, 0,
GL_RGBA, GL_UNSIGNED_INT, self.framebuffer
)
glutMainLoop()
def clearFB (self, r, g, b):
for x in range(self.width):
for y in range(self.height):
self.set_pixel(x, y, r, g, b)
def set_pixel(self, x, y, r, g, b):
# sets the pixel at (x,y) to (r, g, b, 255), with 8-bit integer colors
yy = self.height - y - 1
self.framebuffer[x*4 + 0 + yy*self.width*4] = r # r
self.framebuffer[x*4 + 1 + yy*self.width*4] = g # g
self.framebuffer[x*4 + 2 + yy*self.width*4] = b # b
self.framebuffer[x*4 + 3 + yy*self.width*4] = 255 # a
def applyFB(self):
glEnable(GL_TEXTURE_2D)
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, 3, self.width, self.height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, self.framebuffer
)
glBindTexture(GL_TEXTURE_2D, self.t_id)
self._render()
#
# some private stuff you should be concerned with
#
def _fullscreen_quad(self):
# the quad!
glBegin(GL_QUADS)
glTexCoord2f(0.0, 0.0)
glVertex2f(0, 0)
glTexCoord2f(1.0, 0.0)
glVertex2f(self.width, 0)
glTexCoord2f(1.0, 1.0)
glVertex2f(self.width, self.height)
glTexCoord2f(0.0, 1.0)
glVertex2f(0, self.height)
glEnd()
def _render(self):
# internal function that actually draws the scene
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glDisable(GL_LIGHTING)
glViewport(0, 0, self.width, self.height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0.0, self.width, self.height, 0.0, 0.0, 1.0)
glMatrixMode (GL_MODELVIEW)
glLoadIdentity()
glColor3f(1.0, 1.0, 0.0)
glEnable(GL_TEXTURE_2D)
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)
glBindTexture(GL_TEXTURE_2D, self.t_id)
self._fullscreen_quad()
glutSwapBuffers()
def _keypress (self, key, x, y):
keychar = key.decode('UTF-8')
self.myEngine.keyboard (keychar)
if (keychar == 'q') :
os.kill(os.getpid(), signal.SIGTERM)
def drawRect (self, top, bottom, right, left, r, g, b):
#top and bottom edges
for i in range (left, right):
self.set_pixel (i, top, r, g, b)
self.set_pixel (i, bottom, r, g, b)
# left and right
for j in range (bottom, top):
self.set_pixel (left, j, r, g, b)
self.set_pixel (right, j, r, g, b)