Wednesday, November 07, 2007

Example 2-10 using Stride: another way to draw Elements

Instead of defining the vertex and colour arrays one by one, you can also define one large array holding (in this case) vertex and colour data. The data is then referenced by pointing to the different "starting points" within the array.
The parts in bold are changed from the previous 2-10 example.

Have a look at the code:

# Example 2-10 : Using glArrayElement() to Define Colors and Vertices

from pyglet.gl import *
from pyglet import window

win = window.Window()

def display():

intertwined = [
1.0, 0.2, 1.0, 100.0, 100.0, 0.0,
1.0, 0.2, 0.2, 0.0, 200.0, 0.0,
1.0, 1.0, 0.2, 100.0, 300.0, 0.0,
0.2, 1.0, 0.2, 200.0, 300.0, 0.0,
0.2, 1.0, 1.0, 300.0, 200.0, 0.0,
0.2, 0.2, 1.0, 200.0, 100.0, 0.0]

glEnableClientState (GL_COLOR_ARRAY)
glEnableClientState (GL_VERTEX_ARRAY)

glColorPointer (3, GL_FLOAT, 6 * sizeof(GLfloat), (GLfloat * len(intertwined))(*intertwined))
glVertexPointer (3, GL_FLOAT, 6 * sizeof(GLfloat), (GLfloat * len(intertwined[3:]))(*intertwined[3:]))

glBegin(GL_TRIANGLES)
glArrayElement (2)
glArrayElement (3)
glArrayElement (5)
glEnd()

def init():
glClearColor (0.0, 0.0, 0.0, 0.0)
glShadeModel (GL_FLAT)

while not win.has_exit:
win.dispatch_events()
win.clear()
display()
win.flip()


glVertexPointer() is being told that

  • each vertex is composed of 3 coordinates
  • the coordinates are of the type GL_FLOAT
  • the next vertex is to be found at 6 * the size of a GL_FLOAT variable further on in the array
  • the vertex data starts at the 4th element of the intertwined array.

No comments: