-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05-hellocoord.py
executable file
·222 lines (167 loc) · 6.92 KB
/
05-hellocoord.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/Users/marc/miniconda3/bin/python3
import glfw
from OpenGL.GL import *
from OpenGL.GLU import *
import glm
import math
import ctypes
def framebuffer_size_callback(window, width, height):
# make sure the viewport matches the new window dimensions; note that width and
# height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height)
# process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
# ---------------------------------------------------------------------------------------------------------
def processInput(window):
if glfw.get_key(window, glfw.KEY_ESCAPE) == glfw.PRESS:
glfw.set_window_should_close(window, True)
width = 800
height = 600
# Initialize the library
if not glfw.init():
print("Failed to init glfw")
else:
glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, GL_TRUE)
window = glfw.create_window(width, height, "LearnOpenGL", None, None)
if not window:
print("Failed to create GLFW window")
glfw.terminate()
glfw.make_context_current(window)
glfw.set_framebuffer_size_callback(window, framebuffer_size_callback)
## Load, compile, link shaders
import myshader
shaders = myshader.shader( "shaders/hellocoord.vert", "shaders/hellocoord.frag")
shaders.linkShaders()
# ## Textures
import mytexture
t1 = mytexture.texture('resources/wall.jpg', GL_TEXTURE0)
# t2 = mytexture.texture('awesomeface.png', GL_TEXTURE1)
#
# set up vertex data (and buffer(s)) and configure vertex attributes
# ------------------------------------------------------------------
import numpy as np
vertices = np.array([
-0.5, -0.5, -0.5, 0.0, 0.0,
0.5, -0.5, -0.5, 1.0, 0.0,
0.5, 0.5, -0.5, 1.0, 1.0,
0.5, 0.5, -0.5, 1.0, 1.0,
-0.5, 0.5, -0.5, 0.0, 1.0,
-0.5, -0.5, -0.5, 0.0, 0.0,
-0.5, -0.5, 0.5, 0.0, 0.0,
0.5, -0.5, 0.5, 1.0, 0.0,
0.5, 0.5, 0.5, 1.0, 1.0,
0.5, 0.5, 0.5, 1.0, 1.0,
-0.5, 0.5, 0.5, 0.0, 1.0,
-0.5, -0.5, 0.5, 0.0, 0.0,
-0.5, 0.5, 0.5, 1.0, 0.0,
-0.5, 0.5, -0.5, 1.0, 1.0,
-0.5, -0.5, -0.5, 0.0, 1.0,
-0.5, -0.5, -0.5, 0.0, 1.0,
-0.5, -0.5, 0.5, 0.0, 0.0,
-0.5, 0.5, 0.5, 1.0, 0.0,
0.5, 0.5, 0.5, 1.0, 0.0,
0.5, 0.5, -0.5, 1.0, 1.0,
0.5, -0.5, -0.5, 0.0, 1.0,
0.5, -0.5, -0.5, 0.0, 1.0,
0.5, -0.5, 0.5, 0.0, 0.0,
0.5, 0.5, 0.5, 1.0, 0.0,
-0.5, -0.5, -0.5, 0.0, 1.0,
0.5, -0.5, -0.5, 1.0, 1.0,
0.5, -0.5, 0.5, 1.0, 0.0,
0.5, -0.5, 0.5, 1.0, 0.0,
-0.5, -0.5, 0.5, 0.0, 0.0,
-0.5, -0.5, -0.5, 0.0, 1.0,
-0.5, 0.5, -0.5, 0.0, 1.0,
0.5, 0.5, -0.5, 1.0, 1.0,
0.5, 0.5, 0.5, 1.0, 0.0,
0.5, 0.5, 0.5, 1.0, 0.0,
-0.5, 0.5, 0.5, 0.0, 0.0,
-0.5, 0.5, -0.5, 0.0, 1.0
], dtype=np.float32)
cubePositions = [
glm.vec3( 0.0, 0.0, 0.0),
glm.vec3( 2.0, 5.0, -15.0),
glm.vec3(-1.5, -2.2, -2.5),
glm.vec3(-3.8, -2.0, -12.3),
glm.vec3( 2.4, -0.4, -3.5),
glm.vec3(-1.7, 3.0, -7.5),
glm.vec3( 1.3, -2.0, -2.5),
glm.vec3( 1.5, 2.0, -2.5),
glm.vec3( 1.5, 0.2, -1.5),
glm.vec3(-1.3, 1.0, -1.5)
]
# bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
VAO = glGenVertexArrays(1)
glBindVertexArray(VAO)
VBO = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW)
# glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices_buffer, GL_STATIC_DRAW)
# d = glGetBufferSubData( GL_ELEMENT_ARRAY_BUFFER, 0, 6 * 4)
# print(d)
# d = glGetBufferSubData( GL_ARRAY_BUFFER, 0, 12 * 4)
# print(d)
## position of the attrib array, must match the shader
location = 0
glVertexAttribPointer(location, 3, GL_FLOAT, GL_FALSE, 5*4, None) #3 * 4, 0)
glEnableVertexAttribArray(location)
## position of the attrib array, must match the shader
location = 1
glVertexAttribPointer(location, 3, GL_FLOAT, GL_FALSE, 5*4, ctypes.c_void_p(3*4)) #3 * 4, 0)
glEnableVertexAttribArray(location)
# note that this is allowed, the call to glVertexAttribPointer registered VBO as the
# vertex attribute's bound vertex buffer object so afterwards we can safely unbind
# glBindBuffer(GL_ARRAY_BUFFER, 0)
# remember: do NOT unbind the EBO while a VAO is active as the bound element buffer object
# IS stored in the VAO; keep the EBO bound.
# NO glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
# You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO,
# but this rarely happens. Modifying other VAOs requires a call to glBindVertexArray anyways
# so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary.
glBindVertexArray(0)
# uncomment this call to draw in wireframe polygons.
# glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
# render loop
# -----------
glClearColor(0.9, 0.7, 0.7, 1.0)
shaders.use()
shaders.setUniform1i("ourTexture", 0); # set Textures
# no need to bind it every time, but we'll do so to keep things a bit more organized
glBindVertexArray(VAO) # seeing as we only have a single VAO there's
# note that we're translating the scene in the reverse direction of where we want to move
view = glm.mat4(1.0)
view = glm.translate(view, glm.vec3(0.0, 0.0, -3.0))
glEnable(GL_DEPTH_TEST)
while not glfw.window_should_close(window):
# input
processInput(window)
timeValue = glfw.get_time()*1.0
projection = glm.perspective( glm.radians(45.0), 800.0 / 600.0, 0.1, 100.0)
# glm.ortho(0.0, 800.0, 0.0, 600.0, 0.1, 100.0)
shaders.setUniformMatrix4fv("view", glm.value_ptr( view ))
shaders.setUniformMatrix4fv("projection", glm.value_ptr( projection ))
# render
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
# glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, None)
for i in range(10):
model = glm.mat4(1.0)
model = glm.translate(model, cubePositions[i]);
# model = glm.rotate(model, timeValue * glm.radians(-55.0), glm.vec3(1.0, 0.5, 0.0))
angle = 20.0 * i
model = glm.rotate(model, glm.radians(angle), glm.vec3(1.0, 0.3, 0.5));
shaders.setUniformMatrix4fv("model", glm.value_ptr( model ))
glDrawArrays(GL_TRIANGLES, 0, 36)
# glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
# -------------------------------------------------------------------------------
glfw.swap_buffers(window)
glfw.poll_events()
glBindVertexArray(0) # no need to unbind it every time
# optional: de-allocate all resources once they've outlived their purpose:
# ------------------------------------------------------------------------
glDeleteVertexArrays(1, [VAO])
glDeleteBuffers(1, [VBO])
# glfw: terminate, clearing all previously allocated GLFW resources.
# ------------------------------------------------------------------
glfw.terminate()