http://svn.poeml.de/viewvc/ppc/src-unpacked/xscreensaver/xscreensaver-4.12.tar.bz2...
[xscreensaver] / hacks / glx / flurry-texture.c
1 /*
2
3 Copyright (c) 2002, Calum Robinson
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8
9 * Redistributions of source code must retain the above copyright notice, this
10   list of conditions and the following disclaimer.
11
12 * Redistributions in binary form must reproduce the above copyright notice,
13   this list of conditions and the following disclaimer in the documentation
14   and/or other materials provided with the distribution.
15
16 * Neither the name of the author nor the names of its contributors may be used
17   to endorse or promote products derived from this software without specific
18   prior written permission.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
24 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 */
32
33 /*
34  *  Texture.c
35  *  AppleFlurry
36  *
37  *  Created by calumr on Sat Jul 07 2001.
38  *  Copyright (c) 2001 __CompanyName__. All rights reserved.
39  *
40  */
41
42 #ifdef HAVE_CONFIG_H
43 # include "config.h"
44 #endif
45
46 #include "flurry.h"
47
48 #include <GL/gl.h>
49 #include <GL/glu.h>
50
51 #include <stdlib.h>
52 #include <math.h>
53
54 static GLubyte smallTextureArray[32][32];
55 static GLubyte bigTextureArray[256][256][2];
56 GLuint theTexture = 0;
57
58 /* simple smoothing routine */
59 static void SmoothTexture(void)
60 {
61     GLubyte filter[32][32];
62     int i,j;
63     float t;
64     for (i=1;i<31;i++)
65     {
66         for (j=1;j<31;j++)
67         {
68             t = (float) smallTextureArray[i][j]*4;
69             t += (float) smallTextureArray[i-1][j];
70             t += (float) smallTextureArray[i+1][j];
71             t += (float) smallTextureArray[i][j-1];
72             t += (float) smallTextureArray[i][j+1];
73             t /= 8.0f;
74             filter[i][j] = (GLubyte) t;
75         }
76     }
77     for (i=1;i<31;i++)
78     {
79         for (j=1;j<31;j++)
80         {
81             smallTextureArray[i][j] = filter[i][j];
82         }
83     }
84 }
85
86 /* add some randomness to texture data */
87 static void SpeckleTexture(void)
88 {
89     int i,j;
90     int speck;
91     float t;
92     for (i=2;i<30;i++)
93     {
94         for (j=2;j<30;j++)
95         {
96             speck = 1;
97             while (speck <= 32 && random() % 2)
98             {
99                 t = (float) MIN_(255,smallTextureArray[i][j]+speck);
100                 smallTextureArray[i][j] = (GLubyte) t;
101                 speck+=speck;
102             }
103             speck = 1;
104             while (speck <= 32 && random() % 2)
105             {
106                 t = (float) MAX_(0,smallTextureArray[i][j]-speck);
107                 smallTextureArray[i][j] = (GLubyte) t;
108                 speck+=speck;
109             }
110         }
111     }
112 }
113
114 static void MakeSmallTexture(void)
115 {
116     static int firstTime = 1;
117     int i,j;
118     float r,t;
119     if (firstTime)
120     {
121         firstTime = 0;
122         for (i=0;i<32;i++)
123         {
124             for (j=0;j<32;j++)
125             {
126                 r = (float) sqrt((i-15.5)*(i-15.5)+(j-15.5)*(j-15.5));
127                 if (r > 15.0f)
128                 {
129                     smallTextureArray[i][j] = 0;
130                 }
131                 else
132                 {
133                     t = 255.0f * (float) cos(r*M_PI/31.0);
134                     smallTextureArray[i][j] = (GLubyte) t;
135                 }
136             }
137         }
138     }
139     else
140     {
141         for (i=0;i<32;i++)
142         {
143             for (j=0;j<32;j++)
144             {
145                 r = (float) sqrt((i-15.5)*(i-15.5)+(j-15.5)*(j-15.5));
146                 if (r > 15.0f)
147                 {
148                     t = 0.0f;
149                 }
150                 else
151                 {
152                     t = 255.0f * (float) cos(r*M_PI/31.0);
153                 }
154                 smallTextureArray[i][j] = (GLubyte) MIN_(255,(t+smallTextureArray[i][j]+smallTextureArray[i][j])/3);
155             }
156         }
157     }
158     SpeckleTexture();
159     SmoothTexture();
160     SmoothTexture();
161 }
162
163 static void CopySmallTextureToBigTexture(int k, int l)
164 {
165     int i,j;
166     for (i=0;i<32;i++)
167     {
168         for (j=0;j<32;j++)
169         {
170             bigTextureArray[i+k][j+l][0] = smallTextureArray[i][j];
171             bigTextureArray[i+k][j+l][1] = smallTextureArray[i][j];
172         }
173     }
174 }
175
176 static void AverageLastAndFirstTextures(void)
177 {
178     int i,j;
179     int t;
180     for (i=0;i<32;i++)
181     {
182         for (j=0;j<32;j++)
183         {
184             t = (smallTextureArray[i][j] + bigTextureArray[i][j][0]) / 2;
185             smallTextureArray[i][j] = (GLubyte) MIN_(255,t);
186         }
187     }
188 }
189
190 void MakeTexture()
191 {
192     int i,j;
193     for (i=0;i<8;i++)
194     {
195         for (j=0;j<8;j++)
196         {
197             if (i==7 && j==7)
198             {
199                 AverageLastAndFirstTextures();
200             }
201             else
202             {
203                 MakeSmallTexture();
204             }
205             CopySmallTextureToBigTexture(i*32,j*32);
206         }
207     }
208
209     glPixelStorei(GL_UNPACK_ALIGNMENT,1);
210
211     glGenTextures(1, &theTexture);
212     glBindTexture(GL_TEXTURE_2D, theTexture);
213
214     /* Set the tiling mode (this is generally always GL_REPEAT). */
215     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
216     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
217
218     /* Set the filtering. */
219     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
220     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
221
222     gluBuild2DMipmaps(GL_TEXTURE_2D, 2, 256, 256, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, bigTextureArray);
223     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
224 }