From 42fc94cff9ad541f9bd98107ff46e88533d41f58 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Fri, 12 Jul 2019 17:33:38 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=86=85=E7=BD=AE=E5=87=A0=E4=BD=95=E4=BD=93?= =?UTF-8?q?=E7=94=9F=E6=88=90=E4=BB=A3=E7=A0=81=E4=BF=AE=E8=AE=A2=EF=BC=8C?= =?UTF-8?q?=202.CAMERA=E9=BB=98=E8=AE=A4znear/zfar=E4=BF=AE=E8=AE=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/Vulkan/Deferred.cpp | 54 +++++++++++--------- example/Vulkan/VulkanAppFramework.h | 4 +- inc/hgl/graph/Camera.h | 2 +- inc/hgl/graph/vulkan/VKCommandBuffer.h | 1 + inc/hgl/math/Matrix.h | 5 ++ res/shader/gbuffer_opaque.frag | 8 +-- res/shader/gbuffer_opaque.vert | 8 +-- src/RenderDevice/Vulkan/VKPipeline.cpp | 2 +- src/SceneGraph/Camera.cpp | 6 +-- src/SceneGraph/InlineGeometry.cpp | 68 +++++++++++++------------- 10 files changed, 88 insertions(+), 70 deletions(-) diff --git a/example/Vulkan/Deferred.cpp b/example/Vulkan/Deferred.cpp index 80b405f4..ee3564af 100644 --- a/example/Vulkan/Deferred.cpp +++ b/example/Vulkan/Deferred.cpp @@ -19,9 +19,6 @@ 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; - constexpr uint32_t SCREEN_WIDTH=128; constexpr uint32_t SCREEN_HEIGHT=128; @@ -83,7 +80,8 @@ private: { vulkan::Material * material; vulkan::DescriptorSets *desc_sets; - vulkan::Pipeline * pipeline; + vulkan::Pipeline * pipeline_line; + vulkan::Pipeline * pipeline_solid; };// SubpassParam sp_gbuffer; @@ -151,8 +149,8 @@ private: bool InitGBuffer() { - gbuffer.extent.width =GBUFFER_WIDTH; - gbuffer.extent.height =GBUFFER_HEIGHT; + gbuffer.extent.width =512; + gbuffer.extent.height =512; //根据候选格式表选择格式 //const VkFormat position_format =GetCandidateFormat(position_candidate_format, sizeof(position_candidate_format)); @@ -166,9 +164,9 @@ private: // ||depth_format ==FMT_UNDEFINED) // return(false); - const VkFormat position_format =FMT_RGBA16F; - const VkFormat color_format =FMT_RGBA16F; - const VkFormat normal_format =FMT_RGBA16F; + const VkFormat position_format =FMT_RGBA32F; + const VkFormat color_format =FMT_RGBA32F; + const VkFormat normal_format =FMT_RGBA32F; const VkFormat depth_format =FMT_D32F; gbuffer.position=device->CreateAttachmentTextureColor(position_format, gbuffer.extent.width,gbuffer.extent.height); @@ -230,16 +228,24 @@ private: bool InitGBufferPipeline(SubpassParam *sp) { AutoDelete pipeline_creater=new vulkan::PipelineCreater(device,sp->material,gbuffer.renderpass,gbuffer.extent); - //pipeline_creater->SetCullMode(VK_CULL_MODE_BACK_BIT); - pipeline_creater->CloseCullFace(); pipeline_creater->Set(PRIM_TRIANGLES); - sp->pipeline=pipeline_creater->Create(); + sp->pipeline_solid=pipeline_creater->Create(); - if(!sp->pipeline) + if(!sp->pipeline_solid) return(false); - db->Add(sp->pipeline); + db->Add(sp->pipeline_solid); + + pipeline_creater->CloseCullFace(); + pipeline_creater->Set(PRIM_LINES); + + sp->pipeline_line=pipeline_creater->Create(); + + if(!sp->pipeline_line) + return(false); + + db->Add(sp->pipeline_line); return(true); } @@ -251,12 +257,12 @@ private: pipeline_creater->SetCullMode(VK_CULL_MODE_NONE); pipeline_creater->Set(PRIM_TRIANGLES); - sp->pipeline=pipeline_creater->Create(); + sp->pipeline_solid=pipeline_creater->Create(); - if(!sp->pipeline) + if(!sp->pipeline_solid) return(false); - db->Add(sp->pipeline); + db->Add(sp->pipeline_solid); return(true); } @@ -270,6 +276,7 @@ private: texture.color =vulkan::LoadTGATexture(OS_TEXT("res/image/cardboardPlainStain.tga"),device); texture.normal =vulkan::LoadTGATexture(OS_TEXT("res/image/APOCWALL029_NRM.tga"),device); + //texture.normal =vulkan::LoadTGATexture(OS_TEXT("res/image/flat_normal.tga"),device); //texture.specular=vulkan::LoadTGATexture(OS_TEXT("res/image/APOCWALL029_SPEC.tga"),device); VkSamplerCreateInfo sampler_create_info; @@ -372,13 +379,12 @@ private: bool InitScene(SubpassParam *sp) { CreateRenderObject(sp->material); - render_root.Add(db->CreateRenderableInstance(sp->pipeline,sp->desc_sets,ro_cube),scale(50,50,50)); - render_root.Add(db->CreateRenderableInstance(sp->pipeline,sp->desc_sets,ro_plane_grid)); - render_root.Add(db->CreateRenderableInstance(sp->pipeline,sp->desc_sets,ro_torus)); - render_root.Add(db->CreateRenderableInstance(sp->pipeline,sp->desc_sets,ro_cube ),translate(-10, 0, 5)*scale(10,10,10)); - render_root.Add(db->CreateRenderableInstance(sp->pipeline,sp->desc_sets,ro_sphere ),translate( 10, 0, 5)*scale(40,40,40)); - render_root.Add(db->CreateRenderableInstance(sp->pipeline,sp->desc_sets,ro_cylinder ),translate( 0, 16, 0)); - render_root.Add(db->CreateRenderableInstance(sp->pipeline,sp->desc_sets,ro_cone ),translate( 0,-16, 0)); + render_root.Add(db->CreateRenderableInstance(sp->pipeline_line,sp->desc_sets,ro_plane_grid)); + render_root.Add(db->CreateRenderableInstance(sp->pipeline_solid,sp->desc_sets,ro_torus ),translate(0,0,25)); + render_root.Add(db->CreateRenderableInstance(sp->pipeline_solid,sp->desc_sets,ro_sphere ),scale(25,25,25)); + render_root.Add(db->CreateRenderableInstance(sp->pipeline_solid,sp->desc_sets,ro_cube ),translate(-16, 0,15)*scale(10,10,10)); + render_root.Add(db->CreateRenderableInstance(sp->pipeline_solid,sp->desc_sets,ro_cylinder ),translate( 16, 16,10)*scale(1,1,2)); + render_root.Add(db->CreateRenderableInstance(sp->pipeline_solid,sp->desc_sets,ro_cone ),translate( 0,-16, 0)*scale(1,1,2)); render_root.RefreshMatrix(); render_root.ExpendToList(&render_list); diff --git a/example/Vulkan/VulkanAppFramework.h b/example/Vulkan/VulkanAppFramework.h index 82afec6c..829e612c 100644 --- a/example/Vulkan/VulkanAppFramework.h +++ b/example/Vulkan/VulkanAppFramework.h @@ -276,7 +276,9 @@ public: camera.width=w; camera.height=h; camera.center.Set(0,0,0,1); - camera.eye.Set(100,100,0,1); //xyz三个值不要一样,以方便调试 + camera.eye.Set(100,100,100,1); //xyz三个值不要一样,以方便调试 + camera.znear=16; + camera.zfar=256; camera.Refresh(); //更新矩阵计算 } diff --git a/inc/hgl/graph/Camera.h b/inc/hgl/graph/Camera.h index 1a118916..fe02479c 100644 --- a/inc/hgl/graph/Camera.h +++ b/inc/hgl/graph/Camera.h @@ -39,7 +39,7 @@ namespace hgl float height; ///<视图高 float fov=60; ///<水平FOV - float znear=0.01,zfar=1024; ///cv_count)return; diff --git a/inc/hgl/math/Matrix.h b/inc/hgl/math/Matrix.h index 098ffac4..8fc426bc 100644 --- a/inc/hgl/math/Matrix.h +++ b/inc/hgl/math/Matrix.h @@ -90,6 +90,11 @@ namespace hgl { const float f = 1.0f / tan( hgl_ang2rad( 0.5f * field_of_view ) ); + // float scaleX, shearXy, shearXz, x; + //float shearYx, scaleY, shearYz, y; + //float shearZx, shearZy, scaleZ, z; + //float shearWx, shearWy, shearWz, w; + return Matrix4f( f / aspect_ratio, 0.0f, 0.0f, 0.0f, 0.0f, -f, 0.0f, 0.0f, diff --git a/res/shader/gbuffer_opaque.frag b/res/shader/gbuffer_opaque.frag index 6c3f687d..bf2e6393 100644 --- a/res/shader/gbuffer_opaque.frag +++ b/res/shader/gbuffer_opaque.frag @@ -16,12 +16,14 @@ void main() { outPosition=vec4(normalize(FragmentPosition*2.0-vec3(1.0)),1.0); - vec3 N = normalize(FragmentNormal); + /*vec3 N = normalize(FragmentNormal); vec3 T = normalize(FragmentTangent); vec3 B = cross(N,T); mat3 TBN = mat3(T,B,N); - vec3 tnorm = normalize(texture(TextureNormal,FragmentTexCoord).xyz*2.0-vec3(1.0))*TBN; + vec3 tnorm = (texture(TextureNormal,FragmentTexCoord).xyz*2.0-vec3(1.0))*TBN; - outNormal=vec4(tnorm,1.0); + outNormal=vec4(normalize(tnorm),1.0);*/ + outNormal=vec4(normalize(FragmentNormal),1.0); + outColor=texture(TextureColor,FragmentTexCoord); } diff --git a/res/shader/gbuffer_opaque.vert b/res/shader/gbuffer_opaque.vert index f42976d6..535240f7 100644 --- a/res/shader/gbuffer_opaque.vert +++ b/res/shader/gbuffer_opaque.vert @@ -32,8 +32,10 @@ void main() FragmentPosition=(pos*world.modelview).xyz; FragmentTexCoord=TexCoord; - mat3 n=mat3(pc.local_to_world*world.modelview); + //mat3 n=mat3(pc.local_to_world*world.modelview); - FragmentNormal=normalize(Normal)*n; - FragmentTangent=normalize(Tangent)*n; + //FragmentNormal=normalize(Normal)*n; + //FragmentTangent=normalize(Tangent)*n; + FragmentNormal=normalize(Normal); + FragmentTangent=normalize(Tangent); } diff --git a/src/RenderDevice/Vulkan/VKPipeline.cpp b/src/RenderDevice/Vulkan/VKPipeline.cpp index e9b0f878..52720e5e 100644 --- a/src/RenderDevice/Vulkan/VKPipeline.cpp +++ b/src/RenderDevice/Vulkan/VKPipeline.cpp @@ -93,7 +93,7 @@ PipelineCreater::PipelineCreater(Device *dev,const Material *material,RenderPass rasterizer.rasterizerDiscardEnable = VK_FALSE; rasterizer.polygonMode = VK_POLYGON_MODE_FILL; rasterizer.cullMode = VK_CULL_MODE_BACK_BIT; - rasterizer.frontFace = VK_FRONT_FACE_CLOCKWISE; //顺时针 + rasterizer.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE; //逆时针,和opengl一样 rasterizer.depthBiasEnable = VK_FALSE; rasterizer.depthBiasConstantFactor = 0; rasterizer.depthBiasClamp = 0; diff --git a/src/SceneGraph/Camera.cpp b/src/SceneGraph/Camera.cpp index 2c27769f..67a25150 100644 --- a/src/SceneGraph/Camera.cpp +++ b/src/SceneGraph/Camera.cpp @@ -16,9 +16,9 @@ namespace hgl V nup=cross(side,forward); - Matrix4f result( side.x, side.y, side.z, 0.0f, - nup.x, nup.y, nup.z, 0.0f, - -forward.x, -forward.y, -forward.z/2.0f, 0.0f, + Matrix4f result( side.x, side.y, side.z, 1.0f, + nup.x, nup.y, nup.z, 1.0f, + -forward.x, -forward.y, -forward.z/2.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f); // ^^^^^^ // 某些引擎此项为0.5,这个会影响最终输出的z值,但我们这里必须为0 diff --git a/src/SceneGraph/InlineGeometry.cpp b/src/SceneGraph/InlineGeometry.cpp index d506c434..992c5db0 100644 --- a/src/SceneGraph/InlineGeometry.cpp +++ b/src/SceneGraph/InlineGeometry.cpp @@ -417,12 +417,12 @@ namespace hgl for (uint j = 0; j < numberSlices; j++) { *tp= i * (numberSlices + 1) + j; ++tp; - *tp=(i + 1) * (numberSlices + 1) + j; ++tp; *tp=(i + 1) * (numberSlices + 1) + (j + 1); ++tp; + *tp=(i + 1) * (numberSlices + 1) + j; ++tp; *tp= i * (numberSlices + 1) + j; ++tp; - *tp=(i + 1) * (numberSlices + 1) + (j + 1); ++tp; *tp= i * (numberSlices + 1) + (j + 1); ++tp; + *tp=(i + 1) * (numberSlices + 1) + (j + 1); ++tp; } } } @@ -533,9 +533,9 @@ namespace hgl { for (uint j = 0; j < numberSlices + 1; j++) { - float x=sin(angleStep * (double) i) * sin(angleStep * (double) j); - float y=sin(angleStep * (double) i) * cos(angleStep * (double) j); - float z=cos(angleStep * (double) i); + float x= sin(angleStep * (double) i) * sin(angleStep * (double) j); + float y= sin(angleStep * (double) i) * cos(angleStep * (double) j); + float z=-cos(angleStep * (double) i); *vp=x;++vp; *vp=y;++vp; @@ -614,9 +614,9 @@ namespace hgl uint tangentIndex = (i * (dci->numberSlices + 1) + j) * 3; uint texCoordsIndex = (i * (dci->numberSlices + 1) + j) * 2; - float x=dci->radius * sinf(angleStep * (float) i) * sinf(angleStep * (float) j); - float y=dci->radius * sinf(angleStep * (float) i) * cosf(angleStep * (float) j); - float z=dci->radius * cosf(angleStep * (float) i); + float x= dci->radius * sinf(angleStep * (float) i) * sinf(angleStep * (float) j); + float y= dci->radius * sinf(angleStep * (float) i) * cosf(angleStep * (float) j); + float z=-dci->radius * cosf(angleStep * (float) i); *vp=x;++vp; *vp=y;++vp; @@ -678,13 +678,13 @@ namespace hgl // first triangle of the face, counter clock wise winding *tp = v0; ++tp; - *tp = v1; ++tp; *tp = v2; ++tp; + *tp = v1; ++tp; // second triangle of the face, counter clock wise winding *tp = v0; ++tp; - *tp = v2; ++tp; *tp = v3; ++tp; + *tp = v2; ++tp; } } } @@ -751,7 +751,7 @@ namespace hgl // generate vertex and stores it in the right position *vp = (centerRadius + torusRadius * cos2PIt) * cos2PIs; ++vp; - *vp = (centerRadius + torusRadius * cos2PIt) * sin2PIs; ++vp; + *vp =-(centerRadius + torusRadius * cos2PIt) * sin2PIs; ++vp; *vp = torusRadius * sin2PIt; ++vp; if(np) @@ -760,7 +760,7 @@ namespace hgl // NOTE: cos (2PIx) = cos (x) and sin (2PIx) = sin (x) so, we can use this formula // normal = {cos(2PIs)cos(2PIt) , sin(2PIs)cos(2PIt) ,sin(2PIt)} *np = cos2PIs * cos2PIt; ++np; - *np = sin2PIs * cos2PIt; ++np; + *np =-sin2PIs * cos2PIt; ++np; *np = sin2PIt; ++np; } @@ -867,13 +867,13 @@ namespace hgl *vp = 0.0f; ++vp; *vp = 0.0f; ++vp; - *vp = -cci->halfExtend; ++vp; + *vp = cci->halfExtend; ++vp; if(np) { - *np = 0.0f; ++np; - *np = 0.0f; ++np; - *np = -1.0f; ++np; + *np = 0.0f; ++np; + *np = 0.0f; ++np; + *np = 1.0f; ++np; } if(tp) @@ -895,13 +895,13 @@ namespace hgl *vp = cosf(currentAngle) * cci->radius;++vp; *vp = -sinf(currentAngle) * cci->radius;++vp; - *vp = -cci->halfExtend; ++vp; + *vp = cci->halfExtend; ++vp; if(np) { - *np = 0.0f; ++np; - *np = 0.0f; ++np; - *np = -1.0f; ++np; + *np = 0.0f; ++np; + *np = 0.0f; ++np; + *np = 1.0f; ++np; } if(tp) @@ -920,13 +920,13 @@ namespace hgl *vp = 0.0f; ++vp; *vp = 0.0f; ++vp; - *vp = cci->halfExtend; ++vp; + *vp =-cci->halfExtend; ++vp; if(np) { *np = 0.0f; ++np; *np = 0.0f; ++np; - *np = 1.0f; ++np; + *np = -1.0f; ++np; } if(tp) @@ -948,13 +948,13 @@ namespace hgl *vp = cosf(currentAngle) * cci->radius;++vp; *vp = -sinf(currentAngle) * cci->radius;++vp; - *vp = cci->halfExtend; ++vp; + *vp = -cci->halfExtend; ++vp; if(np) { *np = 0.0f; ++np; *np = 0.0f; ++np; - *np = 1.0f; ++np; + *np = -1.0f; ++np; } if(tp) @@ -981,7 +981,7 @@ namespace hgl { *vp = cosf(currentAngle) * cci->radius; ++vp; *vp = -sinf(currentAngle) * cci->radius; ++vp; - *vp = cci->halfExtend * sign; ++vp; + *vp = -cci->halfExtend * sign; ++vp; if(np) { @@ -1028,8 +1028,8 @@ namespace hgl for (i = 0; i < numberSlices; i++) { *tp = centerIndex; ++tp; - *tp = indexCounter + 1; ++tp; *tp = indexCounter; ++tp; + *tp = indexCounter + 1; ++tp; indexCounter++; } @@ -1041,12 +1041,12 @@ namespace hgl for (i = 0; i < numberSlices; i++) { *tp = indexCounter; ++tp; - *tp = indexCounter + 1; ++tp; *tp = indexCounter + numberSlices + 1; ++tp; + *tp = indexCounter + 1; ++tp; *tp = indexCounter + 1; ++tp; - *tp = indexCounter + numberSlices + 2; ++tp; *tp = indexCounter + numberSlices + 1; ++tp; + *tp = indexCounter + numberSlices + 2; ++tp; indexCounter++; } @@ -1084,13 +1084,13 @@ namespace hgl *vp = 0.0f; ++vp; *vp = 0.0f; ++vp; - *vp = -cci->halfExtend; ++vp; + *vp = cci->halfExtend; ++vp; if(np) { *np = 0.0f;++np; *np = 0.0f;++np; - *np = -1.0f;++np; + *np = 1.0f;++np; } if(tp) @@ -1112,13 +1112,13 @@ namespace hgl *vp = cosf(currentAngle) * cci->radius;++vp; *vp = -sinf(currentAngle) * cci->radius;++vp; - *vp = -cci->halfExtend; ++vp; + *vp = cci->halfExtend; ++vp; if(np) { *np = 0.0f;++np; *np = 0.0f;++np; - *np = -1.0f;++np; + *np = 1.0f;++np; } if(tp) @@ -1145,13 +1145,13 @@ namespace hgl *vp = cosf(currentAngle) * cci->radius * (1.0f - level); ++vp; *vp = -sinf(currentAngle) * cci->radius * (1.0f - level); ++vp; - *vp = -cci->halfExtend + 2.0f * cci->halfExtend * level; ++vp; + *vp = cci->halfExtend + 2.0f * cci->halfExtend * level; ++vp; if(np) { *np = h / l * cosf(currentAngle); ++np; *np = h / l * -sinf(currentAngle); ++np; - *np = r / l; ++np; + *np =-r / l; ++np; } if(tp)