Skip to content

Commit

Permalink
First fugly version with GL4 quaternion shader
Browse files Browse the repository at this point in the history
  • Loading branch information
lhog committed Dec 16, 2024
1 parent 06f93bf commit 8aab2c2
Show file tree
Hide file tree
Showing 21 changed files with 101 additions and 53 deletions.
73 changes: 54 additions & 19 deletions cont/base/springcontent/shaders/GLSL/ModelVertProgGL4.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,13 @@ layout(std140, binding = 1) uniform UniformParamsBuffer {
vec4 teamColor[255]; //all team colors
};

layout(std140, binding = 0) readonly buffer MatrixBuffer {
mat4 mat[];
struct Transform {
vec4 quat;
vec4 trSc;
};

layout(std140, binding = 0) readonly buffer TransformBuffer {
Transform transforms[];
};

uniform int cameraMode = 0;
Expand Down Expand Up @@ -114,7 +119,7 @@ out Data {
};
out float gl_ClipDistance[3];

#line 1117
#line 1122

void TransformPlayerCam(vec4 worldPos) {
gl_Position = cameraViewProj * worldPos;
Expand All @@ -132,6 +137,37 @@ uint GetUnpackedValue(uint packedValue, uint byteNum) {
return (packedValue >> (8u * byteNum)) & 0xFFu;
}

vec4 MultiplyQuat(vec4 a, vec4 b)
{
return vec4(a.w * b.w - dot(a.w, b.w), a.w * b.xyz + b.w * a.xyz + cross(a.xyz, b.xyz));
}

vec3 RotateByQuaternion(vec4 q, vec3 v) {
return 2.0f * dot(q.xyz, v) * q.xyz + (q.w * q.w - dot(q.xyz, q.xyz)) * v + 2.0 * q.w * cross(q.xyz, v);
}

vec4 RotateByQuaternion(vec4 q, vec4 v) {
return vec4(RotateByQuaternion(q, v.xyz), v.w);
}

vec3 ApplyTransform(Transform tra, vec3 v) {
return RotateByQuaternion(tra.quat, v * tra.trSc.w) + tra.trSc.xyz;
}

vec4 ApplyTransform(Transform tra, vec4 v) {
return vec4(ApplyTransform(tra, v.xyz), v.w);
}

Transform ApplyTransform(Transform parentTra, Transform childTra) {
return Transform(
MultiplyQuat(parentTra.quat, childTra.quat),
vec4(
parentTra.trSc.xyz + RotateByQuaternion(parentTra.quat, parentTra.trSc.w * childTra.trSc.xyz),
parentTra.trSc.w * childTra.trSc.w
)
);
}

void GetModelSpaceVertex(out vec4 msPosition, out vec3 msNormal)
{
bool staticModel = (matrixMode > 0);
Expand All @@ -146,13 +182,12 @@ void GetModelSpaceVertex(out vec4 msPosition, out vec3 msNormal)
);

uint b0 = GetUnpackedValue(bonesInfo.x, 0); //first boneID
mat4 b0BoneMat = mat[instData.x + b0 + uint(!staticModel)];
mat3 b0NormMat = mat3(b0BoneMat);
Transform b0BoneTra = transforms[instData.x + b0 + uint(!staticModel)];

weights[0] *= b0BoneMat[3][3];
weights[0] *= step(0.0, b0BoneTra.trSc.w);

msPosition = b0BoneMat * piecePos;
msNormal = b0NormMat * normal;
msPosition = ApplyTransform(b0BoneTra, piecePos);
msNormal = RotateByQuaternion(b0BoneTra.quat, normal);

if (staticModel || weights[0] == 1.0)
return;
Expand All @@ -164,7 +199,7 @@ void GetModelSpaceVertex(out vec4 msPosition, out vec3 msNormal)
wSum += weights[0];

uint numPieces = GetUnpackedValue(instData.z, 3);
mat4 bposeMat = mat[instData.w + b0];
Transform bposeTra = transforms[instData.w + b0];

// Vertex[ModelSpace,BoneX] = PieceMat[BoneX] * InverseBindPosMat[BoneX] * BindPosMat[Bone0] * Vertex[Bone0]
for (uint bi = 1; bi < 3; ++bi) {
Expand All @@ -173,16 +208,15 @@ void GetModelSpaceVertex(out vec4 msPosition, out vec3 msNormal)
if (bID == 0xFFu || weights[bi] == 0.0)
continue;

mat4 bposeInvMat = mat[instData.w + numPieces + bID];
mat4 boneMat = mat[instData.x + 1u + bID];
Transform bposeInvTra = transforms[instData.w + numPieces + bID];
Transform boneTra = transforms[instData.x + 1u + bID];

weights[bi] *= boneMat[3][3];
weights[bi] *= step(0.0, boneTra.trSc.w);

mat4 skinMat = boneMat * bposeInvMat * bposeMat;
mat3 normMat = mat3(skinMat);
Transform skinTra = ApplyTransform(ApplyTransform(boneTra, bposeInvTra), bposeTra);

msPosition += skinMat * piecePos * weights[bi];
msNormal += normMat * normal * weights[bi];
msPosition += ApplyTransform(skinTra, piecePos * weights[bi]);
msNormal += RotateByQuaternion(skinTra.quat, normal * weights[bi]);
wSum += weights[bi];
}

Expand All @@ -194,14 +228,15 @@ void main(void)
{
bool staticModel = (matrixMode > 0);

mat4 worldMatrix = staticModel ? staticModelMatrix : mat[instData.x]; //don't cover ARRAY_MATMODE yet
//mat4 worldMatrix = staticModel ? staticModelMatrix : mat[instData.x]; //don't cover ARRAY_MATMODE yet
Transform worldTra = transforms[instData.x]; //don't cover ARRAY_MATMODE yet

vec4 modelPos;
vec3 modelNormal;
GetModelSpaceVertex(modelPos, modelNormal);

worldPos = worldMatrix * modelPos;
worldNormal = mat3(worldMatrix) * modelNormal;
worldPos = ApplyTransform(worldTra, modelPos);
worldNormal = RotateByQuaternion(worldTra.quat, modelNormal);

gl_ClipDistance[0] = dot(modelPos, clipPlane0); //upper construction clip plane
gl_ClipDistance[1] = dot(modelPos, clipPlane1); //lower construction clip plane
Expand Down
2 changes: 1 addition & 1 deletion rts/Lua/LuaOpenGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1423,7 +1423,7 @@ static void GLObjectPieceMultMatrix(lua_State* L, const CSolidObject* obj)
if (lmp == nullptr)
return;

glMultMatrixf(lmp->GetModelSpaceMatrix());
glMultMatrixf(lmp->GetModelSpaceTransform().ToMatrix());
}

static bool GLObjectDrawWithLuaMat(lua_State* L, CSolidObject* obj, LuaObjType objType)
Expand Down
2 changes: 1 addition & 1 deletion rts/Lua/LuaSyncedRead.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8245,7 +8245,7 @@ static int GetSolidObjectPieceMatrix(lua_State* L, const CSolidObject* o)
if (lmp == nullptr)
return 0;

const CMatrix44f& mat = lmp->GetModelSpaceMatrix();
const CMatrix44f& mat = lmp->GetModelSpaceTransform().ToMatrix();

for (float mi: mat.m) {
lua_pushnumber(L, mi);
Expand Down
6 changes: 3 additions & 3 deletions rts/Rendering/Common/ModelDrawerData.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ inline void CModelDrawerDataBase<T>::UpdateObjectSMMA(const T* o)
{
ScopedTransformMemAlloc& smma = GetObjectTransformMemAlloc(o);

const auto tmNew = o->GetTransformMatrix();
const auto tmNew = Transform::FromMatrix(o->GetTransformMatrix());

// from one point it doesn't worth the comparison, cause units usually move
// but having not updated smma[0] allows for longer solid no-update areas in ModelUniformsUploader::UpdateDerived()
Expand All @@ -174,12 +174,12 @@ inline void CModelDrawerDataBase<T>::UpdateObjectSMMA(const T* o)

if unlikely(!lmp.GetScriptVisible()) {
//smma[i + 1] = CMatrix44f::Zero();
smma.UpdateForced(i + 1, CMatrix44f::Zero());
smma.UpdateForced(i + 1, Transform::Zero());
continue;
}

// UpdateIfChanged is not needed, wasCustomDirty takes that role
smma.UpdateForced(i + 1, lmp.GetModelSpaceMatrix());
smma.UpdateForced(i + 1, lmp.GetModelSpaceTransform());
}
}

Expand Down
2 changes: 1 addition & 1 deletion rts/Rendering/DebugColVolDrawer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ static void DrawObjectDebugPieces(const CSolidObject* o, const float4& defColor)
float4{ (1.0f - (hitDeltaTime / 150.0f)), 0.0f, 0.0f, 1.0f } :
defColor;

const CMatrix44f mp = mo * lmp->GetModelSpaceMatrix();
const CMatrix44f mp = mo * lmp->GetModelSpaceTransform().ToMatrix();
// factors in the volume offsets
DrawCollisionVolume(lmpVol, mp, curColor);
}
Expand Down
16 changes: 8 additions & 8 deletions rts/Rendering/Models/3DModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ void LocalModel::UpdateBoundingVolume()
float3 bbMaxs = DEF_MAX_SIZE;

for (const auto& lmPiece: pieces) {
const CMatrix44f& matrix = lmPiece.GetModelSpaceMatrix();
const auto& tra = lmPiece.GetModelSpaceTransform();
const S3DModelPiece* piece = lmPiece.original;

// skip empty pieces or bounds will not be sensible
Expand All @@ -485,7 +485,7 @@ void LocalModel::UpdateBoundingVolume()
};

for (const float3& v: verts) {
const float3 vertex = matrix * v;
const float3 vertex = tra * v;

bbMins = float3::min(bbMins, vertex);
bbMaxs = float3::max(bbMaxs, vertex);
Expand Down Expand Up @@ -662,7 +662,7 @@ void LocalModelPiece::Draw() const
assert(original);

glPushMatrix();
glMultMatrixf(GetModelSpaceMatrix());
glMultMatrixf(GetModelSpaceTransform().ToMatrix());
S3DModelHelpers::BindLegacyAttrVBOs();
original->DrawElements();
S3DModelHelpers::UnbindLegacyAttrVBOs();
Expand All @@ -679,7 +679,7 @@ void LocalModelPiece::DrawLOD(uint32_t lod) const
return;

glPushMatrix();
glMultMatrixf(GetModelSpaceMatrix());
glMultMatrixf(GetModelSpaceTransform().ToMatrix());
if (const auto ldl = lodDispLists[lod]; ldl == 0) {
S3DModelHelpers::BindLegacyAttrVBOs();
original->DrawElements();
Expand Down Expand Up @@ -711,8 +711,8 @@ bool LocalModelPiece::GetEmitDirPos(float3& emitPos, float3& emitDir) const
return false;

// note: actually OBJECT_TO_WORLD but transform is the same
emitPos = GetModelSpaceMatrix() * original->GetEmitPos() * WORLD_TO_OBJECT_SPACE;
emitDir = GetModelSpaceMatrix() * float4(original->GetEmitDir(), 0.0f) * WORLD_TO_OBJECT_SPACE;
emitPos = GetModelSpaceTransform() * original->GetEmitPos() * WORLD_TO_OBJECT_SPACE;
emitDir = GetModelSpaceTransform() * float4(original->GetEmitDir(), 0.0f) * WORLD_TO_OBJECT_SPACE;
return true;
}

Expand Down Expand Up @@ -764,14 +764,14 @@ void S3DModel::SetPieceMatrices()
for (size_t i = 0; i < pieceObjects.size(); ++i) {
const auto* po = pieceObjects[i];
//traAlloc[0 + i] = po->bposeTransform.ToMatrix();
traAlloc.UpdateForced((0 + i), po->bposeTransform.ToMatrix());
traAlloc.UpdateForced((0 + i), po->bposeTransform);
}

// use this occasion and copy inverse bpose matrices
// store them right after all bind pose matrices
for (size_t i = 0; i < pieceObjects.size(); ++i) {
const auto* po = pieceObjects[i];
//traAlloc[numPieces + i] = po->bposeInvTransform.ToMatrix();
traAlloc.UpdateForced((numPieces + i), po->bposeInvTransform.ToMatrix());
traAlloc.UpdateForced((numPieces + i), po->bposeInvTransform);
}
}
5 changes: 2 additions & 3 deletions rts/Rendering/Models/3DModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ struct LocalModelPiece
}

