From c8b3ed0286d1a48a00cc5836891229b0d0d36009 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Thu, 7 Mar 2024 13:59:28 +0800 Subject: [PATCH] preparing blinnphong light sample. added codes of to load ubo. --- .../Std3D/BlinnPhong/BlinnPhongSun.ubo | 2 ++ .../Std3D/BlinnPhong/SunLightPureColor.mtl | 2 +- .../LightBasic/BlinnPhongDirectionLight.cpp | 26 ++++++++++++++++--- src/ShaderGen/MaterialFileLoader.cpp | 9 ++++++- src/ShaderGen/ShaderCreateInfo.cpp | 3 ++- 5 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 ShaderLibrary/Std3D/BlinnPhong/BlinnPhongSun.ubo diff --git a/ShaderLibrary/Std3D/BlinnPhong/BlinnPhongSun.ubo b/ShaderLibrary/Std3D/BlinnPhong/BlinnPhongSun.ubo new file mode 100644 index 00000000..9eda1481 --- /dev/null +++ b/ShaderLibrary/Std3D/BlinnPhong/BlinnPhongSun.ubo @@ -0,0 +1,2 @@ +vec3 direction; +vec3 color; diff --git a/ShaderLibrary/Std3D/BlinnPhong/SunLightPureColor.mtl b/ShaderLibrary/Std3D/BlinnPhong/SunLightPureColor.mtl index fb0994cf..62f7b86b 100644 --- a/ShaderLibrary/Std3D/BlinnPhong/SunLightPureColor.mtl +++ b/ShaderLibrary/Std3D/BlinnPhong/SunLightPureColor.mtl @@ -11,7 +11,7 @@ define HAVE_SPECULAR off //默认不定义HAVE_SPECULAR UBO { - File Sun.ubo //文件名 + File BlinnPhongSun.ubo //文件名,如果/开头表示从ShaderLibrary根目录开始,没有则表示同一目录 Name sun //在代码中的变量名 Stage Vertex,Fragment //会引用的shader } diff --git a/example/LightBasic/BlinnPhongDirectionLight.cpp b/example/LightBasic/BlinnPhongDirectionLight.cpp index dd76d2cd..79054c9d 100644 --- a/example/LightBasic/BlinnPhongDirectionLight.cpp +++ b/example/LightBasic/BlinnPhongDirectionLight.cpp @@ -9,6 +9,7 @@ #include #include #include +#include using namespace hgl; using namespace hgl::graph; @@ -17,6 +18,12 @@ static float lumiance_data[2]={1,1}; static Color4f white_color(1,1,1,1); +static mtl::blinnphong::SunLight sun_light= +{ + Vector3f(1,1,1), + Vector3f(1,0.975,0.95) +}; + class TestApp:public SceneAppFramework { private: //plane grid @@ -26,6 +33,10 @@ private: //plane grid Pipeline * p_line =nullptr; Primitive * prim_plane_grid =nullptr; +private: + + DeviceBuffer * ubo_sun =nullptr; + private: //sphere Material * mtl_sun_light =nullptr; @@ -55,6 +66,16 @@ private: return(true); } + bool CreateBlinnPhongUBO() + { + sun_light.color=Vector3f(1,1,1); + + ubo_sun=db->CreateUBO("sun",sizeof(sun_light),&sun_light); + if(!ubo_sun)return(false); + + return(true); + } + bool InitBlinnPhongSunLightMP() { mtl::Material3DCreateConfig cfg(device->GetDeviceAttribute(),"BlinnPhong3D",Prim::Triangles); @@ -64,8 +85,7 @@ private: mtl_sun_light=db->LoadMaterial("Std3D/BlinnPhong/SunLightPureColor",&cfg); if(!mtl_sun_light)return(false); - mtl_sun_light->BindUBO(DescriptorSetType::Global,"sun",sun_data,sizeof(sun_data)); //恢复SUN.UBO或是使用内嵌SUN定义也行, - //恢复SUN.UBO的意义是让引擎支持读取外部UBO配置文件 + mtl_sun_light->BindUBO(DescriptorSetType::Global,"sun",ubo_sun); mi_sphere=db->CreateMaterialInstance(mtl_sun_light); if(!mi_sphere)return(false); @@ -123,7 +143,7 @@ private: Add(prim_plane_grid,mi_plane_grid,p_line); Add(prim_sphere,mi_sphere,p_sphere); - camera->pos=Vector3f(32,32,32); + camera->pos=Vector3f(32,15,32); camera_control->SetTarget(Vector3f(0,0,0)); camera_control->Refresh(); diff --git a/src/ShaderGen/MaterialFileLoader.cpp b/src/ShaderGen/MaterialFileLoader.cpp index 2ed50ef5..ac96f92a 100644 --- a/src/ShaderGen/MaterialFileLoader.cpp +++ b/src/ShaderGen/MaterialFileLoader.cpp @@ -614,11 +614,16 @@ namespace }; }//namespace MaterialFile +namespace +{ + constexpr const os_char HGL_SHADER_LIBRARY_FOLDER[]=OS_TEXT("ShaderLibrary"); +}//namespace + MaterialFileData *LoadMaterialDataFromFile(const AnsiString &mtl_filename) { const OSString mtl_osfn=ToOSString(mtl_filename+".mtl"); - const OSString mtl_os_filename=filesystem::MergeFilename(OS_TEXT("ShaderLibrary"),mtl_osfn); + const OSString mtl_os_filename=filesystem::MergeFilename(HGL_SHADER_LIBRARY_FOLDER,mtl_osfn); if(!filesystem::FileExist(mtl_os_filename)) return nullptr; @@ -653,6 +658,8 @@ MaterialFileData *LoadMaterialDataFromFile(const AnsiString &mtl_filename) char *data; int size=filesystem::LoadFileToMemory(ubo_os_filename,(void **)&data,true); + + ..//读取所有的UBO文件 } } diff --git a/src/ShaderGen/ShaderCreateInfo.cpp b/src/ShaderGen/ShaderCreateInfo.cpp index 1a604b30..88570f71 100644 --- a/src/ShaderGen/ShaderCreateInfo.cpp +++ b/src/ShaderGen/ShaderCreateInfo.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -448,6 +448,7 @@ bool ShaderCreateInfo::CreateShader(ShaderCreateInfo *last_sc) #ifdef _DEBUG + //想办法存成文件或是输出行号,以方便出错了调试 LOG_INFO(AnsiString(GetShaderStageName(shader_stage))+" shader: \n"+final_shader); #endif//_DEBUG