Revision 6c00149d
Added by Leszek Koltunski about 6 years ago
src/main/java/org/distorted/library/main/DistortedBuffer.java | ||
---|---|---|
33 | 33 |
*/ |
34 | 34 |
public class DistortedBuffer extends DistortedObject |
35 | 35 |
{ |
36 |
int[] mIndex; |
|
36 |
public int[] mIndex;
|
|
37 | 37 |
|
38 | 38 |
private int mTarget, mSize, mUsage; |
39 | 39 |
private Buffer mBuffer; |
40 | 40 |
|
41 | 41 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
42 | 42 |
|
43 |
DistortedBuffer(int target, int usage) |
|
43 |
public DistortedBuffer(int target, int usage)
|
|
44 | 44 |
{ |
45 | 45 |
super(DistortedObject.NOT_CREATED_YET,DistortedObject.TYPE_USER); |
46 | 46 |
|
... | ... | |
53 | 53 |
|
54 | 54 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
55 | 55 |
|
56 |
void setData(int size, Buffer buffer) |
|
56 |
public void setData(int size, Buffer buffer)
|
|
57 | 57 |
{ |
58 | 58 |
mSize = size; |
59 | 59 |
mBuffer = buffer; |
src/main/java/org/distorted/library/main/DistortedEffects.java | ||
---|---|---|
29 | 29 |
import org.distorted.library.effect.EffectType; |
30 | 30 |
import org.distorted.library.effect.FragmentEffect; |
31 | 31 |
import org.distorted.library.effect.VertexEffect; |
32 |
import org.distorted.library.mesh.MeshObject; |
|
32 | 33 |
import org.distorted.library.message.EffectListener; |
33 | 34 |
import org.distorted.library.program.DistortedProgram; |
34 | 35 |
|
... | ... | |
424 | 425 |
|
425 | 426 |
private void displayNormals(MeshObject mesh) |
426 | 427 |
{ |
428 |
int num = mesh.getNumVertices(); |
|
429 |
|
|
427 | 430 |
GLES31.glBindBufferBase(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, 0, mesh.getTFO() ); |
428 | 431 |
GLES31.glBeginTransformFeedback( GLES31.GL_POINTS); |
429 | 432 |
DistortedRenderState.switchOffDrawing(); |
430 |
GLES31.glDrawArrays( GLES31.GL_POINTS, 0, mesh.numVertices);
|
|
433 |
GLES31.glDrawArrays( GLES31.GL_POINTS, 0, num );
|
|
431 | 434 |
DistortedRenderState.restoreDrawing(); |
432 | 435 |
GLES31.glEndTransformFeedback(); |
433 | 436 |
GLES31.glBindBufferBase(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0); |
... | ... | |
438 | 441 |
GLES31.glVertexAttribPointer(mNormalProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, 0, 0); |
439 | 442 |
GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0); |
440 | 443 |
GLES31.glLineWidth(8.0f); |
441 |
GLES31.glDrawArrays(GLES31.GL_LINES, 0, 2*mesh.numVertices);
|
|
444 |
GLES31.glDrawArrays(GLES31.GL_LINES, 0, 2*num);
|
|
442 | 445 |
} |
443 | 446 |
|
444 | 447 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
453 | 456 |
|
454 | 457 |
void drawPrivOIT(float halfW, float halfH, MeshObject mesh, DistortedOutputSurface surface, long currTime) |
455 | 458 |
{ |
456 |
float halfZ = halfW*mesh.zFactor;
|
|
459 |
float halfZ = halfW*mesh.getZFactor();
|
|
457 | 460 |
|
458 | 461 |
mM.compute(currTime); |
459 | 462 |
mV.compute(currTime,halfW,halfH,halfZ); |
... | ... | |
477 | 480 |
mV.send(1); |
478 | 481 |
mF.send(1); |
479 | 482 |
|
480 |
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.numVertices);
|
|
483 |
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
|
|
481 | 484 |
|
482 |
if( mesh.mShowNormals )
|
|
485 |
if( mesh.getShowNormals() )
|
|
483 | 486 |
{ |
484 | 487 |
mMainProgram.useProgram(); |
485 | 488 |
mM.send(surface,halfW,halfH,halfZ,0,0); |
... | ... | |
493 | 496 |
|
494 | 497 |
void drawPriv(float halfW, float halfH, MeshObject mesh, DistortedOutputSurface surface, long currTime) |
495 | 498 |
{ |
496 |
float halfZ = halfW*mesh.zFactor;
|
|
499 |
float halfZ = halfW*mesh.getZFactor();
|
|
497 | 500 |
|
498 | 501 |
mM.compute(currTime); |
499 | 502 |
mV.compute(currTime,halfW,halfH,halfZ); |
... | ... | |
515 | 518 |
mV.send(0); |
516 | 519 |
mF.send(0); |
517 | 520 |
|
518 |
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.numVertices);
|
|
521 |
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
|
|
519 | 522 |
|
520 |
if( mesh.mShowNormals ) displayNormals(mesh);
|
|
523 |
if( mesh.getShowNormals() ) displayNormals(mesh);
|
|
521 | 524 |
} |
522 | 525 |
|
523 | 526 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
src/main/java/org/distorted/library/main/DistortedNode.java | ||
---|---|---|
21 | 21 |
|
22 | 22 |
import android.opengl.GLES31; |
23 | 23 |
|
24 |
import org.distorted.library.mesh.MeshObject; |
|
25 |
|
|
24 | 26 |
import java.util.ArrayList; |
25 | 27 |
import java.util.Collections; |
26 | 28 |
import java.util.HashMap; |
src/main/java/org/distorted/library/main/DistortedOutputSurface.java | ||
---|---|---|
23 | 23 |
import android.opengl.Matrix; |
24 | 24 |
|
25 | 25 |
import org.distorted.library.effect.EffectQuality; |
26 |
import org.distorted.library.mesh.MeshObject; |
|
26 | 27 |
|
27 | 28 |
import java.util.ArrayList; |
28 | 29 |
|
src/main/java/org/distorted/library/main/DistortedScreen.java | ||
---|---|---|
27 | 27 |
import android.opengl.GLES31; |
28 | 28 |
|
29 | 29 |
import org.distorted.library.effect.MatrixEffectMove; |
30 |
import org.distorted.library.mesh.MeshFlat; |
|
31 |
import org.distorted.library.mesh.MeshObject; |
|
30 | 32 |
import org.distorted.library.type.Static3D; |
31 | 33 |
|
32 | 34 |
/** |
src/main/java/org/distorted/library/main/DistortedSurface.java | ||
---|---|---|
21 | 21 |
|
22 | 22 |
import android.opengl.GLES31; |
23 | 23 |
|
24 |
import org.distorted.library.mesh.MeshObject; |
|
25 |
|
|
24 | 26 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
25 | 27 |
/** |
26 | 28 |
* This is not really part of the public API. |
... | ... | |
100 | 102 |
*/ |
101 | 103 |
public int getDepth(MeshObject mesh) |
102 | 104 |
{ |
103 |
return mesh==null ? 0 : (int)(mWidth*mesh.zFactor);
|
|
105 |
return mesh==null ? 0 : (int)(mWidth*mesh.getZFactor() );
|
|
104 | 106 |
} |
105 | 107 |
|
106 | 108 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
src/main/java/org/distorted/library/main/EffectQueuePostprocess.java | ||
---|---|---|
27 | 27 |
import org.distorted.library.effect.EffectType; |
28 | 28 |
import org.distorted.library.effect.PostprocessEffect; |
29 | 29 |
import org.distorted.library.effect.VertexEffect; |
30 |
import org.distorted.library.mesh.MeshObject; |
|
30 | 31 |
import org.distorted.library.message.EffectMessage; |
31 | 32 |
import org.distorted.library.program.DistortedProgram; |
32 | 33 |
|
... | ... | |
161 | 162 |
|
162 | 163 |
float halfW = input.getWidth() / 2.0f; |
163 | 164 |
float halfH = input.getHeight()/ 2.0f; |
164 |
float halfZ = halfW*mesh.zFactor;
|
|
165 |
float halfZ = halfW*mesh.getZFactor();
|
|
165 | 166 |
|
166 | 167 |
DistortedRenderState.setUpStencilMark(mA!=0.0f); |
167 | 168 |
|
... | ... | |
183 | 184 |
GLES31.glUniform1i(mPreTextureH, 0); |
184 | 185 |
} |
185 | 186 |
|
186 |
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.numVertices);
|
|
187 |
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
|
|
187 | 188 |
|
188 | 189 |
DistortedRenderState.unsetUpStencilMark(); |
189 | 190 |
|
src/main/java/org/distorted/library/main/MeshCubes.java | ||
---|---|---|
1 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
2 |
// Copyright 2016 Leszek Koltunski // |
|
3 |
// // |
|
4 |
// This file is part of Distorted. // |
|
5 |
// // |
|
6 |
// Distorted is free software: you can redistribute it and/or modify // |
|
7 |
// it under the terms of the GNU General Public License as published by // |
|
8 |
// the Free Software Foundation, either version 2 of the License, or // |
|
9 |
// (at your option) any later version. // |
|
10 |
// // |
|
11 |
// Distorted is distributed in the hope that it will be useful, // |
|
12 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of // |
|
13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // |
|
14 |
// GNU General Public License for more details. // |
|
15 |
// // |
|
16 |
// You should have received a copy of the GNU General Public License // |
|
17 |
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. // |
|
18 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
19 |
|
|
20 |
package org.distorted.library.main; |
|
21 |
|
|
22 |
import java.nio.ByteBuffer; |
|
23 |
import java.nio.ByteOrder; |
|
24 |
import java.util.ArrayList; |
|
25 |
|
|
26 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
27 |
/** |
|
28 |
* Create a 3D grid composed of Cubes. |
|
29 |
* <p> |
|
30 |
* Any subset of a MxNx1 cuboid is possible. |
|
31 |
*/ |
|
32 |
public class MeshCubes extends MeshObject |
|
33 |
{ |
|
34 |
private static final float R = 0.0f;//0.2f; |
|
35 |
|
|
36 |
private static final int NORTH = 0; |
|
37 |
private static final int WEST = 1; |
|
38 |
private static final int EAST = 2; |
|
39 |
private static final int SOUTH = 3; |
|
40 |
|
|
41 |
private static final float[] mNormalX = new float[4]; |
|
42 |
private static final float[] mNormalY = new float[4]; |
|
43 |
private static final float[] mNormalZ = new float[4]; |
|
44 |
|
|
45 |
private class Edge |
|
46 |
{ |
|
47 |
final int side; |
|
48 |
final int row; |
|
49 |
final int col; |
|
50 |
|
|
51 |
Edge(int s, int r, int c) |
|
52 |
{ |
|
53 |
side= s; |
|
54 |
row = r; |
|
55 |
col = c; |
|
56 |
} |
|
57 |
} |
|
58 |
|
|
59 |
private int mCols, mRows, mSlices; |
|
60 |
private int[][] mCubes; |
|
61 |
private ArrayList<Edge> mEdges = new ArrayList<>(); |
|
62 |
|
|
63 |
private int remainingVert; |
|
64 |
private int mSideBends; |
|
65 |
private int mEdgeNum; |
|
66 |
private int mSideWalls; |
|
67 |
|
|
68 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
69 |
// a Block is split into two triangles along the NE-SW line iff it is in the top-right |
|
70 |
// or bottom-left quadrant of the grid. |
|
71 |
|
|
72 |
private boolean isNE(int row,int col) |
|
73 |
{ |
|
74 |
return ( (2*row<mRows)^(2*col<mCols) ); |
|
75 |
} |
|
76 |
|
|
77 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
78 |
// return the number of vertices our grid will contain |
|
79 |
|
|
80 |
private int computeDataLength() |
|
81 |
{ |
|
82 |
int frontWalls=0, frontSegments=0, triangleShifts=0, windingShifts=0; |
|
83 |
int shiftCol = (mCols-1)/2; |
|
84 |
|
|
85 |
boolean lastBlockIsNE=false; |
|
86 |
boolean thisBlockIsNE; // the block we are currently looking at is split into |
|
87 |
// two triangles along the NE-SW line (rather than NW-SE) |
|
88 |
for(int row=0; row<mRows; row++) |
|
89 |
{ |
|
90 |
if( mCols>=2 && (mCubes[row][shiftCol]%2 == 1) && (mCubes[row][shiftCol+1]%2 == 1) ) triangleShifts++; |
|
91 |
|
|
92 |
for(int col=0; col<mCols; col++) |
|
93 |
{ |
|
94 |
if( mCubes[row][col]%2 == 1 ) // land |
|
95 |
{ |
|
96 |
thisBlockIsNE = isNE(row,col); |
|
97 |
if( thisBlockIsNE^lastBlockIsNE ) windingShifts++; |
|
98 |
lastBlockIsNE = thisBlockIsNE; |
|
99 |
frontWalls++; |
|
100 |
if( col==mCols-1 || mCubes[row][col+1]%2 == 0 ) frontSegments++; |
|
101 |
} |
|
102 |
} |
|
103 |
} |
|
104 |
|
|
105 |
int frontVert = 2*( frontWalls + 2*frontSegments - 1) +2*triangleShifts + windingShifts; |
|
106 |
int sideVertOneSlice= 2*( mSideWalls + mSideBends + mEdgeNum -1); |
|
107 |
int sideVert = 2*(mSlices-1) + mSlices*sideVertOneSlice; |
|
108 |
int firstWinding = (mSlices>0 && (frontVert+1)%2==1 ) ? 1:0; |
|
109 |
int dataL = mSlices==0 ? frontVert : (frontVert+1) +firstWinding+ (1+sideVert+1) + (1+frontVert); |
|
110 |
/* |
|
111 |
android.util.Log.e("CUBES","triangleShifts="+triangleShifts+" windingShifts="+windingShifts+" winding1="+firstWinding+" frontVert="+frontVert+" sideVert="+sideVert); |
|
112 |
android.util.Log.e("CUBES", "frontW="+frontWalls+" fSegments="+frontSegments+" sWalls="+mSideWalls+" sSegments="+mEdgeNum+" sideBends="+mSideBends+" dataLen="+dataL ); |
|
113 |
*/ |
|
114 |
return dataL<0 ? 0:dataL; |
|
115 |
} |
|
116 |
|
|
117 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
118 |
/* |
|
119 |
private static String debug(short[] val) |
|
120 |
{ |
|
121 |
String ret="";j |
|
122 |
|
|
123 |
for(int i=0; i<val.length; i++) ret+=(" "+val[i]); |
|
124 |
|
|
125 |
return ret; |
|
126 |
} |
|
127 |
*/ |
|
128 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
129 |
|
|
130 |
private static String debug(float[] val, int stop) |
|
131 |
{ |
|
132 |
String ret=""; |
|
133 |
float v; |
|
134 |
boolean neg; |
|
135 |
int mod; |
|
136 |
|
|
137 |
for(int i=0; i<val.length; i++) |
|
138 |
{ |
|
139 |
if( i%stop==0 ) ret+="\n"; |
|
140 |
|
|
141 |
mod = i%stop; |
|
142 |
|
|
143 |
if( mod==0 || mod==3 || mod==6 ) ret+=" ("; |
|
144 |
|
|
145 |
v = val[i]; |
|
146 |
if( v==-0.0f ) v=0.0f; |
|
147 |
|
|
148 |
|
|
149 |
neg = v<0; |
|
150 |
v = (v<0 ? -v:v); |
|
151 |
|
|
152 |
ret+=((neg? " -":" +")+v); |
|
153 |
|
|
154 |
if( mod==2 || mod==5 || mod==7 ) ret+=")"; |
|
155 |
} |
|
156 |
|
|
157 |
return ret; |
|
158 |
} |
|
159 |
|
|
160 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
161 |
/* |
|
162 |
private static String debug(Edge e) |
|
163 |
{ |
|
164 |
String d = ""; |
|
165 |
|
|
166 |
switch(e.side) |
|
167 |
{ |
|
168 |
case NORTH: d+="NORTH "; break; |
|
169 |
case SOUTH: d+="SOUTH "; break; |
|
170 |
case WEST : d+="WEST "; break; |
|
171 |
case EAST : d+="EAST "; break; |
|
172 |
} |
|
173 |
|
|
174 |
d+=("("+e.row+","+e.col+")"); |
|
175 |
|
|
176 |
return d; |
|
177 |
} |
|
178 |
*/ |
|
179 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
180 |
|
|
181 |
private void prepareDataStructures(int cols, String desc, int slices) |
|
182 |
{ |
|
183 |
mRows =0; |
|
184 |
mCols =0; |
|
185 |
mSlices =slices; |
|
186 |
numVertices=0; |
|
187 |
|
|
188 |
if( cols>0 && desc.contains("1") ) |
|
189 |
{ |
|
190 |
mCols = cols; |
|
191 |
mRows = desc.length()/cols; |
|
192 |
|
|
193 |
mCubes = new int[mRows][mCols]; |
|
194 |
|
|
195 |
for(int j=0; j<mCols; j++) |
|
196 |
for(int i=0; i<mRows; i++) |
|
197 |
mCubes[i][j] = (desc.charAt(i*mCols+j) == '1' ? 1:0); |
|
198 |
|
|
199 |
markRegions(); |
|
200 |
numVertices = computeDataLength(); |
|
201 |
|
|
202 |
remainingVert = numVertices; |
|
203 |
} |
|
204 |
} |
|
205 |
|
|
206 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
207 |
// full grid |
|
208 |
|
|
209 |
private void prepareDataStructures(int cols, int rows, int slices) |
|
210 |
{ |
|
211 |
mRows =rows; |
|
212 |
mCols =cols; |
|
213 |
mSlices =slices; |
|
214 |
numVertices = 0; |
|
215 |
|
|
216 |
if( cols>0 && rows>0 ) |
|
217 |
{ |
|
218 |
mCubes = new int[mRows][mCols]; |
|
219 |
|
|
220 |
for(int j=0; j<mCols; j++) |
|
221 |
for(int i=0; i<mRows; i++) |
|
222 |
mCubes[i][j] = 1; |
|
223 |
|
|
224 |
markRegions(); |
|
225 |
numVertices = computeDataLength(); |
|
226 |
|
|
227 |
remainingVert = numVertices; |
|
228 |
} |
|
229 |
} |
|
230 |
|
|
231 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
232 |
// Mark all the 'regions' of our grid - i.e. separate pieces of 'land' (connected blocks that will |
|
233 |
// be rendered) and 'water' (connected holes in between) with integers. Each connected block of land |
|
234 |
// gets a unique odd integer, each connected block of water a unique even integer. |
|
235 |
// |
|
236 |
// Water on the edges of the grid is also considered connected to itself! |
|
237 |
// |
|
238 |
// This function also creates a list of 'Edges'. Each Edge is a data structure from which later on we |
|
239 |
// will start building the side walls of each connected block of land (and sides of holes of water |
|
240 |
// inside). Each Edge needs to point from Land to Water (thus the '(SOUTH,i-1,j)' below) - otherwise |
|
241 |
// later on setting up normal vectors wouldn't work. |
|
242 |
|
|
243 |
private void markRegions() |
|
244 |
{ |
|
245 |
int i, j, numWater=1, numLand=0; |
|
246 |
|
|
247 |
for(i=0; i<mRows;i++) if( mCubes[ i][ 0]==0 ) markRegion((short)2, i, 0); |
|
248 |
for(i=0; i<mRows;i++) if( mCubes[ i][mCols-1]==0 ) markRegion((short)2, i, mCols-1); |
|
249 |
for(i=0; i<mCols;i++) if( mCubes[0 ][ i]==0 ) markRegion((short)2, 0, i); |
|
250 |
for(i=0; i<mCols;i++) if( mCubes[mRows-1][ i]==0 ) markRegion((short)2,mRows-1, i); |
|
251 |
|
|
252 |
for(i=0; i<mRows; i++) |
|
253 |
for(j=0; j<mCols; j++) |
|
254 |
{ |
|
255 |
if( mCubes[i][j] == 0 ) { numWater++; markRegion( (short)(2*numWater ),i,j); mEdges.add(new Edge(SOUTH,i-1,j)); } |
|
256 |
if( mCubes[i][j] == 1 ) { numLand ++; markRegion( (short)(2*numLand+1),i,j); mEdges.add(new Edge(NORTH,i ,j)); } |
|
257 |
} |
|
258 |
|
|
259 |
// now we potentially need to kick out some Edges . Otherwise the following does not work: |
|
260 |
// |
|
261 |
// 0 1 0 |
|
262 |
// 1 0 1 |
|
263 |
// 0 1 0 |
|
264 |
|
|
265 |
mEdgeNum= mEdges.size(); |
|
266 |
int initCol, initRow, initSide, lastSide; |
|
267 |
Edge e1,e2; |
|
268 |
|
|
269 |
for(i=0; i<mEdgeNum; i++) |
|
270 |
{ |
|
271 |
e1 = mEdges.get(i); |
|
272 |
initRow= e1.row; |
|
273 |
initCol= e1.col; |
|
274 |
initSide=e1.side; |
|
275 |
|
|
276 |
do |
|
277 |
{ |
|
278 |
//android.util.Log.d("CUBES", "checking edge "+debug(e1)); |
|
279 |
|
|
280 |
mSideWalls++; |
|
281 |
|
|
282 |
if( e1.side==NORTH || e1.side==SOUTH ) |
|
283 |
{ |
|
284 |
for(j=i+1;j<mEdgeNum;j++) |
|
285 |
{ |
|
286 |
e2 = mEdges.get(j); |
|
287 |
|
|
288 |
if( e2.side==e1.side && e2.row==e1.row && e2.col==e1.col ) |
|
289 |
{ |
|
290 |
mEdges.remove(j); |
|
291 |
mEdgeNum--; |
|
292 |
j--; |
|
293 |
|
|
294 |
//android.util.Log.e("CUBES", "removing edge "+debug(e2)); |
|
295 |
} |
|
296 |
} |
|
297 |
} |
|
298 |
|
|
299 |
lastSide = e1.side; |
|
300 |
e1 = getNextEdge(e1); |
|
301 |
if( e1.side!=lastSide ) mSideBends++; |
|
302 |
} |
|
303 |
while( e1.col!=initCol || e1.row!=initRow || e1.side!=initSide ); |
|
304 |
} |
|
305 |
} |
|
306 |
|
|
307 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
308 |
// when calling, make sure that newVal != val |
|
309 |
|
|
310 |
private void markRegion(short newVal, int row, int col) |
|
311 |
{ |
|
312 |
int val = mCubes[row][col]; |
|
313 |
mCubes[row][col] = newVal; |
|
314 |
|
|
315 |
if( row>0 && mCubes[row-1][col ]==val ) markRegion(newVal, row-1, col ); |
|
316 |
if( row<mRows-1 && mCubes[row+1][col ]==val ) markRegion(newVal, row+1, col ); |
|
317 |
if( col>0 && mCubes[row ][col-1]==val ) markRegion(newVal, row , col-1); |
|
318 |
if( col<mCols-1 && mCubes[row ][col+1]==val ) markRegion(newVal, row , col+1); |
|
319 |
} |
|
320 |
|
|
321 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
322 |
|
|
323 |
private void createNormals(boolean front, int row, int col) |
|
324 |
{ |
|
325 |
int td,lr; |
|
326 |
|
|
327 |
int nw = (col>0 && row>0 ) ? (mCubes[row-1][col-1]%2) : 0; |
|
328 |
int w = (col>0 ) ? (mCubes[row ][col-1]%2) : 0; |
|
329 |
int n = ( row>0 ) ? (mCubes[row-1][col ]%2) : 0; |
|
330 |
int c = (mCubes[row ][col ]%2); |
|
331 |
int sw = (col>0 && row<mRows-1) ? (mCubes[row+1][col-1]%2) : 0; |
|
332 |
int s = ( row<mRows-1) ? (mCubes[row+1][col ]%2) : 0; |
|
333 |
int ne = (col<mCols-1 && row>0 ) ? (mCubes[row-1][col+1]%2) : 0; |
|
334 |
int e = (col<mCols-1 ) ? (mCubes[row ][col+1]%2) : 0; |
|
335 |
int se = (col<mCols-1 && row<mRows-1) ? (mCubes[row+1][col+1]%2) : 0; |
|
336 |
|
|
337 |
if(front) |
|
338 |
{ |
|
339 |
mNormalZ[0] = 1.0f; |
|
340 |
mNormalZ[1] = 1.0f; |
|
341 |
mNormalZ[2] = 1.0f; |
|
342 |
mNormalZ[3] = 1.0f; |
|
343 |
} |
|
344 |
else |
|
345 |
{ |
|
346 |
mNormalZ[0] =-1.0f; |
|
347 |
mNormalZ[1] =-1.0f; |
|
348 |
mNormalZ[2] =-1.0f; |
|
349 |
mNormalZ[3] =-1.0f; |
|
350 |
} |
|
351 |
|
|
352 |
td = nw+n-w-c; |
|
353 |
lr = c+n-w-nw; |
|
354 |
if( td<0 ) td=-1; |
|
355 |
if( td>0 ) td= 1; |
|
356 |
if( lr<0 ) lr=-1; |
|
357 |
if( lr>0 ) lr= 1; |
|
358 |
mNormalX[0] = lr*R; |
|
359 |
mNormalY[0] = td*R; |
|
360 |
|
|
361 |
td = w+c-sw-s; |
|
362 |
lr = c+s-w-sw; |
|
363 |
if( td<0 ) td=-1; |
|
364 |
if( td>0 ) td= 1; |
|
365 |
if( lr<0 ) lr=-1; |
|
366 |
if( lr>0 ) lr= 1; |
|
367 |
mNormalX[1] = lr*R; |
|
368 |
mNormalY[1] = td*R; |
|
369 |
|
|
370 |
td = n+ne-c-e; |
|
371 |
lr = e+ne-c-n; |
|
372 |
if( td<0 ) td=-1; |
|
373 |
if( td>0 ) td= 1; |
|
374 |
if( lr<0 ) lr=-1; |
|
375 |
if( lr>0 ) lr= 1; |
|
376 |
mNormalX[2] = lr*R; |
|
377 |
mNormalY[2] = td*R; |
|
378 |
|
|
379 |
td = c+e-s-se; |
|
380 |
lr = e+se-c-s; |
|
381 |
if( td<0 ) td=-1; |
|
382 |
if( td>0 ) td= 1; |
|
383 |
if( lr<0 ) lr=-1; |
|
384 |
if( lr>0 ) lr= 1; |
|
385 |
mNormalX[3] = lr*R; |
|
386 |
mNormalY[3] = td*R; |
|
387 |
/* |
|
388 |
android.util.Log.d("CUBES", "row="+row+" col="+col); |
|
389 |
android.util.Log.d("CUBES", mNormalX[0]+" "+mNormalY[0]); |
|
390 |
android.util.Log.d("CUBES", mNormalX[1]+" "+mNormalY[1]); |
|
391 |
android.util.Log.d("CUBES", mNormalX[2]+" "+mNormalY[2]); |
|
392 |
android.util.Log.d("CUBES", mNormalX[3]+" "+mNormalY[3]); |
|
393 |
*/ |
|
394 |
} |
|
395 |
|
|
396 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
397 |
|
|
398 |
private int buildFrontBackGrid(boolean front, int vertex, float[] attribs) |
|
399 |
{ |
|
400 |
int last, current; |
|
401 |
boolean seenLand=false; |
|
402 |
boolean lastBlockIsNE = false; |
|
403 |
boolean currentBlockIsNE; |
|
404 |
float vectZ = (front ? 0.5f : -0.5f); |
|
405 |
|
|
406 |
//android.util.Log.d("CUBES", "buildFrontBack"); |
|
407 |
|
|
408 |
for(int row=0; row<mRows; row++) |
|
409 |
{ |
|
410 |
last =0; |
|
411 |
|
|
412 |
for(int col=0; col<mCols; col++) |
|
413 |
{ |
|
414 |
current = mCubes[row][col]; |
|
415 |
|
|
416 |
if( current%2 == 1 ) |
|
417 |
{ |
|
418 |
currentBlockIsNE = isNE(row,col); |
|
419 |
|
|
420 |
if( !seenLand && !front && ((vertex%2==1)^currentBlockIsNE) ) |
|
421 |
{ |
|
422 |
//android.util.Log.d("CUBES","repeating winding2 vertex"); |
|
423 |
|
|
424 |
vertex = repeatLast(vertex,attribs); |
|
425 |
} |
|
426 |
|
|
427 |
createNormals(front,row,col); |
|
428 |
|
|
429 |
if( currentBlockIsNE ) |
|
430 |
{ |
|
431 |
if( (last!=current) || !lastBlockIsNE ) |
|
432 |
{ |
|
433 |
if( seenLand && (last != current) ) vertex = repeatLast(vertex,attribs); |
|
434 |
vertex= addFrontVertex( vertex, 0, vectZ, col, row, attribs); |
|
435 |
if( seenLand && (last != current) ) vertex = repeatLast(vertex,attribs); |
|
436 |
if( !lastBlockIsNE || (!front && !seenLand) ) vertex = repeatLast(vertex,attribs); |
|
437 |
vertex= addFrontVertex( vertex, 1, vectZ, col, row+1, attribs); |
|
438 |
} |
|
439 |
vertex= addFrontVertex( vertex, 2, vectZ, col+1, row, attribs); |
|
440 |
vertex= addFrontVertex( vertex, 3, vectZ, col+1, row+1, attribs); |
|
441 |
} |
|
442 |
else |
|
443 |
{ |
|
444 |
if( (last!=current) || lastBlockIsNE ) |
|
445 |
{ |
|
446 |
if( seenLand && (last != current) ) vertex = repeatLast(vertex,attribs); |
|
447 |
vertex= addFrontVertex( vertex, 1, vectZ, col, row+1, attribs); |
|
448 |
if( seenLand && (last != current) ) vertex = repeatLast(vertex,attribs); |
|
449 |
if( lastBlockIsNE || (!front && !seenLand) ) vertex = repeatLast(vertex,attribs); |
|
450 |
vertex= addFrontVertex( vertex, 0, vectZ, col, row, attribs); |
|
451 |
} |
|
452 |
vertex= addFrontVertex( vertex, 3, vectZ, col+1, row+1, attribs); |
|
453 |
vertex= addFrontVertex( vertex, 2, vectZ, col+1, row , attribs); |
|
454 |
} |
|
455 |
|
|
456 |
seenLand = true; |
|
457 |
lastBlockIsNE = currentBlockIsNE; |
|
458 |
} |
|
459 |
|
|
460 |
last = current; |
|
461 |
} |
|
462 |
} |
|
463 |
|
|
464 |
return vertex; |
|
465 |
} |
|
466 |
|
|
467 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
468 |
|
|
469 |
private int repeatLast(int vertex, float[] attribs) |
|
470 |
{ |
|
471 |
//android.util.Log.e("CUBES", "repeating last vertex!"); |
|
472 |
|
|
473 |
if( vertex>0 ) |
|
474 |
{ |
|
475 |
remainingVert--; |
|
476 |
|
|
477 |
attribs[8*vertex ] = attribs[8*vertex-8]; |
|
478 |
attribs[8*vertex+1] = attribs[8*vertex-7]; |
|
479 |
attribs[8*vertex+2] = attribs[8*vertex-6]; |
|
480 |
attribs[8*vertex+3] = attribs[8*vertex-5]; |
|
481 |
attribs[8*vertex+4] = attribs[8*vertex-4]; |
|
482 |
attribs[8*vertex+5] = attribs[8*vertex-3]; |
|
483 |
attribs[8*vertex+6] = attribs[8*vertex-2]; |
|
484 |
attribs[8*vertex+7] = attribs[8*vertex-1]; |
|
485 |
|
|
486 |
vertex++; |
|
487 |
} |
|
488 |
|
|
489 |
return vertex; |
|
490 |
} |
|
491 |
|
|
492 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
493 |
|
|
494 |
private int buildSideGrid(int vertex, float[] attribs) |
|
495 |
{ |
|
496 |
//android.util.Log.d("CUBES", "buildSide"); |
|
497 |
|
|
498 |
for(int i=0; i<mEdgeNum; i++) |
|
499 |
{ |
|
500 |
vertex = buildIthSide(mEdges.get(i), vertex, attribs); |
|
501 |
} |
|
502 |
|
|
503 |
return vertex; |
|
504 |
} |
|
505 |
|
|
506 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
507 |
|
|
508 |
private int buildIthSide(Edge curr, int vertex, float[] attribs) |
|
509 |
{ |
|
510 |
Edge prev, next; |
|
511 |
int col, row, side; |
|
512 |
|
|
513 |
if( curr.side==NORTH ) // water outside |
|
514 |
{ |
|
515 |
prev = new Edge(WEST,curr.row,curr.col); |
|
516 |
} |
|
517 |
else // land outside; we need to move forward one link because we are |
|
518 |
{ // going in opposite direction and we need to start from a bend. |
|
519 |
prev = curr; |
|
520 |
curr = new Edge(EAST,curr.row+1,curr.col-1); |
|
521 |
} |
|
522 |
|
|
523 |
for(int i=0; i<mSlices; i++) |
|
524 |
{ |
|
525 |
col = curr.col; |
|
526 |
row = curr.row; |
|
527 |
side= curr.side; |
|
528 |
next = getNextEdge(curr); |
|
529 |
|
|
530 |
addSideVertex(curr,true,i+1,prev.side,vertex++,attribs); |
|
531 |
|
|
532 |
do |
|
533 |
{ |
|
534 |
if( prev.side!=curr.side ) |
|
535 |
{ |
|
536 |
addSideVertex(curr,true,i+1,prev.side,vertex++,attribs); |
|
537 |
addSideVertex(curr,true,i ,prev.side,vertex++,attribs); |
|
538 |
} |
|
539 |
|
|
540 |
addSideVertex(curr,false,i+1,next.side,vertex++,attribs); |
|
541 |
addSideVertex(curr,false,i ,next.side,vertex++,attribs); |
|
542 |
|
|
543 |
prev = curr; |
|
544 |
curr = next; |
|
545 |
next = getNextEdge(curr); |
|
546 |
} |
|
547 |
while( curr.col!=col || curr.row!=row || curr.side!=side ); |
|
548 |
|
|
549 |
vertex = repeatLast(vertex,attribs); |
|
550 |
} |
|
551 |
|
|
552 |
return vertex; |
|
553 |
} |
|
554 |
|
|
555 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
556 |
|
|
557 |
private Edge getNextEdge(Edge curr) |
|
558 |
{ |
|
559 |
int col = curr.col; |
|
560 |
int row = curr.row; |
|
561 |
|
|
562 |
//android.util.Log.e("CUBES", "row="+row+" col="+col+" mRows="+mRows+" mCols="+mCols); |
|
563 |
|
|
564 |
switch(curr.side) |
|
565 |
{ |
|
566 |
case NORTH: if( col==mCols-1 ) |
|
567 |
return new Edge(EAST,row,col); |
|
568 |
if( row>0 && mCubes[row-1][col+1]==mCubes[row][col] ) |
|
569 |
return new Edge(WEST,row-1,col+1); |
|
570 |
if( mCubes[row][col+1]==mCubes[row][col] ) |
|
571 |
return new Edge(NORTH,row,col+1); |
|
572 |
else |
|
573 |
return new Edge(EAST,row,col); |
|
574 |
|
|
575 |
case SOUTH: if( col==0 ) |
|
576 |
return new Edge(WEST,row,col); |
|
577 |
if( (row<mRows-1) && mCubes[row+1][col-1]==mCubes[row][col] ) |
|
578 |
return new Edge(EAST,row+1,col-1); |
|
579 |
if( mCubes[row][col-1]==mCubes[row][col] ) |
|
580 |
return new Edge(SOUTH,row,col-1); |
|
581 |
else |
|
582 |
return new Edge(WEST,row,col); |
|
583 |
|
|
584 |
case EAST : if( row==mRows-1 ) |
|
585 |
return new Edge(SOUTH,row,col); |
|
586 |
if( (col<mCols-1) && mCubes[row+1][col+1]==mCubes[row][col] ) |
|
587 |
return new Edge(NORTH,row+1,col+1); |
|
588 |
if( mCubes[row+1][col]==mCubes[row][col] ) |
|
589 |
return new Edge(EAST,row+1,col); |
|
590 |
else |
|
591 |
return new Edge(SOUTH,row,col); |
|
592 |
|
|
593 |
default : if( row==0 ) |
|
594 |
return new Edge(NORTH,row,col); |
|
595 |
if( col>0 && mCubes[row-1][col-1]==mCubes[row][col] ) |
|
596 |
return new Edge(SOUTH,row-1,col-1); |
|
597 |
if( mCubes[row-1][col]==mCubes[row][col] ) |
|
598 |
return new Edge(WEST,row-1,col); |
|
599 |
else |
|
600 |
return new Edge(NORTH,row,col); |
|
601 |
} |
|
602 |
} |
|
603 |
|
|
604 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
605 |
|
|
606 |
private int addFrontVertex(int vertex, int index, float vectZ, int col, int row, float[] attribs) |
|
607 |
{ |
|
608 |
remainingVert--; |
|
609 |
|
|
610 |
float x = (float)col/mCols; |
|
611 |
float y = (float)row/mRows; |
|
612 |
|
|
613 |
attribs[8*vertex ] = x-0.5f; |
|
614 |
attribs[8*vertex+1] = 0.5f-y; |
|
615 |
attribs[8*vertex+2] = vectZ; |
|
616 |
attribs[8*vertex+3] = mNormalX[index]; |
|
617 |
attribs[8*vertex+4] = mNormalY[index]; |
|
618 |
attribs[8*vertex+5] = mNormalZ[index]; |
|
619 |
attribs[8*vertex+6] = x; |
|
620 |
attribs[8*vertex+7] = 1.0f-y; |
|
621 |
|
|
622 |
return vertex+1; |
|
623 |
} |
|
624 |
|
|
625 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
626 |
|
|
627 |
private void addSideVertex(Edge curr, boolean back, int slice,int side, int vertex, float[] attribs) |
|
628 |
{ |
|
629 |
//android.util.Log.e("CUBES", "adding Side vertex!"); |
|
630 |
|
|
631 |
remainingVert--; |
|
632 |
|
|
633 |
float x, y; |
|
634 |
|
|
635 |
switch(curr.side) |
|
636 |
{ |
|
637 |
case NORTH: x = (float)(back ? (curr.col ):(curr.col+1))/mCols; |
|
638 |
|
|
639 |
attribs[8*vertex ] = x - 0.5f; |
|
640 |
attribs[8*vertex+1] = 0.5f - (float)curr.row/mRows; |
|
641 |
attribs[8*vertex+2] = 0.5f - (float)slice/mSlices; |
|
642 |
attribs[8*vertex+3] = side==NORTH ? 0.0f : (side==WEST?-R:R); |
|
643 |
attribs[8*vertex+4] = 1.0f; |
|
644 |
attribs[8*vertex+5] = (slice==0 ? R : (slice==mSlices ? -R:0) ); |
|
645 |
attribs[8*vertex+6] = x; |
|
646 |
attribs[8*vertex+7] = 1.0f-(float)(curr.row-slice)/mRows; |
|
647 |
break; |
|
648 |
case SOUTH: x = (float)(back ? (curr.col+1):(curr.col ))/mCols; |
|
649 |
|
|
650 |
attribs[8*vertex ] = x - 0.5f; |
|
651 |
attribs[8*vertex+1] = 0.5f - (float)(curr.row+1)/mRows; |
|
652 |
attribs[8*vertex+2] = 0.5f - (float)slice/mSlices; |
|
653 |
attribs[8*vertex+3] = side==SOUTH ? 0.0f: (side==EAST?-R:R); |
|
654 |
attribs[8*vertex+4] =-1.0f; |
|
655 |
attribs[8*vertex+5] = (slice==0 ? R : (slice==mSlices ? -R:0) ); |
|
656 |
attribs[8*vertex+6] = x; |
|
657 |
attribs[8*vertex+7] = 1.0f - (float)(curr.row+1+slice)/mRows; |
|
658 |
break; |
|
659 |
case WEST : y = (float)(back ? (curr.row+1):(curr.row))/mRows; |
|
660 |
|
|
661 |
attribs[8*vertex ] = (float)curr.col/mCols -0.5f; |
|
662 |
attribs[8*vertex+1] = 0.5f - y; |
|
663 |
attribs[8*vertex+2] = 0.5f - (float)slice/mSlices; |
|
664 |
attribs[8*vertex+3] =-1.0f; |
|
665 |
attribs[8*vertex+4] = side==WEST ? 0.0f : (side==NORTH?-R:R); |
|
666 |
attribs[8*vertex+5] = (slice==0 ? R : (slice==mSlices ? -R:0) ); |
|
667 |
attribs[8*vertex+6] = (float)(curr.col-slice)/mCols; |
|
668 |
attribs[8*vertex+7] = 1.0f - y; |
|
669 |
break; |
|
670 |
case EAST : y = (float)(back ? (curr.row):(curr.row+1))/mRows; |
|
671 |
|
|
672 |
attribs[8*vertex ] = (float)(curr.col+1)/mCols -0.5f; |
|
673 |
attribs[8*vertex+1] = 0.5f - y; |
|
674 |
attribs[8*vertex+2] = 0.5f - (float)slice/mSlices; |
|
675 |
attribs[8*vertex+3] = 1.0f; |
|
676 |
attribs[8*vertex+4] = side==EAST ? 0.0f : (side==SOUTH?-R:R); |
|
677 |
attribs[8*vertex+5] = (slice==0 ? R : (slice==mSlices ? -R:0) ); |
|
678 |
attribs[8*vertex+6] = (float)(curr.col+1+slice)/mCols; |
|
679 |
attribs[8*vertex+7] = 1.0f - y; |
|
680 |
break; |
|
681 |
} |
|
682 |
|
|
683 |
if(attribs[8*vertex+6]>1.0f) attribs[8*vertex+6] = 2.0f-attribs[8*vertex+6]; |
|
684 |
if(attribs[8*vertex+6]<0.0f) attribs[8*vertex+6] = -attribs[8*vertex+6]; |
|
685 |
if(attribs[8*vertex+7]>1.0f) attribs[8*vertex+7] = 2.0f-attribs[8*vertex+7]; |
|
686 |
if(attribs[8*vertex+7]<0.0f) attribs[8*vertex+7] = -attribs[8*vertex+7]; |
|
687 |
} |
|
688 |
|
|
689 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
690 |
|
|
691 |
private void build() |
|
692 |
{ |
|
693 |
int vertSoFar=0; |
|
694 |
float[] attribs= new float[(POS_DATA_SIZE+NOR_DATA_SIZE+TEX_DATA_SIZE)*numVertices]; |
|
695 |
|
|
696 |
vertSoFar = buildFrontBackGrid(true, vertSoFar,attribs); |
|
697 |
|
|
698 |
if( mSlices>0 ) |
|
699 |
{ |
|
700 |
vertSoFar = repeatLast(vertSoFar,attribs); |
|
701 |
if( vertSoFar%2==1 ) |
|
702 |
{ |
|
703 |
vertSoFar = repeatLast(vertSoFar,attribs); |
|
704 |
} |
|
705 |
vertSoFar = buildSideGrid (vertSoFar,attribs); |
|
706 |
buildFrontBackGrid (false,vertSoFar,attribs); |
|
707 |
} |
|
708 |
|
|
709 |
mEdges.clear(); |
|
710 |
mEdges = null; |
|
711 |
mCubes = null; |
|
712 |
|
|
713 |
if( remainingVert!=0 ) |
|
714 |
android.util.Log.e("MeshCubes", "remainingVert " +remainingVert ); |
|
715 |
|
|
716 |
mVertAttribs = ByteBuffer.allocateDirect(numVertices*VERTSIZE).order(ByteOrder.nativeOrder()).asFloatBuffer(); |
|
717 |
mVertAttribs.put(attribs).position(0); |
|
718 |
|
|
719 |
setData(numVertices, mVertAttribs); |
|
720 |
} |
|
721 |
|
|
722 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
723 |
// PUBLIC API |
|
724 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
725 |
/** |
|
726 |
* Creates the underlying mesh of vertices, normals, texture coords. |
|
727 |
* |
|
728 |
* @param cols Integer helping to parse the next parameter. |
|
729 |
* @param desc String describing the subset of a MxNx1 cuboid that we want to create. |
|
730 |
* Its MxN characters - all 0 or 1 - decide of appropriate field is taken or not. |
|
731 |
* <p></p> |
|
732 |
* <p> |
|
733 |
* <pre> |
|
734 |
* For example, (cols=2, desc="111010") describes the following shape: |
|
735 |
* |
|
736 |
* XX |
|
737 |
* X |
|
738 |
* X |
|
739 |
* |
|
740 |
* whereas (cols=2,desc="110001") describes |
|
741 |
* |
|
742 |
* XX |
|
743 |
* |
|
744 |
* X |
|
745 |
* </pre> |
|
746 |
* </p> |
|
747 |
* @param slices Number of slices, i.e. 'depth' of the Mesh. |
|
748 |
*/ |
|
749 |
public MeshCubes(int cols, String desc, int slices) |
|
750 |
{ |
|
751 |
super( (float)slices/cols); |
|
752 |
prepareDataStructures(cols,desc,slices); |
|
753 |
build(); |
|
754 |
} |
|
755 |
|
|
756 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
757 |
/** |
|
758 |
* Creates a full, hole-less underlying mesh of vertices, normals, texture coords and colors. |
|
759 |
* |
|
760 |
* @param cols Number of columns, i.e. 'width' of the Mesh. |
|
761 |
* @param rows Number of rows, i.e. 'height' of the Mesh. |
|
762 |
* @param slices Number of slices, i.e. 'depth' of the Mesh. |
|
763 |
*/ |
|
764 |
public MeshCubes(int cols, int rows, int slices) |
|
765 |
{ |
|
766 |
super( (float)slices/cols); |
|
767 |
prepareDataStructures(cols,rows,slices); |
|
768 |
build(); |
|
769 |
} |
|
770 |
} |
src/main/java/org/distorted/library/main/MeshFlat.java | ||
---|---|---|
1 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
2 |
// Copyright 2016 Leszek Koltunski // |
|
3 |
// // |
|
4 |
// This file is part of Distorted. // |
|
5 |
// // |
|
6 |
// Distorted is free software: you can redistribute it and/or modify // |
|
7 |
// it under the terms of the GNU General Public License as published by // |
|
8 |
// the Free Software Foundation, either version 2 of the License, or // |
|
9 |
// (at your option) any later version. // |
|
10 |
// // |
|
11 |
// Distorted is distributed in the hope that it will be useful, // |
|
12 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of // |
|
13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // |
|
14 |
// GNU General Public License for more details. // |
|
15 |
// // |
|
16 |
// You should have received a copy of the GNU General Public License // |
|
17 |
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. // |
|
18 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
19 |
|
|
20 |
package org.distorted.library.main; |
|
21 |
|
|
22 |
import java.nio.ByteBuffer; |
|
23 |
import java.nio.ByteOrder; |
|
24 |
|
|
25 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
26 |
/** |
|
27 |
* Create a flat, rectangular grid. |
|
28 |
* <p> |
|
29 |
* Perfect if you just want to display a flat Texture. If you are not planning to apply any VERTEX |
|
30 |
* effects to it, use MeshFlat(1,1), i.e. a Quad. Otherwise, create more vertices for more realistic effects! |
|
31 |
*/ |
|
32 |
public class MeshFlat extends MeshObject |
|
33 |
{ |
|
34 |
private int mCols, mRows; |
|
35 |
private int remainingVert; |
|
36 |
|
|
37 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
38 |
// Create a flat, full grid. |
|
39 |
|
|
40 |
private void computeNumberOfVertices(int cols, int rows) |
|
41 |
{ |
|
42 |
mRows=rows; |
|
43 |
mCols=cols; |
|
44 |
|
|
45 |
if( cols==1 && rows==1 ) |
|
46 |
{ |
|
47 |
numVertices = 4; |
|
48 |
} |
|
49 |
else |
|
50 |
{ |
|
51 |
numVertices = 2*( mRows*mCols +2*mRows - 1) +2*(mCols>=2 ? mRows:0) + |
|
52 |
(mCols>=2 && mRows>=2 ? 2*mRows-2 : 1); |
|
53 |
} |
|
54 |
|
|
55 |
//android.util.Log.e("MeshFlat","vertices="+numVertices+" rows="+mRows+" cols="+mCols); |
|
56 |
|
|
57 |
remainingVert = numVertices; |
|
58 |
} |
|
59 |
|
|
60 |
|
|
61 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
62 |
|
|
63 |
private int addVertex(int vertex, float x, float y, float[] attribs) |
|
64 |
{ |
|
65 |
remainingVert--; |
|
66 |
|
|
67 |
attribs[8*vertex ] = x-0.5f; |
|
68 |
attribs[8*vertex+1] = 0.5f-y; |
|
69 |
attribs[8*vertex+2] = 0; |
|
70 |
|
|
71 |
attribs[8*vertex+3] = 0.0f; |
|
72 |
attribs[8*vertex+4] = 0.0f; |
|
73 |
attribs[8*vertex+5] = 1.0f; |
|
74 |
|
|
75 |
attribs[8*vertex+6] = x; |
|
76 |
attribs[8*vertex+7] = 1.0f-y; |
|
77 |
|
|
78 |
return vertex+1; |
|
79 |
} |
|
80 |
|
|
81 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
82 |
|
|
83 |
private int repeatLast(int vertex, float[] attribs) |
|
84 |
{ |
|
85 |
remainingVert--; |
|
86 |
|
|
87 |
//android.util.Log.e("MeshFlat", "repeating last vertex!"); |
|
88 |
|
|
89 |
if( vertex>0 ) |
|
90 |
{ |
|
91 |
attribs[8*vertex ] = attribs[8*vertex-8]; |
|
92 |
attribs[8*vertex+1] = attribs[8*vertex-7]; |
|
93 |
attribs[8*vertex+2] = attribs[8*vertex-6]; |
|
94 |
attribs[8*vertex+3] = attribs[8*vertex-5]; |
|
95 |
attribs[8*vertex+4] = attribs[8*vertex-4]; |
|
96 |
attribs[8*vertex+5] = attribs[8*vertex-3]; |
|
97 |
attribs[8*vertex+6] = attribs[8*vertex-2]; |
|
98 |
attribs[8*vertex+7] = attribs[8*vertex-1]; |
|
99 |
|
|
100 |
vertex++; |
|
101 |
} |
|
102 |
|
|
103 |
return vertex; |
|
104 |
} |
|
105 |
|
|
106 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
107 |
|
|
108 |
private void buildGrid(float[] attribs) |
|
109 |
{ |
|
110 |
boolean lastBlockIsNE = false; |
|
111 |
boolean currentBlockIsNE; |
|
112 |
int vertex = 0; |
|
113 |
|
|
114 |
float x,y; |
|
115 |
final float X = 1.0f/mCols; |
|
116 |
final float Y = 1.0f/mRows; |
|
117 |
|
|
118 |
//android.util.Log.d("MeshFlat", "buildGrid"); |
|
119 |
|
|
120 |
y = 0.0f; |
|
121 |
|
|
122 |
for(int row=0; row<mRows; row++) |
|
123 |
{ |
|
124 |
x = 0.0f; |
|
125 |
|
|
126 |
for(int col=0; col<mCols; col++) |
|
127 |
{ |
|
128 |
currentBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1); |
|
129 |
|
|
130 |
if( col==0 || (lastBlockIsNE^currentBlockIsNE) ) |
|
131 |
{ |
|
132 |
if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs); |
|
133 |
vertex= addVertex( vertex, x, y+(currentBlockIsNE?0:Y), attribs); |
|
134 |
if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs); |
|
135 |
if( lastBlockIsNE^currentBlockIsNE) vertex = repeatLast(vertex,attribs); |
|
136 |
vertex= addVertex( vertex, x, y+(currentBlockIsNE?Y:0), attribs); |
|
137 |
} |
|
138 |
vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?0:Y), attribs); |
|
139 |
vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?Y:0), attribs); |
|
140 |
|
|
141 |
lastBlockIsNE = currentBlockIsNE; |
|
142 |
x+=X; |
|
143 |
} |
|
144 |
|
|
145 |
y+=Y; |
|
146 |
} |
|
147 |
|
|
148 |
//android.util.Log.d("MeshFlat", "buildGrid done"); |
|
149 |
} |
|
150 |
|
|
151 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
152 |
/* |
|
153 |
private static String debug(float[] val, int stop) |
|
154 |
{ |
|
155 |
String ret=""; |
|
156 |
|
|
157 |
for(int i=0; i<val.length; i++) |
|
158 |
{ |
|
159 |
if( i%stop==0 ) ret+="\n"; |
|
160 |
ret+=(" "+val[i]); |
|
161 |
} |
|
162 |
|
|
163 |
return ret; |
|
164 |
} |
|
165 |
*/ |
|
166 |
|
|
167 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
168 |
// PUBLIC API |
|
169 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
170 |
/** |
|
171 |
* Creates the underlying grid of vertices, normals and texture coords. |
|
172 |
* |
|
173 |
* @param cols Number of columns in the grid. |
|
174 |
* @param rows Number of rows in the grid. |
|
175 |
*/ |
|
176 |
public MeshFlat(int cols, int rows) |
|
177 |
{ |
|
178 |
super(0.0f); |
|
179 |
computeNumberOfVertices(cols,rows); |
|
180 |
|
|
181 |
float[] attribs= new float[(POS_DATA_SIZE+NOR_DATA_SIZE+TEX_DATA_SIZE)*numVertices]; |
|
182 |
|
|
183 |
buildGrid(attribs); |
|
184 |
|
|
185 |
//android.util.Log.e("MeshFlat", "dataLen="+numVertices); |
|
186 |
//android.util.Log.d("MeshFlat", "attribs: "+debug(attribs,8) ); |
|
187 |
|
|
188 |
if( remainingVert!=0 ) |
|
189 |
android.util.Log.d("MeshFlat", "remainingVert " +remainingVert ); |
|
190 |
|
|
191 |
mVertAttribs = ByteBuffer.allocateDirect(numVertices*VERTSIZE).order(ByteOrder.nativeOrder()).asFloatBuffer(); |
|
192 |
mVertAttribs.put(attribs).position(0); |
|
193 |
|
|
194 |
setData(numVertices, mVertAttribs); |
|
195 |
} |
|
196 |
} |
src/main/java/org/distorted/library/main/MeshObject.java | ||
---|---|---|
1 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
2 |
// Copyright 2016 Leszek Koltunski // |
|
3 |
// // |
|
4 |
// This file is part of Distorted. // |
|
5 |
// // |
|
6 |
// Distorted is free software: you can redistribute it and/or modify // |
|
7 |
// it under the terms of the GNU General Public License as published by // |
|
8 |
// the Free Software Foundation, either version 2 of the License, or // |
|
9 |
// (at your option) any later version. // |
|
10 |
// // |
|
11 |
// Distorted is distributed in the hope that it will be useful, // |
|
12 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of // |
|
13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // |
|
14 |
// GNU General Public License for more details. // |
|
15 |
// // |
|
16 |
// You should have received a copy of the GNU General Public License // |
|
17 |
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. // |
|
18 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
19 |
|
|
20 |
package org.distorted.library.main; |
|
21 |
|
|
22 |
import android.opengl.GLES31; |
|
23 |
import java.nio.FloatBuffer; |
|
24 |
|
|
25 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
26 |
/** |
|
27 |
* Abstract class which represents a Mesh, ie 3 arrays of Vertex attributes: 1) positions |
|
28 |
* 2) normals 3) texture coordinates. |
|
29 |
* <p> |
|
30 |
* If you want to render to a particular shape, extend from here, construct the attrib FloatBuffer |
|
31 |
* and provide correct numVertices. |
|
32 |
*/ |
|
33 |
public abstract class MeshObject |
|
34 |
{ |
|
35 |
private static final int BYTES_PER_FLOAT = 4; |
|
36 |
|
|
37 |
static final int POS_DATA_SIZE= 3; |
|
38 |
static final int NOR_DATA_SIZE= 3; |
|
39 |
static final int TEX_DATA_SIZE= 2; |
|
40 |
|
|
41 |
static final int OFFSET0 = 0; |
|
42 |
static final int OFFSET1 = (POS_DATA_SIZE )*BYTES_PER_FLOAT; |
|
43 |
static final int OFFSET2 = (POS_DATA_SIZE+NOR_DATA_SIZE )*BYTES_PER_FLOAT; |
|
44 |
|
|
45 |
static final int TFSIZE = (POS_DATA_SIZE+POS_DATA_SIZE )*BYTES_PER_FLOAT; |
|
46 |
static final int VERTSIZE= (POS_DATA_SIZE+NOR_DATA_SIZE+TEX_DATA_SIZE)*BYTES_PER_FLOAT; |
|
47 |
|
|
48 |
boolean mShowNormals; |
|
49 |
|
|
50 |
int numVertices; |
|
51 |
FloatBuffer mVertAttribs; // packed: PosX,PosY,PosZ, NorX,NorY,NorZ, TexS,TexT |
|
52 |
|
|
53 |
DistortedBuffer mVBO, mTFO; // main vertex buffer and transform feedback buffer |
|
54 |
|
|
55 |
final float zFactor; // strange workaround for the fact that we need to somehow store the 'depth' |
|
56 |
// of the Mesh. Used in DistortedEffects. See DistortedTexture.getDepth(). |
|
57 |
|
|
58 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
59 |
|
|
60 |
MeshObject(float factor) |
|
61 |
{ |
|
62 |
zFactor = factor; |
|
63 |
mShowNormals = false; |
|
64 |
|
|
65 |
mVBO = new DistortedBuffer(GLES31.GL_ARRAY_BUFFER , GLES31.GL_STATIC_READ); |
|
66 |
mTFO = new DistortedBuffer(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, GLES31.GL_STATIC_READ); |
|
67 |
} |
|
68 |
|
|
69 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
70 |
|
|
71 |
void setData(int size, FloatBuffer buffer) |
|
72 |
{ |
|
73 |
mVBO.setData(size*VERTSIZE, buffer); |
|
74 |
mTFO.setData(size*TFSIZE , null ); |
|
75 |
} |
|
76 |
|
|
77 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
78 |
|
|
79 |
int getVBO() |
|
80 |
{ |
|
81 |
return mVBO.mIndex[0]; |
|
82 |
} |
|
83 |
|
|
84 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
85 |
|
|
86 |
int getTFO() |
|
87 |
{ |
|
88 |
return mTFO.mIndex[0]; |
|
89 |
} |
|
90 |
|
|
91 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
92 |
// PUBLIC API |
|
93 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
94 |
/** |
|
95 |
* When rendering this Mesh, do we want to render the Normal vectors as well? |
|
96 |
* <p> |
|
97 |
* Will work only on OpenGL ES >= 3.0 devices. |
|
98 |
* |
|
99 |
* @param show Controls if we render the Normal vectors or not. |
|
100 |
*/ |
|
101 |
public void setShowNormals(boolean show) |
|
102 |
{ |
|
103 |
mShowNormals = (Distorted.GLSL >= 300 && show); |
|
104 |
} |
|
105 |
|
|
106 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
107 |
/** |
|
108 |
* Release all internal resources. |
|
109 |
*/ |
|
110 |
public void markForDeletion() |
|
111 |
{ |
|
112 |
mVertAttribs.clear(); |
|
113 |
|
|
114 |
mVBO.markForDeletion(); |
|
115 |
mTFO.markForDeletion(); |
|
116 |
} |
|
117 |
} |
|
118 |
|
|
119 |
|
|
120 |
|
src/main/java/org/distorted/library/mesh/MeshCubes.java | ||
---|---|---|
1 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
2 |
// Copyright 2016 Leszek Koltunski // |
|
3 |
// // |
|
4 |
// This file is part of Distorted. // |
|
5 |
// // |
|
6 |
// Distorted is free software: you can redistribute it and/or modify // |
|
7 |
// it under the terms of the GNU General Public License as published by // |
|
8 |
// the Free Software Foundation, either version 2 of the License, or // |
|
9 |
// (at your option) any later version. // |
|
10 |
// // |
|
11 |
// Distorted is distributed in the hope that it will be useful, // |
|
12 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of // |
|
13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // |
|
14 |
// GNU General Public License for more details. // |
|
15 |
// // |
|
16 |
// You should have received a copy of the GNU General Public License // |
|
17 |
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. // |
|
18 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
19 |
|
|
20 |
package org.distorted.library.mesh; |
|
21 |
|
|
22 |
import java.nio.ByteBuffer; |
|
23 |
import java.nio.ByteOrder; |
|
24 |
import java.util.ArrayList; |
|
25 |
|
|
26 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
27 |
/** |
|
28 |
* Create a 3D grid composed of Cubes. |
|
29 |
* <p> |
|
30 |
* Any subset of a MxNx1 cuboid is possible. |
|
31 |
*/ |
|
32 |
public class MeshCubes extends MeshObject |
|
33 |
{ |
|
34 |
private static final float R = 0.0f;//0.2f; |
|
35 |
|
|
36 |
private static final int NORTH = 0; |
|
37 |
private static final int WEST = 1; |
|
38 |
private static final int EAST = 2; |
|
39 |
private static final int SOUTH = 3; |
|
40 |
|
|
41 |
private static final float[] mNormalX = new float[4]; |
|
42 |
private static final float[] mNormalY = new float[4]; |
|
43 |
private static final float[] mNormalZ = new float[4]; |
|
44 |
|
|
45 |
private class Edge |
|
46 |
{ |
|
47 |
final int side; |
|
48 |
final int row; |
|
49 |
final int col; |
|
50 |
|
|
51 |
Edge(int s, int r, int c) |
|
52 |
{ |
|
53 |
side= s; |
|
54 |
row = r; |
|
55 |
col = c; |
|
56 |
} |
|
57 |
} |
|
58 |
|
|
59 |
private int mCols, mRows, mSlices; |
|
60 |
private int[][] mCubes; |
|
61 |
private ArrayList<Edge> mEdges = new ArrayList<>(); |
|
62 |
|
|
63 |
private int remainingVert; |
|
64 |
private int mSideBends; |
|
65 |
private int mEdgeNum; |
|
66 |
private int mSideWalls; |
|
67 |
|
|
68 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
69 |
// a Block is split into two triangles along the NE-SW line iff it is in the top-right |
|
70 |
// or bottom-left quadrant of the grid. |
|
71 |
|
|
72 |
private boolean isNE(int row,int col) |
|
73 |
{ |
|
74 |
return ( (2*row<mRows)^(2*col<mCols) ); |
|
75 |
} |
|
76 |
|
|
77 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
78 |
// return the number of vertices our grid will contain |
|
79 |
|
|
80 |
private int computeDataLength() |
|
81 |
{ |
|
82 |
int frontWalls=0, frontSegments=0, triangleShifts=0, windingShifts=0; |
|
83 |
int shiftCol = (mCols-1)/2; |
|
84 |
|
|
85 |
boolean lastBlockIsNE=false; |
|
86 |
boolean thisBlockIsNE; // the block we are currently looking at is split into |
|
87 |
// two triangles along the NE-SW line (rather than NW-SE) |
|
88 |
for(int row=0; row<mRows; row++) |
|
89 |
{ |
|
90 |
if( mCols>=2 && (mCubes[row][shiftCol]%2 == 1) && (mCubes[row][shiftCol+1]%2 == 1) ) triangleShifts++; |
|
91 |
|
|
92 |
for(int col=0; col<mCols; col++) |
|
93 |
{ |
|
94 |
if( mCubes[row][col]%2 == 1 ) // land |
|
95 |
{ |
|
96 |
thisBlockIsNE = isNE(row,col); |
|
97 |
if( thisBlockIsNE^lastBlockIsNE ) windingShifts++; |
|
98 |
lastBlockIsNE = thisBlockIsNE; |
|
99 |
frontWalls++; |
|
100 |
if( col==mCols-1 || mCubes[row][col+1]%2 == 0 ) frontSegments++; |
|
101 |
} |
|
102 |
} |
|
103 |
} |
|
104 |
|
|
105 |
int frontVert = 2*( frontWalls + 2*frontSegments - 1) +2*triangleShifts + windingShifts; |
|
106 |
int sideVertOneSlice= 2*( mSideWalls + mSideBends + mEdgeNum -1); |
|
107 |
int sideVert = 2*(mSlices-1) + mSlices*sideVertOneSlice; |
|
108 |
int firstWinding = (mSlices>0 && (frontVert+1)%2==1 ) ? 1:0; |
|
109 |
int dataL = mSlices==0 ? frontVert : (frontVert+1) +firstWinding+ (1+sideVert+1) + (1+frontVert); |
|
110 |
/* |
|
111 |
android.util.Log.e("CUBES","triangleShifts="+triangleShifts+" windingShifts="+windingShifts+" winding1="+firstWinding+" frontVert="+frontVert+" sideVert="+sideVert); |
|
112 |
android.util.Log.e("CUBES", "frontW="+frontWalls+" fSegments="+frontSegments+" sWalls="+mSideWalls+" sSegments="+mEdgeNum+" sideBends="+mSideBends+" dataLen="+dataL ); |
|
113 |
*/ |
|
114 |
return dataL<0 ? 0:dataL; |
|
115 |
} |
|
116 |
|
|
117 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
118 |
/* |
|
119 |
private static String debug(short[] val) |
|
120 |
{ |
|
121 |
String ret="";j |
|
122 |
|
|
123 |
for(int i=0; i<val.length; i++) ret+=(" "+val[i]); |
|
124 |
|
|
125 |
return ret; |
|
126 |
} |
|
127 |
*/ |
|
128 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
129 |
|
|
130 |
private static String debug(float[] val, int stop) |
|
131 |
{ |
|
132 |
String ret=""; |
|
133 |
float v; |
|
134 |
boolean neg; |
|
135 |
int mod; |
|
136 |
|
|
137 |
for(int i=0; i<val.length; i++) |
|
138 |
{ |
|
139 |
if( i%stop==0 ) ret+="\n"; |
|
140 |
|
|
141 |
mod = i%stop; |
|
142 |
|
|
143 |
if( mod==0 || mod==3 || mod==6 ) ret+=" ("; |
|
144 |
|
|
145 |
v = val[i]; |
|
146 |
if( v==-0.0f ) v=0.0f; |
|
147 |
|
|
148 |
|
|
149 |
neg = v<0; |
|
150 |
v = (v<0 ? -v:v); |
|
151 |
|
|
152 |
ret+=((neg? " -":" +")+v); |
|
153 |
|
|
154 |
if( mod==2 || mod==5 || mod==7 ) ret+=")"; |
|
155 |
} |
|
156 |
|
|
157 |
return ret; |
|
158 |
} |
|
159 |
|
|
160 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
161 |
/* |
|
162 |
private static String debug(Edge e) |
|
163 |
{ |
|
164 |
String d = ""; |
|
165 |
|
|
166 |
switch(e.side) |
|
167 |
{ |
|
168 |
case NORTH: d+="NORTH "; break; |
|
169 |
case SOUTH: d+="SOUTH "; break; |
|
170 |
case WEST : d+="WEST "; break; |
|
171 |
case EAST : d+="EAST "; break; |
|
172 |
} |
|
173 |
|
|
174 |
d+=("("+e.row+","+e.col+")"); |
|
175 |
|
|
176 |
return d; |
|
177 |
} |
|
178 |
*/ |
|
179 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
180 |
|
|
181 |
private void prepareDataStructures(int cols, String desc, int slices) |
|
182 |
{ |
|
183 |
mRows =0; |
|
184 |
mCols =0; |
|
185 |
mSlices =slices; |
|
186 |
numVertices=0; |
|
187 |
|
Also available in: Unified diff
Fully move the Meshes to their own package.