Hi all,

I draw a lot of circles in my pyglet code and I'm trying to optimize
as much as possible. I've implemented SiegeLord's brilliant approach
(http://slabode.exofire.net/circle_draw.shtml; mirrored at
http://www.mmsguru.com/mirror/circle_cached.html), using the standard
glBegin()...glEnd() method, but I understand that using glDrawArrays()
might speed things up even further. However, I'm hitting a snag in
providing an array of the proper format to glVertexPointers(). I'm
getting a "argument 4: <type 'exceptions.TypeError'>: wrong type"
error, which presumably means that the array of verticies I'm passing
to glVertexPointers() isn't properly formatted.

My code is below, hopefully someone can show me the error of my ways!

Mike

#import requisite libraries
import time, math
from pyglet.gl import *

#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(1000):
        draw_circle(0,0,100)
print time.time()-start

#define SiegeLord's (faster) circle function
def DrawCircle(cx, cy, radius, num_segments):
        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):
                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(1000):
        DrawCircle(0,0,100,361)
print time.time()-start

#enable the vertex array client state
glEnableClientState(GL_VERTEX_ARRAY)

#define the glDrawArrays() version of the standard function
def draw_circle2(cx,cy,radius):
        verticies = [cx,cy]
        for angle in range(361*4):
                verticies.append(cx + math.sin(angle/4.0) * radius)
                verticies.append(cy + math.cos(angle/4.0) * radius)
        #######NEXT LINE THROWS ERROR
        glVertexPointer(2, GL_FLOAT, 0, verticies)
        glDrawArrays(GL_TRIANGLE_FAN, 0, 361*4+1)

#time the glDrawArrays() version of the standard function
start=time.time()
for i in range(100):
        draw_circle2(0,0,100)
print time.time()-start

#define the glDrawArrays() version of SiegeLord's function
def DrawCircle2(cx, cy, radius, num_segments):
        theta = 2 * math.pi / num_segments
        tangetial_factor = math.tan(theta)
        radial_factor = math.cos(theta)
        x = radius
        y = 0
        verticies = [cx,cy]
        for i in range(num_segments):
                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
        #######NEXT LINE THROWS ERROR
        glVertexPointer(2, GL_FLOAT, 0, verticies)
        glDrawArrays(GL_TRIANGLE_FAN, 0, num_segments+1)

#time the glDrawArrays() version of SiegeLord's function
start=time.time()
for i in range(100):
        DrawCircle2(0,0,100,361*4)
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to