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.
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.
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.
18 * Vasek Potocek (Dec-28-2007)
19 * vasek.potocek@post.cz
22 #define DEFAULTS "*delay: 20000 \n" \
23 "*showFPS: False \n" \
24 "*wireframe: False \n"
26 # define refresh_cubicgrid 0
27 #include "xlockmore.h"
31 #define DEF_SPEED "1.0"
34 #define DEF_BIGDOTS "True"
37 #define countof(x) (sizeof((x))/sizeof((*x)))
40 #include "gltrackball.h"
42 /*************************************************************************/
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 },
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" },
64 ENTRYPOINT ModeSpecOpt cubicgrid_opts = {countof(opts), opts, countof(vars), vars, NULL};
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
76 GLXContext *glx_context;
81 trackball_state *trackball;
86 static cubicgrid_conf *cubicgrid = NULL;
88 static const GLfloat zpos = -18.0;
90 /*************************************************************************/
93 cubicgrid_handle_event (ModeInfo *mi, XEvent *event)
95 cubicgrid_conf *cp = &cubicgrid[MI_SCREEN(mi)];
97 if (gltrackball_event_handler (event, cp->trackball,
98 MI_WIDTH (mi), MI_HEIGHT (mi),
106 static Bool draw_main(cubicgrid_conf *cp)
110 glClear(GL_COLOR_BUFFER_BIT);
113 glRotatef (180, 1, 0, 0); /* Make trackball track the right way */
114 glRotatef (180, 0, 1, 0);
116 glTranslatef(0, 0, zpos);
118 glScalef(size/ticks, size/ticks, size/ticks);
120 gltrackball_rotate (cp->trackball);
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);
127 glTranslatef(-ticks/2.0, -ticks/2.0, -ticks/2.0);
128 glCallList(cp->list);
132 static void init_gl(ModeInfo *mi)
134 cubicgrid_conf *cp = &cubicgrid[MI_SCREEN(mi)];
138 glDrawBuffer(GL_BACK);
142 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
143 glShadeModel(GL_FLAT);
145 cp->list = glGenLists(1);
146 glNewList(cp->list, GL_COMPILE);
148 glColor3f(1.0, 1.0, 1.0);
150 for(x = 0; x < ticks; x++) {
151 for(y = 0; y < ticks; y++) {
152 for(z = 0; z < ticks; z++) {
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);
177 /*************************************************************************/
179 ENTRYPOINT void reshape_cubicgrid(ModeInfo *mi, int width, int height)
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);
187 gluPerspective(30.0, cp->ratio, 1.0, 100.0);
188 glMatrixMode(GL_MODELVIEW);
189 glClear(GL_COLOR_BUFFER_BIT);
192 ENTRYPOINT void release_cubicgrid(ModeInfo *mi)
194 if (cubicgrid != NULL) {
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;
202 free((void *)cubicgrid);
208 ENTRYPOINT void init_cubicgrid(ModeInfo *mi)
212 cubicgrid = (cubicgrid_conf *)calloc(MI_NUM_SCREENS(mi), sizeof(cubicgrid_conf));
213 if(!cubicgrid) return;
215 cp = &cubicgrid[MI_SCREEN(mi)];
217 if ((cp->glx_context = init_GL(mi)) != NULL) {
219 reshape_cubicgrid(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
225 double spin_speed = 0.045 * speed;
226 double spin_accel = 0.005 * speed;
228 cp->rot = make_rotator (spin_speed, spin_speed, spin_speed,
229 spin_accel, 0, True);
230 cp->trackball = gltrackball_init (True);
234 ENTRYPOINT void draw_cubicgrid(ModeInfo * mi)
236 Display *display = MI_DISPLAY(mi);
237 Window window = MI_WINDOW(mi);
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);
248 mi->polygon_count = cp->npoints;
249 if (MI_IS_FPS(mi)) do_fps (mi);
251 glXSwapBuffers(display, window);
255 ENTRYPOINT void change_cubicgrid(ModeInfo * mi)
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));
262 #endif /* !STANDALONE */
265 XSCREENSAVER_MODULE ("CubicGrid", cubicgrid)