SHARE
TWEET

Untitled

a guest Apr 23rd, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python3
  2.  
  3. import pyglet
  4. import glooey
  5. import trimesh
  6.  
  7. from math import pi
  8. from pyglet.gl import *
  9.  
  10. class SceneGroup(pyglet.graphics.Group):
  11.  
  12.     def __init__(self, rect, parent=None):
  13.         super().__init__(parent)
  14.         self.rect = rect
  15.  
  16.     def set_state(self):
  17.         glPushAttrib(GL_ENABLE_BIT)
  18.         glEnable(GL_SCISSOR_TEST)
  19.         glScissor(
  20.             int(self.rect.left),
  21.             int(self.rect.bottom),
  22.             int(self.rect.width),
  23.             int(self.rect.height),
  24.         )
  25.  
  26.         self._mode = (GLint)()
  27.         glGetIntegerv(GL_MATRIX_MODE, self._mode)
  28.         self._viewport = (GLint * 4)()
  29.         glGetIntegerv(GL_VIEWPORT, self._viewport)
  30.  
  31.         left, bottom = int(self.rect.left), int(self.rect.bottom)
  32.         width, height = int(self.rect.width), int(self.rect.height)
  33.         glViewport(left, bottom, width, height)
  34.         glMatrixMode(GL_PROJECTION)
  35.         glPushMatrix()
  36.         glLoadIdentity()
  37.         gluPerspective(60, width / height, 0.01, 1000.0)
  38.         glMatrixMode(GL_MODELVIEW)
  39.  
  40.         self._enable_depth()
  41.         self._enable_color_material()
  42.         self._enable_blending()
  43.         self._enable_smooth_lines()
  44.         self._enable_lighting()
  45.         self._clear_buffers()
  46.  
  47.     def unset_state(self):
  48.         glMatrixMode(GL_PROJECTION)
  49.         glPopMatrix()
  50.         glMatrixMode(self._mode.value)
  51.         glViewport(
  52.             self._viewport[0],
  53.             self._viewport[1],
  54.             self._viewport[2],
  55.             self._viewport[3],
  56.         )
  57.  
  58.         glPopAttrib()
  59.  
  60.     def _enable_depth(self):
  61.         glEnable(GL_DEPTH_TEST)
  62.         glEnable(GL_CULL_FACE)
  63.  
  64.         glDepthRange(0.0, 100.0)
  65.         glDepthFunc(GL_LEQUAL)
  66.         glClearDepth(1.0)
  67.  
  68.     def _enable_color_material(self):
  69.         from trimesh.rendering import vector_to_gl as v
  70.  
  71.         glEnable(GL_COLOR_MATERIAL)
  72.  
  73.         glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
  74.         glShadeModel(GL_SMOOTH)
  75.  
  76.         glMaterialfv(GL_FRONT, GL_AMBIENT,  v(0.192250, 0.192250, 0.192250))
  77.         glMaterialfv(GL_FRONT, GL_DIFFUSE,  v(0.507540, 0.507540, 0.507540))
  78.         glMaterialfv(GL_FRONT, GL_SPECULAR, v(0.5082730, .5082730, .5082730))
  79.         glMaterialf(GL_FRONT, GL_SHININESS, 0.4 * 128.0)
  80.  
  81.     def _enable_blending(self):
  82.         glEnable(GL_BLEND)
  83.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
  84.  
  85.     def _enable_smooth_lines(self):
  86.         # Make things generally less ugly.
  87.         glEnable(GL_LINE_SMOOTH)
  88.         glHint(GL_LINE_SMOOTH_HINT, GL_NICEST)
  89.         glLineWidth(1.5)
  90.         glPointSize(4)
  91.  
  92.     def _enable_lighting(self):
  93.         from trimesh.rendering import vector_to_gl as v
  94.  
  95.         glEnable(GL_LIGHTING)
  96.         glEnable(GL_LIGHT0)
  97.  
  98.         glLightfv(GL_LIGHT0, GL_AMBIENT,  v(0.5, 0.5, 0.5, 1.0))
  99.         glLightfv(GL_LIGHT0, GL_DIFFUSE,  v(1.0, 1.0, 1.0, 1.0))
  100.         glLightfv(GL_LIGHT0, GL_SPECULAR, v(1.0, 1.0, 1.0, 1.0))
  101.         glLightfv(GL_LIGHT0, GL_POSITION, v(0.0, 0.0, 0.0, 1.0))
  102.  
  103.     def _clear_buffers(self):
  104.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  105.  
  106.  
  107. class SpinGroup(pyglet.graphics.Group):
  108.     # This group just rotates things along the y-axis.  It's not really
  109.     # necessary, but it was helpful for me when debugging.
  110.  
  111.     def __init__(self, parent=None):
  112.         super().__init__(parent)
  113.         self.angle_radians = 0
  114.  
  115.     def set_state(self):
  116.         from trimesh.rendering import matrix_to_gl
  117.         from trimesh.transformations import \
  118.                 translation_matrix, rotation_matrix, concatenate_matrices
  119.  
  120.         glPushMatrix()
  121.  
  122.         glLoadIdentity()
  123.         T = translation_matrix([0, 0, -10])
  124.         R = rotation_matrix(self.angle_radians, [0, 1, 0])
  125.         M = concatenate_matrices(T, R)
  126.         glMultMatrixf(matrix_to_gl(M))
  127.  
  128.     def unset_state(self):
  129.         glPopMatrix()
  130.  
  131.  
  132. class SceneWidget(glooey.Widget):
  133.  
  134.     # I suppose this should take a scene object rather than a mesh, but I
  135.     # couldn't really figure out how to make a scene object.
  136.     def __init__(self, mesh):
  137.         super().__init__()
  138.         self.mesh = mesh
  139.         self.vertex_list = None
  140.         self.spin_group = None
  141.  
  142.     def do_attach(self):
  143.         pyglet.clock.schedule_interval(self.on_update, 1/60)
  144.  
  145.     def do_detach(self):
  146.         pyglet.clock.unschedule(self.on_update)
  147.  
  148.     def do_claim(self):
  149.         return 0, 0
  150.  
  151.     def do_regroup(self):
  152.         if self.vertex_list is not None:
  153.             self.spin_group = SpinGroup(
  154.                 SceneGroup(rect=self.rect, parent=self.group)
  155.             )
  156.             self.batch.migrate(
  157.                     self.vertex_list, GL_TRIANGLES,
  158.                     self.spin_group, self.batch,
  159.             )
  160.  
  161.     def do_draw(self):
  162.         from trimesh.rendering import mesh_to_vertexlist
  163.  
  164.         # Because the vertex list can't change, we don't need to do anything if
  165.         # the vertex list is already set.
  166.         if self.vertex_list is None:
  167.             self.spin_group = SpinGroup(
  168.                 SceneGroup(rect=self.rect, parent=self.group)
  169.             )
  170.             args = mesh_to_vertexlist(self.mesh, group=self.spin_group)
  171.             self.vertex_list = self.batch.add_indexed(*args)
  172.  
  173.     def do_undraw(self):
  174.         if self.vertex_list is not None:
  175.             self.vertex_list.delete()
  176.             self.vertex_list = None
  177.  
  178.     def on_update(self, dt):
  179.         if self.spin_group:
  180.             self.spin_group.angle_radians += (2 * pi / 8) * dt
  181.         self._draw()
  182.  
  183. if __name__ == '__main__':
  184.     window = pyglet.window.Window(width=1280, height=480)
  185.     gui = glooey.Gui(window)
  186.  
  187.     hbox = glooey.HBox()
  188.  
  189.     # hbox.add(glooey.Placeholder(min_width=640, min_height=480))
  190.     mesh = trimesh.creation.annulus(0.2, 1, 0.2)
  191.     scene = SceneWidget(mesh)
  192.     hbox.add(scene)
  193.  
  194.     mesh = trimesh.creation.axis(0.3)
  195.     scene = SceneWidget(mesh)
  196.     hbox.add(scene)
  197.     # hbox.add(glooey.Placeholder(min_width=640, min_height=480))
  198.  
  199.     gui.add(hbox)
  200.  
  201.     pyglet.app.run()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top