Easy3D 2.5.3
picker.h
1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
27#ifndef EASY3D_GUI_PICKER_H
28#define EASY3D_GUI_PICKER_H
29
30#include <easy3d/core/types.h>
31#include <easy3d/renderer/camera.h>
32
33
34namespace easy3d {
35
36 class FramebufferObject;
37
43 class Picker {
44 public:
45 explicit Picker(const Camera *cam);
46
47 virtual ~Picker();
48
50 const Camera *camera() const { return camera_; }
51
60 Line3 picking_line(int x, int y) const {
61 const vec3 p_near = unproject(x, y, 0);
62 const vec3 p_far = unproject(x, y, 1);
63 return Line3::from_two_points(p_near, p_far);
64 }
65
74 vec3 picking_dir(int x, int y) const {
75 return picking_line(x, y).direction();
76 }
77
88 vec3 project(const vec3 &p) const {
89 return camera()->projectedCoordinatesOf(p);
90 }
91
102 vec3 unproject(int x, int y, float depth) const {
103 return camera()->unprojectedCoordinatesOf(vec3(static_cast<float>(x), static_cast<float>(y), depth));
104 }
105
117 void screen_to_opengl(int x, int y, int &gl_x, int &gl_y, int width, int height) const;
118
119 protected:
120
121 // prepare a frame buffer for the offscreen rendering
122 void setup_framebuffer(int width, int height);
123
124 protected:
125 const Camera *camera_;
126
127 bool use_gpu_if_supported_; // use GPU if supported
128
129 // All the picking tasks can share the same framebuffer
130 static FramebufferObject *fbo_;
131 };
132
133}
134
135#endif // EASY3D_GUI_PICKER_H
A perspective or orthographic camera.
Definition: camera.h:116
vec3 projectedCoordinatesOf(const vec3 &src, const Frame *frame=nullptr) const
Definition: camera.cpp:1255
vec3 unprojectedCoordinatesOf(const vec3 &src, const Frame *frame=nullptr) const
Definition: camera.cpp:1305
An implementation of framebuffer object (FBO).
Definition: framebuffer_object.h:122
A generic line representation, which supports both 2D and 3D lines.
Definition: line.h:40
static GenericLine from_two_points(const Point &p, const Point &q)
Constructs a line from two points p and q.
Definition: line.h:50
const Vector & direction() const
Returns the direction of a line.
Definition: line.h:62
Base class for picking mechanism.
Definition: picker.h:43
void screen_to_opengl(int x, int y, int &gl_x, int &gl_y, int width, int height) const
Convert a point expressed in the screen coordinate system (with an origin in the upper left corner) i...
Definition: picker.cpp:71
const Camera * camera() const
Returns the pointer of the camera.
Definition: picker.h:50
Line3 picking_line(int x, int y) const
Construct a picking line.
Definition: picker.h:60
vec3 project(const vec3 &p) const
Project a 3D point in the world coordinate system onto the 2D screen coordinate system.
Definition: picker.h:88
vec3 picking_dir(int x, int y) const
The picking direction, pointing inside the screen.
Definition: picker.h:74
vec3 unproject(int x, int y, float depth) const
Compute the world coordinates of a point defined in the screen coordinate system.
Definition: picker.h:102
Definition: collider.cpp:182
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition: types.h:45