From 1db836890f4ab253a794aaa044a13f674ee39517 Mon Sep 17 00:00:00 2001 From: Peter Slattery Date: Sun, 6 Sep 2020 20:33:28 -0700 Subject: [PATCH] Cleaned up world -> panel space conversion --- .../panels/foldhaus_panel_sculpture_view.h | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/app/editor/panels/foldhaus_panel_sculpture_view.h b/src/app/editor/panels/foldhaus_panel_sculpture_view.h index 8ac383a..56d9ff0 100644 --- a/src/app/editor/panels/foldhaus_panel_sculpture_view.h +++ b/src/app/editor/panels/foldhaus_panel_sculpture_view.h @@ -131,6 +131,28 @@ DrawQuad(render_command_buffer* RenderBuffer, v4 C, r32 Rad, v4 Color) PushRenderQuad3D(RenderBuffer, P0, P1, P2, P3, Color); } +internal v2 +SculptureView_WorldToScreenPosition(v4 WorldPosition, camera Camera, rect2 PanelBounds) +{ + v2 Result = {0}; + + r32 PanelW = Rect2Width(PanelBounds); + r32 PanelH = Rect2Height(PanelBounds); + + m44 Matrix = GetCameraPerspectiveProjectionMatrix(Camera) * GetCameraModelViewMatrix(Camera); + v4 WorldPos = Matrix * WorldPosition; + + // this is the Perspective Divide + v2 ProjectedPos = WorldPos.xy / WorldPos.w; + + // Projection gets us in a range [-1, 1], and we want [0, width] + ProjectedPos.x = ((ProjectedPos.x / 2) * PanelW) + (PanelW / 2); + ProjectedPos.y = ((ProjectedPos.y / 2) * PanelH) + (PanelH / 2); + + Result = ProjectedPos + PanelBounds.Min; + return Result; +} + GSMetaTag(panel_render); GSMetaTag(panel_type_sculpture_view); internal void @@ -183,14 +205,13 @@ SculptureView_Render(panel Panel, rect2 PanelBounds, render_command_buffer* Rend //__debugbreak(); v4 LedPosition = LedBuffer->Positions[FocusPixel]; - m44 Matrix = GetCameraPerspectiveProjectionMatrix(State->Camera) * GetCameraModelViewMatrix(State->Camera); - v4 LedProjectedPosition = Matrix * LedPosition; - v2 LedOnScreenPosition = LedProjectedPosition.xy; + v2 LedOnScreenPosition = SculptureView_WorldToScreenPosition(LedPosition, State->Camera, PanelBounds); gs_string Tempgs_string = PushString(&State->Transient, 256); PrintF(&Tempgs_string, "%f %f", LedOnScreenPosition.x, LedOnScreenPosition.y); DrawString(RenderBuffer, Tempgs_string, State->Interface.Style.Font, v2{PanelBounds.Min.x + 100, PanelBounds.Max.y - 200}, WhiteV4); + v2 BoxHalfDim = v2{ 25, 25 }; v2 BoxMin = LedOnScreenPosition - BoxHalfDim; v2 BoxMax = LedOnScreenPosition + BoxHalfDim;