-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest-2-11.py
110 lines (84 loc) · 3.2 KB
/
test-2-11.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
# import standard library
import sys
# import third party library
from OpenGL.GL import *
from PySide6.QtCore import Qt
# import local library
from core.base import Base, baseApp
from core.openGLUtils import OpenGLUtils
from core.attribute import Attribute
from core.uniform import Uniform
# animate triangle moving across screen
class Test(Base):
def __init__(self, screenSize=[512, 512], title=""):
super().__init__(screenSize, title)
self.timer.stop()
def initializeGL(self):
super().initializeGL()
### initialize program ###
vsCode = """
in vec3 position;
uniform vec3 translation;
void main()
{
vec3 pos = position + translation;
gl_Position = vec4(pos.x, pos.y, pos.z, 1.0);
}
"""
fsCode = """
uniform vec3 baseColor;
out vec4 fragColor;
void main()
{
fragColor = vec4(baseColor.r, baseColor.g, baseColor.b, 1.0);
}
"""
# send code to GPU and compile; store program reference
self.programRef = OpenGLUtils.initializeProgram(vsCode, fsCode)
### render settings (optional) ###
# specify color used when clearly
glClearColor(0.0, 0.0, 0.0, 1.0)
### set up vertex array object ###
self.vaoRef = glGenVertexArrays(1)
glBindVertexArray(self.vaoRef)
### set up vertex attributes ###
self.positionData = [[0.0, 0.2, 0.0], [0.2, -0.2, 0.0],
[-0.2, -0.2, 0.0]]
self.vertexCount = len(self.positionData)
self.positionAttribute = Attribute("vec3", self.positionData)
self.positionAttribute.associateVariable(self.programRef, "position")
### set up uniforms ###
self.translation = Uniform("vec3", [-0.5, 0.0, 0.0])
self.translation.locateVariable(self.programRef, "translation")
self.baseColor = Uniform("vec3", [1.0, 0.0, 0.0])
self.baseColor.locateVariable(self.programRef, "baseColor")
# triangle speed, units per second
self.speed = 0.5
def paintGL(self):
super().paintGL()
### update data ###
# distance = self.speed * self.deltaTime
distance = 0.01
if self.input.isKeyPressed(Qt.Key_Left):
self.translation.data[0] -= distance
if self.input.isKeyPressed(Qt.Key_Right):
self.translation.data[0] += distance
if self.input.isKeyPressed(Qt.Key_Down):
self.translation.data[1] -= distance
if self.input.isKeyPressed(Qt.Key_Up):
self.translation.data[1] += distance
### render scene ###
# reset color buffer with specified color
glClear(GL_COLOR_BUFFER_BIT)
# using same program to render both shapes
glUseProgram(self.programRef)
self.translation.uploadData()
self.baseColor.uploadData()
glDrawArrays(GL_TRIANGLES, 0, self.vertexCount)
def main():
app = baseApp(sys.argv)
window = Test(title="Test-2-11")
window.show()
sys.exit(app.exec())
if __name__ == '__main__':
main()