summaryrefslogtreecommitdiffstats
path: root/hacks/glx/atunnel.c
blob: f21d04fd5d964e6915b4ab20f53c9f08ee8c2139 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* -*- Mode: C; tab-width: 4 -*- */
/* atunnel --- OpenGL Advanced Tunnel Screensaver */

#if 0
static const char sccsid[] = "@(#)atunnel.c	5.13 2004/05/25 xlockmore";
#endif

/* Copyright (c) E. Lassauge, 2003-2004. */

/*
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notice appear in all copies and that
 * both that copyright notice and this permission notice appear in
 * supporting documentation.
 *
 * This file is provided AS IS with no warranties of any kind.  The author
 * shall have no liability with respect to the infringement of copyrights,
 * trade secrets or any patents by this file or any part thereof.  In no
 * event will the author be liable for any lost revenue or profits or
 * other special, indirect and consequential damages.
 *
 * The original code for this mode was written by Roman Podobedov
 * Email: romka@ut.ee
 * WEB: http://romka.demonews.com
 *
 * Eric Lassauge  (May-25-2004) <lassauge@users.sourceforge.net>
 * 				    http://lassauge.free.fr/linux.html
 *
 * REVISION HISTORY:
 *
 * E.Lassauge - 25-May-2004:
 *	- added more texture !
 * E.Lassauge - 16-Mar-2002:
 *	- created based on the Roman demo.
 *	- deleted all external file stuff to use xpm textures and
 *	  hardcoded path point values.
 *
 */

#ifdef STANDALONE		/* xscreensaver mode */
#define	DEFAULTS                "*delay:	10000	\n" \
                                "*showFPS:  False   \n" \
								"*suppressRotationAnimation: True\n" \

# define release_atunnel 0
# define atunnel_handle_event 0
#define MODE_atunnel
# include "xlockmore.h"		/* from the xscreensaver distribution */
#else 				/* !STANDALONE */
# include "xlock.h"		/* from the xlockmore distribution */
#endif 				/* !STANDALONE */

#ifdef MODE_atunnel /* whole file */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "tunnel_draw.h"

#if defined( USE_XPM ) || defined( USE_XPMINC ) || defined(STANDALONE)
/* USE_XPM & USE_XPMINC in xlock mode ; STANDALONE in xscreensaver mode */
#include "ximage-loader.h"
#define I_HAVE_XPM

#include "images/gen/tunnel0_png.h"
#include "images/gen/tunnel1_png.h"
#include "images/gen/tunnel2_png.h"
#include "images/gen/tunnel3_png.h"
#include "images/gen/tunnel4_png.h"
#include "images/gen/tunnel5_png.h"
#endif /* HAVE_XPM */


#undef countof
#define countof(x) (sizeof((x))/sizeof((*x)))

#define DEF_LIGHT	"True"
#define DEF_WIRE    "False"
#define DEF_TEXTURE	"True"

static Bool do_light;
static Bool do_wire;
static Bool do_texture;

static XrmOptionDescRec opts[] = {
  {"-light",   ".atunnel.light",      XrmoptionNoArg, "true" },
  {"+light",   ".atunnel.light",      XrmoptionNoArg, "false" },
  {"-wireframe",".atunnel.wire",       XrmoptionNoArg, "true" },
  {"+wireframe",".atunnel.wire",       XrmoptionNoArg, "false" },
  {"-texture", ".atunnel.texture",    XrmoptionNoArg, "true" },
  {"+texture", ".atunnel.texture",    XrmoptionNoArg, "false" },
};

static argtype vars[] = {
  {&do_light,   "light",   "Light",   DEF_LIGHT,   t_Bool},
  {&do_wire,    "wire",    "Wire",    DEF_WIRE,    t_Bool},
  {&do_texture, "texture", "Texture", DEF_TEXTURE, t_Bool},
};

static OptionStruct desc[] =
{
  {"-/+ light",   "whether to do enable lighting (slower)"},
  {"-/+ wire",    "whether to do use wireframe instead of filled (faster)"},
  {"-/+ texture", "whether to apply a texture (slower)"},
};

ENTRYPOINT ModeSpecOpt atunnel_opts = {countof(opts), opts, countof(vars), vars, desc};

#ifdef USE_MODULES
ModStruct   atunnel_description =
{"atunnel", "init_atunnel", "draw_atunnel", NULL,
 "draw_atunnel", "init_atunnel", "free_atunnel", &atunnel_opts,
 1000, 1, 2, 1, 4, 1.0, "",
 "OpenGL advanced tunnel screensaver", 0, NULL};
#endif

/* structure for holding the screensaver data */
typedef struct {
  int screen_width, screen_height;
  GLXContext *glx_context;
  Window window;
  struct tunnel_state *ts;
  GLuint texture[MAX_TEXTURE]; /* texture id: GL world */
} atunnelstruct;

static atunnelstruct *Atunnel = NULL;

