Project

General

Profile

« Previous | Next » 

Revision b948df7a

Added by Leszek Koltunski over 3 years ago

Rename Meshes.

View differences:

src/main/java/org/distorted/library/mesh/MeshRectangles.java
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[] attribs1, float[] attribs2)
59
     {
60
     remainingVert--;
61

  
62
     attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB  ] =    x;
63
     attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] =   -y;
64
     attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = 0.0f;
65

  
66
     attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB  ] = 0.0f;
67
     attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+1] = 0.0f;
68
     attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+2] = 1.0f;
69

  
70
     attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB  ] = x+0.5f;
71
     attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = 0.5f-y;
72

  
73
     return vertex+1;
74
     }
75

  
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

  
78
  private int repeatLast(int vertex, float[] attribs1, float[] attribs2)
79
     {
80
     if( vertex>0 )
81
       {
82
       remainingVert--;
83

  
84
       attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB  ] = attribs1[VERT1_ATTRIBS*(vertex-1) + POS_ATTRIB  ];
85
       attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] = attribs1[VERT1_ATTRIBS*(vertex-1) + POS_ATTRIB+1];
86
       attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = attribs1[VERT1_ATTRIBS*(vertex-1) + POS_ATTRIB+2];
87

  
88
       attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB  ] = attribs1[VERT1_ATTRIBS*(vertex-1) + NOR_ATTRIB  ];
89
       attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+1] = attribs1[VERT1_ATTRIBS*(vertex-1) + NOR_ATTRIB+1];
90
       attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+2] = attribs1[VERT1_ATTRIBS*(vertex-1) + NOR_ATTRIB+2];
91

  
92
       attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB  ] = attribs2[VERT2_ATTRIBS*(vertex-1) + TEX_ATTRIB  ];
93
       attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = attribs2[VERT2_ATTRIBS*(vertex-1) + TEX_ATTRIB+1];
94

  
95
       vertex++;
96
       }
97

  
98
     return vertex;
99
     }
100

  
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

  
103
  private void buildGrid(float[] attribs1, float[] attribs2)
104
     {
105
     boolean currBlockIsNE, lastBlockIsNE = false;
106
     int vertex = 0;
107
     float x,y;
108
     final float dx = 1.0f/mCols;
109
     final float dy = 1.0f/mRows;
110

  
111
     y =-0.5f;
112

  
113
     for(int row=0; row<mRows; row++)
114
       {
115
       x =-0.5f;
116

  
117
       for(int col=0; col<mCols; col++)
118
         {
119
         currBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1);
120

  
121
         if( col==0 || (lastBlockIsNE^currBlockIsNE) )
122
           {
123
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs1,attribs2);
124
           vertex= addVertex( vertex, x, y+(currBlockIsNE?0:dy), attribs1,attribs2);
125
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs1,attribs2);
126
           if( lastBlockIsNE^currBlockIsNE)  vertex = repeatLast(vertex,attribs1,attribs2);
127
           vertex= addVertex( vertex, x, y+(currBlockIsNE?dy:0), attribs1,attribs2);
128
           }
129
         vertex= addVertex( vertex, x+dx, y+(currBlockIsNE?0:dy), attribs1,attribs2);
130
         vertex= addVertex( vertex, x+dx, y+(currBlockIsNE?dy:0), attribs1,attribs2);
131

  
132
         lastBlockIsNE = currBlockIsNE;
133
         x+=dx;
134
         }
135

  
136
       y+=dy;
137
       }
138
     }
139

  
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

  
142
  private void buildGrid(float[] attribs1, float[] attribs2, float[] xLoc, float[] yLoc)
