improve rendering and physics

This commit is contained in:
2026-03-18 18:41:28 +02:00
parent 0a506f7185
commit b56d85f95d
33 changed files with 141 additions and 82 deletions

View File

@@ -197,11 +197,14 @@ HFunnyMesh CAssetManager::LoadMesh( const char *szName )
}
pMesh->m_nPositionCount = filesystem->Size(hMesh)/4;
*pMesh = {};
pMesh->m_nPositionCount = filesystem->Size(hMesh)/32*3;
pMesh->m_pfPositions = new float[pMesh->m_nPositionCount];
filesystem->Read( hMesh, pMesh->m_pfPositions, filesystem->Size(hMesh));
pMesh->m_nIndiciesCount = 0;
pMesh->m_puIndicies = 0;
for ( int i = 0; i < filesystem->Size(hMesh) / 32; i++)
{
filesystem->Seek( hMesh, SEEKMODE_RELATIVE_START, 32*i);
filesystem->Read( hMesh, &pMesh->m_pfPositions[i*3], 12);
}
return hAsset;
@@ -251,16 +254,13 @@ HFunnyPhysics CAssetManager::LoadPhysics( const char *szName )
return 0;
}
CUtlString szType = pTypeValue->GetStringValue();
V_printf("%s\n", szType.GetString());
if (szType == "Sphere")
{
pPhysics->m_hShape = g_pPhysics->CreateBall({1.0});
}
if (szType == "TriangleMesh")
{
V_printf("hi\n");
IJSONValue *pMeshValue = pMainObject->GetValue("Mesh");
V_printf("hi\n");
if (!pMeshValue)
{
@@ -269,11 +269,9 @@ HFunnyPhysics CAssetManager::LoadPhysics( const char *szName )
return 0;
}
CUtlString szMesh = pMeshValue->GetStringValue();
V_printf("%s\n", szMesh.GetString());
HFunnyMesh hMesh = LoadMesh(szMesh);
FunnyMesh_t *pMesh = GetMeshByIndex(hMesh);
pPhysics->m_hShape = g_pPhysics->CreateTriangleMesh({pMesh->m_pfPositions, pMesh->m_nPositionCount});
V_printf("%p\n", pPhysics->m_hShape);
pPhysics->m_hShape = g_pPhysics->CreateTriangleMesh({pMesh->m_pfPositions, pMesh->m_nPositionCount, pMesh->m_puIndicies, pMesh->m_nIndiciesCount});
pPhysics->m_hMesh = hMesh;
}

View File

@@ -21,9 +21,6 @@ void CBaseEntity::Precache()
void CBaseEntity::Spawn()
{
Precache();
SetAbsOrigin({0, 0, 0});
SetAbsQAngles(0, 0, 0);
SetScale(1);
}
void CBaseEntity::SetAbsAngles( Quat vQuat )

View File

@@ -78,8 +78,8 @@ public:
virtual void SetNextThink( float fThink );
fnThink m_pfnThink = NULL;
const char *m_szClassName;
uint64_t m_ullOwner;
const char *m_szClassName = NULL;
uint64_t m_ullOwner = 0;
private:
Vector m_vPosition = {};
Quat m_vRotation = {};

View File

@@ -16,9 +16,9 @@ public:
virtual void SetModel( const char *szName );
virtual void OnModelChanged( const char *szName );
virtual void Think( float fDelta );
char m_szModel[256] = {};
private:
char m_szCurrentModel[256] = {};
char m_szModel[256] = {};
};
#endif

View File

@@ -7,13 +7,18 @@
void CMOBAPlayer::Spawn()
{
CBaseEntity::Spawn();
SetModel("game/core/models/cube.fmdl");
SetPhysics(k_EPhysics_Static);
SetModel("game/core/models/sphere.fmdl");
SetScale(1);
SetAbsOrigin({0,-14.5, 0});
SetThink(Think);
};
void CMOBAPlayer::Think( float fDelta )
{
BaseClass::Think(fDelta);
Vector vPosition = GetAbsOrigin();
// player might lie to us
m_vMovementVector.x = glm_clamp(m_vMovementVector.x, -1, 1);
@@ -22,7 +27,6 @@ void CMOBAPlayer::Think( float fDelta )
vPosition.x += m_vMovementVector.x*fDelta*5;
vPosition.z += m_vMovementVector.z*fDelta*5;
SetAbsOrigin(vPosition);
V_printf("player: %f %f %f\n", vPosition.x, vPosition.y, vPosition.z);
};
LINK_ENTITY_TO_CLASS(player, CMOBAPlayer)

View File

