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 MeshRectangles(1,1), i.e. a Quad. Otherwise, create more vertices for more realistic effects!
|
28
|
*/
|
29
|
public class MeshRectangles 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
|
remainingVert = numVertices;
|
54
|
}
|
55
|
|
56
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
57
|
|
58
|
private int addVertex(int vertex, float x, float y, float[] attribs)
|
59
|
{
|
60
|
remainingVert--;
|
61
|
|
62
|
attribs[VERT_ATTRIBS*vertex + POS_ATTRIB ] = x-0.5f;
|
63
|
attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+1] = 0.5f-y;
|
64
|
attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+2] = 0.0f;
|
65
|
|
66
|
attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB ] = 0.0f;
|
67
|
attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+1] = 0.0f;
|
68
|
attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+2] = 1.0f;
|
69
|
|
70
|
attribs[VERT_ATTRIBS*vertex + INF_ATTRIB ] = (x-0.5f);
|
71
|
attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+1] = (0.5f-y);
|
72
|
attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+2] = 0.01f ; // Inflated surface needs to be slightly in front
|
73
|
|
74
|
attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB ] = x;
|
75
|
attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB+1] = 1.0f-y;
|
76
|
|
77
|
return vertex+1;
|
78
|
}
|
79
|
|
80
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
81
|
|
82
|
private int repeatLast(int vertex, float[] attribs)
|
83
|
{
|
84
|
if( vertex>0 )
|
85
|
{
|
86
|
remainingVert--;
|
87
|
|
88
|
attribs[VERT_ATTRIBS*vertex + POS_ATTRIB ] = attribs[VERT_ATTRIBS*(vertex-1) + POS_ATTRIB ];
|
89
|
attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+1] = attribs[VERT_ATTRIBS*(vertex-1) + POS_ATTRIB+1];
|
90
|
attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+2] = attribs[VERT_ATTRIBS*(vertex-1) + POS_ATTRIB+2];
|
91
|
|
92
|
attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB ] = attribs[VERT_ATTRIBS*(vertex-1) + NOR_ATTRIB ];
|
93
|
attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+1] = attribs[VERT_ATTRIBS*(vertex-1) + NOR_ATTRIB+1];
|
94
|
attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+2] = attribs[VERT_ATTRIBS*(vertex-1) + NOR_ATTRIB+2];
|
95
|
|
96
|
attribs[VERT_ATTRIBS*vertex + INF_ATTRIB ] = attribs[VERT_ATTRIBS*(vertex-1) + INF_ATTRIB ];
|
97
|
attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+1] = attribs[VERT_ATTRIBS*(vertex-1) + INF_ATTRIB+1];
|
98
|
attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+2] = attribs[VERT_ATTRIBS*(vertex-1) + INF_ATTRIB+2];
|
99
|
|
100
|
attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB ] = attribs[VERT_ATTRIBS*(vertex-1) + TEX_ATTRIB ];
|
101
|
attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB+1] = attribs[VERT_ATTRIBS*(vertex-1) + TEX_ATTRIB+1];
|
102
|
|
103
|
vertex++;
|
104
|
}
|
105
|
|
106
|
return vertex;
|
107
|
}
|
108
|
|
109
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
110
|
|
111
|
private void buildGrid(float[] attribs)
|
112
|
{
|
113
|
boolean lastBlockIsNE = false;
|
114
|
boolean currentBlockIsNE;
|
115
|
int vertex = 0;
|
116
|
|
117
|
float x,y;
|
118
|
final float X = 1.0f/mCols;
|
119
|
final float Y = 1.0f/mRows;
|
120
|
|
121
|
y = 0.0f;
|
122
|
|
123
|
for(int row=0; row<mRows; row++)
|
124
|
{
|
125
|
x = 0.0f;
|
126
|
|
127
|
for(int col=0; col<mCols; col++)
|
128
|
{
|
129
|
currentBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1);
|
130
|
|
131
|
if( col==0 || (lastBlockIsNE^currentBlockIsNE) )
|
132
|
{
|
133
|
if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs);
|
134
|
vertex= addVertex( vertex, x, y+(currentBlockIsNE?0:Y), attribs);
|
135
|
if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs);
|
136
|
if( lastBlockIsNE^currentBlockIsNE) vertex = repeatLast(vertex,attribs);
|
137
|
vertex= addVertex( vertex, x, y+(currentBlockIsNE?Y:0), attribs);
|
138
|
}
|
139
|
vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?0:Y), attribs);
|
140
|
vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?Y:0), attribs);
|
141
|
|
142
|
lastBlockIsNE = currentBlockIsNE;
|
143
|
x+=X;
|
144
|
}
|
145
|
|
146
|
y+=Y;
|
147
|
}
|
148
|
}
|
149
|
|
150
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
151
|
// PUBLIC API
|
152
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
153
|
/**
|
154
|
* Creates the underlying grid of vertices, normals and texture coords.
|
155
|
*
|
156
|
* @param cols Number of columns in the grid.
|
157
|
* @param rows Number of rows in the grid.
|
158
|
*/
|
159
|
public MeshRectangles(int cols, int rows)
|
160
|
{
|
161
|
super(1,1,0);
|
162
|
computeNumberOfVertices(cols,rows);
|
163
|
|
164
|
float[] attribs= new float[VERT_ATTRIBS*numVertices];
|
165
|
|
166
|
buildGrid(attribs);
|
167
|
|
168
|
if( remainingVert!=0 )
|
169
|
android.util.Log.d("MeshRectangles", "remainingVert " +remainingVert );
|
170
|
|
171
|
setAttribs(attribs);
|
172
|
}
|
173
|
}
|