printrun-src/printrun/gl/panel.py

Sat, 04 Jun 2016 15:18:06 +0200

author
mbayer
date
Sat, 04 Jun 2016 15:18:06 +0200
changeset 26
5c98c7e24b02
parent 15
0bbb006204fc
child 46
cce0af6351f0
permissions
-rw-r--r--

Add quick access button to laser options

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 from threading import Lock
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
19 import logging
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
20 import traceback
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
21 import numpy
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
22 import numpy.linalg
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
23
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
24 import wx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
25 from wx import glcanvas
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
26
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
27 import pyglet
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
28 pyglet.options['debug_gl'] = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
29
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
30 from pyglet.gl import glEnable, glDisable, GL_LIGHTING, glLightfv, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
31 GL_LIGHT0, GL_LIGHT1, GL_LIGHT2, GL_POSITION, GL_DIFFUSE, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
32 GL_AMBIENT, GL_SPECULAR, GL_COLOR_MATERIAL, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
33 glShadeModel, GL_SMOOTH, GL_NORMALIZE, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
34 GL_BLEND, glBlendFunc, glClear, glClearColor, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
35 glClearDepth, GL_COLOR_BUFFER_BIT, GL_CULL_FACE, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
36 GL_DEPTH_BUFFER_BIT, glDepthFunc, GL_DEPTH_TEST, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
37 GLdouble, glGetDoublev, glGetIntegerv, GLint, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
38 GL_LEQUAL, glLoadIdentity, glMatrixMode, GL_MODELVIEW, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
39 GL_MODELVIEW_MATRIX, GL_ONE_MINUS_SRC_ALPHA, glOrtho, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
40 GL_PROJECTION, GL_PROJECTION_MATRIX, glScalef, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
41 GL_SRC_ALPHA, glTranslatef, gluPerspective, gluUnProject, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
42 glViewport, GL_VIEWPORT
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
43 from pyglet import gl
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
44 from .trackball import trackball, mulquat
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
45 from .libtatlin.actors import vec
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
46
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
47 class wxGLPanel(wx.Panel):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
48 '''A simple class for using OpenGL with wxPython.'''
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
49
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
50 orthographic = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
51 color_background = (0.98, 0.98, 0.78, 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
52 do_lights = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
53
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
54 def __init__(self, parent, id, pos = wx.DefaultPosition,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
55 size = wx.DefaultSize, style = 0,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
56 antialias_samples = 0):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
57 # Forcing a no full repaint to stop flickering
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
58 style = style | wx.NO_FULL_REPAINT_ON_RESIZE
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
59 super(wxGLPanel, self).__init__(parent, id, pos, size, style)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
60
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
61 self.GLinitialized = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
62 self.mview_initialized = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
63 attribList = (glcanvas.WX_GL_RGBA, # RGBA
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
64 glcanvas.WX_GL_DOUBLEBUFFER, # Double Buffered
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
65 glcanvas.WX_GL_DEPTH_SIZE, 24) # 24 bit
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
66
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
67 if antialias_samples > 0 and hasattr(glcanvas, "WX_GL_SAMPLE_BUFFERS"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
68 attribList += (glcanvas.WX_GL_SAMPLE_BUFFERS, 1,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
69 glcanvas.WX_GL_SAMPLES, antialias_samples)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
70
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
71 self.width = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
72 self.height = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
73
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
74 self.sizer = wx.BoxSizer(wx.HORIZONTAL)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
75 self.canvas = glcanvas.GLCanvas(self, attribList = attribList)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
76 self.context = glcanvas.GLContext(self.canvas)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
77 self.sizer.Add(self.canvas, 1, wx.EXPAND)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
78 self.SetSizerAndFit(self.sizer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
79
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
80 self.rot_lock = Lock()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
81 self.basequat = [0, 0, 0, 1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
82 self.zoom_factor = 1.0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
83
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
84 self.gl_broken = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
85
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
86 # bind events
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
87 self.canvas.Bind(wx.EVT_ERASE_BACKGROUND, self.processEraseBackgroundEvent)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
88 self.canvas.Bind(wx.EVT_SIZE, self.processSizeEvent)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
89 self.canvas.Bind(wx.EVT_PAINT, self.processPaintEvent)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
90
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
91 def processEraseBackgroundEvent(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
92 '''Process the erase background event.'''
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
93 pass # Do nothing, to avoid flashing on MSWin
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
94
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
95 def processSizeEvent(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
96 '''Process the resize event.'''
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
97 if self.IsFrozen():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
98 event.Skip()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
99 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
100 if (wx.VERSION > (2, 9) and self.canvas.IsShownOnScreen()) or self.canvas.GetContext():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
101 # Make sure the frame is shown before calling SetCurrent.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
102 self.canvas.SetCurrent(self.context)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
103 self.OnReshape()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
104 self.Refresh(False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
105 timer = wx.CallLater(100, self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
106 timer.Start()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
107 event.Skip()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
108
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
109 def processPaintEvent(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
110 '''Process the drawing event.'''
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
111 self.canvas.SetCurrent(self.context)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
112
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
113 if not self.gl_broken:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
114 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
115 self.OnInitGL()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
116 self.OnDraw()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
117 except pyglet.gl.lib.GLException:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
118 self.gl_broken = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
119 logging.error(_("OpenGL failed, disabling it:")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
120 + "\n" + traceback.format_exc())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
121 event.Skip()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
122
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
123 def Destroy(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
124 # clean up the pyglet OpenGL context
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
125 self.pygletcontext.destroy()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
126 # call the super method
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
127 super(wxGLPanel, self).Destroy()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
128
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
129 # ==========================================================================
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
130 # GLFrame OpenGL Event Handlers
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
131 # ==========================================================================
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
132 def OnInitGL(self, call_reshape = True):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
133 '''Initialize OpenGL for use in the window.'''
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
134 if self.GLinitialized:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
135 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
136 self.GLinitialized = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
137 # create a pyglet context for this panel
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
138 self.pygletcontext = gl.Context(gl.current_context)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
139 self.pygletcontext.canvas = self
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
140 self.pygletcontext.set_current()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
141 # normal gl init
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
142 glClearColor(*self.color_background)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
143 glClearDepth(1.0) # set depth value to 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
144 glDepthFunc(GL_LEQUAL)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
145 glEnable(GL_COLOR_MATERIAL)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
146 glEnable(GL_DEPTH_TEST)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
147 glEnable(GL_CULL_FACE)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
148 glEnable(GL_BLEND)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
149 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
150 if call_reshape:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
151 self.OnReshape()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
152
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
153 def OnReshape(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
154 """Reshape the OpenGL viewport based on the size of the window"""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
155 size = self.GetClientSize()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
156 oldwidth, oldheight = self.width, self.height
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
157 width, height = size.width, size.height
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
158 if width < 1 or height < 1:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
159 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
160 self.width = max(float(width), 1.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
161 self.height = max(float(height), 1.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
162 self.OnInitGL(call_reshape = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
163 glViewport(0, 0, width, height)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
164 glMatrixMode(GL_PROJECTION)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
165 glLoadIdentity()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
166 if self.orthographic:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
167 glOrtho(-width / 2, width / 2, -height / 2, height / 2,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
168 -5 * self.dist, 5 * self.dist)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
169 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
170 gluPerspective(60., float(width) / height, 10.0, 3 * self.dist)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
171 glTranslatef(0, 0, -self.dist) # Move back
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
172 glMatrixMode(GL_MODELVIEW)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
173
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
174 if not self.mview_initialized:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
175 self.reset_mview(0.9)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
176 self.mview_initialized = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
177 elif oldwidth is not None and oldheight is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
178 wratio = self.width / oldwidth
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
179 hratio = self.height / oldheight
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
180
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
181 factor = min(wratio * self.zoomed_width, hratio * self.zoomed_height)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
182 x, y, _ = self.mouse_to_3d(self.width / 2, self.height / 2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
183 self.zoom(factor, (x, y))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
184 self.zoomed_width *= wratio / factor
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
185 self.zoomed_height *= hratio / factor
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
186
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
187 # Wrap text to the width of the window
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
188 if self.GLinitialized:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
189 self.pygletcontext.set_current()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
190 self.update_object_resize()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
191
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
192 def setup_lights(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
193 if not self.do_lights:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
194 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
195 glEnable(GL_LIGHTING)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
196 glDisable(GL_LIGHT0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
197 glLightfv(GL_LIGHT0, GL_AMBIENT, vec(0.4, 0.4, 0.4, 1.0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
198 glLightfv(GL_LIGHT0, GL_SPECULAR, vec(0, 0, 0, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
199 glLightfv(GL_LIGHT0, GL_DIFFUSE, vec(0, 0, 0, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
200 glEnable(GL_LIGHT1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
201 glLightfv(GL_LIGHT1, GL_AMBIENT, vec(0, 0, 0, 1.0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
202 glLightfv(GL_LIGHT1, GL_SPECULAR, vec(0.6, 0.6, 0.6, 1.0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
203 glLightfv(GL_LIGHT2, GL_DIFFUSE, vec(0.8, 0.8, 0.8, 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
204 glLightfv(GL_LIGHT1, GL_POSITION, vec(1, 2, 3, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
205 glEnable(GL_LIGHT2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
206 glLightfv(GL_LIGHT2, GL_AMBIENT, vec(0, 0, 0, 1.0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
207 glLightfv(GL_LIGHT2, GL_SPECULAR, vec(0.6, 0.6, 0.6, 1.0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
208 glLightfv(GL_LIGHT2, GL_DIFFUSE, vec(0.8, 0.8, 0.8, 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
209 glLightfv(GL_LIGHT2, GL_POSITION, vec(-1, -1, 3, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
210 glEnable(GL_NORMALIZE)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
211 glShadeModel(GL_SMOOTH)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
212
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
213 def reset_mview(self, factor):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
214 glMatrixMode(GL_MODELVIEW)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
215 glLoadIdentity()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
216 self.setup_lights()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
217 if self.orthographic:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
218 wratio = self.width / self.dist
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
219 hratio = self.height / self.dist
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
220 minratio = float(min(wratio, hratio))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
221 self.zoom_factor = 1.0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
222 self.zoomed_width = wratio / minratio
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
223 self.zoomed_height = hratio / minratio
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
224 glScalef(factor * minratio, factor * minratio, 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
225
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
226 def OnDraw(self, *args, **kwargs):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
227 """Draw the window."""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
228 self.pygletcontext.set_current()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
229 glClearColor(*self.color_background)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
230 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
231 self.draw_objects()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
232 self.canvas.SwapBuffers()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
233
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
234 # ==========================================================================
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
235 # To be implemented by a sub class
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
236 # ==========================================================================
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
237 def create_objects(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
238 '''create opengl objects when opengl is initialized'''
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
239 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
240
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
241 def update_object_resize(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
242 '''called when the window recieves only if opengl is initialized'''
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
243 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
244
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
245 def draw_objects(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
246 '''called in the middle of ondraw after the buffer has been cleared'''
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
247 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
248
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
249 # ==========================================================================
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
250 # Utils
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
251 # ==========================================================================
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
252 def get_modelview_mat(self, local_transform):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
253 mvmat = (GLdouble * 16)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
254 glGetDoublev(GL_MODELVIEW_MATRIX, mvmat)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
255 return mvmat
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
256
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
257 def mouse_to_3d(self, x, y, z = 1.0, local_transform = False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
258 x = float(x)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
259 y = self.height - float(y)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
260 # The following could work if we were not initially scaling to zoom on
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
261 # the bed
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
262 # if self.orthographic:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
263 # return (x - self.width / 2, y - self.height / 2, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
264 pmat = (GLdouble * 16)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
265 mvmat = self.get_modelview_mat(local_transform)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
266 viewport = (GLint * 4)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
267 px = (GLdouble)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
268 py = (GLdouble)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
269 pz = (GLdouble)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
270 glGetIntegerv(GL_VIEWPORT, viewport)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
271 glGetDoublev(GL_PROJECTION_MATRIX, pmat)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
272 glGetDoublev(GL_MODELVIEW_MATRIX, mvmat)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
273 gluUnProject(x, y, z, mvmat, pmat, viewport, px, py, pz)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
274 return (px.value, py.value, pz.value)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
275
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
276 def mouse_to_ray(self, x, y, local_transform = False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
277 x = float(x)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
278 y = self.height - float(y)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
279 pmat = (GLdouble * 16)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
280 mvmat = (GLdouble * 16)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
281 viewport = (GLint * 4)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
282 px = (GLdouble)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
283 py = (GLdouble)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
284 pz = (GLdouble)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
285 glGetIntegerv(GL_VIEWPORT, viewport)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
286 glGetDoublev(GL_PROJECTION_MATRIX, pmat)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
287 mvmat = self.get_modelview_mat(local_transform)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
288 gluUnProject(x, y, 1, mvmat, pmat, viewport, px, py, pz)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
289 ray_far = (px.value, py.value, pz.value)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
290 gluUnProject(x, y, 0., mvmat, pmat, viewport, px, py, pz)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
291 ray_near = (px.value, py.value, pz.value)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
292 return ray_near, ray_far
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
293
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
294 def mouse_to_plane(self, x, y, plane_normal, plane_offset, local_transform = False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
295 # Ray/plane intersection
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
296 ray_near, ray_far = self.mouse_to_ray(x, y, local_transform)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
297 ray_near = numpy.array(ray_near)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
298 ray_far = numpy.array(ray_far)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
299 ray_dir = ray_far - ray_near
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
300 ray_dir = ray_dir / numpy.linalg.norm(ray_dir)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
301 plane_normal = numpy.array(plane_normal)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
302 q = ray_dir.dot(plane_normal)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
303 if q == 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
304 return None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
305 t = - (ray_near.dot(plane_normal) + plane_offset) / q
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
306 if t < 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
307 return None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
308 return ray_near + t * ray_dir
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
309
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
310 def zoom(self, factor, to = None):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
311 glMatrixMode(GL_MODELVIEW)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
312 if to:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
313 delta_x = to[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
314 delta_y = to[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
315 glTranslatef(delta_x, delta_y, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
316 glScalef(factor, factor, 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
317 self.zoom_factor *= factor
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
318 if to:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
319 glTranslatef(-delta_x, -delta_y, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
320 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
321
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
322 def zoom_to_center(self, factor):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
323 self.canvas.SetCurrent(self.context)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
324 x, y, _ = self.mouse_to_3d(self.width / 2, self.height / 2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
325 self.zoom(factor, (x, y))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
326
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
327 def handle_rotation(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
328 if self.initpos is None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
329 self.initpos = event.GetPositionTuple()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
330 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
331 p1 = self.initpos
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
332 p2 = event.GetPositionTuple()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
333 sz = self.GetClientSize()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
334 p1x = float(p1[0]) / (sz[0] / 2) - 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
335 p1y = 1 - float(p1[1]) / (sz[1] / 2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
336 p2x = float(p2[0]) / (sz[0] / 2) - 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
337 p2y = 1 - float(p2[1]) / (sz[1] / 2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
338 quat = trackball(p1x, p1y, p2x, p2y, self.dist / 250.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
339 with self.rot_lock:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
340 self.basequat = mulquat(self.basequat, quat)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
341 self.initpos = p2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
342
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
343 def handle_translation(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
344 if self.initpos is None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
345 self.initpos = event.GetPositionTuple()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
346 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
347 p1 = self.initpos
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
348 p2 = event.GetPositionTuple()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
349 if self.orthographic:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
350 x1, y1, _ = self.mouse_to_3d(p1[0], p1[1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
351 x2, y2, _ = self.mouse_to_3d(p2[0], p2[1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
352 glTranslatef(x2 - x1, y2 - y1, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
353 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
354 glTranslatef(p2[0] - p1[0], -(p2[1] - p1[1]), 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
355 self.initpos = p2

mercurial