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

@@ -317,7 +317,6 @@ HFunnyPhysics CAssetManager::LoadPhysics( const char *szName )
IJSONValue *pRoot = JSONManager()->ReadString(szProperties);
if (!pRoot)
return 0;
V_printf("PHYSICS %s\n", szName);
IJSONObject *pMainObject;
switch (pRoot->GetType())
@@ -340,10 +339,9 @@ HFunnyPhysics CAssetManager::LoadPhysics( const char *szName )
return 0;
}
CUtlString szType = pTypeValue->GetStringValue();
V_printf("szType\n");
if (szType == "Sphere")
{
pPhysics->m_hShape = g_pPhysics->CreateBall({1.0});
pPhysics->m_hShape = g_pPhysics->CreateBall({0.1});
}
return hPhysics;

View File

@@ -54,6 +54,8 @@ void C_BaseModelEntity::UpdateModel()
FunnyMesh_t *pMesh = g_pAssetManager->GetMeshByIndex(m_pModel->m_hMesh);
m_pInstance = g_pWorldRenderer->CreateInstance(pMesh->m_pMesh);
m_pInstance->SetPosition({0,0,0});
m_pInstance->SetRotation({0,0,0,1});
}

View File

@@ -8,6 +8,7 @@
#include "game.h"
#include "netprotocol.h"
#include "userinput.h"
#include "math.h"
#ifdef STEAM
#include "steam/isteamgameserver.h"
#include "steam/steam_gameserver.h"
@@ -157,9 +158,10 @@ void CFunnyGameBridge::Frame( float fDelta )
m_fNetUpdateTimer += fDelta;
if (m_fNetUpdateTimer >= fTickRate)
m_fNetUpdateTimer = fTickRate;
while (m_fNetUpdateTimer >= fTickRate)
if (m_fNetUpdateTimer >= fTickRate)
{
m_fNetUpdateTimer-=fTickRate;
m_fNetUpdateTimer = fmod(m_fNetUpdateTimer, fTickRate);
if (pCurrentServer)
EntitySystem()->NetSendThink(pCurrentServer);
}

View File

@@ -21,7 +21,6 @@ void C_MOBAPlayer::Think( float fDelta )
C_MOBAPlayer *pEntity = (C_MOBAPlayer*)UTIL_GetLocalPlayer();
m_vMovementVector.z = m_bIsForward - m_bIsBack;
m_vMovementVector.x = m_bIsLeft - m_bIsRight;

View File

