printrun-src/printrun/gl/panel.py

Wed, 20 Jan 2021 10:17:01 +0100

author
mdd
date
Wed, 20 Jan 2021 10:17:01 +0100
changeset 47
dcc64b767b64
parent 46
cce0af6351f0
permissions
-rw-r--r--

updated pronterface.py losing my own changes

15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1 # This file is part of the Printrun suite.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
2 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
3 # Printrun is free software: you can redistribute it and/or modify
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
4 # it under the terms of the GNU General Public License as published by
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
5 # the Free Software Foundation, either version 3 of the License, or
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
6 # (at your option) any later version.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
7 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
8 # Printrun is distributed in the hope that it will be useful,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
11 # GNU General Public License for more details.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
12 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
13 # You should have received a copy of the GNU General Public License
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
14 # along with Printrun. If not, see <http://www.gnu.org/licenses/>.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
16 from threading import Lock
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
17 import logging
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
18 import traceback
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
19 import numpy
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
20 import numpy.linalg
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
21
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
22 import wx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
23 from wx import glcanvas
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
24
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
25 import pyglet
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
26 pyglet.options['debug_gl'] = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
27
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
28 from pyglet.gl import glEnable, glDisable, GL_LIGHTING, glLightfv, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
29 GL_LIGHT0, GL_LIGHT1, GL_LIGHT2, GL_POSITION, GL_DIFFUSE, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
30 GL_AMBIENT, GL_SPECULAR, GL_COLOR_MATERIAL, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
31 glShadeModel, GL_SMOOTH, GL_NORMALIZE, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
32 GL_BLEND, glBlendFunc, glClear, glClearColor, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
33 glClearDepth, GL_COLOR_BUFFER_BIT, GL_CULL_FACE, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
34 GL_DEPTH_BUFFER_BIT, glDepthFunc, GL_DEPTH_TEST, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
35 GLdouble, glGetDoublev, glGetIntegerv, GLint, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
36 GL_LEQUAL, glLoadIdentity, glMatrixMode, GL_MODELVIEW, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
37 GL_MODELVIEW_MATRIX, GL_ONE_MINUS_SRC_ALPHA, glOrtho, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
38 GL_PROJECTION, GL_PROJECTION_MATRIX, glScalef, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
39 GL_SRC_ALPHA, glTranslatef, gluPerspective, gluUnProject, \
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
40 glViewport, GL_VIEWPORT, glPushMatrix, glPopMatrix, \
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
41 glBegin, glVertex2f, glVertex3f, glEnd, GL_LINE_LOOP, glColor3f, \
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
42 GL_LINE_STIPPLE, glColor4f, glLineStipple
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
43
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
44 from pyglet import gl
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
45 from .trackball import trackball, mulquat, axis_to_quat
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
46 from .libtatlin.actors import vec
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
47 from pyglet.gl.glu import gluOrtho2D
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
48
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
49 # When Subclassing wx.Window in Windows the focus goes to the wx.Window
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
50 # instead of GLCanvas and it does not draw the focus rectangle and
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
51 # does not consume used keystrokes
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
52 # BASE_CLASS = wx.Window
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
53 # Subclassing Panel solves problem In Windows
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
54 BASE_CLASS = wx.Panel
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
55 # BASE_CLASS = wx.ScrolledWindow
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
56 # BASE_CLASS = glcanvas.GLCanvas
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
57 class wxGLPanel(BASE_CLASS):
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
58 '''A simple class for using OpenGL with wxPython.'''
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
59
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
60 orbit_control = True
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
61 orthographic = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
62 color_background = (0.98, 0.98, 0.78, 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
63 do_lights = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
64
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
65 def __init__(self, parent, pos = wx.DefaultPosition,
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
66 size = wx.DefaultSize, style = 0,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
67 antialias_samples = 0):
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
68 # Full repaint should not be a performance problem
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
69 #TODO: test on windows, tested in Ubuntu
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
70 style = style | wx.FULL_REPAINT_ON_RESIZE
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
71
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
72 self.GLinitialized = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
73 self.mview_initialized = False
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
74 attribList = [glcanvas.WX_GL_RGBA, # RGBA
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
75 glcanvas.WX_GL_DOUBLEBUFFER, # Double Buffered
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
76 glcanvas.WX_GL_DEPTH_SIZE, 24 # 24 bit
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
77 ]
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
78
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
79 if antialias_samples > 0 and hasattr(glcanvas, "WX_GL_SAMPLE_BUFFERS"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
80 attribList += (glcanvas.WX_GL_SAMPLE_BUFFERS, 1,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
81 glcanvas.WX_GL_SAMPLES, antialias_samples)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
82
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
83 attribList.append(0)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
84
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
85 if BASE_CLASS is glcanvas.GLCanvas:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
86 super().__init__(parent, wx.ID_ANY, attribList, pos, size, style)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
87 self.canvas = self
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
88 else:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
89 super().__init__(parent, wx.ID_ANY, pos, size, style)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
90 self.canvas = glcanvas.GLCanvas(self, wx.ID_ANY, attribList, pos, size, style)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
91
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
92 self.width = self.height = None
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
93
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
94 self.context = glcanvas.GLContext(self.canvas)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
95
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
96 self.rot_lock = Lock()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
97 self.basequat = [0, 0, 0, 1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
98 self.zoom_factor = 1.0
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
99 self.angle_z = 0
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
100 self.angle_x = 0
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
101
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
102 self.gl_broken = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
103
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
104 # bind events
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
105 self.canvas.Bind(wx.EVT_SIZE, self.processSizeEvent)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
106 if self.canvas is not self:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
107 self.Bind(wx.EVT_SIZE, self.OnScrollSize)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
108 # do not focus parent (panel like) but its canvas
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
109 self.SetCanFocus(False)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
110
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
111 self.canvas.Bind(wx.EVT_ERASE_BACKGROUND, self.processEraseBackgroundEvent)
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
112 # In wxWidgets 3.0.x there is a clipping bug during resizing
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
113 # which could be affected by painting the container
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
114 # self.Bind(wx.EVT_PAINT, self.processPaintEvent)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
115 # Upgrade to wxPython 4.1 recommended
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
116 self.canvas.Bind(wx.EVT_PAINT, self.processPaintEvent)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
117
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
118 self.canvas.Bind(wx.EVT_SET_FOCUS, self.processFocus)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
119 self.canvas.Bind(wx.EVT_KILL_FOCUS, self.processKillFocus)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
120
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
121 def processFocus(self, ev):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
122 # print('processFocus')
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
123 self.Refresh(False)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
124 ev.Skip()
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
125
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
126 def processKillFocus(self, ev):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
127 # print('processKillFocus')
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
128 self.Refresh(False)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
129 ev.Skip()
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
130 # def processIdle(self, event):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
131 # print('processIdle')
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
132 # event.Skip()
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
133
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
134 def Layout(self):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
135 return super().Layout()
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
136
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
137 def Refresh(self, eraseback=True):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
138 # print('Refresh')
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
139 return super().Refresh(eraseback)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
140
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
141 def OnScrollSize(self, event):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
142 self.canvas.SetSize(event.Size)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
143
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
144 def processEraseBackgroundEvent(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
145 '''Process the erase background event.'''
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
146 pass # Do nothing, to avoid flashing on MSWin
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
147
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
148 def processSizeEvent(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
149 '''Process the resize event.'''
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
150
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
151 # print('processSizeEvent frozen', self.IsFrozen(), event.Size.x, self.ClientSize.x)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
152 if not self.IsFrozen() and self.canvas.IsShownOnScreen():
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
153 # Make sure the frame is shown before calling SetCurrent.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
154 self.canvas.SetCurrent(self.context)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
155 self.OnReshape()
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
156
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
157 # self.Refresh(False)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
158 # print('Refresh')
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
159 event.Skip()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
160
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
161 def processPaintEvent(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
162 '''Process the drawing event.'''
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
163 # print('wxGLPanel.processPaintEvent', self.ClientSize.Width)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
164 self.canvas.SetCurrent(self.context)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
165
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
166 if not self.gl_broken:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
167 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
168 self.OnInitGL()
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
169 self.DrawCanvas()
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
170 except pyglet.gl.lib.GLException:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
171 self.gl_broken = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
172 logging.error(_("OpenGL failed, disabling it:")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
173 + "\n" + traceback.format_exc())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
174 event.Skip()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
175
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
176 def Destroy(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
177 # clean up the pyglet OpenGL context
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
178 self.pygletcontext.destroy()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
179 # call the super method
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
180 super().Destroy()
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
181
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
182 # ==========================================================================
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
183 # GLFrame OpenGL Event Handlers
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
184 # ==========================================================================
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
185 def OnInitGL(self, call_reshape = True):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
186 '''Initialize OpenGL for use in the window.'''
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
187 if self.GLinitialized:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
188 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
189 self.GLinitialized = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
190 # create a pyglet context for this panel
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
191 self.pygletcontext = gl.Context(gl.current_context)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
192 self.pygletcontext.canvas = self
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
193 self.pygletcontext.set_current()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
194 # normal gl init
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
195 glClearColor(*self.color_background)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
196 glClearDepth(1.0) # set depth value to 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
197 glDepthFunc(GL_LEQUAL)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
198 glEnable(GL_COLOR_MATERIAL)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
199 glEnable(GL_DEPTH_TEST)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
200 glEnable(GL_CULL_FACE)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
201 glEnable(GL_BLEND)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
202 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
203 if call_reshape:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
204 self.OnReshape()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
205
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
206 def OnReshape(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
207 """Reshape the OpenGL viewport based on the size of the window"""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
208 size = self.GetClientSize()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
209 oldwidth, oldheight = self.width, self.height
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
210 width, height = size.width, size.height
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
211 if width < 1 or height < 1:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
212 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
213 self.width = max(float(width), 1.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
214 self.height = max(float(height), 1.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
215 self.OnInitGL(call_reshape = False)
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
216 # print('glViewport', width)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
217 glViewport(0, 0, width, height)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
218 glMatrixMode(GL_PROJECTION)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
219 glLoadIdentity()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
220 if self.orthographic:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
221 glOrtho(-width / 2, width / 2, -height / 2, height / 2,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
222 -5 * self.dist, 5 * self.dist)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
223 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
224 gluPerspective(60., float(width) / height, 10.0, 3 * self.dist)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
225 glTranslatef(0, 0, -self.dist) # Move back
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
226 glMatrixMode(GL_MODELVIEW)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
227
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
228 if not self.mview_initialized:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
229 self.reset_mview(0.9)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
230 self.mview_initialized = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
231 elif oldwidth is not None and oldheight is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
232 wratio = self.width / oldwidth
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
233 hratio = self.height / oldheight
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
234
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
235 factor = min(wratio * self.zoomed_width, hratio * self.zoomed_height)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
236 x, y, _ = self.mouse_to_3d(self.width / 2, self.height / 2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
237 self.zoom(factor, (x, y))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
238 self.zoomed_width *= wratio / factor
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
239 self.zoomed_height *= hratio / factor
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
240
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
241 # Wrap text to the width of the window
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
242 if self.GLinitialized:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
243 self.pygletcontext.set_current()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
244 self.update_object_resize()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
245
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
246 def setup_lights(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
247 if not self.do_lights:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
248 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
249 glEnable(GL_LIGHTING)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
250 glDisable(GL_LIGHT0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
251 glLightfv(GL_LIGHT0, GL_AMBIENT, vec(0.4, 0.4, 0.4, 1.0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
252 glLightfv(GL_LIGHT0, GL_SPECULAR, vec(0, 0, 0, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
253 glLightfv(GL_LIGHT0, GL_DIFFUSE, vec(0, 0, 0, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
254 glEnable(GL_LIGHT1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
255 glLightfv(GL_LIGHT1, GL_AMBIENT, vec(0, 0, 0, 1.0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
256 glLightfv(GL_LIGHT1, GL_SPECULAR, vec(0.6, 0.6, 0.6, 1.0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
257 glLightfv(GL_LIGHT2, GL_DIFFUSE, vec(0.8, 0.8, 0.8, 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
258 glLightfv(GL_LIGHT1, GL_POSITION, vec(1, 2, 3, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
259 glEnable(GL_LIGHT2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
260 glLightfv(GL_LIGHT2, GL_AMBIENT, vec(0, 0, 0, 1.0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
261 glLightfv(GL_LIGHT2, GL_SPECULAR, vec(0.6, 0.6, 0.6, 1.0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
262 glLightfv(GL_LIGHT2, GL_DIFFUSE, vec(0.8, 0.8, 0.8, 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
263 glLightfv(GL_LIGHT2, GL_POSITION, vec(-1, -1, 3, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
264 glEnable(GL_NORMALIZE)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
265 glShadeModel(GL_SMOOTH)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
266
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
267 def reset_mview(self, factor):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
268 glMatrixMode(GL_MODELVIEW)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
269 glLoadIdentity()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
270 self.setup_lights()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
271 if self.orthographic:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
272 wratio = self.width / self.dist
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
273 hratio = self.height / self.dist
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
274 minratio = float(min(wratio, hratio))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
275 self.zoom_factor = 1.0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
276 self.zoomed_width = wratio / minratio
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
277 self.zoomed_height = hratio / minratio
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
278 glScalef(factor * minratio, factor * minratio, 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
279
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
280 def DrawCanvas(self):
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
281 """Draw the window."""
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
282 #import time
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
283 #start = time.perf_counter()
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
284 # print('DrawCanvas', self.canvas.GetClientRect())
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
285 self.pygletcontext.set_current()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
286 glClearColor(*self.color_background)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
287 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
288 self.draw_objects()
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
289
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
290 if self.canvas.HasFocus():
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
291 self.drawFocus()
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
292 self.canvas.SwapBuffers()
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
293 #print('Draw took', '%.2f'%(time.perf_counter()-start))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
294
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
295 def drawFocus(self):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
296 glColor4f(0, 0, 0, 0.4)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
297
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
298 glPushMatrix()
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
299 glLoadIdentity()
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
300
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
301 glMatrixMode(GL_PROJECTION)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
302 glPushMatrix()
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
303 glLoadIdentity()
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
304 gluOrtho2D(0, self.width, 0, self.height)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
305
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
306 glLineStipple(1, 0xf0f0)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
307 glEnable(GL_LINE_STIPPLE)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
308 glBegin(GL_LINE_LOOP)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
309 glVertex2f(1, 0)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
310 glVertex2f(self.width, 0)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
311 glVertex2f(self.width, self.height-1)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
312 glVertex2f(1, self.height-1)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
313 glEnd()
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
314 glDisable(GL_LINE_STIPPLE)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
315
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
316 glPopMatrix() # restore PROJECTION
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
317
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
318 glMatrixMode(GL_MODELVIEW)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
319 glPopMatrix()
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
320
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
321 # ==========================================================================
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
322 # To be implemented by a sub class
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
323 # ==========================================================================
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
324 def create_objects(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
325 '''create opengl objects when opengl is initialized'''
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
326 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
327
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
328 def update_object_resize(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
329 '''called when the window recieves only if opengl is initialized'''
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
330 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
331
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
332 def draw_objects(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
333 '''called in the middle of ondraw after the buffer has been cleared'''
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
334 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
335
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
336 # ==========================================================================
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
337 # Utils
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
338 # ==========================================================================
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
339 def get_modelview_mat(self, local_transform):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
340 mvmat = (GLdouble * 16)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
341 glGetDoublev(GL_MODELVIEW_MATRIX, mvmat)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
342 return mvmat
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
343
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
344 def mouse_to_3d(self, x, y, z = 1.0, local_transform = False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
345 x = float(x)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
346 y = self.height - float(y)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
347 # The following could work if we were not initially scaling to zoom on
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
348 # the bed
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
349 # if self.orthographic:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
350 # return (x - self.width / 2, y - self.height / 2, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
351 pmat = (GLdouble * 16)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
352 mvmat = self.get_modelview_mat(local_transform)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
353 viewport = (GLint * 4)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
354 px = (GLdouble)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
355 py = (GLdouble)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
356 pz = (GLdouble)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
357 glGetIntegerv(GL_VIEWPORT, viewport)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
358 glGetDoublev(GL_PROJECTION_MATRIX, pmat)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
359 glGetDoublev(GL_MODELVIEW_MATRIX, mvmat)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
360 gluUnProject(x, y, z, mvmat, pmat, viewport, px, py, pz)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
361 return (px.value, py.value, pz.value)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
362
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
363 def mouse_to_ray(self, x, y, local_transform = False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
364 x = float(x)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
365 y = self.height - float(y)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
366 pmat = (GLdouble * 16)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
367 mvmat = (GLdouble * 16)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
368 viewport = (GLint * 4)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
369 px = (GLdouble)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
370 py = (GLdouble)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
371 pz = (GLdouble)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
372 glGetIntegerv(GL_VIEWPORT, viewport)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
373 glGetDoublev(GL_PROJECTION_MATRIX, pmat)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
374 mvmat = self.get_modelview_mat(local_transform)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
375 gluUnProject(x, y, 1, mvmat, pmat, viewport, px, py, pz)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
376 ray_far = (px.value, py.value, pz.value)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
377 gluUnProject(x, y, 0., mvmat, pmat, viewport, px, py, pz)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
378 ray_near = (px.value, py.value, pz.value)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
379 return ray_near, ray_far
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
380
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
381 def mouse_to_plane(self, x, y, plane_normal, plane_offset, local_transform = False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
382 # Ray/plane intersection
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
383 ray_near, ray_far = self.mouse_to_ray(x, y, local_transform)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
384 ray_near = numpy.array(ray_near)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
385 ray_far = numpy.array(ray_far)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
386 ray_dir = ray_far - ray_near
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
387 ray_dir = ray_dir / numpy.linalg.norm(ray_dir)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
388 plane_normal = numpy.array(plane_normal)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
389 q = ray_dir.dot(plane_normal)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
390 if q == 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
391 return None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
392 t = - (ray_near.dot(plane_normal) + plane_offset) / q
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
393 if t < 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
394 return None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
395 return ray_near + t * ray_dir
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
396
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
397 def zoom(self, factor, to = None):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
398 glMatrixMode(GL_MODELVIEW)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
399 if to:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
400 delta_x = to[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
401 delta_y = to[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
402 glTranslatef(delta_x, delta_y, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
403 glScalef(factor, factor, 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
404 self.zoom_factor *= factor
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
405 if to:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
406 glTranslatef(-delta_x, -delta_y, 0)
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
407 # For wxPython (<4.1) and GTK:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
408 # when you resize (enlarge) 3d view fast towards the log pane
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
409 # sash garbage may remain in GLCanvas
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
410 # The following refresh clears it at the cost of
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
411 # doubled frame draws.
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
412 # wx.CallAfter(self.Refresh)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
413 self.Refresh(False)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
414
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
415 def zoom_to_center(self, factor):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
416 self.canvas.SetCurrent(self.context)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
417 x, y, _ = self.mouse_to_3d(self.width / 2, self.height / 2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
418 self.zoom(factor, (x, y))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
419
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
420 def orbit(self, p1x, p1y, p2x, p2y):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
421 rz = p2x-p1x
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
422 self.angle_z-=rz
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
423 rotz = axis_to_quat([0.0,0.0,1.0],self.angle_z)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
424
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
425 rx = p2y-p1y
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
426 self.angle_x+=rx
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
427 rota = axis_to_quat([1.0,0.0,0.0],self.angle_x)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
428 return mulquat(rotz,rota)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
429
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
430 def handle_rotation(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
431 if self.initpos is None:
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
432 self.initpos = event.GetPosition()
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
433 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
434 p1 = self.initpos
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
435 p2 = event.GetPosition()
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
436 sz = self.GetClientSize()
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
437 p1x = p1[0] / (sz[0] / 2) - 1
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
438 p1y = 1 - p1[1] / (sz[1] / 2)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
439 p2x = p2[0] / (sz[0] / 2) - 1
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
440 p2y = 1 - p2[1] / (sz[1] / 2)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
441 quat = trackball(p1x, p1y, p2x, p2y, self.dist / 250.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
442 with self.rot_lock:
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
443 if self.orbit_control:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
444 self.basequat = self.orbit(p1x, p1y, p2x, p2y)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
445 else:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
446 self.basequat = mulquat(self.basequat, quat)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
447 self.initpos = p2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
448
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
449 def handle_translation(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
450 if self.initpos is None:
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
451 self.initpos = event.GetPosition()
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
452 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
453 p1 = self.initpos
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
454 p2 = event.GetPosition()
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
455 if self.orthographic:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
456 x1, y1, _ = self.mouse_to_3d(p1[0], p1[1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
457 x2, y2, _ = self.mouse_to_3d(p2[0], p2[1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
458 glTranslatef(x2 - x1, y2 - y1, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
459 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
460 glTranslatef(p2[0] - p1[0], -(p2[1] - p1[1]), 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
461 self.initpos = p2

mercurial