1 |
d333eb6b
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
// Copyright 2016 Leszek Koltunski //
|
3 |
|
|
// //
|
4 |
46b572b5
|
Leszek Koltunski
|
// This file is part of Distorted. //
|
5 |
d333eb6b
|
Leszek Koltunski
|
// //
|
6 |
46b572b5
|
Leszek Koltunski
|
// Distorted is free software: you can redistribute it and/or modify //
|
7 |
d333eb6b
|
Leszek Koltunski
|
// 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 |
46b572b5
|
Leszek Koltunski
|
// Distorted is distributed in the hope that it will be useful, //
|
12 |
d333eb6b
|
Leszek Koltunski
|
// 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 |
46b572b5
|
Leszek Koltunski
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18 |
d333eb6b
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19 |
|
|
|
20 |
6c00149d
|
Leszek Koltunski
|
package org.distorted.library.mesh;
|
21 |
6a06a912
|
Leszek Koltunski
|
|
22 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
23 |
e0b6c593
|
Leszek Koltunski
|
/**
|
24 |
05403bba
|
Leszek Koltunski
|
* Create a flat, rectangular grid.
|
25 |
e0b6c593
|
Leszek Koltunski
|
* <p>
|
26 |
|
|
* Perfect if you just want to display a flat Texture. If you are not planning to apply any VERTEX
|
27 |
12e379d6
|
Leszek Koltunski
|
* effects to it, use MeshRectangles(1,1), i.e. a Quad. Otherwise, create more vertices for more realistic effects!
|
28 |
e0b6c593
|
Leszek Koltunski
|
*/
|
29 |
12e379d6
|
Leszek Koltunski
|
public class MeshRectangles extends MeshBase
|
30 |
6a06a912
|
Leszek Koltunski
|
{
|
31 |
adb2661c
|
Leszek Koltunski
|
private int mCols, mRows;
|
32 |
|
|
private int remainingVert;
|
33 |
da681e7e
|
Leszek Koltunski
|
private int numVertices;
|
34 |
6a06a912
|
Leszek Koltunski
|
|
35 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
36 |
cacc63de
|
Leszek Koltunski
|
// Create a flat, full grid.
|
37 |
6a06a912
|
Leszek Koltunski
|
|
38 |
a51fe521
|
Leszek Koltunski
|
private void computeNumberOfVertices(int cols, int rows)
|
39 |
adb2661c
|
Leszek Koltunski
|
{
|
40 |
79f172ab
|
Leszek Koltunski
|
mRows=rows;
|
41 |
|
|
mCols=cols;
|
42 |
d44ac567
|
Leszek Koltunski
|
|
43 |
79f172ab
|
Leszek Koltunski
|
if( cols==1 && rows==1 )
|
44 |
|
|
{
|
45 |
da681e7e
|
Leszek Koltunski
|
numVertices = 4;
|
46 |
79f172ab
|
Leszek Koltunski
|
}
|
47 |
|
|
else
|
48 |
adb2661c
|
Leszek Koltunski
|
{
|
49 |
da681e7e
|
Leszek Koltunski
|
numVertices = 2*( mRows*mCols +2*mRows - 1) +2*(mCols>=2 ? mRows:0) +
|
50 |
a51fe521
|
Leszek Koltunski
|
(mCols>=2 && mRows>=2 ? 2*mRows-2 : 1);
|
51 |
79f172ab
|
Leszek Koltunski
|
}
|
52 |
d44ac567
|
Leszek Koltunski
|
|
53 |
da681e7e
|
Leszek Koltunski
|
remainingVert = numVertices;
|
54 |
adb2661c
|
Leszek Koltunski
|
}
|
55 |
d44ac567
|
Leszek Koltunski
|
|
56 |
adb2661c
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
57 |
6a06a912
|
Leszek Koltunski
|
|
58 |
e54bfada
|
Leszek Koltunski
|
private int addVertex(int vertex, float x, float y, float[] attribs1, float[] attribs2)
|
59 |
adb2661c
|
Leszek Koltunski
|
{
|
60 |
|
|
remainingVert--;
|
61 |
6a06a912
|
Leszek Koltunski
|
|
62 |
1833b0a0
|
Leszek Koltunski
|
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB ] = x;
|
63 |
|
|
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] = -y;
|
64 |
e54bfada
|
Leszek Koltunski
|
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = 0.0f;
|
65 |
6a06a912
|
Leszek Koltunski
|
|
66 |
e54bfada
|
Leszek Koltunski
|
attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB ] = 0.0f;
|
67 |
|
|
attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+1] = 0.0f;
|
68 |
|
|
attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+2] = 1.0f;
|
69 |
6a06a912
|
Leszek Koltunski
|
|
70 |
1833b0a0
|
Leszek Koltunski
|
attribs1[VERT1_ATTRIBS*vertex + INF_ATTRIB ] = x;
|
71 |
|
|
attribs1[VERT1_ATTRIBS*vertex + INF_ATTRIB+1] = -y;
|
72 |
|
|
attribs1[VERT1_ATTRIBS*vertex + INF_ATTRIB+2] = 0.01f; // Inflated surface needs to be slightly in front
|
73 |
6f2d931d
|
Leszek Koltunski
|
|
74 |
1833b0a0
|
Leszek Koltunski
|
attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB ] = x+0.5f;
|
75 |
|
|
attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = 0.5f-y;
|
76 |
6a06a912
|
Leszek Koltunski
|
|
77 |
adb2661c
|
Leszek Koltunski
|
return vertex+1;
|
78 |
|
|
}
|
79 |
466450b5
|
Leszek Koltunski
|
|
80 |
adb2661c
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
81 |
d44ac567
|
Leszek Koltunski
|
|
82 |
e54bfada
|
Leszek Koltunski
|
private int repeatLast(int vertex, float[] attribs1, float[] attribs2)
|
83 |
adb2661c
|
Leszek Koltunski
|
{
|
84 |
|
|
if( vertex>0 )
|
85 |
|
|
{
|
86 |
6f2d931d
|
Leszek Koltunski
|
remainingVert--;
|
87 |
|
|
|
88 |
e54bfada
|
Leszek Koltunski
|
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 |
6f2d931d
|
Leszek Koltunski
|
|
92 |
e54bfada
|
Leszek Koltunski
|
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 |
6f2d931d
|
Leszek Koltunski
|
|
96 |
e54bfada
|
Leszek Koltunski
|
attribs1[VERT1_ATTRIBS*vertex + INF_ATTRIB ] = attribs1[VERT1_ATTRIBS*(vertex-1) + INF_ATTRIB ];
|
97 |
|
|
attribs1[VERT1_ATTRIBS*vertex + INF_ATTRIB+1] = attribs1[VERT1_ATTRIBS*(vertex-1) + INF_ATTRIB+1];
|
98 |
|
|
attribs1[VERT1_ATTRIBS*vertex + INF_ATTRIB+2] = attribs1[VERT1_ATTRIBS*(vertex-1) + INF_ATTRIB+2];
|
99 |
6f2d931d
|
Leszek Koltunski
|
|
100 |
e54bfada
|
Leszek Koltunski
|
attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB ] = attribs2[VERT2_ATTRIBS*(vertex-1) + TEX_ATTRIB ];
|
101 |
|
|
attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = attribs2[VERT2_ATTRIBS*(vertex-1) + TEX_ATTRIB+1];
|
102 |
d44ac567
|
Leszek Koltunski
|
|
103 |
adb2661c
|
Leszek Koltunski
|
vertex++;
|
104 |
|
|
}
|
105 |
6a06a912
|
Leszek Koltunski
|
|
106 |
adb2661c
|
Leszek Koltunski
|
return vertex;
|
107 |
|
|
}
|
108 |
d44ac567
|
Leszek Koltunski
|
|
109 |
adb2661c
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
110 |
|
|
|
111 |
e54bfada
|
Leszek Koltunski
|
private void buildGrid(float[] attribs1, float[] attribs2)
|
112 |
adb2661c
|
Leszek Koltunski
|
{
|
113 |
1833b0a0
|
Leszek Koltunski
|
boolean currBlockIsNE, lastBlockIsNE = false;
|
114 |
adb2661c
|
Leszek Koltunski
|
int vertex = 0;
|
115 |
69ed1eb4
|
Leszek Koltunski
|
float x,y;
|
116 |
1833b0a0
|
Leszek Koltunski
|
final float dx = 1.0f/mCols;
|
117 |
|
|
final float dy = 1.0f/mRows;
|
118 |
69ed1eb4
|
Leszek Koltunski
|
|
119 |
1833b0a0
|
Leszek Koltunski
|
y =-0.5f;
|
120 |
69ed1eb4
|
Leszek Koltunski
|
|
121 |
adb2661c
|
Leszek Koltunski
|
for(int row=0; row<mRows; row++)
|
122 |
|
|
{
|
123 |
1833b0a0
|
Leszek Koltunski
|
x =-0.5f;
|
124 |
69ed1eb4
|
Leszek Koltunski
|
|
125 |
adb2661c
|
Leszek Koltunski
|
for(int col=0; col<mCols; col++)
|
126 |
|
|
{
|
127 |
1833b0a0
|
Leszek Koltunski
|
currBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1);
|
128 |
adb2661c
|
Leszek Koltunski
|
|
129 |
1833b0a0
|
Leszek Koltunski
|
if( col==0 || (lastBlockIsNE^currBlockIsNE) )
|
130 |
adb2661c
|
Leszek Koltunski
|
{
|
131 |
e54bfada
|
Leszek Koltunski
|
if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs1,attribs2);
|
132 |
1833b0a0
|
Leszek Koltunski
|
vertex= addVertex( vertex, x, y+(currBlockIsNE?0:dy), attribs1,attribs2);
|
133 |
e54bfada
|
Leszek Koltunski
|
if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs1,attribs2);
|
134 |
1833b0a0
|
Leszek Koltunski
|
if( lastBlockIsNE^currBlockIsNE) vertex = repeatLast(vertex,attribs1,attribs2);
|
135 |
|
|
vertex= addVertex( vertex, x, y+(currBlockIsNE?dy:0), attribs1,attribs2);
|
136 |
adb2661c
|
Leszek Koltunski
|
}
|
137 |
1833b0a0
|
Leszek Koltunski
|
vertex= addVertex( vertex, x+dx, y+(currBlockIsNE?0:dy), attribs1,attribs2);
|
138 |
|
|
vertex= addVertex( vertex, x+dx, y+(currBlockIsNE?dy:0), attribs1,attribs2);
|
139 |
adb2661c
|
Leszek Koltunski
|
|
140 |
1833b0a0
|
Leszek Koltunski
|
lastBlockIsNE = currBlockIsNE;
|
141 |
|
|
x+=dx;
|
142 |
adb2661c
|
Leszek Koltunski
|
}
|
143 |
69ed1eb4
|
Leszek Koltunski
|
|
144 |
1833b0a0
|
Leszek Koltunski
|
y+=dy;
|
145 |
|
|
}
|
146 |
|
|
}
|
147 |
|
|
|
148 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
149 |
|
|
|
150 |
|
|
private void buildGrid(float[] attribs1, float[] attribs2, float[] xLoc, float[] yLoc)
|
151 |
|
|
{
|
152 |
|
|
boolean currBlockIsNE,lastBlockIsNE = false;
|
153 |
|
|
int vertex = 0;
|
154 |
|
|
float dx,dy,x,y= yLoc[0];
|
155 |
|
|
|
156 |
|
|
for(int row=0; row<mRows; row++)
|
157 |
|
|
{
|
158 |
|
|
x = xLoc[0];
|
159 |
|
|
dy= yLoc[row+1];
|
160 |
|
|
|
161 |
|
|
for(int col=0; col<mCols; col++)
|
162 |
|
|
{
|
163 |
|
|
dx = xLoc[col+1];
|
164 |
|
|
currBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1);
|
165 |
|
|
|
166 |
|
|
if( col==0 || (lastBlockIsNE^currBlockIsNE) )
|
167 |
|
|
{
|
168 |
|
|
if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs1,attribs2);
|
169 |
|
|
vertex= addVertex( vertex, x, y+(currBlockIsNE?0:dy), attribs1,attribs2);
|
170 |
|
|
if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs1,attribs2);
|
171 |
|
|
if( lastBlockIsNE^currBlockIsNE) vertex = repeatLast(vertex,attribs1,attribs2);
|
172 |
|
|
vertex= addVertex( vertex, x, y+(currBlockIsNE?dy:0), attribs1,attribs2);
|
173 |
|
|
}
|
174 |
|
|
vertex= addVertex( vertex, x+dx, y+(currBlockIsNE?0:dy), attribs1,attribs2);
|
175 |
|
|
vertex= addVertex( vertex, x+dx, y+(currBlockIsNE?dy:0), attribs1,attribs2);
|
176 |
|
|
|
177 |
|
|
lastBlockIsNE = currBlockIsNE;
|
178 |
|
|
x+=dx;
|
179 |
|
|
}
|
180 |
|
|
|
181 |
|
|
y+=dy;
|
182 |
adb2661c
|
Leszek Koltunski
|
}
|
183 |
|
|
}
|
184 |
|
|
|
185 |
79f172ab
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
186 |
a56bc359
|
Leszek Koltunski
|
// PUBLIC API
|
187 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
188 |
adb2661c
|
Leszek Koltunski
|
/**
|
189 |
1833b0a0
|
Leszek Koltunski
|
* Creates a rectangular grid of vertices, normals and texture coords.
|
190 |
adb2661c
|
Leszek Koltunski
|
*
|
191 |
|
|
* @param cols Number of columns in the grid.
|
192 |
|
|
* @param rows Number of rows in the grid.
|
193 |
|
|
*/
|
194 |
e54bfada
|
Leszek Koltunski
|
public MeshRectangles(int cols, int rows)
|
195 |
a51fe521
|
Leszek Koltunski
|
{
|
196 |
0f10a0b6
|
Leszek Koltunski
|
super();
|
197 |
a51fe521
|
Leszek Koltunski
|
computeNumberOfVertices(cols,rows);
|
198 |
adb2661c
|
Leszek Koltunski
|
|
199 |
e54bfada
|
Leszek Koltunski
|
float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
|
200 |
|
|
float[] attribs2= new float[VERT2_ATTRIBS*numVertices];
|
201 |
adb2661c
|
Leszek Koltunski
|
|
202 |
e54bfada
|
Leszek Koltunski
|
buildGrid(attribs1,attribs2);
|
203 |
adb2661c
|
Leszek Koltunski
|
|
204 |
a51fe521
|
Leszek Koltunski
|
if( remainingVert!=0 )
|
205 |
12e379d6
|
Leszek Koltunski
|
android.util.Log.d("MeshRectangles", "remainingVert " +remainingVert );
|
206 |
adb2661c
|
Leszek Koltunski
|
|
207 |
e54bfada
|
Leszek Koltunski
|
setAttribs(attribs1,attribs2);
|
208 |
a51fe521
|
Leszek Koltunski
|
}
|
209 |
cbd502ec
|
Leszek Koltunski
|
|
210 |
1833b0a0
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
211 |
|
|
/**
|
212 |
|
|
* Creates a rectangular grid of vertices, normals and texture coords.
|
213 |
|
|
*
|
214 |
|
|
* @param xLoc list of x-coordinates of vertices. First value: distance of the left edge from 0.
|
215 |
|
|
* Next values: distance of the next column from the previous. Must be NonNull!
|
216 |
|
|
* @param yLoc list of y-coordinates of vertices. First value: distance of the bottom edge from 0.
|
217 |
|
|
* Next values: distance of the next row from the previous. Must be NonNull!
|
218 |
|
|
*/
|
219 |
|
|
public MeshRectangles(float[] xLoc, float[] yLoc)
|
220 |
|
|
{
|
221 |
|
|
super();
|
222 |
|
|
computeNumberOfVertices(xLoc.length-1,yLoc.length-1);
|
223 |
|
|
|
224 |
|
|
float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
|
225 |
|
|
float[] attribs2= new float[VERT2_ATTRIBS*numVertices];
|
226 |
|
|
|
227 |
|
|
buildGrid(attribs1,attribs2,xLoc,yLoc);
|
228 |
|
|
|
229 |
|
|
if( remainingVert!=0 )
|
230 |
|
|
android.util.Log.d("MeshRectangles", "remainingVert " +remainingVert );
|
231 |
|
|
|
232 |
|
|
setAttribs(attribs1,attribs2);
|
233 |
|
|
}
|
234 |
|
|
|
235 |
cbd502ec
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
236 |
|
|
/**
|
237 |
4f81e0c8
|
Leszek Koltunski
|
* Copy constructor.
|
238 |
cbd502ec
|
Leszek Koltunski
|
*/
|
239 |
4f81e0c8
|
Leszek Koltunski
|
public MeshRectangles(MeshRectangles mesh, boolean deep)
|
240 |
e54bfada
|
Leszek Koltunski
|
{
|
241 |
4f81e0c8
|
Leszek Koltunski
|
super(mesh,deep);
|
242 |
e54bfada
|
Leszek Koltunski
|
}
|
243 |
cbd502ec
|
Leszek Koltunski
|
|
244 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
245 |
|
|
/**
|
246 |
4f81e0c8
|
Leszek Koltunski
|
* Copy the Mesh.
|
247 |
|
|
*
|
248 |
|
|
* @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
|
249 |
|
|
* normals and inflates (the rest, in particular the mVertAttribs2 containing texture
|
250 |
aee078f8
|
Leszek Koltunski
|
* coordinates and effect associations, is always deep copied)
|
251 |
cbd502ec
|
Leszek Koltunski
|
*/
|
252 |
4f81e0c8
|
Leszek Koltunski
|
public MeshRectangles copy(boolean deep)
|
253 |
e54bfada
|
Leszek Koltunski
|
{
|
254 |
4f81e0c8
|
Leszek Koltunski
|
return new MeshRectangles(this,deep);
|
255 |
e54bfada
|
Leszek Koltunski
|
}
|
256 |
a51fe521
|
Leszek Koltunski
|
}
|