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 import pygame
26 from OpenGL.GL import *
27 """
28 Draws things in OpenGL
29 """
30
31
32
33
34
35
36
37
38
39
40
41
42 -def texture_from_image(texture, image, square_texture=False):
43 """
44 Copies the pixels from a pygame surface to an OpenGL texture object.
45 """
46 textureData = pygame.image.tostring(image, "RGBX", True)
47 if square_texture:
48 glBindTexture(GL_TEXTURE_2D, texture)
49 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.get_width(), image.get_height(), 0, \
50 GL_RGBA, GL_UNSIGNED_BYTE, textureData)
51 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
52 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
53 else:
54 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texture)
55 glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, image.get_width(), \
56 image.get_height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData)
57 glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
58 glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
59
60 -def draw_textured_square(w=None, h=None):
61 """
62 Draws a texture square of 2 x 2 size centered at 0, 0
63
64 Make sure to call glEnable(GL_TEXTURE_RECTANGLE_ARB) first.
65
66 :param w: width of the image in pixels
67 :param h: height of the image in pixels
68 """
69 if w is None or h is None:
70 glBegin(GL_QUADS)
71 glTexCoord2f(0.0, 0.0)
72 glVertex2f(-1.0, -1.0)
73 glTexCoord2f(1.0, 0.0)
74 glVertex2f(1.0, -1.0)
75 glTexCoord2f(1.0, 1.0)
76 glVertex2f(1.0, 1.0)
77 glTexCoord2f(0.0, 1.0)
78 glVertex2f(-1.0, 1.0)
79 glEnd()
80 else:
81 glBegin(GL_QUADS)
82 glTexCoord2f(0.0, 0.0)
83 glVertex2f(-1.0, -1.0)
84 glTexCoord2f(w, 0.0)
85 glVertex2f(1.0, -1.0)
86 glTexCoord2f(w, h)
87 glVertex2f(1.0, 1.0)
88 glTexCoord2f(0.0, h)
89 glVertex2f(-1.0, 1.0)
90 glEnd()
91
104
105 -def draw_horizontal_progress_bar(background_color=(0.0, 0.0, 0.0, 1.0), foreground_color=(1.0, 1.0, 1.0, 1.0), progress=0.0, line_color=(0.0, 0.0, 0.0, 1.0)):
106 """
107 Draws an horizontal progress bar.
108
109 The programmer should scale this shape, since its position is between
110 -1 and 1 on both axis.
111
112 Make sure to call glDisable(GL_TEXTURE_RECTANGLE_ARB) first.
113
114 :param background_color: tuple of 4 floats from 0 to 1
115 :param foreground_color: tuple of 4 floats from 0 to 1
116 :param progress: float from 0 to 1
117 """
118 glColor4f(*foreground_color)
119 glBegin(GL_QUADS)
120 glVertex2f(-1.0, -1.0)
121 glVertex2f(progress * 2.0 - 1.0, -1.0)
122 glVertex2f(progress * 2.0 - 1.0, 1.0)
123 glVertex2f(-1.0, 1.0)
124 glEnd()
125 glColor4f(*background_color)
126 glBegin(GL_QUADS)
127 glVertex2f(-1.0, -1.0)
128 glVertex2f(1.0, -1.0)
129 glVertex2f(1.0, 1.0)
130 glVertex2f(-1.0, 1.0)
131 glEnd()
132
133 glColor4f(*line_color)
134 glBegin(GL_LINE_LOOP)
135 glVertex2f(-1.0, -1.0)
136 glVertex2f(1.0, -1.0)
137 glVertex2f(1.0, 1.0)
138 glVertex2f(-1.0, 1.0)
139 glEnd()
140