diff --git a/example/Vulkan/Deferred.cpp b/example/Vulkan/Deferred.cpp index da4d84eb..74559e6e 100644 --- a/example/Vulkan/Deferred.cpp +++ b/example/Vulkan/Deferred.cpp @@ -14,6 +14,10 @@ using namespace hgl; using namespace hgl::graph; +VK_NAMESPACE_BEGIN +Texture2D *LoadTGATexture(const OSString &filename,Device *device); +VK_NAMESPACE_END + constexpr uint32_t GBUFFER_WIDTH=1024; constexpr uint32_t GBUFFER_HEIGHT=1024; @@ -76,7 +80,14 @@ private: SubpassParam sp_gbuffer; SubpassParam sp_composition; - vulkan::Renderable *ro_sphere; + vulkan::Renderable * ro_cube; + + vulkan::Sampler * sampler; + + struct + { + vulkan::Texture2D *color,*normal,*specular; + }texture; private: @@ -224,12 +235,18 @@ private: if(!InitGBufferPipeline(&sp_gbuffer))return(false); if(!InitCompositionPipeline(&sp_composition))return(false); + texture.color =vulkan::LoadTGATexture(OS_TEXT("cardboardPlainStain.tga"),device); + texture.normal =vulkan::LoadTGATexture(OS_TEXT("APOCWALL029_NRM.tga"),device); + texture.specular=vulkan::LoadTGATexture(OS_TEXT("APOCWALL029_SPEC.tga"),device); + return(true); } void CreateRenderObject(vulkan::Material *mtl) { - ro_sphere=CreateRenderableSphere(db,mtl,128); + struct CubeCreateInfo cci; + + ro_cube=CreateRenderableCube(db,mtl,&cci); } bool InitUBO(SubpassParam *sp) @@ -248,7 +265,7 @@ private: CreateRenderObject(sp->material); - render_root.Add(db->CreateRenderableInstance(sp->pipeline,sp->desc_sets,ro_sphere),scale(1000)); + render_root.Add(db->CreateRenderableInstance(sp->pipeline,sp->desc_sets,ro_cube),scale(1000)); render_root.RefreshMatrix(); render_root.ExpendToList(&render_list); diff --git a/res/image/APOCWALL029_NRM.tga b/res/image/APOCWALL029_NRM.tga new file mode 100644 index 00000000..6fe7ef3c Binary files /dev/null and b/res/image/APOCWALL029_NRM.tga differ diff --git a/res/image/APOCWALL029_SPEC.tga b/res/image/APOCWALL029_SPEC.tga new file mode 100644 index 00000000..ee6dd8e5 Binary files /dev/null and b/res/image/APOCWALL029_SPEC.tga differ diff --git a/res/image/brdflut.png b/res/image/brdflut.png deleted file mode 100644 index 5560949a..00000000 Binary files a/res/image/brdflut.png and /dev/null differ diff --git a/res/image/brdflut.tga b/res/image/brdflut.tga new file mode 100644 index 00000000..afcbbe66 Binary files /dev/null and b/res/image/brdflut.tga differ diff --git a/res/image/cardboardPlainStain.tga b/res/image/cardboardPlainStain.tga new file mode 100644 index 00000000..cc46d648 Binary files /dev/null and b/res/image/cardboardPlainStain.tga differ diff --git a/res/shader/Atomsphere.frag b/res/shader/Atomsphere.frag index 23cf1d26..2e1eca69 100644 --- a/res/shader/Atomsphere.frag +++ b/res/shader/Atomsphere.frag @@ -121,7 +121,7 @@ vec3 atmosphere(vec3 r, vec3 r0, vec3 pSun, float iSun, float rPlanet, float rAt void main() { - vec3 nrd=vec3(.x,-FragmentVertex.y,FragmentVertex.z); //vulkan coord to opengl(shader from opengl sample) + vec3 nrd=vec3(FragmentVertex.x,-FragmentVertex.y,FragmentVertex.z); //vulkan coord to opengl(shader from opengl sample) vec3 color=atmosphere( nrd, // normalized ray direction diff --git a/res/shader/gbuffer_opaque.frag b/res/shader/gbuffer_opaque.frag new file mode 100644 index 00000000..df04bd27 --- /dev/null +++ b/res/shader/gbuffer_opaque.frag @@ -0,0 +1,27 @@ +#version 450 + +layout (binding = 1) uniform sampler2D TextureColor; +layout (binding = 2) uniform sampler2D TextureNormal; + +layout(location = 0) in vec3 FragmentNormal; +layout(location = 1) in vec3 FragmentTangent; +layout(location = 2) in vec3 FragmentPosition; +layout(location = 3) in vec2 FragmentTexCoord; + +layout (location = 0) out vec4 outPosition; +layout (location = 1) out vec4 outNormal; +layout (location = 2) out vec4 outColor; + +void main() +{ + outPosition=vec4(FragmentPosition,1.0); + + vec3 N = normalize(FragmentNormal); + vec3 T = normalize(FragmentTangent); + vec3 B = cross(N,T); + mat3 TBN = mat3(T,B,N); + vec3 tnorm = TBN * normalize(texture(TextureNormal,FragmentTexCoord).xyz*2.0-vec3(1.0)); + + outNormal=vec4(tnorm,1.0); + outColor=texture(TextureColor,FragmentTexCoord); +} diff --git a/res/shader/gbuffer_opaque.vert b/res/shader/gbuffer_opaque.vert new file mode 100644 index 00000000..d27bc5dd --- /dev/null +++ b/res/shader/gbuffer_opaque.vert @@ -0,0 +1,38 @@ +#version 450 core + +layout(location = 0) in vec3 Vertex; +layout(location = 1) in vec2 TexCoord; +layout(location = 2) in vec3 Normal; +layout(location = 3) in vec3 Tangent; + +layout(binding = 0) uniform WorldMatrix +{ + mat4 two_dim; + mat4 projection; + mat4 modelview; + mat4 mvp; +} world; + +layout(push_constant) uniform Consts { + mat4 local_to_world; +} pc; + +layout(location = 0) out vec3 FragmentNormal; +layout(location = 1) out vec3 FragmentTangent; +layout(location = 2) out vec3 FragmentPosition; +layout(location = 3) out vec2 FragmentTexCoord; + +void main() +{ + vec4 pos=vec4(Vertex,1.0)*pc.local_to_world; + + gl_Position=pos*world.mvp; + + FragmentPosition=pos.xyz; + FragmentTexCoord=TexCoord; + + mat3 n=transpose(inverse(mat3(pc.local_to_world))); + + FragmentNormal=n*normalize(Normal); + FragmentTangent=n*normalize(Tangent); +} diff --git a/res/shader/shader_compile.sh b/res/shader/shader_compile.sh index 6ae4f732..6122d02f 100755 --- a/res/shader/shader_compile.sh +++ b/res/shader/shader_compile.sh @@ -14,3 +14,6 @@ glslangValidator -V -o c_gbuffer.frag.spv c_gbuffer.frag glslangValidator -V -o Atomsphere.vert.spv Atomsphere.vert glslangValidator -V -o Atomsphere.frag.spv Atomsphere.frag + +glslangValidator -V -o gbuffer_opaque.vert.spv gbuffer_opaque.vert +glslangValidator -V -o gbuffer_opaque.frag.spv gbuffer_opaque.frag