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
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
23
|
/**
|
24
|
* Create a flat, rectangular grid.
|
25
|
* <p>
|
26
|
* Perfect if you just want to display a flat Texture. If you are not planning to apply any VERTEX
|
27
|
* effects to it, use MeshFlat(1,1), i.e. a Quad. Otherwise, create more vertices for more realistic effects!
|
28
|
*/
|
29
|
public class MeshFlat extends MeshBase
|
30
|
{
|
31
|
private int mCols, mRows;
|
32
|
private int remainingVert;
|
33
|
private int numVertices;
|
34
|
|
35
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
36
|
// Create a flat, full grid.
|
37
|
|
38
|
private void computeNumberOfVertices(int cols, int rows)
|
39
|
{
|
40
|
mRows=rows;
|
41
|
mCols=cols;
|
42
|
|
43
|
if( cols==1 && rows==1 )
|
44
|
{
|
45
|
numVertices = 4;
|
46
|
}
|
47
|
else
|
48
|
{
|
49
|
numVertices = 2*( mRows*mCols +2*mRows - 1) +2*(mCols>=2 ? mRows:0) +
|
50
|
(mCols>=2 && mRows>=2 ? 2*mRows-2 : 1);
|
51
|
}
|
52
|
|
53
|
//android.util.Log.e("MeshFlat","vertices="+numVertices+" rows="+mRows+" cols="+mCols);
|
54
|
|
55
|
remainingVert = numVertices;
|
56
|
}
|
57
|
|
58
|
|
59
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
60
|
|
61
|
private int addVertex(int vertex, float x, float y, float[] attribs)
|
62
|
{
|
63
|
remainingVert--;
|
64
|
|
65
|
attribs[8*vertex ] = x-0.5f;
|
66
|
attribs[8*vertex+1] = 0.5f-y;
|
67
|
attribs[8*vertex+2] = 0;
|
68
|
|
69
|
attribs[8*vertex+3] = 0.0f;
|
70
|
attribs[8*vertex+4] = 0.0f;
|
71
|
attribs[8*vertex+5] = 1.0f;
|
72
|
|
73
|
attribs[8*vertex+6] = x;
|
74
|
attribs[8*vertex+7] = 1.0f-y;
|
75
|
|
76
|
return vertex+1;
|
77
|
}
|
78
|
|
79
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
80
|
|
81
|
private int repeatLast(int vertex, float[] attribs)
|
82
|
{
|
83
|
remainingVert--;
|
84
|
|
85
|
//android.util.Log.e("MeshFlat", "repeating last vertex!");
|
86
|
|
87
|
if( vertex>0 )
|
88
|
{
|
89
|
attribs[8*vertex ] = attribs[8*vertex-8];
|
90
|
attribs[8*vertex+1] = attribs[8*vertex-7];
|
91
|
attribs[8*vertex+2] = attribs[8*vertex-6];
|
92
|
attribs[8*vertex+3] = attribs[8*vertex-5];
|
93
|
attribs[8*vertex+4] = attribs[8*vertex-4];
|
94
|
attribs[8*vertex+5] = attribs[8*vertex-3];
|
95
|
attribs[8*vertex+6] = attribs[8*vertex-2];
|
96
|
attribs[8*vertex+7] = attribs[8*vertex-1];
|
97
|
|
98
|
vertex++;
|
99
|
}
|
100
|
|
101
|
return vertex;
|
102
|
}
|
103
|
|
104
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
105
|
|
106
|
private void buildGrid(float[] attribs)
|
107
|
{
|
108
|
boolean lastBlockIsNE = false;
|
109
|
boolean currentBlockIsNE;
|
110
|
int vertex = 0;
|
111
|
|
112
|
float x,y;
|
113
|
final float X = 1.0f/mCols;
|
114
|
final float Y = 1.0f/mRows;
|
115
|
|
116
|
//android.util.Log.d("MeshFlat", "buildGrid");
|
117
|
|
118
|
y = 0.0f;
|
119
|
|
120
|
for(int row=0; row<mRows; row++)
|
121
|
{
|
122
|
x = 0.0f;
|
123
|
|
124
|
for(int col=0; col<mCols; col++)
|
125
|
{
|
126
|
currentBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1);
|
127
|
|
128
|
if( col==0 || (lastBlockIsNE^currentBlockIsNE) )
|
129
|
{
|
130
|
if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs);
|
131
|
vertex= addVertex( vertex, x, y+(currentBlockIsNE?0:Y), attribs);
|
132
|
if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs);
|
133
|
if( lastBlockIsNE^currentBlockIsNE) vertex = repeatLast(vertex,attribs);
|
134
|
vertex= addVertex( vertex, x, y+(currentBlockIsNE?Y:0), attribs);
|
135
|
}
|
136
|
vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?0:Y), attribs);
|
137
|
vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?Y:0), attribs);
|
138
|
|
139
|
lastBlockIsNE = currentBlockIsNE;
|
140
|
x+=X;
|
141
|
}
|
142
|
|
143
|
y+=Y;
|
144
|
}
|
145
|
|
146
|
//android.util.Log.d("MeshFlat", "buildGrid done");
|
147
|
}
|
148
|
|
149
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
150
|
/*
|
151
|
private static String debug(float[] val, int stop)
|
152
|
{
|
153
|
String ret="";
|
154
|
|
155
|
for(int i=0; i<val.length; i++)
|
156
|
{
|
157
|
if( i%stop==0 ) ret+="\n";
|
158
|
ret+=(" "+val[i]);
|
159
|
}
|
160
|
|
161
|
return ret;
|
162
|
}
|
163
|
*/
|
164
|
|
165
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
166
|
// PUBLIC API
|
167
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
168
|
/**
|
169
|
* Creates the underlying grid of vertices, normals and texture coords.
|
170
|
*
|
171
|
* @param cols Number of columns in the grid.
|
172
|
* @param rows Number of rows in the grid.
|
173
|
*/
|
174
|
public MeshFlat(int cols, int rows)
|
175
|
{
|
176
|
super(0.0f);
|
177
|
computeNumberOfVertices(cols,rows);
|
178
|
|
179
|
float[] attribs= new float[(POS_DATA_SIZE+NOR_DATA_SIZE+TEX_DATA_SIZE)*numVertices];
|
180
|
|
181
|
buildGrid(attribs);
|
182
|
|
183
|
//android.util.Log.e("MeshFlat", "dataLen="+numVertices);
|
184
|
//android.util.Log.d("MeshFlat", "attribs: "+debug(attribs,8) );
|
185
|
|
186
|
if( remainingVert!=0 )
|
187
|
android.util.Log.d("MeshFlat", "remainingVert " +remainingVert );
|
188
|
|
189
|
setData(numVertices, attribs);
|
190
|
}
|
191
|
}
|