Skip to content

Commit eee995e

Browse files
committed
ADDED: GetWorldToScreenEx()
Addressing issue #1056
1 parent a3ca859 commit eee995e

2 files changed

Lines changed: 37 additions & 28 deletions

File tree

src/core.c

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,43 +1522,51 @@ Matrix GetCameraMatrix2D(Camera2D camera)
15221522
// Returns the screen space position from a 3d world space position
15231523
Vector2 GetWorldToScreen(Vector3 position, Camera camera)
15241524
{
1525-
// Calculate projection matrix (from perspective instead of frustum
1526-
Matrix matProj = MatrixIdentity();
1525+
Vector2 screenPosition = GetWorldToScreenEx(position, camera, GetScreenWidth(), GetScreenHeight());
15271526

1528-
if (camera.type == CAMERA_PERSPECTIVE)
1529-
{
1530-
// Calculate projection matrix from perspective
1531-
matProj = MatrixPerspective(camera.fovy*DEG2RAD, ((double)GetScreenWidth()/(double)GetScreenHeight()), DEFAULT_NEAR_CULL_DISTANCE, DEFAULT_FAR_CULL_DISTANCE);
1532-
}
1533-
else if (camera.type == CAMERA_ORTHOGRAPHIC)
1534-
{
1535-
float aspect = (float)screenWidth/(float)screenHeight;
1536-
double top = camera.fovy/2.0;
1537-
double right = top*aspect;
1527+
return screenPosition;
1528+
}
15381529

1539-
// Calculate projection matrix from orthographic
1540-
matProj = MatrixOrtho(-right, right, -top, top, 0.01, 1000.0);
1541-
}
1530+
// Returns size position for a 3d world space position (useful for texture drawing)
1531+
Vector2 GetWorldToScreenEx(Vector3 position, Camera camera, int width, int height)
1532+
{
1533+
// Calculate projection matrix (from perspective instead of frustum
1534+
Matrix matProj = MatrixIdentity();
15421535

1543-
// Calculate view matrix from camera look at (and transpose it)
1544-
Matrix matView = MatrixLookAt(camera.position, camera.target, camera.up);
1536+
if (camera.type == CAMERA_PERSPECTIVE)
1537+
{
1538+
// Calculate projection matrix from perspective
1539+
matProj = MatrixPerspective(camera.fovy * DEG2RAD, ((double)width/(double)height), DEFAULT_NEAR_CULL_DISTANCE, DEFAULT_FAR_CULL_DISTANCE);
1540+
}
1541+
else if (camera.type == CAMERA_ORTHOGRAPHIC)
1542+
{
1543+
float aspect = (float)width/(float)height;
1544+
double top = camera.fovy/2.0;
1545+
double right = top*aspect;
15451546

1546-
// Convert world position vector to quaternion
1547-
Quaternion worldPos = { position.x, position.y, position.z, 1.0f };
1547+
// Calculate projection matrix from orthographic
1548+
matProj = MatrixOrtho(-right, right, -top, top, DEFAULT_NEAR_CULL_DISTANCE, DEFAULT_FAR_CULL_DISTANCE);
1549+
}
15481550

1549-
// Transform world position to view
1550-
worldPos = QuaternionTransform(worldPos, matView);
1551+
// Calculate view matrix from camera look at (and transpose it)
1552+
Matrix matView = MatrixLookAt(camera.position, camera.target, camera.up);
15511553

1552-
// Transform result to projection (clip space position)
1553-
worldPos = QuaternionTransform(worldPos, matProj);
1554+
// Convert world position vector to quaternion
1555+
Quaternion worldPos = { position.x, position.y, position.z, 1.0f };
15541556

1555-
// Calculate normalized device coordinates (inverted y)
1556-
Vector3 ndcPos = { worldPos.x/worldPos.w, -worldPos.y/worldPos.w, worldPos.z/worldPos.w };
1557+
// Transform world position to view
1558+
worldPos = QuaternionTransform(worldPos, matView);
15571559

1558-
// Calculate 2d screen position vector
1559-
Vector2 screenPosition = { (ndcPos.x + 1.0f)/2.0f*(float)GetScreenWidth(), (ndcPos.y + 1.0f)/2.0f*(float)GetScreenHeight() };
1560+
// Transform result to projection (clip space position)
1561+
worldPos = QuaternionTransform(worldPos, matProj);
15601562

1561-
return screenPosition;
1563+
// Calculate normalized device coordinates (inverted y)
1564+
Vector3 ndcPos = { worldPos.x/worldPos.w, -worldPos.y/worldPos.w, worldPos.z/worldPos.w };
1565+
1566+
// Calculate 2d screen position vector
1567+
Vector2 sizePosition = { (ndcPos.x + 1.0f)/2.0f*(float)width, (ndcPos.y + 1.0f)/2.0f*(float)height };
1568+
1569+
return sizePosition;
15621570
}
15631571

15641572
// Returns the screen space position for a 2d camera world space position

src/raylib.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,7 @@ RLAPI Ray GetMouseRay(Vector2 mousePosition, Camera camera); // Returns a r
920920
RLAPI Matrix GetCameraMatrix(Camera camera); // Returns camera transform matrix (view matrix)
921921
RLAPI Matrix GetCameraMatrix2D(Camera2D camera); // Returns camera 2d transform matrix
922922
RLAPI Vector2 GetWorldToScreen(Vector3 position, Camera camera); // Returns the screen space position for a 3d world space position
923+
RLAPI Vector2 GetWorldToScreenEx(Vector3 position, Camera camera, int width, int height); // Returns size position for a 3d world space position
923924
RLAPI Vector2 GetWorldToScreen2D(Vector2 position, Camera2D camera); // Returns the screen space position for a 2d camera world space position
924925
RLAPI Vector2 GetScreenToWorld2D(Vector2 position, Camera2D camera); // Returns the world space position for a 2d camera screen space position
925926

0 commit comments

Comments
 (0)