This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Feb 16th, 2012  |  syntax: Python  |  size: 1.93 KB  |  views: 111  |  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.arrays import vbo
  3. from OpenGL.GL import shaders
  4.  
  5. import wx
  6. import wx.glcanvas
  7.  
  8. class MyCanvas(wx.glcanvas.GLCanvas):
  9.     def __init__(self, parent):
  10.         wx.glcanvas.GLCanvas.__init__(self, parent)
  11.  
  12.         VERTEX_SHADER = shaders.compileShader("""#version 330
  13.        void main() {
  14.            gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
  15.        }""", GL_VERTEX_SHADER)
  16.         FRAGMENT_SHADER = shaders.compileShader("""#version 330
  17.        void main() {
  18.            gl_FragColor = vec4( 0, 1, 0, 1 );
  19.        }""", GL_FRAGMENT_SHADER)
  20.         self.shader = shaders.compileProgram(VERTEX_SHADER,FRAGMENT_SHADER)
  21.         self.vbo = vbo.VBO(
  22.             array( [
  23.                 [  0, 1, 0 ],
  24.                 [ -1,-1, 0 ],
  25.                 [  1,-1, 0 ],
  26.                 [  2,-1, 0 ],
  27.                 [  4,-1, 0 ],
  28.                 [  4, 1, 0 ],
  29.                 [  2,-1, 0 ],
  30.                 [  4, 1, 0 ],
  31.                 [  2, 1, 0 ],
  32.             ],'f')
  33.         )
  34.         wx.EVT_PAINT(self, self.OnPaint)
  35.         self.haveInited = False
  36.         self.context = wx.glcanvas.GLContext(self)
  37.  
  38.     def OnPaint( self, mode):
  39.         if not self.haveInited:
  40.             self.SetCurrent(self.context)
  41.             glClearColor(.3, .3, .3, 0)
  42.             self.haveInited = True
  43.         shaders.glUseProgram(self.shader)
  44.         try:
  45.             self.vbo.bind()
  46.             try:
  47.                 glEnableClientState(GL_VERTEX_ARRAY);
  48.                 glVertexPointerf( self.vbo )
  49.                 glDrawArrays(GL_TRIANGLES, 0, 9)
  50.             finally:
  51.                 self.vbo.unbind()
  52.                 glDisableClientState(GL_VERTEX_ARRAY);
  53.         finally:
  54.             shaders.glUseProgram( 0 )
  55.  
  56. class MyApp(wx.App):
  57.     def OnInit(self):
  58.         frame = wx.Frame(None)
  59.         canvas = MyCanvas(frame)
  60.         frame.Show()
  61.         return True
  62. MyApp(redirect = False).MainLoop()
clone this paste RAW Paste Data