-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_screen.py
120 lines (96 loc) · 3.3 KB
/
config_screen.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
# File containing functions that configure the screen that will be used to show our project.
import glfw
from OpenGL.GL import *
HEIGHT = 700
WIDTH = 700
WINDOW_NAME = 'Project 1'
# Shaders' code.
vertex_code = """
attribute vec3 position;
uniform mat4 mat_transformation;
void main(){
gl_Position = mat_transformation * vec4(position,1.0);
}
"""
fragment_code = """
uniform vec4 color;
void main(){
gl_FragColor = color;
}
"""
def init_window():
'''
Instantiates a GLFW window.
'''
glfw.init()
glfw.window_hint(glfw.VISIBLE, glfw.FALSE)
window = glfw.create_window(WIDTH, HEIGHT, WINDOW_NAME, None, None)
glfw.make_context_current(window)
glfw.show_window(window)
return window
def create_program():
'''
Requests GPU slots for the program and shaders to then compile and attach
these shaders to this program slot.
'''
# Request a program and shader slots from GPU.
program = glCreateProgram()
vertex = glCreateShader(GL_VERTEX_SHADER)
fragment = glCreateShader(GL_FRAGMENT_SHADER)
# Set shaders source.
glShaderSource(vertex, vertex_code)
glShaderSource(fragment, fragment_code)
# Compile shaders.
glCompileShader(vertex)
if not glGetShaderiv(vertex, GL_COMPILE_STATUS):
error = glGetShaderInfoLog(vertex).decode()
print(error)
raise RuntimeError("Error compiling Vertex Shader.")
glCompileShader(fragment)
if not glGetShaderiv(fragment, GL_COMPILE_STATUS):
error = glGetShaderInfoLog(fragment).decode()
print(error)
raise RuntimeError("Error compiling Fragment Shader.")
# Attach shader objects to the program.
glAttachShader(program, vertex)
glAttachShader(program, fragment)
# Build program.
glLinkProgram(program)
if not glGetProgramiv(program, GL_LINK_STATUS):
print(glGetProgramInfoLog(program))
raise RuntimeError('Error linking the program.')
# Make program the default program.
glUseProgram(program)
return program
def send_data_to_gpu(program, vertexes):
'''
Requests GPU slots to program data and then sends this data to this slot.
'''
# Request a buffer slot from GPU.
buffer = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, buffer)
# Localize the GPU variable (the one we defined in vertex shader) that represents each vertex.
loc_vertex = glGetAttribLocation(program, "position")
glEnableVertexAttribArray(loc_vertex)
# Sending vertexes data to this GPU variable.
glBufferData(GL_ARRAY_BUFFER, vertexes.nbytes, vertexes, GL_DYNAMIC_DRAW)
glBindBuffer(GL_ARRAY_BUFFER, buffer)
stride = vertexes.strides[0]
offset = ctypes.c_void_p(0)
glVertexAttribPointer(loc_vertex, 3, GL_FLOAT, False, stride, offset)
def render_window(window):
'''
Render a already created window.
'''
glfw.show_window(window)
glEnable(GL_DEPTH_TEST)
def get_loc_color(program):
'''
Returns the color variable localized in the GPU.
'''
return glGetUniformLocation(program, "color")
def get_loc_transformation(program):
'''
Returns the transformation matrix variable localized in the GPU.
'''
return glGetUniformLocation(program, "mat_transformation")