From dd6b45294a7bfa09daa0e50aad0e33aa8193e0e9 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Mon, 27 May 2019 22:45:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E7=9A=84Camera=E4=B8=8EFrustum?= =?UTF-8?q?=E5=85=B3=E7=B3=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/hgl/graph/Camera.h | 23 +++++++++++++++++++---- inc/hgl/math/Vector.h | 2 ++ res/shader/shader_compile.sh | 1 + src/SceneGraph/Camera.cpp | 19 ++++++++----------- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/inc/hgl/graph/Camera.h b/inc/hgl/graph/Camera.h index ef1aa138..aa53bcea 100644 --- a/inc/hgl/graph/Camera.h +++ b/inc/hgl/graph/Camera.h @@ -6,11 +6,19 @@ namespace hgl { namespace graph { + enum class CameraType + { + Perspective, + Ortho + };// + /** * 摄像机数据结构 */ struct Camera { + CameraType type=CameraType::Perspective; + float width; ///<视图宽 float height; ///<视图高 @@ -21,12 +29,19 @@ namespace hgl Vector3f center; ///<视点坐标 Vector3f up_vector; ///<向上量 Vector3f forward_vector; ///<向前量 + + public: + + Matrix4f projection; + Matrix4f modelview; + + Frustum frustum; + + public: + + void Refresh(); };//struct Camera - void CameraToFrustum(Frustum *,const Camera *); - - void MakeCameraMatrix(Matrix4f *proj,Matrix4f *mv,const Camera *cam); - /** * 行走模拟像机 */ diff --git a/inc/hgl/math/Vector.h b/inc/hgl/math/Vector.h index e80208ec..6ad98549 100644 --- a/inc/hgl/math/Vector.h +++ b/inc/hgl/math/Vector.h @@ -260,6 +260,7 @@ namespace hgl { struct { T x,y; }; struct { T r,g; }; + struct { T u,v; }; T data[2]; public: @@ -285,6 +286,7 @@ namespace hgl { struct { T x,y,z; }; struct { T r,g,b; }; + struct { T u,v,w; }; T data[3]; public: diff --git a/res/shader/shader_compile.sh b/res/shader/shader_compile.sh index 68909989..7bece093 100755 --- a/res/shader/shader_compile.sh +++ b/res/shader/shader_compile.sh @@ -1,5 +1,6 @@ glslangValidator -V -o FlatColor.vert.spv FlatColor.vert glslangValidator -V -o OnlyPosition.vert.spv OnlyPosition.vert +glslangValidator -V -o OnlyPosition3D.vert.spv OnlyPosition3D.vert glslangValidator -V -o FlatColor.frag.spv FlatColor.frag glslangValidator -V -o FlatTexture.vert.spv FlatTexture.vert diff --git a/src/SceneGraph/Camera.cpp b/src/SceneGraph/Camera.cpp index 14f5dcc8..1b872c77 100644 --- a/src/SceneGraph/Camera.cpp +++ b/src/SceneGraph/Camera.cpp @@ -23,20 +23,17 @@ namespace hgl return result*translate(-eye); } - void CameraToFrustum(Frustum *f,const Camera *cam) + void Camera::Refresh() { - if(!f||!cam)return; + if(type==CameraType::Perspective) + projection=perspective(width/height,fov,znear,zfar); + else + projection=ortho(width,height,znear,zfar); - f->SetVerticalFovAndAspectRatio(DegToRad(cam->fov),cam->width/cam->height); - f->SetViewPlaneDistances(cam->znear,cam->zfar); + modelview=hgl::graph::LookAt(eye,center,up_vector); - //Matrix4f projection_matrix=f->ProjectionMatrix(); //可以用Frustum来算projection matrix - } - - void MakeCameraMatrix(Matrix4f *proj,Matrix4f *mv,const Camera *cam) - { - *proj=perspective(cam->width/cam->height,cam->fov,cam->znear,cam->zfar); - *mv=hgl::graph::LookAt(cam->eye,cam->center,cam->up_vector); + frustum.SetVerticalFovAndAspectRatio(DegToRad(fov),width/height); + frustum.SetViewPlaneDistances(znear,zfar); } }//namespace graph }//namespace hgl