From http://www.jwz.org/xscreensaver/xscreensaver-5.30.tar.gz
[xscreensaver] / hacks / glx / cubicgrid.c
1 /*-
2  * Permission to use, copy, modify, and distribute this software and its
3  * documentation for any purpose and without fee is hereby granted,
4  * provided that the above copyright notice appear in all copies and that
5  * both that copyright notice and this permission notice appear in
6  * supporting documentation.
7  *
8  * This file is provided AS IS with no warranties of any kind.  The author
9  * shall have no liability with respect to the infringement of copyrights,
10  * trade secrets or any patents by this file or any part thereof.  In no
11  * event will the author be liable for any lost revenue or profits or
12  * other special, indirect and consequential damages.
13  *
14  * Cubic Grid - a 3D lattice. The observer is located in the centre of 
15  * a spinning finite lattice. As it rotates, various view-throughs appear and 
16  * evolve. A simple idea with interesting results.
17  * 
18  * Vasek Potocek (Dec-28-2007)
19  * vasek.potocek@post.cz
20  */
21
22 #define DEFAULTS   "*delay:         20000         \n" \
23                    "*showFPS:       False         \n" \
24                    "*wireframe:     False         \n"
25
26 # define refresh_cubicgrid 0
27 #include "xlockmore.h"
28
29 #ifdef USE_GL
30
31 #define DEF_SPEED   "1.0"
32 #define DEF_DIV     "30"
33 #define DEF_ZOOM    "20"
34 #define DEF_BIGDOTS "True"
35
36 #undef countof
37 #define countof(x) (sizeof((x))/sizeof((*x)))
38
39 #include "rotator.h"
40 #include "gltrackball.h"
41
42 /*************************************************************************/
43
44 static int ticks;
45 static float size;
46 static float speed;
47 static Bool bigdots;
48
49 static argtype vars[] = {
50   { &speed,   "speed",   "Speed",   DEF_SPEED,   t_Float },
51   { &size,    "zoom",    "Zoom",    DEF_ZOOM,    t_Float },
52   { &ticks,   "ticks",   "Ticks",   DEF_DIV,     t_Int },
53   { &bigdots, "bigdots", "BigDots", DEF_BIGDOTS, t_Bool },
54 };
55
56 static XrmOptionDescRec opts[] = {
57   { "-speed",   ".speed",   XrmoptionSepArg, 0 },
58   { "-zoom",    ".zoom",    XrmoptionSepArg, 0 },
59   { "-ticks",   ".ticks",   XrmoptionSepArg, 0 },
60   { "-bigdots", ".bigdots", XrmoptionNoArg,  "True" },
61   { "+bigdots", ".bigdots", XrmoptionNoArg,  "False" },
62 };
63
64 ENTRYPOINT ModeSpecOpt cubicgrid_opts = {countof(opts), opts, countof(vars), vars, NULL};
65
66 #ifdef USE_MODULES
67 ModStruct   cubicgrid_description =
68 { "cubicgrid", "init_cubicgrid", "draw_cubicgrid", "release_cubicgrid",
69   "draw_cubicgrid", "change_cubicgrid", NULL, &cubicgrid_opts,
70   25000, 1, 1, 1, 1.0, 4, "",
71   "Shows a rotating 3D lattice from inside", 0, NULL
72 };
73 #endif
74
75 typedef struct {
76   GLXContext    *glx_context;
77   GLfloat       ratio;
78   GLint         list;
79
80   rotator *rot;
81   trackball_state *trackball;
82   Bool button_down_p;
83   int npoints;
84 } cubicgrid_conf;
85
86 static cubicgrid_conf *cubicgrid = NULL;
87
88 static const GLfloat zpos = -18.0;
89
90 /*************************************************************************/
91
92 ENTRYPOINT Bool
93 cubicgrid_handle_event (ModeInfo *mi, XEvent *event)
94 {
95   cubicgrid_conf *cp = &cubicgrid[MI_SCREEN(mi)];
96
97   if (gltrackball_event_handler (event, cp->trackball,
98                                  MI_WIDTH (mi), MI_HEIGHT (mi),
99                                  &cp->button_down_p))
100     return True;
101
102   return False;
103 }
104
105
106 static Bool draw_main(cubicgrid_conf *cp) 
107 {
108   double x, y, z;
109
110   glClear(GL_COLOR_BUFFER_BIT);
111   glLoadIdentity();
112
113   glRotatef (180, 1, 0, 0);  /* Make trackball track the right way */
114   glRotatef (180, 0, 1, 0);
115
116   glTranslatef(0, 0, zpos);
117
118   glScalef(size/ticks, size/ticks, size/ticks);
119
120   gltrackball_rotate (cp->trackball);
121
122   get_rotation (cp->rot, &x, &y, &z, !cp->button_down_p);
123   glRotatef (-x * 360, 1.0, 0.0, 0.0);
124   glRotatef (-y * 360, 0.0, 1.0, 0.0);
125   glRotatef (-z * 360, 0.0, 0.0, 1.0);
126
127   glTranslatef(-ticks/2.0, -ticks/2.0, -ticks/2.0);
128   glCallList(cp->list);
129   return True;
130 }
131
132 static void init_gl(ModeInfo *mi) 
133 {
134   cubicgrid_conf *cp = &cubicgrid[MI_SCREEN(mi)];
135   int x, y, z;
136   float tf = ticks;
137
138   glDrawBuffer(GL_BACK);
139   if(bigdots) {
140     glPointSize(2.0);
141   }
142   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
143   glShadeModel(GL_FLAT);
144
145   cp->list = glGenLists(1);
146   glNewList(cp->list, GL_COMPILE);
147   if(MI_IS_MONO(mi)) {
148     glColor3f(1.0, 1.0, 1.0);
149     glBegin(GL_POINTS);
150     for(x = 0; x < ticks; x++) {
151       for(y = 0; y < ticks; y++) {
152         for(z = 0; z < ticks; z++) {
153           glVertex3f(x, y, z);
154           cp->npoints++;
155         }
156       }
157     }
158     glEnd();
159   }
160   else
161   {
162     glBegin(GL_POINTS);
163     for(x = 0; x < ticks; x++) {
164       for(y = 0; y < ticks; y++) {
165         for(z = 0; z < ticks; z++) {
166           glColor3f(x/tf, y/tf, z/tf);
167           glVertex3f(x, y, z);
168           cp->npoints++;
169         }
170       }
171     }
172     glEnd();
173   }
174   glEndList();
175 }
176
177 /*************************************************************************/
178
179 ENTRYPOINT void reshape_cubicgrid(ModeInfo *mi, int width, int height) 
180 {
181   cubicgrid_conf *cp = &cubicgrid[MI_SCREEN(mi)];
182   if(!height) height = 1;
183   cp->ratio = (GLfloat)width/(GLfloat)height;
184   glViewport(0, 0, (GLint) width, (GLint) height);
185   glMatrixMode(GL_PROJECTION);
186   glLoadIdentity();
187   gluPerspective(30.0, cp->ratio, 1.0, 100.0);
188   glMatrixMode(GL_MODELVIEW);
189   glClear(GL_COLOR_BUFFER_BIT);
190 }
191
192 ENTRYPOINT void release_cubicgrid(ModeInfo *mi) 
193 {
194   if (cubicgrid != NULL) {
195     int screen;
196     for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
197       cubicgrid_conf *cp = &cubicgrid[screen];
198       if (cp->glx_context) {
199         cp->glx_context = NULL;
200       }
201     }
202     free((void *)cubicgrid);
203     cubicgrid = NULL;
204   }
205   FreeAllGL(mi);
206 }
207
208 ENTRYPOINT void init_cubicgrid(ModeInfo *mi) 
209 {
210   cubicgrid_conf *cp;
211   if(!cubicgrid) {
212     cubicgrid = (cubicgrid_conf *)calloc(MI_NUM_SCREENS(mi), sizeof(cubicgrid_conf));
213     if(!cubicgrid) return;
214   }
215   cp = &cubicgrid[MI_SCREEN(mi)];
216
217   if ((cp->glx_context = init_GL(mi)) != NULL) {
218     init_gl(mi);
219     reshape_cubicgrid(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
220   } else {
221     MI_CLEARWINDOW(mi);
222   }
223
224   {
225     double spin_speed = 0.045 * speed;
226     double spin_accel = 0.005 * speed;
227
228     cp->rot = make_rotator (spin_speed, spin_speed, spin_speed,
229                             spin_accel, 0, True);
230     cp->trackball = gltrackball_init (True);
231   }
232 }
233
234 ENTRYPOINT void draw_cubicgrid(ModeInfo * mi) 
235 {
236   Display *display = MI_DISPLAY(mi);
237   Window window = MI_WINDOW(mi);
238   cubicgrid_conf *cp;
239   if (!cubicgrid) return;
240   cp = &cubicgrid[MI_SCREEN(mi)];
241   MI_IS_DRAWN(mi) = True;
242   if (!cp->glx_context) return;
243   glXMakeCurrent(display, window, *(cp->glx_context));
244   if (!draw_main(cp)) {
245     release_cubicgrid(mi);
246     return;
247   }
248   mi->polygon_count = cp->npoints;
249   if (MI_IS_FPS(mi)) do_fps (mi);
250   glFlush();
251   glXSwapBuffers(display, window);
252 }
253
254 #ifndef STANDALONE
255 ENTRYPOINT void change_cubicgrid(ModeInfo * mi) 
256 {
257   cubicgrid_conf *cp = &cubicgrid[MI_SCREEN(mi)];
258   if (!cp->glx_context) return;
259   glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(cp->glx_context));
260   init_gl(mi);
261 }
262 #endif /* !STANDALONE */
263
264
265 XSCREENSAVER_MODULE ("CubicGrid", cubicgrid)
266
267 #endif