143
     {
144
     boolean currBlockIsNE,lastBlockIsNE = false;
145
     int vertex = 0;
146
     float dx,dy,x,y= yLoc[0];
147

  
148
     for(int row=0; row<mRows; row++)
149
       {
150
       x = xLoc[0];
151
       dy= yLoc[row+1];
152

  
153
       for(int col=0; col<mCols; col++)
154
         {
155
         dx = xLoc[col+1];
156
         currBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1);
157

  
158
         if( col==0 || (lastBlockIsNE^currBlockIsNE) )
159
           {
160
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs1,attribs2);
161
           vertex= addVertex( vertex, x, y+(currBlockIsNE?0:dy), attribs1,attribs2);
162
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs1,attribs2);
163
           if( lastBlockIsNE^currBlockIsNE)  vertex = repeatLast(vertex,attribs1,attribs2);
164
           vertex= addVertex( vertex, x, y+(currBlockIsNE?dy:0), attribs1,attribs2);
165
           }
166
         vertex= addVertex( vertex, x+dx, y+(currBlockIsNE?0:dy), attribs1,attribs2);
167
         vertex= addVertex( vertex, x+dx, y+(currBlockIsNE?dy:0), attribs1,attribs2);
168

  
169
         lastBlockIsNE = currBlockIsNE;
170
         x+=dx;
171
         }
172

  
173
       y+=dy;
174
       }
175
     }
176

  
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178
// PUBLIC API
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180
/**
181
 * Creates a rectangular grid of vertices, normals and texture coords.
182
 *
183
 * @param cols Number of columns in the grid.
184
 * @param rows Number of rows in the grid.
185
 */
186
  public MeshRectangles(int cols, int rows)
187
    {
188
    super();
189
    computeNumberOfVertices(cols,rows);
190

  
191
    float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
192
    float[] attribs2= new float[VERT2_ATTRIBS*numVertices];
193

  
194
    buildGrid(attribs1,attribs2);
195

  
196
    if( remainingVert!=0 )
197
      android.util.Log.d("MeshRectangles", "remainingVert " +remainingVert );
198

  
199
    setAttribs(attribs1,attribs2);
200
    }
201

  
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203
/**
204
 * Creates a rectangular grid of vertices, normals and texture coords.
205
 *
206
 * @param xLoc list of x-coordinates of vertices. First value: distance of the left edge from 0.
207
 *             Next values: distance of the next column from the previous. Must be NonNull!
208
 * @param yLoc list of y-coordinates of vertices. First value: distance of the bottom edge from 0.
209
 *             Next values: distance of the next row from the previous. Must be NonNull!
210
 */
211
  public MeshRectangles(float[] xLoc, float[] yLoc)
212
    {
213
    super();
214
    computeNumberOfVertices(xLoc.length-1,yLoc.length-1);
215

  
216
    float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
217
    float[] attribs2= new float[VERT2_ATTRIBS*numVertices];
218

  
219
    buildGrid(attribs1,attribs2,xLoc,yLoc);
220

  
221
    if( remainingVert!=0 )
222
      android.util.Log.d("MeshRectangles", "remainingVert " +remainingVert );
223

  
224
    setAttribs(attribs1,attribs2);
225
    }
226

  
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228
/**
229
 * Copy constructor.
230
 */
231
  public MeshRectangles(MeshRectangles mesh, boolean deep)
232
    {
233
    super(mesh,deep);
234
    }
235

  
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237
/**
238
 * Copy the Mesh.
239
 *
240
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
241
 *             normals and inflates (the rest, in particular the mVertAttribs2 containing texture
242
 *             coordinates and effect associations, is always deep copied)
243
 */
244
  public MeshRectangles copy(boolean deep)
245
    {
246
    return new MeshRectangles(this,deep);
247
    }
248
 }
src/main/java/org/distorted/library/mesh/MeshSquare.java
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 MeshSquare 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[] attribs1, float[] attribs2)
59
     {
60
     remainingVert--;
61

  
62
     attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB  ] =    x;
63
     attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] =   -y;
64
     attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = 0.0f;
65

  
66
     attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB  ] = 0.0f;
67
     attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+1] = 0.0f;
68
     attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+2] = 1.0f;
69

  
70
     attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB  ] = x+0.5f;
71
     attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = 0.5f-y;
72

  
73
     return vertex+1;
74
     }
75

  
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

  
78
  private int repeatLast(int vertex, float[] attribs1, float[] attribs2)
