5eedaf7c107c23586c6728f77e545c5e080d5aa1
[xscreensaver] / hacks / bumps.c
1 /* Bumps, Copyright (c) 1999 Shane Smit <blackend@inconnect.com>
2  *
3  * Permission to use, copy, modify, distribute, and sell this software and its
4  * documentation for any purpose is hereby granted without fee, provided that
5  * the above copyright notice appear in all copies and that both that
6  * copyright notice and this permission notice appear in supporting
7  * documentation.  No representations are made about the suitability of this
8  * software for any purpose.  It is provided "as is" without express or
9  * implied warranty.
10  *
11  * Module: "Bumps.cpp"
12  * Tab Size: 4
13  *
14  * Description:
15  *  This is typical bump-mapping.  The actual bump map is generated by a screen
16  *  grab.  The light source is represented by a spotlight of random color. This
17  *  spotlight randomly traverses the bump map in a sinus pattern.
18  *
19  *  Essentially, it 3D-izes your desktop, based on color intensity.
20  *
21  * Modification History:
22  *  [10/01/99] - Shane Smit: Creation
23  *  [10/08/99] - Shane Smit: Port to C. (Ick)
24  */
25
26
27 #include "bumps.h"
28
29
30 void CreateSpotLight( SSpotLight *pSpotLight, uint16_ iDiameter, uint16_ nColorCount )
31 {
32         double nDelta;
33         int16_ iHeight, iWidth;
34         
35         pSpotLight->nDiameter = iDiameter;
36 #ifdef VERBOSE
37         printf( "%s: Light Diameter: %d\n", progclass, pSpotLight->nDiameter );
38 #endif
39
40         pSpotLight->aLightMap = calloc( pSpotLight->nDiameter * pSpotLight->nDiameter, sizeof(uint8_) );
41         memset( pSpotLight->aLightMap, 0, pSpotLight->nDiameter * pSpotLight->nDiameter );
42
43         /* The falloff max values... 3/4 of the entire lightmap. */
44         pSpotLight->nRadius = (uint16_)(pSpotLight->nDiameter / 2.5F);
45         
46         for( iHeight=-pSpotLight->nRadius; iHeight<pSpotLight->nRadius; iHeight++ )
47                 for( iWidth=-pSpotLight->nRadius; iWidth<pSpotLight->nRadius; iWidth++ )
48                 {
49                         nDelta = ( nColorCount * 2.5F ) - ( ( sqrt( pow( iWidth+0.5F, 2 ) + pow( iHeight+0.5F, 2 ) ) / pSpotLight->nRadius ) * ( nColorCount * 2.5F ) );
50                         nDelta += nColorCount;
51                         if( nDelta >= ( nColorCount * 2 ) ) nDelta = ( nColorCount * 2 ) - 1;
52                         if( nDelta >= nColorCount )
53                                 pSpotLight->aLightMap[ ( ( iHeight + (pSpotLight->nDiameter/2) ) * pSpotLight->nDiameter ) + iWidth + (pSpotLight->nDiameter/2) ] = (uint8_)nDelta;
54                 }
55
56         /* The actual lightmap... 1/2 of the entire lightmap. */
57         pSpotLight->nRadius = pSpotLight->nDiameter / 4;
58
59         for( iHeight=-pSpotLight->nRadius; iHeight<pSpotLight->nRadius; iHeight++ )
60                 for( iWidth=-pSpotLight->nRadius; iWidth<pSpotLight->nRadius; iWidth++ )
61                 {
62                         nDelta = nColorCount - ( ( sqrt( pow( iWidth+0.5F, 2 ) + pow( iHeight+0.5F, 2 ) ) / pSpotLight->nRadius ) * ( nColorCount - 1 ) );
63                         if( nDelta >= 1 )
64                                 pSpotLight->aLightMap[ ( ( iHeight + (pSpotLight->nDiameter/2) ) * pSpotLight->nDiameter ) + iWidth + (pSpotLight->nDiameter/2) ] = (uint8_)nDelta;
65                 }
66                 
67         CreateTables( pSpotLight );
68
69         pSpotLight->nRadius = pSpotLight->nDiameter / 2;        /* Now set the radius back to what it should be. */
70         pSpotLight->nAngleX = RANDOM() % pSpotLight->nDegreeCount;
71         pSpotLight->nAngleY = RANDOM() % pSpotLight->nDegreeCount;
72         pSpotLight->nVelocityX = ( RANDOM() % 3 ) + 1;
73         pSpotLight->nVelocityY = ( RANDOM() % 3 ) + 1;
74 }
75
76
77 void CreateTables( SSpotLight *pSpotLight )
78 {
79         double nUnit;
80         uint16_ iDegree;
81         
82         pSpotLight->nDegreeCount = get_integer_resource( "degrees", "Integer" );
83         if( pSpotLight->nDegreeCount < 180 ) pSpotLight->nDegreeCount = 180;    /* Less than 180 will show trails in higher resolutions. */
84 #ifdef VERBOSE
85         printf( "%s: Using a %d degree circle.\n", progclass, pSpotLight->nDegreeCount );
86 #endif
87
88         pSpotLight->aSinTable = calloc( pSpotLight->nDegreeCount, sizeof(double) );
89
90         /* This funtion builds the Sine Lookup Tables. */
91         nUnit = (double)( PI * 2.0F ) / (double)( pSpotLight->nDegreeCount );
92
93         for( iDegree=0; iDegree<pSpotLight->nDegreeCount; iDegree++)
94                 pSpotLight->aSinTable[ iDegree ] = sin( nUnit * (double)iDegree );
95 }
96
97
98 void CalcLightPos( SSpotLight *pSpotLight, uint16_ *pXPos, uint16_ *pYPos )
99 {
100         pSpotLight->nVelocityX += ( RANDOM() % 2 ) ? 0.05F : -0.05F;
101         if( pSpotLight->nVelocityX < 1 )                pSpotLight->nVelocityX = 1;
102         else if( pSpotLight->nVelocityX > 3 )   pSpotLight->nVelocityX = 3;
103
104         pSpotLight->nVelocityX += ( RANDOM() % 2 ) ? 0.05F : -0.05F;
105         if( pSpotLight->nVelocityY < 1 )                pSpotLight->nVelocityX = 1;
106         else if( pSpotLight->nVelocityY > 3 )   pSpotLight->nVelocityX = 3;
107         
108     pSpotLight->nAngleX += pSpotLight->nVelocityX;
109     if( pSpotLight->nAngleX >= pSpotLight->nDegreeCount )       pSpotLight->nAngleX -= pSpotLight->nDegreeCount;
110
111     pSpotLight->nAngleY += pSpotLight->nVelocityY;
112     if( pSpotLight->nAngleY >= pSpotLight->nDegreeCount )       pSpotLight->nAngleY -= pSpotLight->nDegreeCount;
113
114     *pXPos = (uint16_)( ( pSpotLight->iWinXCenter - pSpotLight->nRadius ) * pSpotLight->aSinTable[ (uint16_)pSpotLight->nAngleX ] ) + pSpotLight->iWinXCenter;
115     *pYPos = (uint16_)( ( pSpotLight->iWinYCenter - pSpotLight->nRadius ) * pSpotLight->aSinTable[ (uint16_)pSpotLight->nAngleY ] ) + pSpotLight->iWinYCenter;
116
117         /* Offset to upper left hand corner. */
118         *pXPos -= pSpotLight->nRadius;
119         *pYPos -= pSpotLight->nRadius;
120 }
121
122
123 void CreateBumps( SBumps *pBumps, Display *pNewDisplay, Window NewWin )
124 {
125         XWindowAttributes XWinAttribs;
126         XGCValues GCValues;
127         int32_ nGCFlags;
128         uint16_ iWidth, iHeight;
129         uint16_ iDiameter;
130         
131         XGetWindowAttributes( pNewDisplay, NewWin, &XWinAttribs );
132         pBumps->iWinWidth = XWinAttribs.width;
133         pBumps->iWinHeight = XWinAttribs.height;
134         pBumps->SpotLight.iWinXCenter = XWinAttribs.width / 2;
135         pBumps->SpotLight.iWinYCenter = XWinAttribs.height / 2;
136         pBumps->pDisplay = pNewDisplay;
137         pBumps->Win = NewWin;
138
139         pBumps->pXImage = XCreateImage( pBumps->pDisplay, XWinAttribs.visual, XWinAttribs.depth, ZPixmap, 0, NULL,
140                 pBumps->iWinWidth, pBumps->iWinHeight, BitmapPad( pBumps->pDisplay ), 0 );
141         pBumps->pXImage->data = calloc( pBumps->pXImage->bytes_per_line * pBumps->pXImage->height, sizeof(int8_) );
142
143         GCValues.function = GXcopy;
144         GCValues.subwindow_mode = IncludeInferiors;
145         nGCFlags = GCForeground | GCFunction;
146         if( use_subwindow_mode_p( XWinAttribs.screen, pBumps->Win ) ) /* See grabscreen.c */
147                 nGCFlags |= GCSubwindowMode;
148         pBumps->GraphicsContext = XCreateGC( pBumps->pDisplay, pBumps->Win, nGCFlags, &GCValues );
149         
150         SetPalette( pBumps, &XWinAttribs );
151         iDiameter = ( ( pBumps->iWinWidth < pBumps->iWinHeight ) ? pBumps->iWinWidth : pBumps->iWinHeight ) / 3;
152         CreateSpotLight( &pBumps->SpotLight, iDiameter, pBumps->nColorCount );
153         InitBumpMap( pBumps, &XWinAttribs );
154
155         /* Clear the image. */
156   if (pBumps->aXColors[ 0 ].pixel == 0)
157     memset (pBumps->pXImage->data, 0,
158             pBumps->pXImage->bytes_per_line * pBumps->pXImage->height);
159   else
160     for( iHeight=0; iHeight<pBumps->iWinHeight; iHeight++ )
161       for( iWidth=0; iWidth<pBumps->iWinWidth; iWidth++ )
162         XPutPixel( pBumps->pXImage, iWidth, iHeight,
163                    pBumps->aXColors[ 0 ].pixel );
164   XSetWindowBackground( pBumps->pDisplay, pBumps->Win,
165                         pBumps->aXColors[ 0 ].pixel );
166   XClearWindow (pBumps->pDisplay, pBumps->Win);
167 }
168
169
170 void SetPalette( SBumps *pBumps, XWindowAttributes *pXWinAttribs )
171 {
172         XColor Color;
173         char *sColor;                   /* Spotlight Color */
174         int16_ iColor;
175         uint32_ *aPixels;
176         
177         sColor = get_string_resource( "color", "Color" );
178
179         Color.red = RANDOM() % 0xFFFF; 
180         Color.green = RANDOM() % 0xFFFF;
181         Color.blue = RANDOM() % 0xFFFF;
182
183         if( strcasecmp( sColor, "random" ) && !XParseColor( pBumps->pDisplay, pXWinAttribs->colormap, sColor, &Color ) )
184                 fprintf( stderr, "%s: color %s not found in database. Choosing random...\n", progname, sColor );
185
186 #ifdef VERBOSE
187         printf( "%s: Spotlight color is <%d,%d,%d> RGB.\n", progclass, Color.red, Color.green, Color.blue );
188 #endif  /*  VERBOSE */
189
190         pBumps->nColorCount = get_integer_resource( "colorcount", "Integer" );
191         if( pBumps->nColorCount < 2 )   pBumps->nColorCount = 2;
192         if( pBumps->nColorCount > 128 ) pBumps->nColorCount = 128;
193
194         pBumps->aXColors = calloc( pBumps->nColorCount, sizeof(XColor ) );
195                 aPixels  = calloc( pBumps->nColorCount, sizeof(uint32_) );
196
197         /* Creates a phong shade:                 / SpotColor  \                               Index/ColorCount 
198          *                                                      PhongShade = | ------------ | Index + ( 65535 - SpotColor )^ 
199          *                                                                                \ ColorCount /                                                                                                */
200         pBumps->nColorCount--;
201         for( iColor=0; iColor<=pBumps->nColorCount; iColor++ )
202         {
203                 pBumps->aXColors[ iColor ].red   = (uint16_)( ( ( Color.red   / (double)pBumps->nColorCount ) * iColor ) + pow( 0xFFFF - Color.red,   iColor/(double)pBumps->nColorCount ) );
204                 pBumps->aXColors[ iColor ].green = (uint16_)( ( ( Color.green / (double)pBumps->nColorCount ) * iColor ) + pow( 0xFFFF - Color.green, iColor/(double)pBumps->nColorCount ) );
205                 pBumps->aXColors[ iColor ].blue  = (uint16_)( ( ( Color.blue  / (double)pBumps->nColorCount ) * iColor ) + pow( 0xFFFF - Color.blue,  iColor/(double)pBumps->nColorCount ) );
206
207                 if( !XAllocColor( pBumps->pDisplay, pXWinAttribs->colormap, &pBumps->aXColors[ iColor ] ) )
208                 {
209                         XFreeColors( pBumps->pDisplay, pXWinAttribs->colormap, aPixels, iColor, 0 );
210                         free( pBumps->aXColors );
211                         free(         aPixels );
212                         pBumps->nColorCount--;
213                         pBumps->aXColors = calloc( pBumps->nColorCount, sizeof(XColor) );
214                         aPixels  = calloc( pBumps->nColorCount, sizeof(uint32_) );
215                         iColor = -1;
216                 }
217                 else
218                         aPixels[ iColor ] = pBumps->aXColors[ iColor ].pixel;
219         }
220         pBumps->nColorCount++;
221
222 #ifdef VERBOSE
223         printf( "%s: Allocated %d colors.\n", progclass, pBumps->nColorCount );
224 #endif  /*  VERBOSE */
225
226         XSetWindowBackground( pBumps->pDisplay, pBumps->Win, pBumps->aXColors[ 0 ].pixel );
227 }
228
229
230 void InitBumpMap( SBumps *pBumps, XWindowAttributes *pXWinAttribs )
231 {
232         XImage *pScreenImage;
233         XColor *aColors;
234         uint8_ nSoften;
235         uint16_ iWidth, iHeight;
236         BOOL bInvert = (BOOL)get_boolean_resource( "invert", "Boolean" );
237         
238         aColors = (XColor*)calloc( pBumps->iWinWidth, sizeof(XColor) );
239         grab_screen_image( pXWinAttribs->screen, pBumps->Win );
240         pScreenImage = XGetImage( pBumps->pDisplay, pBumps->Win, 0, 0, pBumps->iWinWidth, pBumps->iWinHeight, ~0L, ZPixmap );
241
242         /* jwz: get the grabbed bits off the screen fast */
243         XClearWindow (pBumps->pDisplay, pBumps->Win);
244         XSync (pBumps->pDisplay, 0);
245
246         pBumps->aBumpMap = calloc( pBumps->iWinWidth * pBumps->iWinHeight, sizeof(uint16_) );
247         for( iHeight=0; iHeight<pBumps->iWinHeight; iHeight++ )
248         {
249                 for( iWidth=0; iWidth<pBumps->iWinWidth; iWidth++ )
250                         aColors[ iWidth ].pixel = XGetPixel( pScreenImage, iWidth, iHeight );
251
252                 XQueryColors( pBumps->pDisplay, pXWinAttribs->colormap, aColors, pBumps->iWinWidth );
253         
254                 if( bInvert )
255                         for( iWidth=0; iWidth<pBumps->iWinWidth; iWidth++ )
256                                 pBumps->aBumpMap[ ( iHeight * pBumps->iWinWidth ) + iWidth ] = (uint16_)
257                                         ( ( aColors[ iWidth ].red + aColors[ iWidth ].green + aColors[ iWidth ].blue ) / ( 0x2FFFD / (double)pBumps->SpotLight.nDiameter ) );
258                 else
259                         for( iWidth=0; iWidth<pBumps->iWinWidth; iWidth++ )
260                                 pBumps->aBumpMap[ ( iHeight * pBumps->iWinWidth ) + iWidth ] = (uint16_)
261                                         ( pBumps->SpotLight.nDiameter - ( ( aColors[ iWidth ].red + aColors[ iWidth ].green + aColors[ iWidth ].blue ) / ( 0x2FFFD / (double)pBumps->SpotLight.nDiameter ) ) );
262         }
263
264         XDestroyImage( pScreenImage );
265
266         nSoften = get_integer_resource( "soften", "Integer" );
267 #ifdef VERBOSE
268         if( nSoften )   printf( "%s: Softening Bump Map %d time(s)...\n", progclass, nSoften );
269 #endif
270         while( nSoften-- )
271                 SoftenBumpMap( pBumps );
272         free( aColors );
273 }
274
275
276 void SoftenBumpMap( SBumps *pBumps )
277 {
278         uint16_ *pOffset;
279         uint16_ nHeight;
280         uint16_ iWidth, iHeight;
281         uint16_ *pTempBuffer = calloc( pBumps->iWinWidth * pBumps->iWinHeight, sizeof(uint16_) );
282
283         for( iHeight=1; iHeight<pBumps->iWinHeight-1; iHeight++ )
284         {
285                 pOffset = pBumps->aBumpMap + ( iHeight * pBumps->iWinWidth );
286                 for( iWidth=1; iWidth<pBumps->iWinWidth-1; iWidth++ )
287                 {       
288                         nHeight = 0;
289                         nHeight += pOffset[ iWidth ];
290                         nHeight += pOffset[ iWidth - pBumps->iWinWidth ];
291                         nHeight += pOffset[ iWidth + 1 ];
292                         nHeight += pOffset[ iWidth + pBumps->iWinWidth ];
293                         nHeight += pOffset[ iWidth - 1 ];
294                         nHeight /= 5;
295                         pTempBuffer[ ( iHeight * pBumps->iWinWidth ) + iWidth ] = nHeight;
296                 }
297         }                                               
298         
299         memcpy( pBumps->aBumpMap, pTempBuffer, pBumps->iWinWidth * pBumps->iWinHeight * 2 );
300         free( pTempBuffer );
301 }
302
303
304 void Execute( SBumps *pBumps )
305 {
306         uint16_ nLightXPos, nLightYPos;
307         uint16_ iWidth, iHeight;
308         uint16_ iLightWidth, iLightHeight;
309         uint16_ *pBOffset;
310         uint8_ *pLOffset;
311         int16_ nX, nY;
312         uint16_ nColor;
313         CalcLightPos( &pBumps->SpotLight, &nLightXPos, &nLightYPos );
314
315         for( iHeight=nLightYPos, iLightHeight=0; iLightHeight<pBumps->SpotLight.nDiameter; iHeight++, iLightHeight++ )
316         {
317                 pBOffset = pBumps->aBumpMap + ( iHeight * pBumps->iWinWidth );
318                 pLOffset = pBumps->SpotLight.aLightMap + ( iLightHeight * pBumps->SpotLight.nDiameter );
319                 for( iWidth=nLightXPos, iLightWidth=0; iLightWidth<pBumps->SpotLight.nDiameter; iWidth++, iLightWidth++ )
320                 {
321                         if( pLOffset[ iLightWidth ] )
322                         {                               
323                                 nX = pBOffset[ iWidth + 1                 ] - pBOffset[ iWidth ] + iLightWidth;
324                                 nY = pBOffset[ iWidth + pBumps->iWinWidth ] - pBOffset[ iWidth ] + iLightHeight;
325
326                                 if( nX < 0 )                                    nX = 0;
327                                 else if( nX >= pBumps->SpotLight.nDiameter )    nX = pBumps->SpotLight.nDiameter - 1;
328
329                                 if( nY < 0 )                                    nY = 0;
330                                 else if( nY >= pBumps->SpotLight.nDiameter )    nY = pBumps->SpotLight.nDiameter - 1;
331
332                                 nColor = pBumps->SpotLight.aLightMap[ ( nY * pBumps->SpotLight.nDiameter ) + nX ];
333                                 if( nColor >= pBumps->nColorCount )
334                                         nColor = 1;
335
336                                 if( pLOffset[ iLightWidth ] >= pBumps->nColorCount )
337                                         if( nColor > pLOffset[ iLightWidth ] - pBumps->nColorCount )
338                                                 nColor = pLOffset[ iLightWidth ] - pBumps->nColorCount;
339                                                 
340                                 XPutPixel( pBumps->pXImage, iWidth, iHeight, pBumps->aXColors[ nColor ].pixel );
341                         }
342                         else
343                                 XPutPixel( pBumps->pXImage, iWidth, iHeight, pBumps->aXColors[ 0 ].pixel );
344                 }
345         }       
346
347         XPutImage( pBumps->pDisplay, pBumps->Win, pBumps->GraphicsContext, pBumps->pXImage, nLightXPos, nLightYPos, nLightXPos, nLightYPos, pBumps->SpotLight.nDiameter, pBumps->SpotLight.nDiameter );
348         XSync( pBumps->pDisplay, False );
349 }
350
351
352 void DestroyBumps( SBumps *pBumps )
353 {
354         DestroySpotLight( &pBumps->SpotLight );
355         free( pBumps->aXColors );
356         free( pBumps->aBumpMap );
357         XDestroyImage( pBumps->pXImage );
358 }
359
360
361 /* All messages to the screensaver are processed here. */
362 void screenhack( Display *pDisplay, Window Win )
363 {
364         SBumps Bumps;
365         uint32_ iDelay;
366 #ifdef VERBOSE
367         time_t Time = time( NULL );
368         uint16_ iFrame = 0;
369 #endif  /*  VERBOSE */
370         
371         CreateBumps( &Bumps, pDisplay, Win );
372         iDelay = get_integer_resource( "delay", "Integer" );
373
374         while( 1 )
375         {
376                 screenhack_handle_events( pDisplay );
377                 Execute( &Bumps );
378                 usleep( iDelay );
379
380 #ifdef VERBOSE
381                 iFrame++;
382                 if( Time - time( NULL ) )
383                 {
384                         printf( "FPS: %d\n", iFrame );
385                         Time = time( NULL );
386                         iFrame = 0;
387                 }
388 #endif  /*  VERBOSE */
389         }
390
391         DestroyBumps( &Bumps );
392 }
393
394  
395 /*
396  * End of Module: "Bumps.cpp"
397  */
398
399 /* vim: ts=4
400  */