Want more features on Pastebin? Sign Up, it's FREE!
Guest

pyopengl problems?

By: a guest on Feb 22nd, 2012  |  syntax: Python  |  size: 2.76 KB  |  views: 36  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. from OpenGL.GL import *
  2. from OpenGL.GLUT import *
  3.  
  4. def _make_shader(stype, src):
  5.     shader = glCreateShader(stype)
  6.     glShaderSource(shader, src)
  7.     glCompileShader(shader)
  8.  
  9.     if not glGetShaderiv(shader, GL_COMPILE_STATUS):
  10.         err = glGetShaderInfoLog(shader)
  11.         glDeleteShader(shader)
  12.         raise Exception(err)
  13.     return shader
  14.  
  15. verts = (GLfloat*8)()
  16. for i, v in enumerate([-1.0, -1.0, 1.0, -1.0, -1.0,  1.0, 1.0,  1.0]):
  17.     verts[i] = v
  18. polys = (GLushort*6)()
  19. for i, v in enumerate([0,1,3,1,2,3]):
  20.     polys[i] = v
  21.  
  22. def init_gl():
  23.     global program, posidx, vbuf, ebuf
  24.  
  25.     glEnable(GL_BLEND)
  26.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
  27.     glClearColor(0.5,0.5,0.5,1)
  28.     glClearDepth(1.0)
  29.     glDepthFunc(GL_LESS)
  30.     glEnable(GL_DEPTH_TEST)
  31.     glEnable(GL_TEXTURE_2D)
  32.     glViewport(0,0,800,600)
  33.  
  34.     ######################################
  35.     # Make the shaders and programs
  36.     ######################################
  37.     vshade = _make_shader(GL_VERTEX_SHADER, open("stupid.v.glsl").read())
  38.     fshade = _make_shader(GL_FRAGMENT_SHADER, open("stupid.f.glsl").read())
  39.  
  40.     program = glCreateProgram()
  41.     glAttachShader(program, vshade)
  42.     glAttachShader(program, fshade)
  43.     glLinkProgram(program)
  44.  
  45.     #######################################
  46.     # Fetch positions, push poly to card
  47.     #######################################
  48.     posidx = glGetAttribLocation(program, "position")
  49.    
  50.     vbuf = glGenBuffers(1)
  51.     ebuf = glGenBuffers(1)
  52.     glBindBuffer(GL_ARRAY_BUFFER, vbuf)
  53.     glBufferData(GL_ARRAY_BUFFER, verts, GL_STATIC_DRAW)
  54.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebuf)
  55.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, polys, GL_STATIC_DRAW)
  56.  
  57.  
  58. #####################################
  59. # Enter main drawing loop!
  60. #####################################
  61. def render():
  62.     global program, posidx, vbuf, ebuf
  63.     # Clear the screen
  64.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  65.     glUseProgram(program)
  66.  
  67.     # Enable the position attribute
  68.     glEnableVertexAttribArray(posidx)
  69.     glBindBuffer(GL_ARRAY_BUFFER, vbuf)
  70.     glVertexAttribPointer( posidx, 2, GL_FLOAT, GL_FALSE, 4*2, 0)
  71.  
  72.     # Draw the two triangles
  73.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebuf);
  74.     glDrawElements(
  75.         GL_TRIANGLES,           # mode
  76.         6,           # count
  77.         GL_UNSIGNED_SHORT,      # type
  78.         0                       # element array buffer offset
  79.     )
  80.  
  81.     glDisableVertexAttribArray(posidx)
  82.     glutSwapBuffers()
  83.  
  84. def main():
  85.     glutInit()
  86.     glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
  87.     glutInitWindowSize(800, 600);
  88.     glutCreateWindow("Flag");
  89.     glutDisplayFunc(render);
  90.  
  91.     init_gl();
  92.  
  93.     glutMainLoop();
  94.  
  95. if __name__ == "__main__":
  96.     main()
clone this paste RAW Paste Data