From 3aed05a6cb265e4f60a17f87eb368fb33c93a562 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Sat, 10 May 2025 16:26:33 -0500 Subject: cleanup some --- renderer.py | 49 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 15 deletions(-) (limited to 'renderer.py') diff --git a/renderer.py b/renderer.py index 92d0686..01c9f3f 100644 --- a/renderer.py +++ b/renderer.py @@ -8,7 +8,7 @@ class ImageRenderer: def __init__(self): # Setup shader and quad self.shader = self._init_shader() - self._init_quad() + self.vao = self._init_quad() # Get uniform locations from the shader self.uTransform = glGetUniformLocation(self.shader, "uTransform") @@ -21,8 +21,11 @@ class ImageRenderer: # Setup transition self.transition = None - def set_transition(self, transition): - self.transition = transition(self) + # State helper + self._state = None + + def set_transition(self, transition_cls): + self.transition = transition_cls(self) @staticmethod def compile_shader(source, shader_type): @@ -33,7 +36,8 @@ class ImageRenderer: raise RuntimeError(glGetShaderInfoLog(shader).decode()) # Raise error with log if failed return shader - def _init_shader(self): + @staticmethod + def _init_shader(): vertex_src = """ #version 330 core layout (location = 0) in vec2 aPos; // Vertex position @@ -68,7 +72,8 @@ class ImageRenderer: glLinkProgram(prog) return prog - def _init_quad(self): + @staticmethod + def _init_quad(): # Define a full-screen quad with positions and texture coordinates quad = np.array([ -1, -1, 0, 0, # Bottom-left @@ -78,9 +83,9 @@ class ImageRenderer: ], dtype=np.float32) # Create and bind a Vertex Array Object - self.vao = glGenVertexArrays(1) + vao = glGenVertexArrays(1) vbo = glGenBuffers(1) - glBindVertexArray(self.vao) + glBindVertexArray(vao) glBindBuffer(GL_ARRAY_BUFFER, vbo) glBufferData(GL_ARRAY_BUFFER, quad.nbytes, quad, GL_STATIC_DRAW) @@ -91,8 +96,9 @@ class ImageRenderer: # Setup vertex attributes: texture coordinates (location = 1) glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 16, ctypes.c_void_p(8)) glEnableVertexAttribArray(1) + return vao - def draw_static(self, tex, win_w, win_h, alpha): + def draw_image(self, tex, win_w, win_h, alpha): glUseProgram(self.shader) glBindVertexArray(self.vao) glBindTexture(GL_TEXTURE_2D, tex.id) @@ -124,9 +130,25 @@ class ImageRenderer: # Draw the textured quad glDrawArrays(GL_TRIANGLE_STRIP, 0, 4) + def draw_static(self, tex, win_w, win_h, alpha): + # Set the background color to black + glClearColor(0.0, 0.0, 0.0, 1.0) + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) + + self.draw_image(tex, win_w, win_h, alpha) + + glutSwapBuffers() + def draw_transition(self, tex_start, tex_end, win_w, win_h, delta_time, transition_time, transition_duration): assert self.transition, "No transition has been set" - return self.transition.draw(tex_start, tex_end, win_w, win_h, delta_time, transition_time, transition_duration) + + # Set the background color to black + glClearColor(0.0, 0.0, 0.0, 1.0) + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) + + self.transition.draw(tex_start, tex_end, win_w, win_h, delta_time, transition_time, transition_duration) + + glutSwapBuffers() class Transition(Protocol): @@ -144,9 +166,6 @@ class TransitionMix(Transition): if alpha > 1.0: alpha = 1.0 - # Draw the first image - self.renderer.draw_static(tex_start, win_w, win_h, 1 - alpha) # TODO instead of decreasing alpha, draw transparent letterboxes - # Draw the second image (with transparency) - self.renderer.draw_static(tex_end, win_w, win_h, alpha) - - return alpha >= 1.0 # Complete + # Draw the images on top of one another + self.renderer.draw_image(tex_start, win_w, win_h, 1 - alpha) # TODO instead of decreasing alpha, draw transparent letterboxes + self.renderer.draw_image(tex_end, win_w, win_h, alpha) -- cgit v1.2.3