2019-06-24 20:18:01 +08:00
|
|
|
|
// 8.大气渲染
|
2019-06-21 12:06:57 +08:00
|
|
|
|
// 画一个球,纯粹使用shader计算出颜色
|
|
|
|
|
|
|
|
|
|
#include"VulkanAppFramework.h"
|
|
|
|
|
#include<hgl/filesystem/FileSystem.h>
|
|
|
|
|
#include<hgl/graph/InlineGeometry.h>
|
2020-10-21 12:39:22 +08:00
|
|
|
|
#include<hgl/graph/VKRenderResource.h>
|
2025-05-18 02:15:33 +08:00
|
|
|
|
#include<hgl/graph/Mesh.h>
|
2019-06-21 12:06:57 +08:00
|
|
|
|
#include<hgl/graph/RenderList.h>
|
|
|
|
|
|
|
|
|
|
using namespace hgl;
|
|
|
|
|
using namespace hgl::graph;
|
|
|
|
|
|
2021-09-27 10:44:35 +08:00
|
|
|
|
constexpr uint32_t SCREEN_WIDTH=512;
|
|
|
|
|
constexpr uint32_t SCREEN_HEIGHT=512;
|
2019-06-21 12:06:57 +08:00
|
|
|
|
|
2020-09-19 23:49:32 +08:00
|
|
|
|
struct AtmosphereData
|
2019-06-21 12:06:57 +08:00
|
|
|
|
{
|
2020-01-20 20:00:03 +08:00
|
|
|
|
Vector3f position;
|
2019-06-21 12:06:57 +08:00
|
|
|
|
float intensity;
|
|
|
|
|
float scattering_direction;
|
2020-09-19 23:49:32 +08:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
AtmosphereData()
|
|
|
|
|
{
|
2021-09-27 10:44:35 +08:00
|
|
|
|
position=Vector3f(0,0.1f,-1.0f);
|
2020-09-19 23:49:32 +08:00
|
|
|
|
intensity=22.0f;
|
|
|
|
|
scattering_direction=0.758f;
|
|
|
|
|
}
|
|
|
|
|
};//struct AtmosphereData
|
2019-06-21 12:06:57 +08:00
|
|
|
|
|
|
|
|
|
class TestApp:public CameraAppFramework
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
|
2021-09-27 10:44:35 +08:00
|
|
|
|
SceneNode render_root;
|
|
|
|
|
RenderList * render_list=nullptr;
|
2019-06-21 12:06:57 +08:00
|
|
|
|
|
2021-09-27 10:44:35 +08:00
|
|
|
|
MaterialInstance * material_instance =nullptr;
|
2020-10-21 12:47:06 +08:00
|
|
|
|
Pipeline * pipeline_solid =nullptr;
|
2019-06-21 12:06:57 +08:00
|
|
|
|
|
2022-10-14 17:52:35 +08:00
|
|
|
|
DeviceBuffer * ubo_atomsphere =nullptr;
|
2020-10-21 12:47:06 +08:00
|
|
|
|
AtmosphereData atomsphere_data;
|
2020-09-19 23:49:32 +08:00
|
|
|
|
|
2022-10-14 17:52:35 +08:00
|
|
|
|
Primitive * ro_sphere =nullptr;
|
2019-06-21 12:06:57 +08:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
bool InitMaterial()
|
|
|
|
|
{
|
2020-09-19 23:49:32 +08:00
|
|
|
|
material_instance=db->CreateMaterialInstance(OS_TEXT("res/material/Atmosphere")); //不需要优先创建Material,也不需要写扩展名
|
|
|
|
|
if(!material_instance)return(false);
|
2020-09-20 21:17:16 +08:00
|
|
|
|
|
|
|
|
|
// pipeline_solid=db->CreatePipeline(material_instance,sc_render_target,OS_TEXT("res/pipeline/sky"));
|
2021-09-27 10:44:35 +08:00
|
|
|
|
pipeline_solid=CreatePipeline(material_instance,InlinePipeline::Sky,Prim::Triangles); //等同上一行,为Framework重载,默认使用swapchain的render target
|
2020-09-19 23:49:32 +08:00
|
|
|
|
if(!pipeline_solid)return(false);
|
2019-06-21 12:06:57 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitUBO()
|
|
|
|
|
{
|
2020-09-19 23:49:32 +08:00
|
|
|
|
ubo_atomsphere=db->CreateUBO(sizeof(AtmosphereData),&atomsphere_data);
|
2020-09-18 22:39:33 +08:00
|
|
|
|
|
2020-09-19 23:49:32 +08:00
|
|
|
|
if(!ubo_atomsphere)
|
2020-09-18 22:39:33 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2021-09-27 10:44:35 +08:00
|
|
|
|
{
|
2023-02-22 21:53:51 +08:00
|
|
|
|
MaterialParameters *mp=material_instance->GetMP(DescriptorSetType::Value);
|
2021-09-27 10:44:35 +08:00
|
|
|
|
|
|
|
|
|
if(!mp)return(false);
|
|
|
|
|
|
|
|
|
|
if(!mp->BindUBO("sun",ubo_atomsphere))
|
|
|
|
|
return(false);
|
2019-06-21 12:06:57 +08:00
|
|
|
|
|
2021-09-27 10:44:35 +08:00
|
|
|
|
mp->Update();
|
|
|
|
|
}
|
2019-06-21 12:06:57 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitScene()
|
|
|
|
|
{
|
2022-10-11 19:16:06 +08:00
|
|
|
|
ro_sphere=inline_geometry::CreateSphere(db,material_instance->GetVIL(),128);
|
2020-09-27 22:30:12 +08:00
|
|
|
|
|
2022-06-24 21:06:38 +08:00
|
|
|
|
render_root.CreateSubNode(scale(100),db->CreateRenderable(ro_sphere,material_instance,pipeline_solid));
|
2019-06-21 12:06:57 +08:00
|
|
|
|
|
|
|
|
|
render_root.RefreshMatrix();
|
2021-09-27 10:44:35 +08:00
|
|
|
|
render_list->Expend(GetCameraInfo(),&render_root);
|
2019-06-21 12:06:57 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2021-09-27 10:44:35 +08:00
|
|
|
|
~TestApp()
|
|
|
|
|
{
|
|
|
|
|
SAFE_CLEAR(render_list);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-21 12:06:57 +08:00
|
|
|
|
bool Init()
|
|
|
|
|
{
|
|
|
|
|
if(!CameraAppFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
|
|
|
|
|
return(false);
|
2021-09-27 10:44:35 +08:00
|
|
|
|
|
|
|
|
|
render_list=new RenderList(device);
|
2019-06-21 12:06:57 +08:00
|
|
|
|
|
|
|
|
|
if(!InitMaterial())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitUBO())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitScene())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BuildCommandBuffer(uint32_t index) override
|
|
|
|
|
{
|
|
|
|
|
render_root.RefreshMatrix();
|
2021-09-27 10:44:35 +08:00
|
|
|
|
render_list->Expend(GetCameraInfo(),&render_root);
|
2019-06-21 12:06:57 +08:00
|
|
|
|
|
2021-09-27 10:44:35 +08:00
|
|
|
|
VulkanApplicationFramework::BuildCommandBuffer(index,render_list);
|
2019-06-21 12:06:57 +08:00
|
|
|
|
}
|
|
|
|
|
};//class TestApp:public CameraAppFramework
|
|
|
|
|
|
|
|
|
|
int main(int,char **)
|
|
|
|
|
{
|
|
|
|
|
TestApp app;
|
|
|
|
|
|
|
|
|
|
if(!app.Init())
|
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
|
|
while(app.Run());
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|