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>
|
|
|
|
|
#include<hgl/graph/SceneDB.h>
|
|
|
|
|
#include<hgl/graph/RenderableInstance.h>
|
|
|
|
|
#include<hgl/graph/RenderList.h>
|
|
|
|
|
|
|
|
|
|
using namespace hgl;
|
|
|
|
|
using namespace hgl::graph;
|
|
|
|
|
|
|
|
|
|
constexpr uint32_t SCREEN_WIDTH=128;
|
|
|
|
|
constexpr uint32_t SCREEN_HEIGHT=128;
|
|
|
|
|
|
|
|
|
|
struct AtomsphereData
|
|
|
|
|
{
|
2020-01-20 20:00:03 +08:00
|
|
|
|
Vector3f position;
|
2019-06-21 12:06:57 +08:00
|
|
|
|
float intensity;
|
|
|
|
|
float scattering_direction;
|
|
|
|
|
};//
|
|
|
|
|
|
|
|
|
|
class TestApp:public CameraAppFramework
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
SceneNode render_root;
|
|
|
|
|
RenderList render_list;
|
|
|
|
|
|
|
|
|
|
vulkan::Material * material =nullptr;
|
2020-01-20 20:00:03 +08:00
|
|
|
|
vulkan::MaterialInstance * material_instance =nullptr;
|
2019-06-21 12:06:57 +08:00
|
|
|
|
|
2020-01-20 20:00:03 +08:00
|
|
|
|
vulkan::Renderable * ro_sphere =nullptr;
|
2019-06-21 12:06:57 +08:00
|
|
|
|
|
2020-01-20 20:00:03 +08:00
|
|
|
|
vulkan::Pipeline * pipeline_solid =nullptr;
|
2019-06-21 12:06:57 +08:00
|
|
|
|
|
|
|
|
|
vulkan::Buffer * ubo_atomsphere =nullptr;
|
|
|
|
|
AtomsphereData atomsphere_data;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
bool InitMaterial()
|
|
|
|
|
{
|
2020-06-12 15:55:51 +08:00
|
|
|
|
material=shader_manage->CreateMaterial(OS_TEXT("res/shader/Atomsphere.vert"),
|
|
|
|
|
OS_TEXT("res/shader/Atomsphere.frag"));
|
2019-06-21 12:06:57 +08:00
|
|
|
|
if(!material)
|
|
|
|
|
return(false);
|
|
|
|
|
|
2020-01-20 20:00:03 +08:00
|
|
|
|
material_instance=material->CreateInstance();
|
2019-06-21 12:06:57 +08:00
|
|
|
|
|
|
|
|
|
db->Add(material);
|
2020-01-20 20:00:03 +08:00
|
|
|
|
db->Add(material_instance);
|
2019-06-21 12:06:57 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CreateRenderObject()
|
|
|
|
|
{
|
2019-06-21 16:44:22 +08:00
|
|
|
|
ro_sphere=CreateRenderableSphere(db,material,128);
|
2019-06-21 12:06:57 +08:00
|
|
|
|
}
|
2020-07-09 20:37:34 +08:00
|
|
|
|
|
2020-07-09 13:26:39 +08:00
|
|
|
|
bool InitAtomsphereUBO(vulkan::MaterialInstance *mi,const AnsiString &sun_node_name)
|
2019-06-21 12:06:57 +08:00
|
|
|
|
{
|
|
|
|
|
atomsphere_data.position.Set(0,0.1f,-1.0f);
|
|
|
|
|
atomsphere_data.intensity=22.0f;
|
|
|
|
|
atomsphere_data.scattering_direction=0.758f;
|
|
|
|
|
|
|
|
|
|
ubo_atomsphere=db->CreateUBO(sizeof(AtomsphereData),&atomsphere_data);
|
|
|
|
|
|
|
|
|
|
if(!ubo_atomsphere)
|
|
|
|
|
return(false);
|
|
|
|
|
|
2020-01-20 20:00:03 +08:00
|
|
|
|
return mi->BindUBO(sun_node_name,ubo_atomsphere);
|
2019-06-21 12:06:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitUBO()
|
|
|
|
|
{
|
2020-07-09 13:26:39 +08:00
|
|
|
|
if(!material_instance->BindUBO("world",GetCameraMatrixBuffer()))
|
2019-06-21 12:06:57 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2020-01-20 20:00:03 +08:00
|
|
|
|
if(!InitAtomsphereUBO(material_instance,"sun"))
|
2019-06-21 12:06:57 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2020-01-20 20:00:03 +08:00
|
|
|
|
material_instance->Update();
|
2019-06-21 12:06:57 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitPipeline()
|
|
|
|
|
{
|
2019-07-05 17:03:28 +08:00
|
|
|
|
AutoDelete<vulkan::PipelineCreater>
|
2019-07-16 20:22:29 +08:00
|
|
|
|
pipeline_creater=new vulkan::PipelineCreater(device,material,sc_render_target);
|
2019-06-21 12:06:57 +08:00
|
|
|
|
pipeline_creater->SetDepthTest(true);
|
|
|
|
|
pipeline_creater->SetDepthWrite(true);
|
|
|
|
|
pipeline_creater->SetCullMode(VK_CULL_MODE_NONE);
|
|
|
|
|
pipeline_creater->Set(PRIM_TRIANGLES);
|
|
|
|
|
pipeline_solid=pipeline_creater->Create();
|
|
|
|
|
|
|
|
|
|
if(!pipeline_solid)
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
db->Add(pipeline_solid);
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitScene()
|
|
|
|
|
{
|
2020-01-20 20:00:03 +08:00
|
|
|
|
render_root.Add(db->CreateRenderableInstance(pipeline_solid,material_instance,ro_sphere),scale(100));
|
2019-06-21 12:06:57 +08:00
|
|
|
|
|
|
|
|
|
render_root.RefreshMatrix();
|
|
|
|
|
render_root.ExpendToList(&render_list);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
bool Init()
|
|
|
|
|
{
|
|
|
|
|
if(!CameraAppFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitMaterial())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
CreateRenderObject();
|
|
|
|
|
|
|
|
|
|
if(!InitUBO())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitPipeline())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitScene())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BuildCommandBuffer(uint32_t index) override
|
|
|
|
|
{
|
|
|
|
|
render_root.RefreshMatrix();
|
|
|
|
|
render_list.Clear();
|
|
|
|
|
render_root.ExpendToList(&render_list);
|
|
|
|
|
|
|
|
|
|
VulkanApplicationFramework::BuildCommandBuffer(index,&render_list);
|
|
|
|
|
}
|
|
|
|
|
};//class TestApp:public CameraAppFramework
|
|
|
|
|
|
|
|
|
|
int main(int,char **)
|
|
|
|
|
{
|
|
|
|
|
TestApp app;
|
|
|
|
|
|
|
|
|
|
if(!app.Init())
|
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
|
|
while(app.Run());
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|