-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcube.py
More file actions
152 lines (104 loc) · 3.64 KB
/
Copy pathcube.py
File metadata and controls
152 lines (104 loc) · 3.64 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
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
150
151
152
import OpenGL
from OpenGL.GL import *
import numpy
import glutils
import glfw
import math
import time
strVS = """
#version 330 core
layout(location = 0) in vec3 aVert;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
uniform float uTheta;
void main() {
mat4 rot = mat4(
vec4(cos(uTheta), sin(uTheta), 0.0, 0.0),
vec4(-sin(uTheta), cos(uTheta), 0.0, 0.0),
vec4(0.0, 0.0, 1.0, 0.0),
vec4(0.0, 0.0, 0.0, 1.0)
);
gl_Position = uPMatrix * uMVMatrix * rot * vec4(aVert , 1.0);
}
"""
strFS = """
#version 330 core
out vec4 fragColor;
void main(){
fragColor = vec4(0.05, 0.3, 0.8, 0.1);
}
"""
def main():
if not glfw.init():
print('glfw.init error')
return
window = glfw.create_window(640, 480, 'Cube', None, None)
if not window:
glfw.terminate()
print('glfw.create_window error')
return
glfw.make_context_current(window)
print(glGetString(GL_VERSION))
# init OpenGL
glEnable(GL_DEPTH_TEST)
glClearColor(0.5, 0.5, 0.5, 1)
# glPolygonMode( GL_FRONT_AND_BACK, GL_LINE )
# shader
program = glutils.loadShaders(strVS, strFS)
glUseProgram(program)
# vertex buffer
positions = numpy.array([ -1.0, 1.0, -1.0,
1.0, 1.0, -1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
1.0, 1.0, -1.0,
1.0, -1.0, -1.0,
1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
-1.0, -1.0, 1.0,
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
-1.0, -1.0, 1.0,
-1.0, 1.0, 1.0,
-1.0, 1.0, -1.0,
-1.0, -1.0, -1.0,
1.0, 1.0, 1.0,
1.0, -1.0, 1.0,
-1.0, -1.0, 1.0,
-1.0, 1.0, 1.0,
1.0, 1.0, -1.0,
-1.0, 1.0, -1.0,
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,], numpy.float32)
buffer = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, buffer)
glBufferData(GL_ARRAY_BUFFER, positions.itemsize * len(positions), positions, GL_STATIC_DRAW)
glEnableVertexAttribArray(0)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, positions.itemsize * 3, None)
# Projections
pMatrix = glutils.perspective(45, 640/480.0 , 0.1, 100.0)
mvMatrix = glutils.lookAt([ 3.0, 3.0, 3.0], [0.0, 0.0, 0.0], [0.0, 0.0, 1])
pMatrixUniform = glGetUniformLocation(program, b'uPMatrix')
mvMatrixUniform = glGetUniformLocation(program, b'uMVMatrix')
glUniformMatrix4fv(pMatrixUniform, 1, GL_FALSE, pMatrix)
glUniformMatrix4fv(mvMatrixUniform, 1, GL_FALSE, mvMatrix)
t = 0
while not glfw.window_should_close(window):
time.sleep(1/60)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
# glBegin(GL_TRIANGLES)
# glVertex2f(-0.5, -0.5)
# glVertex2f( 0.0 , 0.5)
# glVertex2f( 0.5, -0.5)
# glEnd()
# rotation animation
t = (t + 1) % 360
# set shader angle in radians
glUniform1f(glGetUniformLocation(program, 'uTheta'), math.radians(t))
# draw call
glDrawArrays(GL_QUADS, 0, 24)
glfw.swap_buffers(window)
glfw.poll_events()
if __name__=='__main__':
main()