PDA

View Full Version : Problem with drawing a vertex array (w/VBO)



moonpirate
05-25-2010, 02:41 PM
I've been trying to draw an array of points for some 2D point sprite rendering, but they display oddly and I cannot figure out why.

Here's my code (note im using pyopenGL, but it shouldn't make a difference)



def init():
global index
glEnable(GL_TEXTURE_2D)
glShadeModel(GL_SMOOTH)
glClearColor(0.0, 0.0, 0.0, 0.0)
glClearDepth(1.0)
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LEQUAL)
glEnable(GL_BLEND)
glEnable(GL_ALPHA_TEST)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glAlphaFunc(GL_GREATER, 0.9)
glEnable(GL_POINT_SPRITE_ARB)
glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT)
glPointSize(8.0)
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)


def make_VBO():
glInitVertexBufferObjectARB() #this creates all the buffer functions and gives them the right names
global VBOVerts, Vertices
scale = 4.0
Vertices = []
for x in range(100):
for y in range(100):
Vertices.append([x/3.0 - 7,y/3.0-4])
Vertices = numpy.array(Vertices) #stores the vertices in a higher performance array
VBOVerts = glGenBuffersARB(1)
glLoadIdentity ()
glBindBufferARB(GL_ARRAY_BUFFER_ARB, VBOVerts);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, Vertices, GL_DYNAMIC_DRAW_ARB);

def load_image():
TextureImage = pygame.image.load('small.png')
TextureString = pygame.image.tostring(TextureImage, "RGBA", 1)
sizeX = TextureImage.get_width()
sizeY = TextureImage.get_height()
TextureID = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, TextureID)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, sizeX, sizeY, 0,
GL_RGBA, GL_UNSIGNED_BYTE, TextureString);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)



def draw():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity ()
glEnableClientState(GL_VERTEX_ARRAY);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, VBOVerts)
glBufferDataARB(GL_ARRAY_BUFFER_ARB, Vertices, GL_DYNAMIC_DRAW_ARB)
glVertexPointer(2, GL_FLOAT, 0, None)

glDrawArrays(GL_POINTS, 0, len(Vertices))

glDisableClientState(GL_VERTEX_ARRAY)
pygame.display.flip()

def main():
video_flags = OPENGL|DOUBLEBUF
pygame.init()
surface = pygame.display.set_mode((640,480), video_flags)
resize((640,480))
init()
load_image()
make_VBO()


while True:
event = pygame.event.poll()
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
break
t = time.clock()
draw()
t = time.clock() - t
admin.timeline += 1
if admin.timeline % 30 == 0:
print 1.0/t, 'Hz'


I thought that this would draw a grid of small.png's, but it seems that everything is drawn on the x and y coordinate axes. Here's what it looks like on my screen:

http://i442.photobucket.com/albums/qq150/m00npirate/whatitlookslike-2.png

I am new to VBOs and vertex arrays and really have no clue why it would be drawing them like this. If I try a straight vertex array it seems to draw fine. I have seen that glGenBuffers takes two arguments usually, but PyOpenGL only allows it to take the first argument (otherwise it complains there are too many). Perhaps this could be the problem? Any help is appreciated.

EDIT: The solution was to replace numpy.array(Vertices) with numpy.array(Vertices, 'f'). *facepalm* (this took me 10+ hours to figure out!)