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