diff --git a/example/Vulkan/main.cpp b/example/Vulkan/main.cpp index c991b8c7..b8835e93 100644 --- a/example/Vulkan/main.cpp +++ b/example/Vulkan/main.cpp @@ -17,8 +17,7 @@ #include #include -#ifdef WIN32 -#else +#ifndef WIN32 #include #endif// @@ -88,7 +87,7 @@ vulkan::Buffer *CreateUBO(vulkan::Device *dev,vulkan::Shader *shader) { const VkExtent2D extent=dev->GetExtent(); - ubo_vs.mvp=ortho2d(extent.width,extent.height); + ubo_vs.mvp=ortho(extent.width,extent.height); } vulkan::Buffer *ubo=dev->CreateUBO(sizeof(ubo_vs)); @@ -106,9 +105,9 @@ vulkan::Buffer *CreateUBO(vulkan::Device *dev,vulkan::Shader *shader) constexpr float vertex_data[]= { - SCREEN_WIDTH/2,SCREEN_HEIGHT/4, - SCREEN_WIDTH*3/4,SCREEN_HEIGHT*3/4, - SCREEN_WIDTH/4,SCREEN_HEIGHT*3/4 + SCREEN_WIDTH*0.5, SCREEN_HEIGHT*0.25, + SCREEN_WIDTH*0.75, SCREEN_HEIGHT*0.75, + SCREEN_WIDTH*0.25, SCREEN_HEIGHT*0.75 }; constexpr float color_data[]={1,0,0, 0,1,0, 0,0,1 }; @@ -214,6 +213,10 @@ int main(int,char **) vulkan::PipelineLayout *pl=CreatePipelineLayout(*device,dsl); + pc.SetDepthTest(false); + pc.SetDepthWrite(false); + pc.CloseCullFace(); + pc.Set(shader); pc.Set(vi); pc.Set(PRIM_TRIANGLES); diff --git a/inc/hgl/math/MathMGL.h b/inc/hgl/math/MathMGL.h index df53c5e2..232d4ad5 100644 --- a/inc/hgl/math/MathMGL.h +++ b/inc/hgl/math/MathMGL.h @@ -119,21 +119,59 @@ namespace hgl return m.Inverted(); } + inline Matrix4f ortho( float left_plane, + float right_plane, + float bottom_plane, + float top_plane, + float near_plane, + float far_plane ) + { + Matrix4f orthographic_projection_matrix = + { + 2.0f / (right_plane - left_plane),0.0f,0.0f,-(right_plane + left_plane) / (right_plane - left_plane), + 0.0f,2.0f / (bottom_plane - top_plane),0.0f,-(bottom_plane + top_plane) / (bottom_plane - top_plane), + 0.0f,0.0f,1.0f / (near_plane - far_plane),near_plane / (near_plane - far_plane), + 0.0f,0.0f,0.0f,1.0f + }; + + return orthographic_projection_matrix; + } /** * 生成一个2D正角视图矩阵 * @param width 宽 * @param height 高 - * @param top_to_bottom 从上到下(是否最顶部y轴为0,默认否) * @param znear 近平面z值 * @param zfar 远平台z值 */ - inline Matrix4f ortho2d(float width,float height,bool top_to_bottom=false,float znear=0,float zfar=1) + inline Matrix4f ortho(float width,float height,float znear=0,float zfar=1) { - //MathGeoLib生成的2D正交矩阵中心是0,0,所以需要偏移 + Matrix4f orthographic_projection_matrix = + { + 2.0f / width, 0.0f, 0.0f, -1, + 0.0f, 2.0f / height, 0.0f, -1, + 0.0f, 0.0f, 1.0f / (znear - zfar), znear / (znear - zfar), + 0.0f, 0.0f, 0.0f, 1.0f + }; - return Matrix4f::OpenGLOrthoProjRH(znear,zfar,width,height) - *Matrix4f::Scale(1,top_to_bottom?-1:1,1) - *Matrix4f::Translate(-(width/2.0f),-(height/2.0f),0); + return orthographic_projection_matrix; + } + + inline Matrix4f perspective(float aspect_ratio, + float field_of_view, + float near_plane, + float far_plane ) + { + const float f = 1.0f / tan( hgl_ang2rad( 0.5f * field_of_view ) ); + + Matrix4f perspective_projection_matrix = + { + f / aspect_ratio, 0.0f, 0.0f, 0.0f, + 0.0f, -f, 0.0f, 0.0f, + 0.0f, 0.0f, far_plane / (near_plane - far_plane), (near_plane * far_plane) / (near_plane - far_plane), + 0.0f, 0.0f, -1.0f, 0.0f + }; + + return perspective_projection_matrix; } inline Matrix4f translate(const Vector3f &v)