/
render_test.py
255 lines (217 loc) · 7.53 KB
/
render_test.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import ctypes
import time
from math import sin, cos, tan, radians, pi
import glfw
import OpenGL.GL as gl
from OpenGL.GL import shaders
from OpenGL.arrays import vbo
from numpy import array
from scipy.misc import imread
VERTEXS = 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
], 'f')
CUBEPOSITIONS = (
( 0.0, 0.0, 0.0),
( 2.0, 5.0, -15.0),
(-1.5, -2.2, -2.5),
(-3.8, -2.0, -12.3),
( 2.4, -0.4, -3.5),
(-1.7, 3.0, -7.5),
( 1.3, -2.0, -2.5),
( 1.5, 2.0, -2.5),
( 1.5, 0.2, -1.5),
(-1.3, 1.0, -1.5)
)
VERTEX_SHADERSOURCE = """
#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec2 texCoord;
out vec2 TexCoord;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
gl_Position = projection*view*model*vec4(position, 1.0f);
TexCoord = vec2(texCoord.x, 1.0 - texCoord.y);
}"""
FRAGMENT_SHADERSOURCE = """
#version 330 core
in vec2 TexCoord;
out vec4 color;
uniform sampler2D ourTexture1;
uniform sampler2D ourTexture2;
void main()
{
color = mix(texture(ourTexture1, TexCoord), texture(ourTexture2, TexCoord), 0.2);
}"""
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
def init_window():
# Initialize the library
if not glfw.init():
return None
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.RESIZABLE, False)
# Create a windowed mode window and its OpenGL context
window = glfw.create_window(WINDOW_WIDTH, WINDOW_HEIGHT, "OpenGL Example", None, None)
if not window:
glfw.terminate()
return None
# Make the window's context current
glfw.make_context_current(window)
glfw.set_key_callback(window, key_cb)
return window
def get_shader():
vertex_shader = shaders.compileShader(VERTEX_SHADERSOURCE, gl.GL_VERTEX_SHADER)
fragment_shader = shaders.compileShader(FRAGMENT_SHADERSOURCE, gl.GL_FRAGMENT_SHADER)
shader = shaders.compileProgram(vertex_shader, fragment_shader)
return shader
def main():
window = init_window()
if not window:
return
shader = get_shader()
vao = get_vertex()
tex = bind_texture("wall.png")
tex2 = bind_texture("wall2.jpg")
gl.glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT)
gl.glEnable(gl.GL_DEPTH_TEST)
# Loop until the user closes the window
while not glfw.window_should_close(window):
# Poll for and process events
glfw.poll_events()
# Render here, e.g. using pyOpenGL
render(shader, vao, tex, tex2)
# Swap front and back buffers
glfw.swap_buffers(window)
time.sleep(0.05)
gl.glDeleteVertexArrays(1, vao)
glfw.terminate()
def render(shader, vao, tex, tex2):
gl.glClearColor(0.2, 0.3, 0.3, 0.4)
gl.glClear(gl.GL_COLOR_BUFFER_BIT|gl.GL_DEPTH_BUFFER_BIT)
gl.glBindTexture(gl.GL_TEXTURE_2D, tex)
gl.glUseProgram(shader)
gl.glActiveTexture(gl.GL_TEXTURE0)
gl.glBindTexture(gl.GL_TEXTURE_2D, tex)
gl.glUniform1i(gl.glGetUniformLocation(shader, "ourTexture1"), 0)
gl.glActiveTexture(gl.GL_TEXTURE1)
gl.glBindTexture(gl.GL_TEXTURE_2D, tex2)
gl.glUniform1i(gl.glGetUniformLocation(shader, "ourTexture2"), 1)
view = gl.glGetUniformLocation(shader, "view")
view_matrix = array([
[0.8, 0, 0, 0],
[0, 0.8, 0, 0],
[0, 0, 0.8, -4],
[0, 0, 0, 1],
])
gl.glUniformMatrix4fv(view, 1, gl.GL_TRUE, view_matrix)
projection = gl.glGetUniformLocation(shader, "projection")
projection_matrix = get_projection_matrix(45, WINDOW_WIDTH/WINDOW_HEIGHT, 0.1, 100)
gl.glUniformMatrix4fv(projection, 1, gl.GL_FALSE, projection_matrix)
gl.glBindVertexArray(vao)
for idx in range(0, len(CUBEPOSITIONS)):
model = gl.glGetUniformLocation(shader, "model")
angel = glfw.get_time()%360
model_matrix = get_model_matrix(idx, angel)
gl.glUniformMatrix4fv(model, 1, gl.GL_TRUE, model_matrix)
gl.glDrawArrays(gl.GL_TRIANGLES, 0, VERTEXS.size)
gl.glBindVertexArray(0)
gl.glUseProgram(0)
def get_projection_matrix(fov, aspect, zN, zF):
fov = radians(fov/2)
h = 1.0/tan(fov)
return array([[h/aspect, 0.0, 0.0, 0.0],
[0.0, h, 0.0, 0.0],
[0.0, 0.0, (zF+zN)/(zN-zF), -1.0],
[0.0, 0.0, 2.0*zF*zN/(zN-zF), 0.0]],
'f')
def get_model_matrix(index, angel):
(x, y, z) = CUBEPOSITIONS[index]
return array([
[1, 0, 0, x],
[0, cos(angel), -sin(angel), y],
[0, sin(angel), cos(angel), z],
[0, 0, 0, 1],
])
def get_vertex():
vertex_array_object = gl.glGenVertexArrays(1)
gl.glBindVertexArray(vertex_array_object)
_vbo = vbo.VBO(VERTEXS)
_vbo.bind()
gl.glVertexAttribPointer(0, 3, gl.GL_FLOAT, False,
5*ctypes.sizeof(ctypes.c_float),
ctypes.c_void_p(0))
gl.glEnableVertexAttribArray(0)
gl.glVertexAttribPointer(1, 2, gl.GL_FLOAT, False,
5*ctypes.sizeof(ctypes.c_float),
ctypes.c_void_p(3*ctypes.sizeof(ctypes.c_float)))
gl.glEnableVertexAttribArray(1)
gl.glBindVertexArray(0)
gl.glDisableVertexAttribArray(0)
gl.glDisableVertexAttribArray(1)
_vbo.unbind()
return vertex_array_object
def bind_texture(img_name):
"""
open image file and use it to generate texture
"""
tex = gl.glGenTextures(1)
gl.glBindTexture(gl.GL_TEXTURE_2D, tex)
gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_REPEAT)
gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, gl.GL_REPEAT)
gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR)
gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR)
im = imread(img_name)
(width, height, _) = im.shape
image_bytes = im.data.tobytes()
gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGB,
width, height, 0, gl.GL_RGB,
gl.GL_UNSIGNED_BYTE, image_bytes)
gl.glGenerateMipmap(gl.GL_TEXTURE_2D)
gl.glBindTexture(gl.GL_TEXTURE_2D, 0)
return tex
def key_cb(window, key, scancode, action, mode):
if key == glfw.KEY_ESCAPE and action == glfw.PRESS:
# print("quit now")
glfw.set_window_should_close(window, True)
if __name__ == "__main__":
main()