增加新的ortho/perspective函数

This commit is contained in:
HuYingzhuo 2019-04-23 11:05:40 +08:00
parent 4f47279ed5
commit 215b58de0b
2 changed files with 53 additions and 12 deletions

View File

@ -17,8 +17,7 @@
#include<hgl/math/Math.h>
#include<fstream>
#ifdef WIN32
#else
#ifndef WIN32
#include<unistd.h>
#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);

View File

@ -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)