presets.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # ***** BEGIN GPL LICENSE BLOCK *****
  2. #
  3. # This program is free software; you can redistribute it and/or
  4. # modify it under the terms of the GNU General Public License
  5. # as published by the Free Software Foundation; either version 2
  6. # of the License, or (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software Foundation,
  15. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. #
  17. # ***** END GPL LICENSE BLOCK *****
  18. def draw_circle_2d(position, color, radius, segments=32):
  19. """
  20. Draw a circle.
  21. :arg position: Position where the circle will be drawn.
  22. :type position: 2D Vector
  23. :arg color: Color of the circle. To use transparency GL_BLEND has to be enabled.
  24. :type color: tuple containing RGBA values
  25. :arg radius: Radius of the circle.
  26. :type radius: float
  27. :arg segments: How many segments will be used to draw the circle.
  28. Higher values give besser results but the drawing will take longer.
  29. :type segments: int
  30. """
  31. from math import sin, cos, pi
  32. import gpu
  33. from gpu.types import (
  34. GPUBatch,
  35. GPUVertBuf,
  36. GPUVertFormat,
  37. )
  38. if segments <= 0:
  39. raise ValueError("Amount of segments must be greater than 0.")
  40. with gpu.matrix.push_pop():
  41. gpu.matrix.translate(position)
  42. gpu.matrix.scale_uniform(radius)
  43. mul = (1.0 / (segments - 1)) * (pi * 2)
  44. verts = [(sin(i * mul), cos(i * mul)) for i in range(segments)]
  45. fmt = GPUVertFormat()
  46. pos_id = fmt.attr_add(id="pos", comp_type='F32', len=2, fetch_mode='FLOAT')
  47. vbo = GPUVertBuf(len=len(verts), format=fmt)
  48. vbo.attr_fill(id=pos_id, data=verts)
  49. batch = GPUBatch(type='LINE_STRIP', buf=vbo)
  50. shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR')
  51. batch.program_set(shader)
  52. shader.uniform_float("color", color)
  53. batch.draw()
  54. def draw_texture_2d(texture_id, position, width, height):
  55. """
  56. Draw a 2d texture.
  57. :arg texture_id: OpenGL id of the texture (e.g. :class:`bpy.types.Image.bindcode`).
  58. :type texture_id: int
  59. :arg position: Position of the lower left corner.
  60. :type position: 2D Vector
  61. :arg width: Width of the image when drawn (not necessarily
  62. the original width of the texture).
  63. :type width: float
  64. :arg height: Height of the image when drawn.
  65. :type height: float
  66. """
  67. import gpu
  68. import bgl
  69. from . batch import batch_for_shader
  70. coords = ((0, 0), (1, 0), (1, 1), (0, 1))
  71. shader = gpu.shader.from_builtin('2D_IMAGE')
  72. batch = batch_for_shader(shader, 'TRI_FAN',
  73. {"pos" : coords,
  74. "texCoord" : coords})
  75. bgl.glActiveTexture(bgl.GL_TEXTURE0)
  76. bgl.glBindTexture(bgl.GL_TEXTURE_2D, texture_id)
  77. with gpu.matrix.push_pop():
  78. gpu.matrix.translate(position)
  79. gpu.matrix.scale((width, height))
  80. shader = gpu.shader.from_builtin('2D_IMAGE')
  81. shader.bind()
  82. shader.uniform_int("image", 0)
  83. batch.draw(shader)