-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
144 lines (108 loc) · 4.6 KB
/
app.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
import pygame
from OpenGL.GL import *
from OpenGL.GL.shaders import compileProgram, compileShader
import numpy as np
import pyrr
import time
import pathlib
from Cube import Cube, Mesh
from Material import Material
APP_SIZE = (1280, 720)
APP_PATH = pathlib.Path(__file__).parent.resolve()
class App:
def __init__(self) -> None:
# Initialize pygame
pygame.init()
pygame.display.set_mode((APP_SIZE[0], APP_SIZE[1]), pygame.OPENGL | pygame.DOUBLEBUF)
self.clock = pygame.time.Clock()
# Initialize OpenGL
glClearColor(0.1, 0.1, 0.1, 1.0)
# Enable and set up blending for transparency
glEnable(GL_BLEND)
glEnable(GL_DEPTH_TEST)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
# Load and use shaders from files
self.shader = self.create_shader(f'{APP_PATH}/shaders/vertex.vert', f'{APP_PATH}/shaders/fragment.frag')
glUseProgram(self.shader)
# Set texture unit 0 as active uniform sampler location for texture named 'imageTexture' in fragment shader
glUniform1i(glGetUniformLocation(self.shader, 'imageTexture'), 0)
# Define cube
self.cube = Cube(
position=[0, 0, -3],
eulers=[0, 0, 0]
)
self.cube_mesh = Mesh(f'{APP_PATH}/models/cube.obj')
# Load texture image
self.image_texture = Material(f'{APP_PATH}/images/me.jpg')
# Define a 4x4 projection transform with params
projection_transform = pyrr.matrix44.create_perspective_projection(
fovy=45, aspect=APP_SIZE[0]/APP_SIZE[1], near=0.1, far=10, dtype=np.float32
)
#! add comment
glUniformMatrix4fv(
glGetUniformLocation(self.shader, 'projection'), 1, GL_FALSE, projection_transform
)
# Get location in shader where model matrix should go and stores it for efficiency
self.model_matrix_location = glGetUniformLocation(self.shader, 'model')
self.main_loop()
def create_shader(self, vertex_file_path, fragment_file_path):
with open(vertex_file_path, 'r') as f:
vertex_src = ''.join(f.readlines())
with open(fragment_file_path, 'r') as f:
fragment_src = ''.join(f.readlines())
shader = compileProgram(
compileShader(vertex_src, GL_VERTEX_SHADER),
compileShader(fragment_src, GL_FRAGMENT_SHADER)
)
return shader
def main_loop(self):
start_time = time.time()
running = True
while running:
# current_time = time.time() - start_time
# Check events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Update cube
self.cube.eulers[2] += 1
if (self.cube.eulers[2] > 360):
self.cube.eulers[2] -= 360
# Refresh screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
# Use shader program
glUseProgram(self.shader)
self.image_texture.use()
# Create identity, multiply transformations progressively
model_transform = pyrr.matrix44.create_identity(dtype=np.float32)
# Rotate cube around its own axis
model_transform = pyrr.matrix44.multiply(
m1=model_transform,
m2=pyrr.matrix44.create_from_eulers(
eulers=np.radians(self.cube.eulers),
dtype=np.float32
)
)
# Translate cube to its position
model_transform = pyrr.matrix44.multiply(
m1=model_transform,
m2=pyrr.matrix44.create_from_translation(
vec=self.cube.position,
dtype=np.float32
)
)
glUniformMatrix4fv(self.model_matrix_location, 1, GL_FALSE, model_transform)
glBindVertexArray(self.cube_mesh.vao)
# Draw cube using currently bound shader and VAO
glDrawArrays(GL_TRIANGLES, 0, self.cube_mesh.vertex_count)
pygame.display.flip()
# Timing
self.clock.tick(60)
self.quit()
def quit(self):
self.cube_mesh.delete()
self.image_texture.delete()
glDeleteProgram(self.shader)
pygame.quit()
if __name__ == '__main__':
myApp = App()