-
Notifications
You must be signed in to change notification settings - Fork 1
/
22_Lightning mboh.py
254 lines (216 loc) · 8.78 KB
/
22_Lightning mboh.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 os
import math
import pyrr
import pygame
import numpy as np
from OpenGL.GL import *
from libraries.Texture_Loader import load_texture_pygame
from OpenGL.GL.shaders import compileProgram, compileShader
# ==========================================
# Vertex and fragment shader
# ==========================================
vertex_src = """
# version 330
layout(location = 0) in vec3 position_in;
layout(location = 1) in vec2 texture_in;
layout(location = 2) in vec3 color_in;
uniform mat4 model;
uniform mat4 projection;
uniform mat4 view;
out vec3 color_out;
out vec2 texture_out;
void main()
{
gl_Position = projection * view * model * vec4(position_in, 1.0);
texture_out = texture_in;
color_out = color_in;
}
"""
fragment_src = """
# version 330
in vec2 texture_out;
in vec3 color_out;
uniform sampler2D texture_sampler;
uniform int switcher;
out vec4 color;
void main()
{
if(switcher == 0){
color = texture(texture_sampler, texture_out);
}
else{
color = vec4(color_out, 1.0);
}
}
"""
# =============================================
# Define vertices color and index vertex array
# =============================================
# Cube Object
cube_vertices = [-0.5, -0.5, 0.5, 0.0, 0.0, 1, 0, 0,
0.5, -0.5, 0.5, 1.0, 0.0, 1, 0, 0,
0.5, 0.5, 0.5, 1.0, 1.0, 1, 0, 0,
-0.5, 0.5, 0.5, 0.0, 1.0, 1, 0, 0,
-0.5, -0.5, -0.5, 0.0, 0.0, 1, 0, 0,
0.5, -0.5, -0.5, 1.0, 0.0, 1, 0, 0,
0.5, 0.5, -0.5, 1.0, 1.0, 1, 0, 0,
-0.5, 0.5, -0.5, 0.0, 1.0, 1, 0, 0,
0.5, -0.5, -0.5, 0.0, 0.0, 1, 0, 0,
0.5, 0.5, -0.5, 1.0, 0.0, 1, 0, 0,
0.5, 0.5, 0.5, 1.0, 1.0, 1, 0, 0,
0.5, -0.5, 0.5, 0.0, 1.0, 1, 0, 0,
-0.5, -0.5, -0.5, 1.0, 0.0, 1, 0, 0,
-0.5, 0.5, -0.5, 0.0, 0.0, 1, 0, 0,
-0.5, 0.5, 0.5, 0.0, 1.0, 1, 0, 0,
-0.5, -0.5, 0.5, 1.0, 1.0, 1, 0, 0,
-0.5, -0.5, -0.5, 0.0, 0.0, 1, 0, 0,
0.5, -0.5, -0.5, 1.0, 0.0, 1, 0, 0,
0.5, -0.5, 0.5, 1.0, 1.0, 1, 0, 0,
-0.5, -0.5, 0.5, 0.0, 1.0, 1, 0, 0,
-0.5, 0.5, -0.5, 1.0, 0.0, 1, 0, 0,
0.5, 0.5, -0.5, 0.0, 0.0, 1, 0, 0,
0.5, 0.5, 0.5, 0.0, 1.0, 1, 0, 0,
-0.5, 0.5, 0.5, 1.0, 1.0, 1, 0, 0]
cube_vertices = np.array(cube_vertices, dtype=np.float32)
cube_indices = [0, 1, 2, 2, 3, 0,
4, 5, 6, 6, 7, 4,
8, 9, 10, 10, 11, 8,
12, 13, 14, 14, 15, 12,
16, 17, 18, 18, 19, 16,
20, 21, 22, 22, 23, 20]
cube_indices = np.array(cube_indices, dtype=np.uint32)
# Quad Object
quad_vertices = [-0.5, -0.5, 0, 0.0, 0.0,
0.5, -0.5, 0, 1.0, 0.0,
0.5, 0.5, 0, 1.0, 1.0,
-0.5, 0.5, 0, 0.0, 1.0]
quad_vertices = np.array(quad_vertices, dtype=np.float32)
quad_indices = [0, 1, 2, 2, 3, 0]
quad_indices = np.array(quad_indices, dtype=np.uint32)
# Triangle Object
triangle_vertices = [-0.5, -0.5, 0, 1, 0, 0,
0.5, -0.5, 0, 0, 1, 0,
0.0, 0.5, 0, 0, 0, 1]
triangle_vertices = np.array(triangle_vertices, dtype=np.float32)
# Window init
pygame.init()
pygame.display.set_mode((640, 480), pygame.OPENGL |
pygame.DOUBLEBUF | pygame.RESIZABLE)
os.environ['SDL_VIDEO_WINDOW_POS'] = '200, 100'
# Make Shader Program
shader = compileProgram(compileShader(vertex_src, GL_VERTEX_SHADER),
compileShader(fragment_src, GL_FRAGMENT_SHADER))
# =============================================
# Define VBO, EBO, and VAO
# =============================================
# Cube Object 🧊
cube_VAO = glGenVertexArrays(1)
glBindVertexArray(cube_VAO)
cube_VBO = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, cube_VBO)
glBufferData(GL_ARRAY_BUFFER, cube_vertices.nbytes,
cube_vertices, GL_STATIC_DRAW)
cube_EBO = glGenBuffers(1)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cube_EBO)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, cube_indices.nbytes,
cube_indices, GL_STATIC_DRAW)
# Define Vertex and Texture Shader
glEnableVertexAttribArray(0)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
cube_vertices.itemsize * 8, ctypes.c_void_p(0))
glEnableVertexAttribArray(2)
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE,
cube_vertices.itemsize * 8, ctypes.c_void_p(20))
# QUAD OBJECT ⬜
quad_VAO = glGenVertexArrays(1)
glBindVertexArray(quad_VAO)
quad_VBO = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, quad_VBO)
glBufferData(GL_ARRAY_BUFFER, quad_vertices.nbytes,
quad_vertices, GL_STATIC_DRAW)
quad_EBO = glGenBuffers(1)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, quad_EBO)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, quad_indices.nbytes,
quad_indices, GL_STATIC_DRAW)
# Define Vertex and Texture Shader
glEnableVertexAttribArray(0)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
quad_vertices.itemsize * 5, ctypes.c_void_p(0))
glEnableVertexAttribArray(1)
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
quad_vertices.itemsize * 5, ctypes.c_void_p(12))
# TRIANGLE OBJECT 🔺
triangle_VAO = glGenVertexArrays(1)
glBindVertexArray(triangle_VAO)
triangle_VBO = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, triangle_VBO)
glBufferData(GL_ARRAY_BUFFER, triangle_vertices.nbytes,
triangle_vertices, GL_STATIC_DRAW)
# Define Vertex and Texture Shader
glEnableVertexAttribArray(0)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
triangle_vertices.itemsize * 6, ctypes.c_void_p(0))
glEnableVertexAttribArray(2)
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE,
triangle_vertices.itemsize * 6, ctypes.c_void_p(12))
# load texture
texture = glGenTextures(2)
cube_tex = load_texture_pygame('textures/wood.jpeg', texture[0])
quad_tex = load_texture_pygame('textures/brick.png', texture[1])
# Using program shader
glUseProgram(shader)
glClearColor(0, 0, 0.1, 0)
glEnable(GL_DEPTH_TEST)
# Define projection, transform, and view array
projection = pyrr.matrix44.create_perspective_projection(45, 800/600, 0.1, 100)
view = pyrr.matrix44.create_look_at(pyrr.Vector3(
[0, 0, 3]), pyrr.Vector3([0, 0, 0]), pyrr.Vector3([0, 1, 0]))
cube = pyrr.matrix44.create_from_translation(pyrr.Vector3([1, 0, 0]))
quad = pyrr.matrix44.create_from_translation(pyrr.Vector3([-1, 0, 0]))
triangle = pyrr.matrix44.create_from_translation(pyrr.Vector3([0, 1, -1]))
model_loc = glGetUniformLocation(shader, "model")
proj_loc = glGetUniformLocation(shader, "projection")
view_loc = glGetUniformLocation(shader, "view")
switcher_loc = glGetUniformLocation(shader, "switcher")
glUniformMatrix4fv(proj_loc, 1, GL_FALSE, projection)
glUniformMatrix4fv(view_loc, 1, GL_FALSE, view)
running = True
# ==========================================
# Loop until the user closes the window
# ==========================================
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.VIDEORESIZE:
glViewport(0, 0, event.w, event.h)
projection = pyrr.matrix44.create_perspective_projection(
45, event.w/event.h, 0.1, 100)
glUniformMatrix4fv(proj_loc, 1, GL_FALSE, projection)
counter = pygame.time.get_ticks() / 1000
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
# Rotation
rot_x = pyrr.Matrix44.from_x_rotation(0.5 * counter)
rot_y = pyrr.Matrix44.from_y_rotation(0.7 * counter)
rot_z = pyrr.Matrix44.from_z_rotation(0.9 * counter)
rotation = pyrr.matrix44.multiply((rot_x * rot_y), rot_z)
# Draw Object
model = pyrr.matrix44.multiply(rotation, cube)
glBindVertexArray(cube_VAO)
glUniform1i(switcher_loc, 1)
glBindTexture(GL_TEXTURE_2D, texture[0])
glUniformMatrix4fv(model_loc, 1, GL_FALSE, model)
glDrawElements(GL_TRIANGLES, len(cube_indices), GL_UNSIGNED_INT, None)
model = pyrr.matrix44.multiply(rot_x, quad)
glBindVertexArray(quad_VAO)
glUniform1i(switcher_loc, 0)
glBindTexture(GL_TEXTURE_2D, texture[1])
glUniformMatrix4fv(model_loc, 1, GL_FALSE, model)
glDrawElements(GL_TRIANGLES, len(quad_indices), GL_UNSIGNED_INT, None)
model = pyrr.matrix44.multiply(rot_y, triangle)
glBindVertexArray(triangle_VAO)
glUniform1i(switcher_loc, 1)
glUniformMatrix4fv(model_loc, 1, GL_FALSE, model)
glDrawArrays(GL_TRIANGLES, 0, 3)
pygame.display.flip()
pygame.quit()