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
|
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
class Transition:
def draw(self, texture_prev, texture_next, window_width, window_height, delta_time, transition_time, transition_duration):
# Update alpha value for fade effect
alpha = transition_time / transition_duration
if alpha > 1.0:
alpha = 1.0
# Draw the first image
self.draw_image(texture_prev, window_width, window_height, 1 - alpha) # TODO instead of decreasing alpha, draw transparent letterboxes
# Draw the second image (with transparency)
self.draw_image(texture_next, window_width, window_height, alpha)
return alpha >= 1.0 # Complete
# Draw the image with blending enabled (to allow fade effect)
def draw_image(self, texture, window_width, window_height, alpha):
if not alpha: return
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glBindTexture(GL_TEXTURE_2D, texture.id)
glColor4f(1.0, 1.0, 1.0, alpha) # Set alpha to control transparency
# Calculate aspect ratio
img_aspect = texture.width / float(texture.height)
win_aspect = window_width / float(window_height)
scaled_width = window_width
scaled_height = window_height
# Scale the image to fit inside the window while maintaining aspect ratio
if img_aspect > win_aspect:
# Image is wider than window, letterbox vertically
scaled_height = window_width / img_aspect
else:
# Image is taller than window, letterbox horizontally
scaled_width = window_height * img_aspect
# Position the image so it is centered
offset_x = (window_width - scaled_width) / 2
offset_y = (window_height - scaled_height) / 2
# Normalize coordinates to range from -1 to 1
x1 = (offset_x / window_width ) * 2 - 1
y1 = (offset_y / window_height) * 2 - 1
x2 = ((offset_x + scaled_width ) / window_width ) * 2 - 1
y2 = ((offset_y + scaled_height) / window_height) * 2 - 1
# Draw the image in the center with scaling
glBegin(GL_QUADS)
glTexCoord2f(0, 0)
glVertex2f(x1, y1)
glTexCoord2f(1, 0)
glVertex2f(x2, y1)
glTexCoord2f(1, 1)
glVertex2f(x2, y2)
glTexCoord2f(0, 1)
glVertex2f(x1, y2)
glEnd()
glDisable(GL_BLEND)
|