Skip to content

[ZH] Adjust field of view based on aspect ratio #518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions GeneralsMD/Code/GameEngine/Include/GameClient/View.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ class View : public Snapshot
virtual void forceCameraConstraintRecalc(void) {}
virtual void setGuardBandBias( const Coord2D *gb ) = 0;

virtual void adjustFovToAspectRatio(const Int width, const Int height) { };

protected:

friend class Display;
Expand Down
4 changes: 4 additions & 0 deletions GeneralsMD/Code/GameEngine/Source/GameClient/Display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ Bool Display::setDisplayMode( UnsignedInt xres, UnsignedInt yres, UnsignedInt bi
TheTacticalView->setHeight((Real)oldViewHeight/(Real)oldDisplayHeight*(Real)yres);
TheTacticalView->setOrigin((Real)oldViewOriginX/(Real)oldDisplayWidth*(Real)xres,
(Real)oldViewOriginY/(Real)oldDisplayHeight*(Real)yres);

// TheSuperHackers @tweak valeronm 25/03/2025 Adjust camera FOV according to display resolution
TheTacticalView->adjustFovToAspectRatio(xres, yres);

return TRUE;
}

Expand Down
9 changes: 7 additions & 2 deletions GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1165,10 +1165,15 @@ void InGameUI::init( void )
TheTacticalView->init();
TheDisplay->attachView( TheTacticalView );

Int displayWidth = TheDisplay->getWidth();
Int displayHeight = TheDisplay->getHeight();
// make the tactical display the full screen width for now
TheTacticalView->setWidth( TheDisplay->getWidth());
TheTacticalView->setWidth(displayWidth);
// make the tactical display 0.76 of full screen so no drawing under GUI.
TheTacticalView->setHeight( TheDisplay->getHeight() * 0.77f);
TheTacticalView->setHeight(displayHeight * 0.77f);

// TheSuperHackers @tweak valeronm 25/03/2025 Adjust camera FOV according to display resolution
TheTacticalView->adjustFovToAspectRatio(displayWidth, displayHeight);
}
TheTacticalView->setDefaultView(0.0f, 0.0f, 1.0f);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ class W3DView : public View, public SubsystemInterface

virtual void setGuardBandBias( const Coord2D *gb ) { m_guardBandBias.x = gb->x; m_guardBandBias.y = gb->y; }


virtual void adjustFovToAspectRatio(const Int width, const Int height);
private:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add blank line before this


CameraClass *m_3DCamera; ///< camera representation for 3D scene
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,48 @@ void W3DView::setWidth(Int width)
m_3DCamera->Get_Viewport(vMin,vMax);
vMax.X=(Real)(m_originX+width)/(Real)TheDisplay->getWidth();
m_3DCamera->Set_Viewport(vMin,vMax);
}

// TheSuperHackers @tweak valeronm 25/03/2025 Adjusting FOV to look similar to what we have in in 4:3 (hFOV: 50, vFOV 38.55)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in in

void W3DView::adjustFovToAspectRatio(const Int width, const Int height)
{
static const Real fixedHFOV = 50.00f;
static const Real fixedVFOV = 38.55f;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use CONSTEXPR instead of static

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did the magical 38.55 come to be?


//we want to maintain the same scale, so we'll need to adjust the fov.
//default W3D fov for full-screen is 50 degrees.
m_3DCamera->Set_View_Plane((Real)width/(Real)TheDisplay->getWidth()*DEG_TO_RADF(50.0f),-1);
const Real aspectRatio = static_cast<Real>(width)/ static_cast<Real>(height);
Real vFOVRad, hFOVRad;
Bool drawEntireTerrain = false;
if (aspectRatio >= 4.0f / 3.0f)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For Multiplayer, I very much prefer we cap this at max 16/9, min 9/16, to not give these ultra screens a massive advantage, preferably configurable in GameData.ini, so that Mods can do their own setups.

For Singleplayer and Replay it is fine to go wide with no limits.

{
// Wider than 4:3 resolutions get fixed vFOV 38.55
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment repeats the code value that is assigned to fixedVFOV. This means the commented value can go out of sync when someone changes the code value. Prefer not baking values into comments, unless they definitely never change.

vFOVRad = DEG_TO_RADF(fixedVFOV);
hFOVRad = 2.0f * atan(tan(vFOVRad / 2.0f) * aspectRatio);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not mind a brief explanation in a comment for the math how the fov is scaled.

drawEntireTerrain = true;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks suspicious. The GenTool implementation did not need this for up to 16:9. The original game also did not need this for 4:3. It will affect performance.

}
else if (aspectRatio >= 1.0)
{
// Narrower than 4:3 but not yet portrait resolutions get fixed hFOV 50.0
hFOVRad = DEG_TO_RADF(fixedHFOV);
vFOVRad = 2.0f * atan(tan(hFOVRad / 2.0f) / aspectRatio);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there no cut in fov'ness between the different conditions? How does this work?

}
else
{
// Portrait resolutions get fixed vFOV adjusted to aspect ratio
// vFOV increases from 50.0 for 1:1 to 60.0 for 9:21 and then fixed 60.0
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason for this fine tuning?

static const Real adjMinRatio = 9.0f / 21.0f;
static const Real adjValue = 10.0f;

Real adjustment;
if (aspectRatio < adjMinRatio)
adjustment = adjValue;
else
adjustment = (1 - aspectRatio) * adjValue / (1 - adjMinRatio);
vFOVRad = DEG_TO_RADF(50 + adjustment);
hFOVRad = 2.0f * atan(tan(vFOVRad / 2.0f) * aspectRatio);
drawEntireTerrain = true;
}
TheWritableGlobalData->m_drawEntireTerrain = drawEntireTerrain;
m_3DCamera->Set_View_Plane(hFOVRad, vFOVRad);
}

//-------------------------------------------------------------------------------------------------
Expand Down
Loading