~ubuntu-branches/ubuntu/dapper/xscreensaver/dapper-updates

« back to all changes in this revision

Viewing changes to hacks/glx/gflux.c

  • Committer: Bazaar Package Importer
  • Author(s): Ralf Hildebrandt
  • Date: 2005-04-09 00:06:43 UTC
  • mfrom: (1.1.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20050409000643-z0abtifbt9s20pcc
Tags: 4.21-3
Patch by Joachim Breitner to check more frequently if DPMS kicked in (closes: #303374, #286664).

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/* gflux - creates a fluctuating 3D grid 
3
3
 * requires OpenGL or MesaGL
4
4
 * 
5
 
 * Copyright (c) Josiah Pease, 2000
 
5
 * Copyright (c) Josiah Pease, 2000, 2003
6
6
 * Permission to use, copy, modify, distribute, and sell this software and its
7
7
 * documentation for any purpose is hereby granted without fee, provided that
8
8
 * the above copyright notice appear in all copies and that both that
35
35
 * 21 July 2000 : cleaned up code from bug hunts, manpage written
36
36
 * 24 November 2000 : fixed x co-ord calculation in solid - textured
37
37
 * 05 March 2001 : put back non pnmlib code with #ifdefs
38
 
 */
39
 
 
40
 
 
41
 
/*-
42
 
 * due to a Bug/feature in VMS X11/Intrinsic.h has to be placed before xlock.
43
 
 * otherwise caddr_t is not defined correctly
44
 
 */
45
 
 
46
 
#include <X11/Intrinsic.h>
 
38
 * 11 May 2002 : fixed image problems with large images
 
39
 */
47
40
 
48
41
 
49
42
#ifdef STANDALONE
51
44
# define HACK_INIT                                              init_gflux
52
45
# define HACK_DRAW                                              draw_gflux
53
46
# define HACK_RESHAPE                                   reshape_gflux
 
47
# define HACK_HANDLE_EVENT                              gflux_handle_event
 
48
# define EVENT_MASK                                             PointerMotionMask
54
49
# define gflux_opts                                             xlockmore_opts
55
50
#define DEFAULTS                        "*delay:                20000   \n" \
56
51
                                                                                "*showFPS:      False   \n" \
57
 
                                        "*squares:      19      \n" \
58
 
                                                                                "*resolution:   0       \n" \
59
 
                                        "*draw:         2       \n" \
60
 
                                        "*flat:         0       \n" \
61
 
                                        "*speed:        0.05    \n" \
62
 
                                        "*rotationx:    0.01    \n" \
63
 
                                        "*rotationy:    0.0     \n" \
64
 
                                        "*rotationz:    0.1     \n" \
65
 
                                        "*waves:        3       \n" \
66
 
                                        "*waveChange:   50      \n" \
67
 
                                        "*waveHeight:   1.0     \n" \
68
 
                                        "*waveFreq:    3.0     \n" \
69
 
                                        "*zoom:         1.0     \n" 
 
52
                                        "*mode:         light" "\n" \
 
53
                                        "*useSHM:      True     \n" 
70
54
 
71
55
 
72
56
# include "xlockmore.h"                         /* from the xscreensaver distribution */
76
60
 
77
61
#ifdef USE_GL /* whole file */
78
62
 
79
 
#ifdef HAVE_XPM
80
 
# include <X11/xpm.h>
81
 
# ifndef PIXEL_ALREADY_TYPEDEFED
82
 
# define PIXEL_ALREADY_TYPEDEFED /* Sigh, Xmu/Drawing.h needs this... */
83
 
# endif
84
 
#endif
85
 
 
86
63
#ifdef HAVE_XMU
87
64
# ifndef VMS
88
65
#  include <X11/Xmu/Drawing.h>
91
68
# endif /* VMS */
92
69
#endif
93
70
 
94
 
#ifdef HAVE_PPM
95
 
#include <ppm.h>
96
 
#endif
97
 
 
98
71
#undef countof
99
72
#define countof(x) (sizeof((x))/sizeof((*x)))
100
73
 
107
80
 
108
81
#include <math.h>
109
82
 
110
 
static enum {wire=0,solid,light,checker,textured} _draw; /* draw style */
 
83
#include "grab-ximage.h"
 
84
#include "gltrackball.h"
 
85
 
 
86
 
 
87
static enum {wire=0,solid,light,checker,grab} _draw; /* draw style */
111
88
static int _squares = 19;                                 /* grid size */
112
89
static int _resolution = 4;                    /* wireframe resolution */
113
90
static int _flat = 0;
123
100
static float _waveHeight = 1.0;
124
101
static float _waveFreq = 3.0;
125
102
 
 
103
static trackball_state *trackball;
 
104
static Bool button_down_p = False;
 
105
 
126
106
#define WIDTH 320
127
107
#define HEIGHT 240
128
108
 
129
109
static XrmOptionDescRec opts[] = {
130
 
    {"-squares", ".gflux.squares", XrmoptionSepArg, (caddr_t) NULL},
131
 
    {"-resolution", ".gflux.resolution", XrmoptionSepArg, (caddr_t) NULL},
132
 
    {"-draw", ".gflux.draw", XrmoptionSepArg, (caddr_t) NULL},
133
 
    {"-flat", ".gflux.flat", XrmoptionSepArg, (caddr_t) NULL},
134
 
    {"-speed", ".gflux.speed", XrmoptionSepArg, (caddr_t) NULL},
135
 
    {"-rotationx", ".gflux.rotationx", XrmoptionSepArg, (caddr_t) NULL},
136
 
    {"-rotationy", ".gflux.rotationy", XrmoptionSepArg, (caddr_t) NULL},
137
 
    {"-rotationz", ".gflux.rotationz", XrmoptionSepArg, (caddr_t) NULL},
138
 
    {"-waves", ".gflux.waves", XrmoptionSepArg, (caddr_t) NULL},
139
 
    {"-waveChange", ".gflux.waveChange", XrmoptionSepArg, (caddr_t) NULL},
140
 
    {"-waveHeight", ".gflux.waveHeight", XrmoptionSepArg, (caddr_t) NULL},
141
 
    {"-waveFreq", ".gflux.waveFreq", XrmoptionSepArg, (caddr_t) NULL},
142
 
    {"-zoom", ".gflux.zoom", XrmoptionSepArg, (caddr_t) NULL},
 
110
    {"-squares", ".gflux.squares", XrmoptionSepArg, 0},
 
111
    {"-resolution", ".gflux.resolution", XrmoptionSepArg, 0},
 
112
/*    {"-draw", ".gflux.draw", XrmoptionSepArg, 0},*/
 
113
    {"-mode", ".gflux.mode", XrmoptionSepArg, 0},
 
114
    {"-flat", ".gflux.flat", XrmoptionSepArg, 0},
 
115
    {"-speed", ".gflux.speed", XrmoptionSepArg, 0},
 
116
    {"-rotationx", ".gflux.rotationx", XrmoptionSepArg, 0},
 
117
    {"-rotationy", ".gflux.rotationy", XrmoptionSepArg, 0},
 
118
    {"-rotationz", ".gflux.rotationz", XrmoptionSepArg, 0},
 
119
    {"-waves", ".gflux.waves", XrmoptionSepArg, 0},
 
120
    {"-waveChange", ".gflux.waveChange", XrmoptionSepArg, 0},
 
121
    {"-waveHeight", ".gflux.waveHeight", XrmoptionSepArg, 0},
 
122
    {"-waveFreq", ".gflux.waveFreq", XrmoptionSepArg, 0},
 
123
    {"-zoom", ".gflux.zoom", XrmoptionSepArg, 0},
143
124
};
144
125
 
145
126
 
146
127
static argtype vars[] = {
147
 
    {(caddr_t *) & _squares, "squares", "Squares", "19", t_Int},
148
 
    {(caddr_t *) & _resolution, "resolution", "Resolution", "4", t_Int},
149
 
    {(caddr_t *) & _draw, "draw", "Draw", "2", t_Int},
150
 
    {(caddr_t *) & _flat, "flat", "Flat", "0", t_Int},
151
 
    {(caddr_t *) & _speed, "speed", "Speed", "0.05", t_Float},
152
 
    {(caddr_t *) & _rotationx, "rotationx", "Rotationx", "0.01", t_Float},
153
 
    {(caddr_t *) & _rotationy, "rotationy", "Rotationy", "0.0", t_Float},
154
 
    {(caddr_t *) & _rotationz, "rotationz", "Rotationz", "0.1", t_Float},
155
 
    {(caddr_t *) & _waves, "waves", "Waves", "3", t_Int},
156
 
    {(caddr_t *) & _waveChange, "waveChange", "WaveChange", "50", t_Int},
157
 
    {(caddr_t *) & _waveHeight, "waveHeight", "WaveHeight", "1.0", t_Float},
158
 
    {(caddr_t *) & _waveFreq, "waveFreq", "WaveFreq", "3.0", t_Float},
159
 
    {(caddr_t *) & _zoom, "zoom", "Zoom", "1.0", t_Float},
 
128
    {&_squares, "squares", "Squares", "19", t_Int},
 
129
    {&_resolution, "resolution", "Resolution", "4", t_Int},
 
130
/*    {&_draw, "draw", "Draw", "2", t_Int},*/
 
131
    {&_flat, "flat", "Flat", "0", t_Int},
 
132
    {&_speed, "speed", "Speed", "0.05", t_Float},
 
133
    {&_rotationx, "rotationx", "Rotationx", "0.01", t_Float},
 
134
    {&_rotationy, "rotationy", "Rotationy", "0.0", t_Float},
 
135
    {&_rotationz, "rotationz", "Rotationz", "0.1", t_Float},
 
136
    {&_waves, "waves", "Waves", "3", t_Int},
 
137
    {&_waveChange, "waveChange", "WaveChange", "50", t_Int},
 
138
    {&_waveHeight, "waveHeight", "WaveHeight", "1.0", t_Float},
 
139
    {&_waveFreq, "waveFreq", "WaveFreq", "3.0", t_Float},
 
140
    {&_zoom, "zoom", "Zoom", "1.0", t_Float},
160
141
};
161
142
 
162
143
 
164
145
{
165
146
    {"-squares num", "size of grid in squares (19)"},
166
147
    {"-resolution num", "detail of lines making grid, wireframe only (4)"},
167
 
    {"-draw num", "draw method to use: 0=wireframe 1=solid 2=lit (0)"},
 
148
/*    {"-draw num", "draw method to use: 0=wireframe 1=solid 2=lit (0)"},*/
168
149
    {"-flat num", "shading method, not wireframe: 0=smooth 1=flat (0)"},
169
150
    {"-speed num", "speed of waves (0.05)"},
170
151
    {"-rotationx num", "speed of xrotation (0.01)"},
189
170
 
190
171
/* structure for holding the gflux data */
191
172
typedef struct {
 
173
    ModeInfo *modeinfo;
192
174
    int screen_width, screen_height;
193
175
    GLXContext *glx_context;
194
176
    Window window;
199
181
    double dispy[MAXWAVES];
200
182
    double dispx[MAXWAVES];
201
183
    GLfloat colour[3];
202
 
    int imageWidth;
203
 
    int imageHeight;
204
 
#ifdef HAVE_PPM
205
 
        pixval imageMax;
206
 
    pixel **image;
207
 
#else
208
 
        int imageMax;
209
 
        GLubyte *image;
210
 
#endif
211
 
    GLint texName;
 
184
    GLuint texName;
 
185
    GLfloat tex_xscale;
 
186
    GLfloat tex_yscale;
 
187
    XRectangle img_geom;
 
188
    int img_width, img_height;
212
189
    void (*drawFunc)(void);
213
190
} gfluxstruct;
214
191
static gfluxstruct *gflux = NULL;
215
192
 
216
193
/* prototypes */
217
194
void initLighting(void);
218
 
void initTexture(void);
219
 
void loadTexture(void);
 
195
void grabTexture(void);
220
196
void createTexture(void);
221
197
void displaySolid(void);            /* drawFunc implementations */
222
198
void displayLight(void);
238
214
/* BEGINNING OF FUNCTIONS */
239
215
 
240
216
 
 
217
Bool
 
218
gflux_handle_event (ModeInfo *mi, XEvent *event)
 
219
{
 
220
  if (event->xany.type == ButtonPress &&
 
221
      event->xbutton.button == Button1)
 
222
    {
 
223
      button_down_p = True;
 
224
      gltrackball_start (trackball,
 
225
                         event->xbutton.x, event->xbutton.y,
 
226
                         MI_WIDTH (mi), MI_HEIGHT (mi));
 
227
      return True;
 
228
    }
 
229
  else if (event->xany.type == ButtonRelease &&
 
230
           event->xbutton.button == Button1)
 
231
    {
 
232
      button_down_p = False;
 
233
      return True;
 
234
    }
 
235
  else if (event->xany.type == ButtonPress &&
 
236
           (event->xbutton.button == Button4 ||
 
237
            event->xbutton.button == Button5))
 
238
    {
 
239
      gltrackball_mousewheel (trackball, event->xbutton.button, 10,
 
240
                              !!event->xbutton.state);
 
241
      return True;
 
242
    }
 
243
  else if (event->xany.type == MotionNotify &&
 
244
           button_down_p)
 
245
    {
 
246
      gltrackball_track (trackball,
 
247
                         event->xmotion.x, event->xmotion.y,
 
248
                         MI_WIDTH (mi), MI_HEIGHT (mi));
 
249
      return True;
 
250
    }
 
251
 
 
252
  return False;
 
253
}
 
254
 
 
255
 
 
256
static void
 
257
userRot(void)
 
258
{
 
259
  gltrackball_rotate (trackball);
 
260
}
 
261
 
241
262
/* draw the gflux once */
242
263
void draw_gflux(ModeInfo * mi)
243
264
{
280
301
{
281
302
  reshape_gflux(mi, width, height);
282
303
  glViewport( 0, 0, width, height ); 
 
304
 
 
305
  gflux->tex_xscale = 1.0;  /* maybe changed later */
 
306
  gflux->tex_yscale = 1.0;
 
307
 
283
308
  switch(_draw) {
284
309
    case solid :
285
310
      gflux->drawFunc = (displaySolid);
294
319
      gflux->drawFunc = (displayTexture);
295
320
      glEnable(GL_DEPTH_TEST);
296
321
      createTexture();
297
 
      initTexture();
298
322
      initLighting();
299
323
    break;
300
 
        case textured :
 
324
        case grab :
301
325
      gflux->drawFunc = (displayTexture);
302
326
      glEnable(GL_DEPTH_TEST);
303
 
      loadTexture();
304
 
      initTexture();
 
327
      grabTexture();
305
328
      initLighting();
306
329
    break;
307
330
    case wire :
330
353
    }
331
354
    gp = &gflux[screen];
332
355
 
 
356
    trackball = gltrackball_init ();
 
357
 
 
358
    {
 
359
      char *s = get_string_resource ("mode", "Mode");
 
360
      if (!s || !*s)                       _draw = wire;
 
361
      else if (!strcasecmp (s, "wire"))    _draw = wire;
 
362
      else if (!strcasecmp (s, "solid"))   _draw = solid;
 
363
      else if (!strcasecmp (s, "light"))   _draw = light;
 
364
      else if (!strcasecmp (s, "checker")) _draw = checker;
 
365
      else if (!strcasecmp (s, "grab"))    _draw = grab;
 
366
      else
 
367
        {
 
368
          fprintf (stderr,
 
369
                   "%s: mode must be one of: wire, solid, "
 
370
                   "light, checker, or grab; not \"%s\"\n",
 
371
                   progname, s);
 
372
          exit (1);
 
373
        }
 
374
    }
 
375
 
 
376
    gp->modeinfo = mi;
333
377
    gp->window = MI_WINDOW(mi);
334
378
    if ((gp->glx_context = init_GL(mi)) != NULL) {
335
379
        reshape_gflux(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
342
386
/* cleanup code */
343
387
void release_gflux(ModeInfo * mi)
344
388
{
345
 
    if(gflux->image!=NULL) free(gflux->image);
346
389
    if(gflux->glx_context!=NULL) free(gflux->glx_context);
347
390
    if (gflux != NULL) {
348
391
        (void) free((void *) gflux);
351
394
    FreeAllGL(mi);
352
395
}
353
396
 
354
 
#ifdef HAVE_PPM
355
 
 
356
 
/* load pnm from stdin using pnm libs */
357
 
void loadTexture(void)
358
 
{
359
 
    FILE *file = stdin;
360
 
        gflux->image = ppm_readppm( file, 
361
 
                        &(gflux->imageHeight), &(gflux->imageWidth), &(gflux->imageMax) );
362
 
}
363
 
 
364
 
/* creates an image for texture mapping */
365
 
void createTexture(void)
366
 
{
367
 
    int i,j,c;
368
 
    pixel **result;
369
 
 
370
 
        gflux->imageHeight = gflux->imageWidth = 8;
371
 
 
372
 
        result = ppm_allocarray(gflux->imageHeight,gflux->imageWidth);
373
 
    for(i=0;i<gflux->imageHeight;i++) {
374
 
        for(j=0;j<gflux->imageWidth;j++) {
375
 
            c = (((i)%2 ^ (j)%2) ? 100 : 200 );
376
 
                        PPM_ASSIGN( result[i][j] , c, c, c );
377
 
        }
378
 
    }
379
 
        gflux->image = result;
380
 
}
381
 
 
382
 
/* specifies image as texture */    
383
 
void initTexture(void)
384
 
{
385
 
        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
386
 
        glGenTextures(1, &gflux->texName);
387
 
        glBindTexture(GL_TEXTURE_2D, gflux->texName);
388
 
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
389
 
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
390
 
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
391
 
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
392
 
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, gflux->imageWidth,
393
 
                        gflux->imageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, *(gflux->image));
394
 
        
395
 
}
396
 
 
397
 
#else /* HAVE_PPM FALSE */
398
 
 
399
 
#define presult(A,B,C) (*(result+(A)*(gflux->imageWidth)*4+(B)*4+(C)))
400
 
void loadTexture(void)
401
 
{
402
 
    int i, j, levels, width, height;
403
 
    int red,green,blue;
404
 
    char s[4];
405
 
    int ppmType=0;
406
 
    FILE *file = stdin;
407
 
    GLubyte *result;
408
 
 
409
 
    fgets(s,4,file);
410
 
 
411
 
    if(!strncmp(s,"P6",2)) ppmType=6;
412
 
    if(!strncmp(s,"P5",2)) ppmType=5;
413
 
    if(!strncmp(s,"P3",2)) ppmType=3;
414
 
    if(!strncmp(s,"P2",2)) ppmType=2;
415
 
    if(!ppmType)exit(1);
416
 
 
417
 
    while((i=getc(file))=='#')
418
 
    {
419
 
        while(getc(file)!='\n');
420
 
    }
421
 
    ungetc(i,file);
422
 
 
423
 
    fscanf(file,"%d %d %d",&width,&height,&levels);
424
 
 
425
 
    result = malloc(sizeof(GLubyte)*4*width*height);
426
 
    gflux->imageWidth = width;
427
 
    gflux->imageHeight = height;
428
 
 
429
 
    switch(ppmType) {
430
 
        case 2 :    /* ASCII grey */
431
 
            for(i=0;i<height;i++) {
432
 
                for(j=0;j<width;j++) {
433
 
                    fscanf(file,"%d",&red);
434
 
                    presult(j,i,0) = red;
435
 
                    presult(j,i,1) = red;
436
 
                    presult(j,i,2) = red;
437
 
                }
438
 
            }
439
 
            break;
440
 
        case 3 :    /* ASCII rgb */
441
 
            for(i=0;i<height;i++) {
442
 
                for(j=0;j<width;j++) {
443
 
                   fscanf(file,"%d %d %d",&red,&green,&blue);
444
 
                    presult(j,i,0) = red;
445
 
                    presult(j,i,1) = green;
446
 
                    presult(j,i,2) = blue;
447
 
                }
448
 
            }
449
 
            break;
450
 
        case 5 :    /* Binary grey */
451
 
            getc(file); /* seems nessessary */
452
 
            for(i=0;i<height;i++) {
453
 
                for(j=0;j<width;j++) {
454
 
                    red = getc(file);
455
 
                    presult(j,i,0) = red;
456
 
                    presult(j,i,1) = red;
457
 
                    presult(j,i,2) = red;
458
 
                }
459
 
            }
460
 
        break;
461
 
        case 6 :    /* Binary rgb */
462
 
            getc(file); /* seems nessessary */
463
 
            for(i=0;i<height;i++) {
464
 
                for(j=0;j<width;j++) {
465
 
                    red = getc(file);
466
 
                    green = getc(file);
467
 
                    blue = getc(file);
468
 
                    presult(j,i,0) = red;
469
 
                    presult(j,i,1) = green;
470
 
                    presult(j,i,2) = blue;
471
 
                }
472
 
            }
473
 
        break;
474
 
    }
475
 
    gflux->image = result;
476
 
}
477
 
 
478
 
void createTexture(void)
479
 
{
480
 
    int i,j,c;
481
 
    GLubyte *result;
482
 
 
483
 
    gflux->imageHeight = gflux->imageWidth = 8;
484
 
 
485
 
    result = malloc(sizeof(GLubyte)*4*gflux->imageHeight*gflux->imageWidth);
486
 
    for(i=0;i<gflux->imageHeight;i++) {
487
 
        for(j=0;j<gflux->imageWidth;j++) {
488
 
            c = (((i)%2 ^ (j)%2) ? 100 : 200 );
489
 
            presult(i,j,0) = (GLubyte) c;
490
 
            presult(i,j,1) = (GLubyte) c;
491
 
            presult(i,j,2) = (GLubyte) c;
492
 
            presult(i,j,3) = (GLubyte) 255;
493
 
        }
494
 
    }
495
 
    gflux->image = result;
496
 
}
497
 
 
498
 
/* specifies image as texture */
499
 
void initTexture(void)
500
 
{
501
 
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
502
 
    glGenTextures(1, &gflux->texName);
503
 
    glBindTexture(GL_TEXTURE_2D, gflux->texName);
504
 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
505
 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
506
 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
507
 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
508
 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, gflux->imageWidth,
509
 
            gflux->imageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, gflux->image);
510
 
 
511
 
}
512
 
 
513
 
#undef presult
514
 
#endif
 
397
 
 
398
 
 
399
void createTexture(void)
 
400
{
 
401
  int size = 4;
 
402
  unsigned int data[] = { 0xFFFFFFFF, 0xAAAAAAAA, 0xFFFFFFFF, 0xAAAAAAAA,
 
403
                          0xAAAAAAAA, 0xFFFFFFFF, 0xAAAAAAAA, 0xFFFFFFFF,
 
404
                          0xFFFFFFFF, 0xAAAAAAAA, 0xFFFFFFFF, 0xAAAAAAAA,
 
405
                          0xAAAAAAAA, 0xFFFFFFFF, 0xAAAAAAAA, 0xFFFFFFFF };
 
406
 
 
407
  gflux->tex_xscale = size;
 
408
  gflux->tex_yscale = size;
 
409
 
 
410
  glGenTextures (1, &gflux->texName);
 
411
  glBindTexture (GL_TEXTURE_2D, gflux->texName);
 
412
 
 
413
  glTexImage2D (GL_TEXTURE_2D, 0, 3, size, size, 0,
 
414
                GL_RGBA, GL_UNSIGNED_BYTE, data);
 
415
 
 
416
  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 
417
  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 
418
 
 
419
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
 
420
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
 
421
}
 
422
 
 
423
 
 
424
void
 
425
grabTexture(void)
 
426
{
 
427
  Bool mipmap_p = True;
 
428
  int iw, ih, tw, th;
 
429
 
 
430
  if (MI_IS_WIREFRAME(gflux->modeinfo))
 
431
    return;
 
432
 
 
433
  if (! screen_to_texture (gflux->modeinfo->xgwa.screen,
 
434
                           gflux->modeinfo->window, 0, 0, mipmap_p,
 
435
                           NULL, &gflux->img_geom, &iw, &ih, &tw, &th))
 
436
    exit (1);
 
437
 
 
438
  gflux->tex_xscale =  (GLfloat) iw / tw;
 
439
  gflux->tex_yscale = -(GLfloat) ih / th;
 
440
  gflux->img_width  = iw;
 
441
  gflux->img_height = ih;
 
442
   
 
443
  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 
444
  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
 
445
                   (mipmap_p ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR));
 
446
}
 
447
 
515
448
 
516
449
void initLighting(void)
517
450
{
564
497
    double du = 2.0/((double)_squares);
565
498
    double dv = 2.0/((double)_squares);
566
499
 
 
500
    double xs = gflux->tex_xscale;
 
501
    double ys = gflux->tex_yscale;
 
502
 
 
503
    double minx, miny, maxx, maxy;
 
504
    double minu, minv;
 
505
 
 
506
#if 0
 
507
    minx = (GLfloat) gflux->img_geom.x / gflux->img_width;
 
508
    miny = (GLfloat) gflux->img_geom.y / gflux->img_height;
 
509
    maxx = ((GLfloat) (gflux->img_geom.x + gflux->img_geom.width) /
 
510
            gflux->img_width);
 
511
    maxy = ((GLfloat) (gflux->img_geom.y + gflux->img_geom.height) /
 
512
            gflux->img_height);
 
513
    minu = minx;
 
514
    minv = miny;
 
515
    minx = (minx * 2) - 1;
 
516
    miny = (miny * 2) - 1;
 
517
    maxx = (maxx * 2) - 1;
 
518
    maxy = (maxy * 2) - 1;
 
519
#else
 
520
    minx = -1;
 
521
    miny = -1;
 
522
    maxx = 1;
 
523
    maxy = 1;
 
524
    minv = 0;
 
525
    minu = 0;
 
526
#endif
 
527
 
567
528
        glMatrixMode (GL_TEXTURE);
568
529
        glLoadIdentity ();
569
530
        glTranslatef(-1,-1,0);
571
532
        glMatrixMode (GL_MODELVIEW);
572
533
 
573
534
    glLoadIdentity();
 
535
    userRot();
574
536
    glRotatef(anglex,1,0,0);
575
537
    glRotatef(angley,0,1,0);
576
538
    glRotatef(anglez,0,0,1);
577
539
    glScalef(1,1,(GLfloat)_waveHeight);
578
540
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
579
541
        glEnable(GL_TEXTURE_2D);
 
542
 
 
543
    clear_gl_error();
580
544
        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
581
545
        glBindTexture(GL_TEXTURE_2D, gflux->texName);
 
546
    check_gl_error("texture binding");
 
547
 
582
548
        glColor3f(0.5,0.5,0.5);
583
549
 
584
 
    for(x=-1,u= 0;x<0.9999;x+=dx,u+=du) {
 
550
    for(x = minx, u = minu; x < maxx - 0.01; x += dx, u += du) {
585
551
        glBegin(GL_QUAD_STRIP);
586
 
        for(y=-1,v= 0;y<=1;y+=dy,v+=dv) {
 
552
        for (y = miny, v = minv; y <= maxy + 0.01; y += dy, v += dv) {
587
553
            z = getGrid(x,y,time);
588
 
        /*  genColour(z);
589
 
            glColor3fv(gflux->colour);
590
 
        */  glTexCoord2f(u,v);
 
554
            glTexCoord2f(u*xs,v*ys);
591
555
            glNormal3f(
592
556
                getGrid(x+dx,y,time)-getGrid(x-dx,y,time),
593
557
                getGrid(x,y+dy,time)-getGrid(x,y-dy,time),
596
560
            glVertex3f(x,y,z);
597
561
 
598
562
            z = getGrid(x+dx,y,time);
599
 
        /*  genColour(z);
600
 
            glColor3fv(gflux->colour);
601
 
        */  glTexCoord2f(u+du,v);
 
563
            glTexCoord2f((u+du)*xs,v*ys);
602
564
            glNormal3f(
603
565
                getGrid(x+dx+dx,y,time)-getGrid(x,y,time),
604
566
                getGrid(x+dx,y+dy,time)-getGrid(x+dx,y-dy,time),
609
571
        glEnd();
610
572
    }
611
573
 
612
 
    time -= _speed;
613
 
    anglex -= _rotationx;
614
 
    angley -= _rotationy;
615
 
    anglez -= _rotationz;
 
574
    /* Draw a border around the grid.
 
575
     */
 
576
    glColor3f(0.4, 0.4, 0.4);
 
577
    glDisable(GL_TEXTURE_2D);
 
578
    glEnable (GL_LINE_SMOOTH);
 
579
 
 
580
    glBegin(GL_LINE_LOOP);
 
581
    y = miny;
 
582
    for (x = minx; x <= maxx; x += dx)
 
583
      glVertex3f (x, y, getGrid (x, y, time));
 
584
    x = maxx;
 
585
    for (y = miny; y <= maxy; y += dy)
 
586
      glVertex3f (x, y, getGrid (x, y, time));
 
587
    y = maxy;
 
588
    for (x = maxx; x >= minx; x -= dx)
 
589
      glVertex3f (x, y, getGrid (x, y, time));
 
590
    x = minx;
 
591
    for (y = maxy; y >= miny; y -= dy)
 
592
      glVertex3f (x, y, getGrid (x, y, time));
 
593
 
 
594
    glEnd();
 
595
    glEnable(GL_TEXTURE_2D);
 
596
 
 
597
    if (! button_down_p) {
 
598
      time -= _speed;
 
599
      anglex -= _rotationx;
 
600
      angley -= _rotationy;
 
601
      anglez -= _rotationz;
 
602
    }
616
603
}
617
604
void displaySolid(void)
618
605
{
630
617
    glRotatef(anglex,1,0,0);
631
618
    glRotatef(angley,0,1,0);
632
619
    glRotatef(anglez,0,0,1);
 
620
    userRot();
633
621
    glScalef(1,1,(GLfloat)_waveHeight);
634
622
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
635
623
 
649
637
        glEnd();
650
638
    }
651
639
 
652
 
    time -= _speed;
653
 
    anglex -= _rotationx;
654
 
    angley -= _rotationy;
655
 
    anglez -= _rotationz;
 
640
    if (! button_down_p) {
 
641
      time -= _speed;
 
642
      anglex -= _rotationx;
 
643
      angley -= _rotationy;
 
644
      anglez -= _rotationz;
 
645
    }
656
646
 
657
647
}
658
648
 
672
662
    glRotatef(anglex,1,0,0);
673
663
    glRotatef(angley,0,1,0);
674
664
    glRotatef(anglez,0,0,1);
 
665
    userRot();
675
666
    glScalef(1,1,(GLfloat)_waveHeight);
676
667
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
677
668
 
701
692
        glEnd();
702
693
    }
703
694
 
704
 
    time -= _speed;
705
 
    anglex -= _rotationx;
706
 
    angley -= _rotationy;
707
 
    anglez -= _rotationz;
 
695
    if (! button_down_p) {
 
696
      time -= _speed;
 
697
      anglex -= _rotationx;
 
698
      angley -= _rotationy;
 
699
      anglez -= _rotationz;
 
700
    }
708
701
}
709
702
 
710
703
void displayWire(void)
725
718
    glRotatef(anglex,1,0,0);
726
719
    glRotatef(angley,0,1,0);
727
720
    glRotatef(anglez,0,0,1);
 
721
    userRot();
728
722
    glScalef(1,1,(GLfloat)_waveHeight);
729
723
    glClear(GL_COLOR_BUFFER_BIT);
730
724
 
749
743
        glEnd();
750
744
    }
751
745
 
752
 
    time -= _speed;
753
 
    anglex -= _rotationx;
754
 
    angley -= _rotationy;
755
 
    anglez -= _rotationz;
 
746
    if (! button_down_p) {
 
747
      time -= _speed;
 
748
      anglex -= _rotationx;
 
749
      angley -= _rotationy;
 
750
      anglez -= _rotationz;
 
751
    }
756
752
}
757
753
 
758
754
/* generates new ripples */
762
758
    double tmp;
763
759
    static int newWave;
764
760
 
 
761
    if (button_down_p) return;
 
762
 
765
763
    tmp = 1.0/((double)_waveChange);
766
764
    if(!(counter%_waveChange)) {
767
765
        newWave = ((int)(counter*tmp))%_waves;
768
 
        gflux->dispx[newWave] = 1.0 - ((double)random())/RAND_MAX;
769
 
        gflux->dispy[newWave] = 1.0 - ((double)random())/RAND_MAX;
770
 
        gflux->freq[newWave] = _waveFreq * ((float)random())/RAND_MAX;
 
766
        gflux->dispx[newWave] = -frand(1.0);
 
767
        gflux->dispy[newWave] = -frand(1.0);
 
768
        gflux->freq[newWave] = _waveFreq * frand(1.0);
771
769
        gflux->wa[newWave] = 0.0;
772
770
    }
773
771
    counter++;