comparison mupdf-source/thirdparty/freeglut/progs/demos/spaceball/spaceball.c @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 /* Spaceball demo
2 *
3 * Written by John Tsiombikas <nuclear@member.fsf.org>
4 * (converted from the libspnav cube example)
5 *
6 * Use the spaceball to move and rotate the colored cube.
7 * Pressing any button will reset the cube at its original location.
8 *
9 * Press escape or q to exit.
10 */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <math.h>
15 #include <GL/freeglut.h>
16 #include "vmath.h"
17
18 #ifndef M_PI
19 #define M_PI 3.14159265358979323846264338327950
20 #endif
21
22 void draw_cube(void);
23
24 /* callbacks */
25 void disp(void);
26 void reshape(int x, int y);
27 void keyb(unsigned char key, int x, int y);
28 void sbmot(int x, int y, int z); /* spaceball translation */
29 void sbrot(int x, int y, int z); /* spaceball rotation */
30 void sbbut(int bn, int state); /* spaceball button */
31
32 vec3_t pos = {0, 0, -6};
33 quat_t rot = {0, 0, 0, 1};
34
35 int main(int argc, char **argv)
36 {
37 glutInit(&argc, argv);
38 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
39 glutCreateWindow("spaceball demo");
40
41 glutDisplayFunc(disp);
42 glutReshapeFunc(reshape);
43 glutKeyboardFunc(keyb);
44 glutSpaceballMotionFunc(sbmot);
45 glutSpaceballRotateFunc(sbrot);
46 glutSpaceballButtonFunc(sbbut);
47
48 glEnable(GL_CULL_FACE);
49
50 glutMainLoop();
51 return 0;
52 }
53
54 void disp(void)
55 {
56 mat4_t xform;
57
58 quat_to_mat(xform, rot);
59
60 glClear(GL_COLOR_BUFFER_BIT);
61
62 glMatrixMode(GL_MODELVIEW);
63 glLoadIdentity();
64 glTranslatef(pos.x, pos.y, pos.z);
65 glMultMatrixf((float*)xform);
66
67 draw_cube();
68
69 glutSwapBuffers();
70 }
71
72 void draw_cube(void)
73 {
74 glBegin(GL_QUADS);
75 /* face +Z */
76 glNormal3f(0, 0, 1);
77 glColor3f(1, 0, 0);
78 glVertex3f(-1, -1, 1);
79 glVertex3f(1, -1, 1);
80 glVertex3f(1, 1, 1);
81 glVertex3f(-1, 1, 1);
82 /* face +X */
83 glNormal3f(1, 0, 0);
84 glColor3f(0, 1, 0);
85 glVertex3f(1, -1, 1);
86 glVertex3f(1, -1, -1);
87 glVertex3f(1, 1, -1);
88 glVertex3f(1, 1, 1);
89 /* face -Z */
90 glNormal3f(0, 0, -1);
91 glColor3f(0, 0, 1);
92 glVertex3f(1, -1, -1);
93 glVertex3f(-1, -1, -1);
94 glVertex3f(-1, 1, -1);
95 glVertex3f(1, 1, -1);
96 /* face -X */
97 glNormal3f(-1, 0, 0);
98 glColor3f(1, 1, 0);
99 glVertex3f(-1, -1, -1);
100 glVertex3f(-1, -1, 1);
101 glVertex3f(-1, 1, 1);
102 glVertex3f(-1, 1, -1);
103 /* face +Y */
104 glNormal3f(0, 1, 0);
105 glColor3f(0, 1, 1);
106 glVertex3f(-1, 1, 1);
107 glVertex3f(1, 1, 1);
108 glVertex3f(1, 1, -1);
109 glVertex3f(-1, 1, -1);
110 /* face -Y */
111 glNormal3f(0, -1, 0);
112 glColor3f(1, 0, 1);
113 glVertex3f(-1, -1, -1);
114 glVertex3f(1, -1, -1);
115 glVertex3f(1, -1, 1);
116 glVertex3f(-1, -1, 1);
117 glEnd();
118 }
119
120 /* 45deg fov */
121 #define FOV (M_PI / 4.0)
122
123 void reshape(int x, int y)
124 {
125 float aspect = (float)x / (float)y;
126 float halfy = (float)tan(FOV / 2.0);
127 float halfx = halfy * aspect;
128
129 glViewport(0, 0, x, y);
130
131 glMatrixMode(GL_PROJECTION);
132 glLoadIdentity();
133 glFrustum(-halfx, halfx, -halfy, halfy, 1.0, 1000.0);
134 }
135
136 void keyb(unsigned char key, int x, int y)
137 {
138 switch(key) {
139 case 'q':
140 case 'Q':
141 case 27:
142 exit(0);
143
144 case ' ':
145 /* reset initial view */
146 pos = v3_cons(0, 0, -6);
147 rot = quat_cons(1, 0, 0, 0);
148 glutPostRedisplay();
149
150 default:
151 break;
152 }
153 }
154
155 void sbmot(int x, int y, int z)
156 {
157 pos.x += x * 0.001f;
158 pos.y += y * 0.001f;
159 pos.z -= z * 0.001f;
160 glutPostRedisplay();
161 }
162
163 void sbrot(int x, int y, int z)
164 {
165 float axis_len = (float)sqrt(x * x + y * y + z * z);
166 rot = quat_rotate(rot, axis_len * 0.001f, -x / axis_len, -y / axis_len, z / axis_len);
167 glutPostRedisplay();
168 }
169
170 void sbbut(int bn, int state)
171 {
172 if(state == GLUT_DOWN) {
173 pos = v3_cons(0, 0, -6);
174 rot = quat_cons(1, 0, 0, 0);
175 glutPostRedisplay();
176 }
177 }