-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopengl3_modern.py
87 lines (59 loc) · 1.94 KB
/
opengl3_modern.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
import glfw
from OpenGL.GL import *
from OpenGL.GL.shaders import compileProgram, compileShader
import numpy as np
import math
import sys
VERTEX_SH = """
# version 330 core
layout(location = 0) in vec3 a_position;
layout(location = 1) in vec3 a_color;
out vec3 v_color;
void main(){
gl_Position = vec4(a_position, 1.0);
v_color = a_color;
}
"""
FRAGMENT_SH = """
# version 330 core
out vec4 out_color;
in vec3 v_color;
void main(){
out_color = vec4(v_color, 1.0);
}
"""
def window_resize(wind, width, height):
glViewport(0, 0, width, height)
if not glfw.init():
raise Exception('AAAAAA')
window = glfw.create_window(1024, 768, 'XXXXXDDDD', None, None)
if not window:
glfw.terminate()
raise Exception('AAAAA')
glfw.set_window_pos(window, 100, 100)
glfw.set_window_size_callback(window, window_resize)
glfw.make_context_current(window)
glClearColor(0, 0, 0, 0)
vertices = np.array([-0.5, -0.5, 0.0, 1.0, 0.0, 0.0,
0.5, -0.5, 0.0, 0.0, 1.0, 0.0,
-0.5, 0.5, 0.0, 0.0, 0.0, 1.0,
0.5, 0.5, 0.0, 1.0, 1.0, 1.0
# colors in one array strided
], dtype=np.float32)
shader = compileProgram(compileShader(VERTEX_SH, GL_VERTEX_SHADER), compileShader(FRAGMENT_SH, GL_FRAGMENT_SHADER))
glUseProgram(shader)
VBO = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices, GL_STATIC_DRAW)
glEnableVertexAttribArray(0) # 0 - position layout location
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(0))
# 0 - position layout location
glEnableVertexAttribArray(1) # 1 - color layout location
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(12))
while not glfw.window_should_close(window):
glfw.poll_events()
glClear(GL_COLOR_BUFFER_BIT)
ct = glfw.get_time()
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4)
glfw.swap_buffers(window)
glfw.terminate()