79
     {
80
     if( vertex>0 )
81
       {
82
       remainingVert--;
83

  
84
       attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB  ] = attribs1[VERT1_ATTRIBS*(vertex-1) + POS_ATTRIB  ];
85
       attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] = attribs1[VERT1_ATTRIBS*(vertex-1) + POS_ATTRIB+1];
86
       attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = attribs1[VERT1_ATTRIBS*(vertex-1) + POS_ATTRIB+2];
87

  
88
       attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB  ] = attribs1[VERT1_ATTRIBS*(vertex-1) + NOR_ATTRIB  ];
89
       attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+1] = attribs1[VERT1_ATTRIBS*(vertex-1) + NOR_ATTRIB+1];
90
       attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+2] = attribs1[VERT1_ATTRIBS*(vertex-1) + NOR_ATTRIB+2];
91

  
92
       attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB  ] = attribs2[VERT2_ATTRIBS*(vertex-1) + TEX_ATTRIB  ];
93
       attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = attribs2[VERT2_ATTRIBS*(vertex-1) + TEX_ATTRIB+1];
94

  
95
       vertex++;
96
       }
97

  
98
     return vertex;
99
     }
100

  
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

  
103
  private void buildGrid(float[] attribs1, float[] attribs2)
104
     {
105
     boolean currBlockIsNE, lastBlockIsNE = false;
106
     int vertex = 0;
107
     float x,y;
108
     final float dx = 1.0f/mCols;
109
     final float dy = 1.0f/mRows;
110

  
111
     y =-0.5f;
112

  
113
     for(int row=0; row<mRows; row++)
114
       {
115
       x =-0.5f;
116

  
117
       for(int col=0; col<mCols; col++)
118
         {
119
         currBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1);
120

  
121
         if( col==0 || (lastBlockIsNE^currBlockIsNE) )
122
           {
123
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs1,attribs2);
124
           vertex= addVertex( vertex, x, y+(currBlockIsNE?0:dy), attribs1,attribs2);
125
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs1,attribs2);
126
           if( lastBlockIsNE^currBlockIsNE)  vertex = repeatLast(vertex,attribs1,attribs2);
127
           vertex= addVertex( vertex, x, y+(currBlockIsNE?dy:0), attribs1,attribs2);
128
           }
129
         vertex= addVertex( vertex, x+dx, y+(currBlockIsNE?0:dy), attribs1,attribs2);
130
         vertex= addVertex( vertex, x+dx, y+(currBlockIsNE?dy:0), attribs1,attribs2);
131

  
132
         lastBlockIsNE = currBlockIsNE;
133
         x+=dx;
134
         }
135

  
136
       y+=dy;
137
       }
138
     }
139

  
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

  
142
  private void buildGrid(float[] attribs1, float[] attribs2, float[] xLoc, float[] yLoc)
143
     {
144
     boolean currBlockIsNE,lastBlockIsNE = false;
145
     int vertex = 0;
146
     float dx,dy,x,y= yLoc[0];
147

  
148
     for(int row=0; row<mRows; row++)
149
       {
150
       x = xLoc[0];
151
       dy= yLoc[row+1];
152

  
153
       for(int col=0; col<mCols; col++)
154
         {
155
         dx = xLoc[col+1];
156
         currBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1);
157

  
158
         if( col==0 || (lastBlockIsNE^currBlockIsNE) )
159
           {
160
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs1,attribs2);
161
           vertex= addVertex( vertex, x, y+(currBlockIsNE?0:dy), attribs1,attribs2);
162
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs1,attribs2);
163
           if( lastBlockIsNE^currBlockIsNE)  vertex = repeatLast(vertex,attribs1,attribs2);
164
           vertex= addVertex( vertex, x, y+(currBlockIsNE?dy:0), attribs1,attribs2);
165
           }
166
         vertex= addVertex( vertex, x+dx, y+(currBlockIsNE?0:dy), attribs1,attribs2);
167
         vertex= addVertex( vertex, x+dx, y+(currBlockIsNE?dy:0), attribs1,attribs2);
168

  
169
         lastBlockIsNE = currBlockIsNE;
170
         x+=dx;
171
         }
172

  
173
       y+=dy;
174
       }
175
     }
