osx - Vertex Buffer Objects in PyOpenGL - vertex, index and colour -
osx 10.8.3, python, pyopengl.
i'm trying put basic terrain-drawing function in pyopengl using vbos. having couple of problems don't know how resolve, because don't understand way vbos work. basic test sandbox started when had difficulty changing project on vbos, it's sloppy in places (see: frame counter).
- i've hardcoded basic 3x3 terrain array , corresponding index array, seems drawing 2 of 3 rows (it y=0 row , y=0.5 not y=2.0). i'm not entirely comfortable numpy, might cause.
- the contents of colour array have erratic bearing on final colours: assigning numpy.zeros() array produces black screen, assigning numpy.ones() produces green , purple stripes rather white surface i'm expecting. using random colours didn't different ones(). don't understand how opengl supposed determine color vbo rather else.
- the triangle-drawing algorithm problematic, when - don't know how best draw multiple rows without using gl_primitive_restart, isn't available in glut (i'd rather change glfw or similar last resort). right now, i'm not concerned it. drawing in strips isn't quite want: http:// www.matrix44.net/cms/notes/opengl-3d-graphics/understanding-gl_triangle_strip (sorry, don't have 10 reputation)
how looks, when rotating: http://i.imgur.com/4db4qyj.png
have been using http://www.mbsoftworks.sk/index.php?page=tutorials&series=1&tutorial=8 successful guide.
i'd appreciate guidance - no single resource i've found on web has explained all, , isn't complex topic.
from opengl.gl import * opengl.glut import * opengl.glu import * opengl.arrays import vbo math import sin, cos, tan, radians, sqrt numpy import * random import random hwindow = 0 frameno = 0 sr2 = sqrt(0.75) def initgl( nwidth, nheight ): glclearcolor( 0.0, 0.0, 0.0, 0.0 ) glcleardepth( 1.0 ) gldepthfunc( gl_less ) glenable( gl_depth_test ) glshademodel( gl_smooth ) print str(glgetstring( gl_version )) global terrain_vbo, index_vbo, color_vbo # these 2 produce working (huge) triangle #terrain_array = array([[ 0,0,0 ], [ 9,0,0 ], [ 0,9,0 ], [ 0,0,9 ]], dtype=float32) #index_array = array([ 0,1, 0,2, 0,3, 2,3, 2,1],dtype=ubyte) terrain_array = array([ [[0,0,0], [0,0,1], [0,0,2]], [[1,0.5,0], [1,0.5,1], [1,0.5,2]], [[2,2,0], [2,2,1], [2,2,2]] ], dtype=float32) index_array = array([ 0, 3, 1, 4, 2, 5, 8, 4, 7, 3, 6 ], dtype=ubyte ) color_array = zeros( (9, 3) ) in range(9): color_array[i] += (1.0, 1.0, 1.0) ''' color_array[0] = [1.0, 0.0, 0.0] color_array[1] = [0.0, 1.0, 0.0] color_array[2] = [0.0, 0.0, 1.0] color_array[3] = [1.0, 1.0, 1.0] color_array[4] = [1.0, 1.0, 0.0] ''' #for in range(len(terrain_array)): #index_array[i][0] = in range(len(terrain_array)): print terrain_array[i] terrain_vbo = vbo.vbo(terrain_array) #index_vbo = vbo.vbo( zeros((1,3)), target=gl_element_array_buffer ) index_vbo = vbo.vbo(index_array, target=gl_element_array_buffer) color_vbo = vbo.vbo(color_array) resizeglscene( nwidth, nheight ) def resizeglscene( nwidth, nheight ): # prevent divide-by-zero error if window small if nheight == 0: nheight = 1 # reset current viewport , recalculate perspective transformation # projection matrix glviewport( 0, 0, nwidth, nheight ) glmatrixmode( gl_projection ) glloadidentity( ) gluperspective( 45.0, float( nwidth )/float( nheight ), 0.1, 100.0 ) # return modelview matrix mode glmatrixmode( gl_modelview ) # # draw scene. # def drawglscene( ): # clear screen , depth buffer glclear( gl_color_buffer_bit | gl_depth_buffer_bit ) # reset matrix stack identity matrix glloadidentity( ) global frameno frameno = frameno + 1 gltranslatef( 0.0, -0.2, -2.0 ) glrotatef( frameno/6, 0.0, sr2, 0.0 ) # draw code #gltranslatef( 0.0, 0.0, -3.0 ) glscalef( 0.5, 0.5, 0.5 ) glcolor3f( 1.0, 1.0, 1.0 ) global index_vbo, terrain_vbo, color_vbo color_vbo.bind() glenableclientstate( gl_color_array ) glcolorpointer( 3, gl_float, 0, color_vbo ) terrain_vbo.bind() glenableclientstate(gl_vertex_array) glvertexpointer( 3, gl_float, 0, none ) index_vbo.bind() gldrawelements(gl_triangle_strip, 6, gl_unsigned_byte,none) gldisableclientstate( gl_color_array ) gldisableclientstate( gl_vertex_array ) index_vbo.unbind() terrain_vbo.unbind() color_vbo.unbind() glutswapbuffers( ) def keypressed( key, x, y ): key = ord(key) if key == 27: glutdestroywindow( hwindow ) sys.exit( ) def main( ): global hwindow # initialise glut , few other things glutinit( "" ) glutinitdisplaymode( glut_rgba | glut_double | glut_alpha | glut_depth ) glutinitwindowsize( 640, 480 ) glutinitwindowposition( 0, 0 ) # create our window hwindow = glutcreatewindow( "vbo testing" ) # setup display function callback glutdisplayfunc( drawglscene ) # go full-screen if want glutfullscreen( ) # setup idle function callback -- if idle, want keep # drawing screen glutidlefunc( drawglscene ) # setup window resize callback -- needed if arent going # full-screen glutreshapefunc( resizeglscene ) # setup keyboard function callback handle key presses glutkeyboardfunc( keypressed ) # call our init function initgl( 640, 480 ) # enter window's main loop set things rolling glutmainloop( ) print "hit esc key quit." main( )
Comments
Post a Comment