-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwxpythonopengl.py
215 lines (160 loc) · 6.25 KB
/
wxpythonopengl.py
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import wx
import numpy as np
import time
from wx import glcanvas
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.arrays import vbo
from OpenGL.GL import shaders
from readobj import Obj3D
__author__ = 'Bhupendra Aole'
__version__ = '0.1.0'
vertexShader = """
#version 120
void main() {
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
"""
fragmentShader = """
#version 120
void main() {
gl_FragColor = vec4( .9, .9, .9, 1 );
}
"""
class GLFrame( glcanvas.GLCanvas ):
"""A simple class for using OpenGL with wxPython."""
near_plane = 0.1
far_plane = 100
world_pos = (0, 0, -6)
world_rot = (0, 0, 0)
def __init__(self, parent):
self.GLinitialized = False
attribList = (glcanvas.WX_GL_RGBA, # RGBA
glcanvas.WX_GL_DOUBLEBUFFER, # Double Buffered
glcanvas.WX_GL_DEPTH_SIZE, 24) # 24 bit
super(GLFrame, self).__init__( parent, attribList=attribList )
#
# Create the canvas
self.context = glcanvas.GLContext( self )
self.left_down = False
#
# Set the event handlers.
self.Bind(wx.EVT_ERASE_BACKGROUND, self.processEraseBackgroundEvent)
self.Bind(wx.EVT_SIZE, self.processSizeEvent)
self.Bind(wx.EVT_PAINT, self.processPaintEvent)
self.Bind(wx.EVT_MOUSEWHEEL, self.processWheelEvent)
self.Bind(wx.EVT_MOTION, self.processMotion)
self.Bind(wx.EVT_LEFT_DOWN, self.processLeftDown)
self.Bind(wx.EVT_LEFT_UP, self.processLeftUp)
#
# Canvas Proxy Methods
def GetGLExtents(self):
"""Get the extents of the OpenGL canvas."""
return self.GetClientSize()
#
# wxPython Window Handlers
def processLeftDown( self, event ):
self.last_pos = event.GetPosition()
self.left_down = True
def processLeftUp( self, event ):
self.left_down = False
def processMotion( self, event ):
if self.left_down:
pos = event.GetPosition()
diff = (pos-self.last_pos)
self.world_rot = ( self.world_rot[0]+diff[1], self.world_rot[1]+diff[0], self.world_rot[2] )
# print( )
self.last_pos = pos
self.Refresh( False )
def processWheelEvent( self, event ):
delta = event.GetWheelRotation() / 100
self.world_pos = ( self.world_pos[0], self.world_pos[1], self.world_pos[2]+delta )
self.Refresh( False )
def processEraseBackgroundEvent( self, event ):
"""Process the erase background event."""
pass # Do nothing, to avoid flashing on MSWin
def processSizeEvent( self, event ):
self.Show()
self.SetCurrent( self.context )
size = self.GetGLExtents()
self.OnReshape( size.width, size.height )
self.Refresh( False )
event.Skip()
def processPaintEvent(self, event):
self.SetCurrent( self.context )
# This is a 'perfect' time to initialize OpenGL ... only if we need to
if not self.GLinitialized:
self.OnInitGL()
self.GLinitialized = True
self.OnDraw()
event.Skip()
#
# GLFrame OpenGL Event Handlers
def OnInitGL(self):
"""Initialize OpenGL for use in the window."""
glClearColor(1, 1, 1, 1)
VERTEX_SHADER = shaders.compileShader( vertexShader, GL_VERTEX_SHADER )
FRAGMENT_SHADER = shaders.compileShader( fragmentShader, GL_FRAGMENT_SHADER )
self.shader = shaders.compileProgram( VERTEX_SHADER, FRAGMENT_SHADER )
cube = Obj3D( 'testdata\cube.obj' )
data = cube.getVerticesFlat()
self.vbo = vbo.VBO( np.array( data, 'f' ) )
def OnReshape( self, width, height ):
"""Reshape the OpenGL viewport based on the dimensions of the window."""
glViewport( 0, 0, width, height )
glMatrixMode( GL_PROJECTION )
glLoadIdentity()
# glOrtho( -0.5, 0.5, -0.5, 0.5, -1, 1 )
gluPerspective( 45.0, width/height, self.near_plane, self.far_plane )
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
def OnDraw( self ):
glPushMatrix()
glTranslate( self.world_pos[0], self.world_pos[1], self.world_pos[2] )
glRotated( self.world_rot[1], 0, 1, 0 )
glRotated( self.world_rot[0], 1, 0, 0 )
glClear( GL_COLOR_BUFFER_BIT )
shaders.glUseProgram( self.shader )
self.vbo.bind()
glEnableClientState( GL_VERTEX_ARRAY );
glVertexPointerf( self.vbo )
glDrawArrays( GL_TRIANGLES, 0, len( self.vbo ) )
self.vbo.unbind()
glDisableClientState( GL_VERTEX_ARRAY );
shaders.glUseProgram( 0 )
glPopMatrix()
self.SwapBuffers()
class Window( wx.Frame ):
def __init__( self, *args, **kwargs ):
super().__init__( *args, **kwargs )
self.initUI()
def initUI( self ):
panel = GLFrame(self)
panel.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
wx.StaticText( panel, label='Boilerplate Code', pos=( 10, 10 ) )
fmenu = wx.Menu()
self.popupMenu = wx.Menu()
fitem = fmenu.Append( wx.ID_OPEN, '&Open\tCtrl+O', 'Open file' )
self.popupMenu.Append( wx.ID_OPEN, '&Open\tCtrl+O', 'Open file' )
self.Bind( wx.EVT_MENU, self.onOpen, fitem )
fmenu.AppendSeparator()
fitem = fmenu.Append( wx.ID_EXIT, 'E&xit\tCtrl+Q', 'Exit Application' )
self.popupMenu.Append( wx.ID_EXIT, 'E&xit\tCtrl+Q', 'Exit Application' )
self.Bind(wx.EVT_MENU, self.onQuit, fitem)
mbar = wx.MenuBar()
mbar.Append( fmenu, '&File' )
self.SetMenuBar( mbar )
self.Show()
def OnRightDown(self, event):
self.PopupMenu( self.popupMenu, event.GetPosition() )
def onQuit( self, event ):
self.Close()
def onOpen( self, event ):
print( 'open' )
class Application( wx.App ):
def run( self ):
frame = Window(None, -1, 'Boilerplate Window', size=(400,300))
frame.Show()
self.MainLoop()
self.Destroy()
Application().run()