@@ -3,10 +3,10 @@
#include "physicsprop.h"
class CMOBAPlayer: public CBaseModelEntity
class CMOBAPlayer: public CPhysicsProp
{
public:
DECLARE_CLASS(CMOBAPlayer, CBaseModelEntity);
DECLARE_CLASS(CMOBAPlayer, CPhysicsProp);
DECLARE_DATADESC();
DECLARE_SERVERCLASS()

View File

@@ -1,6 +1,11 @@
#include "physicsprop.h"
#include "game.h"
CPhysicsProp::CPhysicsProp()
{
SetAbsOrigin({0,0,0});
SetAbsQAngles(0,0,0);
}
void CPhysicsProp::Precache()
{
@@ -16,12 +21,17 @@ void CPhysicsProp::Spawn()
void CPhysicsProp::Think( float fDelta )
{
BaseClass::Think(fDelta);
if (m_pBody)
{
if (m_ePhysicsType != m_eCurrentPhysicsType)
{
m_ePhysicsType = m_eCurrentPhysicsType;
m_pBody->SetType(m_eCurrentPhysicsType);
}
BaseClass::SetAbsOrigin(m_pBody->GetPosition());
BaseClass::SetAbsAngles(m_pBody->GetRotation());
}
V_printf("%f %f %f\n", GetAbsOrigin().x, GetAbsOrigin().y, GetAbsOrigin().z);
}
void CPhysicsProp::SetAbsAngles( Quat vQuat )
@@ -29,7 +39,7 @@ void CPhysicsProp::SetAbsAngles( Quat vQuat )
BaseClass::SetAbsAngles( vQuat );
if (m_pBody)
m_pBody->SetRotation(m_pBody->GetRotation());
m_pBody->SetRotation(GetAbsAngles());
}
void CPhysicsProp::SetAbsQAngles( float fPitch, float fYaw, float fRoll )
@@ -37,14 +47,14 @@ void CPhysicsProp::SetAbsQAngles( float fPitch, float fYaw, float fRoll )
BaseClass::SetAbsQAngles( fPitch, fYaw, fRoll);
if (m_pBody)
m_pBody->SetRotation(m_pBody->GetRotation());
m_pBody->SetRotation(GetAbsAngles());
}
void CPhysicsProp::SetAbsOrigin( Vector origin )
{
BaseClass::SetAbsOrigin( origin );
if (m_pBody)
m_pBody->SetPosition(m_pBody->GetPosition());
m_pBody->SetPosition(GetAbsOrigin());
}
void CPhysicsProp::OnModelChanged( const char *szName )
@@ -57,9 +67,16 @@ void CPhysicsProp::OnModelChanged( const char *szName )
m_pModel = g_pAssetManager->GetModelByIndex(m_hModel);
m_pPhysics = g_pAssetManager->GetPhysicsByIndex(m_pModel->m_hPhysics);
m_hCollider = g_pPhysics->CreateCollider(m_pPhysics->m_hShape);
m_pBody = g_pPhysicsWorld->CreateRigidBody(m_hCollider, k_EPhysics_Dynamic);
m_pBody->SetPosition({0,0,0});
m_pBody->SetRotation({0,0,0});
m_pBody = g_pPhysicsWorld->CreateRigidBody(m_hCollider, m_eCurrentPhysicsType);
m_pBody->SetPosition(GetAbsOrigin());
m_pBody->SetRotation(GetAbsAngles());
m_ePhysicsType = m_eCurrentPhysicsType;
if (!V_strcmp(szName, "game/core/maps/test/test0.fmdl"))
{
m_pBody->SetType(k_EPhysics_Static);
SetScale(1);
}
}
void CPhysicsProp::EnableMovement()
@@ -71,6 +88,13 @@ void CPhysicsProp::DisableMovement()
{
}
void CPhysicsProp::SetPhysics( EPhysicsBodyType eType )
{
m_eCurrentPhysicsType = eType;
}
BEGIN_DATADESC(CPhysicsProp)
DEFINE_KEYFIELD(m_ePhysicsType, FIELD_INTEGER, "physics_type")
END_DATADESC()

View File

@@ -8,8 +8,10 @@ class CPhysicsProp: public CBaseModelEntity
friend CBaseEntity;
friend CBaseModelEntity;
public:
DECLARE_CLASS(CPhysicsProp, CBaseModelEntity)
DECLARE_DATADESC()
CPhysicsProp();
virtual void Spawn() override;
virtual void Precache() override;
@@ -25,8 +27,8 @@ public:
void Think( float fDelta );
private:
EPhysicsBodyType m_ePhysicsType;
EPhysicsBodyType m_eCurrentPhysicsType;
EPhysicsBodyType m_ePhysicsType = k_EPhysics_Dynamic;
EPhysicsBodyType m_eCurrentPhysicsType = k_EPhysics_Dynamic;
HFunnyModel m_hModel = 0;
FunnyModel_t *m_pModel = NULL;
FunnyPhysics_t *m_pPhysics = NULL;

View File

@@ -54,7 +54,6 @@ void CWorldSystem::LoadEntity( IJSONValue *pObject )
if (pValue->GetType() != JSON_PARAMETER_STRING)
continue;
V_strncpy(pcData, pValue->GetStringValue(), pDataMap->m_uFieldSize);
V_printf("loading %s %u\n", pValue->GetStringValue(), pDataMap->m_uFieldSize);
continue;
case FIELD_FLOAT3:
if (pValue->GetType() != JSON_PARAMETER_ARRAY)
@@ -83,7 +82,6 @@ void CWorldSystem::LoadEntity( IJSONValue *pObject )
bool CWorldSystem::LoadMap( const char *szName )
{
V_printf("Loading %s\n", szName);
// unload the map
IFileHandle *pHandle = filesystem->Open(szName, FILEMODE_READ);