/usr/share/pymt-examples/apps/3Dviewer/3Dviewer.py is in python-pymt 0.5.1-0ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | from __future__ import with_statement
# PYMT Plugin integration
IS_PYMT_PLUGIN = True
PLUGIN_TITLE = '3D Viewer'
PLUGIN_AUTHOR = 'Thomas Hansen'
PLUGIN_EMAIL = 'thomas.hansen@gmail.com'
import os
from pymt import *
from OpenGL.GL import *
from OpenGL.GLU import *
current_dir = os.path.dirname(__file__)
class GLPerspectiveWidget(MTWidget):
"""Sets up 3d projection in on_draw function and then calls seld.draw, origin in the center"""
def __init__(self, **kargs):
super(GLPerspectiveWidget, self).__init__(**kargs)
self.needs_redisplay = True
self.fbo = Fbo(size=self.size)
def on_resize(self, w, h):
self.size = w, h
del self.fbo
self.fbo = Fbo(size=self.size)
self.needs_redisplay = True
def on_draw(self):
if self.needs_redisplay:
with self.fbo:
self.draw3D()
glColor3f(1,1,1)
drawTexturedRectangle(self.fbo.texture, size=self.size)
def draw3D(self):
glPushAttrib(GL_VIEWPORT_BIT)
glViewport(0,0,self.fbo.size[0], self.fbo.size[1])
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0)
glEnable(GL_NORMALIZE)
glEnable(GL_DEPTH_TEST)
glMatrixMode(GL_PROJECTION)
glPushMatrix()
glLoadIdentity()
gluPerspective(60.,self.width/float(self.height) , 1., 100.)
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glLoadIdentity()
glTranslatef(0.0,0.0,-3.0)
self.draw()
glMatrixMode(GL_MODELVIEW)
glPopMatrix()
glMatrixMode(GL_PROJECTION)
glPopMatrix()
glMatrixMode(GL_MODELVIEW)
glDisable(GL_DEPTH_TEST)
glDisable(GL_LIGHTING)
glDisable(GL_LIGHT0)
glPopAttrib()
self.needs_redisplay = False
class ModelViewer(GLPerspectiveWidget):
def __init__(self, **kargs):
GLPerspectiveWidget.__init__(self, **kargs)
self.touch_position = {}
self.model = bunny = OBJ(os.path.join(current_dir, 'monkey.obj'))
self.rotation_matrix = None
self.reset_rotation()
self.touch1, self.touch2 = None, None
self.zoom = 1.0
def reset_rotation(self):
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glLoadIdentity()
self.rotation_matrix = glGetFloatv(GL_MODELVIEW_MATRIX)
glPopMatrix()
def draw(self):
glMultMatrixf(self.rotation_matrix)
#glRotatef(180.0, 0,0,1)
glRotatef(90.0, 1,0,0)
glScalef(self.zoom, self.zoom, self.zoom)
self.model.draw()
def check_touches(self, touches):
if self.touch1 and not self.touch1 in touches:
if self.touch1 in self.touch_position:
del self.touch_position[self.touch1]
self.touch1 = None
if self.touch2 and not self.touch2 in touches:
if self.touch2 in self.touch_position:
del self.touch_position[self.touch2]
self.touch2 = None
def on_touch_down(self, touch):
self.check_touches(getCurrentTouches())
self.touch_position[touch.id] = (touch.x, touch.y)
touch.grab(self)
if len(self.touch_position) == 1:
self.touch1 = touch
elif len(self.touch_position) == 2:
self.touch2 = touch
if self.touch1 and self.touch2 and \
self.touch1.id in self.touch_position and \
self.touch2.id in self.touch_position:
v1 = Vector(*self.touch_position[self.touch1.id])
v2 = Vector(*self.touch_position[self.touch2.id])
self.scale_dist = v1.distance(v2)
def on_touch_move(self, touch):
if not touch.grab_current == self:
return
self.check_touches(getCurrentTouches())
dx, dy = 0,0
scale = 1.0
angle = 0.0
if self.touch1 and self.touch2 and \
self.touch1.id in self.touch_position and \
self.touch2.id in self.touch_position:
v1 = Vector(*self.touch_position[self.touch1.id])
v2 = Vector(*self.touch_position[self.touch2.id])
new_dist = v1.distance(v2)
zoomfactor = new_dist/self.scale_dist
if zoomfactor > 1.0:
zoomfactor = 1.0 + (zoomfactor - 1.0) * 0.5
else:
zoomfactor = 1.0 - abs(1.0 - zoomfactor ) * 0.5
self.zoom *= zoomfactor
self.scale_dist = new_dist
# compute rotation angle
old_line = v1 - v2
new_line = None
if self.touch1.id == touch.id:
new_line = Vector(touch.x, touch.y) - v2
else:
new_line = v1 - Vector(touch.x, touch.y)
angle = -1.0 * old_line.angle(new_line)
elif touch.id in self.touch_position:
dx = 200.0*(touch.x-self.touch_position[touch.id][0])/float(self.width)
dy = 200.0*(touch.y-self.touch_position[touch.id][1])/float(self.height)
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glLoadIdentity()
glRotatef(angle, 0,0,1)
glRotatef(dx, 0,1,0)
glRotatef(-dy, 1,0,0)
glMultMatrixf(self.rotation_matrix)
self.rotation_matrix = glGetFloatv(GL_MODELVIEW_MATRIX)
glPopMatrix()
if touch.id in self.touch_position:
self.touch_position[touch.id] = (touch.x, touch.y)
self.needs_redisplay = True
def on_touch_up(self, touch):
if not touch.grab_current == self:
return
self.check_touches(getCurrentTouches())
if touch.id in self.touch_position:
del self.touch_position[touch.id]
self.needs_redisplay = True
def pymt_plugin_activate(root, ctx):
ctx.mv = ModelViewer(size=(root.width,root.height))
root.add_widget(ctx.mv)
def pymt_plugin_deactivate(root, ctx):
root.remove_widget(ctx.mv)
if __name__ == '__main__':
w = MTWindow()
ctx = MTContext()
pymt_plugin_activate(w, ctx)
runTouchApp()
pymt_plugin_deactivate(w, ctx)
|