gpu.7.py 778 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. """
  2. 2D Image
  3. --------
  4. To use this example you have to provide an image that should be displayed.
  5. """
  6. import bpy
  7. import gpu
  8. import bgl
  9. from gpu_extras.batch import batch_for_shader
  10. IMAGE_NAME = "Untitled"
  11. image = bpy.data.images[IMAGE_NAME]
  12. shader = gpu.shader.from_builtin('2D_IMAGE')
  13. batch = batch_for_shader(
  14. shader, 'TRI_FAN',
  15. {
  16. "pos": ((100, 100), (200, 100), (200, 200), (100, 200)),
  17. "texCoord": ((0, 0), (1, 0), (1, 1), (0, 1)),
  18. },
  19. )
  20. if image.gl_load():
  21. raise Exception()
  22. def draw():
  23. bgl.glActiveTexture(bgl.GL_TEXTURE0)
  24. bgl.glBindTexture(bgl.GL_TEXTURE_2D, image.bindcode)
  25. shader.bind()
  26. shader.uniform_int("image", 0)
  27. batch.draw(shader)
  28. bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_PIXEL')