1 '''

2 @author: Stou Sandalski (stou@icapsid.net)

3 @license: Public Domain

4 '''

5

6 #Uncomment if you have multiple wxWidgets versions

7 #import wxversion

8 #wxversion.select('2.8')

9

10 import math , wx

11

12 from wx.glcanvas import GLCanvas

13 from OpenGL.GLU import *

14 from OpenGL.GL import *

15

16

17 class WxGLTest ( GLCanvas ):

18 def __init__ ( self , parent ):

19 GLCanvas . __init__ ( self , parent , - 1 , attribList = [ wx . glcanvas . WX_GL_DOUBLEBUFFER ])

20 wx . EVT_PAINT ( self , self . OnDraw )

21 wx . EVT_SIZE ( self , self . OnSize )

22 wx . EVT_MOTION ( self , self . OnMouseMotion )

23 wx . EVT_WINDOW_DESTROY ( self , self . OnDestroy )

24

25 self . init = True

26

27 def OnDraw ( self , event ):

28 self . SetCurrent ()

29

30 if not self . init :

31 self . InitGL ()

32 self . init = False

33

34 glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )

35 glLoadIdentity ()

36

37 # Draw the spiral in 'immediate mode'

38 # WARNING: You should not be doing the spiral calculation inside the loop

39 # even if you are using glBegin/glEnd, sin/cos are fairly expensive functions

40 # I've left it here as is to make the code simpler.

41 radius = 1.0

42 x = radius * math . sin ( 0 )

43 y = radius * math . cos ( 0 )

44 glColor ( 0.0 , 1.0 , 0.0 )

45 glBegin ( GL_LINE_STRIP )

46 for deg in xrange ( 1000 ):

47 glVertex ( x , y , 0.0 )

48 rad = math . radians ( deg )

49 radius -= 0.001

50 x = radius * math . sin ( rad )

51 y = radius * math . cos ( rad )

52 glEnd ()

53

54 glEnableClientState ( GL_VERTEX_ARRAY )

55

56 spiral_array = []

57

58 # Second Spiral using "array immediate mode" (i.e. Vertex Arrays)

59 radius = 0.8

60 x = radius * math . sin ( 0 )

61 y = radius * math . cos ( 0 )

62 glColor ( 1.0 , 0.0 , 0.0 )

63 for deg in xrange ( 820 ):

64 spiral_array . append ([ x , y ])

65 rad = math . radians ( deg )

66 radius -= 0.001

67 x = radius * math . sin ( rad )

68 y = radius * math . cos ( rad )

69

70 glVertexPointerf ( spiral_array )

71 glDrawArrays ( GL_LINE_STRIP , 0 , len ( spiral_array ))

72 glFlush ()

73 self . SwapBuffers ()

74 return

75

76 def InitGL ( self ):

77 '''

78 Initialize GL

79 '''

80

81 # # set viewing projection

82 # glClearColor(0.0, 0.0, 0.0, 1.0)

83 # glClearDepth(1.0)

84 #

85 # glMatrixMode(GL_PROJECTION)

86 # glLoadIdentity()

87 # gluPerspective(40.0, 1.0, 1.0, 30.0)

88 #

89 # glMatrixMode(GL_MODELVIEW)

90 # glLoadIdentity()

91 # gluLookAt(0.0, 0.0, 10.0,

92 # 0.0, 0.0, 0.0,

93 # 0.0, 1.0, 0.0)

94

95 def OnSize ( self , event ):

96

97 try :

98 width , height = event . GetSize ()

99 except :

100 width = event . GetSize () . width

101 height = event . GetSize () . height

102

103 self . Refresh ()

104 self . Update ()

105

106 def OnMouseMotion ( self , event ):

107 x = event . GetX ()

108 y = event . GetY ()

109

110 def OnDestroy ( self , event ):

111 print "Destroying Window"

112

113

114 if __name__ == '__main__' :

115

116 app = wx . App ()

117 frame = wx . Frame ( None , - 1 , 'wxPython and OpenGL' , wx . DefaultPosition , wx . Size ( 400 , 400 ))

118 canvas = WxGLTest ( frame )

119

120 frame . Show ()

121 app . MainLoop ()

