resonance 1.0.1
Loading...
Searching...
No Matches
cam.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4
5#include "math/frustum.h"
6#include "math/vector2.h"
7#include "math/vector3.h"
8#include "rnd/collideable.h"
9#include "rnd/drawable.h"
10#include "rnd/manager.h"
11#include "rnd/transformable.h"
12
13class HxStr;
14namespace Rnd {
15class Dbg;
16class Object;
17class Stream;
18class Tex;
19} // namespace Rnd
20
21namespace Rnd {
22
53class Cam : public Drawable, public Transformable, public Collideable {
54public:
64 static Cam *sCurrent;
65
75 static Cam *(*sNew)(const HxStr &name);
76
84
93 void *operator new(size_t nSize);
94
102 void operator delete(void *pBlock);
103
113 struct Rect {
114 float x; // +0x00
115 float y; // +0x04
116 float w; // +0x08
117 float h; // +0x0c
118 };
119
132 explicit Cam(const HxStr &name);
133
140 virtual ~Cam();
141
156
169
186 Segment ScreenToRay(const Vector2 &ptScreen, float flLength);
187
202 Vector2 ProjectToUnit(const Vector3 &pt);
203
218
228 void SetScreenRect(const Rect &rect);
229
243 void SetFrustum(float flNear, float flFar, float flFov);
244
254 float GetNearPlane() const {
255 return mNearPlane;
256 }
257
267 float GetFarPlane() const {
268 return mFarPlane;
269 }
270
280 float GetFov() const {
281 return mFov;
282 }
283
298 void SetTargetTex(Tex *pTex);
299
312 virtual Vector2 ScreenToPixels(const Vector2 &ptScreen);
313
324 virtual void UpdateTargetAspect();
325
336 virtual const HxStr &ClassName() const;
337
351 virtual void DumpText(Dbg &sink);
352
365 virtual void Replace(Object *pFrom, Object *pTo);
366
380 virtual int UpdateWorldXfm(Transformable *pParent, int nForce);
381
392 virtual void Save(Stream &stream);
393
405 virtual void Copy(const Object *pSource, unsigned nFlags);
406
419 virtual void Load(Stream &stream);
420
429 static Cam *NewCam(const HxStr &name);
430
431 // Declared in recovered offset order, with the access specifiers interleaved. Each member that
432 // remains public is read directly by a class outside this hierarchy, and the image exposes no
433 // accessor for it.
434
443
446
447private:
448 // Inverse of mLocalProject. Only UpdateProjection() fills it and only UpdateWorldProject()
449 // reads it.
450 Vector3 mInvLocalProject[kXfmRowCount]; // +0x160
451
452public:
455
458
459protected:
460 // The view volume in camera space, which UpdateProjection() builds from the four projection
461 // parameters below. Protected because Rnd::PsCam::DrawShowing() reads it, along with the near
462 // and far planes, the field of view, and mZRange.
464
465public:
473
474protected:
475 float mNearPlane; // +0x2e0
476 float mFarPlane; // +0x2e4
477 float mFov; // +0x2e8 Zero selects an orthographic projection.
478
479protected:
480 // Protected because Rnd::PsCam writes it when there is no render target.
481 float mYRatio; // +0x2ec
482
483protected:
484 // Depth the projected image is mapped into, as a low value in x and a high value in y.
485 Vector2 mZRange; // +0x2f0
486
487public:
494
502
503protected:
516 virtual void FindCollisions(const Vector2 &point, std::list<Collision> &collisions);
517
528 virtual int DrawShowing();
529
530private:
539 void AcquireTargetTex();
540
549 void ReleaseTargetTex();
550
551 // A point carried through a transform, the basis rows weighted by its components plus the
552 // translation, with out.w taken from the input. A VU0 multiply and accumulate in the image,
553 // whose one out-of-line copy has no caller.
554 static void XfmPoint(const Vector3 &in, const Vector3 *pXfm, Vector3 &out);
555
556 // Neither written by the constructor nor read anywhere in the image. A reserved run records a
557 // span that has not been recovered and is not a field. This one is either such a field or the
558 // alignment the virtual base subobject at 0x310 is placed on.
559 unsigned char mReserved30c[0x04];
560};
561
562// NTSC-U/C: 0x004b1dd0, PAL: 0x004efff8
563inline void Cam::XfmPoint(const Vector3 &in, const Vector3 *pXfm, Vector3 &out) {
564 const float flX = pXfm[0].x * in.x + pXfm[1].x * in.y + pXfm[2].x * in.z + pXfm[3].x;
565 const float flY = pXfm[0].y * in.x + pXfm[1].y * in.y + pXfm[2].y * in.z + pXfm[3].y;
566 const float flZ = pXfm[0].z * in.x + pXfm[1].z * in.y + pXfm[2].z * in.z + pXfm[3].z;
567 const float flW = in.w;
568 out.x = flX;
569 out.y = flY;
570 out.z = flZ;
571 out.w = flW;
572}
573
574// NTSC-U/C: 0x004b2008, PAL: 0x004f0230
576 Vector3 ptProjected;
577 XfmPoint(pt, mWorldProject, ptProjected);
578 Vector2 ptNdc; // Yes, the binary leaves this unset for a point at zero depth.
579 if (ptProjected.z != 0.0f) {
580 const float flInvDepth = 1.0f / ptProjected.z;
581 ptNdc.x = ptProjected.x * flInvDepth;
582 ptNdc.y = ptProjected.y * flInvDepth;
583 }
584 const Vector2 one{1.0f, 1.0f};
585 Vector2 ptShifted;
586 Rnd::Add(ptNdc, one, ptShifted);
587 Vector2 ptUnit;
588 Rnd::Multiply(ptShifted, 0.5f, ptUnit);
589 return ptUnit;
590}
591
604
618
631inline void RegisterCamClass() {
633 Cam::sCurrent = nullptr;
635}
636
637} // namespace Rnd
Six-plane view volume.
Definition frustum.h:19
Heap-allocated NUL-terminated string.
Definition hxstr.h:41
Camera.
Definition cam.h:53
void SetTargetTex(Tex *pTex)
Set the texture this camera draws into, or none to draw into the frame buffer.
virtual void FindCollisions(const Vector2 &point, std::list< Collision > &collisions)
Report whether a screen point lies inside the screen rectangle.
float mNearPlane
Definition cam.h:475
virtual ~Cam()
Clear Cam::sCurrent when it addresses this camera and release the render target.
static Cam *(* sNew)(const HxStr &name)
Creator the registered "Cam" class builds through.
Definition cam.h:75
virtual void Load(Stream &stream)
Read this camera's serialised form from stream.
static Cam * sCurrent
Camera the frame is being drawn through.
Definition cam.h:64
Segment ScreenToRay(const Vector2 &ptScreen, float flLength)
Build the segment under a point of the screen rectangle.
static Cam * NewCam(const HxStr &name)
Build a camera the class registry vends.
Vector3 UnprojectFar(const Vector2 &ptUnit)
Carry a point of the unit square of the projected image onto the far side of the projection.
virtual void Save(Stream &stream)
Write this camera's serialised form to stream.
virtual void Copy(const Object *pSource, unsigned nFlags)
Copy the state of pSource into this camera.
Vector3 mLocalProject[kXfmRowCount]
Projection from camera space onto the screen rectangle.
Definition cam.h:445
float GetFov() const
Report the field of view, zero for an orthographic projection.
Definition cam.h:280
Cam(const HxStr &name)
Construct a camera with the default projection.
Frustum mLocalFrustum
Definition cam.h:463
float GetFarPlane() const
Report the far clip distance.
Definition cam.h:267
virtual void DumpText(Dbg &sink)
Write a description of this camera to sink.
Vector3 mWorldProject[kXfmRowCount]
mWorldToCam followed by mLocalProject.
Definition cam.h:454
virtual void UpdateTargetAspect()
Take the aspect ratio from the render target and rebuild the projection.
void SetFrustum(float flNear, float flFar, float flFov)
Set the clip distances and the field of view, then rebuild the projection.
Vector2 ProjectToUnit(const Vector3 &pt)
Place a world point in the unit square of the projected image.
Definition cam.h:575
void UpdateWorldProject()
Compose the world projection, its inverse, and the world frustum.
Tex * mpTargetTex
Texture this camera draws into, or null to draw into the frame buffer.
Definition cam.h:501
virtual const HxStr & ClassName() const
Report the class key a .rnd file writes for a camera.
Vector3 mWorldToCam[kXfmRowCount]
Inverse of the world transform, which maps world space into camera space.
Definition cam.h:442
virtual int UpdateWorldXfm(Transformable *pParent, int nForce)
Recompose the world transform and, when it changed, the world projection.
Frustum mWorldFrustum
The view volume in world space.
Definition cam.h:472
float mFarPlane
Definition cam.h:476
virtual int DrawShowing()
Make this camera the one the frame is drawn through.
static HxStr sClassName
Registered class name of Rnd::Cam, the string "Cam".
Definition cam.h:83
virtual Vector2 ScreenToPixels(const Vector2 &ptScreen)
Place a point of the screen rectangle in render target pixels.
float mYRatio
Definition cam.h:481
float GetNearPlane() const
Report the near clip distance.
Definition cam.h:254
float mFov
Definition cam.h:477
virtual void Replace(Object *pFrom, Object *pTo)
Repoint the render target when the object it addressed is replaced.
Rect mScreenRect
Fraction of the render target the projected image is placed in.
Definition cam.h:493
Vector2 mZRange
Definition cam.h:485
void UpdateProjection()
Build the local frustum and both local projection matrices.
Vector3 mInvWorldProject[kXfmRowCount]
mInvLocalProject followed by the world transform.
Definition cam.h:457
void SetScreenRect(const Rect &rect)
Replace mScreenRect and rebuild the projection.
Collideable()
Construct a collideable with no children.
Sink that the engine formats its diagnostic and failure text into.
Definition dbg.h:33
Drawable()
Construct a visible drawable with no children.
Base of every object the renderer can load, resolve by name, and serialise.
Definition object.h:43
Byte stream that every renderer object serialises itself through.
Definition stream.h:31
Texture, as a bitmap path plus the load state of its mip levels.
Definition tex.h:43
Transformable()
Construct a dirty transformable with identity transforms and no children.
The rendering engine.
Definition animhelper.h:3
constexpr int kXfmRowCount
Rows in one transform.
Definition transformable.h:12
void RegisterCamClass()
Point Cam::sNew at Cam::NewCam(), clear Cam::sCurrent, and register the "Cam" class with Rnd::Manager...
Definition cam.h:631
Object * CreateRegisteredCam(const HxStr &name)
Build a camera for the registered "Cam" class.
Cam * NewCamThroughHook(const HxStr &name)
Build a camera through Cam::sNew, without the narrowing CreateRegisteredCam() performs.
Manager TheManager
The renderer's object registry.
void Multiply(const Vector2 &v, float flScale, Vector2 &out)
Scale a two-component vector.
void Add(const Vector2 &a, const Vector2 &b, Vector2 &out)
Add two two-component vectors.
Rectangle the projected image is placed in.
Definition cam.h:113
float x
Definition cam.h:114
float w
Definition cam.h:116
float y
Definition cam.h:115
float h
Definition cam.h:117
Line segment a collision query is run along.
Definition collideable.h:22
Two-component vector, used for texture coordinates.
Definition vector2.h:10
float x
Definition vector2.h:11
float y
Definition vector2.h:12
Three-component vector, padded to a PlayStation 2 quadword.
Definition vector3.h:12
float w
Definition vector3.h:16
float x
Definition vector3.h:13
float y
Definition vector3.h:14
float z
Definition vector3.h:15