-
Notifications
You must be signed in to change notification settings - Fork 351
Update the arcade window so it resets gl context properly everyframe. #2752
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DragonMoffon
wants to merge
5
commits into
development
Choose a base branch
from
camera-context-reset
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
121e601
Move viewport camera to it's own file
DragonMoffon c1f4d29
Update context's reset method to use the default camera protecting th…
DragonMoffon 79dc705
Reset context state before all draw calls
DragonMoffon 5c5611a
viewport hack to force the default camera to update it's viewport.
DragonMoffon 2a22c41
linting
DragonMoffon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
from __future__ import annotations | ||
|
||
from collections.abc import Generator | ||
from contextlib import contextmanager | ||
from typing import TYPE_CHECKING | ||
|
||
from pyglet.math import Mat4, Vec2, Vec3 | ||
from typing_extensions import Self | ||
|
||
from arcade.types import LBWH, Point, Rect | ||
from arcade.window_commands import get_window | ||
|
||
if TYPE_CHECKING: | ||
from arcade.context import ArcadeContext | ||
|
||
__all__ = ("ViewportProjector",) | ||
|
||
|
||
class ViewportProjector: | ||
""" | ||
A simple Projector which does not rely on any camera PoDs. | ||
|
||
Does not have a way of moving, rotating, or zooming the camera. | ||
perfect for something like UI or for mapping to an offscreen framebuffer. | ||
|
||
Args: | ||
viewport: The viewport to project to. | ||
context: The window context to bind the camera to. Defaults to the currently active window. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
viewport: Rect | None = None, | ||
*, | ||
context: ArcadeContext | None = None, | ||
): | ||
self._ctx: ArcadeContext = context or get_window().ctx | ||
self._viewport: Rect = viewport or LBWH(*self._ctx.viewport) | ||
self._projection_matrix: Mat4 = Mat4.orthogonal_projection( | ||
0.0, self._viewport.width, 0.0, self._viewport.height, -100, 100 | ||
) | ||
|
||
@property | ||
def viewport(self) -> Rect: | ||
""" | ||
The viewport use to derive projection and view matrix. | ||
""" | ||
return self._viewport | ||
|
||
@viewport.setter | ||
def viewport(self, viewport: Rect) -> None: | ||
self._viewport = viewport | ||
self._projection_matrix = Mat4.orthogonal_projection( | ||
0, viewport.width, 0, viewport.height, -100, 100 | ||
) | ||
|
||
def use(self) -> None: | ||
""" | ||
Set the window's projection and view matrix. | ||
Also sets the projector as the windows current camera. | ||
""" | ||
self._ctx.current_camera = self | ||
|
||
self._ctx.viewport = self.viewport.lbwh_int # get the integer 4-tuple LBWH | ||
|
||
self._ctx.view_matrix = Mat4() | ||
self._ctx.projection_matrix = self._projection_matrix | ||
|
||
@contextmanager | ||
def activate(self) -> Generator[Self, None, None]: | ||
""" | ||
The context manager version of the use method. | ||
|
||
usable with the 'with' block. e.g. 'with ViewportProjector.activate() as cam: ...' | ||
""" | ||
previous = self._ctx.current_camera | ||
try: | ||
self.use() | ||
yield self | ||
finally: | ||
previous.use() | ||
|
||
def project(self, world_coordinate: Point) -> Vec2: | ||
""" | ||
Take a Vec2 or Vec3 of coordinates and return the related screen coordinate | ||
""" | ||
x, y, *z = world_coordinate | ||
return Vec2(x, y) | ||
|
||
def unproject(self, screen_coordinate: Point) -> Vec3: | ||
""" | ||
Map the screen pos to screen_coordinates. | ||
|
||
Due to the nature of viewport projector this does not do anything. | ||
""" | ||
x, y, *_z = screen_coordinate | ||
z = 0.0 if not _z else _z[0] | ||
|
||
return Vec3(x, y, z) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reset method was more for unit tests. We can still probably trim it down. These lines should definitly not be called every frame but they should be called in conftest between tests except that blending needs to disable instead.
I also don't know if it's necessary to bind the windowblock every frame but that should be done in unit tests.