@@ -59,8 +59,8 @@ public:
virtual void Frame();
PerMeshData_t m_data = {};
Quat m_vRotation = {};
Vector m_vPosition = {};
Quat m_vRotation = { 0, 0, 0, 1 };
Vector m_vPosition = { 0, 0, 0};
Vector m_vScale = { 1, 1, 1 };
};
@@ -105,6 +105,12 @@ void CFunnyMeshInstance::Frame()
glm_mat4_mul(m_data.m_matTranslation, m, m_data.m_matTranslation);
m_data.m_uAlbedo = 1;
/*
V_printf("AAAAA %f %f %f %f\n", m_data.m_matTranslation[0][0], m_data.m_matTranslation[0][1], m_data.m_matTranslation[0][2], m_data.m_matTranslation[0][3]);
V_printf("AAAAA %f %f %f %f\n", m_data.m_matTranslation[1][0], m_data.m_matTranslation[1][1], m_data.m_matTranslation[1][2], m_data.m_matTranslation[1][3]);
V_printf("AAAAA %f %f %f %f\n", m_data.m_matTranslation[2][0], m_data.m_matTranslation[2][1], m_data.m_matTranslation[2][2], m_data.m_matTranslation[2][3]);
V_printf("AAAAA %f %f %f %f\n", m_data.m_matTranslation[3][0], m_data.m_matTranslation[3][1], m_data.m_matTranslation[3][2], m_data.m_matTranslation[3][3]);
*/
}
@@ -222,7 +228,7 @@ void CFunnyWorldRenderer::Frame( float fDelta )
glm_mat4_identity(matCamera);
glm_mat4_identity(matCamera2);
glm_translate(matCamera2, m_vPos);
glm_perspective(glm_rad(60), uWidth/(float)uHeight, 0.01, 10000, matCamera);
glm_perspective(glm_rad(75), uWidth/(float)uHeight, 0.01, 10000, matCamera);
glm_mul(matCamera, matCamera2, matCamera);
m_pViewBufferData = (ViewBuffer_t*)m_pViewBuffer->Map();
m_pViewBuffer->Lock();
@@ -278,38 +284,44 @@ void CFunnyWorldRenderer::Frame( float fDelta )
m_pRasterCommandList->SetScissors(0, 0, uWidth, uHeight);
m_pRasterCommandList->SetClearColor(0, 0, 0, 0, 0);
m_pRasterCommandList->SetClearDepth(1);
uint32_t uTotalMeshes = 0;
uint32_t u = 0;
for ( auto mesh: m_pMeshes)
{
uTotalMeshes+=mesh->m_instances.GetSize();
}
if (!uTotalMeshes)
return;
IBuffer *pDataBuffer = g_pRenderContext->CreateStorageBuffer(uTotalMeshes*sizeof(PerMeshData_t));
pDataBuffer->Lock();
void *pData = pDataBuffer->Map();
for ( auto mesh: m_pMeshes)
{
if (mesh->m_instances.GetSize()==0)
continue;
CUtlVector<PerMeshData_t> data = {};
data.Reserve(mesh->m_instances.GetSize());
for ( auto instance: mesh->m_instances )
{
instance->Frame();
data.AppendTail(instance->m_data);
}
IBuffer *pDataBuffer = g_pRenderContext->CreateStorageBuffer(data.GetSize()*sizeof(PerMeshData_t));
pDataBuffer->Lock();
void *pData = pDataBuffer->Map();
for ( uint32_t i = 0; i < mesh->m_instances.GetSize(); i++ )
{
V_memcpy(&((PerMeshData_t*)pData)[i], &mesh->m_instances[i]->m_data, sizeof(PerMeshData_t));
mesh->m_instances[i]->Frame();
V_memcpy(&((PerMeshData_t*)pData)[i+u], &mesh->m_instances[i]->m_data, sizeof(PerMeshData_t));
}
pDataBuffer->Unmap();
pDataBuffer->Unlock();
m_pRasterMaterial->VSSetConstantsBuffer(0, m_pViewBuffer);
m_pRasterMaterial->VSSetConstantsBuffer(1, pDataBuffer);
m_pRasterMaterial->PSSetTextureArray(1, m_pTextures);
g_pRenderContext->DestroyBuffer(pDataBuffer);
u+=mesh->m_instances.GetSize();
}
pDataBuffer->Unmap();
pDataBuffer->Unlock();
g_pRenderContext->DestroyBuffer(pDataBuffer);
m_pRasterMaterial->VSSetConstantsBuffer(0, m_pViewBuffer);
m_pRasterMaterial->VSSetConstantsBuffer(1, pDataBuffer);
m_pRasterMaterial->PSSetTextureArray(1, m_pTextures);
m_pRasterCommandList->SetMaterial(m_pRasterMaterial);
u = 0;
for ( auto mesh: m_pMeshes)
{
if (mesh->m_instances.GetSize()==0)
continue;
m_pRasterCommandList->SetVertexBuffer(0, mesh->m_pVertexBuffer);
m_pRasterCommandList->DrawPrimitives(mesh->m_pVertexBuffer->GetSize()/32, 0, mesh->m_instances.GetSize(), 0);
m_pRasterCommandList->DrawPrimitives(mesh->m_pVertexBuffer->GetSize()/32, 0, mesh->m_instances.GetSize(), u);
u += mesh->m_instances.GetSize();
}
m_pRasterCommandList->ResolveImage(m_pOutputImage, m_pResolvedOutputImage);

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);