176

  
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178
// PUBLIC API
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180
/**
181
 * Creates a rectangular grid of vertices, normals and texture coords.
182
 *
183
 * @param cols Number of columns in the grid.
184
 * @param rows Number of rows in the grid.
185
 */
186
  public MeshSquare(int cols, int rows)
187
    {
188
    super();
189
    computeNumberOfVertices(cols,rows);
190

  
191
    float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
192
    float[] attribs2= new float[VERT2_ATTRIBS*numVertices];
193

  
194
    buildGrid(attribs1,attribs2);
195

  
196
    if( remainingVert!=0 )
197
      android.util.Log.d("MeshRectangles", "remainingVert " +remainingVert );
198

  
199
    setAttribs(attribs1,attribs2);
200
    }
201

  
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203
/**
204
 * Creates a rectangular grid of vertices, normals and texture coords.
205
 *
206
 * @param xLoc list of x-coordinates of vertices. First value: distance of the left edge from 0.
207
 *             Next values: distance of the next column from the previous. Must be NonNull!
208
 * @param yLoc list of y-coordinates of vertices. First value: distance of the bottom edge from 0.
209
 *             Next values: distance of the next row from the previous. Must be NonNull!
210
 */
211
  public MeshSquare(float[] xLoc, float[] yLoc)
212
    {
213
    super();
214
    computeNumberOfVertices(xLoc.length-1,yLoc.length-1);
215

  
216
    float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
217
    float[] attribs2= new float[VERT2_ATTRIBS*numVertices];
218

  
219
    buildGrid(attribs1,attribs2,xLoc,yLoc);
220

  
221
    if( remainingVert!=0 )
222
      android.util.Log.d("MeshRectangles", "remainingVert " +remainingVert );
223

  
224
    setAttribs(attribs1,attribs2);
225
    }
226

  
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228
/**
229
 * Copy constructor.
230
 */
231
  public MeshSquare(MeshSquare mesh, boolean deep)
232
    {
233
    super(mesh,deep);
234
    }
235

  
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237
/**
238
 * Copy the Mesh.
239
 *
240
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
241
 *             normals and inflates (the rest, in particular the mVertAttribs2 containing texture
242
 *             coordinates and effect associations, is always deep copied)
243
 */
244
  public MeshSquare copy(boolean deep)
245
    {
246
    return new MeshSquare(this,deep);
247
    }
248
 }
src/main/java/org/distorted/library/mesh/MeshTriangle.java
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 MeshTriangle 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
     attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB  ] = x;
46
     attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = y;
47

  
48
     return vertex+1;
49
     }
50

  
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

  
53
  private void computeNumberOfVertices(int level)
54
     {
55
     numVertices = level*(level+2);
56
     remainingVert = numVertices;
57
     }
58

  
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

  
61
  private int buildRow(int vertex,float sx, float sy, int length, float dx, float dy, float[] attribs1, float[] attribs2)
62
    {
63
    for(int i=0; i<length; i++)
64
      {
65
      vertex = addVertex(vertex, sx   , sy   , attribs1, attribs2);
66
      vertex = addVertex(vertex, sx+dx, sy+dy, attribs1, attribs2);
67
      sx += 2*dx;
68
      }
69

  
70
    vertex = addVertex(vertex, sx, sy, attribs1, attribs2);
71

  
72
    return vertex;
73
    }
74

  
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

  
77
  private void buildGrid(float[] attribs1, float[] attribs2, int level)
78
     {
79
     float sx = 0.0f;
80
     float sy = 0.0f;
81
     float dx = 0.5f/level;
82
     float dy = 1.0f/level;
83
     int vertex = 0;
84

  
85
     for(int row=level; row>=1; row--)
86
       {
87
       vertex = buildRow(vertex,sx,sy,row,dx,dy,attribs1,attribs2);
88

  
89
       sx += 2*dx*(row-0.5f);
90
       sy += dy;
91
       dx *= -1;
92
       }
93
     }
94

  
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96
// PUBLIC API
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
  /**
99
   * Creates the underlying grid of vertices with the usual attribs which approximates an equilateral
100
   * triangle.
101
   *
102
   * @param level Specifies the level of slicing. Valid values: level &ge; 1
103
   */
