-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBaseWidget.h
478 lines (404 loc) · 15.5 KB
/
BaseWidget.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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//------------------------------------------------------------------------
// BaseWidget.h
// Joe Kniss
// base class for direct manipulation widgets
// 2-27-01
// ________ ____ ___
// | \ / | / /
// +---+ \/ |/ /
// +--+| |\ /| <
// | || | \ / | |\ \
// | | \/ | | \ \
// \_____| |__| \__\
// Copyright 2001
// Joe Michael Kniss
//-------------------------------------------------------------------------
#ifndef __BASE_WIDGET_H
#define __BASE_WIDGET_H
#ifdef WIN32
#include <windows.h>
#endif
#include <GL/gl.h>
#include <GL/glu.h>
#ifndef WIN32
#include <X11/keysym.h>
#endif
//=========================================================================
// Please READ:
//Rules for Picking Widgets: (in your (widget's) draw function)
// First(1) push 'I_AM_A_WIDGET' to indecate what you are.
// Next(2) push your 'this' pointer, (1&2 are coverd by 'pushName()')
// Finaly(3) push two integers.
// note: for 64 bit builds, the 'this' pointer is 2 integers.
// your pickcb will be called first, if it returns non zero
// mouse and keyboard state should be sent to you via respective
// callbacks
// If the app takes back controll of events (un-picks you), your
// release() function will be called
// These rules only work if the event manager respects them.
// see the Widget::pushName() and Widget::popNames() functions below
//=========================================================================
//---- Widget names --------------------------------------------------
// names to push during a pick, part one (1) above
enum{
I_AM_A_WIDGET = 0x98765432,
I_AM_A_WIDGET_GROUP = 0x98765111,
EMPTY_WIDGET_UINT = 0x12345678
};
//---- Mouse State enums ---------------------------------------------
// mouse button enums for callbacks
typedef enum{
WdgMouseUnknown,
WdgM_BLeft,
WdgM_BMiddle,
WdgM_BRight
} WdgMouseButton;
typedef enum{
WdgM_SUnknown,
WdgM_SUp,
WdgM_SDown
} WdgMouseState;
//---- axis enums for default glyph renderers ------------------------
typedef enum{
WdgAxisUnknown,
WdgXAxis,
WdgYAxis,
WdgZAxis
} WdgRotAxis;
//============================================================================
//------- THE WIDGET !!! -----------------------------------------------------
//============================================================================
class Widget
{
public:
Widget();
//timing is everyting!
//(after OpenGL has been inited)
void widgetInit();
//Memory leaks are bad!!
void widgetFree();
~Widget(){}
//-----------------------------------------------------------------
//callbacks should return 0 when the object wants to be released---
//draw function should be over
// ridden for any widget, since it
// will be drawn independant of its
// pick status, a return value is
// not necessary.
virtual void draw();
//this object has been picked at
// {x,y,z}, if z = 0, picked in
// screen space, needs transformation
virtual int pickcb(int data1, // this is the data pushed on
int data2, // after your name in a pick
float x, float y, float z = 0);
//this object has moved to
// {x,y,z}, if z = 0, move happened
// in screen space
virtual int movecb(float x, float y, float z = 0);
//standard keyboard press, send key
// only if the object has been selected
virtual int keycb(char key);
//X windows keyboard callback, send key
// only if the object has been selected
virtual int Xkeycb(unsigned int ks);
//this object has been selected by the
// mouse with buttion, and click state
// see enums above
virtual int mousecb(WdgMouseButton button,
WdgMouseState state,
float x, float y, float z);
//widgets need to know the size of the
// view port if the mouse is being used
virtual void resize(int x, int y);
//The application is releasing the
//widget, it is no longer picked
virtual int releasecb();
//end callbacks----------------------------------------------------
//-----------------------------------------------------------------
//-----------------------------------------------------------------
//--------General utils -------------------------------------------
//set the position of the widget
//in world space
inline void SetPos(float x, float y, float z);
//get the position of the widget
inline float *GetPos();
//set the transform of the widget
inline void SetXform(GLfloat Mat[16], //4x4 transformation matrix
GLfloat Trans[3], //translation can be {0,0,0}
GLfloat Scale = 1.0);//can set a global scale
//standard material for widgets
// sets color, specular, diffuse etc..
inline void defaultMaterial();
inline void ident(GLfloat m[16]); //matrix identity function (vectorMath.h)
//pushes your TYPE & 'this' pointer
//for you, don't forget to push two more
//integer values on the stack as well
inline void pushName();
//pops ALL of the required names
//off the stack, including 2 datas
inline void popNames();
//this should move to ::TWidget
//inline void setName(WidgetGroup *n){ name = n; }
//set clipping planes based on bounds
//-- should this move too??
inline void setClips(GLfloat m[16], GLfloat trans[3], GLfloat scale);
//unset ALL clip planes
inline void killClips();
//---- draw some standard Widget parts -----------------------------
inline void drawBar(float rot, WdgRotAxis axis,
float len, float pos[3], unsigned int name);
inline void drawSphere(float pos[3], unsigned int name);
inline void drawCone(float rot, WdgRotAxis axis, float len,
float pos[3], unsigned int name);
inline void drawSlider(float rot, WdgRotAxis axis,
float pos[3], unsigned int name);
//---- draw styles -------------------------------------------------
void transparent(); // realy just silhoette style
void fill();
//cheap and dirty mutex, realy needs to be fixed!
bool busy;
protected:
//WidgetGroup *name;
//--- global widget values -----------
GLfloat position[3];
GLfloat orient[16];
//--- global widget display properties
GLUquadricObj *qobj;
float barRad;
float barSlice, barStack;
float ballRad;
float ballSlice;
float coneRad; //NOTE: cone uses barSlice & barStacks rendering
//--- environment variables ----------
int winWidth;
int winHeight;
GLfloat xform[16];
GLfloat trans[3];
GLfloat scale;
//--- global widget bounds -----------
//assume axis aligned for now! for cliping to volumes
float LLbound[3]; //Lower Left Bound [0,0,0]
float URbound[3]; //Upper right Bound [1,1,1]
float maxBound[3]; //global Upper Right bound
float minBound[3]; //global Lower Left bound
//--- selection stuff ----------------
int pickedObj;
float lastPos[3];
WdgMouseButton but;
WdgMouseState m_state;
private:
};
//=============================================================================
//-------- inline methods -----------------------------------------------------
//=============================================================================
//--------------------------------------------------------------------------
inline void Widget::SetPos(float x, float y, float z)
{
position[0] = x; position[1] = y; position[2] = z;
}
//--------------------------------------------------------------------------
inline float *Widget::GetPos()
{
return position;
}
//--------------------------------------------------------------------------
//set the transform of the widget
inline void Widget::SetXform(GLfloat Mat[16], GLfloat Trans[3], GLfloat Scale)
{
for(int i=0; i<16; ++i) xform[i] = Mat[i];
for(i=0; i<3; ++i) trans[i] = Trans[i];
scale = Scale;
}
//--------------------------------------------------------------------------
inline void Widget::defaultMaterial()
{
GLfloat wamb[] = { (float)0.3, (float)0.3, (float)0.3, (float)1.0 };
GLfloat wspec[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat wdiff[] = { 0.5, 0.5, 0.5, 1.0 };
GLfloat wshine[] = { 50.0 };
glEnable(GL_COLOR_MATERIAL);
glMaterialfv(GL_FRONT, GL_AMBIENT, wamb);
glMaterialfv(GL_FRONT, GL_DIFFUSE, wdiff);
glMaterialfv(GL_FRONT, GL_SPECULAR, wspec);
glMaterialfv(GL_FRONT, GL_SHININESS, wshine);
glColor4f(1,1,1,1);
}
//--------------------------------------------------------------------------
inline void Widget::ident(GLfloat m[16])
{
m[0]=1; m[4]=0; m[8]= 0; m[12]=0;
m[1]=0; m[5]=1; m[9]= 0; m[13]=0;
m[2]=0; m[6]=0; m[10]=1; m[14]=0;
m[3]=0; m[7]=0; m[11]=0; m[15]=1;
}
//--------------------------------------------------------------------------
inline void Widget::pushName()
{
//if(name)
// glPushName(I_AM_A_WIDGET_GROUP);
//else
glPushName(I_AM_A_WIDGET);
#if (_MIPS_SZPTR == 64) //64 Bit
unsigned long o;
//if(name) o=(unsigned long)name;
//else
o=(unsigned long)this;
unsigned int o1=(o>>32)&0xffffffff;
unsigned int o2=o&0xffffffff;
glPushName(o1);
glPushName(o2);
#else //32 bit
// if(name)
// glPushName((GLuint)name);
//else
glPushName((GLuint)this);
#endif
}
//--------------------------------------------------------------------------
inline void Widget::popNames()
{
#if (_MIPS_SZPTR == 64) //64 Bit
glPopName();
#endif
glPopName();
glPopName();
glPopName();
glPopName();
}
//--------------------------------------------------------------------------
inline void Widget::drawBar(float rot, WdgRotAxis axis,
float len, float pos[3], unsigned int name)
{
glPushMatrix(); { //bottom front bar
glLoadName((GLuint) name);
glTranslatef(pos[0], pos[1], pos[2]);
if(axis == WdgXAxis) glRotatef(rot, 1, 0, 0);
else if(axis == WdgYAxis) glRotatef(rot, 0, 1, 0);
else glRotatef(rot, 0, 0, 1);
gluCylinder(qobj, barRad, barRad, len, (int)barSlice, (int)barStack);
}
glPopMatrix();
}
//--------------------------------------------------------------------------
inline void Widget::drawSphere(float pos[3], unsigned int name)
{
glPushMatrix();{
glLoadName((GLuint) name);
glTranslatef(pos[0], pos[1], pos[2]);
gluSphere(qobj, ballRad, (int)ballSlice, (int)ballSlice);
}
glPopMatrix();
}
//--------------------------------------------------------------------------
inline void Widget::drawCone(float rot, WdgRotAxis axis,
float len, float pos[3], unsigned int name)
{
glPushMatrix();{
glLoadName((GLuint) name);
glTranslatef(pos[0], pos[1], pos[2]);
if(axis == WdgXAxis) glRotatef(rot, 1, 0, 0);
else if(axis == WdgYAxis) glRotatef(rot, 0, 1, 0);
else glRotatef(rot, 0, 0, 1);
gluCylinder(qobj, 0, coneRad, len, (int)barSlice, (int)barStack);
}
glPopMatrix();
}
//--------------------------------------------------------------------------
inline void Widget::drawSlider(float rot, WdgRotAxis axis,
float pos[3], unsigned int name)
{
glPushMatrix();{
glLoadName((GLuint) name);
glTranslatef(pos[0], pos[1], pos[2]);
if(axis == WdgXAxis) glRotatef(rot, 1, 0, 0);
else if(axis == WdgYAxis) glRotatef(rot, 0, 1, 0);
else glRotatef(rot, 0, 0, 1);
gluCylinder(qobj, ballRad*1.1, ballRad*1.1, ballRad*1.1, (int)barSlice, 2);
gluDisk(qobj, 0, ballRad*1.1, (int)barSlice, 1);
glTranslatef((float)0, (float)0, (float)(ballRad*1.1));
gluDisk(qobj, 0, ballRad*1.1, (int)barSlice, 1);
}
glPopMatrix();
}
//--------------------------------------------------------------------------
inline void Widget::setClips(GLfloat m[16], GLfloat trans[3], GLfloat scale)
{
//this will set clipping planes based on the upper right and lower left
// bounds. if the bounds of a sub regon are inside the max and min
// bounds the geometry will be clipped, other wise not. This is useful
// for parallel compositing. This widget may only belong to a small sub-
// region, it should be clipped where it abutts other sub-regions
GLdouble yup[4] = { 0, 1, 0,0};
GLdouble ydn[4] = { 0,-1, 0,0};
GLdouble xup[4] = { 1, 0, 0,0};
GLdouble xdn[4] = {-1, 0, 0,0};
GLdouble zup[4] = { 0, 0, 1,0};
GLdouble zdn[4] = { 0, 0,-1,0};
glPushMatrix();{
glTranslatef(trans[0], trans[1], trans[2]);
glMultMatrixf(m);
glScalef(scale, scale, scale);
glTranslatef(-.5, -.5, -.5 );//querk of the data representation
if(LLbound[0] > minBound[0]){
glEnable(GL_CLIP_PLANE1);
glPushMatrix();{
glTranslatef(LLbound[0], 0, 0);
glClipPlane(GL_CLIP_PLANE1, xup);
};
glPopMatrix();
}
if(URbound[0] < maxBound[0]){
glEnable(GL_CLIP_PLANE2);
glPushMatrix();{
glTranslatef(URbound[0], 0, 0);
glClipPlane(GL_CLIP_PLANE2, xdn);
};
glPopMatrix();
}
if(LLbound[1] > minBound[1]){
glEnable(GL_CLIP_PLANE3);
glPushMatrix();{
glTranslatef(0, LLbound[1], 0);
glClipPlane(GL_CLIP_PLANE3, yup);
};
glPopMatrix();
}
if(URbound[1] < maxBound[1]){
glEnable(GL_CLIP_PLANE4);
glPushMatrix();{
glTranslatef(0, URbound[1], 0);
glClipPlane(GL_CLIP_PLANE4, ydn);
};
glPopMatrix();
}
if(LLbound[2] > minBound[2]){
glEnable(GL_CLIP_PLANE5);
glPushMatrix();{
glTranslatef(0, 0, LLbound[2]);
glClipPlane(GL_CLIP_PLANE5, zup);
};
glPopMatrix();
}
if(URbound[2] < maxBound[2]){
glEnable(GL_CLIP_PLANE0);
glPushMatrix();{
glTranslatef(0, 0, URbound[2]);
glClipPlane(GL_CLIP_PLANE0, zdn);
};
glPopMatrix();
}
}
glPopMatrix();
}
//--------------------------------------------------------------------------
inline void Widget::killClips()
{
glDisable(GL_CLIP_PLANE1);
glDisable(GL_CLIP_PLANE2);
glDisable(GL_CLIP_PLANE3);
glDisable(GL_CLIP_PLANE4);
glDisable(GL_CLIP_PLANE5);
glDisable(GL_CLIP_PLANE0);
}
#endif