// note: actually OBJECT_TO_WORLD but transform is the same
float3 GetAbsolutePos() const { return (GetModelSpaceMatrix().GetPos() * WORLD_TO_OBJECT_SPACE); }
float3 GetAbsolutePos() const { return (GetModelSpaceTransform().t * WORLD_TO_OBJECT_SPACE); }

bool GetEmitDirPos(float3& emitPos, float3& emitDir) const;

Expand Down Expand Up @@ -476,7 +476,7 @@ struct LocalModelPiece

const float3& GetDirection() const { return dir; }

CMatrix44f GetModelSpaceMatrix() const { if (dirty) UpdateParentMatricesRec(); return modelSpaceTra.ToMatrix(); }
Transform GetModelSpaceTransform() const { if (dirty) UpdateParentMatricesRec(); return modelSpaceTra; }

const CollisionVolume* GetCollisionVolume() const { return &colvol; }
CollisionVolume* GetCollisionVolume() { return &colvol; }
Expand Down Expand Up @@ -538,7 +538,6 @@ struct LocalModel

// raw forms, the piece-index must be valid
const float3 GetRawPiecePos(int pieceIdx) const { return pieces[pieceIdx].GetAbsolutePos(); }
const CMatrix44f GetRawPieceMatrix(int pieceIdx) const { return pieces[pieceIdx].GetModelSpaceMatrix(); }

