forked from phu004/JavaRTS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtexture.java
More file actions
389 lines (286 loc) · 9.89 KB
/
Copy pathtexture.java
File metadata and controls
389 lines (286 loc) · 9.89 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package core;
//the texture dimension should only be power of 2
import java.awt.image.PixelGrabber;
import java.awt.*;
public class texture {
//holds the pixel data in 16bits color format
public static int[] textureBuffer;
//hold mipmaps of the original texture
public short[] pixelData;
public byte[] pixelDataByte;
//stores a sequence of explosion texture
public int[][] explosions;
//stores a sequence of smoke texture
public int[][] smoke;
//store height map
public int[] heightmap;
//stores animated light maps created by an explosion
public short[][] explosionAura;
//store displacement map
public short[] displacementMap;
//store height map that associated with water texture
public byte[] waterHeightMap;
public byte[][] waterHeightMaps;
//store information that determine water surface movement direction (can be either up or down)
public boolean[] waterSurfaceDirections;
//dimension of the texture
public int height, width, heightMask, widthMask, widthBits, heightBits;
public String type;
public int ID;
//produce texture based on input image
public texture(String type, Image img, int widthBits , int heightBits){
this.widthBits = widthBits;
this.heightBits = heightBits;
height = (int)Math.pow(2, heightBits);
width = (int)Math.pow(2, widthBits);
heightMask = height -1;
widthMask = width - 1;
this.type = type;
if(textureBuffer == null)
textureBuffer = new int[1024*1024]; //support a max texture size of 1024 * 1024
//load texture image and store it as an array of int
PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, textureBuffer, 0, width);
try {
pg.grabPixels();
}catch(Exception e){
e.printStackTrace();
}
int r, g, b;
//create displacement map
if(type.equals("displacement")){
displacementMap = new short[height*width];
int dh = height;
int dw = width;
int i = 0;
for ( int y = 0; y < dh; y++ ) {
for ( int x = 0; x < dw; x++ ) {
int rgb = textureBuffer[i];
r = (rgb >> 16) & 0xff;
g = (rgb >> 8) & 0xff;
b = rgb & 0xff;
textureBuffer[i] = (r+g+b) / 8; // An arbitrary scaling factor which gives a good range for "amount"
i++;
}
}
i = 0;
for ( int y = 0; y < dh; y++ ) {
int j1 = ((y+dh-1) % dh) * dw;
int j2 = y*dw;
int j3 = ((y+1) % dh) * dw;
for ( int x = 0; x < dw; x++ ) {
int k1 = (x+dw-1) % dw;
int k2 = x;
int k3 = (x+1) % dw;
int xMap = (textureBuffer[k1+j1] + textureBuffer[k1+j2] + textureBuffer[k1+j3] - textureBuffer[k3+j1] - textureBuffer[k3+j2] - textureBuffer[k3+j3]);
int yMap = (textureBuffer[k1+j3] + textureBuffer[k2+j3] + textureBuffer[k3+j3] - textureBuffer[k1+j1] - textureBuffer[k2+j1] - textureBuffer[k3+j1]);
displacementMap[i] = (short)( ((textureBuffer[i] * 2 / 3) << 10) | ((xMap + 16) << 5) | (yMap + 16));
i++;
}
}
}
//create basic 15bits version of the original texture
if(type.equals("basic")){
pixelData = new short[width*height];
for(int i = 0; i < width*height; i ++){
r = (textureBuffer[i] & 0x00ff0000)>>16;
g = (textureBuffer[i] & 0x0000ff00)>>8;
b = (textureBuffer[i] & 0x000000ff);
r = r/8;
g = g/8;
b = b/8;
pixelData[i] = (short)(r <<10 | g << 5 | b);
}
}
//create a series of explosion texture
if(type.equals("explosion")){
explosions = new int[16][64*64];
Color temp = new Color(0,0,0);
for(int i = 0; i < 16; i++){
int x = (i%4)*64;
int y = (i/4)*64;
for(int j = 0; j < 64*64; j++){
int color = textureBuffer[x+y*256 + j%64 + (j/64)*256];
temp = new Color(color);
if(temp.getRed() < 40)
color = 0;
explosions[i][j] = color;
}
}
}
//create series of smoke texture
if(type.equals("smoke")){
smoke = new int[40][64*64];
Color temp = new Color(0,0,0);
for(int i = 0; i < 40; i++){
int x = (i%8)*64;
int y = (i/8)*64;
for(int j = 0; j < 64*64; j++){
int color = textureBuffer[x+y*512 + j%64 + (j/64)*512];
temp = new Color(color);
r = ((color & 0xff0000) >> 16);
g = ((color & 0xff00) >> 8);
b = ((color & 0xff));
if(r < 0)
r = 0;
if(g < 0)
g = 0;
if(b < 0)
b = 0;
color = r;
if(r < 5)
color = 0;
smoke[i][j] = color ;
}
}
}
//create height map
if(type.equals("heightmap")){
heightmap = new int[(width+1)*(height+1)];
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
r = (textureBuffer[j + i* width] & 0xff);
if(i ==0 || i == (height-1) || j == 0 || j == (width -1))
r = 0;
heightmap[j + i * (width+1)] = r;
}
}
}
//create a series of light maps to stimulate the illumination created during an explosion
if(type.equals("explosion aura")){
explosionAura = new short[16][width*height];
for(int j = 0; j < 16; j++){
for(int i = 0; i < width*height; i ++){
int I = (((textureBuffer[i] & 0x00ff0000)>>16) - 20) * 5/9;
if(I < 80)
I = I*I*I*I/80/80/80;
explosionAura[j][i] = (short)(I/2.5 - 4*j);
}
}
}
}
//produce texture based on input image
public texture(String type, Image img, Image img2, int widthBits , int heightBits){
this.widthBits = widthBits;
this.heightBits = heightBits;
height = (int)Math.pow(2, heightBits);
width = (int)Math.pow(2, widthBits);
heightMask = height -1;
widthMask = width - 1;
this.type = type;
if(textureBuffer == null)
textureBuffer = new int[1024*1024]; //support a max texture size of 1024 * 1024
//load texture image and store it as an array of int
PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, textureBuffer, 0, width);
try {
pg.grabPixels();
}catch(Exception e){
e.printStackTrace();
}
int r, g, b;
//create displacement map
if(type.equals("water")){
displacementMap = new short[height*width];
waterHeightMap = new byte[height*width];
waterHeightMaps = new byte[48][height*width];
waterSurfaceDirections = new boolean[height*width];
int dh = height;
int dw = width;
int i = 0;
for ( int y = 0; y < dh; y++ ) {
for ( int x = 0; x < dw; x++ ) {
int rgb = textureBuffer[i];
r = (rgb >> 16) & 0xff;
g = (rgb >> 8) & 0xff;
b = rgb & 0xff;
textureBuffer[i] = (r+g+b) / 8; // An arbitrary scaling factor which gives a good range for "amount"
i++;
}
}
i = 0;
for ( int y = 0; y < dh; y++ ) {
int j1 = ((y+dh-1) % dh) * dw;
int j2 = y*dw;
int j3 = ((y+1) % dh) * dw;
for ( int x = 0; x < dw; x++ ) {
int k1 = (x+dw-1) % dw;
int k2 = x;
int k3 = (x+1) % dw;
int xMap = (textureBuffer[k1+j1] + textureBuffer[k1+j2] + textureBuffer[k1+j3] - textureBuffer[k3+j1] - textureBuffer[k3+j2] - textureBuffer[k3+j3]);
int yMap = (textureBuffer[k1+j3] + textureBuffer[k2+j3] + textureBuffer[k3+j3] - textureBuffer[k1+j1] - textureBuffer[k2+j1] - textureBuffer[k3+j1]);
displacementMap[i] = (short)( ((xMap + 16) << 5) | (yMap + 16));
i++;
}
}
//load height map for the water surface
pg = new PixelGrabber(img2, 0, 0, width, height, textureBuffer, 0, width);
try {
pg.grabPixels();
}catch(Exception e){
e.printStackTrace();
}
i = 0;
for ( int y = 0; y < dh; y++ ) {
for ( int x = 0; x < dw; x++ ) {
int h = (int)(((textureBuffer[i]&0xff) - 160) * 0.9);
if(h < 0)
h = 0;
waterHeightMap[i] = (byte)(h);
if(waterHeightMap[i] > 63){
System.out.println(waterHeightMap[i]);
waterHeightMap[i] = 63;
}
i++;
}
}
for(i = 0; i < 256*256; i++){
waterHeightMaps[0][i] = waterHeightMap[i];
waterHeightMaps[15][i] = waterHeightMap[(3456 + 256*256-i-1)%(256*256)];
waterHeightMaps[31][i] = waterHeightMap[i - i%256+ (255- i%256)];
}
for(int j = 1; j < 15; j++){
for(i = 0; i < 256*256; i++ ){
waterHeightMaps[j][i] = (byte)(waterHeightMaps[0][i] + (float)(waterHeightMaps[15][i] - waterHeightMaps[0][i])/15f*j);
}
}
for(int j = 16; j < 31; j++){
for(i = 0; i < 256*256; i++ ){
waterHeightMaps[j][i] = (byte)(waterHeightMaps[15][i] + (float)(waterHeightMaps[31][i] - waterHeightMaps[15][i])/15f*(j-15));
}
}
for(int j = 32; j < 48; j++){
for(i = 0; i < 256*256; i++ ){
waterHeightMaps[j][i] = (byte)(waterHeightMaps[31][i] + (float)(waterHeightMaps[0][i] - waterHeightMaps[31][i])/16f*(j-31));
}
}
}
}
//produce texture based on raw data
public texture(String type, short[] pixelData, int widthBits, int heightBits){
this.widthBits = widthBits;
this.heightBits = heightBits;
height = (int)Math.pow(2, heightBits);
width = (int)Math.pow(2, widthBits);
heightMask = height -1;
widthMask = width - 1;
this.pixelData = pixelData;
this.type = type;
}
//produce texture based on a color
public texture(String type, int color, int widthBits, int heightBits){
this.widthBits = widthBits;
this.heightBits = heightBits;
height = (int)Math.pow(2, heightBits);
width = (int)Math.pow(2, widthBits);
heightMask = height -1;
widthMask = width - 1;
int r = ((color & 0xff0000) >> 16)/8;
int g = ((color & 0xff00) >> 8)/8;
int b = (color & 0xff)/8;
short c = (short)(r <<10 | g << 5 | b);
this.pixelData = new short[height * width];
for(int i = 0; i < pixelData.length; i ++){
pixelData[i] = c;
}
this.type = type;
}
}