Guest User

Untitled

a guest
Nov 17th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. from kivy.app import App
  2. from kivy.clock import Clock
  3. from kivy.graphics import Mesh, RenderContext, Color, Callback
  4. from kivy.graphics.opengl import glDisable, glEnable, GL_DEPTH_TEST
  5. from kivy.graphics.transformation import Matrix
  6. from kivy.uix.floatlayout import FloatLayout
  7. from kivy.lang import Builder
  8. from kivy.properties import ObjectProperty
  9. from kivy.core.window import Window
  10.  
  11. vs3d = '''
  12. #ifdef GL_ES
  13. precision highp float;
  14. #endif
  15.  
  16. /* Outputs to the fragment shader */
  17. varying vec4 frag_color;
  18. varying vec2 tex_coord0;
  19.  
  20. /* vertex attributes */
  21. attribute vec3 vPosition;
  22. attribute vec4 vColor;
  23. attribute vec2 vTexCoords0;
  24.  
  25. /* uniform variables */
  26. uniform mat4 modelview_mat;
  27. uniform mat4 projection_mat;
  28. uniform vec4 color;
  29.  
  30. void main (void) {
  31. frag_color = color * vColor;
  32. tex_coord0 = vTexCoords0;
  33. gl_Position = projection_mat * modelview_mat * vec4(vPosition.xyz, 1.0);
  34. }
  35. '''
  36.  
  37. Builder.load_string('''
  38. <ViewControler>:
  39. GridLayout:
  40. size_hint_x: .2
  41. cols: 2
  42. Label:
  43. text: 'fovy'
  44. Slider:
  45. value: 45.
  46. on_value: root.view.params['fovy'] = args[1]
  47. Label:
  48. text: 'aspect'
  49. Slider:
  50. value: 1.
  51. max: 2.
  52. on_value: root.view.params['aspect'] = args[1]
  53. Label:
  54. text: 'zNear'
  55. Slider:
  56. value: 0.1
  57. min: 1
  58. max: 10
  59. on_value: root.view.params['zNear'] = args[1]
  60. Label:
  61. text: 'zMax'
  62. Slider:
  63. value: 1000.
  64. min: 100
  65. max: 1000
  66. on_value: root.view.params['zFar'] = args[1]
  67. ''')
  68.  
  69. class ViewControler(FloatLayout):
  70. view = ObjectProperty(None)
  71.  
  72. class View3D(FloatLayout):
  73. def __init__(self, **kwargs):
  74. self.params = {}
  75. self.canvas = RenderContext()
  76. self.canvas.shader.vs = vs3d
  77. self.targetx = 0
  78. self.targety = 0
  79. super(View3D, self).__init__(**kwargs)
  80. Clock.schedule_interval(self.update_glsl, 0)
  81.  
  82. with self.canvas:
  83. Callback(self.activate_depthtest)
  84. Color(.8, 0, .7)
  85. self.create_3dcube(pos=(0, 0, 0), size=(100, 100, 100))
  86. Callback(self.deactivate_depthtest)
  87.  
  88. def activate_depthtest(self, instr):
  89. glEnable(GL_DEPTH_TEST)
  90.  
  91. def deactivate_depthtest(self, instr):
  92. glDisable(GL_DEPTH_TEST)
  93.  
  94. def create_3dcube(self, pos, size):
  95. x, y, z = pos
  96. w, h, p = size
  97. fmt = [('vPosition', 3, 'float'), ('vColor', 4, 'float')]
  98. vertices = [
  99. x, y, z, 1, 0, 0, 1,
  100. x + w, y, z, .5, .5, 0, 1,
  101. x + w, y + h, z, 0, 1, 0, 1,
  102. x, y + h, z, 0, .5, .5, 1,
  103. x, y, z + p, 0, 0, 1, 1,
  104. x + w, y, z + p, .5, 0, .5, 1,
  105. x + w, y + h, z + p, 0, 0, 0, 1,
  106. x, y + h, z + p, 1, 1, 1, 1 ]
  107. indices = [
  108. 0, 2, 1,
  109. 0, 3, 2,
  110. 5, 0, 1,
  111. 5, 4, 0,
  112. 5, 2, 6,
  113. 5, 1, 2,
  114. 4, 3, 0,
  115. 4, 7, 3,
  116. 2, 7, 6,
  117. 2, 3, 7,
  118. 5, 6, 4,
  119. 4, 6, 7]
  120.  
  121. Color(1, 1, 1)
  122. Mesh(vertices=vertices, indices=indices, fmt=fmt, mode='triangles')
  123.  
  124. vertices = [
  125. x, y, z, 1, 1, 1, 1,
  126. x + w, y, z, 1, 1, 1, 1,
  127. x + w, y + h, z, 1, 1, 1, 1,
  128. x, y + h, z, 1, 1, 1, 1,
  129. x, y, z + p, 1, 1, 1, 1,
  130. x + w, y, z + p, 1, 1, 1, 1,
  131. x + w, y + h, z + p, 1, 1, 1, 1,
  132. x, y + h, z + p, 1, 1, 1, 1]
  133. indices = [
  134. 0, 2, 2, 1, 1, 0, 0, 3, 3, 2,
  135. 1, 5, 2, 5, 2, 6, 6, 5,
  136. 0, 4, 4, 3, 3, 7, 7, 4,
  137. 7, 2, 0, 5, 4, 5, 7, 6, 4, 6]
  138.  
  139. Color(1, 1, 1)
  140. Mesh(vertices=vertices, indices=indices, fmt=fmt, mode='lines')
  141.  
  142. def update_glsl(self, dt):
  143. from kivy.core.window import Window
  144. g = self.params.get
  145. w, h = Window.system_size
  146. projection_mat = Matrix()
  147. #projection_mat.view_clip(0.0, w, 0.0, h, 10.0, 1000.0, 1)
  148. projection_mat.perspective(g('fovy', 45.), g('aspect', 1), g('zNear', 10), g('zFar', 1000))
  149. #projection_mat.view_clip(0.0, w, 0.0, h, -1, 1, 0)
  150. modelview_mat = Matrix().look_at(0, 200, -200, self.targetx, self.targety, 0, 0, 1, 0)
  151. self.canvas['projection_mat'] = projection_mat
  152. self.canvas['modelview_mat'] = modelview_mat
  153.  
  154. def on_touch_move(self, touch):
  155. print touch.dx, touch.dy
  156.  
  157. self.targetx -= touch.dx
  158. self.targety += touch.dy
  159. return True
  160.  
  161.  
  162. class View3DApp(App):
  163. def build(self):
  164. root = FloatLayout()
  165. view3d = View3D()
  166. root.add_widget(view3d)
  167. root.add_widget(ViewControler(view=view3d))
  168. return root
  169.  
  170. if __name__ == '__main__':
  171. View3DApp().run()
Add Comment
Please, Sign In to add comment