-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathhello_triangle_glfw_begin_end.py
68 lines (59 loc) · 2.34 KB
/
hello_triangle_glfw_begin_end.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
from OpenGL.GL import *
from OpenGL.GLU import *
from glfw.GLFW import *
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))
]
if glfwInit() == GLFW_FALSE:
raise Exception("error: init glfw")
glfwWindowHint(GLFW_SAMPLES, 8)
window = glfwCreateWindow(640, 480, "OpenGL Window", None, None)
glfwMakeContextCurrent(window)
def size_callback(window, _w, _h):
width, height = glfwGetFramebufferSize(window)
glViewport(0, 0, width, height)
aspect = width / height
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
if aspect >= 1:
glOrtho(-aspect, aspect, -1, 1, -1, 1)
else:
glOrtho(-1, 1, -1/aspect, 1/aspect, -1, 1)
glMatrixMode(GL_MODELVIEW)
glfwSetWindowSizeCallback(window, size_callback)
size_callback(window, 640, 480)
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")
@GLDEBUGPROC
def __CB_OpenGL_DebugMessage(source, type, id, severity, length, message, userParam):
msg = message[0:length]
print(msg.decode("utf-8"))
glDebugMessageCallback(__CB_OpenGL_DebugMessage, None)
errors_only = False
if errors_only:
glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, None, GL_FALSE)
glDebugMessageControl(GL_DEBUG_SOURCE_API, GL_DEBUG_TYPE_ERROR, GL_DONT_CARE, 0, None, GL_TRUE)
else:
glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, None, GL_TRUE)
glEnable(GL_DEBUG_OUTPUT)
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS)
glEnable(GL_MULTISAMPLE) # default
glEnable(GL_DEPTH_TEST)
glClearColor(0.0, 0.0, 0.0, 0.0)
while not glfwWindowShouldClose(window):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glBegin(GL_TRIANGLES)
for vertex, color in attributes:
glColor4f(*color)
glVertex3f(*vertex)
glEnd()
glfwSwapBuffers(window)
glfwPollEvents()
glfwTerminate()
exit()