gl.py 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # coding: utf-8
  2. import typing
  3. import pyglet
  4. rectangle_positions_type = typing.Union[
  5. typing.Tuple[
  6. typing.Tuple[int, int], typing.Tuple[int, int], typing.Tuple[int, int], typing.Tuple[int, int]
  7. ],
  8. typing.List[typing.Tuple[int, int]],
  9. ]
  10. rgb_type = typing.Tuple[int, int, int]
  11. rgb_alpha_type = typing.Tuple[int, int, int, float]
  12. def draw_rectangle(
  13. positions: rectangle_positions_type,
  14. around_color: rgb_type,
  15. fill_color: typing.Optional[rgb_alpha_type]=None,
  16. ):
  17. """
  18. A<---D
  19. | |
  20. B--->C
  21. """
  22. pyglet.gl.glColor3ub(*around_color)
  23. pyglet.gl.glBegin(pyglet.gl.GL_LINE_STRIP)
  24. for v in positions:
  25. pyglet.gl.glVertex2f(*v)
  26. pyglet.gl.glVertex2f(*positions[0])
  27. pyglet.gl.glEnd()
  28. if fill_color:
  29. pyglet.gl.glColor4f(*fill_color)
  30. pyglet.gl.glBegin(pyglet.gl.GL_QUADS)
  31. pyglet.gl.glVertex3f(positions[0][0], positions[0][1], 0)
  32. pyglet.gl.glVertex3f(positions[1][0], positions[1][1], 0)
  33. pyglet.gl.glVertex3f(positions[2][0], positions[2][1], 0)
  34. pyglet.gl.glVertex3f(positions[3][0], positions[3][1], 0)
  35. pyglet.gl.glEnd()
  36. def draw_line(
  37. from_position: typing.Tuple[int, int],
  38. to_position: typing.Tuple[int, int],
  39. color: rgb_type,
  40. width: typing.Optional[int]=1,
  41. ):
  42. pyglet.gl.glColor3ub(*color)
  43. pyglet.gl.glLineWidth(width)
  44. pyglet.graphics.draw(
  45. 4,
  46. pyglet.gl.GL_LINES,
  47. ("v2f", (0, 0, 0, 0, from_position[0], from_position[1], to_position[0], to_position[1]))
  48. )