test sunlight UBO ok!

This commit is contained in:
hyzboy 2024-03-10 00:35:35 +08:00
parent 5188d4149c
commit f3a59c1bb3
5 changed files with 42 additions and 31 deletions

View File

@ -1,2 +1,2 @@
vec3 direction; vec4 direction;
vec3 color; vec4 color;

View File

@ -14,11 +14,17 @@ UBO
File BlinnPhongSun.ubo //文件名,如果/开头表示从ShaderLibrary根目录开始没有则表示同一目录 File BlinnPhongSun.ubo //文件名,如果/开头表示从ShaderLibrary根目录开始没有则表示同一目录
Struct BlinnPhongSun //结构名称 Struct BlinnPhongSun //结构名称
Name sun //在代码中的变量名 Name sun //在代码中的变量名
Stage Vertex,Fragment //会引用的shader Stage Fragment //会引用的shader
Set Global //Descriptor Set Set Global //Descriptor Set
} }
#MaterialInstance #MaterialInstance
Length 16
Stage Fragment
Code
{
vec4 Color;
}
#VertexInput #VertexInput
vec3 Normal vec3 Normal
@ -33,6 +39,7 @@ Code
{ {
void main() void main()
{ {
HandoverMI();
gl_Position=GetPosition3D(); gl_Position=GetPosition3D();
} }
} }
@ -47,31 +54,33 @@ Code
{ {
void main() void main()
{ {
MaterialInstance mi=GetMI();
//将法线归一化 //将法线归一化
vec3 world_normal =normalize(Input.Normal); vec3 world_normal =normalize(Input.Normal);
//对世界坐标下的灯光方法归一化 //对世界坐标下的灯光方法归一化
vec3 world_light_direction =normalize(sun.direction); vec3 world_light_direction =normalize(sun.direction.xyz);
//点乘法线和光照 //点乘法线和光照
vec3 diffuse =0.5*dot(world_light_direction,world_normal)+0.5; vec3 diffuse =vec3(0.5)*dot(world_light_direction.xyz,world_normal)+vec3(0.5);
//直接光颜色 //直接光颜色
vec3 direct_color =sun.diffuse*diffuse*sun.color; vec3 direct_color =sun.color.rgb*mi.Color.rgb;
#ifndef HAVE_SPECULAR // #ifndef HAVE_SPECULAR
FragColor=vec4(direct_color,1.0); FragColor=vec4(direct_color,1.0);
#else // #else
//归一代视角方向 // //归一代视角方向
vec3 view_direction =normalize(camera.pos-world_position); // vec3 view_direction =normalize(camera.pos-world_position);
//
//世界坐标下的反射光方向 // //世界坐标下的反射光方向
vec3 reflect_direction =normalize(reflect(-world_light_direction,world_normal)); // vec3 reflect_direction =normalize(reflect(-world_light_direction,world_normal));
//
//高光 // //高光
vec3 specular =sun.specular*pow(saturate(dot(reflect_direction,view_direction)),gloss); // vec3 specular =sun.specular*pow(saturate(dot(reflect_direction,view_direction)),gloss);
//
FragColor=vec4(direct_color+specular,1.0); // FragColor=vec4(direct_color+specular,1.0);
#endif//HAVE_SPECULAR // #endif//HAVE_SPECULAR
} }
} }

View File

@ -20,8 +20,8 @@ static Color4f white_color(1,1,1,1);
static mtl::blinnphong::SunLight sun_light= static mtl::blinnphong::SunLight sun_light=
{ {
Vector3f(1,1,1), Vector4f(1,1,1,0),
Vector3f(1,0.975,0.95) Vector4f(1,0,0,1)
}; };
class TestApp:public SceneAppFramework class TestApp:public SceneAppFramework
@ -68,8 +68,6 @@ private:
bool CreateBlinnPhongUBO() bool CreateBlinnPhongUBO()
{ {
sun_light.color=Vector3f(1,1,1);
ubo_sun=db->CreateUBO("sun",sizeof(sun_light),&sun_light); ubo_sun=db->CreateUBO("sun",sizeof(sun_light),&sun_light);
if(!ubo_sun)return(false); if(!ubo_sun)return(false);
@ -86,8 +84,9 @@ private:
if(!mtl_sun_light)return(false); if(!mtl_sun_light)return(false);
mtl_sun_light->BindUBO(DescriptorSetType::Global,"sun",ubo_sun); mtl_sun_light->BindUBO(DescriptorSetType::Global,"sun",ubo_sun);
mtl_sun_light->Update();
mi_sphere=db->CreateMaterialInstance(mtl_sun_light); mi_sphere=db->CreateMaterialInstance(mtl_sun_light,nullptr,&white_color);
if(!mi_sphere)return(false); if(!mi_sphere)return(false);
p_sphere=CreatePipeline(mtl_sun_light,InlinePipeline::Solid3D,Prim::Triangles); p_sphere=CreatePipeline(mtl_sun_light,InlinePipeline::Solid3D,Prim::Triangles);
@ -163,6 +162,9 @@ public:
if(!InitVertexLumMP()) if(!InitVertexLumMP())
return(false); return(false);
if(!CreateBlinnPhongUBO())
return(false);
if(!InitBlinnPhongSunLightMP()) if(!InitBlinnPhongSunLightMP())
return(false); return(false);

View File

@ -49,15 +49,15 @@ public:
virtual ~Material(); virtual ~Material();
const UTF8String & GetName ()const{return name;} const AnsiString & GetName ()const{return name;}
const VertexInput * GetVertexInput ()const{return vertex_input;} const VertexInput * GetVertexInput ()const{return vertex_input;}
const ShaderStageCreateInfoList & GetStageList ()const{return shader_stage_list;} const ShaderStageCreateInfoList & GetStageList ()const{return shader_stage_list;}
const MaterialDescriptorManager * GetDescriptorSets ()const{return desc_manager;} // const MaterialDescriptorManager * GetDescriptorManager ()const{return desc_manager;}
const VkPipelineLayout GetPipelineLayout ()const; const VkPipelineLayout GetPipelineLayout ()const;
const PipelineLayoutData * GetPipelineLayoutData ()const{return pipeline_layout_data;} // const PipelineLayoutData * GetPipelineLayoutData ()const{return pipeline_layout_data;}
public: public:

View File

@ -8,8 +8,8 @@ namespace blinnphong
{ {
struct SunLight struct SunLight
{ {
Vector3f direction; Vector4f direction;
Vector3f color; Vector4f color;
};//struct SunLight };//struct SunLight
constexpr const ShaderBufferSource SBS_SunLight= constexpr const ShaderBufferSource SBS_SunLight=
@ -18,8 +18,8 @@ namespace blinnphong
"sun", "sun",
R"( R"(
vec3 direction; vec4 direction;
vec3 color; vec4 color;
)" )"
}; };
}//namespace blinnphong }//namespace blinnphong