added material rendering
This commit is contained in:
2
external/funnystdlib
vendored
2
external/funnystdlib
vendored
Submodule external/funnystdlib updated: 8f8343e250...4d3914380b
@@ -1,4 +1,5 @@
|
||||
{
|
||||
"shader": "funny_basic_pbr",
|
||||
"AlbedoTexture": "game/core/textures/bricks.png"
|
||||
"AlbedoTexture": "game/core/textures/cat.png",
|
||||
"AlbedoMultiplier": [1,1,1,1]
|
||||
}
|
||||
|
||||
@@ -10,13 +10,21 @@ COMMON {
|
||||
float4x4 g_matViewProjection;
|
||||
float4 g_vViewPosition;
|
||||
};
|
||||
struct PerModelData
|
||||
|
||||
struct MaterialData_t
|
||||
{
|
||||
float4 m_vAlbedoColor;
|
||||
uint32_t m_uAlbedo;
|
||||
};
|
||||
|
||||
struct PerModelData_t
|
||||
{
|
||||
float4x4 m_matTranslation;
|
||||
float4x4 m_matRotation;
|
||||
uint32_t m_uAlbedo;
|
||||
MaterialData_t m_material;
|
||||
}
|
||||
StructuredBuffer<PerModelData> g_modelData;
|
||||
|
||||
StructuredBuffer<PerModelData_t> g_modelData;
|
||||
struct PS_INPUT
|
||||
{
|
||||
float4 m_vScreenPosition: SV_POSITION;
|
||||
@@ -80,8 +88,18 @@ PS
|
||||
PS_OUTPUT psMain( PS_INPUT input )
|
||||
{
|
||||
PS_OUTPUT output = {};
|
||||
PerModelData data = g_modelData[input.m_uInstance];
|
||||
output.m_vAlbedo = g_textures[data.m_uAlbedo].Sample(g_textureSampler, input.m_vTexCoord.xy);
|
||||
PerModelData_t data = g_modelData[input.m_uInstance];
|
||||
if ( data.m_material.m_uAlbedo != -1 )
|
||||
{
|
||||
output.m_vAlbedo = g_textures[data.m_material.m_uAlbedo]
|
||||
.Sample(g_textureSampler, input.m_vTexCoord.xy)
|
||||
* data.m_material.m_vAlbedoColor;
|
||||
}
|
||||
else
|
||||
{
|
||||
output.m_vAlbedo = data.m_material.m_vAlbedoColor;
|
||||
|
||||
}
|
||||
output.m_vWorldPosition = input.m_vWorldPosition;
|
||||
output.m_vNormal = input.m_vNormal;
|
||||
return output;
|
||||
|
||||
BIN
funnyassets/textures/cat.png
Normal file
BIN
funnyassets/textures/cat.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 142 KiB |
@@ -96,9 +96,16 @@ public:
|
||||
IShader **GetShaderByIndex( uint32_t hShader );
|
||||
void UnrefShader( uint32_t hShader );
|
||||
|
||||
virtual HFunnyTexture LoadTexture( const char *szName ) override;
|
||||
virtual FunnyTexture_t *GetTextureByIndex( HFunnyTexture hTexture ) override;
|
||||
virtual void UnrefTexture( HFunnyTexture hTexture ) override;
|
||||
|
||||
void LoadMaterialData( CBaseMaterial *pMaterial, IJSONObject *pObj );
|
||||
|
||||
CAssetArc<FunnyModel_t, MAX_MODEL_COUNT> m_models = {};
|
||||
CAssetArc<FunnyMesh_t, MAX_MESH_COUNT> m_meshes = {};
|
||||
CAssetArc<FunnyMaterial_t, MAX_MATERIAL_COUNT> m_materials = {};
|
||||
CAssetArc<FunnyTexture_t, MAX_TEXTURE_COUNT> m_textures = {};
|
||||
CAssetArc<IShader*, MAX_SHADER_COUNT> m_shaders = {};
|
||||
CAssetArc<FunnyPhysics_t, MAX_PHYSICS_COUNT> m_physics = {};
|
||||
|
||||
@@ -162,6 +169,7 @@ uint32_t CAssetManager::LoadModel( const char *szName )
|
||||
IJSONValue *pMesh = pMainObject->GetValue("Mesh");
|
||||
IJSONValue *pMaterial = pMainObject->GetValue("Material");
|
||||
IJSONValue *pPhysics = pMainObject->GetValue("Physics");
|
||||
V_printf("%s\n", pMaterial->GetStringValue());
|
||||
if (pMesh)
|
||||
pModel->m_hMesh = LoadMesh(pMesh->GetStringValue());
|
||||
if (pMaterial)
|
||||
@@ -184,6 +192,48 @@ void CAssetManager::UnrefModel( uint32_t uIndex )
|
||||
|
||||
}
|
||||
|
||||
void CAssetManager::LoadMaterialData( CBaseMaterial *pMaterial, IJSONObject *pObj )
|
||||
{
|
||||
for ( int i = 0; i < pMaterial->GetDataMap()->m_iNumFields; i++ )
|
||||
{
|
||||
typedescription_t desc = pMaterial->GetDataMap()->m_pData[i];
|
||||
IJSONValue *pValue = pObj->GetValue(desc.m_szEditorName);
|
||||
switch ( desc.m_eFieldType )
|
||||
{
|
||||
case FIELD_SHADER_TEXTURE:
|
||||
{
|
||||
FMat::XMTexture *texture = (FMat::XMTexture*)((char*)pMaterial+desc.m_iFieldOffset);
|
||||
if ( pValue->GetType()
|
||||
== JSON_PARAMETER_STRING )
|
||||
*texture = GetTextureByIndex(LoadTexture(pValue->GetStringValue()))->m_hTexture;
|
||||
break;
|
||||
}
|
||||
case FIELD_SHADER_COLOR_FLOAT4:
|
||||
{
|
||||
FMat::XMFLOAT4 *data = (FMat::XMFLOAT4*)((char*)pMaterial+desc.m_iFieldOffset);
|
||||
if ( pValue->GetArray()->GetParameter(0)->GetType()
|
||||
== JSON_PARAMETER_NUMBER )
|
||||
data->x = pValue->GetArray()->GetParameter(0)->GetNumberValue();
|
||||
if ( pValue->GetArray()->GetParameter(1)->GetType()
|
||||
== JSON_PARAMETER_NUMBER )
|
||||
data->y = pValue->GetArray()->GetParameter(1)->GetNumberValue();
|
||||
if ( pValue->GetArray()->GetParameter(2)->GetType()
|
||||
== JSON_PARAMETER_NUMBER )
|
||||
data->z = pValue->GetArray()->GetParameter(2)->GetNumberValue();
|
||||
if ( pValue->GetArray()->GetParameter(3)->GetType()
|
||||
== JSON_PARAMETER_NUMBER )
|
||||
data->w = pValue->GetArray()->GetParameter(3)->GetNumberValue();
|
||||
V_printf("%s %f %f %f %f\n", desc.m_szEditorName, data->x, data->y, data->z, data->w);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
uint32_t CAssetManager::LoadMaterial( const char *szName )
|
||||
{
|
||||
@@ -216,6 +266,7 @@ uint32_t CAssetManager::LoadMaterial( const char *szName )
|
||||
IJSONValue *pShaderValue = pMainObject->GetValue("shader");
|
||||
CUtlString szShader = pShaderValue->GetStringValue();
|
||||
CBaseMaterial *pRenderMaterial = CreateMaterial(szShader);
|
||||
LoadMaterialData(pRenderMaterial, pMainObject);
|
||||
|
||||
uint32_t uShaderId = LoadShader(pRenderMaterial->GetShaderPath());
|
||||
if (!uShaderId)
|
||||
@@ -301,6 +352,36 @@ void CAssetManager::UnrefShader( uint32_t uIndex )
|
||||
m_shaders.UnrefObject(uIndex);
|
||||
}
|
||||
|
||||
HFunnyTexture CAssetManager::LoadTexture( const char *szName )
|
||||
{
|
||||
bool bHasBeenCreated = false;
|
||||
uint32_t hTexture = m_textures.GetOrCreateObject(szName, &bHasBeenCreated);
|
||||
if (!bHasBeenCreated)
|
||||
return hTexture;
|
||||
FunnyTexture_t *pTexture = m_textures.GetObjectPtr(hTexture);
|
||||
|
||||
|
||||
uint32_t hTex = g_pWorldRenderer->GetTextures()->LoadTexture(szName);
|
||||
if ( hTex == 0 )
|
||||
{
|
||||
V_printf("Failed to load %s\n", szName);
|
||||
m_textures.UnrefObject(hTexture);
|
||||
return 0;
|
||||
}
|
||||
pTexture->m_hTexture = hTex;
|
||||
return hTexture;
|
||||
}
|
||||
|
||||
FunnyTexture_t *CAssetManager::GetTextureByIndex( HFunnyTexture hTexture )
|
||||
{
|
||||
return m_textures.GetObjectPtr(hTexture);
|
||||
}
|
||||
|
||||
void CAssetManager::UnrefTexture( uint32_t hTexture )
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
HFunnyPhysics CAssetManager::LoadPhysics( const char *szName )
|
||||
{
|
||||
@@ -354,6 +435,7 @@ HFunnyPhysics CAssetManager::LoadPhysics( const char *szName )
|
||||
return hPhysics;
|
||||
}
|
||||
|
||||
|
||||
FunnyPhysics_t *CAssetManager::GetPhysicsByIndex( HFunnyPhysics hPhysics )
|
||||
{
|
||||
return m_physics.GetObjectPtr(hPhysics);
|
||||
|
||||
@@ -12,6 +12,7 @@ typedef uint32_t HFunnyMaterial;
|
||||
typedef uint32_t HFunnyMesh;
|
||||
typedef uint32_t HFunnyPhysics;
|
||||
typedef uint32_t HFunnyModel;
|
||||
typedef uint32_t HFunnyTexture;
|
||||
|
||||
struct FunnyMaterial_t
|
||||
{
|
||||
@@ -20,6 +21,11 @@ struct FunnyMaterial_t
|
||||
CBaseMaterial *m_pLayout;
|
||||
};
|
||||
|
||||
struct FunnyTexture_t
|
||||
{
|
||||
uint32_t m_hTexture;
|
||||
};
|
||||
|
||||
struct FunnyMesh_t
|
||||
{
|
||||
IMesh *m_pMesh;
|
||||
@@ -56,6 +62,10 @@ public:
|
||||
virtual HFunnyPhysics LoadPhysics( const char *szName ) = 0;
|
||||
virtual FunnyPhysics_t *GetPhysicsByIndex( HFunnyPhysics hPhysics ) = 0;
|
||||
virtual void UnrefPhysics( HFunnyPhysics hPhysics ) = 0;
|
||||
|
||||
virtual uint32_t LoadTexture( const char *szName ) = 0;
|
||||
virtual FunnyTexture_t *GetTextureByIndex( uint32_t hTexture ) = 0;
|
||||
virtual void UnrefTexture( uint32_t hTexture ) = 0;
|
||||
};
|
||||
|
||||
extern IAssetManager *g_pAssetManager;
|
||||
|
||||
@@ -45,17 +45,18 @@ void C_BaseModelEntity::UpdateModel()
|
||||
}
|
||||
m_hModelHandle = g_pAssetManager->LoadModel(m_szCurrentModel);
|
||||
m_pModel = g_pAssetManager->GetModelByIndex(m_hModelHandle);
|
||||
CBaseMaterial *pMat = g_pAssetManager->GetMaterialByIndex(m_pModel->m_hMaterial)->m_pLayout;
|
||||
if (!m_pModel)
|
||||
{
|
||||
V_printf("Failed to load %u %s\n", V_strnlen(m_szCurrentModel,255), m_szCurrentModel);
|
||||
m_pInstance = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
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});
|
||||
m_pInstance->SetMaterial(pMat);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define FUNNY_MATERIALS_H
|
||||
#include "datamap.h"
|
||||
#include "stdint.h"
|
||||
#include "cglm/mat4.h"
|
||||
|
||||
#define TEXTURE_INVALID 0
|
||||
#define TEXTURE_NO_TEXTURE -1
|
||||
@@ -35,6 +36,11 @@ namespace FMat
|
||||
XMFLOAT4 row4;
|
||||
};
|
||||
}
|
||||
struct MaterialData_t
|
||||
{
|
||||
FMat::XMFLOAT4 m_vAlbedoColor;
|
||||
uint32_t m_uAlbedo;
|
||||
};
|
||||
|
||||
class CBaseMaterial
|
||||
{
|
||||
@@ -44,6 +50,7 @@ public:
|
||||
virtual const char *GetShaderPath() { return NULL; };
|
||||
virtual void *GetDataPtr() { return 0; };
|
||||
virtual size_t GetDataSize() { return 0; };
|
||||
virtual void SetUpMesh( MaterialData_t *pData ) {};
|
||||
|
||||
uint64_t m_uHash;
|
||||
};
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
#include "materials.h"
|
||||
#include "worldrender.h"
|
||||
#include "tier0/lib.h"
|
||||
|
||||
class CBasicPBR: public CBaseMaterial
|
||||
{
|
||||
@@ -6,6 +8,11 @@ public:
|
||||
DECLARE_CLASS(CBasicPBR, CBaseMaterial)
|
||||
DECLARE_DATADESC()
|
||||
DECLARE_SHADER("game/core/shaders/funny_basic_pbr.shader_c")
|
||||
virtual void SetUpMesh( MaterialData_t *pData ) override {
|
||||
pData->m_vAlbedoColor = m_vAlbedo;
|
||||
pData->m_uAlbedo = m_tAlbedo;
|
||||
V_printf("SetUpMesh %f\n", pData->m_vAlbedoColor.x);
|
||||
};
|
||||
|
||||
|
||||
FMat::XMFLOAT4 m_vAlbedo = { 1, 1, 1, 1 };
|
||||
|
||||
@@ -16,9 +16,10 @@ struct PerMeshData_t
|
||||
{
|
||||
mat4 m_matTranslation;
|
||||
mat4 m_matRotation;
|
||||
uint32_t m_uAlbedo;
|
||||
MaterialData_t m_textureData;
|
||||
};
|
||||
|
||||
|
||||
class CFunnyMeshInstance;
|
||||
class CFunnyMesh: public IMesh
|
||||
{
|
||||
@@ -56,6 +57,7 @@ public:
|
||||
virtual void SetPosition( Vector vPosition ) override;
|
||||
virtual void SetRotation( Quat vRotation ) override;
|
||||
virtual void SetScale( Vector vScale ) override;
|
||||
virtual void SetMaterial( CBaseMaterial *pMaterial ) override;
|
||||
virtual void Frame();
|
||||
|
||||
PerMeshData_t m_data = {};
|
||||
@@ -104,7 +106,6 @@ void CFunnyMeshInstance::Frame()
|
||||
glm_scale_make(m, v);
|
||||
glm_mat4_mul(m_data.m_matTranslation, m, m_data.m_matTranslation);
|
||||
|
||||
m_data.m_uAlbedo = 0;
|
||||
/*
|
||||
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]);
|
||||
@@ -113,6 +114,12 @@ void CFunnyMeshInstance::Frame()
|
||||
*/
|
||||
}
|
||||
|
||||
void CFunnyMeshInstance::SetMaterial( CBaseMaterial *pMaterial )
|
||||
{
|
||||
pMaterial->SetUpMesh(&m_data.m_textureData);
|
||||
|
||||
}
|
||||
|
||||
|
||||
class CFunnyWorldRenderer: public IWorldRenderer
|
||||
{
|
||||
@@ -131,6 +138,8 @@ public:
|
||||
virtual void DestroyMesh( IMesh *pMesh ) override;
|
||||
|
||||
virtual void ConfigureShader( IShader *pShader ) override;
|
||||
|
||||
virtual ITextureArray *GetTextures() override;
|
||||
private:
|
||||
CUtlVector<CFunnyMesh*> m_pMeshes;
|
||||
IImage *m_pOutputImage = NULL;
|
||||
@@ -395,3 +404,9 @@ void CFunnyWorldRenderer::DestroyMesh( IMesh *pMesh )
|
||||
|
||||
}
|
||||
|
||||
|
||||
ITextureArray *CFunnyWorldRenderer::GetTextures()
|
||||
{
|
||||
return m_pTextures;
|
||||
|
||||
}
|
||||
|
||||
@@ -5,6 +5,11 @@
|
||||
#include "gamesystem.h"
|
||||
#include "trig.h"
|
||||
#include "materialsystem/imaterialsystem.h"
|
||||
#include "materials.h"
|
||||
|
||||
#include "cglm/mat4.h"
|
||||
|
||||
|
||||
|
||||
abstract_class IMesh
|
||||
{
|
||||
@@ -32,6 +37,7 @@ public:
|
||||
virtual void SetPosition( Vector vPosition ) = 0;
|
||||
virtual void SetRotation( Quat vRotation ) = 0;
|
||||
virtual void SetScale( Vector vScale ) = 0;
|
||||
virtual void SetMaterial( CBaseMaterial *pMaterial ) = 0;
|
||||
};
|
||||
|
||||
abstract_class IWorldRenderer: public IGameSystem
|
||||
@@ -46,6 +52,8 @@ public:
|
||||
virtual void DestroyMesh( IMesh *pMesh ) = 0;
|
||||
|
||||
virtual void ConfigureShader( IShader *pShader ) = 0;
|
||||
|
||||
virtual ITextureArray *GetTextures() = 0;
|
||||
};
|
||||
|
||||
extern IWorldRenderer *g_pWorldRenderer;
|
||||
|
||||
BIN
shadercompiler/CallableMain
Normal file
BIN
shadercompiler/CallableMain
Normal file
Binary file not shown.
Binary file not shown.
BIN
shadercompiler/psMain
Normal file
BIN
shadercompiler/psMain
Normal file
Binary file not shown.
BIN
shadercompiler/rayMain
Normal file
BIN
shadercompiler/rayMain
Normal file
Binary file not shown.
BIN
shadercompiler/vsMain
Normal file
BIN
shadercompiler/vsMain
Normal file
Binary file not shown.
Reference in New Issue
Block a user