-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
143 lines (104 loc) · 4.14 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
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
from OpenGL.GL import *
import glfw
import numpy as np
from utils import load_image
def create_native_window():
glfw.init()
glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, GL_TRUE)
glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
window = glfw.create_window(800, 600, 'hello triangle', None, None)
glfw.make_context_current(window)
return window
window = create_native_window()
def setup_triangle():
coords = np.array([
0, 0.5,
-0.5, -0.5,
0.5, -0.5
], dtype=np.float32)
colors = np.array([
1, 0.5, 0.5,
0.5, 1, 0.5,
0.5, 0.5, 1
], dtype=np.float32)
tex_coords = np.array([
0.5, 0,
0, 1,
1, 1
], dtype=np.float32)
itemsize = np.dtype('float32').itemsize
vao = glGenVertexArrays(1)
glBindVertexArray(vao)
vbo_id = glGenBuffers(3)
glBindBuffer(GL_ARRAY_BUFFER, vbo_id[0])
glBufferData(GL_ARRAY_BUFFER, itemsize * coords.size, coords, GL_STATIC_DRAW)
# get vertex variable location: in this case coords location
coords_attrib_location = glGetAttribLocation(program, 'coords')
# tell how data should be read
glVertexAttribPointer(coords_attrib_location, 2, GL_FLOAT, GL_FALSE, 0, None)
# link variable with currently bound buffer on GL_ARRAY_BUFFER target (target=slot)
glEnableVertexAttribArray(coords_attrib_location)
# the same for colors
glBindBuffer(GL_ARRAY_BUFFER, vbo_id[1])
glBufferData(GL_ARRAY_BUFFER, itemsize * colors.size, colors, GL_STATIC_DRAW)
colors_attrib_location = glGetAttribLocation(program, 'colors')
glVertexAttribPointer(colors_attrib_location, 3, GL_FLOAT, GL_FALSE, 0, None)
glEnableVertexAttribArray(colors_attrib_location)
# the same for texture
glBindBuffer(GL_ARRAY_BUFFER, vbo_id[2])
glBufferData(GL_ARRAY_BUFFER, itemsize * tex_coords.size, tex_coords, GL_STATIC_DRAW)
tex_coords_attrib_location = glGetAttribLocation(program, 'a_tex_coords')
glVertexAttribPointer(tex_coords_attrib_location, 2, GL_FLOAT, GL_FALSE, 0, None)
glEnableVertexAttribArray(tex_coords_attrib_location)
def load_textures():
texture_data, width, height = load_image('./assets/femalecodercat.jpg')
print(texture_data, width, height)
texture = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, texture)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, texture_data)
glBindTexture(GL_TEXTURE_2D, 0)
texture_loc = glGetUniformLocation(program, 'octo_texture')
return texture, texture_loc
def load_shaders():
with open('./shaders/vertex.glsl') as vertex_shader:
vertex_src = vertex_shader.read()
with open('./shaders/frag.glsl') as frag_shader:
frag_src = frag_shader.read()
vert_shader = glCreateShader(GL_VERTEX_SHADER)
glShaderSource(vert_shader, vertex_src)
glCompileShader(vert_shader)
frag_shader = glCreateShader(GL_FRAGMENT_SHADER)
glShaderSource(frag_shader, frag_src)
glCompileShader(frag_shader)
print(glGetShaderInfoLog(vert_shader))
print(glGetShaderInfoLog(frag_shader))
program = glCreateProgram()
glAttachShader(program, vert_shader)
glAttachShader(program, frag_shader)
glLinkProgram(program)
return program
def handle_texture():
glUniform1i(texture_loc, 0)
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, texture)
program = load_shaders()
setup_triangle()
glUseProgram(program)
texture, texture_loc = load_textures()
def draw():
glClear(GL_COLOR_BUFFER_BIT)
handle_texture()
glDrawArrays(GL_TRIANGLES, 0, 3)
def main_loop():
while not glfw.window_should_close(window):
draw()
glfw.swap_buffers(window)
glfw.poll_events()
glfw.terminate()
main_loop()