Project

General

Profile

Download (9.68 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / mesh / MeshSquare.java @ d23592bb

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2017 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 flat, rectangular grid.
26
 * <p>
27
 * Perfect if you just want to display a flat Texture. If you are not planning to apply any VERTEX
28
 * effects to it, use MeshRectangles(1,1), i.e. a Quad. Otherwise, create more vertices for more realistic effects!
29
 */
30
public class MeshSquare extends MeshBase
31
  {
32
  private int mCols, mRows;
33
  private int remainingVert;
34
  private int numVertices;
35

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37
// Create a flat, full grid.
38

    
39
  private void computeNumberOfVertices(int cols, int rows)
40
     {
41
     mRows=rows;
42
     mCols=cols;
43

    
44
     if( cols==1 && rows==1 )
45
       {
46
       numVertices = 4;
47
       }
48
     else
49
       {
50
       numVertices = 2*( mRows*mCols +2*mRows - 1) +2*(mCols>=2 ? mRows:0) +
51
                     (mCols>=2 && mRows>=2 ? 2*mRows-2 : 1);
52
       }
53

    
54
     remainingVert = numVertices;
55
     }
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
  private int addVertex(int vertex, float x, float y, float[] attribs1, float[] attribs2)
60
     {
61
     remainingVert--;
62

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

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

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

    
74
     return vertex+1;
75
     }
76

    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

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

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

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

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

    
96
       vertex++;
97
       }
98

    
99
     return vertex;
100
     }
101

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

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

    
112
     y =-0.5f;
113

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

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

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

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

    
137
       y+=dy;
138
       }
139
     }
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142

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

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

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

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

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

    
174
       y+=dy;
175
       }
176
     }
177

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

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

    
195
    buildGrid(attribs1,attribs2);
196

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

    
200
    setAttribs(attribs1,attribs2);
201
    }
202

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

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

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

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

    
225
    setAttribs(attribs1,attribs2);
226
    }
227

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

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238
/**
239
 * Copy the Mesh.
240
 *
241
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
242
 *             normals and inflates (the rest, in particular the mVertAttribs2 containing texture
243
 *             coordinates and effect associations, is always deep copied)
244
 */
245
  public MeshSquare copy(boolean deep)
246
    {
247
    return new MeshSquare(this,deep);
248
    }
249
 }
(9-9/10)