CalculateVertices Refactor

- Added roll support for Y billboarding
- Fixed a bunch of broken checks that prevented Y billboarding from working properly
- Y billboarding takes precedence over sprite facing
- Optimized ROLLCENTER: now combines the sprite's embedded offsets with SpriteOffsets instead of doing wasteful transforms before/after rotations
- Greatly cleaned up a bunch of cruft
This commit is contained in:
Major Cooke 2024-01-19 20:17:13 -06:00 committed by Rachael Alexanderson
parent 1c7f195353
commit 4e48250cf2

View file

@ -340,19 +340,32 @@ void HWSprite::DrawSprite(HWDrawInfo *di, FRenderState &state, bool translucent)
// //
//========================================================================== //==========================================================================
bool HWSprite::CalculateVertices(HWDrawInfo *di, FVector3 *v, DVector3 *vp) void HandleSpriteOffsets(Matrix3x4 *mat, const FRotator *HW, FVector2 *offset, bool XYBillboard)
{ {
const auto &HWAngles = di->Viewpoint.HWAngles; FAngle zero = FAngle::fromDeg(0);
FAngle pitch = (XYBillboard) ? HW->Pitch : zero;
FAngle yaw = FAngle::fromDeg(270.) - HW->Yaw;
FQuaternion quat = FQuaternion::FromAngles(yaw, pitch, zero);
FVector3 sideVec = quat * FVector3(0, 1, 0);
FVector3 upVec = quat * FVector3(0, 0, 1);
FVector3 res = sideVec * offset->X + upVec * offset->Y;
mat->Translate(res.X, res.Z, res.Y);
}
bool HWSprite::CalculateVertices(HWDrawInfo* di, FVector3* v, DVector3* vp)
{
FVector3 center = FVector3((x1 + x2) * 0.5, (y1 + y2) * 0.5, (z1 + z2) * 0.5);
const auto& HWAngles = di->Viewpoint.HWAngles;
Matrix3x4 mat;
if (actor != nullptr && (actor->renderflags & RF_SPRITETYPEMASK) == RF_FLATSPRITE) if (actor != nullptr && (actor->renderflags & RF_SPRITETYPEMASK) == RF_FLATSPRITE)
{ {
Matrix3x4 mat;
mat.MakeIdentity();
// [MC] Rotate around the center or offsets given to the sprites. // [MC] Rotate around the center or offsets given to the sprites.
// Counteract any existing rotations, then rotate the angle. // Counteract any existing rotations, then rotate the angle.
// Tilt the actor up or down based on pitch (increase 'somersaults' forward). // Tilt the actor up or down based on pitch (increase 'somersaults' forward).
// Then counteract the roll and DO A BARREL ROLL. // Then counteract the roll and DO A BARREL ROLL.
mat.MakeIdentity();
FAngle pitch = FAngle::fromDeg(-Angles.Pitch.Degrees()); FAngle pitch = FAngle::fromDeg(-Angles.Pitch.Degrees());
pitch.Normalized180(); pitch.Normalized180();
@ -362,12 +375,9 @@ bool HWSprite::CalculateVertices(HWDrawInfo *di, FVector3 *v, DVector3 *vp)
if (actor->renderflags & RF_ROLLCENTER) if (actor->renderflags & RF_ROLLCENTER)
{ {
float cx = (x1 + x2) * 0.5; mat.Translate(center.X - x, 0, center.Y - y);
float cy = (y1 + y2) * 0.5;
mat.Translate(cx - x, 0, cy - y);
mat.Rotate(0, 1, 0, - Angles.Roll.Degrees()); mat.Rotate(0, 1, 0, - Angles.Roll.Degrees());
mat.Translate(-cx, -z, -cy); mat.Translate(-center.X, -z, -center.Y);
} }
else else
{ {
@ -393,51 +403,45 @@ bool HWSprite::CalculateVertices(HWDrawInfo *di, FVector3 *v, DVector3 *vp)
// [Nash] has +ROLLSPRITE // [Nash] has +ROLLSPRITE
const bool drawRollSpriteActor = (actor != nullptr && actor->renderflags & RF_ROLLSPRITE); const bool drawRollSpriteActor = (actor != nullptr && actor->renderflags & RF_ROLLSPRITE);
const bool drawRollParticle = (particle != nullptr && particle->flags & SPF_ROLL); const bool drawRollParticle = (particle != nullptr && particle->flags & SPF_ROLL);
const bool doRoll = (drawRollSpriteActor || drawRollParticle);
// [fgsfds] check sprite type mask // [fgsfds] check sprite type mask
uint32_t spritetype = (uint32_t)-1; uint32_t spritetype = (uint32_t)-1;
if (actor != nullptr) spritetype = actor->renderflags & RF_SPRITETYPEMASK; if (actor != nullptr) spritetype = actor->renderflags & RF_SPRITETYPEMASK;
// [Nash] is a flat sprite // [Nash] is a flat sprite
const bool isFlatSprite = (actor != nullptr) && (spritetype == RF_WALLSPRITE); const bool isWallSprite = (actor != nullptr) && (spritetype == RF_WALLSPRITE);
const bool useOffsets = (actor != nullptr) && !(actor->renderflags & RF_ROLLCENTER); const bool useOffsets = (actor != nullptr) && !(actor->renderflags & RF_ROLLCENTER);
// [Nash] check for special sprite drawing modes FVector2 offset = FVector2( -offx, -offy );
if (drawWithXYBillboard || drawBillboardFacingCamera || drawRollSpriteActor || drawRollParticle || isFlatSprite)
{
// Compute center of sprite
float xcenter = (x1 + x2)*0.5;
float ycenter = (y1 + y2)*0.5;
float zcenter = (z1 + z2)*0.5;
float xx = -xcenter + x;
float zz = -zcenter + z;
float yy = -ycenter + y;
Matrix3x4 mat;
mat.MakeIdentity();
mat.Translate(xcenter, zcenter, ycenter); // move to sprite center
// [MC] Sprite offsets. These must be calculated separately in their own matrix, // Account for +ROLLCENTER flag. Takes the embedded image offsets and adds them in with SpriteOffsets.
// otherwise "face sprites" would cause some issues whenever enabled. We don't if (drawRollSpriteActor && useOffsets)
// want those calculations here. Credit to PhantomBeta for this.
if (offx || offy)
{ {
FQuaternion quat = FQuaternion::FromAngles(FAngle::fromDeg(270) - di->Viewpoint.HWAngles.Yaw, di->Viewpoint.HWAngles.Pitch, FAngle::fromDeg(0)); offset.X += center.X - x;
FVector3 sideVec = quat * FVector3(0, 1, 0); offset.Y += center.Z - z;
FVector3 upVec = quat * FVector3(0, 0, 1);
FVector3 res = sideVec * -offx + upVec * -offy;
mat.Translate(res.X, res.Z, res.Y);
} }
// [Nash] check for special sprite drawing modes
if (drawWithXYBillboard || isWallSprite)
{
// Compute center of sprite
mat.MakeIdentity();
mat.Translate(center.X, center.Z, center.Y); // move to sprite center
// [MC] Sprite offsets.
if (!offset.isZero())
HandleSpriteOffsets(&mat, &HWAngles, &offset, true);
// Order of rotations matters. Perform yaw rotation (Y, face camera) before pitch (X, tilt up/down). // Order of rotations matters. Perform yaw rotation (Y, face camera) before pitch (X, tilt up/down).
if (drawBillboardFacingCamera && !isFlatSprite) if (drawBillboardFacingCamera && !isWallSprite)
{ {
// [CMB] Rotate relative to camera XY position, not just camera direction, // [CMB] Rotate relative to camera XY position, not just camera direction,
// which is nicer in VR // which is nicer in VR
float xrel = xcenter - vp->X; float xrel = center.X - vp->X;
float yrel = ycenter - vp->Y; float yrel = center.Y - vp->Y;
float absAngleDeg = atan2(-yrel, xrel) * (180 / M_PI); float absAngleDeg = atan2(-yrel, xrel) * (180 / M_PI);
float counterRotationDeg = 270. - HWAngles.Yaw.Degrees(); // counteracts existing sprite rotation float counterRotationDeg = 270. - HWAngles.Yaw.Degrees(); // counteracts existing sprite rotation
float relAngleDeg = counterRotationDeg + absAngleDeg; float relAngleDeg = counterRotationDeg + absAngleDeg;
@ -446,32 +450,27 @@ bool HWSprite::CalculateVertices(HWDrawInfo *di, FVector3 *v, DVector3 *vp)
} }
// [fgsfds] calculate yaw vectors // [fgsfds] calculate yaw vectors
float rollDegrees = 0; float rollDegrees = doRoll ? Angles.Roll.Degrees() : 0;
float angleRad = (FAngle::fromDeg(270.) - HWAngles.Yaw).Radians(); float angleRad = (FAngle::fromDeg(270.) - HWAngles.Yaw).Radians();
if (actor || drawRollParticle) rollDegrees = Angles.Roll.Degrees();
// [fgsfds] Rotate the sprite about the sight vector (roll) // [fgsfds] Rotate the sprite about the sight vector (roll)
if (spritetype == RF_WALLSPRITE) if (isWallSprite)
{ {
float yawvecX = Angles.Yaw.Cos(); float yawvecX = Angles.Yaw.Cos();
float yawvecY = Angles.Yaw.Sin(); float yawvecY = Angles.Yaw.Sin();
mat.Rotate(0, 1, 0, 0); mat.Rotate(0, 1, 0, 0);
if (drawRollSpriteActor) if (drawRollSpriteActor)
{ {
if (useOffsets) mat.Translate(xx, zz, yy);
mat.Rotate(yawvecX, 0, yawvecY, rollDegrees); mat.Rotate(yawvecX, 0, yawvecY, rollDegrees);
if (useOffsets) mat.Translate(-xx, -zz, -yy);
} }
} }
else if (drawRollSpriteActor || drawRollParticle) else if (doRoll)
{ {
if (useOffsets) mat.Translate(xx, zz, yy);
if (drawWithXYBillboard) if (drawWithXYBillboard)
{ {
mat.Rotate(-sin(angleRad), 0, cos(angleRad), -HWAngles.Pitch.Degrees()); mat.Rotate(-sin(angleRad), 0, cos(angleRad), -HWAngles.Pitch.Degrees());
} }
mat.Rotate(cos(angleRad), 0, sin(angleRad), rollDegrees); mat.Rotate(cos(angleRad), 0, sin(angleRad), rollDegrees);
if (useOffsets) mat.Translate(-xx, -zz, -yy);
} }
else if (drawWithXYBillboard) else if (drawWithXYBillboard)
{ {
@ -481,7 +480,7 @@ bool HWSprite::CalculateVertices(HWDrawInfo *di, FVector3 *v, DVector3 *vp)
mat.Rotate(-sin(angleRad), 0, cos(angleRad), -HWAngles.Pitch.Degrees()); mat.Rotate(-sin(angleRad), 0, cos(angleRad), -HWAngles.Pitch.Degrees());
} }
mat.Translate(-xcenter, -zcenter, -ycenter); // retreat from sprite center mat.Translate(-center.X, -center.Z, -center.Y); // retreat from sprite center
v[0] = mat * FVector3(x1, z1, y1); v[0] = mat * FVector3(x1, z1, y1);
v[1] = mat * FVector3(x2, z1, y2); v[1] = mat * FVector3(x2, z1, y2);
@ -489,12 +488,40 @@ bool HWSprite::CalculateVertices(HWDrawInfo *di, FVector3 *v, DVector3 *vp)
v[3] = mat * FVector3(x2, z2, y2); v[3] = mat * FVector3(x2, z2, y2);
} }
else // traditional "Y" billboard mode else // traditional "Y" billboard mode
{
if (doRoll || !offset.isZero())
{
mat.MakeIdentity();
if (!offset.isZero())
HandleSpriteOffsets(&mat, &HWAngles, &offset, false);
if (doRoll)
{
// Compute center of sprite
float angleRad = (FAngle::fromDeg(270.) - HWAngles.Yaw).Radians();
float rollDegrees = Angles.Roll.Degrees();
mat.Translate(center.X, center.Z, center.Y);
mat.Rotate(cos(angleRad), 0, sin(angleRad), rollDegrees);
mat.Translate(-center.X, -center.Z, -center.Y);
}
v[0] = mat * FVector3(x1, z1, y1);
v[1] = mat * FVector3(x2, z1, y2);
v[2] = mat * FVector3(x1, z2, y1);
v[3] = mat * FVector3(x2, z2, y2);
}
else
{ {
v[0] = FVector3(x1, z1, y1); v[0] = FVector3(x1, z1, y1);
v[1] = FVector3(x2, z1, y2); v[1] = FVector3(x2, z1, y2);
v[2] = FVector3(x1, z2, y1); v[2] = FVector3(x1, z2, y1);
v[3] = FVector3(x2, z2, y2); v[3] = FVector3(x2, z2, y2);
} }
}
return false; return false;
} }
@ -1252,7 +1279,6 @@ void HWSprite::Process(HWDrawInfo *di, AActor* thing, sector_t * sector, area_t
{ {
lightlist = nullptr; lightlist = nullptr;
} }
PutSprite(di, hw_styleflags != STYLEHW_Solid); PutSprite(di, hw_styleflags != STYLEHW_Solid);
rendered_sprites++; rendered_sprites++;
} }