-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp_common.cpp
345 lines (262 loc) · 7.56 KB
/
app_common.cpp
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include "app_common.h"
#include <windows.h>
GLfloat viewZ;
GLfloat mvmat[16];
unsigned int app_screen_width = 960;
unsigned int app_screen_height = 720;
void drawAxis(double length)
{
GLUquadricObj *arrows[3];
glLineWidth(3.0);
// Draw X-axis
glColor3ub(255, 0, 0);
glBegin(GL_LINES);
glVertex3d(-length, 0, 0);
glVertex3d(length, 0, 0);
glEnd();
glPushMatrix();
arrows[0] = gluNewQuadric();
gluQuadricDrawStyle(arrows[0], GLU_FILL);
glTranslated(length, 0.0f, 0.0f);
glRotated(90.0f, 0, 1, 0);
gluCylinder(arrows[0], length / 10, 0.0f, length / 2.5, 8, 8);
glPopMatrix();
// Draw Y-axis
glColor3ub(0, 255, 0);
glBegin(GL_LINES);
glVertex3d(0, -length, 0);
glVertex3d(0, length, 0);
glEnd();
glPushMatrix();
arrows[1] = gluNewQuadric();
gluQuadricDrawStyle(arrows[1], GLU_FILL);
glTranslated(0.0f, length, 0.0f);
glRotated(-90.0f, 1, 0, 0);
gluCylinder(arrows[1], length / 10, 0.0f, length / 2.5, 8, 8);
glPopMatrix();
// Draw Z-axis
glColor3ub(0, 0, 255);
glBegin(GL_LINES);
glVertex3d(0, 0, -length);
glVertex3d(0, 0, length);
glEnd();
glPushMatrix();
arrows[2] = gluNewQuadric();
gluQuadricDrawStyle(arrows[2], GLU_FILL);
glTranslated(0.0f, 0.0f, length);
gluCylinder(arrows[2], length / 10, 0.0f, length / 2.5, 8, 8);
glPopMatrix();
}
void drawROI(ROI roi)
{
glLineWidth(2.0);
Eigen::Vector3f roi_center, roi_size;
roi_center << roi.x.mean(), roi.y.mean(), roi.z.mean();
roi_size << roi.x[1] - roi.x[0], roi.y[1] - roi.y[0], roi.z[1] - roi.z[0];
glDisable(GL_LIGHTING);
glLineWidth(2.0);
glColor3ub(100, 100, 100);
glPushMatrix();
glTranslatef(roi_center.x(), roi_center.y(), roi_center.z());
glScalef(roi_size.x(), roi_size.y(), roi_size.z());
glutWireCube(1.0);
glPopMatrix();
}
void setView(bool sel, int mx, int my)
{
int w, h;
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
/* ウィンドウ全体をビューポートにする */
w = glutGet(GLUT_WINDOW_WIDTH);
h = glutGet(GLUT_WINDOW_HEIGHT);
glViewport(0, 0, w, h);
GLint vp[4];
glGetIntegerv(GL_VIEWPORT, vp);
/* 透視変換行列の指定 */
glMatrixMode(GL_PROJECTION);
/* 透視変換行列の初期化 */
glLoadIdentity();
if (sel) gluPickMatrix(mx, vp[3] - my, 1, 1, vp);
gluPerspective(50.0, (double)w / (double)h, 0.1, 1000.0);
/* モデルビュー変換行列の指定 */
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* モデルビュー変換行列の初期化 */
glLoadIdentity();
/* 視点の移動 */
gluLookAt(0.0, 0.0, viewZ, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
GLfloat lpos[4] = { 0.0, 0.0, -10.0, 0.0 };
/* 光源の位置を設定 */
glLightfv(GL_LIGHT0, GL_POSITION, lpos);
glMultMatrixf(mvmat);
}
void drawScene()
{
setView(false, 0, 0);
//app specific display func
displayApp();
ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());
glutSwapBuffers();
}
void mouseDragCallback(int x, int y)
{
ImGuiIO& io = ImGui::GetIO();
float x_pre = io.MousePos.x;
float y_pre = io.MousePos.y;
io.MousePos = ImVec2((float)x, (float)y);
if (io.MouseDown[1] && !io.WantCaptureMouse) //right btn
{
if (glutGetModifiers() & GLUT_ACTIVE_SHIFT)
{
double dx, dy;
dx = (io.MousePos.x - x_pre) / (float)(io.DisplaySize.x) * 10.0 * fabs(viewZ) / 10.0;
dy = (io.MousePos.y - y_pre) / (float)(io.DisplaySize.y) * 10.0 * fabs(viewZ) / 10.0;
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glTranslatef(-dx, -dy, 0.0);
glMultMatrixf(mvmat);
glGetFloatv(GL_MODELVIEW_MATRIX, mvmat);
glPopMatrix();
}
else if (glutGetModifiers() & GLUT_ACTIVE_CTRL)
{
double dx, dy, a;
dx = (io.MousePos.x - x_pre) / (float)(io.DisplaySize.x) * 2.0;
dy = (io.MousePos.y - y_pre) / (float)(io.DisplaySize.y) * 2.0;
a = dx + dy;
viewZ -= viewZ*a;
}
else
{
float dx, dy;
dx = (io.MousePos.x - x_pre) / (float)(io.DisplaySize.x) * 6.28 * 50.0;
dy = -(io.MousePos.y - y_pre) / (float)(io.DisplaySize.y) * 6.28 * 50.0;
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glRotatef(dx, 0.0, 1.0, 0.0);
glRotatef(dy, 1.0, 0.0, 0.0);
glMultMatrixf(mvmat);
glGetFloatv(GL_MODELVIEW_MATRIX, mvmat);
glPopMatrix();
}
}
io.KeyCtrl = glutGetModifiers() & GLUT_ACTIVE_CTRL;
io.KeyShift = glutGetModifiers() & GLUT_ACTIVE_SHIFT;
}
void mainLoop(void)
{
//app specific loop func
loopApp();
}
static void guiSetClipboardText(void* user_data, const char* text)
{
int buf_size;
char *buf;
HANDLE h_mem;
buf_size = strlen(text) + 1;
h_mem = GlobalAlloc(GMEM_SHARE | GMEM_MOVEABLE, buf_size);
if (!h_mem) return;
buf = (char *)GlobalLock(h_mem);
if (buf)
{
strcpy_s(buf, buf_size, text);
GlobalUnlock(h_mem);
if (OpenClipboard(NULL))
{
EmptyClipboard();
SetClipboardData(CF_TEXT, h_mem);
CloseClipboard();
}
}
}
static const char* guiGetClipboardText(void* user_data)
{
HANDLE h_mem;
PTSTR str_clip;
static std::string text;
if (OpenClipboard(NULL) && (h_mem = GetClipboardData(CF_TEXT)))
{
str_clip = static_cast<PTSTR>(GlobalLock(h_mem));
text = std::string((char*)str_clip);
GlobalUnlock(h_mem);
CloseClipboard();
}
return text.c_str();
}
void startApp(int argc, char **argv, const char* win_title)
{
glutInit(&argc, argv);
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE);
glutInitWindowSize(app_screen_width, app_screen_height);
glutInitWindowPosition(200, 200);
glutCreateWindow(win_title);
// callback
glutMotionFunc(mouseDragCallback);
glutPassiveMotionFunc(mouseDragCallback);
glutDisplayFunc(drawScene);
glutIdleFunc(mainLoop);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glGetFloatv(GL_MODELVIEW_MATRIX, mvmat);
viewZ = -5.0;
glClearColor(0.447f, 0.565f, 0.604f, 1.0f);
// Setup ImGui binding
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
//io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
ImGui_ImplFreeGLUT_Init();
ImGui_ImplFreeGLUT_InstallFuncs();
ImGui_ImplOpenGL2_Init();
//register clipboard functions
io.SetClipboardTextFn = guiSetClipboardText;
io.GetClipboardTextFn = guiGetClipboardText;
io.ClipboardUserData = NULL;
// Setup style
//ImGui::StyleColorsDark();
ImGui::StyleColorsClassic();
// load font
io.Fonts->AddFontDefault();
auto roboto = io.Fonts->AddFontFromMemoryCompressedTTF(font_roboto_compressed_data, font_roboto_compressed_size, 16.0f);
io.FontDefault = roboto;
//no imgui.ini file saving
io.IniFilename = NULL;
//app specific initialization
initApp();
glutMainLoop();
// Cleanup
ImGui_ImplOpenGL2_Shutdown();
ImGui_ImplFreeGLUT_Shutdown();
ImGui::DestroyContext();
}
void resetView(float x1, float y1, float z1, float x2, float y2, float z2, float roll)
{
float d;
//look at (x2, y2, z2) from (x1, y1, z1) (in complicated manner for compatibility with manual view changing)
Eigen::Vector3f v;
Eigen::Vector3f P1(x1, y1, z1);
Eigen::Vector3f P2(x2, y2, z2);
v = P2 - P1;
d = v.norm();
v.normalize();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glGetFloatv(GL_MODELVIEW_MATRIX, mvmat);
viewZ = -d;
v.normalize();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glRotatef(roll / M_PI*180.0, 0.0, 0.0, 1.0);
glRotatef(asin(v.y()) / M_PI*180.0, 1.0, 0.0, 0.0);
if (v.x() < 0) glRotatef(acos(v.z()/sqrt(v.x()*v.x() + v.z()*v.z()) ) / M_PI*180.0, 0.0, 1.0, 0.0);
else glRotatef(-acos(v.z() / sqrt(v.x()*v.x() + v.z()*v.z())) / M_PI*180.0, 0.0, 1.0, 0.0);
glTranslatef(-(x1+v.x()*d), -(y1 + v.y()*d), -(z1 + v.z()*d));
glGetFloatv(GL_MODELVIEW_MATRIX, mvmat);
glPopMatrix();
}