ftp://ftp.krokus.ru/pub/OpenBSD/distfiles/xscreensaver-4.21.tar.gz
[xscreensaver] / hacks / glx / atunnel.c
1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* atunnels --- OpenGL Advanced Tunnel Screensaver */
3
4 #if 0
5 static const char sccsid[] = "@(#)atunnels.c    5.13 2004/05/25 xlockmore";
6 #endif
7
8 /* Copyright (c) E. Lassauge, 2003-2004. */
9
10 /*
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.
16  *
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.
22  *
23  * The original code for this mode was written by Roman Podobedov
24  * Email: romka@ut.ee
25  * WEB: http://romka.demonews.com
26  *
27  * Eric Lassauge  (May-25-2004) <lassauge@users.sourceforge.net>
28  *                                  http://lassauge.free.fr/linux.html
29  *
30  * REVISION HISTORY:
31  *
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.
38  *
39  */
40
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" \
49
50 #define MODE_atunnels
51 # include "xlockmore.h"         /* from the xscreensaver distribution */
52 #else                           /* !STANDALONE */
53 # include "xlock.h"             /* from the xlockmore distribution */
54 #endif                          /* !STANDALONE */
55
56 #ifdef MODE_atunnels /* whole file */
57
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <math.h>
61 #include "tunnel_draw.h"
62
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"
66 #define I_HAVE_XPM
67
68 #ifdef STANDALONE
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 */
83 #endif /* HAVE_XPM */
84
85
86 #undef countof
87 #define countof(x) (sizeof((x))/sizeof((*x)))
88
89 #define DEF_LIGHT       "True"
90 #define DEF_WIRE        "False"
91 #define DEF_TEXTURE     "True"
92
93 static Bool do_light;
94 static Bool do_wire;
95 static Bool do_texture;
96
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" },
104 };
105
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},
110 };
111
112 static OptionStruct desc[] =
113 {
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)"},
117 };
118
119 ModeSpecOpt atunnels_opts = {countof(opts), opts, countof(vars), vars, desc};
120
121 #ifdef USE_MODULES
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};
127 #endif
128
129 /* structure for holding the screensaver data */
130 typedef struct {
131   int screen_width, screen_height;
132   GLXContext *glx_context;
133   Window window;
134 } atunnelsstruct;
135
136 static atunnelsstruct *Atunnels = NULL;
137
138 static GLuint texture[MAX_TEXTURE]; /* texture id: GL world */
139
140 /*=================== Load Texture =========================================*/
141 static void LoadTexture(ModeInfo * mi, char **fn, int t_num)
142 {
143 #if defined( I_HAVE_XPM )
144         XImage *teximage;    /* Texture data */
145  
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]);
150             do_texture = False;
151 #ifdef STANDALONE
152             exit(0);
153 #else
154             return;
155 #endif
156         }
157
158 #ifdef HAVE_GLBINDTEXTURE
159         glBindTexture(GL_TEXTURE_2D, texture[t_num]);
160 #endif /* HAVE_GLBINDTEXTURE */
161         glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
162         clear_gl_error();
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");
166
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); 
170
171         XDestroyImage(teximage);
172 #else /* !I_HAVE_XPM */
173         do_texture = False;
174 #endif /* !I_HAVE_XPM */
175
176
177 /*=================== Main Initialization ==================================*/
178 static void Init(ModeInfo * mi)
179 {
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};
185
186         glClearColor(0, 0, 0, 0);
187         if (do_texture)
188         {
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);
197         }
198         InitTunnel();
199         
200         /* Set lighting parameters */
201         if (do_light)
202         {
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);
207
208                 /* Enable light 0 */
209                 glEnable(GL_LIGHT0);
210                 glDepthFunc(GL_LESS);
211         
212                 glEnable(GL_LIGHTING);
213         }
214         if (do_wire) {
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);
221         }
222         else
223         {
224                 glEnable(GL_DEPTH_TEST);
225                 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
226
227                 /* Enable fog */
228                 glFogi(GL_FOG_MODE, GL_EXP);
229                 glFogfv(GL_FOG_COLOR, fogColor);
230                 glFogf(GL_FOG_DENSITY, 0.3);
231                 glEnable(GL_FOG);
232         
233                 /* Cull face */
234                 glCullFace(GL_FRONT);
235                 glEnable(GL_CULL_FACE);
236         }
237         
238         glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
239 }
240
241
242 /* Standard reshape function */
243 void
244 reshape_atunnels(ModeInfo *mi, int width, int height)
245 {
246         float a;
247
248         glViewport(0, 0, width, height);
249         glMatrixMode(GL_PROJECTION);
250         glLoadIdentity();
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);
254 }
255
256 /* draw the screensaver once */
257 void draw_atunnels(ModeInfo * mi)
258 {
259         atunnelsstruct *sa = &Atunnels[MI_SCREEN(mi)];
260         Display    *display = MI_DISPLAY(mi);
261         Window      window = MI_WINDOW(mi);
262
263         if (!sa->glx_context)
264                 return;
265
266         glXMakeCurrent(display, window, *(sa->glx_context));
267
268         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
269
270         glLoadIdentity();
271
272         DrawTunnel(do_texture, do_light, texture);
273         SplashScreen(do_wire, do_texture, do_light);
274
275         glFlush();  
276         /* manage framerate display */
277         if (MI_IS_FPS(mi)) do_fps (mi);
278         glXSwapBuffers(display, window);
279
280 }
281
282
283 /* xscreensaver initialization routine */
284 void init_atunnels(ModeInfo * mi)
285 {
286   int screen = MI_SCREEN(mi);
287   atunnelsstruct *sa;
288
289   if (Atunnels == NULL) {
290         if ((Atunnels = (atunnelsstruct *) calloc(MI_NUM_SCREENS(mi), sizeof (atunnelsstruct))) == NULL)
291           return;
292   }
293   sa = &Atunnels[screen];
294
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));
298         Init(mi);
299   } else {
300         MI_CLEARWINDOW(mi);
301   }
302
303 }
304
305 /* all sorts of nice cleanup code should go here! */
306 void release_atunnels(ModeInfo * mi)
307 {
308   int screen;
309   if (Atunnels != NULL) {
310         for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
311           /*      atunnelsstruct *sa = &Atunnels[screen];*/
312         }
313         (void) free((void *) Atunnels);
314         Atunnels = NULL;
315   }
316   FreeAllGL(mi);
317 }
318 #endif