Solve a problem – Filter by language, license, keyword, owner, or search text to find code & info fast.
Join Siafoo Now
or
Learn More
Using OpenGL with wxPython
| In Brief | This is a quick example of using OpenGL with wxPython. When the script is run it will display two "spirals". The red spiral is drawn in immediate mode (glBegin/glEnd). The green spiral is drawn using Vertex Arrays.... more |
| Language | Python |
# 's
1#Uncomment if you have multiple wxWidgets versions
2#import wxversion
3#wxversion.select('2.8')
4
5import math, wx
6
7from wx.glcanvas import GLCanvas
8from OpenGL.GLU import *
9from OpenGL.GL import *
10
11
12class WxGLTest(GLCanvas):
13 def __init__(self, parent):
14 GLCanvas.__init__(self, parent,-1, attribList=[wx.glcanvas.WX_GL_DOUBLEBUFFER]))
15 wx.EVT_PAINT(self, self.on_draw)
16 wx.EVT_SIZE(self, self.on_size)
17 wx.EVT_MOTION(self, self.on_mouse_motion)
18
19 self.init = True
20
21 def on_draw(self,event):
22 self.SetCurrent()
23
24 if not self.init:
25 self.InitGL()
26 self.init = False
27
28 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
29 glLoadIdentity();
30
31 # Draw the spiral in 'direct mode'
32 radius = 1.0
33 x = radius*math.sin(0)
34 y = radius*math.cos(0)
35 glColor(0.0, 1.0, 0.0)
36 glBegin(GL_LINE_STRIP)
37 for deg in xrange(1000):
38 glVertex(x, y, 0.0);
39 rad = math.radians(deg)
40 radius -= 0.001
41 x = radius*math.sin(rad)
42 y = radius*math.cos(rad)
43 glEnd()
44
45 glEnableClientState(GL_VERTEX_ARRAY)
46
47 spiral_array = []
48
49 # Second Spiral using Vertex Arrays
50 radius = 0.8
51 x = radius*math.sin(0)
52 y = radius*math.cos(0)
53 glColor(1.0, 0.0, 0.0)
54 for deg in xrange(820):
55 spiral_array.append([x,y])
56 rad = math.radians(deg)
57 radius -= 0.001
58 x = radius*math.sin(rad)
59 y = radius*math.cos(rad)
60
61 glVertexPointerf(spiral_array)
62 glDrawArrays(GL_LINE_STRIP, 0, len(spiral_array))
63 glFlush();
64 self.SwapBuffers()
65 return
66
67 def init_gl(self):
68 # set viewing projection
69 light_diffuse = [1.0, 1.0, 1.0, 1.0]
70 light_position = [1.0, 1.0, 1.0, 0.0]
71
72 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse)
73 glLightfv(GL_LIGHT0, GL_POSITION, light_position)
74
75 glEnable(GL_LIGHTING)
76 glEnable(GL_LIGHT0)
77 glEnable(GL_DEPTH_TEST)
78 glClearColor(0.0, 0.0, 0.0, 1.0)
79 glClearDepth(1.0)
80
81 glMatrixMode(GL_PROJECTION)
82 glLoadIdentity()
83 gluPerspective(40.0, 1.0, 1.0, 30.0)
84
85 glMatrixMode(GL_MODELVIEW)
86 glLoadIdentity()
87 gluLookAt(0.0, 0.0, 10.0,
88 0.0, 0.0, 0.0,
89 0.0, 1.0, 0.0)
90 return
91
92 def on_size(self, event):
93
94 try:
95 width, height = event.GetSize()
96 except:
97 width = event.GetSize().width
98 height = event.GetSize().height
99
100 self.Refresh();
101 self.Update();
102
103 def on_mouse_motion(self, event):
104 x = event.GetX()
105 y = event.GetY()
106
107if __name__ == '__main__':
108
109 app = wx.App()
110 frame = wx.Frame(None, -1, 'wxWidgets and OpenGL', wx.DefaultPosition, wx.Size(400,400))
111 canvas = WxGLTest(frame)
112
113 frame.Show()
114 app.MainLoop()
This is a quick example of using OpenGL with wxPython. When the script is run it will display two "spirals". The red spiral is drawn in immediate mode (glBegin/glEnd). The green spiral is drawn using Vertex Arrays.
Note that the glVertex and glColor calls do not require the parameter type specification (2f,3f, etc). Also note that the call specifying the vertex array pointer (line 61) in Python is simpler than the equivalent call in C/C++ as it only requires one argument (not 4), a list of lists. The reason for these simplifications is that the data types and parameter structure can be determined from the list objects being passed to the OpenGL calls.
When you run the program it should look like this:
Add a Comment