-
Notifications
You must be signed in to change notification settings - Fork 1
/
test-3.py
149 lines (124 loc) · 5.28 KB
/
test-3.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# import standard library
import sys
from math import pi
# import third party library
from OpenGL import GL as gl
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
from core.matrix import Matrix
# move a triangle around the screen
class Test(Base):
def __init__(self, screenSize=[512, 512], title=""):
super().__init__(screenSize, title)
def initializeGL(self):
super().initializeGL()
### initialize program ###
vsCode = """
in vec3 position;
uniform mat4 projectionMatrix;
uniform mat4 modelMatrix;
void main()
{
gl_Position = projectionMatrix * modelMatrix * vec4(position, 1.0);
}
"""
fsCode = """
out vec4 fragColor;
void main()
{
fragColor = vec4(1.0, 1.0, 0.0, 1.0);
}
"""
self.programRef = OpenGLUtils.initializeProgram(vsCode, fsCode)
### Render settings ###
gl.glClearColor(0.0, 0.0, 0.0, 1.0)
gl.glEnable(gl.GL_DEPTH_TEST)
### Set up vertex array object ###
self.vaoRef = gl.glGenVertexArrays(1)
gl.glBindVertexArray(self.vaoRef)
### Set up vertex attribute: three points of triangle ###
self.positionData = [[0.0, 0.2, 0.0], [
0.1, -0.2, 0.0], [-0.1, -0.2, 0.0]]
self.vertexCount = len(self.positionData)
self.positionAttribute = Attribute("vec3", self.positionData)
self.positionAttribute.associateVariable(self.programRef, "position")
### set up uniforms ###
mMatrix = Matrix.makeTranslation(0, 0, -1)
self.modelMatrix = Uniform("mat4", mMatrix)
self.modelMatrix.locateVariable(self.programRef, "modelMatrix")
pMatrix = Matrix.makeIdentity()
self.projectionMatrix = Uniform("mat4", pMatrix)
self.projectionMatrix.locateVariable(
self.programRef, "projectionMatrix")
# movement speed, units per second
self.moveSpeed = 0.5
# rotation speed, radians per second
self.turnSpeed = 90 * (pi / 180)
def paintGL(self):
super().paintGL()
# update data
moveAmount = self.moveSpeed * self.deltaTime
turnAmount = self.turnSpeed * self.deltaTime
#global translations
if self.input.isKeyPressed(Qt.Key_W):
m = Matrix.makeTranslation(0, moveAmount, 0)
self.modelMatrix.data = m @ self.modelMatrix.data
if self.input.isKeyPressed(Qt.Key_S):
m = Matrix.makeTranslation(0, -moveAmount, 0)
self.modelMatrix.data = m @ self.modelMatrix.data
if self.input.isKeyPressed(Qt.Key_A):
m = Matrix.makeTranslation(-moveAmount, 0, 0)
self.modelMatrix.data = m @ self.modelMatrix.data
if self.input.isKeyPressed(Qt.Key_D):
m = Matrix.makeTranslation(moveAmount, 0, 0)
self.modelMatrix.data = m @ self.modelMatrix.data
if self.input.isKeyPressed(Qt.Key_Z):
m = Matrix.makeTranslation(0, 0, moveAmount)
self.modelMatrix.data = m @ self.modelMatrix.data
if self.input.isKeyPressed(Qt.Key_X):
m = Matrix.makeTranslation(0, 0, -moveAmount)
self.modelMatrix.data = m @ self.modelMatrix.data
#global rotation
if self.input.isKeyPressed(Qt.Key_Q):
m = Matrix.makeRotationZ(turnAmount)
self.modelMatrix.data = m @ self.modelMatrix.data
if self.input.isKeyPressed(Qt.Key_E):
m = Matrix.makeRotationZ(-turnAmount)
self.modelMatrix.data = m @ self.modelMatrix.data
# local translation
if self.input.isKeyPressed(Qt.Key_I):
m = Matrix.makeTranslation(0, moveAmount, 0)
self.modelMatrix.data = self.modelMatrix.data @ m
if self.input.isKeyPressed(Qt.Key_K):
m = Matrix.makeTranslation(0, -moveAmount, 0)
self.modelMatrix.data = self.modelMatrix.data @ m
if self.input.isKeyPressed(Qt.Key_J):
m = Matrix.makeTranslation(-moveAmount, 0, 0)
self.modelMatrix.data = self.modelMatrix.data @ m
if self.input.isKeyPressed(Qt.Key_L):
m = Matrix.makeTranslation(moveAmount, 0, 0)
self.modelMatrix.data = self.modelMatrix.data @ m
# local rotation
if self.input.isKeyPressed(Qt.Key_U):
m = Matrix.makeRotationZ(turnAmount)
self.modelMatrix.data = self.modelMatrix.data @ m
if self.input.isKeyPressed(Qt.Key_O):
m = Matrix.makeRotationZ(-turnAmount)
self.modelMatrix.data = self.modelMatrix.data @ m
### render scene ###
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
gl.glUseProgram(self.programRef)
self.projectionMatrix.uploadData()
self.modelMatrix.uploadData()
gl.glDrawArrays(gl.GL_TRIANGLES, 0, self.vertexCount)
def main():
app = baseApp(sys.argv)
window = Test(title="Test-3")
window.show()
sys.exit(app.exec())
if __name__ == '__main__':
main()