Thanks to Tristam for pointing me to pyglet.graphics.vertex_list!
Below is some code that demonstrates its use. The SiegeLord approach
is about 6 times faster than the standard approach, a benefit that
gets even greater when you anti-alias. Just beautiful :O) Any further
optimization suggestions are welcome!
#import requisite libraries
import pyglet, time, math
from pyglet.gl import *
config = Config(double_buffer=True)#, sample_buffers=1, samples=4,
depth_size=16)
win = pyglet.window.Window(fullscreen=True,config=config,vsync=False)
win.set_exclusive_mouse()
pyglet.gl.glClearColor(0, 0, 0, 0)
glColor3f(1,1,1,1)
win.dispatch_events()
pyglet.image.get_buffer_manager().get_color_buffer().image_data
#define the standard slow circle function
def draw_circle(cx,cy,radius):
glBegin(GL_TRIANGLE_FAN)
glVertex2f(cx, cy)
for angle in range(361):
glVertex2f(
cx + math.sin(angle) * radius
,cy + math.cos(angle) * radius
)
glEnd()
#time the standard circle function
start=time.time()
for i in range(100):
win.clear()
draw_circle(win.width/2,win.height/2,win.height*.9/2)
win.flip()
print time.time()-start
#define SiegeLord's faster circle function
def DrawCircle(cx, cy, radius):
num_segments = 360
theta = 2 * math.pi / num_segments
tangetial_factor = math.tan(theta)
radial_factor = math.cos(theta)
x = radius
y = 0
glBegin(GL_TRIANGLE_FAN)
glVertex2f(cx, cy)
for ii in range(num_segments+1):
glVertex2f(x + cx, y + cy)
tx = -y
ty = x
x = x + tx * tangetial_factor
y = y + ty * tangetial_factor
x = x * radial_factor
y = y * radial_factor
glEnd()
#time SiegeLord's function
start=time.time()
for i in range(100):
win.clear()
DrawCircle(win.width/2,win.height/2,win.height*.9/2)
win.flip()
print time.time()-start
#enable the vertex array client state
glEnableClientState(GL_VERTEX_ARRAY)
#define the batched version of the standard function
def draw_circle2(cx,cy,radius):
verticies=[cx,cy]
for angle in range(361):
verticies.append(cx + math.sin(angle) * radius)
verticies.append(cy + math.cos(angle) * radius)
verticies = pyglet.graphics.vertex_list(362, ('v2f',verticies))
verticies.draw(GL_TRIANGLE_FAN)
#time the batched version of the standard function
start=time.time()
for i in range(100):
win.clear()
draw_circle2(win.width/2,win.height/2,win.height*.9/2)
win.flip()
print time.time()-start
#define the batched version of SiegeLord's function
def DrawCircle2(cx, cy, radius):
num_segments = 360
theta = 2 * math.pi / num_segments
tangetial_factor = math.tan(theta)
radial_factor = math.cos(theta)
x = radius
y = 0
verticies = [cx,cy]
for ii in range(num_segments+1):
verticies.append(x + cx)
verticies.append(y + cy)
tx = -y
ty = x
x = x + tx * tangetial_factor
y = y + ty * tangetial_factor
x = x * radial_factor
y = y * radial_factor
verticies = pyglet.graphics.vertex_list(num_segments+2,
('v2f',verticies))
verticies.draw(GL_TRIANGLE_FAN)
#time the batched version of SiegeLord's function
start=time.time()
for i in range(100):
win.clear()
DrawCircle2(win.width/2,win.height/2,win.height*.9/2)
win.flip()
print time.time()-start
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"pyglet-users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/pyglet-users?hl=en
-~----------~----~----~----~------~----~------~--~---