2024-07-01 23:31:18 +08:00
|
|
|
|
#include"VulkanAppFramework.h"
|
|
|
|
|
#include"Gizmo.h"
|
2024-08-01 01:42:21 +08:00
|
|
|
|
#include<hgl/graph/Ray.h>
|
2024-07-01 23:31:18 +08:00
|
|
|
|
|
|
|
|
|
using namespace hgl;
|
|
|
|
|
using namespace hgl::graph;
|
|
|
|
|
|
2024-08-04 22:35:31 +08:00
|
|
|
|
const Vector3f GizmoPosition(0,0,0);
|
|
|
|
|
|
2024-08-01 01:42:21 +08:00
|
|
|
|
/**
|
2024-08-08 01:43:20 +08:00
|
|
|
|
* 一种永远转向正面的场景节点
|
2024-08-01 01:42:21 +08:00
|
|
|
|
*/
|
2024-08-08 01:43:20 +08:00
|
|
|
|
class BillboardSceneNode:public SceneNode
|
2024-08-01 01:42:21 +08:00
|
|
|
|
{
|
2024-08-08 01:43:20 +08:00
|
|
|
|
CameraInfo *camera_info=nullptr;
|
|
|
|
|
bool face_to_camera=false;
|
2024-08-01 01:42:21 +08:00
|
|
|
|
|
2024-08-08 01:43:20 +08:00
|
|
|
|
ViewportInfo *viewport_info=nullptr;
|
|
|
|
|
float fixed_scale=1.0;
|
2024-08-01 01:42:21 +08:00
|
|
|
|
|
2024-08-08 01:43:20 +08:00
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
using SceneNode::SceneNode;
|
|
|
|
|
virtual ~BillboardSceneNode()=default;
|
|
|
|
|
|
|
|
|
|
virtual void SetCameraInfo (CameraInfo * ci ){camera_info =ci;}
|
|
|
|
|
virtual void SetViewportInfo(ViewportInfo * vi ){viewport_info =vi;}
|
|
|
|
|
|
|
|
|
|
virtual void SetFaceToCamera(bool ftc ){face_to_camera=ftc;}
|
|
|
|
|
virtual void SetFixedScale (const float size){fixed_scale =size;}
|
|
|
|
|
|
|
|
|
|
virtual bool RefreshTransform(const Transform &tf=IdentityTransform) override
|
|
|
|
|
{
|
|
|
|
|
if(!camera_info)
|
|
|
|
|
{
|
|
|
|
|
return SceneNode::RefreshTransform(tf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(face_to_camera)
|
|
|
|
|
{
|
|
|
|
|
LocalTransform.SetRotation(CalculateFacingRotationQuat(GetWorldPosition(),camera_info->view,AxisVector::X));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(viewport_info)
|
|
|
|
|
{
|
|
|
|
|
const float screen_height=viewport_info->GetViewportHeight();
|
|
|
|
|
|
|
|
|
|
const Vector4f pos=camera_info->Project(GetWorldPosition());
|
|
|
|
|
|
|
|
|
|
LocalTransform.SetScale(pos.w*fixed_scale/screen_height);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return SceneNode::RefreshTransform(tf);
|
|
|
|
|
}
|
|
|
|
|
};//class BillboardSceneNode:public SceneNode
|
2024-08-01 01:42:21 +08:00
|
|
|
|
|
2024-07-01 23:31:18 +08:00
|
|
|
|
class TestApp:public SceneAppFramework
|
|
|
|
|
{
|
2024-07-31 00:11:24 +08:00
|
|
|
|
SceneNode root;
|
2024-08-08 01:43:20 +08:00
|
|
|
|
BillboardSceneNode *rotate_white_torus=nullptr;
|
2024-07-31 00:11:24 +08:00
|
|
|
|
|
2024-07-30 23:54:25 +08:00
|
|
|
|
StaticMesh *sm_move=nullptr;
|
|
|
|
|
StaticMesh *sm_rotate=nullptr;
|
2024-07-29 14:01:10 +08:00
|
|
|
|
|
2024-08-01 01:42:21 +08:00
|
|
|
|
Renderable *face_torus=nullptr;
|
|
|
|
|
|
2024-07-01 23:31:18 +08:00
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
bool InitGizmo()
|
|
|
|
|
{
|
2024-07-30 00:47:28 +08:00
|
|
|
|
if(!InitGizmoResource(db))
|
2024-07-01 23:31:18 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2024-07-30 23:54:25 +08:00
|
|
|
|
sm_move =GetGizmoMoveStaticMesh();
|
|
|
|
|
sm_rotate =GetGizmoRotateStaticMesh();
|
2024-07-29 14:01:10 +08:00
|
|
|
|
|
2024-08-01 01:42:21 +08:00
|
|
|
|
face_torus =GetGizmoRenderable(GizmoShape::Torus,GizmoColor::White);
|
|
|
|
|
|
2024-07-01 23:31:18 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-04 22:35:31 +08:00
|
|
|
|
void InitGizmoSceneTree()
|
|
|
|
|
{
|
|
|
|
|
camera_control->Refresh();
|
2024-08-08 01:43:20 +08:00
|
|
|
|
CameraInfo *ci=camera_control->GetCameraInfo();
|
2024-08-04 22:35:31 +08:00
|
|
|
|
|
|
|
|
|
root.Clear();
|
|
|
|
|
|
|
|
|
|
root.CreateSubNode(sm_move->GetScene());
|
|
|
|
|
root.CreateSubNode(sm_rotate->GetScene());
|
|
|
|
|
|
2024-08-08 01:43:20 +08:00
|
|
|
|
{
|
|
|
|
|
Transform tm;
|
|
|
|
|
|
|
|
|
|
tm.SetScale(7.5);
|
|
|
|
|
|
|
|
|
|
rotate_white_torus=new BillboardSceneNode(tm,face_torus);
|
|
|
|
|
|
|
|
|
|
rotate_white_torus->SetCameraInfo(ci);
|
|
|
|
|
rotate_white_torus->SetFaceToCamera(true);
|
|
|
|
|
|
|
|
|
|
root.AddSubNode(rotate_white_torus);
|
|
|
|
|
}
|
2024-08-04 22:35:31 +08:00
|
|
|
|
|
|
|
|
|
root.RefreshTransform();
|
2024-08-08 01:43:20 +08:00
|
|
|
|
render_list->SetCamera(ci);
|
2024-08-04 22:35:31 +08:00
|
|
|
|
render_list->Expend(&root);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-01 23:31:18 +08:00
|
|
|
|
public:
|
2024-08-08 01:43:20 +08:00
|
|
|
|
|
2024-07-01 23:31:18 +08:00
|
|
|
|
bool Init(uint w,uint h)
|
|
|
|
|
{
|
|
|
|
|
if(!SceneAppFramework::Init(w,h))
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitGizmo())
|
|
|
|
|
return(false);
|
2024-08-04 22:35:31 +08:00
|
|
|
|
|
2024-08-08 01:43:20 +08:00
|
|
|
|
InitGizmoSceneTree();
|
2024-07-29 14:01:10 +08:00
|
|
|
|
|
|
|
|
|
camera->pos=Vector3f(32,32,32);
|
|
|
|
|
camera_control->SetTarget(Vector3f(0,0,0));
|
2024-08-01 01:42:21 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~TestApp()
|
|
|
|
|
{
|
|
|
|
|
FreeGizmoResource();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BuildCommandBuffer(uint32 index) override
|
|
|
|
|
{
|
2024-07-29 14:01:10 +08:00
|
|
|
|
camera_control->Refresh();
|
2024-08-01 01:42:21 +08:00
|
|
|
|
|
2024-08-08 01:43:20 +08:00
|
|
|
|
const CameraInfo *ci=camera_control->GetCameraInfo();
|
|
|
|
|
const ViewportInfo *vi=GetViewportInfo();
|
2024-08-01 01:42:21 +08:00
|
|
|
|
|
2024-08-08 01:43:20 +08:00
|
|
|
|
const float screen_height=vi->GetViewportHeight();
|
2024-08-01 01:42:21 +08:00
|
|
|
|
|
2024-08-08 01:43:20 +08:00
|
|
|
|
const Vector4f pos=ci->Project(GizmoPosition);
|
2024-08-01 01:42:21 +08:00
|
|
|
|
|
2024-08-08 01:43:20 +08:00
|
|
|
|
//{
|
|
|
|
|
// Transform tm;
|
2024-08-01 01:42:21 +08:00
|
|
|
|
|
2024-08-08 01:43:20 +08:00
|
|
|
|
// tm.SetScale(pos.w*16.0f/screen_height);
|
2024-08-01 01:42:21 +08:00
|
|
|
|
|
2024-08-08 01:43:20 +08:00
|
|
|
|
// root.SetLocalTransform(tm);
|
|
|
|
|
//}
|
2024-08-04 22:35:31 +08:00
|
|
|
|
|
|
|
|
|
root.RefreshTransform();
|
2024-08-27 01:27:53 +08:00
|
|
|
|
render_list->UpdateLocalToWorld();
|
2024-07-01 23:31:18 +08:00
|
|
|
|
|
2024-08-01 01:42:21 +08:00
|
|
|
|
SceneAppFramework::BuildCommandBuffer(index);
|
2024-07-01 23:31:18 +08:00
|
|
|
|
}
|
|
|
|
|
};//class TestApp:public SceneAppFramework
|
|
|
|
|
|
|
|
|
|
int main(int,char **)
|
|
|
|
|
{
|
2024-07-31 00:11:24 +08:00
|
|
|
|
return RunApp<TestApp>(1024,1024);
|
2024-07-01 23:31:18 +08:00
|
|
|
|
}
|