http://www.tienza.es/crux/src/www.jwz.org/xscreensaver/xscreensaver-5.05.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_SIZE    "20.0"
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_SIZE,    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 } cubicgrid_conf;
84
85 static cubicgrid_conf *cubicgrid = NULL;
86
87 static const GLfloat zpos = -18.0;
88
89 /*************************************************************************/
90
91 ENTRYPOINT Bool
92 cubicgrid_handle_event (ModeInfo *mi, XEvent *event)
93 {
94   cubicgrid_conf *cp = &cubicgrid[MI_SCREEN(mi)];
95
96   if (event->xany.type == ButtonPress &&
97       event->xbutton.button == Button1)
98     {
99       cp->button_down_p = True;
100       gltrackball_start (cp->trackball,
101                          event->xbutton.x, event->xbutton.y,
102                          MI_WIDTH (mi), MI_HEIGHT (mi));
103       return True;
104     }
105   else if (event->xany.type == ButtonRelease &&
106            event->xbutton.button == Button1)
107     {
108       cp->button_down_p = False;
109       return True;
110     }
111   else if (event->xany.type == ButtonPress &&
112            (event->xbutton.button == Button4 ||
113             event->xbutton.button == Button5 ||
114             event->xbutton.button == Button6 ||
115             event->xbutton.button == Button7))
116     {
117       gltrackball_mousewheel (cp->trackball, event->xbutton.button, 2,
118                               !!event->xbutton.state);
119       return True;
120     }
121   else if (event->xany.type == MotionNotify &&
122            cp->button_down_p)
123     {
124       gltrackball_track (cp->trackball,
125                          event->xmotion.x, event->xmotion.y,
126                          MI_WIDTH (mi), MI_HEIGHT (mi));
127       return True;
128     }
129
130   return False;
131 }
132
133
134 static Bool draw_main(cubicgrid_conf *cp) 
135 {
136   double x, y, z;
137
138   glClear(GL_COLOR_BUFFER_BIT);
139   glLoadIdentity();
140   glTranslatef(0, 0, zpos);
141
142   glScalef(size/ticks, size/ticks, size/ticks);
143   gltrackball_rotate (cp->trackball);
144   get_rotation (cp->rot, &x, &y, &z, !cp->button_down_p);
145   glRotatef (x * 360, 1.0, 0.0, 0.0);
146   glRotatef (y * 360, 0.0, 1.0, 0.0);
147   glRotatef (z * 360, 0.0, 0.0, 1.0);
148
149   glTranslatef(-ticks/2.0, -ticks/2.0, -ticks/2.0);
150   glCallList(cp->list);
151
152   return True;
153 }
154
155 static void init_gl(ModeInfo *mi) 
156 {
157   cubicgrid_conf *cp = &cubicgrid[MI_SCREEN(mi)];
158   int x, y, z;
159   float tf = ticks;
160
161   glClearColor(0.0, 0.0, 0.0, 1.0);
162   glDrawBuffer(GL_BACK);
163   if(bigdots) {
164     glPointSize(2.0);
165   }
166   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
167   glShadeModel(GL_FLAT);
168
169   cp->list = glGenLists(1);
170   glNewList(cp->list, GL_COMPILE);
171   if(MI_IS_MONO(mi)) {
172     glColor3f(1.0, 1.0, 1.0);
173     glBegin(GL_POINTS);
174     for(x = 0; x < ticks; x++) {
175       for(y = 0; y < ticks; y++) {
176         for(z = 0; z < ticks; z++) {
177           glVertex3f(x, y, z);
178         }
179       }
180     }
181     glEnd();
182   }
183   else
184   {
185     glBegin(GL_POINTS);
186     for(x = 0; x < ticks; x++) {
187       for(y = 0; y < ticks; y++) {
188         for(z = 0; z < ticks; z++) {
189           glColor3f(x/tf, y/tf, z/tf);
190           glVertex3f(x, y, z);
191         }
192       }
193     }
194     glEnd();
195   }
196   glEndList();
197 }
198
199 /*************************************************************************/
200
201 ENTRYPOINT void reshape_cubicgrid(ModeInfo *mi, int width, int height) 
202 {
203   cubicgrid_conf *cp = &cubicgrid[MI_SCREEN(mi)];
204   if(!height) height = 1;
205   cp->ratio = (GLfloat)width/(GLfloat)height;
206   glViewport(0, 0, (GLint) width, (GLint) height);
207   glMatrixMode(GL_PROJECTION);
208   glLoadIdentity();
209   gluPerspective(30.0, cp->ratio, 1.0, 100.0);
210   glMatrixMode(GL_MODELVIEW);
211   glClear(GL_COLOR_BUFFER_BIT);
212 }
213
214 ENTRYPOINT void release_cubicgrid(ModeInfo *mi) 
215 {
216   if (cubicgrid != NULL) {
217     int screen;
218     for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
219       cubicgrid_conf *cp = &cubicgrid[screen];
220       if (cp->glx_context) {
221         cp->glx_context = NULL;
222       }
223     }
224     free((void *)cubicgrid);
225     cubicgrid = NULL;
226   }
227   FreeAllGL(mi);
228 }
229
230 ENTRYPOINT void init_cubicgrid(ModeInfo *mi) 
231 {
232   cubicgrid_conf *cp;
233   if(!cubicgrid) {
234     cubicgrid = (cubicgrid_conf *)calloc(MI_NUM_SCREENS(mi), sizeof(cubicgrid_conf));
235     if(!cubicgrid) return;
236   }
237   cp = &cubicgrid[MI_SCREEN(mi)];
238
239   if ((cp->glx_context = init_GL(mi)) != NULL) {
240     init_gl(mi);
241     reshape_cubicgrid(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
242   } else {
243     MI_CLEARWINDOW(mi);
244   }
245
246   {
247     double spin_speed = 0.045 * speed;
248     double spin_accel = 0.005 * speed;
249
250     cp->rot = make_rotator (spin_speed, spin_speed, spin_speed,
251                             spin_accel, 0, True);
252     cp->trackball = gltrackball_init ();
253   }
254 }
255
256 ENTRYPOINT void draw_cubicgrid(ModeInfo * mi) 
257 {
258   Display *display = MI_DISPLAY(mi);
259   Window window = MI_WINDOW(mi);
260   cubicgrid_conf *cp;
261   if (!cubicgrid) return;
262   cp = &cubicgrid[MI_SCREEN(mi)];
263   MI_IS_DRAWN(mi) = True;
264   if (!cp->glx_context) return;
265   glXMakeCurrent(display, window, *(cp->glx_context));
266   if (!draw_main(cp)) {
267     release_cubicgrid(mi);
268     return;
269   }
270   if (MI_IS_FPS(mi)) do_fps (mi);
271   glFlush();
272   glXSwapBuffers(display, window);
273 }
274
275 #ifndef STANDALONE
276 ENTRYPOINT void change_cubicgrid(ModeInfo * mi) 
277 {
278   cubicgrid_conf *cp = &cubicgrid[MI_SCREEN(mi)];
279   if (!cp->glx_context) return;
280   glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(cp->glx_context));
281   init_gl(mi);
282 }
283 #endif /* !STANDALONE */
284
285
286 XSCREENSAVER_MODULE ("CubicGrid", cubicgrid)
287
288 #endif