Commit d1e4ad9704dfc51a808fc58c5b7ba035c2c1332e

henry 2001-11-11T23:59:25

Initial import of demo

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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
diff --git a/demo/FTGLDemo.c b/demo/FTGLDemo.c
new file mode 100644
index 0000000..de1eae6
--- /dev/null
+++ b/demo/FTGLDemo.c
@@ -0,0 +1,324 @@
+#ifdef __APPLE_CC__
+	#include <OpenGL/glut.h>
+#else
+	#include <GL/glut.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "tb.h"
+
+#include "FTGLExtrdFont.h"
+//#include "FTGLOutlineFont.h"
+//#include "FTGLPolygonFont.h"
+//#include "FTGLTextureFont.h"
+//#include "FTGLPixmapFont.h"
+//#include "FTGLBitmapFont.h"
+
+#ifdef __linux__
+#define FONT_FILE "/usr/share/fonts/truetype/arial.ttf"
+#else
+#define FONT_FILE ":Stacey:Curlz MT"
+#endif
+
+#define EDITING 1
+#define INTERACTIVE 2
+
+GLint w_win = 640, h_win = 480;
+float posX, posY, posZ;
+int mode = INTERACTIVE;
+int carat = 0;
+char myString[16];
+
+static FTGLExtrdFont font;
+//static FTGLOutlineFont font;
+//static FTGLPolygonFont font;
+//static FTGLTextureFont font;
+//static FTGLPixmapFont font;
+//static FTGLBitmapFont font;
+
+void SetCamera(void);
+
+
+void my_lighting()
+{
+   // Set up lighting.
+   float light1_ambient[4]  = { 1.0, 1.0, 1.0, 1.0 };
+   float light1_diffuse[4]  = { 1.0, 0.9, 0.9, 1.0 };
+   float light1_specular[4] = { 1.0, 0.7, 0.7, 1.0 };
+   float light1_position[4] = { -1.0, 1.0, 1.0, 0.0 };
+   glLightfv(GL_LIGHT1, GL_AMBIENT,  light1_ambient);
+   glLightfv(GL_LIGHT1, GL_DIFFUSE,  light1_diffuse);
+   glLightfv(GL_LIGHT1, GL_SPECULAR, light1_specular);
+   glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
+   glEnable(GL_LIGHT1);
+
+   float light2_ambient[4]  = { 0.2, 0.2, 0.2, 1.0 };
+   float light2_diffuse[4]  = { 0.9, 0.9, 0.9, 1.0 };
+   float light2_specular[4] = { 0.7, 0.7, 0.7, 1.0 };
+   float light2_position[4] = { 1.0, -1.0, -1.0, 0.0 };
+   glLightfv(GL_LIGHT2, GL_AMBIENT,  light2_ambient);
+   glLightfv(GL_LIGHT2, GL_DIFFUSE,  light2_diffuse);
+   glLightfv(GL_LIGHT2, GL_SPECULAR, light2_specular);
+   glLightfv(GL_LIGHT2, GL_POSITION, light2_position);
+   glEnable(GL_LIGHT2);
+
+   float front_emission[4] = { 0.3, 0.2, 0.1, 0.0 };
+   float front_ambient[4]  = { 0.2, 0.2, 0.2, 0.0 };
+   float front_diffuse[4]  = { 0.95, 0.95, 0.8, 0.0 };
+   float front_specular[4] = { 0.6, 0.6, 0.6, 0.0 };
+   glMaterialfv(GL_FRONT, GL_EMISSION, front_emission);
+   glMaterialfv(GL_FRONT, GL_AMBIENT, front_ambient);
+   glMaterialfv(GL_FRONT, GL_DIFFUSE, front_diffuse);
+   glMaterialfv(GL_FRONT, GL_SPECULAR, front_specular);
+   glMaterialf(GL_FRONT, GL_SHININESS, 16.0);
+   glColor4fv(front_diffuse);
+
+   glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
+   glCullFace(GL_BACK);
+   glFrontFace(GL_CCW);
+   glEnable(GL_CULL_FACE);
+   glColorMaterial(GL_FRONT, GL_DIFFUSE);
+   glEnable(GL_COLOR_MATERIAL);
+
+   glEnable(GL_LIGHTING);
+   glShadeModel(GL_SMOOTH);
+}
+
+
+void do_display (void)
+{
+//	glTranslatef( posX, posY, posZ);
+
+	font.render( myString);
+
+	int x1, y1, z1, x2, y2, z2;
+	font.BBox( myString, x1, y1, z1, x2, y2, z2);
+	
+	glDisable( GL_LIGHTING);
+	// Draw the bounding box
+	glColor3f( 0.0, 1.0, 0.0);
+	glBegin( GL_LINE_LOOP);
+		glVertex3f( x1, y1, z1);
+		glVertex3f( x1, y2, z1);
+		glVertex3f( x2, y2, z1);
+		glVertex3f( x2, y1, z1);
+	glEnd();
+	glBegin( GL_LINE_LOOP);
+		glVertex3f( x1, y1, z2);
+		glVertex3f( x1, y2, z2);
+		glVertex3f( x2, y2, z2);
+		glVertex3f( x2, y1, z2);
+	glEnd();
+	glBegin( GL_LINES);
+		glVertex3f( x1, y1, z1);
+		glVertex3f( x1, y1, z2);
+		
+		glVertex3f( x1, y2, z1);
+		glVertex3f( x1, y2, z2);
+		
+		glVertex3f( x2, y2, z1);
+		glVertex3f( x2, y2, z2);
+		
+		glVertex3f( x2, y1, z1);
+		glVertex3f( x2, y1, z2);
+		
+		// Draw the baseline, Ascender and Descender
+		glColor3f( 0.0, 0.0, 1.0);
+		glVertex3f( 0.0, 0.0, 0.0);
+		glVertex3f( font.Advance( myString), 0.0, 0.0);
+		
+		glVertex3f( 0.0, font.Ascender(), 0.0);
+		glVertex3f( 0.0, font.Descender(), 0.0);
+		
+	glEnd();
+	
+	// Draw the origin
+	glColor3f( 1.0, 0.0, 0.0);
+	glPointSize( 5.0);
+	glBegin( GL_POINTS);
+		glVertex3f( 0.0, 0.0, 0.0);
+	glEnd();
+
+	glutSwapBuffers();
+}
+
+void display(void)
+{
+	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+   	SetCamera();
+	
+	my_lighting();
+
+	glPushMatrix();
+
+	tbMatrix();
+	do_display();
+
+	glPopMatrix();
+
+}
+
+void myinit ( const char* fontfile)
+{
+	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+	glShadeModel (GL_FLAT);
+	glColor3f(1.0, 1.0, 1.0);
+	glClearColor( 0.13, 0.17, 0.32, 0.0);
+	
+	glEnable( GL_CULL_FACE);
+	glFrontFace( GL_CCW);
+	
+	glEnable( GL_DEPTH_TEST);
+	glEnable( GL_TEXTURE_2D);
+
+	glEnable( GL_LINE_SMOOTH);
+	glHint( GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
+	glEnable(GL_BLEND);
+	 	
+	SetCamera();
+
+	if( !font.Open( fontfile, false))
+	{
+		fprintf( stderr, "Failed to open font %s", fontfile);
+		exit(1);
+	}
+	
+	if( !font.FaceSize( 144))
+	{
+		fprintf( stderr, "Failed to set size");
+		exit(1);
+	}
+
+	font.Depth(20);
+	
+	myString[0] = 65;
+	myString[1] = 0;
+	
+
+	tbInit(GLUT_LEFT_BUTTON);
+	tbAnimate( GL_FALSE);
+
+}
+
+/* ARGSUSED1 */
+void parsekey(unsigned char key, int x, int y)
+{
+	switch (key)
+	{
+		case 27: exit(0); break;
+		case 13:
+			if( mode == EDITING)
+			{
+				mode = INTERACTIVE;
+			}
+			else
+			{
+				mode = EDITING;
+				carat = 0;
+			}
+			break;
+		case ' ':break;
+		default:
+			if( mode == INTERACTIVE)
+			{
+				myString[0] = key;
+				myString[1] = 0;
+				break;
+			}
+			else
+			{
+				myString[carat] = key;
+				myString[carat + 1] = 0;
+				carat = carat > 14 ? 15 : ++carat;
+			}
+	}
+	
+	glutPostRedisplay();
+
+}
+
+/* ARGSUSED1 */
+void parsekey_special(int key, int x, int y)
+{
+	switch (key)
+	{
+		case GLUT_KEY_UP:
+			posY += 10;
+			break;
+		case GLUT_KEY_DOWN:
+			posY -= 10;
+			break;
+		case GLUT_KEY_RIGHT:
+			posX += 10;
+			break;
+		case GLUT_KEY_LEFT:
+			posX -= 10;
+			break;
+	}
+}
+
+void motion(int x, int y)
+{
+	tbMotion( x, y);
+}
+
+void mouse(int button, int state, int x, int y)
+{
+	tbMouse( button, state, x, y);
+}
+
+void myReshape(int w, int h)
+{
+	glMatrixMode (GL_MODELVIEW);
+	glViewport (0, 0, w, h);
+	glLoadIdentity();
+		
+	w_win = w;
+	h_win = h;
+	SetCamera();
+	
+	tbReshape(w_win, h_win);
+
+}
+
+void SetCamera(void)
+{
+	glMatrixMode (GL_PROJECTION);
+	glLoadIdentity ();
+	gluPerspective( 90, w_win/ h_win, 1, 1000);
+
+	glMatrixMode(GL_MODELVIEW);
+	glLoadIdentity();
+	gluLookAt( 0.0, 0.0, h_win / 2, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
+
+}
+
+
+int main(int argc, char *argv[])
+{
+	char* fontfile = FONT_FILE;
+
+	if (argc == 2)
+		fontfile = argv[1];
+
+	glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_DOUBLE | GLUT_MULTISAMPLE);
+	glutInitWindowPosition(50, 50);
+	glutInitWindowSize(w_win, h_win);
+	glutCreateWindow("FTGL TEST");
+	glutDisplayFunc(display);
+	glutKeyboardFunc(parsekey);
+	glutMouseFunc(mouse);
+    glutMotionFunc(motion);
+	glutSpecialFunc(parsekey_special);
+	glutReshapeFunc(myReshape);
+	glutIdleFunc(display);
+
+	myinit( fontfile);
+
+	glutMainLoop();
+
+	return 0;             /* ANSI C requires main to return int. */
+}
diff --git a/demo/tb.c b/demo/tb.c
new file mode 100644
index 0000000..b3dd01e
--- /dev/null
+++ b/demo/tb.c
@@ -0,0 +1,123 @@
+/*
+ *  Simple trackball-like motion adapted (ripped off) from projtex.c
+ *  (written by David Yu and David Blythe).  See the SIGGRAPH '96
+ *  Advanced OpenGL course notes.
+ */
+
+
+/* includes */
+#include <math.h>
+#include <assert.h>
+#include <GL/glut.h>
+#include "tb.h"
+#include "trackball.h"
+
+/* globals */
+static GLuint    tb_lasttime;
+
+float curquat[4];
+float lastquat[4];
+int beginx, beginy;
+
+static GLuint    tb_width;
+static GLuint    tb_height;
+
+static GLint     tb_button = -1;
+static GLboolean tb_tracking = GL_FALSE;
+static GLboolean tb_animate = GL_TRUE;
+
+static void
+_tbAnimate(void)
+{
+  add_quats(lastquat, curquat, curquat);
+  glutPostRedisplay();
+}
+
+void
+_tbStartMotion(int x, int y, int time)
+{
+  assert(tb_button != -1);
+
+  glutIdleFunc(0);
+  tb_tracking = GL_TRUE;
+  tb_lasttime = time;
+  beginx = x;
+  beginy = y;
+}
+
+void
+_tbStopMotion(unsigned time)
+{
+  assert(tb_button != -1);
+
+  tb_tracking = GL_FALSE;
+
+  if (time == tb_lasttime && tb_animate) {
+    glutIdleFunc(_tbAnimate);
+  } else {
+    if (tb_animate) {
+      glutIdleFunc(0);
+    }
+  }
+}
+
+void
+tbAnimate(GLboolean animate)
+{
+  tb_animate = animate;
+}
+
+void
+tbInit(GLuint button)
+{
+  tb_button = button;
+  trackball(curquat, 0.0, 0.0, 0.0, 0.0);
+}
+
+void
+tbMatrix(void)
+{
+  GLfloat m[4][4];
+
+  assert(tb_button != -1);
+  build_rotmatrix(m, curquat);
+  glMultMatrixf(&m[0][0]);
+}
+
+void
+tbReshape(int width, int height)
+{
+  assert(tb_button != -1);
+
+  tb_width  = width;
+  tb_height = height;
+}
+
+void
+tbMouse(int button, int state, int x, int y)
+{
+  assert(tb_button != -1);
+
+  if (state == GLUT_DOWN && button == tb_button)
+    _tbStartMotion(x, y, glutGet(GLUT_ELAPSED_TIME));
+  else if (state == GLUT_UP && button == tb_button)
+    _tbStopMotion(glutGet(GLUT_ELAPSED_TIME));
+}
+
+void
+tbMotion(int x, int y)
+{
+  if (tb_tracking) {
+    trackball(lastquat,
+      (2.0 * beginx - tb_width) / tb_width,
+      (tb_height - 2.0 * beginy) / tb_height,
+      (2.0 * x - tb_width) / tb_width,
+      (tb_height - 2.0 * y) / tb_height
+      );
+    beginx = x;
+    beginy = y;
+    tb_animate = 1;
+    tb_lasttime = glutGet(GLUT_ELAPSED_TIME);
+    _tbAnimate();
+  }
+}
diff --git a/demo/tb.h b/demo/tb.h
new file mode 100644
index 0000000..de0f00c
--- /dev/null
+++ b/demo/tb.h
@@ -0,0 +1,95 @@
+/* 
+ *  Simple trackball-like motion adapted (ripped off) from projtex.c
+ *  (written by David Yu and David Blythe).  See the SIGGRAPH '96
+ *  Advanced OpenGL course notes.
+ *
+ *
+ *  Usage:
+ *  
+ *  o  call tbInit() in before any other tb call
+ *  o  call tbReshape() from the reshape callback
+ *  o  call tbMatrix() to get the trackball matrix rotation
+ *  o  call tbStartMotion() to begin trackball movememt
+ *  o  call tbStopMotion() to stop trackball movememt
+ *  o  call tbMotion() from the motion callback
+ *  o  call tbAnimate(GL_TRUE) if you want the trackball to continue 
+ *     spinning after the mouse button has been released
+ *  o  call tbAnimate(GL_FALSE) if you want the trackball to stop 
+ *     spinning after the mouse button has been released
+ *
+ *  Typical setup:
+ *
+ *
+    void
+    init(void)
+    {
+      tbInit(GLUT_MIDDLE_BUTTON);
+      tbAnimate(GL_TRUE);
+      . . .
+    }
+
+    void
+    reshape(int width, int height)
+    {
+      tbReshape(width, height);
+      . . .
+    }
+
+    void
+    display(void)
+    {
+      glPushMatrix();
+
+      tbMatrix();
+      . . . draw the scene . . .
+
+      glPopMatrix();
+    }
+
+    void
+    mouse(int button, int state, int x, int y)
+    {
+      tbMouse(button, state, x, y);
+      . . .
+    }
+
+    void
+    motion(int x, int y)
+    {
+      tbMotion(x, y);
+      . . .
+    }
+
+    int
+    main(int argc, char** argv)
+    {
+      . . .
+      init();
+      glutReshapeFunc(reshape);
+      glutDisplayFunc(display);
+      glutMouseFunc(mouse);
+      glutMotionFunc(motion);
+      . . .
+    }
+ *
+ * */
+
+
+/* functions */
+void
+tbInit(GLuint button);
+
+void
+tbMatrix(void);
+
+void
+tbReshape(int width, int height);
+
+void
+tbMouse(int button, int state, int x, int y);
+
+void
+tbMotion(int x, int y);
+
+void
+tbAnimate(GLboolean animate);
diff --git a/demo/trackball.c b/demo/trackball.c
new file mode 100644
index 0000000..4843457
--- /dev/null
+++ b/demo/trackball.c
@@ -0,0 +1,324 @@
+/*
+ * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
+ * ALL RIGHTS RESERVED
+ * Permission to use, copy, modify, and distribute this software for
+ * any purpose and without fee is hereby granted, provided that the above
+ * copyright notice appear in all copies and that both the copyright notice
+ * and this permission notice appear in supporting documentation, and that
+ * the name of Silicon Graphics, Inc. not be used in advertising
+ * or publicity pertaining to distribution of the software without specific,
+ * written prior permission.
+ *
+ * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
+ * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
+ * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
+ * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
+ * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
+ * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
+ * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
+ * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
+ * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
+ * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * US Government Users Restricted Rights
+ * Use, duplication, or disclosure by the Government is subject to
+ * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
+ * (c)(1)(ii) of the Rights in Technical Data and Computer Software
+ * clause at DFARS 252.227-7013 and/or in similar or successor
+ * clauses in the FAR or the DOD or NASA FAR Supplement.
+ * Unpublished-- rights reserved under the copyright laws of the
+ * United States.  Contractor/manufacturer is Silicon Graphics,
+ * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
+ *
+ * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
+ */
+/*
+ * Trackball code:
+ *
+ * Implementation of a virtual trackball.
+ * Implemented by Gavin Bell, lots of ideas from Thant Tessman and
+ *   the August '88 issue of Siggraph's "Computer Graphics," pp. 121-129.
+ *
+ * Vector manip code:
+ *
+ * Original code from:
+ * David M. Ciemiewicz, Mark Grossman, Henry Moreton, and Paul Haeberli
+ *
+ * Much mucking with by:
+ * Gavin Bell
+ */
+#include <math.h>
+#include "trackball.h"
+
+/*
+ * This size should really be based on the distance from the center of
+ * rotation to the point on the object underneath the mouse.  That
+ * point would then track the mouse as closely as possible.  This is a
+ * simple example, though, so that is left as an Exercise for the
+ * Programmer.
+ */
+#define TRACKBALLSIZE  (0.4)
+
+/*
+ * Local function prototypes (not defined in trackball.h)
+ */
+static float tb_project_to_sphere(float, float, float);
+static void normalize_quat(float [4]);
+
+void
+vzero(float *v)
+{
+    v[0] = 0.0;
+    v[1] = 0.0;
+    v[2] = 0.0;
+}
+
+void
+vset(float *v, float x, float y, float z)
+{
+    v[0] = x;
+    v[1] = y;
+    v[2] = z;
+}
+
+void
+vsub(const float *src1, const float *src2, float *dst)
+{
+    dst[0] = src1[0] - src2[0];
+    dst[1] = src1[1] - src2[1];
+    dst[2] = src1[2] - src2[2];
+}
+
+void
+vcopy(const float *v1, float *v2)
+{
+    register int i;
+    for (i = 0 ; i < 3 ; i++)
+        v2[i] = v1[i];
+}
+
+void
+vcross(const float *v1, const float *v2, float *cross)
+{
+    float temp[3];
+
+    temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
+    temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
+    temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
+    vcopy(temp, cross);
+}
+
+float
+vlength(const float *v)
+{
+    return sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
+}
+
+void
+vscale(float *v, float div)
+{
+    v[0] *= div;
+    v[1] *= div;
+    v[2] *= div;
+}
+
+void
+vnormal(float *v)
+{
+    vscale(v,1.0/vlength(v));
+}
+
+float
+vdot(const float *v1, const float *v2)
+{
+    return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
+}
+
+void
+vadd(const float *src1, const float *src2, float *dst)
+{
+    dst[0] = src1[0] + src2[0];
+    dst[1] = src1[1] + src2[1];
+    dst[2] = src1[2] + src2[2];
+}
+
+/*
+ * Ok, simulate a track-ball.  Project the points onto the virtual
+ * trackball, then figure out the axis of rotation, which is the cross
+ * product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)
+ * Note:  This is a deformed trackball-- is a trackball in the center,
+ * but is deformed into a hyperbolic sheet of rotation away from the
+ * center.  This particular function was chosen after trying out
+ * several variations.
+ *
+ * It is assumed that the arguments to this routine are in the range
+ * (-1.0 ... 1.0)
+ */
+void
+trackball(float q[4], float p1x, float p1y, float p2x, float p2y)
+{
+    float a[3]; /* Axis of rotation */
+    float phi;  /* how much to rotate about axis */
+    float p1[3], p2[3], d[3];
+    float t;
+
+    if (p1x == p2x && p1y == p2y) {
+        /* Zero rotation */
+        vzero(q);
+        q[3] = 1.0;
+        return;
+    }
+
+    /*
+     * First, figure out z-coordinates for projection of P1 and P2 to
+     * deformed sphere
+     */
+    vset(p1,p1x,p1y,tb_project_to_sphere(TRACKBALLSIZE,p1x,p1y));
+    vset(p2,p2x,p2y,tb_project_to_sphere(TRACKBALLSIZE,p2x,p2y));
+
+    /*
+     *  Now, we want the cross product of P1 and P2
+     */
+    vcross(p2,p1,a);
+
+    /*
+     *  Figure out how much to rotate around that axis.
+     */
+    vsub(p1,p2,d);
+    t = vlength(d) / (2.0*TRACKBALLSIZE);
+
+    /*
+     * Avoid problems with out-of-control values...
+     */
+    if (t > 1.0) t = 1.0;
+    if (t < -1.0) t = -1.0;
+    phi = 2.0 * asin(t);
+
+    axis_to_quat(a,phi,q);
+}
+
+/*
+ *  Given an axis and angle, compute quaternion.
+ */
+void
+axis_to_quat(float a[3], float phi, float q[4])
+{
+    vnormal(a);
+    vcopy(a,q);
+    vscale(q,sin(phi/2.0));
+    q[3] = cos(phi/2.0);
+}
+
+/*
+ * Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
+ * if we are away from the center of the sphere.
+ */
+static float
+tb_project_to_sphere(float r, float x, float y)
+{
+    float d, t, z;
+
+    d = sqrt(x*x + y*y);
+    if (d < r * 0.70710678118654752440) {    /* Inside sphere */
+        z = sqrt(r*r - d*d);
+    } else {           /* On hyperbola */
+        t = r / 1.41421356237309504880;
+        z = t*t / d;
+    }
+    return z;
+}
+
+/*
+ * Given two rotations, e1 and e2, expressed as quaternion rotations,
+ * figure out the equivalent single rotation and stuff it into dest.
+ *
+ * This routine also normalizes the result every RENORMCOUNT times it is
+ * called, to keep error from creeping in.
+ *
+ * NOTE: This routine is written so that q1 or q2 may be the same
+ * as dest (or each other).
+ */
+
+#define RENORMCOUNT 97
+
+void
+add_quats(float q1[4], float q2[4], float dest[4])
+{
+    static int count=0;
+    float t1[4], t2[4], t3[4];
+    float tf[4];
+
+    vcopy(q1,t1);
+    vscale(t1,q2[3]);
+
+    vcopy(q2,t2);
+    vscale(t2,q1[3]);
+
+    vcross(q2,q1,t3);
+    vadd(t1,t2,tf);
+    vadd(t3,tf,tf);
+    tf[3] = q1[3] * q2[3] - vdot(q1,q2);
+
+    dest[0] = tf[0];
+    dest[1] = tf[1];
+    dest[2] = tf[2];
+    dest[3] = tf[3];
+
+    if (++count > RENORMCOUNT) {
+        count = 0;
+        normalize_quat(dest);
+    }
+}
+
+/*
+ * Quaternions always obey:  a^2 + b^2 + c^2 + d^2 = 1.0
+ * If they don't add up to 1.0, dividing by their magnitued will
+ * renormalize them.
+ *
+ * Note: See the following for more information on quaternions:
+ *
+ * - Shoemake, K., Animating rotation with quaternion curves, Computer
+ *   Graphics 19, No 3 (Proc. SIGGRAPH'85), 245-254, 1985.
+ * - Pletinckx, D., Quaternion calculus as a basic tool in computer
+ *   graphics, The Visual Computer 5, 2-13, 1989.
+ */
+static void
+normalize_quat(float q[4])
+{
+    int i;
+    float mag;
+
+    mag = (q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3]);
+    for (i = 0; i < 4; i++) q[i] /= mag;
+}
+
+/*
+ * Build a rotation matrix, given a quaternion rotation.
+ *
+ */
+void
+build_rotmatrix(float m[4][4], float q[4])
+{
+    m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
+    m[0][1] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
+    m[0][2] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
+    m[0][3] = 0.0;
+
+    m[1][0] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
+    m[1][1]= 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
+    m[1][2] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
+    m[1][3] = 0.0;
+
+    m[2][0] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
+    m[2][1] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
+    m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);
+    m[2][3] = 0.0;
+
+    m[3][0] = 0.0;
+    m[3][1] = 0.0;
+    m[3][2] = 0.0;
+    m[3][3] = 1.0;
+}
+
diff --git a/demo/trackball.h b/demo/trackball.h
new file mode 100644
index 0000000..b676fb4
--- /dev/null
+++ b/demo/trackball.h
@@ -0,0 +1,78 @@
+/*
+ * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
+ * ALL RIGHTS RESERVED
+ * Permission to use, copy, modify, and distribute this software for
+ * any purpose and without fee is hereby granted, provided that the above
+ * copyright notice appear in all copies and that both the copyright notice
+ * and this permission notice appear in supporting documentation, and that
+ * the name of Silicon Graphics, Inc. not be used in advertising
+ * or publicity pertaining to distribution of the software without specific,
+ * written prior permission.
+ *
+ * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
+ * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
+ * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
+ * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
+ * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
+ * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
+ * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
+ * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
+ * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
+ * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * US Government Users Restricted Rights
+ * Use, duplication, or disclosure by the Government is subject to
+ * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
+ * (c)(1)(ii) of the Rights in Technical Data and Computer Software
+ * clause at DFARS 252.227-7013 and/or in similar or successor
+ * clauses in the FAR or the DOD or NASA FAR Supplement.
+ * Unpublished-- rights reserved under the copyright laws of the
+ * United States.  Contractor/manufacturer is Silicon Graphics,
+ * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
+ *
+ * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
+ */
+/*
+ * trackball.h
+ * A virtual trackball implementation
+ * Written by Gavin Bell for Silicon Graphics, November 1988.
+ */
+
+/*
+ * Pass the x and y coordinates of the last and current positions of
+ * the mouse, scaled so they are from (-1.0 ... 1.0).
+ *
+ * The resulting rotation is returned as a quaternion rotation in the
+ * first paramater.
+ */
+void
+trackball(float q[4], float p1x, float p1y, float p2x, float p2y);
+
+/*
+ * Given two quaternions, add them together to get a third quaternion.
+ * Adding quaternions to get a compound rotation is analagous to adding
+ * translations to get a compound translation.  When incrementally
+ * adding rotations, the first argument here should be the new
+ * rotation, the second and third the total rotation (which will be
+ * over-written with the resulting new total rotation).
+ */
+void
+add_quats(float *q1, float *q2, float *dest);
+
+/*
+ * A useful function, builds a rotation matrix in Matrix based on
+ * given quaternion.
+ */
+void
+build_rotmatrix(float m[4][4], float q[4]);
+
+/*
+ * This function computes a quaternion based on an axis (defined by
+ * the given vector) and an angle about which to rotate.  The angle is
+ * expressed in radians.  The result is put into the third argument.
+ */
+void
+axis_to_quat(float a[3], float phi, float q[4]);
+