Welcome, guest | Sign In | My Account | Store | Cart
NOTE: Recipes have moved! Please visit GitHub.com/activestate/code for the current versions.

A simple wxPython/OpenGL application to draw a sphere.

Python, 69 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from wxPython.glcanvas import wxGLCanvas
from wxPython.wx import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
import sys,math

name = 'ball_glut'

class myGLCanvas(wxGLCanvas):
    def __init__(self, parent):
	wxGLCanvas.__init__(self, parent,-1)
	EVT_PAINT(self, self.OnPaint)
	self.init = 0
	return

    def OnPaint(self,event):
	dc = wxPaintDC(self)
	self.SetCurrent()
	if not self.init:
	    self.InitGL()
	    self.init = 1
	self.OnDraw()
	return

    def OnDraw(self):
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glPushMatrix()
	color = [1.0,0.,0.,1.]
	glMaterialfv(GL_FRONT,GL_DIFFUSE,color)
	glutSolidSphere(2,20,20)
	glPopMatrix()
        self.SwapBuffers()
        return
	
    def InitGL(self):
        # set viewing projection
        light_diffuse = [1.0, 1.0, 1.0, 1.0]
        light_position = [1.0, 1.0, 1.0, 0.0]

        glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse)
        glLightfv(GL_LIGHT0, GL_POSITION, light_position)

        glEnable(GL_LIGHTING)
        glEnable(GL_LIGHT0)
        glEnable(GL_DEPTH_TEST)
        glClearColor(0.0, 0.0, 0.0, 1.0)
        glClearDepth(1.0)

        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluPerspective(40.0, 1.0, 1.0, 30.0)

        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        gluLookAt(0.0, 0.0, 10.0,
                  0.0, 0.0, 0.0,
                  0.0, 1.0, 0.0)
        return
    

def main():
    app = wxPySimpleApp()
    frame = wxFrame(None,-1,'ball_wx',wxDefaultPosition,wxSize(400,400))
    canvas = myGLCanvas(frame)
    frame.Show()
    app.MainLoop()

if __name__ == '__main__': main()

Here's a wxPython version of the pure PythonOpenGL recipe for drawing a ball. Useful as a test to make sure that the wxPython/OpenGL link is working well, and as a starting point for more complicated applications.

2 comments

Jim Carroll 12 years, 11 months ago  # | flag

Which versions do I need. Which versions of what do I need? I think I have everything, but I get the following errors:

C:\jimc\bin>python C:\jimc\bin\openGLSphere.py Traceback (most recent call last):

File "C:\jimc\bin\openGLSphere.py", line 1, in ? from wxPython.glcanvas import wxGLCanvas

File "C:\Python23\Lib\site-packages\wxPython\glcanvas.py", line 6, in ? from misc2 import *

File "C:\Python23\lib\site-packages\wxPython\misc2.py", line 4, in ? from windows import *

File "C:\Python23\lib\site-packages\wxPython\windows.py", line 6, in ?

from gdi import *

File "C:\Python23\lib\site-packages\wxPython\gdi.py", line 7, in ? import wx

File "C:\Python23\lib\site-packages\wxPython\wx.py", line 22, in ? from mdi import *

File "C:\Python23\lib\site-packages\wxPython\mdi.py", line 14, in ? from frames import *

File "C:\Python23\lib\site-packages\wxPython\frames.py", line 14, in ? from stattool import * File "C:\Python23\lib\site-packages\wxPython\stattool.py", line 14, in ? from controls import * File "C:\Python23\lib\site-packages\wxPython\controls.py", line 16, in ? class wxControlPtr(wxWindowPtr): NameError: name 'wxWindowPtr' is not defined

Yuhei Kuratomi 12 years, 9 months ago  # | flag

Re: Which versions do I need. Change the import order and try again:

from wxPython.wx import *
from wxPython.glcanvas import wxGLCanvas