resonance 1.0.1
Loading...
Searching...
No Matches
mesh.h
Go to the documentation of this file.
1#pragma once
2
3#include <list>
4#include <vector>
5
6#include "math/box.h"
7#include "math/sphere.h"
8#include "math/vector3.h"
9#include "os/hxstr.h"
10#include "rnd/collideable.h"
11#include "rnd/drawable.h"
12#include "rnd/manager.h"
13#include "rnd/meshedge.h"
14#include "rnd/meshface.h"
15#include "rnd/meshvert.h"
16#include "rnd/transformable.h"
17
18namespace Rnd {
19class Dbg;
20class Mat;
21class MeshAnim;
22class Stream;
23} // namespace Rnd
24
25namespace Rnd {
26
46class Mesh : public Drawable, public Transformable, public Collideable {
47public:
58 static Mesh *(*sNew)(const HxStr &name);
59
67
76
88
90 enum { kSyncAllMask = 0x7f };
91
99 enum {
100 kSyncPoints = 0x01,
101 kSyncNorms = 0x08,
102 kSyncColors = 0x10,
103 kSyncTexs = 0x20
104 };
105
112 enum {
116 };
117
119 enum { kSerialVersion = 10 };
120
128 Mesh(const HxStr &name);
129
134 virtual ~Mesh();
135
146 void *operator new(size_t nSize);
147
157 void operator delete(void *pBlock);
158
169 virtual void DumpText(Dbg &sink);
170
182 virtual void Save(Stream &stream);
183
196 virtual void Replace(Object *pFrom, Object *pTo);
197
208 virtual const HxStr &ClassName() const;
209
222 virtual void Copy(const Object *pSource, unsigned nFlags);
223
236 virtual void Load(Stream &stream);
237
248 void SetMat(Mat *pMat);
249
261
276 void SetNext(Mesh *pNext, float flMinScreen);
277
289
300
312
322
336
348
360 void MakeCube(float flHalfSize);
361
381 void ComputeNormals(bool bPositionOnly);
382
402 void WeldVerts(bool bAverageColors);
403
415 void SetVertsOwner(Mesh *pOwner);
416
426 void SetFacesOwner(Mesh *pOwner);
427
436
445 void SetDepthChain(ZMode zMode, ZFunc zFunc);
446
454 void SetVertexColor(const Color &color);
455
467 void MirrorX() {
468 Vec3Scale(mLocalXfm[0], -1.0f, mLocalXfm[0]);
469 mDirty = 1;
470 }
471
484 void ScaleUniform(float flScale);
485
499 void AddQuad(unsigned short nV0, unsigned short nV1, unsigned short nV2, unsigned short nV3) {
500 std::vector<MeshFace> &faces = mFacesOwner->mFaces;
501 faces.push_back(MeshFace{nV0, nV1, nV2});
502 faces.push_back(MeshFace{nV2, nV1, nV3});
503 }
504
518 void AddQuadStrip(int nRowA, int nRowB, int nCount, int nStep) {
519 for (int i = 0; i < nCount - 1; i += nStep) {
520 AddQuad(nRowA, nRowA + nStep, nRowB, nRowB + nStep);
521 nRowA += nStep;
522 nRowB += nStep;
523 }
524 }
525
543 int PrepareDraw(Sphere &worldSphere);
544
561 virtual void FindCollisions(const Segment &ray, std::list<Collision> &collisions);
562
577 virtual void Sync();
578
590 virtual void SyncChanged(int nMask);
591
601 virtual void SyncAll();
602
603protected:
604 // The override below fills Rnd::Drawable vtable slot 7. The access of that base declaration is
605 // not recovered yet, and protected is the narrowest that admits the Rnd::PsMesh override.
606
616 virtual void AddRefObjects();
617
618private:
619 // Take a reference on each object this mesh points at. 0x00493e10 inlines it as its own first
620 // half, and AddRefObjects() is its only caller.
621 void AddObjectRefs();
622
631 void ReleaseObjects();
632
642 void ClearSharedGeometry();
643
644 // One face in AssignFlatVerts(). A face that joins a coplanar neighbour's fan records that
645 // neighbour in mPrimaryFace. A face that heads a fan counts the joined edges in mSharedEdges
646 // and records the vertex every joined edge passes through in mPivot, with mPivotOther the other
647 // end of the only joined edge while there is exactly one, and -1 otherwise.
648 struct FlatFace {
649 int mFace;
650 union {
651 int mSharedEdges;
652 int mPrimaryFace;
653 };
654 int mPivot;
655 int mPivotOther;
656 Vector3 mNormal;
657 };
658
669 friend bool JoinFaces(Mesh *pMesh, FlatFace &primary, FlatFace &face);
670
685 void AssignFlatVerts(std::list<MeshAnim *> &anims);
686
687 // Data members follow the recovered offset order, and the access specifiers interleave.
688
689public:
700
701public:
704 std::vector<MeshVert> mVerts;
707 std::vector<MeshFace> mFaces;
710 std::vector<MeshEdge> mEdges;
711
712protected:
713public:
718
719protected:
720 // Rnd::PsMesh::DrawShowing() reads the sphere radius to cull.
721 Sphere mSphere; // +0x110
722
723public:
730
731protected:
732 // SetTransOwner() is the accessor for the first of the three, and the other two are written
733 // only by a load or a copy. Rnd::PsMesh::DrawShowing() reads mTransOwner directly, which is
734 // what keeps it out of the private section.
736
737private:
738 Transformable *mTrans1Owner; // +0x13c
739 Transformable *mTrans2Owner; // +0x140
740
741protected:
742 // Rnd::PsMesh::DrawShowing() reads the cap to decide how much of the mesh to submit.
743 int mMaxVerts; // +0x144
744
745public:
754};
755
756// NTSC-U/C: 0x002723a0, PAL: 0x0028a830
757inline void Mesh::ScaleUniform(float flScale) {
758 Vec3Scale(mLocalXfm[0], flScale, mLocalXfm[0]);
759 Vec3Scale(mLocalXfm[1], flScale, mLocalXfm[1]);
760 Vec3Scale(mLocalXfm[2], flScale, mLocalXfm[2]);
761 mDirty = 1;
762}
763
774Mesh *NewMesh(const HxStr &name);
775
789
803
819
833extern int g_nRndMeshLoadVersion;
834
835} // namespace Rnd
Axis-aligned bounding box.
Definition box.h:12
Heap-allocated NUL-terminated string.
Definition hxstr.h:41
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.
Surface description a Rnd::Mesh draws with.
Definition mat.h:36
Per-vertex animation of one mesh.
Definition meshanim.h:45
Indexed triangle mesh with one material.
Definition mesh.h:46
ZMode
Depth buffer read and write mode, as the text dump labels the values.
Definition mesh.h:69
@ kZModeZReadWrite
Definition mesh.h:72
@ kZModeWReadOnly
Definition mesh.h:73
@ kZModeDisable
Definition mesh.h:70
@ kZModeZReadOnly
Definition mesh.h:71
@ kZModeWReadWrite
Definition mesh.h:74
virtual void SyncAll()
Report every part of the mesh as changed.
Box BoundingBox()
Compute the bounding box of the vertices.
virtual void AddRefObjects()
Restore the reference bookkeeping and resynchronise after a load or a copy.
std::vector< MeshFace > mFaces
Definition mesh.h:707
void SetFacesOwner(Mesh *pOwner)
Point the mesh at the mesh whose triangles and edges it draws.
virtual void Sync()
Rebuild whatever the platform subclass derives from the geometry.
virtual void Save(Stream &stream)
Serialise the mesh.
void AddQuad(unsigned short nV0, unsigned short nV1, unsigned short nV2, unsigned short nV3)
Append the two triangles of a quad to the faces of mFacesOwner.
Definition mesh.h:499
@ kCopyShareFaces
Definition mesh.h:114
@ kCopyShareVerts
Definition mesh.h:113
@ kCopyShareTransforms
Definition mesh.h:115
void MakeCube(float flHalfSize)
Replace the geometry with an axis-aligned cube.
friend bool JoinFaces(Mesh *pMesh, FlatFace &primary, FlatFace &face)
Add face to the fan that primary heads when the two share a vertex of pMesh, their normals are within...
Sphere mSphere
Definition mesh.h:721
Transformable * mTransOwner
Definition mesh.h:735
virtual void FindCollisions(const Segment &ray, std::list< Collision > &collisions)
Test a ray against this mesh and append what it strikes to sink.
void ComputeNormals(bool bPositionOnly)
Recompute the vertex normals of mVertsOwner from the faces of mFacesOwner.
int PrepareDraw(Sphere &worldSphere)
Decide whether this mesh draws, and yield its world bounding sphere.
virtual void Copy(const Object *pSource, unsigned nFlags)
Copy another mesh over this one.
ZMode mZMode
Definition mesh.h:696
void MirrorX()
Mirror the mesh across its local x axis by negating the x row of its local transform,...
Definition mesh.h:467
virtual void Replace(Object *pFrom, Object *pTo)
Replace one object reference with another.
void SetMaterialChain(Mat *pMat)
Set the material of this mesh and of every coarser level of its mNext chain.
Sphere WorldSphere()
Report the bounding sphere in world space.
virtual ~Mesh()
std::vector< MeshVert > mVerts
Definition mesh.h:704
@ kSyncTexs
Definition mesh.h:103
@ kSyncColors
Definition mesh.h:102
@ kSyncPoints
Definition mesh.h:100
@ kSyncNorms
Definition mesh.h:101
float mMinScreen
Definition mesh.h:750
void SetVertexColor(const Color &color)
Give every vertex of mVertsOwner one colour and report the change through SyncChanged().
void SetVertsOwner(Mesh *pOwner)
Point the mesh at the mesh whose vertices it draws.
void SetTrans1Owner(Transformable *pOwner)
Point the mesh at its second transform owner.
@ kSyncAllMask
Definition mesh.h:90
virtual void SyncChanged(int nMask)
Report which parts of the mesh have changed.
@ kSerialVersion
Definition mesh.h:119
static Mesh *(* sNew)(const HxStr &name)
Creator the registered "Mesh" class builds through.
Definition mesh.h:58
static HxStr sClassName
Registered class name of Rnd::Mesh, the string "Mesh".
Definition mesh.h:66
virtual void DumpText(Dbg &sink)
Write the mesh to the engine text sink.
void SetMat(Mat *pMat)
Point the mesh at a material.
std::vector< MeshEdge > mEdges
Definition mesh.h:710
Mesh * mFacesOwner
Definition mesh.h:729
Mesh * mNext
Definition mesh.h:753
Mesh(const HxStr &name)
Construct an empty mesh that owns its own geometry.
void ForwardSync()
Dispatch Sync().
virtual const HxStr & ClassName() const
Return the registered class name, "Mesh".
void SetTransOwner(Transformable *pOwner)
Point the mesh at the transform it draws with.
ZFunc mZFunc
Definition mesh.h:699
void ScaleUniform(float flScale)
Scale the three basis rows of the local transform by one factor, and mark the transform dirty.
Definition mesh.h:757
int mMaxVerts
Definition mesh.h:743
void SetDepthChain(ZMode zMode, ZFunc zFunc)
Set the depth mode and comparison of this mesh and of every coarser level of its mNext chain.
void SetTrans2Owner(Transformable *pOwner)
Point the mesh at its third transform owner.
ZFunc
Depth comparison, as the text dump labels the values.
Definition mesh.h:78
@ kZFuncEqual
Definition mesh.h:81
@ kZFuncNever
Definition mesh.h:79
@ kZFuncGreaterEqual
Definition mesh.h:85
@ kZFuncNotEqual
Definition mesh.h:84
@ kZFuncGreater
Definition mesh.h:83
@ kZFuncLess
Definition mesh.h:80
@ kZFuncLessEqual
Definition mesh.h:82
@ kZFuncAlways
Definition mesh.h:86
virtual void Load(Stream &stream)
Load the mesh.
void WeldVerts(bool bAverageColors)
Merge duplicate vertices and remove the faces and edges the merge makes redundant.
void AddQuadStrip(int nRowA, int nRowB, int nCount, int nStep)
Append the quads between two rows of vertices with AddQuad().
Definition mesh.h:518
Sphere BoundingSphere()
Compute a bounding sphere of the vertices.
Mat * mMat
Definition mesh.h:717
Mesh * mVertsOwner
Definition mesh.h:726
void SetNext(Mesh *pNext, float flMinScreen)
Set the smallest screen size to draw at, and the mesh to draw in its place below it.
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
float mLocalXfm[kXfmRowCount][kXfmRowFloatCount]
Transform this object is positioned by, before the parent transform is applied.
Definition transformable.h:299
Transformable()
Construct a dirty transformable with identity transforms and no children.
int mDirty
Set by every writer of a transform field and cleared once UpdateWorldXfm has recomposed.
Definition transformable.h:325
Bounding sphere.
Definition sphere.h:15
The rendering engine.
Definition animhelper.h:3
Object * CreateRegisteredMesh(const HxStr &name)
Build a mesh for the registered "Mesh" class.
Manager TheManager
The renderer's object registry.
Mesh * NewMeshThroughHook(const HxStr &name)
Build a mesh through Mesh::sNew, without the narrowing CreateRegisteredMesh() performs.
int g_nRndMeshLoadVersion
Serial version of the mesh record currently being read.
Mesh * NewMesh(const HxStr &name)
Allocate and construct a mesh.
void RegisterMeshClass()
Point Mesh::sNew at NewMesh() and register the "Mesh" class with Rnd::Manager.
Definition mesh.h:815
Linear colour with four float components.
Definition color.h:10
One triangle of a Rnd::Mesh, as three indices into its vertex vector.
Definition meshface.h:12
Line segment a collision query is run along.
Definition collideable.h:22
Three-component vector, padded to a PlayStation 2 quadword.
Definition vector3.h:12
void Vec3Scale(const float *pSrc, float flScale, float *pOut)
Multiply a three-component vector by a scalar.