104
  public MeshTriangle(int level)
105
     {
106
     super();
107

  
108
     computeNumberOfVertices(level);
109

  
110
     float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
111
     float[] attribs2= new float[VERT2_ATTRIBS*numVertices];
112

  
113
     buildGrid(attribs1, attribs2, level);
114

  
115
     if( remainingVert!=0 )
116
       android.util.Log.d("MeshTriangles", "remainingVert " +remainingVert );
117

  
118
     setAttribs(attribs1, attribs2);
119
     }
120

  
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122
/**
123
 * Copy constructor.
124
 */
125
  public MeshTriangle(MeshTriangle mesh, boolean deep)
126
    {
127
    super(mesh,deep);
128
    }
129

  
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131
/**
132
 * Copy the Mesh.
133
 *
134
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
135
 *             normals and inflates (the rest, in particular the mVertAttribs2 containing texture
136
 *             coordinates and effect associations, is always deep copied)
137
 */
138
  public MeshTriangle copy(boolean deep)
139
    {
140
    return new MeshTriangle(this,deep);
141
    }
142
  }
src/main/java/org/distorted/library/mesh/MeshTriangles.java
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
     attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB  ] = x;
46
     attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = y;
47

  
48
     return vertex+1;
49
     }
50

  
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

  
53
  private void computeNumberOfVertices(int level)
54
     {
55
     numVertices = level*(level+2);
56
     remainingVert = numVertices;
57
     }
58

  
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

  
61
  private int buildRow(int vertex,float sx, float sy, int length, float dx, float dy, float[] attribs1, float[] attribs2)
62
    {
63
    for(int i=0; i<length; i++)
64
      {
65
      vertex = addVertex(vertex, sx   , sy   , attribs1, attribs2);
66
      vertex = addVertex(vertex, sx+dx, sy+dy, attribs1, attribs2);
67
      sx += 2*dx;
68
      }
69

  
70
    vertex = addVertex(vertex, sx, sy, attribs1, attribs2);
71

  
72
    return vertex;
73
    }
74

  
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

  
77
  private void buildGrid(float[] attribs1, float[] attribs2, int level)
78
     {
79
     float sx = 0.0f;
80
     float sy = 0.0f;
81
     float dx = 0.5f/level;
82
     float dy = 1.0f/level;
83
     int vertex = 0;
84

  
85
     for(int row=level; row>=1; row--)
86
       {
87
       vertex = buildRow(vertex,sx,sy,row,dx,dy,attribs1,attribs2);
88

  
89
       sx += 2*dx*(row-0.5f);
90
       sy += dy;
91
       dx *= -1;
92
       }
93
     }
94

  
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96
// PUBLIC API
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
  /**
99
   * Creates the underlying grid of vertices with the usual attribs which approximates an equilateral
100
   * triangle.
101
   *
102
   * @param level Specifies the level of slicing. Valid values: level &ge; 1
103
   */
104
  public MeshTriangles(int level)
105
     {
106
     super();
107

  
108
     computeNumberOfVertices(level);
109

  
110
     float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
111
     float[] attribs2= new float[VERT2_ATTRIBS*numVertices];
112

  
113
     buildGrid(attribs1, attribs2, level);
114

  
115
     if( remainingVert!=0 )
116
       android.util.Log.d("MeshTriangles", "remainingVert " +remainingVert );
117

  
118
     setAttribs(attribs1, attribs2);
119
     }
120

  
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122
/**
123
 * Copy constructor.
124
 */
125
  public MeshTriangles(MeshTriangles mesh, boolean deep)
126
    {
127
    super(mesh,deep);
128
    }
129

  
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131
/**
132
 * Copy the Mesh.
133
 *
134
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
135
 *             normals and inflates (the rest, in particular the mVertAttribs2 containing texture
136
 *             coordinates and effect associations, is always deep copied)
137
 */
138
  public MeshTriangles copy(boolean deep)
139
    {
140
    return new MeshTriangles(this,deep);
141
    }
142
  }

Also available in: Unified diff