// used by all SolidObject's; accounts for piece movement
float GetDrawRadius() const { return (boundingVolume.GetBoundingRadius()); }
Expand Down
7 changes: 4 additions & 3 deletions rts/Rendering/Models/ModelsMemStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@

#include "ModelsMemStorageDefs.h"
#include "ModelsLock.h"
#include "System/Matrix44f.h"
#include "System/Transform.hpp"
#include "System/MemPoolTypes.h"
#include "System/FreeListMap.h"
#include "System/UnorderedMap.hpp"
#include "System/TypeToStr.h"
#include "System/Threading/SpringThreading.h"
#include "Sim/Misc/GlobalConstants.h"
#include "Sim/Objects/SolidObjectDef.h"
#include "Rendering/Common/UpdateList.h"

class TransformsMemStorage {
public:
using MyType = CMatrix44f;
using MyType = Transform;
using EqualCmpFunctor = bool(*)(const MyType&, const MyType&);
public:
explicit TransformsMemStorage();
Expand Down Expand Up @@ -93,7 +94,7 @@ class ScopedTransformMemAlloc {
if (firstElem == TransformsMemStorage::INVALID_INDEX)
return;

transformsMemStorage.Free(firstElem, numElems, &CMatrix44f::Zero());
transformsMemStorage.Free(firstElem, numElems, &Transform::Zero());
}

bool Valid() const { return firstElem != TransformsMemStorage::INVALID_INDEX; }
Expand Down
4 changes: 2 additions & 2 deletions rts/Rendering/ModelsDataUploader.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <stddef.h>
#include <memory>

#include "System/Matrix44f.h"
#include "System/Transform.hpp"
#include "System/TypeToStr.h"
#include "Rendering/GL/StreamBuffer.h"
#include "Rendering/Models/ModelsMemStorageDefs.h"
Expand All @@ -21,7 +21,7 @@ struct S3DModel;
class TransformsUploader {
public:
using MyClassName = TransformsUploader;
using MyDataType = CMatrix44f;
using MyDataType = Transform;
public:
void Init();
void Kill();
Expand Down
2 changes: 1 addition & 1 deletion rts/Sim/Features/Feature.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class CFeature: public CSolidObject, public spring::noncopyable
// NOTE:
// unlike CUnit which recalculates the matrix on each call
// (and uses the synced and error args) CFeature caches it
CMatrix44f GetTransformMatrix(bool synced = false, bool fullread = false) const final { return transMatrix[synced]; }
CMatrix44f GetTransformMatrix(bool synced = false, bool fullread = false) const override final { return transMatrix[synced]; }
const CMatrix44f& GetTransformMatrixRef(bool synced = false) const { return transMatrix[synced]; }

private:
Expand Down
2 changes: 1 addition & 1 deletion rts/Sim/Misc/CollisionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ bool CCollisionHandler::IntersectPiecesHelper(
if (!lmp->GetScriptVisible() || lmpVol->IgnoreHits())
continue;

volMat = m * lmp->GetModelSpaceMatrix();
volMat = m * lmp->GetModelSpaceTransform().ToMatrix();
volMat.Translate(lmpVol->GetOffsets());

CollisionQuery cqn;
Expand Down
2 changes: 1 addition & 1 deletion rts/Sim/Misc/CollisionVolume.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ float CollisionVolume::GetPointSurfaceDistance(
assert(this == lmp->GetCollisionVolume());

// transform into piece-space relative to pos
vm <<= lmp->GetModelSpaceMatrix();
vm <<= lmp->GetModelSpaceTransform().ToMatrix();
} else {
// SObj::GetTransformMatrix does not include this
// (its translation component is pos, not midPos)
Expand Down
2 changes: 1 addition & 1 deletion rts/Sim/Objects/SolidObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ class CSolidObject: public CWorldObject {
void UpdateDirVectors(const float3& uDir);

CMatrix44f ComposeMatrix(const float3& p) const { return (CMatrix44f(p, -rightdir, updir, frontdir)); }
virtual CMatrix44f GetTransformMatrix(bool synced = false, bool fullread = false) const { return CMatrix44f(); };
virtual CMatrix44f GetTransformMatrix(bool synced = false, bool fullread = false) const = 0;

const CollisionVolume* GetCollisionVolume(const LocalModelPiece* lmp) const {
if (lmp == nullptr)
Expand Down
2 changes: 1 addition & 1 deletion rts/Sim/Units/Scripts/UnitScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ void CUnitScript::Shatter(int piece, const float3& pos, const float3& speed)

const float pieceChance = 1.0f - (projectileHandler.GetCurrentParticles() - (projectileHandler.maxParticles - 2000)) / 2000.0f;
if (pieceChance > 0.0f) {
const CMatrix44f m = unit->GetTransformMatrix() * lmp->GetModelSpaceMatrix();
const CMatrix44f m = unit->GetTransformMatrix() * lmp->GetModelSpaceTransform().ToMatrix();
omp->Shatter(pieceChance, unit->model->type, unit->model->textureType, unit->team, pos, speed, m);
}
}
Expand Down
4 changes: 2 additions & 2 deletions rts/Sim/Units/Scripts/UnitScript.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ class CUnitScript
return (p->PieceFunc()); \
}

SCRIPT_TO_LOCALPIECE_FUNC( float3, GetPiecePos, GetAbsolutePos )
SCRIPT_TO_LOCALPIECE_FUNC(CMatrix44f, GetPieceMatrix, GetModelSpaceMatrix)
SCRIPT_TO_LOCALPIECE_FUNC( float3, GetPiecePos , GetAbsolutePos )
SCRIPT_TO_LOCALPIECE_FUNC(Transform, GetPieceTransform, GetModelSpaceTransform)

bool GetEmitDirPos(int scriptPieceNum, float3& pos, float3& dir) const {
if (!PieceExists(scriptPieceNum))
Expand Down
2 changes: 1 addition & 1 deletion rts/Sim/Units/Unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ void CUnit::UpdateTransportees()
// slave transportee orientation to piece
if (tu.piece >= 0) {
const CMatrix44f& transMat = GetTransformMatrix(true);
const CMatrix44f& pieceMat = script->GetPieceMatrix(tu.piece);
const auto pieceMat = script->GetPieceTransform(tu.piece).ToMatrix();

transportee->SetDirVectors(transMat * pieceMat);
}
Expand Down
2 changes: 1 addition & 1 deletion rts/Sim/Units/Unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class CUnit : public CSolidObject
void EnableScriptMoveType();
void DisableScriptMoveType();

CMatrix44f GetTransformMatrix(bool synced = false, bool fullread = false) const final;
CMatrix44f GetTransformMatrix(bool synced = false, bool fullread = false) const override final;

void DependentDied(CObject* o);

Expand Down
Loading

0 comments on commit 8aab2c2

Please sign in to comment.