1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2020 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
|
* Create a Mesh which approximates a triangle with vertices at (-0.5,-0.5),(+0.5,-0.5),(0.0,0.5)
|
26
|
*/
|
27
|
public class MeshTriangle extends MeshBase
|
28
|
{
|
29
|
private int numVertices;
|
30
|
private int remainingVert;
|
31
|
|
32
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
33
|
|
34
|
private int addVertex(int vertex, float x, float y, float[] attribs1, float[] attribs2)
|
35
|
{
|
36
|
remainingVert--;
|
37
|
|
38
|
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB ] = x-0.5f;
|
39
|
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] = y-0.5f;
|
40
|
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = 0.0f;
|
41
|
|
42
|
attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB ] = 0.0f;
|
43
|
attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+1] = 0.0f;
|
44
|
attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+2] = 1.0f;
|
45
|
|
46
|
attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB ] = x;
|
47
|
attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = y;
|
48
|
|
49
|
return vertex+1;
|
50
|
}
|
51
|
|
52
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
53
|
|
54
|
private void computeNumberOfVertices(int level)
|
55
|
{
|
56
|
numVertices = level*(level+2);
|
57
|
remainingVert = numVertices;
|
58
|
}
|
59
|
|
60
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
61
|
|
62
|
private int buildRow(int vertex,float sx, float sy, int length, float dx, float dy, float[] attribs1, float[] attribs2)
|
63
|
{
|
64
|
for(int i=0; i<length; i++)
|
65
|
{
|
66
|
vertex = addVertex(vertex, sx , sy , attribs1, attribs2);
|
67
|
vertex = addVertex(vertex, sx+dx, sy+dy, attribs1, attribs2);
|
68
|
sx += 2*dx;
|
69
|
}
|
70
|
|
71
|
vertex = addVertex(vertex, sx, sy, attribs1, attribs2);
|
72
|
|
73
|
return vertex;
|
74
|
}
|
75
|
|
76
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
77
|
|
78
|
private void buildGrid(float[] attribs1, float[] attribs2, int level)
|
79
|
{
|
80
|
float sx = 0.0f;
|
81
|
float sy = 0.0f;
|
82
|
float dx = 0.5f/level;
|
83
|
float dy = 1.0f/level;
|
84
|
int vertex = 0;
|
85
|
|
86
|
for(int row=level; row>=1; row--)
|
87
|
{
|
88
|
vertex = buildRow(vertex,sx,sy,row,dx,dy,attribs1,attribs2);
|
89
|
|
90
|
sx += 2*dx*(row-0.5f);
|
91
|
sy += dy;
|
92
|
dx *= -1;
|
93
|
}
|
94
|
}
|
95
|
|
96
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
97
|
// PUBLIC API
|
98
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
99
|
/**
|
100
|
* Creates the underlying grid of vertices with the usual attribs which approximates a triangle
|
101
|
* with vertices at (-0.5,-0.5),(+0.5,-0.5),(0.0,0.5)
|
102
|
*
|
103
|
* @param level Specifies the level of slicing. Valid values: level ≥ 1
|
104
|
*/
|
105
|
public MeshTriangle(int level)
|
106
|
{
|
107
|
super();
|
108
|
|
109
|
computeNumberOfVertices(level);
|
110
|
|
111
|
float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
|
112
|
float[] attribs2= new float[VERT2_ATTRIBS*numVertices];
|
113
|
|
114
|
buildGrid(attribs1, attribs2, level);
|
115
|
|
116
|
if( remainingVert!=0 )
|
117
|
android.util.Log.d("MeshTriangles", "remainingVert " +remainingVert );
|
118
|
|
119
|
setAttribs(attribs1, attribs2);
|
120
|
}
|
121
|
|
122
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
123
|
/**
|
124
|
* Copy constructor.
|
125
|
*/
|
126
|
public MeshTriangle(MeshTriangle mesh, boolean deep)
|
127
|
{
|
128
|
super(mesh,deep);
|
129
|
}
|
130
|
|
131
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
132
|
/**
|
133
|
* Copy the Mesh.
|
134
|
*
|
135
|
* @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
|
136
|
* normals and inflates (the rest, in particular the mVertAttribs2 containing texture
|
137
|
* coordinates and effect associations, is always deep copied)
|
138
|
*/
|
139
|
public MeshTriangle copy(boolean deep)
|
140
|
{
|
141
|
return new MeshTriangle(this,deep);
|
142
|
}
|
143
|
}
|