Revision a51fe521
Added by Leszek Koltunski about 7 years ago
src/main/java/org/distorted/library/MeshFlat.java | ||
---|---|---|
42 | 42 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
43 | 43 |
// Create a flat, full grid. |
44 | 44 |
|
45 |
private void computeNumberOfVertices(int cols, int rows)
|
|
45 |
private void computeNumberOfVertices(int cols, int rows) |
|
46 | 46 |
{ |
47 | 47 |
mRows=rows; |
48 | 48 |
mCols=cols; |
49 | 49 |
|
50 | 50 |
if( cols==1 && rows==1 ) |
51 | 51 |
{ |
52 |
dataLength = 4;
|
|
52 |
numVertices = 4;
|
|
53 | 53 |
} |
54 | 54 |
else |
55 | 55 |
{ |
56 |
dataLength = 2*( mRows*mCols +2*mRows - 1) +2*(mCols>=2 ? mRows:0) +
|
|
57 |
(mCols>=2 && mRows>=2 ? 2*mRows-2 : 1); |
|
56 |
numVertices = 2*( mRows*mCols +2*mRows - 1) +2*(mCols>=2 ? mRows:0) +
|
|
57 |
(mCols>=2 && mRows>=2 ? 2*mRows-2 : 1);
|
|
58 | 58 |
} |
59 | 59 |
|
60 |
//android.util.Log.e("BITMAP","vertices="+dataLength+" rows="+mRows+" cols="+mCols);
|
|
60 |
//android.util.Log.e("BITMAP","vertices="+numVertices+" rows="+mRows+" cols="+mCols);
|
|
61 | 61 |
|
62 |
remainingVert = dataLength;
|
|
62 |
remainingVert = numVertices;
|
|
63 | 63 |
} |
64 | 64 |
|
65 | 65 |
|
66 | 66 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
67 | 67 |
|
68 |
private int addVertex(int vertex, float x, float y, float[] position, float[] normal, float[] texture)
|
|
68 |
private int addVertex(int vertex, float x, float y, float[] attribs)
|
|
69 | 69 |
{ |
70 | 70 |
remainingVert--; |
71 | 71 |
|
72 |
position[3*vertex ] = x-0.5f;
|
|
73 |
position[3*vertex+1] = 0.5f-y;
|
|
74 |
position[3*vertex+2] = 0;
|
|
72 |
attribs[8*vertex ] = x-0.5f;
|
|
73 |
attribs[8*vertex+1] = 0.5f-y;
|
|
74 |
attribs[8*vertex+2] = 0;
|
|
75 | 75 |
|
76 |
texture[2*vertex ] = x; |
|
77 |
texture[2*vertex+1] = 1.0f-y; |
|
76 |
attribs[8*vertex+3] = 0.0f; |
|
77 |
attribs[8*vertex+4] = 0.0f; |
|
78 |
attribs[8*vertex+5] = 1.0f; |
|
78 | 79 |
|
79 |
normal[3*vertex ] = 0.0f; |
|
80 |
normal[3*vertex+1] = 0.0f; |
|
81 |
normal[3*vertex+2] = 1.0f; |
|
80 |
attribs[8*vertex+6] = x; |
|
81 |
attribs[8*vertex+7] = 1.0f-y; |
|
82 | 82 |
|
83 | 83 |
return vertex+1; |
84 | 84 |
} |
85 | 85 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
86 | 86 |
|
87 |
private int repeatLast(int vertex, float[] position, float[] normal, float[] texture)
|
|
87 |
private int repeatLast(int vertex, float[] attribs)
|
|
88 | 88 |
{ |
89 | 89 |
remainingVert--; |
90 | 90 |
|
... | ... | |
92 | 92 |
|
93 | 93 |
if( vertex>0 ) |
94 | 94 |
{ |
95 |
position[3*vertex ] = position[3*vertex-3]; |
|
96 |
position[3*vertex+1] = position[3*vertex-2]; |
|
97 |
position[3*vertex+2] = position[3*vertex-1]; |
|
98 |
|
|
99 |
normal[3*vertex ] = normal[3*vertex-3]; |
|
100 |
normal[3*vertex+1] = normal[3*vertex-2]; |
|
101 |
normal[3*vertex+2] = normal[3*vertex-1]; |
|
102 |
|
|
103 |
texture[2*vertex ] = texture[2*vertex-2]; |
|
104 |
texture[2*vertex+1] = texture[2*vertex-1]; |
|
95 |
attribs[8*vertex ] = attribs[8*vertex-8]; |
|
96 |
attribs[8*vertex+1] = attribs[8*vertex-7]; |
|
97 |
attribs[8*vertex+2] = attribs[8*vertex-6]; |
|
98 |
attribs[8*vertex+3] = attribs[8*vertex-5]; |
|
99 |
attribs[8*vertex+4] = attribs[8*vertex-4]; |
|
100 |
attribs[8*vertex+5] = attribs[8*vertex-3]; |
|
101 |
attribs[8*vertex+6] = attribs[8*vertex-2]; |
|
102 |
attribs[8*vertex+7] = attribs[8*vertex-1]; |
|
105 | 103 |
|
106 | 104 |
vertex++; |
107 | 105 |
} |
... | ... | |
111 | 109 |
|
112 | 110 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
113 | 111 |
|
114 |
private void buildGrid(float[] position, float[] normal, float[] texture)
|
|
112 |
private void buildGrid(float[] attribs)
|
|
115 | 113 |
{ |
116 | 114 |
boolean lastBlockIsNE = false; |
117 | 115 |
boolean currentBlockIsNE; |
... | ... | |
135 | 133 |
|
136 | 134 |
if( col==0 || (lastBlockIsNE^currentBlockIsNE) ) |
137 | 135 |
{ |
138 |
if( row!=0 && col==0 ) vertex = repeatLast(vertex,position,normal,texture);
|
|
139 |
vertex= addVertex( vertex, x, y+(currentBlockIsNE?0:Y), position, normal, texture);
|
|
140 |
if( row!=0 && col==0 ) vertex = repeatLast(vertex,position,normal,texture);
|
|
141 |
if( lastBlockIsNE^currentBlockIsNE) vertex = repeatLast(vertex,position,normal,texture);
|
|
142 |
vertex= addVertex( vertex, x, y+(currentBlockIsNE?Y:0), position, normal, texture);
|
|
136 |
if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs);
|
|
137 |
vertex= addVertex( vertex, x, y+(currentBlockIsNE?0:Y), attribs);
|
|
138 |
if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs);
|
|
139 |
if( lastBlockIsNE^currentBlockIsNE) vertex = repeatLast(vertex,attribs);
|
|
140 |
vertex= addVertex( vertex, x, y+(currentBlockIsNE?Y:0), attribs);
|
|
143 | 141 |
} |
144 |
vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?0:Y), position, normal, texture);
|
|
145 |
vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?Y:0), position, normal, texture);
|
|
142 |
vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?0:Y), attribs);
|
|
143 |
vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?Y:0), attribs);
|
|
146 | 144 |
|
147 | 145 |
lastBlockIsNE = currentBlockIsNE; |
148 | 146 |
x+=X; |
... | ... | |
156 | 154 |
|
157 | 155 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
158 | 156 |
/* |
159 |
private static String debug(float[] val, int stop)
|
|
157 |
private static String debug(float[] val, int stop) |
|
160 | 158 |
{ |
161 | 159 |
String ret=""; |
162 | 160 |
|
... | ... | |
169 | 167 |
return ret; |
170 | 168 |
} |
171 | 169 |
*/ |
172 |
|
|
173 | 170 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
174 | 171 |
|
175 |
float[] getBoundingVertices()
|
|
172 |
float[] getBoundingVertices() |
|
176 | 173 |
{ |
177 | 174 |
return mBoundingVert; |
178 | 175 |
} |
... | ... | |
186 | 183 |
* @param cols Number of columns in the grid. |
187 | 184 |
* @param rows Number of rows in the grid. |
188 | 185 |
*/ |
189 |
public MeshFlat(int cols, int rows) |
|
190 |
{ |
|
191 |
super(0.0f); |
|
192 |
computeNumberOfVertices(cols,rows); |
|
193 |
|
|
194 |
float[] positionData= new float[POSITION_DATA_SIZE*dataLength]; |
|
195 |
float[] normalData = new float[NORMAL_DATA_SIZE *dataLength]; |
|
196 |
float[] textureData = new float[TEX_DATA_SIZE *dataLength]; |
|
197 |
|
|
198 |
buildGrid(positionData,normalData,textureData); |
|
186 |
public MeshFlat(int cols, int rows) |
|
187 |
{ |
|
188 |
super(0.0f); |
|
189 |
computeNumberOfVertices(cols,rows); |
|
199 | 190 |
|
200 |
//android.util.Log.e("CUBES","dataLen="+dataLength); |
|
201 |
//android.util.Log.d("CUBES", "position: "+debug(positionData,3) ); |
|
202 |
//android.util.Log.d("CUBES", "normal: " +debug( normalData,3) ); |
|
203 |
//android.util.Log.d("CUBES", "texture: " +debug( textureData,2) ); |
|
191 |
float[] attribs= new float[(POSITION_DATA_SIZE+NORMAL_DATA_SIZE+TEX_DATA_SIZE)*numVertices]; |
|
204 | 192 |
|
205 |
if( remainingVert!=0 ) |
|
206 |
android.util.Log.d("BITMAP", "remainingVert " +remainingVert ); |
|
193 |
buildGrid(attribs); |
|
207 | 194 |
|
208 |
mMeshPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
|
209 |
mMeshPositions.put(positionData).position(0);
|
|
195 |
//android.util.Log.e("MeshFlat", "dataLen="+numVertices);
|
|
196 |
//android.util.Log.d("MeshFlat", "attribs: "+debug(attribs,8) );
|
|
210 | 197 |
|
211 |
mMeshNormals = ByteBuffer.allocateDirect(NORMAL_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
|
212 |
mMeshNormals.put(normalData).position(0);
|
|
198 |
if( remainingVert!=0 )
|
|
199 |
android.util.Log.d("BITMAP", "remainingVert " +remainingVert );
|
|
213 | 200 |
|
214 |
mMeshTexture = ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer(); |
|
215 |
mMeshTexture.put(textureData).position(0); |
|
216 |
} |
|
217 |
} |
|
201 |
mVertAttribs = ByteBuffer.allocateDirect(numVertices*VERTSIZE).order(ByteOrder.nativeOrder()).asFloatBuffer(); |
|
202 |
mVertAttribs.put(attribs).position(0); |
|
203 |
} |
|
204 |
} |
Also available in: Unified diff
Move from unpacked to packad server-side Vertex attribute buffer.