ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: Use wxPython/OpenGL to draw a sphere
Submitter: Rick Muller (other recipes)
Last Updated: 2004/10/27
Version no: 1.0
Category: Image

 

Not Rated yet


Description:

A simple wxPython/OpenGL application to draw a sphere.

Source: Text Source

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()

Discussion:

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.



Add comment

Number of comments: 2

Which versions do I need, Jim Carroll, 2004/10/29
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
Add comment

Re: Which versions do I need, Yuhei Kuratomi, 2004/12/19
Change the import order and try again:

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

Add comment



Highest rated recipes:

1. Implementation of sets ...

2. bag collection class

3. deque collection class

4. Floating Point Simulator

5. HTML colors to/from RGB ...

6. Select the nth smallest ...

7. Function Decorators by ...

8. MS SQL Server log monitor

9. Table objects with ...

10. wx twisted support using ...




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.