Changeset 8


Ignore:
Timestamp:
Jun 26, 2010, 11:59:48 PM (10 years ago)
Author:
sam
Message:

Testing vertex buffers. They're really, really faster.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test-map.py

    r5 r8  
    1111from pygame.locals import *
    1212
     13import numpy
     14
    1315textures = [0,0]
     16buflist = False
    1417
    1518def resize((width, height)):
     
    1720    glMatrixMode(GL_PROJECTION)
    1821    glLoadIdentity()
    19     glOrtho(0, width, height, 0, 0, 1);
     22    glOrtho(0, width, height, 0, -1, 10);
    2023    glMatrixMode(GL_MODELVIEW)
    2124    glLoadIdentity()
     
    2427    glEnable(GL_TEXTURE_2D)
    2528    load_textures()
     29    make_vbo()
    2630    glShadeModel(GL_SMOOTH)
    2731    glClearColor(0.0, 0.0, 0.0, 0.0)
     
    4246    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
    4347
     48def make_vbo():
     49    global buflist
     50    buflist = glGenBuffers(3)
     51
     52    vertices = [False] * (20 * 15)
     53    for y in range(15):
     54        for x in range(20):
     55            ty = y * 32
     56            tx = x * 32
     57            # Z coord is used for blit order
     58            vertices[x + y * 20] = [tx, ty, y * 0.01,
     59                                    tx + 32, ty, y * 0.01,
     60                                    tx + 32, ty + 32, y * 0.01,
     61                                    tx, ty + 32, y * 0.01]
     62    vertices = numpy.array(vertices, dtype=numpy.float32)
     63    glBindBuffer(GL_ARRAY_BUFFER, buflist[0])
     64    glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW)
     65
     66    indices = numpy.array([x for x in range(4 * 20 * 15)], dtype=numpy.int)
     67    glBindBuffer(GL_ARRAY_BUFFER, buflist[2])
     68    glBufferData(GL_ARRAY_BUFFER, indices, GL_STATIC_DRAW)
     69
    4470def put_map(themap):
    45     glBegin(GL_QUADS)
    46     y = 0.0
     71    uvs = [False] * (20 * 15)
     72    index = 0
    4773    for line in themap:
    48         x = 0.0
    4974        for tile in line:
    50             sy = .0625 * (15 - tile / 16)
    51             sx = .0625 * (tile % 16)
    52             glTexCoord2f(sx, sy); glVertex2f(x, y + 32.0);
    53             glTexCoord2f(sx + .0625, sy); glVertex2f(x + 32.0, y + 32.0);
    54             glTexCoord2f(sx + .0625, sy + .0625); glVertex2f(x + 32.0, y);
    55             glTexCoord2f(sx, sy + .0625); glVertex2f(x, y);
    56             x += 32.0
    57         y += 32.0
    58     glEnd();                           
     75            ty = .0625 * (15 - tile / 16)
     76            tx = .0625 * (tile % 16)
     77            uvs[index] = [tx, ty + .0625,
     78                          tx + .0625, ty + .0625,
     79                          tx + .0625, ty,
     80                          tx, ty]
     81            index += 1
     82    uvs = numpy.array(uvs, dtype=numpy.float32)
     83    glBindBuffer(GL_ARRAY_BUFFER, buflist[1])
     84    glBufferData(GL_ARRAY_BUFFER, uvs, GL_STATIC_DRAW)
     85
     86    glEnableClientState(GL_VERTEX_ARRAY)
     87    glEnableClientState(GL_TEXTURE_COORD_ARRAY)
     88    glEnableClientState(GL_INDEX_ARRAY)
     89
     90    glBindTexture(GL_TEXTURE_2D, textures[0])
     91
     92    glBindBuffer(GL_ARRAY_BUFFER, buflist[0])
     93    glVertexPointer(3, GL_FLOAT, 0, None)
     94    glBindBuffer(GL_ARRAY_BUFFER, buflist[1])
     95    glTexCoordPointer(2, GL_FLOAT, 0, None)
     96    glBindBuffer(GL_ARRAY_BUFFER, buflist[2])
     97    glIndexPointer(GL_INT, 0, None)
     98
     99    glDrawArrays(GL_QUADS, 0, 4 * 20 * 15)
     100
     101    glDisableClientState(GL_VERTEX_ARRAY)
     102    glDisableClientState(GL_TEXTURE_COORD_ARRAY)
     103    glDisableClientState(GL_INDEX_ARRAY)
    59104
    60105def draw():
    61     #glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
     106    glClear(GL_DEPTH_BUFFER_BIT) # Full redraw: no need to clear color buffer
    62107    glLoadIdentity()
    63108
     
    107152        pygame.display.flip()
    108153        frames = frames+1
    109 #        if frames > 200:
    110 #            break
     154        #if frames > 200:
     155        #    break
    111156
    112157    print "fps:  %d" % ((frames*1000)/(pygame.time.get_ticks()-start))
Note: See TracChangeset for help on using the changeset viewer.