1 |
9099e567
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
c4d06f90
|
Leszek Koltunski
|
// Copyright 2020 Leszek Koltunski leszek@koltunski.pl //
|
3 |
9099e567
|
Leszek Koltunski
|
// //
|
4 |
|
|
// This file is part of Distorted. //
|
5 |
|
|
// //
|
6 |
c4d06f90
|
Leszek Koltunski
|
// 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 |
9099e567
|
Leszek Koltunski
|
// //
|
11 |
c4d06f90
|
Leszek Koltunski
|
// This library is distributed in the hope that it will be useful, //
|
12 |
9099e567
|
Leszek Koltunski
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13 |
c4d06f90
|
Leszek Koltunski
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU //
|
14 |
|
|
// Lesser General Public License for more details. //
|
15 |
9099e567
|
Leszek Koltunski
|
// //
|
16 |
c4d06f90
|
Leszek Koltunski
|
// 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 |
9099e567
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
20 |
|
|
|
21 |
|
|
package org.distorted.library.mesh;
|
22 |
|
|
|
23 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
24 |
8c57d77b
|
Leszek Koltunski
|
|
25 |
|
|
import org.distorted.library.main.DistortedLibrary;
|
26 |
|
|
|
27 |
b17aa4aa
|
Leszek Koltunski
|
/**
|
28 |
45b08c37
|
Leszek Koltunski
|
* Create a Mesh which approximates a triangle with vertices at (-0.5,-0.5),(+0.5,-0.5),(0.0,0.5)
|
29 |
b17aa4aa
|
Leszek Koltunski
|
*/
|
30 |
b948df7a
|
Leszek Koltunski
|
public class MeshTriangle extends MeshBase
|
31 |
9099e567
|
Leszek Koltunski
|
{
|
32 |
|
|
private int numVertices;
|
33 |
|
|
private int remainingVert;
|
34 |
|
|
|
35 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
36 |
|
|
|
37 |
e54bfada
|
Leszek Koltunski
|
private int addVertex(int vertex, float x, float y, float[] attribs1, float[] attribs2)
|
38 |
9099e567
|
Leszek Koltunski
|
{
|
39 |
|
|
remainingVert--;
|
40 |
|
|
|
41 |
e54bfada
|
Leszek Koltunski
|
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB ] = x-0.5f;
|
42 |
|
|
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] = y-0.5f;
|
43 |
|
|
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = 0.0f;
|
44 |
9099e567
|
Leszek Koltunski
|
|
45 |
e54bfada
|
Leszek Koltunski
|
attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB ] = 0.0f;
|
46 |
|
|
attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+1] = 0.0f;
|
47 |
|
|
attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+2] = 1.0f;
|
48 |
9099e567
|
Leszek Koltunski
|
|
49 |
e54bfada
|
Leszek Koltunski
|
attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB ] = x;
|
50 |
|
|
attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = y;
|
51 |
9099e567
|
Leszek Koltunski
|
|
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 |
e54bfada
|
Leszek Koltunski
|
private int buildRow(int vertex,float sx, float sy, int length, float dx, float dy, float[] attribs1, float[] attribs2)
|
66 |
9099e567
|
Leszek Koltunski
|
{
|
67 |
|
|
for(int i=0; i<length; i++)
|
68 |
|
|
{
|
69 |
e54bfada
|
Leszek Koltunski
|
vertex = addVertex(vertex, sx , sy , attribs1, attribs2);
|
70 |
|
|
vertex = addVertex(vertex, sx+dx, sy+dy, attribs1, attribs2);
|
71 |
9099e567
|
Leszek Koltunski
|
sx += 2*dx;
|
72 |
|
|
}
|
73 |
|
|
|
74 |
e54bfada
|
Leszek Koltunski
|
vertex = addVertex(vertex, sx, sy, attribs1, attribs2);
|
75 |
9099e567
|
Leszek Koltunski
|
|
76 |
|
|
return vertex;
|
77 |
|
|
}
|
78 |
|
|
|
79 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
80 |
|
|
|
81 |
e54bfada
|
Leszek Koltunski
|
private void buildGrid(float[] attribs1, float[] attribs2, int level)
|
82 |
9099e567
|
Leszek Koltunski
|
{
|
83 |
|
|
float sx = 0.0f;
|
84 |
|
|
float sy = 0.0f;
|
85 |
|
|
float dx = 0.5f/level;
|
86 |
b35570ad
|
Leszek Koltunski
|
float dy = 1.0f/level;
|
87 |
9099e567
|
Leszek Koltunski
|
int vertex = 0;
|
88 |
|
|
|
89 |
|
|
for(int row=level; row>=1; row--)
|
90 |
|
|
{
|
91 |
e54bfada
|
Leszek Koltunski
|
vertex = buildRow(vertex,sx,sy,row,dx,dy,attribs1,attribs2);
|
92 |
9099e567
|
Leszek Koltunski
|
|
93 |
|
|
sx += 2*dx*(row-0.5f);
|
94 |
|
|
sy += dy;
|
95 |
|
|
dx *= -1;
|
96 |
|
|
}
|
97 |
|
|
}
|
98 |
|
|
|
99 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
100 |
|
|
// PUBLIC API
|
101 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
102 |
|
|
/**
|
103 |
45b08c37
|
Leszek Koltunski
|
* Creates the underlying grid of vertices with the usual attribs which approximates a triangle
|
104 |
|
|
* with vertices at (-0.5,-0.5),(+0.5,-0.5),(0.0,0.5)
|
105 |
9099e567
|
Leszek Koltunski
|
*
|
106 |
|
|
* @param level Specifies the level of slicing. Valid values: level ≥ 1
|
107 |
|
|
*/
|
108 |
b948df7a
|
Leszek Koltunski
|
public MeshTriangle(int level)
|
109 |
9099e567
|
Leszek Koltunski
|
{
|
110 |
0f10a0b6
|
Leszek Koltunski
|
super();
|
111 |
9099e567
|
Leszek Koltunski
|
|
112 |
|
|
computeNumberOfVertices(level);
|
113 |
|
|
|
114 |
e54bfada
|
Leszek Koltunski
|
float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
|
115 |
|
|
float[] attribs2= new float[VERT2_ATTRIBS*numVertices];
|
116 |
|
|
|
117 |
|
|
buildGrid(attribs1, attribs2, level);
|
118 |
9099e567
|
Leszek Koltunski
|
|
119 |
|
|
if( remainingVert!=0 )
|
120 |
8c57d77b
|
Leszek Koltunski
|
DistortedLibrary.logMessage("MeshTriangle: remainingVert " +remainingVert );
|
121 |
9099e567
|
Leszek Koltunski
|
|
122 |
e54bfada
|
Leszek Koltunski
|
setAttribs(attribs1, attribs2);
|
123 |
9099e567
|
Leszek Koltunski
|
}
|
124 |
cbd502ec
|
Leszek Koltunski
|
|
125 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
126 |
|
|
/**
|
127 |
4f81e0c8
|
Leszek Koltunski
|
* Copy constructor.
|
128 |
cbd502ec
|
Leszek Koltunski
|
*/
|
129 |
b948df7a
|
Leszek Koltunski
|
public MeshTriangle(MeshTriangle mesh, boolean deep)
|
130 |
cbd502ec
|
Leszek Koltunski
|
{
|
131 |
4f81e0c8
|
Leszek Koltunski
|
super(mesh,deep);
|
132 |
cbd502ec
|
Leszek Koltunski
|
}
|
133 |
|
|
|
134 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
135 |
|
|
/**
|
136 |
4f81e0c8
|
Leszek Koltunski
|
* 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 |
aee078f8
|
Leszek Koltunski
|
* coordinates and effect associations, is always deep copied)
|
141 |
cbd502ec
|
Leszek Koltunski
|
*/
|
142 |
b948df7a
|
Leszek Koltunski
|
public MeshTriangle copy(boolean deep)
|
143 |
cbd502ec
|
Leszek Koltunski
|
{
|
144 |
b948df7a
|
Leszek Koltunski
|
return new MeshTriangle(this,deep);
|
145 |
cbd502ec
|
Leszek Koltunski
|
}
|
146 |
9099e567
|
Leszek Koltunski
|
}
|