-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwx_obj_canvas.py
155 lines (127 loc) · 4.73 KB
/
wx_obj_canvas.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
import wx
#import sys
from wx import glcanvas
# The Python OpenGL package can be found at
# http://PyOpenGL.sourceforge.net/
from OpenGL.GL import * #@UnusedWildImport
from OpenGL.GLU import * #@UnusedWildImport
# IMPORT OBJECT LOADER
from objloader import OBJ
class MyCanvasBase(glcanvas.GLCanvas):
def __init__(self, parent):
glcanvas.GLCanvas.__init__(self, parent, -1)
self.init = False
# initial mouse position
self.rx, self.ry = self.last_rx,self.last_ry = (0,0)
self.tx, self.ty = self.last_tx, self.last_ty = (0,0)
self.size = None
self.zpos = -3
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
self.Bind(wx.EVT_SIZE, self.OnSize)
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_LEFT_DOWN, self.OnLMouseDown)
self.Bind(wx.EVT_LEFT_UP, self.OnLMouseUp)
self.Bind(wx.EVT_RIGHT_DOWN, self.OnRMouseDown)
self.Bind(wx.EVT_RIGHT_UP, self.OnRMouseUp)
self.Bind(wx.EVT_MOTION, self.OnMouseMotion)
self.Bind(wx.EVT_MOUSEWHEEL, self.OnScroll)
def OnEraseBackground(self, event):
pass # Do nothing, to avoid flashing on MSW.
def OnScroll(self, event):
if event.GetWheelRotation() > 0:
self.zpos += 1
else:
self.zpos -= 1
self.Refresh(True)
def OnSize(self, event):
size = self.size = self.GetClientSize()
'''
if self.GetContext():
self.SetCurrent()
'''
if self.init == True:
glViewport(0, 0, size.width, size.height)
event.Skip()
def OnPaint(self, event):
dc = wx.PaintDC(self)
self.SetCurrent()
if not self.init:
self.InitGL()
self.init = True
self.OnDraw()
def OnLMouseDown(self, evt):
self.SetFocus()
self.CaptureMouse()
self.last_rx, self.last_ry = evt.GetPosition()
def OnLMouseUp(self, evt):
self.Refresh(True)
if self.HasCapture():
self.ReleaseMouse()
def OnRMouseDown(self, evt):
self.SetFocus()
self.CaptureMouse()
self.last_tx, self.last_ty = evt.GetPosition()
def OnRMouseUp(self, evt):
self.Refresh(True)
if self.HasCapture():
self.ReleaseMouse()
def OnMouseMotion(self, evt):
if evt.Dragging() and evt.LeftIsDown():
i,j= evt.GetPosition()
self.rx += i-self.last_rx
self.ry += j-self.last_ry
self.last_rx,self.last_ry= (i,j)
self.Refresh(False)
elif evt.Dragging() and evt.RightIsDown():
i,j= evt.GetPosition()
self.tx += i-self.last_tx
self.ty -= j-self.last_ty
self.last_tx,self.last_ty= (i,j)
self.Refresh(False)
class GL_Canvas(MyCanvasBase):
def InitGL(self):
glLightfv(GL_LIGHT0, GL_DIFFUSE, (1,1,1))
glLightfv(GL_LIGHT0, GL_AMBIENT, (0.2,0.2,0.2))
glLightfv(GL_LIGHT0, GL_SPECULAR, (1.0,1.0,1.0))
glLightfv(GL_LIGHT0, GL_POSITION, (3,3, 0.0))
glEnable(GL_LIGHT0)
glEnable(GL_LIGHTING)
glEnable(GL_COLOR_MATERIAL)
glEnable(GL_DEPTH_TEST)
glShadeModel(GL_SMOOTH) # most obj files expect to be smooth-shaded
self.obj = OBJ("small_sphere.obj", swapyz=True)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(90, 800.0/600.0, 1, 100.0)
glEnable(GL_DEPTH_TEST)
glMatrixMode(GL_MODELVIEW)
def load_obj(self, obj_file):
self.obj = OBJ(obj_file, swapyz=True)
self.Refresh(True)
def adj_amb_light(self, v):
glLightfv(GL_LIGHT0, GL_AMBIENT, (v,v,v))
self.Refresh(True)
def adj_light_pos(self, v):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glLightfv(GL_LIGHT0, GL_POSITION, (3, 3,-v))
self.Refresh(True)
def adj_dif_light(self, v):
glLightfv(GL_LIGHT0, GL_DIFFUSE, (v,v,v))
self.Refresh(True)
def OnDraw(self):
# clear color and depth buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
if self.size is None:
self.size = self.GetClientSize()
w, h = self.size
w = max(w, 1.0)
h = max(h, 1.0)
xScale = 180.0 / w
yScale = 180.0 / h
glTranslate(float(self.tx*xScale)/20.0, float(self.ty*yScale)/20.0, self.zpos)
glRotatef(float(self.rx*xScale), 0.0, 1.0, 0.0);
glRotatef(float(self.ry*yScale), 1.0, 0.0, 0.0);
glCallList(self.obj.gl_list)
self.SwapBuffers()