Skip to content

Commit

Permalink
geometry: screen rectangle
Browse files Browse the repository at this point in the history
  • Loading branch information
einarf committed Jun 3, 2020
1 parent ac35990 commit d8bb1a2
Showing 1 changed file with 21 additions and 51 deletions.
72 changes: 21 additions & 51 deletions arcade/gl/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,54 +46,24 @@ def quad_2d(size: Tuple[float, float] = (1.0, 1.0), pos: Tuple[float, float] = (
)], mode=ctx.TRIANGLE_STRIP)


def screen_rectangle(left: float, right: float, top: float, bottom: float) -> Geometry:
"""Create a screen rectangle by specifying left, right, top, and bottom edges"""
width, height = right - left, top - bottom
x_pos, y_pos = left + width / 2, right + height / 2
print(f"size=({width}, {height}), pos=({x_pos}, {y_pos})")

return quad_2d(pos=(x_pos, y_pos), size=(width, height))


# def screen_rectangle(rectangle_size=None,
# center_pos=None,
# screen_size=None):
# """ Calculate a rectangle """

# if rectangle_size is None and center_pos is None and screen_size is None:
# rectangle_size = (2.0, 2.0)
# center_pos = (0.0, 0.0)
# screen_size = (2.0, 2.0)
# elif screen_size is None:
# screen_size = (get_window().width, get_window().height)
# if center_pos is None:
# center_pos = (get_window().width / 2.0, get_window().height / 2.0)
# print(f"Size: {screen_size}")
# screen_width, screen_height = screen_size
# quad_width, quad_height = rectangle_size
# normalized_width = quad_width / screen_width * 2
# normalized_height = quad_height / screen_height * 2
# xpos, ypos = center_pos
# normalized_xpos = xpos / (screen_width / 2) - 1
# normalized_ypos = ypos / (screen_height / 2) - 1
# my_array = \
# [
# normalized_xpos - normalized_width / 2, normalized_ypos + normalized_height / 2, 0.0, 1.0,
# normalized_xpos - normalized_width / 2, normalized_ypos - normalized_height / 2, 0.0, 0.0,
# normalized_xpos + normalized_width / 2, normalized_ypos - normalized_height / 2, 1.0, 0.0,
# normalized_xpos - normalized_width / 2, normalized_ypos + normalized_height / 2, 0.0, 1.0,
# normalized_xpos + normalized_width / 2, normalized_ypos - normalized_height / 2, 1.0, 0.0,
# normalized_xpos + normalized_width / 2, normalized_ypos + normalized_height / 2, 1.0, 1.0
# ]
# data = array.array('f', my_array)

# ctx = get_window().ctx
# vbo = ctx.buffer(data=data)
# vao_content = [
# BufferDescription(
# vbo,
# '2f 2f',
# ('in_vert', 'in_uv'),
# )
# ]
# return ctx.geometry(vao_content)
def screen_rectangle(bottom_left_x: float, bottom_left_y: float, width: float, height: float) -> Geometry:
"""
Creates screen rectangle using 2 triangle strip with texture coordinates.
:param float bottom_left_x: Bottom left x position
:param float bottom_left_y: Bottom left y position
:param float width: Width of the rectangle
:param float height: Height of the rectangle
"""
ctx = _get_active_context()
data = array.array('f', [
bottom_left_x, bottom_left_y + height, 0.0, 1.0,
bottom_left_x, bottom_left_y, 0.0, 0.0,
bottom_left_x + width, bottom_left_y + height, 1.0, 1.0,
bottom_left_x + width, bottom_left_y, 1.0, 0.0,
])
return ctx.geometry([BufferDescription(
ctx.buffer(data=data),
'2f 2f',
['in_vert', 'in_uv'],
)], mode=ctx.TRIANGLE_STRIP)

0 comments on commit d8bb1a2

Please sign in to comment.