-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab1-2-solution.py
More file actions
95 lines (66 loc) · 1.96 KB
/
lab1-2-solution.py
File metadata and controls
95 lines (66 loc) · 1.96 KB
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
import pygame
from OpenGL.GL import *
def initializeShader(shaderCode, shaderType):
shaderCode = '#version 330\n' + shaderCode
ref = glCreateShader(shaderType)
glShaderSource(ref, shaderCode)
glCompileShader(ref)
success = glGetShaderiv(ref, GL_COMPILE_STATUS)
if not success:
message = glGetShaderInfoLog(ref)
glDeleteShader(ref)
message = '\n' + message.decode('utf-8')
raise Exception(message)
return ref
def initializeProgram(vsCode, fsCode):
vsRef = initializeShader(vsCode, GL_VERTEX_SHADER)
fsRef = initializeShader(fsCode, GL_FRAGMENT_SHADER)
ref = glCreateProgram()
glAttachShader(ref, vsRef)
glAttachShader(ref, fsRef)
glLinkProgram(ref)
success = glGetProgramiv(ref, GL_LINK_STATUS)
if not success:
message = glGetProgramInfoLog(ref)
glDeleteProgram(ref)
message = '\n' + message.decode('utf-8')
raise Exception(message)
return ref
vsCode = """
void main()
{
gl_Position = vec4(0.0, 0.0, 0.0, 1.0);
}
"""
fsCode = """
out vec4 fragColor;
void main()
{
fragColor = vec4(1.0, 1.0, 0.0, 0.5);
}
"""
def main():
# init pygame modules
pygame.init()
# use a core ogl profile for cross-platform compatibility
pygame.display.gl_set_attribute(
pygame.GL_CONTEXT_PROFILE_MASK, pygame.GL_CONTEXT_PROFILE_CORE)
# create and display the window
pygame.display.set_mode(
[512, 512], pygame.DOUBLEBUF | pygame.OPENGL, vsync=1)
pygame.display.set_caption("2DT904 - Setting the stage")
program = initializeProgram(vsCode, fsCode)
vao = glGenVertexArrays(1)
glBindVertexArray(vao)
glPointSize(10)
glUseProgram(program)
running = True
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
glDrawArrays(GL_POINTS, 0, 1)
pygame.display.flip()
pygame.quit()
main()