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 #include "Texture.h" 33 #include "Shared.h" 34 35 static GLubyte smallTextureArray[32][32]; 36 static GLubyte bigTextureArray[256][256][2]; 37 GLuint theTexture = 0; 38 39 /* simple smoothing routine */ 40 static void SmoothTexture(void) 41 { 42 GLubyte filter[32][32]; 43 int i,j; 44 float t; 45 for (i=1;i<31;i++) 46 { 47 for (j=1;j<31;j++) 48 { 49 t = (float) smallTextureArray[i][j]*4; 50 t += (float) smallTextureArray[i-1][j]; 51 t += (float) smallTextureArray[i+1][j]; 52 t += (float) smallTextureArray[i][j-1]; 53 t += (float) smallTextureArray[i][j+1]; 54 t /= 8.0f; 55 filter[i][j] = (GLubyte) t; 56 } 57 } 58 for (i=1;i<31;i++) 59 { 60 for (j=1;j<31;j++) 61 { 62 smallTextureArray[i][j] = filter[i][j]; 63 } 64 } 65 } 66 67 /* add some randomness to texture data */ 68 static void SpeckleTexture(void) 69 { 70 int i,j; 71 int speck; 72 float t; 73 for (i=2;i<30;i++) 74 { 75 for (j=2;j<30;j++) 76 { 77 speck = 1; 78 while (speck <= 32 && random() % 2) 79 { 80 t = (float) MIN_(255,smallTextureArray[i][j]+speck); 81 smallTextureArray[i][j] = (GLubyte) t; 82 speck+=speck; 83 } 84 speck = 1; 85 while (speck <= 32 && random() % 2) 86 { 87 t = (float) MAX_(0,smallTextureArray[i][j]-speck); 88 smallTextureArray[i][j] = (GLubyte) t; 89 speck+=speck; 90 } 91 } 92 } 93 } 94 95 static void MakeSmallTexture(void) 96 { 97 static int firstTime = 1; 98 int i,j; 99 float r,t; 100 if (firstTime) 101 { 102 firstTime = 0; 103 for (i=0;i<32;i++) 104 { 105 for (j=0;j<32;j++) 106 { 107 r = (float) sqrt((i-15.5)*(i-15.5)+(j-15.5)*(j-15.5)); 108 if (r > 15.0f) 109 { 110 smallTextureArray[i][j] = 0; 111 } 112 else 113 { 114 t = 255.0f * (float) cos(r*M_PI/31.0); 115 smallTextureArray[i][j] = (GLubyte) t; 116 } 117 } 118 } 119 } 120 else 121 { 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 t = 0.0f; 130 } 131 else 132 { 133 t = 255.0f * (float) cos(r*M_PI/31.0); 134 } 135 smallTextureArray[i][j] = (GLubyte) MIN_(255,(t+smallTextureArray[i][j]+smallTextureArray[i][j])/3); 136 } 137 } 138 } 139 SpeckleTexture(); 140 SmoothTexture(); 141 SmoothTexture(); 142 } 143 144 static void CopySmallTextureToBigTexture(int k, int l) 145 { 146 int i,j; 147 for (i=0;i<32;i++) 148 { 149 for (j=0;j<32;j++) 150 { 151 bigTextureArray[i+k][j+l][0] = smallTextureArray[i][j]; 152 bigTextureArray[i+k][j+l][1] = smallTextureArray[i][j]; 153 } 154 } 155 } 156 157 static void AverageLastAndFirstTextures(void) 158 { 159 int i,j; 160 int t; 161 for (i=0;i<32;i++) 162 { 163 for (j=0;j<32;j++) 164 { 165 t = (smallTextureArray[i][j] + bigTextureArray[i][j][0]) / 2; 166 smallTextureArray[i][j] = (GLubyte) MIN_(255,t); 167 } 168 } 169 } 170 171 void MakeTexture() 172 { 173 int i,j; 174 for (i=0;i<8;i++) 175 { 176 for (j=0;j<8;j++) 177 { 178 if (i==7 && j==7) 179 { 180 AverageLastAndFirstTextures(); 181 } 182 else 183 { 184 MakeSmallTexture(); 185 } 186 CopySmallTextureToBigTexture(i*32,j*32); 187 } 188 } 189 190 glPixelStorei(GL_UNPACK_ALIGNMENT,1); 191 192 glGenTextures(1, &theTexture); 193 glBindTexture(GL_TEXTURE_2D, theTexture); 194 195 /* Set the tiling mode (this is generally always GL_REPEAT). */ 196 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 197 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 198 199 /* Set the filtering. */ 200 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 201 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); 202 203 gluBuild2DMipmaps(GL_TEXTURE_2D, 2, 256, 256, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, bigTextureArray); 204 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 205 } 206