-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglhelper.py
More file actions
193 lines (158 loc) · 4.86 KB
/
Copy pathglhelper.py
File metadata and controls
193 lines (158 loc) · 4.86 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
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
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from PIL import Image
import os
import numpy
from asset import Asset
from window import Window
def init_glut_window(window: Window, title, enable_transparency=True):
glutInit()
glutInitDisplayMode(GLUT_RGBA)
glutInitWindowPosition(window.x, window.y)
glutInitWindowSize(window.width, window.height)
glut_window = glutCreateWindow(title)
if enable_transparency:
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
return glut_window
def destroy_glut_window(glut_window):
glutDestroyWindow(glut_window)
# Forceful exit is unfortunately needed since there is no way to leave the GLUT main loop otherwise
# ([sys.exit()] or [raise SystemExit] both result in segmentation faults)
# https://www.gamedev.net/forums/topic/376112-terminating-a-glut-loop-inside-a-program/3482380/
# https://stackoverflow.com/a/35430500
os._exit(0)
def store_asset_as_texture(image: Image.Image):
texture_id = glGenTextures(1) # number of textures to alloc
glBindTexture(GL_TEXTURE_2D, texture_id)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
glTexImage2D(
GL_TEXTURE_2D, # target
0, # level
GL_RGB, # internalformat
image.size[0], # width
image.size[1], # height
0, # border
GL_RGBA, # format
GL_UNSIGNED_BYTE, # type
numpy.array(image), # pixels
)
return texture_id
def start_main_loop(display_func, keyboard_func, special_func):
glutDisplayFunc(display_func)
glutIdleFunc(display_func)
glutKeyboardFunc(keyboard_func)
glutSpecialFunc(special_func)
glutMainLoop()
def commit_display():
glFlush()
glutSwapBuffers()
def _run_block_in_matrix_mode(mode, block_func):
glMatrixMode(mode)
block_func()
def _draw_shape(shape, block_func):
glBegin(shape)
block_func()
glEnd()
def reset_display(window: Window):
glClear(GL_COLOR_BUFFER_BIT)
_run_block_in_matrix_mode(
mode=GL_MODELVIEW,
block_func=lambda: _reset_display_in_modelview_mode(window=window),
)
_run_block_in_matrix_mode(
mode=GL_PROJECTION,
block_func=lambda: _reset_display_in_projection_mode(window=window),
)
def _reset_display_in_modelview_mode(window: Window):
glLoadIdentity()
glViewport(
window.x, # x
window.y, # y
window.width, # width
window.height, # height
)
def _reset_display_in_projection_mode(window: Window):
glLoadIdentity()
glOrtho(
window.x, # left
window.x + window.width, # right
window.y, # bottom
window.y + window.height, # top
0.0, # zNear
1.0, # zFar
)
def draw_line(color, x0, y0, x1, y1):
glColor3f(*color)
_run_block_in_matrix_mode(
mode=GL_MODELVIEW,
block_func=lambda: _draw_shape(
shape=GL_LINES,
block_func=lambda: _draw_line_inner(
x0=x0,
y0=y0,
x1=x1,
y1=y1,
),
),
)
def _draw_line_inner(x0, y0, x1, y1):
glVertex2f(x0, y0)
glVertex2f(x1, y1)
def draw_square_asset(asset: Asset, x0, y0, size, alpha):
_run_block_in_matrix_mode(
mode=GL_TEXTURE,
block_func=lambda: _scale_texture(
scale_x=1.0 / size,
scale_y=1.0 / size,
),
)
_run_block_in_matrix_mode(
mode=GL_MODELVIEW,
block_func=lambda: _draw_texture(
texture_id=asset.texture_id, x0=x0, y0=y0, size=size, alpha=alpha
),
)
_run_block_in_matrix_mode(
mode=GL_TEXTURE,
block_func=lambda: _unscale_texture(),
)
def _scale_texture(scale_x, scale_y):
glPushMatrix()
glScalef(
scale_x, # x
scale_y, # y
1, # z
)
def _unscale_texture():
glPopMatrix()
def _draw_texture(texture_id, x0, y0, size, alpha):
glColor4f(0.0, 0.0, 0.0, alpha)
glEnable(GL_TEXTURE_2D)
glEnable(GL_TEXTURE_GEN_S)
glEnable(GL_TEXTURE_GEN_T)
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR)
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR)
glBindTexture(GL_TEXTURE_2D, texture_id)
glPushMatrix()
glTranslatef(
x0, # x
y0, # y
0, # z
)
_draw_shape(
shape=GL_QUADS,
block_func=lambda: _draw_square_inner(size=size),
)
glPopMatrix()
glDisable(GL_TEXTURE_2D)
def _draw_square_inner(size):
glVertex2f(0, 0)
glVertex2f(0, size)
glVertex2f(size, size)
glVertex2f(size, 0)