1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* atunnel --- OpenGL Advanced Tunnel Screensaver */
5 static const char sccsid[] = "@(#)atunnel.c 5.13 2004/05/25 xlockmore";
8 /* Copyright (c) E. Lassauge, 2003-2004. */
11 * Permission to use, copy, modify, and distribute this software and its
12 * documentation for any purpose and without fee is hereby granted,
13 * provided that the above copyright notice appear in all copies and that
14 * both that copyright notice and this permission notice appear in
15 * supporting documentation.
17 * This file is provided AS IS with no warranties of any kind. The author
18 * shall have no liability with respect to the infringement of copyrights,
19 * trade secrets or any patents by this file or any part thereof. In no
20 * event will the author be liable for any lost revenue or profits or
21 * other special, indirect and consequential damages.
23 * The original code for this mode was written by Roman Podobedov
25 * WEB: http://romka.demonews.com
27 * Eric Lassauge (May-25-2004) <lassauge@users.sourceforge.net>
28 * http://lassauge.free.fr/linux.html
32 * E.Lassauge - 25-May-2004:
33 * - added more texture !
34 * E.Lassauge - 16-Mar-2002:
35 * - created based on the Roman demo.
36 * - deleted all external file stuff to use xpm textures and
37 * hardcoded path point values.
41 #ifdef STANDALONE /* xscreensaver mode */
42 #define DEFAULTS "*delay: 10000 \n" \
43 "*showFPS: False \n" \
44 "*suppressRotationAnimation: True\n" \
46 # define refresh_atunnel 0
47 # define release_atunnel 0
48 # define atunnel_handle_event 0
50 # include "xlockmore.h" /* from the xscreensaver distribution */
51 #else /* !STANDALONE */
52 # include "xlock.h" /* from the xlockmore distribution */
53 #endif /* !STANDALONE */
55 #ifdef MODE_atunnel /* whole file */
60 #include "tunnel_draw.h"
62 #if defined( USE_XPM ) || defined( USE_XPMINC ) || defined(STANDALONE)
63 /* USE_XPM & USE_XPMINC in xlock mode ; STANDALONE in xscreensaver mode */
64 #include "xpm-ximage.h"
68 #include "../images/tunnel0.xpm"
69 #include "../images/tunnel1.xpm"
70 #include "../images/tunnel2.xpm"
71 #include "../images/tunnel3.xpm"
72 #include "../images/tunnel4.xpm"
73 #include "../images/tunnel5.xpm"
74 #else /* !STANDALONE */
75 #include "pixmaps/tunnel0.xpm"
76 #include "pixmaps/tunnel1.xpm"
77 #include "pixmaps/tunnel2.xpm"
78 #include "pixmaps/tunnel3.xpm"
79 #include "pixmaps/tunnel4.xpm"
80 #include "pixmaps/tunnel5.xpm"
81 #endif /* !STANDALONE */
86 #define countof(x) (sizeof((x))/sizeof((*x)))
88 #define DEF_LIGHT "True"
89 #define DEF_WIRE "False"
90 #define DEF_TEXTURE "True"
94 static Bool do_texture;
96 static XrmOptionDescRec opts[] = {
97 {"-light", ".atunnel.light", XrmoptionNoArg, "true" },
98 {"+light", ".atunnel.light", XrmoptionNoArg, "false" },
99 {"-wireframe",".atunnel.wire", XrmoptionNoArg, "true" },
100 {"+wireframe",".atunnel.wire", XrmoptionNoArg, "false" },
101 {"-texture", ".atunnel.texture", XrmoptionNoArg, "true" },
102 {"+texture", ".atunnel.texture", XrmoptionNoArg, "false" },
105 static argtype vars[] = {
106 {&do_light, "light", "Light", DEF_LIGHT, t_Bool},
107 {&do_wire, "wire", "Wire", DEF_WIRE, t_Bool},
108 {&do_texture, "texture", "Texture", DEF_TEXTURE, t_Bool},
111 static OptionStruct desc[] =
113 {"-/+ light", "whether to do enable lighting (slower)"},
114 {"-/+ wire", "whether to do use wireframe instead of filled (faster)"},
115 {"-/+ texture", "whether to apply a texture (slower)"},
118 ENTRYPOINT ModeSpecOpt atunnel_opts = {countof(opts), opts, countof(vars), vars, desc};
121 ModStruct atunnel_description =
122 {"atunnel", "init_atunnel", "draw_atunnel", NULL,
123 "draw_atunnel", "init_atunnel", NULL, &atunnel_opts,
124 1000, 1, 2, 1, 4, 1.0, "",
125 "OpenGL advanced tunnel screensaver", 0, NULL};
128 /* structure for holding the screensaver data */
130 int screen_width, screen_height;
131 GLXContext *glx_context;
133 struct tunnel_state *ts;
134 GLuint texture[MAX_TEXTURE]; /* texture id: GL world */
137 static atunnelstruct *Atunnel = NULL;
139 /*=================== Load Texture =========================================*/
140 static void LoadTexture(ModeInfo * mi, char **fn, int t_num)
142 #if defined( I_HAVE_XPM )
143 atunnelstruct *sa = &Atunnel[MI_SCREEN(mi)];
144 XImage *teximage; /* Texture data */
146 if ((teximage = xpm_to_ximage(MI_DISPLAY(mi), MI_VISUAL(mi),
147 MI_COLORMAP(mi), fn)) == None) {
148 (void) fprintf(stderr, "Error reading the texture.\n");
149 glDeleteTextures(1, &sa->texture[t_num]);
158 #ifdef HAVE_GLBINDTEXTURE
159 glBindTexture(GL_TEXTURE_2D, sa->texture[t_num]);
160 #endif /* HAVE_GLBINDTEXTURE */
161 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
163 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, teximage->width, teximage->height,
165 /* GL_UNSIGNED_BYTE, */
166 GL_UNSIGNED_INT_8_8_8_8_REV,
168 check_gl_error("texture");
170 /* Texture parameters, LINEAR scaling for better texture quality */
171 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
172 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
174 XDestroyImage(teximage);
175 #else /* !I_HAVE_XPM */
177 #endif /* !I_HAVE_XPM */
180 /*=================== Main Initialization ==================================*/
181 static void Init(ModeInfo * mi)
183 atunnelstruct *sa = &Atunnel[MI_SCREEN(mi)];
184 GLfloat light_ambient[] = {1.0, 1.0, 1.0, 1.0};
185 GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0};
186 GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0};
187 GLfloat light_position[] = {0.0, 0.0, 1.0, 0.0};
188 GLfloat fogColor[4] = {0.8, 0.8, 0.8, 1.0};
192 glGenTextures(MAX_TEXTURE, sa->texture);
193 LoadTexture(mi, texture0,0);
194 LoadTexture(mi, texture1,1);
195 LoadTexture(mi, texture2,2);
196 LoadTexture(mi, texture3,3);
197 LoadTexture(mi, texture4,4);
198 LoadTexture(mi, texture5,5);
199 glEnable(GL_TEXTURE_2D);
201 sa->ts = atunnel_InitTunnel();
203 /* Set lighting parameters */
206 glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
207 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
208 glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
209 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
213 glDepthFunc(GL_LESS);
215 glEnable(GL_LIGHTING);
218 # ifdef HAVE_JWZGLES /* #### glPolygonMode other than GL_FILL unimplemented */
223 glDisable(GL_NORMALIZE);
224 glDisable(GL_CULL_FACE);
225 glDisable(GL_DEPTH_TEST);
226 glDisable(GL_TEXTURE_2D);
227 glPolygonMode(GL_FRONT,GL_LINE);
228 glPolygonMode(GL_BACK,GL_LINE);
232 glEnable(GL_DEPTH_TEST);
233 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
236 glFogi(GL_FOG_MODE, GL_EXP);
237 glFogfv(GL_FOG_COLOR, fogColor);
238 glFogf(GL_FOG_DENSITY, 0.3);
242 glCullFace(GL_FRONT);
243 glEnable(GL_CULL_FACE);
246 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
250 /* Standard reshape function */
252 reshape_atunnel(ModeInfo *mi, int width, int height)
256 glViewport(0, 0, width, height);
257 glMatrixMode(GL_PROJECTION);
259 a = (float)width/(float)height;
260 glFrustum(-0.1*a, 0.1*a, -0.1, 0.1, 0.1, 10);
261 glMatrixMode(GL_MODELVIEW);
264 /* draw the screensaver once */
265 ENTRYPOINT void draw_atunnel(ModeInfo * mi)
267 atunnelstruct *sa = &Atunnel[MI_SCREEN(mi)];
268 Display *display = MI_DISPLAY(mi);
269 Window window = MI_WINDOW(mi);
271 if (!sa->glx_context)
274 glXMakeCurrent(display, window, *(sa->glx_context));
276 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
280 atunnel_DrawTunnel(sa->ts, do_texture, do_light, sa->texture);
281 atunnel_SplashScreen(sa->ts, do_wire, do_texture, do_light);
284 /* manage framerate display */
285 if (MI_IS_FPS(mi)) do_fps (mi);
286 glXSwapBuffers(display, window);
291 static void free_atunnel(ModeInfo * mi);
293 /* xscreensaver initialization routine */
294 ENTRYPOINT void init_atunnel(ModeInfo * mi)
296 int screen = MI_SCREEN(mi);
299 MI_INIT(mi, Atunnel, free_atunnel);
300 sa = &Atunnel[screen];
302 sa->window = MI_WINDOW(mi);
303 if ((sa->glx_context = init_GL(mi)) != NULL) {
304 reshape_atunnel(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
312 /* all sorts of nice cleanup code should go here! */
313 static void free_atunnel(ModeInfo * mi)
316 atunnelstruct *sa = &Atunnel[MI_SCREEN(mi)];
321 XSCREENSAVER_MODULE ("Atunnel", atunnel)