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