-
Notifications
You must be signed in to change notification settings - Fork 0
/
zpr.cpp
executable file
·411 lines (339 loc) · 10.4 KB
/
zpr.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
* Zoom-pan-rotate mouse manipulation module for GLUT
* Version 0.4, October 2003
*
* Nigel Stewart
* School of Computer Science and Information Technology
* RMIT University
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <math.h>
#include "zpr.h"
using namespace myglut;
void zpr::zprSetWindowID(int w){
windowid = w;
}
void zpr::zprInit(int WindowID){
pick = NULL;
selection = NULL;
zprReferencePoint[0] = zprReferencePoint[1] = zprReferencePoint[2] = zprReferencePoint[3] = 0.;
printf("glutSetWindow(%d)\n", WindowID);
glutSetWindow(WindowID);
getMatrix();
glutReshapeFunc(zprReshape);
glutMouseFunc(zprMouse);
glutMotionFunc(zprMotion);
}
static void zpr::zprReshape(int w,int h){
/*
GLfloat ratio; // http://faculty.ycp.edu/~dbabcock/cs370/labs/lab07.html
glViewport(0,0,w,h); // set new screen extents
glMatrixMode(GL_PROJECTION); // select projection matrix
glLoadIdentity();
if(w <= h){
ratio = ((GLfloat) h)/((GLfloat) w);
glOrtho(-1,1,-ratio,ratio,-1,1);
_bottom = - ratio;
_top = ratio;
}
else if (h <= w){
ratio = (GLfloat) w/ (GLfloat) h;
glOrtho(-ratio,ratio,-1,1,-1,1);
_left = -ratio;
_right = ratio;
}
WINDOWX = glutGet( GLUT_WINDOW_WIDTH );
WINDOWY = glutGet( GLUT_WINDOW_HEIGHT );
glMatrixMode(GL_MODELVIEW);
return; //http://graphics.stanford.edu/courses/cs248-01/OpenGLHelpSession/code_example.html
*/
glutSetWindow(windowid);
glViewport(0,0,w,h);
_top = 1.0;
_bottom = -1.0;
_left = -(double)w/(double)h;
_right = -_left;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(_left,_right,_bottom,_top,_zNear,_zFar);
glMatrixMode(GL_MODELVIEW);
WINDOWX = glutGet( GLUT_WINDOW_WIDTH);
WINDOWY = glutGet( GLUT_WINDOW_HEIGHT );
}
static void zpr::zprMouse(int button, int state, int x, int y){
glutSetWindow(windowid);
GLint viewport[4];
/* Do picking */
if(state==GLUT_DOWN){
if(button == GLUT_RIGHT_BUTTON){
_lastmouseRight = true;
}
else{
_lastmouseRight=false;
}
zprPick(x,glutGet(GLUT_WINDOW_HEIGHT) - 1 - y, 3, 3);
}
_mouseX = x;
_mouseY = y;
if(state==GLUT_UP)
switch(button){
case GLUT_LEFT_BUTTON: _mouseLeft = false; break;
case GLUT_MIDDLE_BUTTON: _mouseMiddle = false; break;
case GLUT_RIGHT_BUTTON: _mouseRight = false; break;
}
else
switch(button){
case GLUT_LEFT_BUTTON:
_mouseLeft = true;
// printf(" x: %d y: %d\n", x, y);
break;
case GLUT_MIDDLE_BUTTON: _mouseMiddle = true; break;
case GLUT_RIGHT_BUTTON: _mouseRight = true; break;
}
glGetIntegerv(GL_VIEWPORT,viewport);
pos(&_dragPosX,&_dragPosY,&_dragPosZ,x,y,viewport);
glutPostRedisplay();
}
static void zpr::zprMotion(int x, int y){
glutSetWindow(windowid);
int changed = false;
const int dx = x - _mouseX;
const int dy = y - _mouseY;
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT,viewport);
if (dx==0 && dy==0)
return;
if (_mouseMiddle || (_mouseLeft && _mouseRight)){
double s = exp((double)dy*0.01);
glTranslatef( zprReferencePoint[0], zprReferencePoint[1], zprReferencePoint[2]);
glScalef(s,s,s);
glTranslatef(-zprReferencePoint[0],-zprReferencePoint[1],-zprReferencePoint[2]);
changed = true;
}
else
if (_mouseLeft){
double ax,ay,az;
double bx,by,bz;
double angle;
ax = dy;
ay = dx;
az = 0.0;
angle = vlen(ax,ay,az)/(double)(viewport[2]+1)*180.0;
/* Use inverse matrix to determine local axis of rotation */
bx = _matrixInverse[0]*ax + _matrixInverse[4]*ay + _matrixInverse[8] *az;
by = _matrixInverse[1]*ax + _matrixInverse[5]*ay + _matrixInverse[9] *az;
bz = _matrixInverse[2]*ax + _matrixInverse[6]*ay + _matrixInverse[10]*az;
glTranslatef( zprReferencePoint[0], zprReferencePoint[1], zprReferencePoint[2]);
glRotatef(angle,bx,by,bz);
glTranslatef(-zprReferencePoint[0],-zprReferencePoint[1],-zprReferencePoint[2]);
changed = true;
}
else
if (_mouseRight){
double px,py,pz;
pos(&px,&py,&pz,x,y,viewport);
glLoadIdentity();
glTranslatef(px-_dragPosX,py-_dragPosY,pz-_dragPosZ);
glMultMatrixd(_matrix);
_dragPosX = px;
_dragPosY = py;
_dragPosZ = pz;
changed = true;
}
_mouseX = x;
_mouseY = y;
if (changed){
pick( -1);
getMatrix();
glutPostRedisplay();
}
}
/*****************************************************************
* Utility functions
*****************************************************************/
static double zpr::vlen(double x, double y, double z){
return sqrt(x*x+y*y+z*z);
}
static void zpr::pos(double *px,double *py,double *pz,const int x,const int y,const int *viewport){
/*
Use the ortho projection and viewport information
to map from mouse co-ordinates back into world
co-ordinates
*/
*px = (double)(x-viewport[0])/(double)(viewport[2]);
*py = (double)(y-viewport[1])/(double)(viewport[3]);
*px = _left + (*px)*(_right-_left);
*py = _top + (*py)*(_bottom-_top);
*pz = _zNear;
}
static void zpr::getMatrix(){
glGetDoublev(GL_MODELVIEW_MATRIX,_matrix);
invertMatrix(_matrix,_matrixInverse);
}
/*
* From Mesa-2.2\src\glu\project.c
*
* Compute the inverse of a 4x4 matrix. Contributed by [email protected]
*/
static void zpr::invertMatrix(const GLdouble *m, GLdouble *out){
/* NB. OpenGL Matrices are COLUMN major. */
#define MAT(m,r,c) (m)[(c)*4+(r)]
/* Here's some shorthand converting standard (row,column) to index. */
#define m11 MAT(m,0,0)
#define m12 MAT(m,0,1)
#define m13 MAT(m,0,2)
#define m14 MAT(m,0,3)
#define m21 MAT(m,1,0)
#define m22 MAT(m,1,1)
#define m23 MAT(m,1,2)
#define m24 MAT(m,1,3)
#define m31 MAT(m,2,0)
#define m32 MAT(m,2,1)
#define m33 MAT(m,2,2)
#define m34 MAT(m,2,3)
#define m41 MAT(m,3,0)
#define m42 MAT(m,3,1)
#define m43 MAT(m,3,2)
#define m44 MAT(m,3,3)
GLdouble det;
GLdouble d12, d13, d23, d24, d34, d41;
GLdouble tmp[16]; /* Allow out == in. */
/* Inverse = adjoint / det. (See linear algebra texts.)*/
/* pre-compute 2x2 dets for last two rows when computing */
/* cofactors of first two rows. */
d12 = (m31*m42-m41*m32);
d13 = (m31*m43-m41*m33);
d23 = (m32*m43-m42*m33);
d24 = (m32*m44-m42*m34);
d34 = (m33*m44-m43*m34);
d41 = (m34*m41-m44*m31);
tmp[0] = (m22 * d34 - m23 * d24 + m24 * d23);
tmp[1] = -(m21 * d34 + m23 * d41 + m24 * d13);
tmp[2] = (m21 * d24 + m22 * d41 + m24 * d12);
tmp[3] = -(m21 * d23 - m22 * d13 + m23 * d12);
/* Compute determinant as early as possible using these cofactors. */
det = m11 * tmp[0] + m12 * tmp[1] + m13 * tmp[2] + m14 * tmp[3];
/* Run singularity test. */
if(det == 0.0) {
/* printf("invert_matrix: Warning: Singular matrix.\n"); */
/* memcpy(out,_identity,16*sizeof(double)); */
}
else{
GLdouble invDet = 1.0 / det;
/* Compute rest of inverse. */
tmp[0] *= invDet;
tmp[1] *= invDet;
tmp[2] *= invDet;
tmp[3] *= invDet;
tmp[4] = -(m12 * d34 - m13 * d24 + m14 * d23) * invDet;
tmp[5] = (m11 * d34 + m13 * d41 + m14 * d13) * invDet;
tmp[6] = -(m11 * d24 + m12 * d41 + m14 * d12) * invDet;
tmp[7] = (m11 * d23 - m12 * d13 + m13 * d12) * invDet;
/* Pre-compute 2x2 dets for first two rows when computing */
/* cofactors of last two rows. */
d12 = m11*m22-m21*m12;
d13 = m11*m23-m21*m13;
d23 = m12*m23-m22*m13;
d24 = m12*m24-m22*m14;
d34 = m13*m24-m23*m14;
d41 = m14*m21-m24*m11;
tmp[8] = (m42 * d34 - m43 * d24 + m44 * d23) * invDet;
tmp[9] = -(m41 * d34 + m43 * d41 + m44 * d13) * invDet;
tmp[10] = (m41 * d24 + m42 * d41 + m44 * d12) * invDet;
tmp[11] = -(m41 * d23 - m42 * d13 + m43 * d12) * invDet;
tmp[12] = -(m32 * d34 - m33 * d24 + m34 * d23) * invDet;
tmp[13] = (m31 * d34 + m33 * d41 + m34 * d13) * invDet;
tmp[14] = -(m31 * d24 + m32 * d41 + m34 * d12) * invDet;
tmp[15] = (m31 * d23 - m32 * d13 + m33 * d12) * invDet;
memcpy(out, tmp, 16*sizeof(GLdouble));
}
#undef m11
#undef m12
#undef m13
#undef m14
#undef m21
#undef m22
#undef m23
#undef m24
#undef m31
#undef m32
#undef m33
#undef m34
#undef m41
#undef m42
#undef m43
#undef m44
#undef MAT
}
/***************************************** Picking ****************************************************/
void zpr::zprSelectionFunc(void (*f)(void)){
selection = f;
}
void zpr::zprPickFunc(void (*f)(GLint name)){
pick = f;
}
/* Draw in selection mode */
static void zpr::zprPick(GLdouble x, GLdouble y,GLdouble delX, GLdouble delY){
glutSetWindow(windowid);
GLuint buffer[1024];
const int bufferSize = sizeof(buffer)/sizeof(GLuint);
GLint viewport[4];
GLdouble projection[16];
GLint hits;
GLint i,j,k;
GLint min = -1;
GLuint minZ = -1;
glSelectBuffer(bufferSize,buffer); /* Selection buffer for hit records */
glRenderMode(GL_SELECT); /* OpenGL selection mode */
glInitNames(); /* Clear OpenGL name stack */
glMatrixMode(GL_PROJECTION);
glPushMatrix(); /* Push current projection matrix */
glGetIntegerv(GL_VIEWPORT,viewport); /* Get the current viewport size */
glGetDoublev(GL_PROJECTION_MATRIX,projection); /* Get the projection matrix */
glLoadIdentity(); /* Reset the projection matrix */
gluPickMatrix(x,y,delX,delY,viewport); /* Set the picking matrix */
glMultMatrixd(projection); /* Apply projection matrix */
glMatrixMode(GL_MODELVIEW);
if (selection)
selection(); /* Draw the scene in selection mode */
hits = glRenderMode(GL_RENDER); /* Return to normal rendering mode */
/* Diagnostic output to stdout */
#ifndef NDEBUG
if (hits!=0){
// printf("hits = %d\n",hits);
for (i=0,j=0; i<hits; i++){
/* printf("\tsize = %u, min = %u, max = %u : ",buffer[j],buffer[j+1],buffer[j+2]);
for (k=0; k < (GLint) buffer[j]; k++)
printf("%u ",buffer[j+3+k]);
printf("\n");
*/
j += 3 + buffer[j];
}
}
#endif
/* Determine the nearest hit */
if (hits){
for (i=0,j=0; i<hits; i++){
if (buffer[j+1]<minZ){
/* If name stack is empty, return -1 */
/* If name stack is not empty, return top-most name */
if (buffer[j]==0)
min = -1;
else
min = buffer[j+2+buffer[j]];
minZ = buffer[j+1];
}
j += buffer[j] + 3;
}
}
glMatrixMode(GL_PROJECTION);
glPopMatrix(); /* Restore projection matrix */
glMatrixMode(GL_MODELVIEW);
if (pick){
pick(min); /* Pass pick event back to application */
}
}