1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2017 Leszek Koltunski leszek@koltunski.pl //
|
3
|
// //
|
4
|
// This file is part of Distorted. //
|
5
|
// //
|
6
|
// This library is free software; you can redistribute it and/or //
|
7
|
// modify it under the terms of the GNU Lesser General Public //
|
8
|
// License as published by the Free Software Foundation; either //
|
9
|
// version 2.1 of the License, or (at your option) any later version. //
|
10
|
// //
|
11
|
// This library 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 GNU //
|
14
|
// Lesser General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU Lesser General Public //
|
17
|
// License along with this library; if not, write to the Free Software //
|
18
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA //
|
19
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
20
|
|
21
|
package org.distorted.library.mesh;
|
22
|
|
23
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
24
|
|
25
|
import org.distorted.library.main.DistortedLibrary;
|
26
|
|
27
|
/**
|
28
|
* Create a flat, rectangular grid.
|
29
|
* <p>
|
30
|
* Perfect if you just want to display a flat Texture. If you are not planning to apply any VERTEX
|
31
|
* effects to it, use MeshRectangles(1,1), i.e. a Quad. Otherwise, create more vertices for more realistic effects!
|
32
|
*/
|
33
|
public class MeshSquare extends MeshBase
|
34
|
{
|
35
|
private int mCols, mRows;
|
36
|
private int remainingVert;
|
37
|
private int numVertices;
|
38
|
|
39
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
40
|
// Create a flat, full grid.
|
41
|
|
42
|
private void computeNumberOfVertices(int cols, int rows)
|
43
|
{
|
44
|
mRows=rows;
|
45
|
mCols=cols;
|
46
|
|
47
|
if( cols==1 && rows==1 )
|
48
|
{
|
49
|
numVertices = 4;
|
50
|
}
|
51
|
else
|
52
|
{
|
53
|
numVertices = 2*( mRows*mCols +2*mRows - 1) +2*(mCols>=2 ? mRows:0) +
|
54
|
(mCols>=2 && mRows>=2 ? 2*mRows-2 : 1);
|
55
|
}
|
56
|
|
57
|
remainingVert = numVertices;
|
58
|
}
|
59
|
|
60
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
61
|
|
62
|
private int addVertex(int vertex, float x, float y, float[] attribs1, float[] attribs2)
|
63
|
{
|
64
|
remainingVert--;
|
65
|
|
66
|
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB ] = x;
|
67
|
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] = -y;
|
68
|
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = 0.0f;
|
69
|
|
70
|
attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB ] = 0.0f;
|
71
|
attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+1] = 0.0f;
|
72
|
attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+2] = 1.0f;
|
73
|
|
74
|
attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB ] = x+0.5f;
|
75
|
attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = 0.5f-y;
|
76
|
|
77
|
return vertex+1;
|
78
|
}
|
79
|
|
80
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
81
|
|
82
|
private int repeatLast(int vertex, float[] attribs1, float[] attribs2)
|
83
|
{
|
84
|
if( vertex>0 )
|
85
|
{
|
86
|
remainingVert--;
|
87
|
|
88
|
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB ] = attribs1[VERT1_ATTRIBS*(vertex-1) + POS_ATTRIB ];
|
89
|
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] = attribs1[VERT1_ATTRIBS*(vertex-1) + POS_ATTRIB+1];
|
90
|
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = attribs1[VERT1_ATTRIBS*(vertex-1) + POS_ATTRIB+2];
|
91
|
|
92
|
attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB ] = attribs1[VERT1_ATTRIBS*(vertex-1) + NOR_ATTRIB ];
|
93
|
attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+1] = attribs1[VERT1_ATTRIBS*(vertex-1) + NOR_ATTRIB+1];
|
94
|
attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+2] = attribs1[VERT1_ATTRIBS*(vertex-1) + NOR_ATTRIB+2];
|
95
|
|
96
|
attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB ] = attribs2[VERT2_ATTRIBS*(vertex-1) + TEX_ATTRIB ];
|
97
|
attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = attribs2[VERT2_ATTRIBS*(vertex-1) + TEX_ATTRIB+1];
|
98
|
|
99
|
vertex++;
|
100
|
}
|
101
|
|
102
|
return vertex;
|
103
|
}
|
104
|
|
105
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
106
|
|
107
|
private void buildGrid(float[] attribs1, float[] attribs2)
|
108
|
{
|
109
|
boolean currBlockIsNE, lastBlockIsNE = false;
|
110
|
int vertex = 0;
|
111
|
float x,y;
|
112
|
final float dx = 1.0f/mCols;
|
113
|
final float dy = 1.0f/mRows;
|
114
|
|
115
|
y =-0.5f;
|
116
|
|
117
|
for(int row=0; row<mRows; row++)
|
118
|
{
|
119
|
x =-0.5f;
|
120
|
|
121
|
for(int col=0; col<mCols; col++)
|
122
|
{
|
123
|
currBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1);
|
124
|
|
125
|
if( col==0 || (lastBlockIsNE^currBlockIsNE) )
|
126
|
{
|
127
|
if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs1,attribs2);
|
128
|
vertex= addVertex( vertex, x, y+(currBlockIsNE?0:dy), attribs1,attribs2);
|
129
|
if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs1,attribs2);
|
130
|
if( lastBlockIsNE^currBlockIsNE) vertex = repeatLast(vertex,attribs1,attribs2);
|
131
|
vertex= addVertex( vertex, x, y+(currBlockIsNE?dy:0), attribs1,attribs2);
|
132
|
}
|
133
|
vertex= addVertex( vertex, x+dx, y+(currBlockIsNE?0:dy), attribs1,attribs2);
|
134
|
vertex= addVertex( vertex, x+dx, y+(currBlockIsNE?dy:0), attribs1,attribs2);
|
135
|
|
136
|
lastBlockIsNE = currBlockIsNE;
|
137
|
x+=dx;
|
138
|
}
|
139
|
|
140
|
y+=dy;
|
141
|
}
|
142
|
}
|
143
|
|
144
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
145
|
|
146
|
private void buildGrid(float[] attribs1, float[] attribs2, float[] xLoc, float[] yLoc)
|
147
|
{
|
148
|
boolean currBlockIsNE,lastBlockIsNE = false;
|
149
|
int vertex = 0;
|
150
|
float dx,dy,x,y= yLoc[0];
|
151
|
|
152
|
for(int row=0; row<mRows; row++)
|
153
|
{
|
154
|
x = xLoc[0];
|
155
|
dy= yLoc[row+1];
|
156
|
|
157
|
for(int col=0; col<mCols; col++)
|
158
|
{
|
159
|
dx = xLoc[col+1];
|
160
|
currBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1);
|
161
|
|
162
|
if( col==0 || (lastBlockIsNE^currBlockIsNE) )
|
163
|
{
|
164
|
if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs1,attribs2);
|
165
|
vertex= addVertex( vertex, x, y+(currBlockIsNE?0:dy), attribs1,attribs2);
|
166
|
if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs1,attribs2);
|
167
|
if( lastBlockIsNE^currBlockIsNE) vertex = repeatLast(vertex,attribs1,attribs2);
|
168
|
vertex= addVertex( vertex, x, y+(currBlockIsNE?dy:0), attribs1,attribs2);
|
169
|
}
|
170
|
vertex= addVertex( vertex, x+dx, y+(currBlockIsNE?0:dy), attribs1,attribs2);
|
171
|
vertex= addVertex( vertex, x+dx, y+(currBlockIsNE?dy:0), attribs1,attribs2);
|
172
|
|
173
|
lastBlockIsNE = currBlockIsNE;
|
174
|
x+=dx;
|
175
|
}
|
176
|
|
177
|
y+=dy;
|
178
|
}
|
179
|
}
|
180
|
|
181
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
182
|
// PUBLIC API
|
183
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
184
|
/**
|
185
|
* Creates a rectangular grid of vertices, normals and texture coords.
|
186
|
*
|
187
|
* @param cols Number of columns in the grid.
|
188
|
* @param rows Number of rows in the grid.
|
189
|
*/
|
190
|
public MeshSquare(int cols, int rows)
|
191
|
{
|
192
|
super();
|
193
|
computeNumberOfVertices(cols,rows);
|
194
|
|
195
|
float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
|
196
|
float[] attribs2= new float[VERT2_ATTRIBS*numVertices];
|
197
|
|
198
|
buildGrid(attribs1,attribs2);
|
199
|
|
200
|
if( remainingVert!=0 )
|
201
|
DistortedLibrary.logMessage("MeshSquare: remainingVert " +remainingVert );
|
202
|
|
203
|
setAttribs(attribs1,attribs2);
|
204
|
}
|
205
|
|
206
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
207
|
/**
|
208
|
* Creates a rectangular grid of vertices, normals and texture coords.
|
209
|
*
|
210
|
* @param xLoc list of x-coordinates of vertices. First value: distance of the left edge from 0.
|
211
|
* Next values: distance of the next column from the previous. Must be NonNull!
|
212
|
* @param yLoc list of y-coordinates of vertices. First value: distance of the bottom edge from 0.
|
213
|
* Next values: distance of the next row from the previous. Must be NonNull!
|
214
|
*/
|
215
|
public MeshSquare(float[] xLoc, float[] yLoc)
|
216
|
{
|
217
|
super();
|
218
|
computeNumberOfVertices(xLoc.length-1,yLoc.length-1);
|
219
|
|
220
|
float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
|
221
|
float[] attribs2= new float[VERT2_ATTRIBS*numVertices];
|
222
|
|
223
|
buildGrid(attribs1,attribs2,xLoc,yLoc);
|
224
|
|
225
|
if( remainingVert!=0 )
|
226
|
DistortedLibrary.logMessage("MeshSquare: remainingVert " +remainingVert );
|
227
|
|
228
|
setAttribs(attribs1,attribs2);
|
229
|
}
|
230
|
|
231
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
232
|
/**
|
233
|
* Copy constructor.
|
234
|
*/
|
235
|
public MeshSquare(MeshSquare mesh, boolean deep)
|
236
|
{
|
237
|
super(mesh,deep);
|
238
|
}
|
239
|
|
240
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
241
|
/**
|
242
|
* Copy the Mesh.
|
243
|
*
|
244
|
* @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
|
245
|
* normals and inflates (the rest, in particular the mVertAttribs2 containing texture
|
246
|
* coordinates and effect associations, is always deep copied)
|
247
|
*/
|
248
|
public MeshSquare copy(boolean deep)
|
249
|
{
|
250
|
return new MeshSquare(this,deep);
|
251
|
}
|
252
|
}
|