/
texgen.cpp
295 lines (272 loc) · 7.48 KB
/
texgen.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<GL/gl.h>
#include<GL/glu.h>
#include <algorithm>
#include"texgen.hpp"
#include "timef.h"
using namespace std;
namespace {
const int NSIZE = 512;
double noise[NSIZE][NSIZE];
void genNoise()
{
for(int i=0; i<NSIZE; ++i)
for(int j=0; j<NSIZE; ++j)
noise[i][j] = randf();
}
double interp(double x,double y,double a)
{
double d = sin(a*M_PI/2);
return d*x+(1-d)*y;
}
double smooth(double x, double y)
{
int ix = int(x), iy = int(y);
double fx = x-ix;
double fy = y-iy;
int ix2 = std::min(ix+1, NSIZE-1);
int iy2 = std::min(iy+1, NSIZE-1);
double n1 = noise[iy][ix];
double n2 = noise[iy][ix2];
double n3 = noise[iy2][ix];
double n4 = noise[iy2][ix2];
double xr1 = interp(n4,n3,fx);
double xr2 = interp(n2,n1,fx);
return interp(xr1,xr2,fy);
return fx*fy*n4 + (1.0-fx)*fy*n3 + fx*(1.0-fy)*n2 + (1.0-fx)*(1.0-fy)*n1;
}
double turbulence(double x, double y, double size)
{
double value = 0.0, asize = size;
while(size >= 1.0) {
value += smooth(x/size, y/size) * size;
size /= 2;
}
return value / asize;
}
unsigned makeTex(void* ptr, int TS, GLenum type)
{
unsigned t;
glGenTextures(1, &t);
glBindTexture(GL_TEXTURE_2D, t);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_NEAREST );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
gluBuild2DMipmaps( GL_TEXTURE_2D, 4,TS,TS, GL_RGBA, type, ptr );
glBindTexture(GL_TEXTURE_2D,0);
return t;
}
static Texture getAmmo()
{
Texture ret;
ret.w = 32;
ret.h = 32;
ret.pix = new unsigned[ret.w*ret.h];
float t[32][32][4];
for(int x=0;x<ret.w;x++)
{
for(int y=0;y<ret.h;y++)
{
float z = (ret.w/2+0.5-x);
z = z*z+(ret.h/2-y)*(ret.h/2-y);
if(z!=0)
z = 420000.0/(z*z);
int iz = std::min(int(z),255);
if(iz<5)iz=0;
for(int i=0; i<4; ++i) t[y][x][i]=iz/255.;
// ret.pix[y*ret.w+x]=iz|iz<<24|(iz)<<16|iz<<8;
//ret.pix[y*ret.w+x]=-1;//iz|iz<<24|(iz)<<16|iz<<8;
}
}
ret.glid = makeTex(t, ret.w, GL_FLOAT);
// ret.glid = 0;
return ret;
}
static Texture getSalama()
{
Texture ret;
ret.w = 64;
ret.h = 64;
ret.pix = new unsigned[ret.w*ret.h];
float t[64][64][4];
for(int x=0;x<ret.w;x++)
{
for(int y=0;y<ret.h;y++)
{
float z = (ret.w/2+0.5-x);
z = abs(z);
if(z!=0)
z = 200.0/(z);
else z = 1000;
z+=z*(0.1*(rand()%10));
//int iz = std::min(int(z),700);
int iz = z;
iz=(iz+70)/2;
if(iz<10)iz=0;
ret.pix[y*ret.w+x]=(std::max(0,std::min(255,iz*2-80)))|std::min(255,iz/2)<<24|std::min(255,iz/2)<<16|std::min(255,int(sqrt(10000*iz)))<<8;
t[y][x][3] = (iz*2-80)/255.;
t[y][x][2] = sqrt(10000*iz)/255.;
t[y][x][1] = iz/2./255.;
t[y][x][0] = iz/2./255.;
for(int i=0; i<4; ++i) t[y][x][i]=max(0.f,min(1.f,t[y][x][i]));
}
}
ret.glid = makeTex(t, ret.w, GL_FLOAT);
return ret;
}
GLuint genExplosionTex()
{
const int TS=256;
float texture[TS][TS][4];
genNoise();
for(int i=0; i<TS; ++i) {
for(int j=0; j<TS; ++j) {
// double x=2.*(j-TS/2)/TS;
// double y=2.*(i-TS/2)/TS;
double x=double(j)/TS, y=double(i)/TS;
texture[i][j][0] = .6+.2*randf();
texture[i][j][1] = .3+.5*randf();
texture[i][j][2] = .2*randf();
// texture[i][j][3] = max(0.0, turbulence(x,y,64) + .5);
double d = (x-.5)*(x-.5) + (y-.5)*(y-.5);
double f = min(1., .05/d);
const double B=.2;
f = min(f, max(0.0, .5/(.5-B) - sqrt(d)/B));
// f = min(f, min(min(x,1-x),min(y,1-y))/B);
const double F=128;
texture[i][j][3] = .4*f*(turbulence(F*x,F*y,16)-.1);
}
}
return makeTex(texture, TS, GL_FLOAT);
}
GLuint genGroundTex()
{
const int TS=256;
static float texture[TS][TS][4];
genNoise();
for(int i=0; i<TS; ++i) {
for(int j=0; j<TS; ++j) {
double x=double(j)/TS, y=double(i)/TS;
//if(x>0.5)x = 1-x;
//if(y>0.5)y = 1-y;
double s = turbulence(128*x,128*y,32);
//s=0;
int is =0;
float z = (i/32+j/32)%2;
int xx= i%32;
int yy= j%32;
const int bev = 10;
int f = (i/32)*37+(j/32)*12;
int foo = std::max(abs(16-xx),abs(16-yy));
if(f%10==0)z=0;
#define FF if(xx+yy<bev)z=0
FF;
xx = 31-xx;
FF;
yy = 31-yy;
FF;
xx = 31-xx;
FF;
if((int(s*20)%2))is=1;
texture[i][j][0] = z*0.0+is*0.01;
texture[i][j][1] = z*0.4+is*0.1;
texture[i][j][2] = z*0.8+is*0.1;
texture[i][j][3] =1;
}
}
return makeTex(texture, TS, GL_FLOAT);
}
GLuint genBuildingTex()
{
const int TS=64;
static float texture[TS][TS][4];
genNoise();
for(int i=0; i<TS; ++i) {
for(int j=0; j<TS; ++j) {
double x=double(j)/TS, y=double(i)/TS;
//if(x>0.5)x = 1-x;
//if(y>0.5)y = 1-y;
//double s = abs(turbulence(28*x,28*y,16));
int s = i==0||j==0||i==TS-1||j==TS-1;// (i/32+j/32)%2;
s=!s;
texture[i][j][0] = s*0.0+(!s)*0.1;
texture[i][j][1] = s*0.0+(!s)*0.4;
texture[i][j][2] = s*0.0+(!s)*0.9;
texture[i][j][3] =1;
}
}
GLuint t;
glGenTextures(1, &t);
glBindTexture(GL_TEXTURE_2D, t);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_NEAREST );
// when texture area is large, bilinear filter the original
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
std::cout<<"mipmaps = "<<gluBuild2DMipmaps( GL_TEXTURE_2D, 4,TS,TS,
GL_RGBA, GL_FLOAT, texture )<<'\n';
glBindTexture(GL_TEXTURE_2D,0);
return t;
}
GLuint genHealthTex()
{
const int TS=64;
static float texture[TS][TS][4];
genNoise();
for(int i=0; i<TS; ++i) {
for(int j=0; j<TS; ++j) {
double x=double(j)/TS, y=double(i)/TS;
texture[i][j][0] = 1;
texture[i][j][1] = 0;
texture[i][j][2] = 0;
if((x<0.333 || x>=0.666) and ( y<1/3.0 or y>2/3.0)){
texture[i][j][1] = 1;
texture[i][j][2] = 1;
}
texture[i][j][3] =1;
}
}
return makeTex(texture, TS, GL_FLOAT);
}
unsigned genSmokeTex()
{
const int TS=64;
float tex[TS][TS][4];
genNoise();
for(int i=0; i<TS; ++i) {
for(int j=0; j<TS; ++j) {
double x=double(j)/TS, y=double(i)/TS;
tex[i][j][0]=tex[i][j][1]=tex[i][j][2] = .25*turbulence(32*(1-x),32*(1-y),32);
double d = (x-.5)*(x-.5) + (y-.5)*(y-.5);
double f = min(1., .05/d);
const double B=.2;
f = min(f, max(0.0, .5/(.5-B) - sqrt(d)/B));
const double F=128;
tex[i][j][3] = .4*f*(turbulence(F*x,F*y,16)-.1);
}
}
return makeTex(tex, TS, GL_FLOAT);
}
}
Texture ammo;
Texture salama;
unsigned explosionTex;
unsigned groundTex;
unsigned buildingTex;
unsigned healthTex;
unsigned smokeTex;
void initTextures()
{
ammo = getAmmo();
salama = getSalama();
explosionTex = genExplosionTex();
groundTex = genGroundTex();
buildingTex = genBuildingTex();
healthTex = genHealthTex();
smokeTex = genSmokeTex();
}