-
Notifications
You must be signed in to change notification settings - Fork 18
/
hello_triangle_pygame_opengl_4_1_core.py
107 lines (91 loc) · 3.49 KB
/
hello_triangle_pygame_opengl_4_1_core.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
from OpenGL.GL import *
from ctypes import c_void_p, sizeof
import glm
import pygame
sh_vert = """
#version 410 core
layout (location = 0) in vec4 a_position;
layout (location = 1) in vec4 a_color;
out vec4 v_color;
uniform mat4 u_projection;
void main()
{
v_color = a_color;
gl_Position = u_projection * a_position;
}
"""
sh_frag = """
#version 410 core
out vec4 frag_color;
in vec4 v_color;
void main()
{
frag_color = v_color;
}
"""
pygame.init()
pygame.display.gl_set_attribute(pygame.GL_CONTEXT_MAJOR_VERSION, 4)
pygame.display.gl_set_attribute(pygame.GL_CONTEXT_MINOR_VERSION, 1)
pygame.display.gl_set_attribute(pygame.GL_CONTEXT_PROFILE_MASK, pygame.GL_CONTEXT_PROFILE_CORE)
pygame.display.gl_set_attribute(pygame.GL_CONTEXT_FLAGS, 1)
pygame.display.gl_set_attribute(pygame.GL_MULTISAMPLEBUFFERS, 1)
pygame.display.gl_set_attribute(pygame.GL_MULTISAMPLESAMPLES, 8)
window = pygame.display.set_mode((640, 480), pygame.DOUBLEBUF | pygame.OPENGL | pygame.RESIZABLE)
clock = pygame.time.Clock()
vendor, renderer = glGetString(GL_VENDOR).decode("utf-8"), glGetString(GL_RENDERER).decode("utf-8")
version, glsl_version = glGetString(GL_VERSION).decode("utf-8"), glGetString(GL_SHADING_LANGUAGE_VERSION).decode("utf-8")
major, minor = glGetInteger(GL_MAJOR_VERSION), glGetInteger(GL_MINOR_VERSION)
extensions = [glGetStringi(GL_EXTENSIONS, i) for i in range(glGetInteger(GL_NUM_EXTENSIONS))]
print(f"\n{vendor} / {renderer}\n OpenGL: {version}\n GLSL: {glsl_version}\n Context {major}.{minor}\n")
attributes = [
# x y z R G B A
-0.866, -0.75, 0, 1, 0, 0, 1,
0.866, -0.75, 0, 1, 1, 0, 1,
0, 0.75, 0, 0, 0, 1, 1
]
attributes = (GLfloat * len(attributes))(*attributes)
vao = glGenVertexArrays(1)
vbo = glGenBuffers(1)
glBindVertexArray(vao)
glBindBuffer(GL_ARRAY_BUFFER, vbo)
glBufferData(GL_ARRAY_BUFFER, attributes, GL_STATIC_DRAW)
glVertexAttribPointer(0, 3, GL_FLOAT, False, 7 * sizeof(GLfloat), None)
glVertexAttribPointer(1, 4, GL_FLOAT, False, 7 * sizeof(GLfloat), c_void_p(3 * sizeof(GLfloat)))
glEnableVertexAttribArray(0)
glEnableVertexAttribArray(1)
program_obj = glCreateProgram()
for sh_type, sh_code in [(GL_VERTEX_SHADER, sh_vert), (GL_FRAGMENT_SHADER, sh_frag)]:
shader_obj = glCreateShader(sh_type)
glShaderSource(shader_obj, sh_code)
glCompileShader(shader_obj)
if not glGetShaderiv(shader_obj, GL_COMPILE_STATUS):
raise Exception(glGetShaderInfoLog(shader_obj).replace(b'\\n', b'\n'))
glAttachShader(program_obj, shader_obj)
glLinkProgram(program_obj)
if not glGetProgramiv(program_obj, GL_LINK_STATUS):
raise Exception(glGetProgramInfoLog(program_obj).replace(b'\\n', b'\n'))
projection_location = glGetUniformLocation(program_obj, "u_projection")
glUseProgram(program_obj)
glEnable(GL_MULTISAMPLE) # default
glEnable(GL_DEPTH_TEST)
glClearColor(0.0, 0.0, 0.0, 0.0)
aspect = 640 / 480
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.VIDEORESIZE:
glViewport(0, 0, event.w, event.h)
aspect = event.w / event.h
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
if aspect >= 1:
projection = glm.ortho(-aspect, aspect, -1, 1, -1, 1)
else:
projection = glm.ortho(-1, 1, -1/aspect, 1/aspect, -1, 1)
glUniformMatrix4fv(projection_location, 1, GL_FALSE, glm.value_ptr(projection))
glDrawArrays(GL_TRIANGLES, 0, 3)
pygame.display.flip()
pygame.quit()
exit()