-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
executable file
·310 lines (268 loc) · 8.06 KB
/
main.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
#include <stdio.h>
#include <string>
#include <iostream>
#include <GL/freeglut.h>
#include <GL/glu.h>
#include "gl_util.h"
#include "material.h"
#include "delta_gl.h"
#include "svector.h"
#include "delta.h"
#include <cmath>
const size_t WindowsWidth = 1024;
const size_t WindowsHeight = 1024;
GLfloat lightPosition0[] = {3000.0, 1000.0, 800.0, 0.0};
GLfloat lightPosition1[] = {-3000.0, -1000.0, 1000.0, 0.0};
GLfloat whiteLight[] = {1.0, 1.0, 1.0, 1.0};
GLfloat lModelAmbient[] = {0.9, 0.9, 0.9, 1.0};
DeltaPrinter printer;
DeltaPrinterGL deltaGL(printer);
int errMat = 0;
class ViewPort {
public:
ViewPort(float x, float y, float w, float h, float fov)
: x(x), y(y), w(w), h(h), fov(fov){
qCamera(0, 0, 0, 1);
mouse_vx = 0;
mouse_vy = 0;
mouse_vz = -1000;
//svector::float4 qNew(0, 0, 0, 1);
//qNew.euler(3.1415 / 2, 0, 0);
//qCamera.quaternion_mult(qNew);
}
void render() {
if (0) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(x, y, w, h);
gluPerspective(fov, w / h, 1, 2000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition0);
glLightfv(GL_LIGHT1, GL_POSITION, lightPosition1);
glTranslatef(mouse_vx, -mouse_vy, mouse_vz);
float pi = 3.14159265358;
svector::float4 qNew(0, 0, 0, 1);
qNew.euler(mouse_dy / 2/h, mouse_dx /2/ w, 0.0);
qCamera.quaternion_mult(qNew);
svector::float4 qAxis = qCamera.axis();
glRotatef(qAxis.w / pi * 180, qAxis.x, qAxis.y, qAxis.z);
glTranslatef(0,0,-400);
glDisable(GL_LIGHTING);
draw_axes_positive(500, 500, 500);
glEnable(GL_LIGHTING);
// deltaGL.render();
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
glDisable(GL_LIGHTING);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(x, y, w, h);
glOrtho(0, w, h, 0, -10, 10);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
float dx = (WindowsWidth - (WindowsHeight - 100) ) / 2.0f;
glTranslatef(dx, 0, 0);
deltaGL.renderTexture(WindowsHeight - 100, WindowsHeight - 100, errMat);
glPopMatrix();
}
void mouse_button(int button, int status, int xm, int ym) {
if (xm < x || xm > x + w || ym < y || ym > y + h) return;
left_button_status = GLUT_UP;
right_button_status = GLUT_UP;
if ((button == 3) || (button == 4)) {
if (status == GLUT_DOWN) {
mouse_vz += button == 3 ? 19 : -19;
if (mouse_vz > 40) mouse_vz = 30;
if (mouse_vz < -2000) mouse_vz = -2000;
}
}
else {
if (button == GLUT_LEFT_BUTTON) {
if (status == GLUT_DOWN) {
left_button_status = GLUT_DOWN;
left_button_down_x = xm;
left_button_down_y = ym;
}
}
if (button == GLUT_RIGHT_BUTTON) {
if (status == GLUT_DOWN) {
right_button_status = GLUT_DOWN;
left_button_down_x = xm;
left_button_down_y = ym;
}
}
}
}
void mouse_active_motion(int xm, int ym) {
if (xm < x || xm > x + w || ym < y || ym > y + h) return;
if (left_button_status == GLUT_DOWN) {
mouse_dx = (xm - left_button_down_x);
mouse_dy = -(ym - left_button_down_y);
if (fabs(mouse_dx) < 2) mouse_dx = 0;
if (fabs(mouse_dy) < 2) mouse_dy = 0;
}
if (right_button_status == GLUT_DOWN) {
float dx = (xm - left_button_down_x) / float(1);
float dy = -(ym - left_button_down_y) / float(1);
mouse_vy += dy;
mouse_vx += dx;
}
left_button_down_x = xm;
left_button_down_y = ym;
}
void mouse_passive_motion(int xm, int ym) {
if (xm < x || xm > x + w || ym < y || ym > y + h) return;
left_button_down_y = ym;
left_button_down_x = xm;
}
private:
float x, y, w, h;
float fov;
svector::float4 qCamera;
int left_button_status;
int right_button_status;
int left_button_down_x;
int left_button_down_y;
float mouse_dx;
float mouse_dy;
float mouse_vx;
float mouse_vy;
float mouse_vz;
};
ViewPort view(0, 0, WindowsWidth, WindowsHeight, 60);
void display() {
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
view.render();
glutSwapBuffers();
}
void init_display() {
glEnable(GL_LINE_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_POLYGON_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glLightfv(GL_LIGHT0, GL_SPECULAR, whiteLight); // sets specular light to white
glLightfv(GL_LIGHT0, GL_DIFFUSE, whiteLight); // sets diffuse light to white
glLightfv(GL_LIGHT1, GL_SPECULAR, whiteLight); // sets specular light to white
glLightfv(GL_LIGHT1, GL_DIFFUSE, whiteLight); // sets diffuse light to white
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lModelAmbient); // global ambient light
glEnable(GL_LIGHTING); // enables lighting
glEnable(GL_LIGHT0); // enables light0
glEnable(GL_LIGHT1); // enables light0
glShadeModel(GL_SMOOTH);
// glEnable(GL_COLOR_MATERIAL); // enables opengl to use glColor3f to define material color
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
}
void reshape(int w, int h) {
init_display();
display();
}
void mouse_wheel(int wheel, int direction, int x, int y) {
int y1 = WindowsHeight - y;
view.mouse_button(direction > 0 ? 3 : 4, GLUT_DOWN, x, y1);
}
void mouse_button(int button, int state, int x, int y) {
int y1 = WindowsHeight - y;
view.mouse_button(button, state, x, y1);
}
void mouse_active_motion(int x, int y) {
int y1 = WindowsHeight - y;
view.mouse_active_motion(x, y1);
}
void mouse_passive_motion(int x, int y) {
int y1 = WindowsHeight - y;
view.mouse_passive_motion(x, y1);
}
DeltaGeometry FKGeo;
DeltaGeometry IKGeo;
void normal_keys(unsigned char key, int x, int y) {
switch (key) {
case 'm':
printer.surfaceMap(IKGeo, FKGeo);
break;
case '1':
errMat = 0;
break;
case '2':
errMat = 1;
break;
case '3':
errMat = 2;
break;
case '4':
errMat = 3;
break;
case '6':
deltaGL.setRenderMode(DeltaPrinterGL::DETAILED);
break;
case '7':
deltaGL.setRenderMode(DeltaPrinterGL::SIMPLIFIED);
break;
case 'a':
//printer.setCarriages(400, 400, 400);
break;
case 'z':
//printer.moveCarriages(-10, 0, 0);
break;
case 's':
//printer.moveCarriages(0, 10, 0);
break;
case 'x':
//printer.moveCarriages(0, -10, 0);
break;
case 'd':
//printer.moveCarriages(0, 0, 10);
break;
case 'c':
printer.calibrate(IKGeo, FKGeo);
printer.surfaceMap(IKGeo, FKGeo);
break;
case 'f':
//printer.moveCarriages(10, 10, 10);
break;
case 'v':
//printer.moveCarriages(-10, -10, -10);
break;
case 't':
//printer.setXYZ(0, 180, 0);
break;
case 'y':
//printer.setXYZ(0, 0, 0);
break;
case 'u':
//printer.setXYZ(0, 0, 30.492798);
break;
case 27:
glutLeaveMainLoop();
break;
default:
break;
}
}
void init_glut_window(int argc, char *argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB);
//glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(0, 0);
glutInitWindowSize(WindowsWidth, WindowsHeight);
glutCreateWindow("Delta Sim");
glutDisplayFunc(display);
glutIdleFunc(display);
glutKeyboardFunc(normal_keys);
//glutSpecialFunc(special_keys);
glutMouseFunc(mouse_button);
glutMotionFunc(mouse_active_motion);
glutPassiveMotionFunc(mouse_passive_motion);
glutMouseWheelFunc(mouse_wheel);
glutReshapeFunc(reshape);
deltaGL.init();
printer.setGeo(FKGeo);
printer.surfaceMap(IKGeo, FKGeo);
glutMainLoop();
}
int main(int argc, char *argv[]) {
init_glut_window(argc, argv);
}