Skip to content

Commit 6eefba3

Browse files
committed
Added GetScreenDirection function to Chapter 9
1 parent c585c63 commit 6eefba3

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

Chapter09/Renderer.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,3 +324,16 @@ Vector3 Renderer::Unproject(const Vector3& screenPoint) const
324324
unprojection.Invert();
325325
return Vector3::TransformWithPerspDiv(deviceCoord, unprojection);
326326
}
327+
328+
void Renderer::GetScreenDirection(Vector3& outStart, Vector3& outDir) const
329+
{
330+
// Get start point (in center of screen on near plane)
331+
Vector3 screenPoint(0.0f, 0.0f, 0.0f);
332+
outStart = Unproject(screenPoint);
333+
// Get end point (in center of screen, between near and far)
334+
screenPoint.z = 0.9f;
335+
Vector3 end = Unproject(screenPoint);
336+
// Get direction vector
337+
outDir = end - outStart;
338+
outDir.Normalize();
339+
}

Chapter09/Renderer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class Renderer
5656
// y = [-screenHeight/2, +screenHeight/2]
5757
// z = [0, 1) -- 0 is closer to camera, 1 is further
5858
Vector3 Unproject(const Vector3& screenPoint) const;
59+
void GetScreenDirection(Vector3& outStart, Vector3& outDir) const;
5960
float GetScreenWidth() const { return mScreenWidth; }
6061
float GetScreenHeight() const { return mScreenHeight; }
6162
private:

0 commit comments

Comments
 (0)