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 #include "Texture.h"
35 #include "Shared.h"
36
37
38 static GLubyte smallTextureArray[32][32];
39 static GLubyte bigTextureArray[256][256][2];
40 GLuint theTexture = 0;
41
42
43 // simple smoothing routine
44 static void
SmoothTexture(void)45 SmoothTexture(void)
46 {
47 GLubyte filter[32][32];
48 float t;
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];
56 t /= 8.0f;
57 filter[i][j] = (GLubyte) t;
58 }
59 }
60
61 for (int i = 1; i < 31; i++){
62 for (int j = 1; j < 31; j++)
63 smallTextureArray[i][j] = filter[i][j];
64 }
65 }
66
67
68 // add some randomness to texture data
69 static void
SpeckleTexture(void)70 SpeckleTexture(void)
71 {
72 int speck;
73 float t;
74 for (int i = 2; i < 30; i++) {
75 for (int j = 2; j < 30; j++) {
76 speck = 1;
77 while (speck <= 32 && random() % 2) {
78 t = (float) MIN_(255, smallTextureArray[i][j] + speck);
79 smallTextureArray[i][j] = (GLubyte)t;
80 speck += speck;
81 }
82 speck = 1;
83 while (speck <= 32 && random() % 2) {
84 t = (float)MAX_(0, smallTextureArray[i][j] - speck);
85 smallTextureArray[i][j] = (GLubyte)t;
86 speck += speck;
87 }
88 }
89 }
90 }
91
92
93 static void
MakeSmallTexture(void)94 MakeSmallTexture(void)
95 {
96 static int firstTime = 1;
97
98 if (firstTime) {
99 firstTime = 0;
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)
103 * (j - 15.5));
104 if (r > 15.0f)
105 smallTextureArray[i][j] = 0;
106 else {
107 float t = 255.0f * (float) cos(r * M_PI / 31.0);
108 smallTextureArray[i][j] = (GLubyte)t;
109 }
110 }
111 }
112 } else {
113 for (int i = 0; i < 32; i++) {
114 for (int j = 0; j < 32; j++) {
115 float t;
116 float r = (float)sqrt((i - 15.5) * (i - 15.5) + (j - 15.5)
117 * (j - 15.5));
118 if (r > 15.0f)
119 t = 0.0f;
120 else
121 t = 255.0f * (float) cos(r*M_PI/31.0);
122
123 smallTextureArray[i][j]
124 = (GLubyte)MIN_(255, (t + smallTextureArray[i][j]
125 + smallTextureArray[i][j]) / 3);
126 }
127 }
128 }
129
130 SpeckleTexture();
131 SmoothTexture();
132 SmoothTexture();
133 }
134
135
136 static void
CopySmallTextureToBigTexture(int k,int l)137 CopySmallTextureToBigTexture(int k, int l)
138 {
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];
143 }
144 }
145 }
146
147
148 static void
AverageLastAndFirstTextures(void)149 AverageLastAndFirstTextures(void)
150 {
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);
155 }
156 }
157 }
158
159
160 void
MakeTexture()161 MakeTexture()
162 {
163 for (int i = 0; i < 8; i++) {
164 for (int j = 0; j < 8; j++) {
165 if (i == 7 && j == 7)
166 AverageLastAndFirstTextures();
167 else
168 MakeSmallTexture();
169
170 CopySmallTextureToBigTexture(i * 32, j * 32);
171 }
172 }
173
174 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
175
176 glGenTextures(1, &theTexture);
177 glBindTexture(GL_TEXTURE_2D, theTexture);
178
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);
182
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);
187
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);
191 }
192