-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenGL.h
123 lines (107 loc) · 2.51 KB
/
OpenGL.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#ifndef _GL_HELPER_
#define _GL_HELPER_
#include <gl/GL.h>
#include <opencv2/core/opengl_interop.hpp>
#include <string>
using std::string;
#pragma comment(lib,"opengl32.lib")
#ifdef _DEBUG
#pragma comment(lib,"opencv_core232d.lib")
#pragma comment(lib,"opencv_highgui232d.lib")
#else
#pragma comment(lib,"opencv_core232.lib")
#pragma comment(lib,"opencv_highgui232.lib")
#endif
namespace cv
{
class I3DRenderer
{
public:
float _near;
float _far;
float _fov;
I3DRenderer(const string& title, int w=800,int h = 600):_window_name(title)
{
namedWindow(title, WINDOW_OPENGL);
resizeWindow(title, w, h);
setOpenGlDrawCallback(title, I3DRenderer::static_draw_cb, this);
setMouseCallback(title, I3DRenderer::static_mouse_cb, this);
_near = 1.0f;
_far = 1000.0f;
_fov = 45.0f;
}
virtual ~I3DRenderer()
{
destroyWindow(_window_name);
}
// enum
// {
// EVENT_MOUSEMOVE =0,
// EVENT_LBUTTONDOWN =1,
// EVENT_RBUTTONDOWN =2,
// EVENT_MBUTTONDOWN =3,
// EVENT_LBUTTONUP =4,
// EVENT_RBUTTONUP =5,
// EVENT_MBUTTONUP =6,
// EVENT_LBUTTONDBLCLK =7,
// EVENT_RBUTTONDBLCLK =8,
// EVENT_MBUTTONDBLCLK =9
// };
virtual void onMouseEvent(int event, int x, int y, int flags){}
virtual void draw() = 0;
void update()
{
updateWindow(_window_name);
}
static void static_mouse_cb(int event, int x, int y, int flags, void* userdata)
{
I3DRenderer* renderer = static_cast<I3DRenderer*>(userdata);
renderer->_calculate_mouse_dxdy(event, x, y, flags);
renderer->onMouseEvent(event, x, y, flags);
}
static void static_draw_cb(void* userdata)
{
I3DRenderer* renderer = static_cast<I3DRenderer*>(userdata);
renderer->_update_camera_projection();
renderer->draw();
}
private:
void _update_camera_projection()
{
double aspect = getWindowProperty(_window_name, WND_PROP_ASPECT_RATIO);
_camera.setPerspectiveProjection(_fov, aspect, _near, _far);
}
void _calculate_mouse_dxdy(int event, int x, int y, int flags)
{
static int oldx = x;
static int oldy = y;
static bool moving = false;
if (event == EVENT_LBUTTONDOWN)
{
oldx = x;
oldy = y;
moving = true;
}
else if (event == EVENT_LBUTTONUP)
{
moving = false;
}
if (moving)
{
_mouse_dx = oldx - x;
_mouse_dy = oldy - y;
}
else
{
_mouse_dx = 0;
_mouse_dy = 0;
}
}
protected:
GlCamera _camera;
string _window_name;
int _mouse_dx;
int _mouse_dy;
};
}
#endif