1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* atunnels --- OpenGL Advanced Tunnel Screensaver */
5 static const char sccsid[] = "@(#)atunnels.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 PROGCLASS "Atunnels"
43 # define HACK_INIT init_atunnels
44 # define HACK_DRAW draw_atunnels
45 # define HACK_RESHAPE reshape_atunnels
46 # define atunnels_opts xlockmore_opts
47 #define DEFAULTS "*delay: 10000 \n" \
48 "*showFPS: False \n" \
51 # include "xlockmore.h" /* from the xscreensaver distribution */
52 #else /* !STANDALONE */
53 # include "xlock.h" /* from the xlockmore distribution */
54 #endif /* !STANDALONE */
56 #ifdef MODE_atunnels /* whole file */
61 #include "tunnel_draw.h"
63 #if defined( USE_XPM ) || defined( USE_XPMINC ) || defined(STANDALONE)
64 /* USE_XPM & USE_XPMINC in xlock mode ; STANDALONE in xscreensaver mode */
65 #include "xpm-ximage.h"
69 #include "../images/tunnel0.xpm"
70 #include "../images/tunnel1.xpm"
71 #include "../images/tunnel2.xpm"
72 #include "../images/tunnel3.xpm"
73 #include "../images/tunnel4.xpm"
74 #include "../images/tunnel5.xpm"
75 #else /* !STANDALONE */
76 #include "pixmaps/tunnel0.xpm"
77 #include "pixmaps/tunnel1.xpm"
78 #include "pixmaps/tunnel2.xpm"
79 #include "pixmaps/tunnel3.xpm"
80 #include "pixmaps/tunnel4.xpm"
81 #include "pixmaps/tunnel5.xpm"
82 #endif /* !STANDALONE */
87 #define countof(x) (sizeof((x))/sizeof((*x)))
89 #define DEF_LIGHT "True"
90 #define DEF_WIRE "False"
91 #define DEF_TEXTURE "True"
95 static Bool do_texture;
97 static XrmOptionDescRec opts[] = {
98 {"-light", ".atunnels.light", XrmoptionNoArg, "true" },
99 {"+light", ".atunnels.light", XrmoptionNoArg, "false" },
100 {"-wire", ".atunnels.wire", XrmoptionNoArg, "true" },
101 {"+wire", ".atunnels.wire", XrmoptionNoArg, "false" },
102 {"-texture", ".atunnels.texture", XrmoptionNoArg, "true" },
103 {"+texture", ".atunnels.texture", XrmoptionNoArg, "false" },
106 static argtype vars[] = {
107 {&do_light, "light", "Light", DEF_LIGHT, t_Bool},
108 {&do_wire, "wire", "Wire", DEF_WIRE, t_Bool},
109 {&do_texture, "texture", "Texture", DEF_TEXTURE, t_Bool},
112 static OptionStruct desc[] =
114 {"-/+ light", "whether to do enable lighting (slower)"},
115 {"-/+ wire", "whether to do use wireframe instead of filled (faster)"},
116 {"-/+ texture", "whether to apply a texture (slower)"},
119 ModeSpecOpt atunnels_opts = {countof(opts), opts, countof(vars), vars, desc};
122 ModStruct atunnels_description =
123 {"atunnels", "init_atunnels", "draw_atunnels", "release_atunnels",
124 "draw_atunnels", "init_atunnels", NULL, &atunnels_opts,
125 1000, 1, 2, 1, 4, 1.0, "",
126 "OpenGL advanced tunnel screensaver", 0, NULL};
129 /* structure for holding the screensaver data */
131 int screen_width, screen_height;
132 GLXContext *glx_context;
136 static atunnelsstruct *Atunnels = NULL;
138 static GLuint texture[MAX_TEXTURE]; /* texture id: GL world */
140 /*=================== Load Texture =========================================*/
141 static void LoadTexture(ModeInfo * mi, char **fn, int t_num)
143 #if defined( I_HAVE_XPM )
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, &texture[t_num]);
158 #ifdef HAVE_GLBINDTEXTURE
159 glBindTexture(GL_TEXTURE_2D, 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,
164 0, GL_RGBA, GL_UNSIGNED_BYTE, teximage->data);
165 check_gl_error("texture");
167 /* Texture parameters, LINEAR scaling for better texture quality */
168 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
169 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
171 XDestroyImage(teximage);
172 #else /* !I_HAVE_XPM */
174 #endif /* !I_HAVE_XPM */
177 /*=================== Main Initialization ==================================*/
178 static void Init(ModeInfo * mi)
180 GLfloat light_ambient[] = {1.0, 1.0, 1.0, 1.0};
181 GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0};
182 GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0};
183 GLfloat light_position[] = {0.0, 0.0, 1.0, 0.0};
184 GLfloat fogColor[4] = {0.8, 0.8, 0.8, 1.0};
186 glClearColor(0, 0, 0, 0);
189 glGenTextures(MAX_TEXTURE, texture);
190 LoadTexture(mi, texture0,0);
191 LoadTexture(mi, texture1,1);
192 LoadTexture(mi, texture2,2);
193 LoadTexture(mi, texture3,3);
194 LoadTexture(mi, texture4,4);
195 LoadTexture(mi, texture5,5);
196 glEnable(GL_TEXTURE_2D);
200 /* Set lighting parameters */
203 glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
204 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
205 glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
206 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
210 glDepthFunc(GL_LESS);
212 glEnable(GL_LIGHTING);
215 glDisable(GL_NORMALIZE);
216 glDisable(GL_CULL_FACE);
217 glDisable(GL_DEPTH_TEST);
218 glDisable(GL_TEXTURE_2D);
219 glPolygonMode(GL_FRONT,GL_LINE);
220 glPolygonMode(GL_BACK,GL_LINE);
224 glEnable(GL_DEPTH_TEST);
225 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
228 glFogi(GL_FOG_MODE, GL_EXP);
229 glFogfv(GL_FOG_COLOR, fogColor);
230 glFogf(GL_FOG_DENSITY, 0.3);
234 glCullFace(GL_FRONT);
235 glEnable(GL_CULL_FACE);
238 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
242 /* Standard reshape function */
244 reshape_atunnels(ModeInfo *mi, int width, int height)
248 glViewport(0, 0, width, height);
249 glMatrixMode(GL_PROJECTION);
251 a = (float)width/(float)height;
252 glFrustum(-0.1*a, 0.1*a, -0.1, 0.1, 0.1, 10);
253 glMatrixMode(GL_MODELVIEW);
256 /* draw the screensaver once */
257 void draw_atunnels(ModeInfo * mi)
259 atunnelsstruct *sa = &Atunnels[MI_SCREEN(mi)];
260 Display *display = MI_DISPLAY(mi);
261 Window window = MI_WINDOW(mi);
263 if (!sa->glx_context)
266 glXMakeCurrent(display, window, *(sa->glx_context));
268 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
272 DrawTunnel(do_texture, do_light, texture);
273 SplashScreen(do_wire, do_texture, do_light);
276 /* manage framerate display */
277 if (MI_IS_FPS(mi)) do_fps (mi);
278 glXSwapBuffers(display, window);
283 /* xscreensaver initialization routine */
284 void init_atunnels(ModeInfo * mi)
286 int screen = MI_SCREEN(mi);
289 if (Atunnels == NULL) {
290 if ((Atunnels = (atunnelsstruct *) calloc(MI_NUM_SCREENS(mi), sizeof (atunnelsstruct))) == NULL)
293 sa = &Atunnels[screen];
295 sa->window = MI_WINDOW(mi);
296 if ((sa->glx_context = init_GL(mi)) != NULL) {
297 reshape_atunnels(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
305 /* all sorts of nice cleanup code should go here! */
306 void release_atunnels(ModeInfo * mi)
309 if (Atunnels != NULL) {
310 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
311 /* atunnelsstruct *sa = &Atunnels[screen];*/
313 (void) free((void *) Atunnels);