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

2
external/SDL vendored

2
external/cglm vendored

2
external/slang vendored

2
external/stb vendored

2
external/volk vendored

2
external/xtool vendored

View File

@@ -2,13 +2,14 @@
{
"classname": "prop_physics",
"model": "game/core/maps/test/test0.fmdl",
"physics": "static"
"physics": "static",
"origin": [ 0, -10, 0 ]
},
{
"classname": "prop_physics",
"model": "game/core/models/cube.fmdl",
"model": "game/core/models/sphere.fmdl",
"physics": "dynamic",
"origin": [ 0, 3, 0 ]
"origin": [ 0, -5, 0 ]
}
]

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,5 @@
{
"Mesh": "game/core/meshes/sphere.fmesh_c",
"Material": "game/core/materials/cube.fmat",
"Physics": "game/core/physics/sphere.fpx"
}

View File

@@ -0,0 +1,4 @@
{
"Type": "Sphere",
"Radius": "1.0"
}

View File

@@ -19,6 +19,7 @@ COMMON {
float4 m_vWorldPosition: POSITION;
float4 m_vTexCoord: TEXCOORD0;
float4 m_vNormal: NORMAL0;
uint m_uInstance: SV_InstanceID;
}
}
VS
@@ -31,8 +32,10 @@ VS
float3 m_vNormal: NORMAL0;
}
PS_INPUT vsMain( VS_INPUT input, uint uInstance: SV_InstanceID )
PS_INPUT vsMain( VS_INPUT input, uint uInstance: SV_InstanceID, uint uBaseInstance: SV_StartInstanceLocation )
{
uint uInstance = uBaseInstance + uInstance;
PS_INPUT output = {};
output.m_vScreenPosition = float4(input.m_vPosition, 1);
output.m_vScreenPosition = mul(
@@ -53,6 +56,7 @@ VS
g_modelData[uInstance].m_matRotation
);
output.m_vTexCoord = float4(input.m_vTexCoord, 0, 0);
output.m_uInstance = uInstance;
return output;
}
}
@@ -69,10 +73,10 @@ PS
float4 m_vWorldPosition: SV_Target2;
}
PS_OUTPUT psMain( PS_INPUT input, uint uInstance: SV_InstanceID )
PS_OUTPUT psMain( PS_INPUT input )
{
PS_OUTPUT output = {};
PerModelData data = g_modelData[uInstance];
PerModelData data = g_modelData[input.m_uInstance];
output.m_vAlbedo = g_textures[data.m_uAlbedo].Sample(g_textureSampler, input.m_vTexCoord.xy);
output.m_vWorldPosition = input.m_vWorldPosition;
output.m_vNormal = input.m_vNormal;

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

View File

@@ -17,10 +17,10 @@ DECLARE_BUILD_STAGE(rapier)
CUtlString szTarget = target.GetTriplet();
if (CommandLine()->CheckParam("-norust"))
return 0;
rapier_lib = CUtlString("target/%s/debug/librapier_rtt.a",szTarget.GetString());
V_printf("%s\n",rapier_lib.GetString());
rapier_lib = CUtlString("target/%s/release/librapier_rtt.a",szTarget.GetString());
CUtlVector<CUtlString> cargo_args = {
"build",
"--release",
"--target",
szTarget
};

View File

@@ -42,6 +42,11 @@ public:
return CRapierPhysicsBody_GetRotation(m_pBody);
}
virtual void SetType( EPhysicsBodyType eType ) override
{
CRapierPhysicsBody_SetType(m_pBody, eType);
}
RapierPhysicsBody_t *m_pBody = NULL;

View File

@@ -16,7 +16,7 @@ macro_rules! V_malloc {
use std::{default, ops::Index, ptr::{self, null, null_mut}, slice::from_raw_parts, sync::Arc};
use parry3d::{glamx::vec3, shape::{Shape, ShapeType, SharedShape}};
use rapier3d::{geometry::Ball, na::{UnitQuaternion, Vector4}};
use rapier3d::{geometry::Ball, na::{UnitQuaternion, Vector4, coordinates::XYZ}};
use rapier3d::prelude::*;
use libc::{malloc, free};
@@ -56,6 +56,7 @@ pub struct RapierShape_t
}
#[repr(C)]
#[derive(Debug)]
pub enum EPhysicsBodyType
{
k_EPhysics_Static,
@@ -119,7 +120,8 @@ pub unsafe extern "C" fn CRapierPhysicsBody_SetRotation( this: *mut RapierPhysic
{
let world: &mut RapierWorld_t = &mut *(*this).m_pWorld;
world.m_rigidBodies[(*this).m_hRigidBodyHandle]
.set_translation(vec3(fX, fY, fZ), true);
.set_rotation(parry3d::glamx::Quat::from_xyzw(fX, fY, fZ, fW), true);
}
#[repr(C)]
@@ -256,7 +258,7 @@ pub unsafe extern "C" fn CRapierPhysics_CreateCube( this: *mut RapierPhysics_t,
pub unsafe extern "C" fn CRapierPhysics_CreateTriangleMesh( this: *mut RapierPhysics_t, triangle: TriangleMeshShape_t ) -> *mut RapierShape_t
{
let pShapeMemory: *mut RapierShape_t = V_malloc!(RapierShape_t, 1);
let positions = Vec::from_raw_parts(triangle.m_pfPositions as *mut Vec3, triangle.m_nPositionCount as usize, triangle.m_nPositionCount as usize);
let positions = Vec::from_raw_parts(triangle.m_pfPositions as *mut Vec3, (triangle.m_nPositionCount/3) as usize, (triangle.m_nPositionCount/3) as usize);
let mut indices: Vec<[u32; 3]>;
if ( triangle.m_nIndiciesCount == 0 )
@@ -265,8 +267,8 @@ pub unsafe extern "C" fn CRapierPhysics_CreateTriangleMesh( this: *mut RapierPhy
{
return null_mut();
}
indices = vec![[0,0,0]; (triangle.m_nPositionCount/3) as usize];
for i in 0..indices.len()/3
indices = vec![[0,0,0]; (triangle.m_nPositionCount/9) as usize];
for i in 0..indices.len()
{
let u = i as u32;
indices[i][0] = u*3;
@@ -281,7 +283,7 @@ pub unsafe extern "C" fn CRapierPhysics_CreateTriangleMesh( this: *mut RapierPhy
{
return null_mut();
}
indices = Vec::from_raw_parts(triangle.m_puIndicies as *mut [u32; 3], triangle.m_nIndiciesCount as usize, triangle.m_nIndiciesCount as usize);
indices = Vec::from_raw_parts(triangle.m_puIndicies as *mut [u32; 3], (triangle.m_nIndiciesCount / 3) as usize, (triangle.m_nIndiciesCount / 3) as usize);
}
let mesh = TriMesh::new(positions, indices);
@@ -291,7 +293,9 @@ pub unsafe extern "C" fn CRapierPhysics_CreateTriangleMesh( this: *mut RapierPhy
{
std::ptr::write(&mut (*pShapeMemory).m_sharedShape, SharedShape::new(m));
}
default => {}
default => {
return null_mut();
}
}
pShapeMemory
}