3D Graphics/libigl

libigl에서 Keyboard, Mouse Picking 이벤트 처리

leonhong 2023. 5. 5. 14:42

이벤트 처리는 Viewer 단계에서 처리함

 이벤트 함수가 미리 정의되어 있는데, 해당 함수를 교체하는 방식으로 사용.

 아래의 예시는 Mesh위에서 마우스 클릭했을때, 클릭한 위치의 Face ID와 Vertex ID를 알려주는 구문 

 

#include <igl/unproject_onto_mesh.h>

bool mouse_down(igl::opengl::glfw::Viewer& viewer, int button, int modifier)
{
	// 클릭한 메시의 Face ID와 가장 가까운 Vertex ID 알려주는 구문
	int fid;
	Eigen::Vector3f bc;

	double x = viewer.current_mouse_x;
	double y = viewer.core().viewport(3) - viewer.current_mouse_y;

	if (igl::unproject_onto_mesh(
		Eigen::Vector2f(x, y),
		viewer.core().view,
		viewer.core().proj,
		viewer.core().viewport,
		viewer.data().V,
		viewer.data().F,
		fid,
		bc))
	{
		int max;
		bc.maxCoeff(&max);
		int vid = viewer.data().F(fid, max);

		printf("Picked Face id : %d,  Vertex id : %d\n", fid, vid);

		return true;
	}
	return false;
}

int main()
{
......
	igl::opengl::glfw::Viewer viewer;
	viewer.callback_mouse_down = &mouse_down;
......
}

 

이외에도 키보드 마우스에 대한 이벤트 처리 방법들이 있는데 아래 튜토리얼의 Interaction With Keyboard And Mouse 부분을 참고해서 사용하면 된다.

https://libigl.github.io/tutorial/