From 136ac06b7ca293b34ba83de3759d4264b79ad045 Mon Sep 17 00:00:00 2001 From: Marvin D <41352111+IcePixelx@users.noreply.github.com> Date: Sun, 23 Oct 2022 01:20:49 +0200 Subject: [PATCH] ClipTransform, ScreenTransform (W2S) --- r5dev/engine/gl_rmain.cpp | 68 ++++++++++++++++++++++++ r5dev/engine/gl_rmain.h | 34 ++++++++++++ r5dev/engine/gl_rsurf.h | 7 +-- r5dev/materialsystem/cmaterialsystem.cpp | 14 +++++ r5dev/materialsystem/cmaterialsystem.h | 27 ++++++---- r5dev/vproj/gamesdk.vcxproj | 2 + r5dev/vproj/gamesdk.vcxproj.filters | 6 +++ 7 files changed, 143 insertions(+), 15 deletions(-) create mode 100644 r5dev/engine/gl_rmain.cpp create mode 100644 r5dev/engine/gl_rmain.h diff --git a/r5dev/engine/gl_rmain.cpp b/r5dev/engine/gl_rmain.cpp new file mode 100644 index 00000000..3f1852d0 --- /dev/null +++ b/r5dev/engine/gl_rmain.cpp @@ -0,0 +1,68 @@ +//=====================================================================================// +// +// Purpose: +// +// $NoKeywords: $ +//=====================================================================================// +#include "core/stdafx.h" + +#ifndef DEDICATED + +#include "engine/gl_rmain.h" +#include "materialsystem/cmaterialsystem.h" + +//----------------------------------------------------------------------------- +// Purpose: compute the scene coordinates of a point in 3D +// Input : &w2sMatrix - +// &point - +// *pClip - +// Output : is offscreen? +//----------------------------------------------------------------------------- +bool ClipTransform(const VMatrix& w2sMatrix, const Vector3D& point, Vector3D* pClip) +{ + pClip->x = w2sMatrix[0][0] * point.x + w2sMatrix[0][1] * point.y + w2sMatrix[0][2] * point.z + w2sMatrix[0][3]; + pClip->y = w2sMatrix[1][0] * point.x + w2sMatrix[1][1] * point.y + w2sMatrix[1][2] * point.z + w2sMatrix[1][3]; + pClip->z = 0.0f; + + float w = w2sMatrix[3][0] * point.x + w2sMatrix[3][1] * point.y + w2sMatrix[3][2] * point.z + w2sMatrix[3][3]; + + if (w < 0.001f) + { + // Clamp here. + pClip->x *= 100000; + pClip->y *= 100000; + return true; + } + + float invw = 1.0f / w; + pClip->x *= invw; + pClip->y *= invw; + + return false; +} + +//----------------------------------------------------------------------------- +// Purpose: translate point to screen position +// Input : &transformInfo - +// &w2sMatrix - +// &point - +// *pClip - +// Output : is offscreen? +//----------------------------------------------------------------------------- +bool ScreenTransform(const TransformInfo_t& transformInfo, const VMatrix& w2sMatrix, const Vector3D& point, Vector3D* pClip) +{ + bool bIsOffscreen = ClipTransform(w2sMatrix, point, pClip); + + // is offscreen? + if (!bIsOffscreen) + { + pClip->x = (transformInfo.width * 0.5f) + (pClip->x * transformInfo.width) * 0.5f + transformInfo.posX; + pClip->y = (transformInfo.height * 0.5f) - (pClip->y * transformInfo.height) * 0.5f + transformInfo.posY; + + return true; + } + + return false; +} + +#endif \ No newline at end of file diff --git a/r5dev/engine/gl_rmain.h b/r5dev/engine/gl_rmain.h new file mode 100644 index 00000000..35fcc896 --- /dev/null +++ b/r5dev/engine/gl_rmain.h @@ -0,0 +1,34 @@ +#pragma once +#include "mathlib/vector.h" +#include "mathlib/vmatrix.h" + +#ifndef DEDICATED + +// Remove after CViewSetup impl +// For main view calc width / 2 = posX same for posY with height. +struct TransformInfo_t +{ + int posX; + int posY; + int width; + int height; +}; + +bool ClipTransform(const VMatrix& w2sMatrix, const Vector3D& point, Vector3D* pClip); +bool ScreenTransform(const TransformInfo_t& transformInfo, const VMatrix& w2sMatrix, const Vector3D& point, Vector3D* pClip); + +/////////////////////////////////////////////////////////////////////////////// +class VGL_RMain : public IDetour +{ + virtual void GetAdr(void) const { } + virtual void GetFun(void) const { } + virtual void GetVar(void) const { } + virtual void GetCon(void) const { } + virtual void Attach(void) const { } + virtual void Detach(void) const { } +}; +/////////////////////////////////////////////////////////////////////////////// + +REGISTER(VGL_RMain); + +#endif \ No newline at end of file diff --git a/r5dev/engine/gl_rsurf.h b/r5dev/engine/gl_rsurf.h index 98d51ba9..e32e6eb6 100644 --- a/r5dev/engine/gl_rsurf.h +++ b/r5dev/engine/gl_rsurf.h @@ -1,5 +1,4 @@ -#ifndef GL_RSURF_H -#define GL_RSURF_H +#pragma once #include "public/ivrenderview.h" inline CMemory P_DrawWorldMeshes; @@ -44,6 +43,4 @@ class VGL_RSurf : public IDetour }; /////////////////////////////////////////////////////////////////////////////// -REGISTER(VGL_RSurf); - -#endif // GL_RSURF_H +REGISTER(VGL_RSurf); \ No newline at end of file diff --git a/r5dev/materialsystem/cmaterialsystem.cpp b/r5dev/materialsystem/cmaterialsystem.cpp index 8eb10085..b3c42f10 100644 --- a/r5dev/materialsystem/cmaterialsystem.cpp +++ b/r5dev/materialsystem/cmaterialsystem.cpp @@ -133,6 +133,20 @@ CMaterialGlue* CMaterialSystem::FindMaterialEx(CMaterialSystem* pMatSys, const c } return pMaterial; } + +//----------------------------------------------------------------------------- +// Purpose: get screen size +// Input : *pMatSys - +// Output : Vector2D screen size +//----------------------------------------------------------------------------- +Vector2D CMaterialSystem::GetScreenSize(CMaterialSystem* pMatSys) +{ + Vector2D vecScreenSize; + + CMaterialSystem_GetScreenSize(pMatSys, &vecScreenSize.x, &vecScreenSize.y); + + return vecScreenSize; +} #endif // !DEDICATED /////////////////////////////////////////////////////////////////////////////// diff --git a/r5dev/materialsystem/cmaterialsystem.h b/r5dev/materialsystem/cmaterialsystem.h index 701c1eba..bfbee2c8 100644 --- a/r5dev/materialsystem/cmaterialsystem.h +++ b/r5dev/materialsystem/cmaterialsystem.h @@ -9,28 +9,32 @@ class CMaterialSystem public: #ifndef DEDICATED static CMaterialGlue* FindMaterialEx(CMaterialSystem* pMatSys, const char* pMaterialName, uint8_t nMaterialType, int nUnk, bool bComplain); + static Vector2D GetScreenSize(CMaterialSystem* pMatSys = nullptr); #endif // !DEDICATED }; /* ==== MATERIALSYSTEM ================================================================================================================================================== */ inline CMemory p_CMaterialSystem__Init; -inline auto CMaterialSystem__Init = p_CMaterialSystem__Init.RCast(); +inline auto CMaterialSystem__Init = p_CMaterialSystem__Init.RCast(); inline void* g_pMaterialSystem = nullptr; inline void* g_pMaterialVFTable = nullptr; #ifndef DEDICATED inline CMemory p_CMaterialSystem__FindMaterialEx; -inline auto CMaterialSystem__FindMaterialEx = p_CMaterialSystem__FindMaterialEx.RCast(); +inline auto CMaterialSystem__FindMaterialEx = p_CMaterialSystem__FindMaterialEx.RCast(); + +inline CMemory p_CMaterialSystem_GetScreenSize; +inline auto CMaterialSystem_GetScreenSize = p_CMaterialSystem_GetScreenSize.RCast(); #if defined (GAMEDLL_S0) || defined (GAMEDLL_S1) inline CMemory p_DispatchDrawCall; -inline auto v_DispatchDrawCall = p_DispatchDrawCall.RCast(); +inline auto v_DispatchDrawCall = p_DispatchDrawCall.RCast(); #elif defined (GAMEDLL_S2) || defined (GAMEDLL_S3) inline CMemory p_DispatchDrawCall; -inline auto v_DispatchDrawCall = p_DispatchDrawCall.RCast(); +inline auto v_DispatchDrawCall = p_DispatchDrawCall.RCast(); #endif inline CMemory p_DrawStreamOverlay; -inline auto v_DrawStreamOverlay = p_DrawStreamOverlay.RCast(); +inline auto v_DrawStreamOverlay = p_DrawStreamOverlay.RCast(); inline CMemory s_pRenderContext; @@ -63,19 +67,22 @@ class VMaterialSystem : public IDetour virtual void GetFun(void) const { p_CMaterialSystem__Init = g_GameDll.FindPatternSIMD(reinterpret_cast("\x48\x89\x5C\x24\x00\x55\x56\x57\x41\x54\x41\x55\x41\x56\x41\x57\x48\x83\xEC\x70\x48\x83\x3D\x00\x00\x00\x00\x00"), "xxxx?xxxxxxxxxxxxxxxxxx?????"); - CMaterialSystem__Init = p_CMaterialSystem__Init.RCast(); /*48 89 5C 24 ?? 55 56 57 41 54 41 55 41 56 41 57 48 83 EC 70 48 83 3D ?? ?? ?? ?? ??*/ + CMaterialSystem__Init = p_CMaterialSystem__Init.RCast(); /*48 89 5C 24 ?? 55 56 57 41 54 41 55 41 56 41 57 48 83 EC 70 48 83 3D ?? ?? ?? ?? ??*/ #ifndef DEDICATED p_CMaterialSystem__FindMaterialEx = g_GameDll.FindPatternSIMD(reinterpret_cast("\x44\x89\x4C\x24\x00\x44\x88\x44\x24\x00\x48\x89\x4C\x24\x00"), "xxxx?xxxx?xxxx?"); - CMaterialSystem__FindMaterialEx = p_CMaterialSystem__FindMaterialEx.RCast(); /*44 89 4C 24 ?? 44 88 44 24 ?? 48 89 4C 24 ??*/ + CMaterialSystem__FindMaterialEx = p_CMaterialSystem__FindMaterialEx.RCast(); /*44 89 4C 24 ?? 44 88 44 24 ?? 48 89 4C 24 ??*/ + + p_CMaterialSystem_GetScreenSize = g_GameDll.FindPatternSIMD(reinterpret_cast("\x8B\x05\x00\x00\x00\x00\x89\x02\x8B\x05\x00\x00\x00\x00\x41\x89\x00\xC3\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\x8B\x05\x00\x00\x00\x00"), "xx????xxxx????xxxxxxxxxxxxxxxxxxxx????"); + CMaterialSystem_GetScreenSize = p_CMaterialSystem_GetScreenSize.RCast(); /*8B 05 ? ? ? ? 89 02 8B 05 ? ? ? ? 41 89 00 C3 CC CC CC CC CC CC CC CC CC CC CC CC CC CC 8B 05 ? ? ? ?*/ #if defined (GAMEDLL_S0) || defined (GAMEDLL_S1) p_DispatchDrawCall = g_GameDll.FindPatternSIMD(reinterpret_cast("\x44\x89\x4C\x24\x00\x44\x89\x44\x24\x00\x48\x89\x4C\x24\x00\x55\x53"), "xxxx?xxxx?xxxx?xx"); - v_DispatchDrawCall = p_DispatchDrawCall.RCast(); + v_DispatchDrawCall = p_DispatchDrawCall.RCast(); #elif defined (GAMEDLL_S2) || defined (GAMEDLL_S3) p_DispatchDrawCall = g_GameDll.FindPatternSIMD(reinterpret_cast("\x44\x89\x4C\x24\x00\x44\x89\x44\x24\x00\x48\x89\x4C\x24\x00\x55\x53\x56"), "xxxx?xxxx?xxxx?xxx"); - v_DispatchDrawCall = p_DispatchDrawCall.RCast(); + v_DispatchDrawCall = p_DispatchDrawCall.RCast(); #endif p_DrawStreamOverlay = g_GameDll.FindPatternSIMD(reinterpret_cast("\x41\x56\xB8\x00\x00\x00\x00\xE8\x00\x00\x00\x00\x48\x2B\xE0\xC6\x02\x00"), "xxx????x????xxxxxx"); - v_DrawStreamOverlay = p_DrawStreamOverlay.RCast(); // 41 56 B8 ?? ?? ?? ?? E8 ?? ?? ?? ?? 48 2B E0 C6 02 00 // + v_DrawStreamOverlay = p_DrawStreamOverlay.RCast(); // 41 56 B8 ?? ?? ?? ?? E8 ?? ?? ?? ?? 48 2B E0 C6 02 00 // #endif // !DEDICATED } virtual void GetVar(void) const diff --git a/r5dev/vproj/gamesdk.vcxproj b/r5dev/vproj/gamesdk.vcxproj index 4225375e..da7e4e25 100644 --- a/r5dev/vproj/gamesdk.vcxproj +++ b/r5dev/vproj/gamesdk.vcxproj @@ -36,6 +36,7 @@ + @@ -195,6 +196,7 @@ + diff --git a/r5dev/vproj/gamesdk.vcxproj.filters b/r5dev/vproj/gamesdk.vcxproj.filters index 6e25596f..91775da3 100644 --- a/r5dev/vproj/gamesdk.vcxproj.filters +++ b/r5dev/vproj/gamesdk.vcxproj.filters @@ -651,6 +651,9 @@ sdk\engine + + sdk\engine + @@ -1883,6 +1886,9 @@ sdk\engine + + sdk\engine +