printrun-src/printrun/stlview.py

Sat, 04 Jun 2016 09:22:51 +0200

author
mbayer
date
Sat, 04 Jun 2016 09:22:51 +0200
changeset 20
03b34402d405
parent 15
0bbb006204fc
child 46
cce0af6351f0
permissions
-rw-r--r--

Code cleanup

15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1 #!/usr/bin/env python
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
3 # This file is part of the Printrun suite.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
4 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
5 # Printrun is free software: you can redistribute it and/or modify
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
6 # it under the terms of the GNU General Public License as published by
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
7 # the Free Software Foundation, either version 3 of the License, or
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
8 # (at your option) any later version.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
9 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
10 # Printrun is distributed in the hope that it will be useful,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
13 # GNU General Public License for more details.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
14 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
15 # You should have received a copy of the GNU General Public License
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
16 # along with Printrun. If not, see <http://www.gnu.org/licenses/>.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
17
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
18 import wx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
19 import time
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
20
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
21 import numpy
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
22 import pyglet
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
23 pyglet.options['debug_gl'] = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
24
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
25 from pyglet.gl import GL_AMBIENT_AND_DIFFUSE, glBegin, glClearColor, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
26 glColor3f, GL_CULL_FACE, GL_DEPTH_TEST, GL_DIFFUSE, GL_EMISSION, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
27 glEnable, glEnd, GL_FILL, GLfloat, GL_FRONT_AND_BACK, GL_LIGHT0, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
28 GL_LIGHT1, glLightfv, GL_LIGHTING, GL_LINE, glMaterialf, glMaterialfv, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
29 glMultMatrixd, glNormal3f, glPolygonMode, glPopMatrix, GL_POSITION, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
30 glPushMatrix, glRotatef, glScalef, glShadeModel, GL_SHININESS, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
31 GL_SMOOTH, GL_SPECULAR, glTranslatef, GL_TRIANGLES, glVertex3f, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
32 glGetDoublev, GL_MODELVIEW_MATRIX, GLdouble, glClearDepth, glDepthFunc, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
33 GL_LEQUAL, GL_BLEND, glBlendFunc, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
34 GL_LINE_LOOP, glGetFloatv, GL_LINE_WIDTH, glLineWidth, glDisable, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
35 GL_LINE_SMOOTH
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
36 from pyglet import gl
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
37
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
38 from .gl.panel import wxGLPanel
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
39 from .gl.trackball import build_rotmatrix
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
40 from .gl.libtatlin import actors
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
41
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
42 def vec(*args):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
43 return (GLfloat * len(args))(*args)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
44
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
45 class stlview(object):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
46 def __init__(self, facets, batch):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
47 # Create the vertex and normal arrays.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
48 vertices = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
49 normals = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
50
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
51 for i in facets:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
52 for j in i[1]:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
53 vertices.extend(j)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
54 normals.extend(i[0])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
55
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
56 # Create a list of triangle indices.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
57 indices = range(3 * len(facets)) # [[3*i, 3*i+1, 3*i+2] for i in xrange(len(facets))]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
58 self.vertex_list = batch.add_indexed(len(vertices) // 3,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
59 GL_TRIANGLES,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
60 None, # group,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
61 indices,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
62 ('v3f/static', vertices),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
63 ('n3f/static', normals))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
64
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
65 def delete(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
66 self.vertex_list.delete()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
67
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
68 class StlViewPanel(wxGLPanel):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
69
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
70 do_lights = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
71
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
72 def __init__(self, parent, size, id = wx.ID_ANY,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
73 build_dimensions = None, circular = False,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
74 antialias_samples = 0):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
75 super(StlViewPanel, self).__init__(parent, id, wx.DefaultPosition, size, 0,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
76 antialias_samples = antialias_samples)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
77 self.batches = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
78 self.rot = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
79 self.canvas.Bind(wx.EVT_MOUSE_EVENTS, self.move)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
80 self.canvas.Bind(wx.EVT_MOUSEWHEEL, self.wheel)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
81 self.canvas.Bind(wx.EVT_LEFT_DCLICK, self.double_click)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
82 self.initialized = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
83 self.parent = parent
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
84 self.initpos = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
85 if build_dimensions:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
86 self.build_dimensions = build_dimensions
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
87 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
88 self.build_dimensions = [200, 200, 100, 0, 0, 0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
89 self.platform = actors.Platform(self.build_dimensions,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
90 circular = circular)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
91 self.dist = max(self.build_dimensions[0], self.build_dimensions[1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
92 self.basequat = [0, 0, 0, 1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
93 wx.CallAfter(self.forceresize)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
94 self.mousepos = (0, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
95
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
96 def OnReshape(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
97 self.mview_initialized = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
98 super(StlViewPanel, self).OnReshape()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
99
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
100 # ==========================================================================
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
101 # GLFrame OpenGL Event Handlers
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
102 # ==========================================================================
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
103 def OnInitGL(self, call_reshape = True):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
104 '''Initialize OpenGL for use in the window.'''
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
105 if self.GLinitialized:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
106 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
107 self.GLinitialized = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
108 # create a pyglet context for this panel
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
109 self.pygletcontext = gl.Context(gl.current_context)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
110 self.pygletcontext.canvas = self
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
111 self.pygletcontext.set_current()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
112 # normal gl init
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
113 glClearColor(0, 0, 0, 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
114 glColor3f(1, 0, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
115 glEnable(GL_DEPTH_TEST)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
116 glClearDepth(1.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
117 glDepthFunc(GL_LEQUAL)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
118 glEnable(GL_CULL_FACE)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
119 glEnable(GL_BLEND)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
120 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
121 # Uncomment this line for a wireframe view
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
122 # glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
123
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
124 # Simple light setup. On Windows GL_LIGHT0 is enabled by default,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
125 # but this is not the case on Linux or Mac, so remember to always
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
126 # include it.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
127 glEnable(GL_LIGHTING)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
128 glEnable(GL_LIGHT0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
129 glEnable(GL_LIGHT1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
130
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
131 glLightfv(GL_LIGHT0, GL_POSITION, vec(.5, .5, 1, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
132 glLightfv(GL_LIGHT0, GL_SPECULAR, vec(.5, .5, 1, 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
133 glLightfv(GL_LIGHT0, GL_DIFFUSE, vec(1, 1, 1, 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
134 glLightfv(GL_LIGHT1, GL_POSITION, vec(1, 0, .5, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
135 glLightfv(GL_LIGHT1, GL_DIFFUSE, vec(.5, .5, .5, 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
136 glLightfv(GL_LIGHT1, GL_SPECULAR, vec(1, 1, 1, 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
137 glShadeModel(GL_SMOOTH)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
138
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
139 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, vec(0.5, 0, 0.3, 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
140 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, vec(1, 1, 1, 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
141 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
142 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, vec(0, 0.1, 0, 0.9))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
143 if call_reshape:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
144 self.OnReshape()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
145 if hasattr(self.parent, "filenames") and self.parent.filenames:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
146 for filename in self.parent.filenames:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
147 self.parent.load_file(filename)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
148 self.parent.autoplate()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
149 if hasattr(self.parent, "loadcb"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
150 self.parent.loadcb()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
151 self.parent.filenames = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
152
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
153 def double_click(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
154 if hasattr(self.parent, "clickcb") and self.parent.clickcb:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
155 self.parent.clickcb(event)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
156
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
157 def forceresize(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
158 self.SetClientSize((self.GetClientSize()[0], self.GetClientSize()[1] + 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
159 self.SetClientSize((self.GetClientSize()[0], self.GetClientSize()[1] - 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
160 self.initialized = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
161
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
162 def move(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
163 """react to mouse actions:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
164 no mouse: show red mousedrop
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
165 LMB: move active object,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
166 with shift rotate viewport
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
167 RMB: nothing
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
168 with shift move viewport
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
169 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
170 self.mousepos = event.GetPositionTuple()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
171 if event.Dragging() and event.LeftIsDown():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
172 self.handle_rotation(event)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
173 elif event.Dragging() and event.RightIsDown():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
174 self.handle_translation(event)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
175 elif event.ButtonUp(wx.MOUSE_BTN_LEFT):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
176 if self.initpos is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
177 self.initpos = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
178 elif event.ButtonUp(wx.MOUSE_BTN_RIGHT):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
179 if self.initpos is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
180 self.initpos = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
181 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
182 event.Skip()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
183 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
184 event.Skip()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
185 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
186
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
187 def handle_wheel(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
188 delta = event.GetWheelRotation()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
189 factor = 1.05
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
190 x, y = event.GetPositionTuple()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
191 x, y, _ = self.mouse_to_3d(x, y, local_transform = True)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
192 if delta > 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
193 self.zoom(factor, (x, y))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
194 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
195 self.zoom(1 / factor, (x, y))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
196
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
197 def wheel(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
198 """react to mouse wheel actions:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
199 rotate object
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
200 with shift zoom viewport
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
201 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
202 self.handle_wheel(event)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
203 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
204
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
205 def keypress(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
206 """gets keypress events and moves/rotates acive shape"""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
207 keycode = event.GetKeyCode()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
208 step = 5
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
209 angle = 18
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
210 if event.ControlDown():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
211 step = 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
212 angle = 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
213 # h
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
214 if keycode == 72:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
215 self.parent.move_shape((-step, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
216 # l
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
217 if keycode == 76:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
218 self.parent.move_shape((step, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
219 # j
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
220 if keycode == 75:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
221 self.parent.move_shape((0, step))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
222 # k
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
223 if keycode == 74:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
224 self.parent.move_shape((0, -step))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
225 # [
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
226 if keycode == 91:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
227 self.parent.rotate_shape(-angle)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
228 # ]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
229 if keycode == 93:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
230 self.parent.rotate_shape(angle)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
231 event.Skip()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
232 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
233
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
234 def anim(self, obj):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
235 g = 50 * 9.8
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
236 v = 20
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
237 dt = 0.05
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
238 basepos = obj.offsets[2]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
239 obj.offsets[2] += obj.animoffset
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
240 while obj.offsets[2] > -1:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
241 time.sleep(dt)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
242 obj.offsets[2] -= v * dt
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
243 v += g * dt
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
244 if obj.offsets[2] < 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
245 obj.scale[2] *= 1 - 3 * dt
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
246 # return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
247 v = v / 4
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
248 while obj.offsets[2] < basepos:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
249 time.sleep(dt)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
250 obj.offsets[2] += v * dt
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
251 v -= g * dt
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
252 obj.scale[2] *= 1 + 5 * dt
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
253 obj.scale[2] = 1.0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
254
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
255 def create_objects(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
256 '''create opengl objects when opengl is initialized'''
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
257 if not self.platform.initialized:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
258 self.platform.init()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
259 self.initialized = 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
260 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
261
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
262 def prepare_model(self, m, scale):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
263 batch = pyglet.graphics.Batch()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
264 stlview(m.facets, batch = batch)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
265 m.batch = batch
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
266 # m.animoffset = 300
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
267 # threading.Thread(target = self.anim, args = (m, )).start()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
268 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
269
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
270 def update_object_resize(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
271 '''called when the window recieves only if opengl is initialized'''
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
272 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
273
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
274 def draw_objects(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
275 '''called in the middle of ondraw after the buffer has been cleared'''
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
276 self.create_objects()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
277
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
278 glPushMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
279 glTranslatef(0, 0, -self.dist)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
280 glMultMatrixd(build_rotmatrix(self.basequat)) # Rotate according to trackball
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
281 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, vec(0.2, 0.2, 0.2, 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
282 glTranslatef(- self.build_dimensions[3] - self.platform.width / 2,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
283 - self.build_dimensions[4] - self.platform.depth / 2, 0) # Move origin to bottom left of platform
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
284 # Draw platform
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
285 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
286 glDisable(GL_LIGHTING)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
287 self.platform.draw()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
288 glEnable(GL_LIGHTING)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
289 # Draw mouse
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
290 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
291 inter = self.mouse_to_plane(self.mousepos[0], self.mousepos[1],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
292 plane_normal = (0, 0, 1), plane_offset = 0,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
293 local_transform = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
294 if inter is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
295 glPushMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
296 glTranslatef(inter[0], inter[1], inter[2])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
297 glBegin(GL_TRIANGLES)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
298 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, vec(1, 0, 0, 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
299 glNormal3f(0, 0, 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
300 glVertex3f(2, 2, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
301 glVertex3f(-2, 2, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
302 glVertex3f(-2, -2, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
303 glVertex3f(2, -2, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
304 glVertex3f(2, 2, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
305 glVertex3f(-2, -2, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
306 glEnd()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
307 glPopMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
308
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
309 # Draw objects
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
310 glDisable(GL_CULL_FACE)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
311 glPushMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
312 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, vec(0.3, 0.7, 0.5, 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
313 for i in self.parent.models:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
314 model = self.parent.models[i]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
315 glPushMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
316 glTranslatef(*(model.offsets))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
317 glRotatef(model.rot, 0.0, 0.0, 1.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
318 glTranslatef(*(model.centeroffset))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
319 glScalef(*model.scale)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
320 model.batch.draw()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
321 glPopMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
322 glPopMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
323 glEnable(GL_CULL_FACE)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
324
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
325 # Draw cutting plane
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
326 if self.parent.cutting:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
327 # FIXME: make this a proper Actor
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
328 axis = self.parent.cutting_axis
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
329 fixed_dist = self.parent.cutting_dist
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
330 dist, plane_width, plane_height = self.get_cutting_plane(axis, fixed_dist)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
331 if dist is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
332 glPushMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
333 if axis == "x":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
334 glRotatef(90, 0, 1, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
335 glRotatef(90, 0, 0, 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
336 glTranslatef(0, 0, dist)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
337 elif axis == "y":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
338 glRotatef(90, 1, 0, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
339 glTranslatef(0, 0, -dist)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
340 elif axis == "z":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
341 glTranslatef(0, 0, dist)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
342 glDisable(GL_CULL_FACE)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
343 glBegin(GL_TRIANGLES)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
344 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, vec(0, 0.9, 0.15, 0.3))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
345 glNormal3f(0, 0, self.parent.cutting_direction)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
346 glVertex3f(plane_width, plane_height, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
347 glVertex3f(0, plane_height, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
348 glVertex3f(0, 0, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
349 glVertex3f(plane_width, 0, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
350 glVertex3f(plane_width, plane_height, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
351 glVertex3f(0, 0, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
352 glEnd()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
353 glEnable(GL_CULL_FACE)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
354 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
355 glEnable(GL_LINE_SMOOTH)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
356 orig_linewidth = (GLfloat)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
357 glGetFloatv(GL_LINE_WIDTH, orig_linewidth)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
358 glLineWidth(4.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
359 glBegin(GL_LINE_LOOP)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
360 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, vec(0, 0.8, 0.15, 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
361 glVertex3f(0, 0, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
362 glVertex3f(0, plane_height, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
363 glVertex3f(plane_width, plane_height, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
364 glVertex3f(plane_width, 0, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
365 glEnd()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
366 glLineWidth(orig_linewidth)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
367 glDisable(GL_LINE_SMOOTH)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
368 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
369 glPopMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
370
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
371 glPopMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
372
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
373 # ==========================================================================
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
374 # Utils
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
375 # ==========================================================================
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
376 def get_modelview_mat(self, local_transform):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
377 mvmat = (GLdouble * 16)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
378 if local_transform:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
379 glPushMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
380 # Rotate according to trackball
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
381 glTranslatef(0, 0, -self.dist)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
382 glMultMatrixd(build_rotmatrix(self.basequat)) # Rotate according to trackball
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
383 glTranslatef(- self.build_dimensions[3] - self.platform.width / 2,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
384 - self.build_dimensions[4] - self.platform.depth / 2, 0) # Move origin to bottom left of platform
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
385 glGetDoublev(GL_MODELVIEW_MATRIX, mvmat)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
386 glPopMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
387 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
388 glGetDoublev(GL_MODELVIEW_MATRIX, mvmat)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
389 return mvmat
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
390
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
391 def get_cutting_plane(self, cutting_axis, fixed_dist, local_transform = False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
392 cutting_plane_sizes = {"x": (self.platform.depth, self.platform.height),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
393 "y": (self.platform.width, self.platform.height),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
394 "z": (self.platform.width, self.platform.depth)}
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
395 plane_width, plane_height = cutting_plane_sizes[cutting_axis]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
396 if fixed_dist is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
397 return fixed_dist, plane_width, plane_height
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
398 ref_sizes = {"x": self.platform.width,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
399 "y": self.platform.depth,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
400 "z": self.platform.height,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
401 }
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
402 ref_planes = {"x": (0, 0, 1),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
403 "y": (0, 0, 1),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
404 "z": (0, 1, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
405 }
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
406 ref_offsets = {"x": 0,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
407 "y": 0,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
408 "z": - self.platform.depth / 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
409 }
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
410 translate_axis = {"x": 0,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
411 "y": 1,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
412 "z": 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
413 }
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
414 fallback_ref_planes = {"x": (0, 1, 0),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
415 "y": (1, 0, 0),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
416 "z": (1, 0, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
417 }
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
418 fallback_ref_offsets = {"x": - self.platform.height / 2,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
419 "y": - self.platform.width / 2,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
420 "z": - self.platform.width / 2,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
421 }
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
422 ref_size = ref_sizes[cutting_axis]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
423 ref_plane = ref_planes[cutting_axis]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
424 ref_offset = ref_offsets[cutting_axis]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
425 inter = self.mouse_to_plane(self.mousepos[0], self.mousepos[1],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
426 plane_normal = ref_plane,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
427 plane_offset = ref_offset,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
428 local_transform = local_transform)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
429 max_size = max((self.platform.width,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
430 self.platform.depth,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
431 self.platform.height))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
432 dist = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
433 if inter is not None and numpy.fabs(inter).max() + max_size / 2 < 2 * max_size:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
434 dist = inter[translate_axis[cutting_axis]]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
435 if dist is None or dist < -0.5 * ref_size or dist > 1.5 * ref_size:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
436 ref_plane = fallback_ref_planes[cutting_axis]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
437 ref_offset = fallback_ref_offsets[cutting_axis]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
438 inter = self.mouse_to_plane(self.mousepos[0], self.mousepos[1],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
439 plane_normal = ref_plane,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
440 plane_offset = ref_offset,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
441 local_transform = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
442 if inter is not None and numpy.fabs(inter).max() + max_size / 2 < 2 * max_size:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
443 dist = inter[translate_axis[cutting_axis]]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
444 if dist is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
445 dist = min(1.5 * ref_size, max(-0.5 * ref_size, dist))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
446 return dist, plane_width, plane_height
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
447
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
448 def main():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
449 app = wx.App(redirect = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
450 frame = wx.Frame(None, -1, "GL Window", size = (400, 400))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
451 StlViewPanel(frame)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
452 frame.Show(True)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
453 app.MainLoop()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
454 app.Destroy()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
455
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
456 if __name__ == "__main__":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
457 main()

mercurial