1 /* Bumps, Copyright (c) 1999 Shane Smit <blackend@inconnect.com>
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
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.
19 * Essentially, it 3D-izes your desktop, based on color intensity.
21 * Modification History:
22 * [10/01/99] - Shane Smit: Creation
23 * [10/08/99] - Shane Smit: Port to C. (Ick)
30 void CreateSpotLight( SSpotLight *pSpotLight, uint16_ iWinWidth, uint16_ nColorCount )
33 int16_ iHeight, iWidth;
35 pSpotLight->nDiameter = iWinWidth / 3;
37 printf( "%s: Light Diameter: %d\n", progclass, pSpotLight->nDiameter );
40 pSpotLight->aLightMap = calloc( pSpotLight->nDiameter * pSpotLight->nDiameter, sizeof(uint8_) );
41 memset( pSpotLight->aLightMap, 0, pSpotLight->nDiameter * pSpotLight->nDiameter );
43 /* The falloff max values... 3/4 of the entire lightmap. */
44 pSpotLight->nRadius = (uint16_)(pSpotLight->nDiameter / 2.5F);
46 for( iHeight=-pSpotLight->nRadius; iHeight<pSpotLight->nRadius; iHeight++ )
47 for( iWidth=-pSpotLight->nRadius; iWidth<pSpotLight->nRadius; iWidth++ )
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;
56 /* The actual lightmap... 1/2 of the entire lightmap. */
57 pSpotLight->nRadius = pSpotLight->nDiameter / 4;
59 for( iHeight=-pSpotLight->nRadius; iHeight<pSpotLight->nRadius; iHeight++ )
60 for( iWidth=-pSpotLight->nRadius; iWidth<pSpotLight->nRadius; iWidth++ )
62 nDelta = nColorCount - ( ( sqrt( pow( iWidth+0.5F, 2 ) + pow( iHeight+0.5F, 2 ) ) / pSpotLight->nRadius ) * ( nColorCount - 1 ) );
64 pSpotLight->aLightMap[ ( ( iHeight + (pSpotLight->nDiameter/2) ) * pSpotLight->nDiameter ) + iWidth + (pSpotLight->nDiameter/2) ] = (uint8_)nDelta;
67 CreateTables( pSpotLight );
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;
77 void CreateTables( SSpotLight *pSpotLight )
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. */
85 printf( "%s: Using a %d degree circle.\n", progclass, pSpotLight->nDegreeCount );
88 pSpotLight->aSinTable = calloc( pSpotLight->nDegreeCount, sizeof(double) );
90 /* This funtion builds the Sine Lookup Tables. */
91 nUnit = (double)( PI * 2.0F ) / (double)( pSpotLight->nDegreeCount );
93 for( iDegree=0; iDegree<pSpotLight->nDegreeCount; iDegree++)
94 pSpotLight->aSinTable[ iDegree ] = sin( nUnit * (double)iDegree );
98 void CalcLightPos( SSpotLight *pSpotLight, uint16_ *pXPos, uint16_ *pYPos )
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;
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;
108 pSpotLight->nAngleX += pSpotLight->nVelocityX;
109 if( pSpotLight->nAngleX >= pSpotLight->nDegreeCount ) pSpotLight->nAngleX -= pSpotLight->nDegreeCount;
111 pSpotLight->nAngleY += pSpotLight->nVelocityY;
112 if( pSpotLight->nAngleY >= pSpotLight->nDegreeCount ) pSpotLight->nAngleY -= pSpotLight->nDegreeCount;
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;
117 /* Offset to upper left hand corner. */
118 *pXPos -= pSpotLight->nRadius;
119 *pYPos -= pSpotLight->nRadius;
123 void CreateBumps( SBumps *pBumps, Display *pNewDisplay, Window NewWin )
125 XWindowAttributes XWinAttribs;
128 uint16_ iWidth, iHeight;
130 XGetWindowAttributes( pNewDisplay, NewWin, &XWinAttribs );
131 pBumps->iWinWidth = XWinAttribs.width;
132 pBumps->iWinHeight = XWinAttribs.height;
133 pBumps->SpotLight.iWinXCenter = XWinAttribs.width / 2;
134 pBumps->SpotLight.iWinYCenter = XWinAttribs.height / 2;
135 pBumps->pDisplay = pNewDisplay;
136 pBumps->Win = NewWin;
138 pBumps->pXImage = XCreateImage( pBumps->pDisplay, XWinAttribs.visual, XWinAttribs.depth, ZPixmap, 0, NULL,
139 pBumps->iWinWidth, pBumps->iWinHeight, BitmapPad( pBumps->pDisplay ), 0 );
140 pBumps->pXImage->data = calloc( pBumps->pXImage->bytes_per_line * pBumps->pXImage->height, sizeof(int8_) );
142 GCValues.function = GXcopy;
143 GCValues.subwindow_mode = IncludeInferiors;
144 nGCFlags = GCForeground | GCFunction;
145 if( use_subwindow_mode_p( XWinAttribs.screen, pBumps->Win ) ) /* See grabscreen.c */
146 nGCFlags |= GCSubwindowMode;
147 pBumps->GraphicsContext = XCreateGC( pBumps->pDisplay, pBumps->Win, nGCFlags, &GCValues );
149 SetPalette( pBumps, &XWinAttribs );
150 CreateSpotLight( &pBumps->SpotLight, pBumps->iWinWidth, pBumps->nColorCount );
151 InitBumpMap( pBumps, &XWinAttribs );
153 /* Clear the image. */
154 if (pBumps->aXColors[ 0 ].pixel == 0)
155 memset (pBumps->pXImage->data, 0,
156 pBumps->pXImage->bytes_per_line * pBumps->pXImage->height);
158 for( iHeight=0; iHeight<pBumps->iWinHeight; iHeight++ )
159 for( iWidth=0; iWidth<pBumps->iWinWidth; iWidth++ )
160 XPutPixel( pBumps->pXImage, iWidth, iHeight,
161 pBumps->aXColors[ 0 ].pixel );
162 XSetWindowBackground( pBumps->pDisplay, pBumps->Win,
163 pBumps->aXColors[ 0 ].pixel );
164 XClearWindow (pBumps->pDisplay, pBumps->Win);
168 void SetPalette( SBumps *pBumps, XWindowAttributes *pXWinAttribs )
171 char *sColor; /* Spotlight Color */
175 sColor = get_string_resource( "color", "Color" );
177 Color.red = RANDOM() % 0xFFFF;
178 Color.green = RANDOM() % 0xFFFF;
179 Color.blue = RANDOM() % 0xFFFF;
181 if( strcasecmp( sColor, "random" ) && !XParseColor( pBumps->pDisplay, pXWinAttribs->colormap, sColor, &Color ) )
182 fprintf( stderr, "%s: color %s not found in database. Choosing random...\n", progname, sColor );
185 printf( "%s: Spotlight color is <%d,%d,%d> RGB.\n", progclass, Color.red, Color.green, Color.blue );
188 pBumps->nColorCount = get_integer_resource( "colorcount", "Integer" );
189 if( pBumps->nColorCount < 2 ) pBumps->nColorCount = 2;
190 if( pBumps->nColorCount > 128 ) pBumps->nColorCount = 128;
192 pBumps->aXColors = calloc( pBumps->nColorCount, sizeof(XColor ) );
193 aPixels = calloc( pBumps->nColorCount, sizeof(uint32_) );
195 /* Creates a phong shade: / SpotColor \ Index/ColorCount
196 * PhongShade = | ------------ | Index + ( 65535 - SpotColor )^
198 pBumps->nColorCount--;
199 for( iColor=0; iColor<=pBumps->nColorCount; iColor++ )
201 pBumps->aXColors[ iColor ].red = (uint16_)( ( ( Color.red / (double)pBumps->nColorCount ) * iColor ) + pow( 0xFFFF - Color.red, iColor/(double)pBumps->nColorCount ) );
202 pBumps->aXColors[ iColor ].green = (uint16_)( ( ( Color.green / (double)pBumps->nColorCount ) * iColor ) + pow( 0xFFFF - Color.green, iColor/(double)pBumps->nColorCount ) );
203 pBumps->aXColors[ iColor ].blue = (uint16_)( ( ( Color.blue / (double)pBumps->nColorCount ) * iColor ) + pow( 0xFFFF - Color.blue, iColor/(double)pBumps->nColorCount ) );
205 if( !XAllocColor( pBumps->pDisplay, pXWinAttribs->colormap, &pBumps->aXColors[ iColor ] ) )
207 XFreeColors( pBumps->pDisplay, pXWinAttribs->colormap, aPixels, iColor, 0 );
208 free( pBumps->aXColors );
210 pBumps->nColorCount--;
211 pBumps->aXColors = calloc( pBumps->nColorCount, sizeof(XColor) );
212 aPixels = calloc( pBumps->nColorCount, sizeof(uint32_) );
216 aPixels[ iColor ] = pBumps->aXColors[ iColor ].pixel;
218 pBumps->nColorCount++;
221 printf( "%s: Allocated %d colors.\n", progclass, pBumps->nColorCount );
224 XSetWindowBackground( pBumps->pDisplay, pBumps->Win, pBumps->aXColors[ 0 ].pixel );
228 void InitBumpMap( SBumps *pBumps, XWindowAttributes *pXWinAttribs )
230 XImage *pScreenImage;
233 uint16_ iWidth, iHeight;
234 BOOL bInvert = (BOOL)get_boolean_resource( "invert", "Boolean" );
236 aColors = (XColor*)calloc( pBumps->iWinWidth, sizeof(XColor) );
237 grab_screen_image( pXWinAttribs->screen, pBumps->Win );
238 pScreenImage = XGetImage( pBumps->pDisplay, pBumps->Win, 0, 0, pBumps->iWinWidth, pBumps->iWinHeight, ~0L, ZPixmap );
240 /* jwz: get the grabbed bits off the screen fast */
241 XClearWindow (pBumps->pDisplay, pBumps->Win);
242 XSync (pBumps->pDisplay, 0);
244 pBumps->aBumpMap = calloc( pBumps->iWinWidth * pBumps->iWinHeight, sizeof(uint16_) );
245 for( iHeight=0; iHeight<pBumps->iWinHeight; iHeight++ )
247 for( iWidth=0; iWidth<pBumps->iWinWidth; iWidth++ )
248 aColors[ iWidth ].pixel = XGetPixel( pScreenImage, iWidth, iHeight );
250 XQueryColors( pBumps->pDisplay, pXWinAttribs->colormap, aColors, pBumps->iWinWidth );
253 for( iWidth=0; iWidth<pBumps->iWinWidth; iWidth++ )
254 pBumps->aBumpMap[ ( iHeight * pBumps->iWinWidth ) + iWidth ] = (uint16_)
255 ( ( aColors[ iWidth ].red + aColors[ iWidth ].green + aColors[ iWidth ].blue ) / ( 0x2FFFD / (double)pBumps->SpotLight.nDiameter ) );
257 for( iWidth=0; iWidth<pBumps->iWinWidth; iWidth++ )
258 pBumps->aBumpMap[ ( iHeight * pBumps->iWinWidth ) + iWidth ] = (uint16_)
259 ( pBumps->SpotLight.nDiameter - ( ( aColors[ iWidth ].red + aColors[ iWidth ].green + aColors[ iWidth ].blue ) / ( 0x2FFFD / (double)pBumps->SpotLight.nDiameter ) ) );
262 XDestroyImage( pScreenImage );
264 nSoften = get_integer_resource( "soften", "Integer" );
266 if( nSoften ) printf( "%s: Softening Bump Map %d time(s)...\n", progclass, nSoften );
269 SoftenBumpMap( pBumps );
274 void SoftenBumpMap( SBumps *pBumps )
278 uint16_ iWidth, iHeight;
279 uint16_ *pTempBuffer = calloc( pBumps->iWinWidth * pBumps->iWinHeight, sizeof(uint16_) );
281 for( iHeight=1; iHeight<pBumps->iWinHeight-1; iHeight++ )
283 pOffset = pBumps->aBumpMap + ( iHeight * pBumps->iWinWidth );
284 for( iWidth=1; iWidth<pBumps->iWinWidth-1; iWidth++ )
287 nHeight += pOffset[ iWidth ];
288 nHeight += pOffset[ iWidth - pBumps->iWinWidth ];
289 nHeight += pOffset[ iWidth + 1 ];
290 nHeight += pOffset[ iWidth + pBumps->iWinWidth ];
291 nHeight += pOffset[ iWidth - 1 ];
293 pTempBuffer[ ( iHeight * pBumps->iWinWidth ) + iWidth ] = nHeight;
297 memcpy( pBumps->aBumpMap, pTempBuffer, pBumps->iWinWidth * pBumps->iWinHeight * 2 );
302 void Execute( SBumps *pBumps )
304 uint16_ nLightXPos, nLightYPos;
305 uint16_ iWidth, iHeight;
306 uint16_ iLightWidth, iLightHeight;
311 CalcLightPos( &pBumps->SpotLight, &nLightXPos, &nLightYPos );
313 for( iHeight=nLightYPos, iLightHeight=0; iLightHeight<pBumps->SpotLight.nDiameter; iHeight++, iLightHeight++ )
315 pBOffset = pBumps->aBumpMap + ( iHeight * pBumps->iWinWidth );
316 pLOffset = pBumps->SpotLight.aLightMap + ( iLightHeight * pBumps->SpotLight.nDiameter );
317 for( iWidth=nLightXPos, iLightWidth=0; iLightWidth<pBumps->SpotLight.nDiameter; iWidth++, iLightWidth++ )
319 if( pLOffset[ iLightWidth ] )
321 nX = pBOffset[ iWidth + 1 ] - pBOffset[ iWidth ] + iLightWidth;
322 nY = pBOffset[ iWidth + pBumps->iWinWidth ] - pBOffset[ iWidth ] + iLightHeight;
325 else if( nX >= pBumps->SpotLight.nDiameter ) nX = pBumps->SpotLight.nDiameter - 1;
328 else if( nY >= pBumps->SpotLight.nDiameter ) nY = pBumps->SpotLight.nDiameter - 1;
330 nColor = pBumps->SpotLight.aLightMap[ ( nY * pBumps->SpotLight.nDiameter ) + nX ];
331 if( nColor >= pBumps->nColorCount )
334 if( pLOffset[ iLightWidth ] >= pBumps->nColorCount )
335 if( nColor > pLOffset[ iLightWidth ] - pBumps->nColorCount )
336 nColor = pLOffset[ iLightWidth ] - pBumps->nColorCount;
338 XPutPixel( pBumps->pXImage, iWidth, iHeight, pBumps->aXColors[ nColor ].pixel );
341 XPutPixel( pBumps->pXImage, iWidth, iHeight, pBumps->aXColors[ 0 ].pixel );
345 XPutImage( pBumps->pDisplay, pBumps->Win, pBumps->GraphicsContext, pBumps->pXImage, nLightXPos, nLightYPos, nLightXPos, nLightYPos, pBumps->SpotLight.nDiameter, pBumps->SpotLight.nDiameter );
346 XSync( pBumps->pDisplay, False );
350 void DestroyBumps( SBumps *pBumps )
352 DestroySpotLight( &pBumps->SpotLight );
353 free( pBumps->aXColors );
354 free( pBumps->aBumpMap );
355 XDestroyImage( pBumps->pXImage );
359 /* All messages to the screensaver are processed here. */
360 void screenhack( Display *pDisplay, Window Win )
365 time_t Time = time( NULL );
369 CreateBumps( &Bumps, pDisplay, Win );
370 iDelay = get_integer_resource( "delay", "Integer" );
374 screenhack_handle_events( pDisplay );
380 if( Time - time( NULL ) )
382 printf( "FPS: %d\n", iFrame );
389 DestroyBumps( &Bumps );
394 * End of Module: "Bumps.cpp"