/*=================== Load Texture =========================================*/
static void LoadTexture(ModeInfo * mi,
                        const unsigned char *data, unsigned long size,
                        int t_num)
{
#if defined( I_HAVE_XPM )
  	atunnelstruct *sa = &Atunnel[MI_SCREEN(mi)];
	XImage *teximage;    /* Texture data */
 
        if ((teximage = image_data_to_ximage(MI_DISPLAY(mi), MI_VISUAL(mi),
                                             data, size))
            == None) {
	    (void) fprintf(stderr, "Error reading the texture.\n");
	    glDeleteTextures(1, &sa->texture[t_num]);
            do_texture = False;
#ifdef STANDALONE
	    exit(0);
#else
	    return;
#endif
	}

#ifdef HAVE_GLBINDTEXTURE
	glBindTexture(GL_TEXTURE_2D, sa->texture[t_num]);
#endif /* HAVE_GLBINDTEXTURE */
	glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
	clear_gl_error();
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, teximage->width, teximage->height, 
                 0, GL_RGBA, GL_UNSIGNED_BYTE, teximage->data);
	check_gl_error("texture");

	/* Texture parameters, LINEAR scaling for better texture quality */
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 

	XDestroyImage(teximage);
#else /* !I_HAVE_XPM */
	do_texture = False;
#endif /* !I_HAVE_XPM */
} 

/*=================== Main Initialization ==================================*/
static void Init(ModeInfo * mi)
{
  	atunnelstruct *sa = &Atunnel[MI_SCREEN(mi)];
	GLfloat light_ambient[] = {1.0, 1.0, 1.0, 1.0};
	GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0};
	GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0};
	GLfloat light_position[] = {0.0, 0.0, 1.0, 0.0};
	GLfloat fogColor[4] = {0.8, 0.8, 0.8, 1.0};

	if (do_texture)
	{
		glGenTextures(MAX_TEXTURE, sa->texture);
		LoadTexture(mi, tunnel0_png, sizeof(tunnel0_png),0);
		LoadTexture(mi, tunnel1_png, sizeof(tunnel1_png),1);
		LoadTexture(mi, tunnel2_png, sizeof(tunnel2_png),2);
		LoadTexture(mi, tunnel3_png, sizeof(tunnel3_png),3);
		LoadTexture(mi, tunnel4_png, sizeof(tunnel4_png),4);
		LoadTexture(mi, tunnel5_png, sizeof(tunnel5_png),5);
		glEnable(GL_TEXTURE_2D);
	}
	sa->ts = atunnel_InitTunnel();
	
	/* Set lighting parameters */
	if (do_light)
	{
		glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
		glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
		glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
		glLightfv(GL_LIGHT0, GL_POSITION, light_position);

		/* Enable light 0 */
		glEnable(GL_LIGHT0);
		glDepthFunc(GL_LESS);
	
		glEnable(GL_LIGHTING);
	}

# ifdef HAVE_JWZGLES /* #### glPolygonMode other than GL_FILL unimplemented */
    do_wire = 0;
# endif

  	if (do_wire) {
		glDisable(GL_NORMALIZE);
		glDisable(GL_CULL_FACE);
		glDisable(GL_DEPTH_TEST);
		glDisable(GL_TEXTURE_2D);
		glPolygonMode(GL_FRONT,GL_LINE);
  		glPolygonMode(GL_BACK,GL_LINE);
  	}
	else
	{
		glEnable(GL_DEPTH_TEST);
		glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

		/* Enable fog */
		glFogi(GL_FOG_MODE, GL_EXP);
		glFogfv(GL_FOG_COLOR, fogColor);
		glFogf(GL_FOG_DENSITY, 0.3);
		glEnable(GL_FOG);
	
		/* Cull face */
		glCullFace(GL_FRONT);
		glEnable(GL_CULL_FACE);
	}
	
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}


/* Standard reshape function */
ENTRYPOINT void
reshape_atunnel(ModeInfo *mi, int width, int height)
{
  double h = (GLfloat) height / (GLfloat) width;  
  int y = 0;

  if (width > height * 2) {   /* tiny window: show middle */
    height = width;
    y = -height/2;
    h = height / (GLfloat) width;
  }

  glViewport(0, y, width, height);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glFrustum(-0.1*(1/h), 0.1*(1/h), -0.1, 0.1, 0.1, 10);
  glMatrixMode(GL_MODELVIEW);
}

/* draw the screensaver once */
ENTRYPOINT void draw_atunnel(ModeInfo * mi)
{
  	atunnelstruct *sa = &Atunnel[MI_SCREEN(mi)];
  	Display    *display = MI_DISPLAY(mi);
  	Window      window = MI_WINDOW(mi);

  	if (!sa->glx_context)
		return;

  	glXMakeCurrent(display, window, *(sa->glx_context));

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glLoadIdentity();

	atunnel_DrawTunnel(sa->ts, do_texture, do_light, sa->texture);
	atunnel_SplashScreen(sa->ts, do_wire, do_texture, do_light);

	glFlush();  
	/* manage framerate display */
    	if (MI_IS_FPS(mi)) do_fps (mi);
  	glXSwapBuffers(display, window);

}


/* xscreensaver initialization routine */
ENTRYPOINT void init_atunnel(ModeInfo * mi)
{
  int screen = MI_SCREEN(mi);
  atunnelstruct *sa;

  MI_INIT(mi, Atunnel);
  sa = &Atunnel[screen];

  sa->window = MI_WINDOW(mi);
  if ((sa->glx_context = init_GL(mi)) != NULL) {
	reshape_atunnel(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
	Init(mi);
  } else {
	MI_CLEARWINDOW(mi);
  }

}

/* all sorts of nice cleanup code should go here! */
ENTRYPOINT void free_atunnel(ModeInfo * mi)
{
#if 0
  atunnelstruct *sa = &Atunnel[MI_SCREEN(mi)];
  FreeTunnel(sa->ts);
#endif
}

XSCREENSAVER_MODULE ("Atunnel", atunnel)

#endif