Skip to content
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

Centralize basic math types and functions (part 1) #368

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
6 changes: 3 additions & 3 deletions OVP/D3D9Client/CSphereMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ void CSphereManager::Render (LPDIRECT3DDEVICE9 dev, int level, double bglvl)
CelFlow.bBeta = m_bStarImg;
CelData.fAlpha = intens;
CelData.fBeta = bgscale;
CelData.mViewProj = *scn->GetProjectionViewMatrix();
CelData.mViewProj = to_FMATRIX4(*scn->GetProjectionViewMatrix());

pShader->Setup(pPatchVertexDecl, false, 0);
pShader->ClearTextures();
Expand Down Expand Up @@ -502,8 +502,8 @@ void CSphereManager::RenderTile (int lvl, int hemisp, int ilat, int nlat, int il
VBMESH &mesh = PATCH_TPL[lvl][ilat]; // patch template

D3D9Stats.Old.Tiles[lvl]++;
CelData.mWorld = mWorld;

CelData.mWorld = to_FMATRIX4(mWorld);

pShader->SetTexture(hTexA, tex, IPF_CLAMP | IPF_ANISOTROPIC);
pShader->SetTexture(hTexB, ltex, IPF_CLAMP | IPF_ANISOTROPIC);
Expand Down
22 changes: 11 additions & 11 deletions OVP/D3D9Client/CelSphere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,11 @@ void D3D9CelestialSphere::Render(LPDIRECT3DDEVICE9 pDevice, const VECTOR3& skyCo
// render ecliptic grid
if (renderFlag & PLN_EGRID) {
FVECTOR4 baseCol1(0.0f, 0.2f, 0.3f, 1.0f);
D3DXVECTOR4 vColor1 = ColorAdjusted(baseCol1);
auto vColor1 = to_D3DXVECTOR4(ColorAdjusted(baseCol1));
HR(s_FX->SetVector(s_eColor, &vColor1));
RenderGrid(s_FX, false);
FVECTOR4 baseCol2(0.0f, 0.4f, 0.6f, 1.0f);
D3DXVECTOR4 vColor2 = ColorAdjusted(baseCol2);
auto vColor2 = to_D3DXVECTOR4(ColorAdjusted(baseCol2));
HR(s_FX->SetVector(s_eColor, &vColor2));
RenderGreatCircle(s_FX);
MATRIX3 ident = _M(1, 0, 0, 0, 1, 0, 0, 0, 1);
Expand All @@ -355,11 +355,11 @@ void D3D9CelestialSphere::Render(LPDIRECT3DDEVICE9 pDevice, const VECTOR3& skyCo
D3DXMatrixMultiply(&rot, &T, m_scene->GetProjectionViewMatrix());
HR(s_FX->SetMatrix(s_eWVP, &rot));
FVECTOR4 baseCol1(0.3f, 0.0f, 0.0f, 1.0f);
D3DXVECTOR4 vColor1 = ColorAdjusted(baseCol1);
auto vColor1 = to_D3DXVECTOR4(ColorAdjusted(baseCol1));
HR(s_FX->SetVector(s_eColor, &vColor1));
RenderGrid(s_FX, false);
FVECTOR4 baseCol2(0.7f, 0.0f, 0.0f, 1.0f);
D3DXVECTOR4 vColor2 = ColorAdjusted(baseCol2);
auto vColor2 = to_D3DXVECTOR4(ColorAdjusted(baseCol2));
HR(s_FX->SetVector(s_eColor, &vColor2));
RenderGreatCircle(s_FX);
double dphi = ElevationScaleRotation(R);
Expand All @@ -374,11 +374,11 @@ void D3D9CelestialSphere::Render(LPDIRECT3DDEVICE9 pDevice, const VECTOR3& skyCo
D3DXMatrixMultiply(&rot, &m_transformCelestial, m_scene->GetProjectionViewMatrix());
HR(s_FX->SetMatrix(s_eWVP, &rot));
FVECTOR4 baseCol1(0.3f, 0.0f, 0.3f, 1.0f);
D3DXVECTOR4 vColor1 = ColorAdjusted(baseCol1);
auto vColor1 = to_D3DXVECTOR4(ColorAdjusted(baseCol1));
HR(s_FX->SetVector(s_eColor, &vColor1));
RenderGrid(s_FX, false);
FVECTOR4 baseCol2(0.7f, 0.0f, 0.7f, 1.0f);
D3DXVECTOR4 vColor2 = ColorAdjusted(baseCol2);
auto vColor2 = to_D3DXVECTOR4(ColorAdjusted(baseCol2));
HR(s_FX->SetVector(s_eColor, &vColor2));
RenderGreatCircle(s_FX);
double dphi = ElevationScaleRotation(m_rotCelestial);
Expand All @@ -393,11 +393,11 @@ void D3D9CelestialSphere::Render(LPDIRECT3DDEVICE9 pDevice, const VECTOR3& skyCo
D3DXMatrixMultiply(&rot, &T, m_scene->GetProjectionViewMatrix());
HR(s_FX->SetMatrix(s_eWVP, &rot));
oapi::FVECTOR4 baseCol1(0.2f, 0.2f, 0.0f, 1.0f);
D3DXVECTOR4 vColor1 = ColorAdjusted(baseCol1);
auto vColor1 = to_D3DXVECTOR4(ColorAdjusted(baseCol1));
HR(s_FX->SetVector(s_eColor, &vColor1));
RenderGrid(s_FX, false);
oapi::FVECTOR4 baseCol2(0.5f, 0.5f, 0.0f, 1.0f);
D3DXVECTOR4 vColor2 = ColorAdjusted(baseCol2);
auto vColor2 = to_D3DXVECTOR4(ColorAdjusted(baseCol2));
HR(s_FX->SetVector(s_eColor, &vColor2));
RenderGreatCircle(s_FX);
double dphi = ElevationScaleRotation(R);
Expand All @@ -421,7 +421,7 @@ void D3D9CelestialSphere::Render(LPDIRECT3DDEVICE9 pDevice, const VECTOR3& skyCo
D3DXMatrixMultiply(&rot, &iR, m_scene->GetProjectionViewMatrix());
HR(s_FX->SetMatrix(s_eWVP, &rot));
FVECTOR4 baseCol(0.0f, 0.6f, 0.0f, 1.0f);
D3DXVECTOR4 vColor = ColorAdjusted(baseCol);
auto vColor = to_D3DXVECTOR4(ColorAdjusted(baseCol));
HR(s_FX->SetVector(s_eColor, &vColor));
RenderGreatCircle(s_FX);
}
Expand Down Expand Up @@ -497,7 +497,7 @@ void D3D9CelestialSphere::RenderStars(ID3DXEffect *FX)
void D3D9CelestialSphere::RenderConstellationLines(ID3DXEffect *FX)
{
const FVECTOR4 baseCol(0.5f, 0.3f, 0.2f, 1.0f);
D3DXVECTOR4 vColor = ColorAdjusted(baseCol);
auto vColor = to_D3DXVECTOR4(ColorAdjusted(baseCol));
HR(s_FX->SetVector(s_eColor, &vColor));

_TRACE;
Expand All @@ -516,7 +516,7 @@ void D3D9CelestialSphere::RenderConstellationLines(ID3DXEffect *FX)
void D3D9CelestialSphere::RenderConstellationBoundaries(ID3DXEffect* FX)
{
const FVECTOR4 baseCol(0.25f, 0.2f, 0.15f, 1.0f);
D3DXVECTOR4 vColor = ColorAdjusted(baseCol);
auto vColor = to_D3DXVECTOR4(ColorAdjusted(baseCol));
HR(s_FX->SetVector(s_eColor, &vColor));

_TRACE;
Expand Down
2 changes: 1 addition & 1 deletion OVP/D3D9Client/Cloudmgr2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void CloudTile::Render()
sp->vMicroOff = GetTexRangeDX(&microrange);
sp->fAlpha = 1.0f;
sp->fBeta = 1.0f;
sp->mWorld = mWorld;
sp->mWorld = to_FMATRIX4(mWorld);

// -------------------------------------------------------------------
// render surface mesh
Expand Down
2 changes: 1 addition & 1 deletion OVP/D3D9Client/D3D9Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1683,7 +1683,7 @@ LRESULT D3D9Client::RenderWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM l
xpos = GET_X_LPARAM(lParam);
ypos = GET_Y_LPARAM(lParam);

GetScene()->vPickRay = GetScene()->GetPickingRay(xpos, ypos);
GetScene()->vPickRay = to_FVECTOR3( GetScene()->GetPickingRay(xpos, ypos) );

TRACKMOUSEEVENT te; te.cbSize = sizeof(TRACKMOUSEEVENT); te.dwFlags = TME_LEAVE; te.hwndTrack = hRenderWnd;
TrackMouseEvent(&te);
Expand Down
8 changes: 4 additions & 4 deletions OVP/D3D9Client/D3D9Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,9 @@ void SurfaceLighting(D3D9Sun *light, OBJHANDLE hP, OBJHANDLE hO, float ao)
lcol *= 1.0f-amb*0.5f; // reduce direct light component to avoid overexposure
}

light->Color = D3DXCOLOR(lcol.x, lcol.y, lcol.z, 1.0f);
light->Ambient = D3DXCOLOR(amb, amb, amb, 1.0f);
light->Dir = D3DXVEC(S) * (-1.0f/s);
light->Color = to_FVECTOR3(lcol);
light->Ambient = FVECTOR3{amb, amb, amb};
light->Dir = to_FVECTOR3(D3DXVEC(S) / -s);
}
// ===========================================
// Remove unecessary spaces and tablations
Expand Down Expand Up @@ -1375,7 +1375,7 @@ void D3D9Light::UpdateLight(const LightEmitter *_le, const class vObject *vo)
// -----------------------------------------------------------------------------
if (Type != 0) {
D3DXVec3TransformNormal(&Direction, ptr(D3DXVEC(le->GetDirection())), vo->MWorld());
float angle = acos(dot(unit(Position), Direction));
float angle = acos(dot(unit(to_FVECTOR3(Position)), to_FVECTOR3(Direction)));
cone = ilerp(U * 0.5f, P * 0.5f, angle);
}
}
Expand Down
20 changes: 20 additions & 0 deletions OVP/D3D9Client/D3D9Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,26 @@ inline float D3DVAL (double x)
return (float)x;
}

inline auto to_FVECTOR3(const D3DXVECTOR3 &v) { return FVECTOR3{v.x, v.y, v.z}; }
inline auto to_FVECTOR4(const D3DXVECTOR4 &v) { return FVECTOR4{v.x, v.y, v.z, v.w}; }

inline auto to_D3DXVECTOR3(const FVECTOR3 &v) { return D3DXVECTOR3{v.x, v.y, v.z}; }
inline auto to_D3DXVECTOR4(const FVECTOR4 &v) { return D3DXVECTOR4{v.x, v.y, v.z, v.w}; }

inline auto to_FMATRIX4(const D3DXMATRIX &m)
{
return FMATRIX4{
m(0,0), m(0,1), m(0,2), m(0,3),
m(1,0), m(1,1), m(1,2), m(1,3),
m(2,0), m(2,1), m(2,2), m(2,3),
m(3,0), m(3,1), m(3,2), m(3,3),
};
}

inline auto to_D3DXCOLOR(const FVECTOR3 &v) { return D3DXCOLOR{v.x, v.y, v.z, 1}; }

inline auto to_FVECTOR4(const D3DXCOLOR &c) { return FVECTOR4{c.r, c.g, c.b, c.a}; }

//char* _fgets(char* cbuf, int num, FILE* stream, bool keepOneSpace = false);

int fgets2(char *buf, int cmax, FILE *file, DWORD param=0);
Expand Down
12 changes: 6 additions & 6 deletions OVP/D3D9Client/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2716,11 +2716,11 @@ void D3D9Mesh::RenderShadowMap(const LPD3DXMATRIX pW, const LPD3DXMATRIX pVP, in
D3DXMATRIX GroupMatrix, mWorldMesh;

MeshShader* pShader = nullptr;

MeshShader::vs_const.mVP = *pVP;

D3DXMatrixIdentity(MeshShader::vs_const.mW);

MeshShader::vs_const.mVP = to_FMATRIX4(*pVP);

D3DXMatrixIdentity((D3DXMATRIX*)&MeshShader::vs_const.mW);

if (bGlobalTF) D3DXMatrixMultiply(&mWorldMesh, &mTransform, pW);
else mWorldMesh = *pW;

Expand Down Expand Up @@ -2768,11 +2768,11 @@ void D3D9Mesh::RenderShadowMap(const LPD3DXMATRIX pW, const LPD3DXMATRIX pVP, in
}

if (Grp[g].bTransform) {
D3DXMatrixMultiply(MeshShader::vs_const.mW, &pGrpTF[g], pW); // Apply Animations to instance matrices
D3DXMatrixMultiply((D3DXMATRIX*)&MeshShader::vs_const.mW, &pGrpTF[g], pW); // Apply Animations to instance matrices
bInit = true;
}
else {
if (bInit) MeshShader::vs_const.mW = mWorldMesh;
if (bInit) MeshShader::vs_const.mW = to_FMATRIX4(mWorldMesh);
bInit = false;
}

Expand Down
18 changes: 9 additions & 9 deletions OVP/D3D9Client/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,7 @@ void Scene::ComputeLocalLightsVisibility()
{
if (Lights[i].cone > 0.0f) {
LLCBuf[nGlares].index = float(nGlares);
LLCBuf[nGlares].pos = Lights[i].Position;
LLCBuf[nGlares].pos = to_FVECTOR3(Lights[i].Position);
LLCBuf[nGlares].cone = Lights[i].cone;
Lights[i].GPUId = nGlares;
nGlares++;
Expand All @@ -1191,7 +1191,7 @@ void Scene::ComputeLocalLightsVisibility()
psgBuffer[GBUF_DEPTH]->GetDesc(&desc);

ComputeData.vSrc = FVECTOR4((float)desc.Width, (float)desc.Height, 1.0f / (float)desc.Width, 1.0f / (float)desc.Height);
ComputeData.vDir = Camera.z;
ComputeData.vDir = to_FVECTOR3(Camera.z);
ComputeData.mSVP = Camera.mProjView;

// Must setup render target before calling Setup()
Expand Down Expand Up @@ -1514,7 +1514,7 @@ void Scene::RenderMainScene()
SmapRenderList.clear();
SmapRenderList.push_back(vFocus);

D3DXVECTOR3 ld = sunLight.Dir;
auto ld = to_D3DXVECTOR3(sunLight.Dir);
D3DXVECTOR3 pos = vFocus->GetBoundingSpherePosDX();
float rad = vFocus->GetBoundingSphereRadius();
float frad = rad;
Expand Down Expand Up @@ -1779,7 +1779,7 @@ void Scene::RenderMainScene()
//
if (Config->ShadowMapMode >= 1) {

D3DXVECTOR3 ld = sunLight.Dir;
auto ld = to_D3DXVECTOR3(sunLight.Dir);
D3DXVECTOR3 pos = vFocus->GetBoundingSpherePosDX();
float rad = vFocus->GetBoundingSphereRadius();

Expand Down Expand Up @@ -1825,7 +1825,7 @@ void Scene::RenderMainScene()

while (!Intersect.empty()) {

D3DXVECTOR3 ld = sunLight.Dir;
auto ld = to_D3DXVECTOR3(sunLight.Dir);
D3DXVECTOR3 pos = Intersect.front()->GetBoundingSpherePosDX();
float rad = Intersect.front()->GetBoundingSphereRadius();

Expand Down Expand Up @@ -2377,7 +2377,7 @@ D3DXCOLOR Scene::GetSunDiffColor()

float pwr = 1.0f;

if (hP == hS) return GetSun()->Color;
if (hP == hS) return to_D3DXCOLOR(GetSun()->Color);

double r = length(P);
double pres = 1000.0;
Expand Down Expand Up @@ -3173,7 +3173,7 @@ FMATRIX4 Scene::PushCameraFrustumLimits(float nearlimit, float farlimit)
FRUSTUM fr = { Camera.nearplane, Camera.farplane };
FrustumStack.push(fr);
SetCameraFrustumLimits(nearlimit, farlimit);
return FMATRIX4(GetProjectionViewMatrix());
return to_FMATRIX4(*GetProjectionViewMatrix());
}

// ===========================================================================================
Expand All @@ -3182,7 +3182,7 @@ FMATRIX4 Scene::PopCameraFrustumLimits()
{
SetCameraFrustumLimits(FrustumStack.top().znear, FrustumStack.top().zfar);
FrustumStack.pop();
return FMATRIX4(GetProjectionViewMatrix());
return to_FMATRIX4(*GetProjectionViewMatrix());
}

// ===========================================================================================
Expand Down Expand Up @@ -3669,7 +3669,7 @@ void Scene::RenderGlares()
Const.GPUId = (float(GPUId) + 0.5f) / desc.Width;
Const.Pos = FVECTOR4(pt.x, pt.y, size, size);
Const.Alpha = Lights[i].cone;
Const.Color = Lights[i].Diffuse;
Const.Color = to_FVECTOR4(Lights[i].Diffuse);
Const.Blend = 1.0f;
pRenderGlares->SetVSConstants("Const", &Const, sizeof(Const));
pRenderGlares->SetPSConstants("Const", &Const, sizeof(Const));
Expand Down
12 changes: 6 additions & 6 deletions OVP/D3D9Client/Surfmgr2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,7 @@ void SurfTile::Render ()

sp->vTexOff = GetTexRangeDX(&texrange);
sp->vMicroOff = GetTexRangeDX(&microrange);
sp->mWorld = mWorld;
sp->mWorld = to_FMATRIX4(mWorld);
sp->fTgtScale = tgtscale;

if (has_lights) sp->fBeta = float(mgr->Cprm().lightfac);
Expand Down Expand Up @@ -1003,7 +1003,7 @@ void SurfTile::Render ()
if (sqrt(D3DXVec3Dot(&bc, &bc) - x * x) < (shd->rad + mesh->bsRad)) {
float s = float(shd->size);
float sr = 2.0f * shd->rad / s;
sp->mLVP = shd->mViewProj;
sp->mLVP = to_FMATRIX4(shd->mViewProj);
sp->vSHD = FVECTOR4(sr, 1.0f / s, 0.0f, 1.0f / shd->depth);
fc->bShadows = true;
}
Expand Down Expand Up @@ -1066,11 +1066,11 @@ void SurfTile::Render ()
for (int i = 0; i < nMeshLights; i++)
{
auto pL = pLights[LightList[i].idx];
Locals.attenuation[i] = pL.Attenuation;
Locals.attenuation[i] = to_FVECTOR3(pL.Attenuation);
Locals.diffuse[i] = FVECTOR4(pL.Diffuse).rgb;
Locals.direction[i] = pL.Direction;
Locals.param[i] = pL.Param;
Locals.position[i] = pL.Position;
Locals.direction[i] = to_FVECTOR3(pL.Direction);
Locals.param[i] = to_FVECTOR4(pL.Param);
Locals.position[i] = to_FVECTOR3(pL.Position);
Spots[i] = (pL.Type == 1);
}

Expand Down
4 changes: 2 additions & 2 deletions OVP/D3D9Client/VBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,8 @@ bool vBase::RenderStructures(LPDIRECT3DDEVICE9 dev)

// render generic objects above shadows
for (DWORD i=0; i<nstructure_as; i++) {
FVECTOR3 bs = structure_as[i]->GetBoundingSpherePos();
FVECTOR3 qw = TransformCoord(bs, mWorld);
auto bs = to_FVECTOR3(structure_as[i]->GetBoundingSpherePos());
auto qw = TransformCoord(bs, to_FMATRIX4(mWorld));
D3D9Sun sp = vP->GetObjectAtmoParams(qw._V() + vP->CameraPos());
structure_as[i]->SetSunLight(&sp);
structure_as[i]->Render(&mWorld, RENDER_BASE);
Expand Down
2 changes: 1 addition & 1 deletion OVP/D3D9Client/VPlanet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ bool vPlanet::Render(LPDIRECT3DDEVICE9 dev)
{

// Must update the latest view projection matrix
cp.mVP = *scn->GetProjectionViewMatrix();
cp.mVP = to_FMATRIX4(*scn->GetProjectionViewMatrix());

// Setup shadow maps for surface base objects and mesh based bodies ---------------
//
Expand Down
6 changes: 3 additions & 3 deletions OVP/D3D9Client/VVessel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -721,9 +721,9 @@ bool vVessel::Render(LPDIRECT3DDEVICE9 dev, bool internalpass)

// Initialize MeshShader constants
//
MeshShader::ps_const.Cam_X = *scn->GetCameraX();
MeshShader::ps_const.Cam_Y = *scn->GetCameraY();
MeshShader::ps_const.Cam_Z = *scn->GetCameraZ();
MeshShader::ps_const.Cam_X = to_FVECTOR3(*scn->GetCameraX());
MeshShader::ps_const.Cam_Y = to_FVECTOR3(*scn->GetCameraY());
MeshShader::ps_const.Cam_Z = to_FVECTOR3(*scn->GetCameraZ());



Expand Down
13 changes: 3 additions & 10 deletions OVP/D3D9Client/VectorHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,6 @@ template <typename T> inline _constexpr_ T lerp (T a, T b, T x)
// return fma(t, v1, fma(-t, v0, v0));
//}

template <typename T> inline _constexpr_ T saturate (T val)
{
return (val > T(1)) ? T(1)
: (val < T(0)) ? T(0)
: val;
}

template <typename T> inline _constexpr_ T clamp(T x, T a, T b)
{
return x > b ? b
Expand Down Expand Up @@ -308,17 +301,17 @@ inline D3DXVECTOR3 lerp(const D3DXVECTOR3 &v, const D3DXVECTOR3 &w, float x)
// D3DXVECTOR4 Helpers ==================================================================
//
//
inline D3DXVECTOR4 abs(D3DXVECTOR4 &a)
inline D3DXVECTOR4 abs(const D3DXVECTOR4 &a)
{
return D3DXVECTOR4(abs(a.x), abs(a.y), abs(a.z), abs(a.w));
}

inline D3DXVECTOR4 sign(D3DXVECTOR4 &a)
inline D3DXVECTOR4 sign(const D3DXVECTOR4 &a)
{
return D3DXVECTOR4(sign(a.x), sign(a.y), sign(a.z), sign(a.w));
}

inline D3DXVECTOR4 pow(float x, D3DXVECTOR4 &y)
inline D3DXVECTOR4 pow(float x, const D3DXVECTOR4 &y)
{
return D3DXVECTOR4(pow(x, y.x), pow(x, y.y), pow(x, y.z), pow(x, y.w));
}
Expand Down
Loading