-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZSprite.java
305 lines (272 loc) · 7.55 KB
/
ZSprite.java
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
import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;
public class ZSprite extends ZDraw implements ScenePrimitive, JSONSerializable{
//transparent ZDraw
int x,y,z;
boolean transparent; //transparent <=> dataVisible!=null (should be)
boolean dataVisible[];
int color[];
public ZSprite(){}
//reads a heightmap from the given array and normalize it
public ZSprite(int[] heightMap, int w, int h){
transparent=true;
setSize(w,h);
readHeightMapFrom(heightMap);
normalizeZ();
}
public ZSprite(int width, int height)
{
transparent=false;
setSize(width, height);
}
//O(1) shared copy (should never be modified)
public ZSprite fastClone(){
ZSprite res = new ZSprite();
res.data = data;
res.w=w;
res.h=h;
res.start=start;
res.stride=stride;
res.transparent = transparent;
res.dataVisible = dataVisible;
res.color = color;
res.x=x;
res.y=y;
res.z=z;
return res;
}
@Override
public void setSize(int nw, int nh){
super.setSize(nw,nh);
if (transparent)
dataVisible=new boolean[start+stride*nh];
}
//subtracts the z value of the first invisible point (!!!!!!!) from every z-vaue
public void normalizeZ(){
int baseZ=0;
int b=getLineIndex(0);
for (int x=0;x<w;x++)
if (transparent && !dataVisible[b+x]) {
baseZ=data[b+x];
break;
}
normalizeZ(baseZ);
}
//subtracts baseZ from every z value
public void normalizeZ(int baseZ){
if (baseZ==0) return;
for (int y=0; y<h; y++)
{
int b = getLineIndex(y);
for (int x=0; x<w; x++)
data[b+x]-=baseZ;
}
}
public void readHeightMapFrom(int[] from){
//read height data
super.readHeightMapFrom(from);
if (transparent) {
//set inverse alpha channel
for (int y=0; y<h; y++)
{
int b = getLineIndex(y);
for (int x=0; x<w; x++)
dataVisible[b+x]=((from[b+x]&0xff000000)>>>24) > 0x80;
}
}
}
public void drawTo(ZDraw zbuffer, int xo, int yo){
// System.out.println("print to: " +x+"/"+y+" size: "+w+":"+h);
int fx=0;
int fy=0;
int tx=w;
int ty=h;
int rx=x+xo-fx;//relativ offset (local->global system)
int ry=y+yo-fy;
//intersect own bounding box with buffer bounding box
if (fx+rx < 0) fx = - rx;
if (tx+rx > zbuffer.w) tx = zbuffer.w - rx;
if (fy+ry < 0) fy = -ry;
if (ty+ry > zbuffer.h) ty = zbuffer.h - ry;
if (fx >= tx) return;
//try{
for (int cy=fy; cy<ty; cy++)
{
int b = getLineIndex(cy);
int bo = zbuffer.getLineIndex(cy+ry)+rx;
if (!transparent) {
for (int cx=fx; cx<tx; cx++)
zbuffer.customPut(bo+cx,data[b+cx]+z);
} else {
for (int cx=fx; cx<tx; cx++)
if (dataVisible[b+cx])
zbuffer.customPut(bo+cx,data[b+cx]+z);
}
}
/* } catch (ArrayIndexOutOfBoundsException e){
System.out.println(x+"/"+y+"/"+z+": "+h+"/"+w+"/"+tx+"/"+ty+"=>"+getLineIndex(ty-1)+" "+ data.length+" "+dataVisible.length+" roi: "+start+" - "+stride);
throw e;
}*/
}
public static void colorPut(ZDraw buffer, ZDraw colorMap, int pos, int value, int color){
if (buffer.data[pos] < value) {
buffer.data[pos] = value;
colorMap.data[pos] = color;
}
}
public void drawTo(ZDraw zbuffer, ZDraw colorMap, int xo, int yo){
if (color == null) {
drawTo(zbuffer, xo, yo);
return;
}
/* } catch (ArrayIndexOutOfBoundsException e){
System.out.println(x+"/"+y+"/"+z+": "+h+"/"+w+"/"+tx+"/"+ty+"=>"+getLineIndex(ty-1)+" "+ data.length+" "+dataVisible.length+" roi: "+start+" - "+stride);
throw e;
}*/
int fx=0;
int fy=0;
int tx=w;
int ty=h;
int rx=x+xo-fx;//relativ offset (local->global system)
int ry=y+yo-fy;
//intersect own bounding box with buffer bounding box
if (fx+rx < 0) fx = - rx;
if (tx+rx > zbuffer.w) tx = zbuffer.w - rx;
if (fy+ry < 0) fy = -ry;
if (ty+ry > zbuffer.h) ty = zbuffer.h - ry;
if (fx >= tx) return;
//try{
for (int cy=fy; cy<ty; cy++)
{
int b = getLineIndex(cy);
int bo = zbuffer.getLineIndex(cy+ry)+rx;
if (!transparent) {
for (int cx=fx; cx<tx; cx++)
colorPut(zbuffer, colorMap, bo+cx,data[b+cx]+z,color[b+cx]);
} else {
for (int cx=fx; cx<tx; cx++)
if (dataVisible[b+cx])
colorPut(zbuffer, colorMap, bo+cx,data[b+cx]+z,color[b+cx]);
}
}
}
public Vector3i centerI(){
return new Vector3i(x+w/2,y+h/2,z);
}
public void move(int x, int y, int z){
this.x+=x;
this.y+=y;
this.z+=z;
}
public void moveTo(Vector3i to){
this.x=to.x-w/2;
this.y=to.y-h/2;
this.z=to.z;
}
public void moveTo(int x, int y, int z){
this.x=x-w/2;
this.y=y-h/2;
this.z=z;
}
public Vector3i cornerLTF(){
return new Vector3i(x, y, z);
}
public Vector3i cornerRBN(){
return new Vector3i(x+w, y+h, ZDraw.MAXZ);
}
public int zAt(int wx, int wy){
if (!inBounds(wx-x, wy-y)) return -1;
if (transparent && !dataVisible[getIndex(wx-x, wy-y)]) return -1;
return data[getIndex(wx-x, wy-y)];
}
public boolean intersectBoundaries2D(ZSprite sprite, int[] boundary) {
//calculate intersection rect in local coords
int ox = sprite.x - x;
int il = Math.max(0, ox);
int ir = Math.min(w, ox + sprite.w);
if (il >= ir)
return false;
int oy = sprite.y - y;
int it = Math.max(0, oy);
int ib = Math.min(h, sprite.h + oy);
if (it >= ib)
return false;
boundary[0] = il;
boundary[1] = it;
boundary[2] = ir;
boundary[3] = ib;
return true;
}
public boolean intersect(ZSprite sprite, int dx, int dy, boolean removeIntersectionInThis){
//calculate intersection rect in local coords
int ox = sprite.x - x + dx;
int il = Math.max(0, ox);
int ir = Math.min(w, ox + sprite.w);
if (il >= ir)
return false;
int oy = sprite.y - y + dy;
int it = Math.max(0, oy);
int ib = Math.min(h, sprite.h + oy);
if (it >= ib)
return false;
//sprite equal to bounding rect
if (!transparent && !sprite.transparent)
return true;
//pixel check
removeIntersectionInThis = removeIntersectionInThis && transparent;
boolean result=false;
int oz=sprite.z - z;
int zeps = 2;
for (int y=it;y<ib;y++){
int b = getLineIndex(y);
int b2 = sprite.getLineIndex(y - oy);
for (int x=il;x<ir;x++)
if (dataVisible[b+x] && sprite.dataVisible[b2+x-ox]&&
Math.abs(data[b+x] - sprite.data[b2+x-ox] - oz) <= zeps){
result=true;
if (removeIntersectionInThis) dataVisible[b+x]=false;
else return result;
}
}
return result;
}
public int minZ(){
assert transparent;
int res = Integer.MAX_VALUE;
for (int y=0; y<h; y++)
{
int b = getLineIndex(y);
for (int x=0; x<w; x++)
if (dataVisible[b+x])
res = Math.min(res, data[b+x]);
}
return res;
}
public int maxZ(){
assert transparent;
int res = Integer.MIN_VALUE;
for (int y=0; y<h; y++)
{
int b = getLineIndex(y);
for (int x=0; x<w; x++)
if (dataVisible[b+x])
res = Math.max(res, data[b+x]);
}
return res;
}
public Object jsonSerialize(){
TreeMap<String,Object> tm = new TreeMap<String,Object>();
tm.put("type", "ZSprite");
tm.put("position", new int[]{x, y, z});
return tm;
}
public void jsonDeserialize(Object obj){
Map<String, Object> map = ((Map<String, Object>)obj);
assert ("ZSprite".equals( map.get("type")));
ArrayList<Number> p = (ArrayList<Number>) map.get("position");
x=p.get(0).intValue();
y=p.get(1).intValue();
z=p.get(2).intValue();
}
}