3 Copyright (c) 2002, Calum Robinson
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
9 * Redistributions of source code must retain the above copyright notice, this
10 list of conditions and the following disclaimer.
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.
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.
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.
38 static GLubyte smallTextureArray
[32][32];
39 static GLubyte bigTextureArray
[256][256][2];
40 GLuint theTexture
= 0;
43 // simple smoothing routine
47 GLubyte filter
[32][32];
49 for (int i
= 1; i
< 31; i
++) {
50 for (int j
= 1; j
< 31; j
++) {
51 t
= (float)smallTextureArray
[i
][j
] * 4;
52 t
+= (float)smallTextureArray
[i
- 1][j
];
53 t
+= (float)smallTextureArray
[i
+ 1][j
];
54 t
+= (float)smallTextureArray
[i
][j
- 1];
55 t
+= (float)smallTextureArray
[i
][j
+ 1];
57 filter
[i
][j
] = (GLubyte
) t
;
61 for (int i
= 1; i
< 31; i
++){
62 for (int j
= 1; j
< 31; j
++)
63 smallTextureArray
[i
][j
] = filter
[i
][j
];
68 // add some randomness to texture data
74 for (int i
= 2; i
< 30; i
++) {
75 for (int j
= 2; j
< 30; j
++) {
77 while (speck
<= 32 && random() % 2) {
78 t
= (float) MIN_(255, smallTextureArray
[i
][j
] + speck
);
79 smallTextureArray
[i
][j
] = (GLubyte
)t
;
83 while (speck
<= 32 && random() % 2) {
84 t
= (float)MAX_(0, smallTextureArray
[i
][j
] - speck
);
85 smallTextureArray
[i
][j
] = (GLubyte
)t
;
94 MakeSmallTexture(void)
96 static int firstTime
= 1;
100 for (int i
= 0; i
< 32; i
++) {
101 for (int j
= 0; j
< 32; j
++) {
102 float r
= (float)sqrt((i
- 15.5) * (i
- 15.5) + (j
- 15.5)
105 smallTextureArray
[i
][j
] = 0;
107 float t
= 255.0f
* (float) cos(r
* M_PI
/ 31.0);
108 smallTextureArray
[i
][j
] = (GLubyte
)t
;
113 for (int i
= 0; i
< 32; i
++) {
114 for (int j
= 0; j
< 32; j
++) {
116 float r
= (float)sqrt((i
- 15.5) * (i
- 15.5) + (j
- 15.5)
121 t
= 255.0f
* (float) cos(r
*M_PI
/31.0);
123 smallTextureArray
[i
][j
]
124 = (GLubyte
)MIN_(255, (t
+ smallTextureArray
[i
][j
]
125 + smallTextureArray
[i
][j
]) / 3);
137 CopySmallTextureToBigTexture(int k
, int l
)
139 for (int i
= 0; i
< 32; i
++) {
140 for (int j
= 0; j
< 32; j
++) {
141 bigTextureArray
[i
+ k
][j
+ l
][0] = smallTextureArray
[i
][j
];
142 bigTextureArray
[i
+ k
][j
+ l
][1] = smallTextureArray
[i
][j
];
149 AverageLastAndFirstTextures(void)
151 for (int i
= 0; i
< 32; i
++) {
152 for (int j
= 0; j
< 32; j
++) {
153 int t
= (smallTextureArray
[i
][j
] + bigTextureArray
[i
][j
][0]) / 2;
154 smallTextureArray
[i
][j
] = (GLubyte
)MIN_(255, t
);
163 for (int i
= 0; i
< 8; i
++) {
164 for (int j
= 0; j
< 8; j
++) {
165 if (i
== 7 && j
== 7)
166 AverageLastAndFirstTextures();
170 CopySmallTextureToBigTexture(i
* 32, j
* 32);
174 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
176 glGenTextures(1, &theTexture
);
177 glBindTexture(GL_TEXTURE_2D
, theTexture
);
179 // Set the tiling mode (this is generally always GL_REPEAT).
180 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
181 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
183 // Set the filtering.
184 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
185 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
,
186 GL_LINEAR_MIPMAP_NEAREST
);
188 gluBuild2DMipmaps(GL_TEXTURE_2D
, 2, 256, 256, GL_LUMINANCE_ALPHA
,
189 GL_UNSIGNED_BYTE
, bigTextureArray
);
190 glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);