Created
December 22, 2010 04:46
-
-
Save bwhite/751111 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import wx | |
import wx.glcanvas | |
from OpenGL.GL import * | |
class GLFrame(wx.Frame): | |
"""A simple class for using OpenGL with wxPython.""" | |
def __init__(self, parent, id, title, pos=wx.DefaultPosition, | |
size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE, | |
name='frame'): | |
# Forcing a specific style on the window. | |
# Should this include styles passed? | |
style = wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE | |
super(GLFrame, self).__init__(parent, id, title, | |
pos, size, style, name) | |
self._GLinitialized = False | |
attribList = (wx.glcanvas.WX_GL_RGBA, # RGBA | |
wx.glcanvas.WX_GL_DOUBLEBUFFER, # Double Buffered | |
wx.glcanvas.WX_GL_DEPTH_SIZE, 24) # 24 bit | |
# Create the canvas | |
self.canvas = wx.glcanvas.GLCanvas(self, attribList=attribList) | |
# Set the event handlers. | |
self.canvas.Bind(wx.EVT_PAINT, self.processPaintEvent) | |
def processPaintEvent(self, event): | |
"""Process the drawing event.""" | |
self.canvas.SetCurrent() | |
if not self._GLinitialized: | |
glClearColor(1, 1, 1, 1) | |
self._GLinitialized = True | |
self._OnDraw() | |
event.Skip() | |
def _OnDraw(self, *args, **kwargs): | |
"Draw the window." | |
glClear(GL_COLOR_BUFFER_BIT) | |
# Drawing an example triangle in the middle of the screen | |
glBegin(GL_TRIANGLES) | |
glColor(0, 0, 0) | |
glVertex(-.25, -.25) | |
glVertex(.25, -.25) | |
glVertex(0, .25) | |
glEnd() | |
self.canvas.SwapBuffers() | |
app = wx.PySimpleApp() | |
frame = GLFrame(None, -1, 'GL Window') | |
frame.Show() | |
if __name__ == '__main__': | |
app.MainLoop() | |
app.Destroy() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment