Project

General

Profile

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

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

1 d333eb6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2 c4d06f90 Leszek Koltunski
// Copyright 2017 Leszek Koltunski  leszek@koltunski.pl                                          //
3 d333eb6b Leszek Koltunski
//                                                                                               //
4 46b572b5 Leszek Koltunski
// This file is part of Distorted.                                                               //
5 d333eb6b Leszek Koltunski
//                                                                                               //
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 d333eb6b Leszek Koltunski
//                                                                                               //
11 c4d06f90 Leszek Koltunski
// This library is distributed in the hope that it will be useful,                               //
12 d333eb6b 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 d333eb6b 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 d333eb6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
20
21 6c00149d Leszek Koltunski
package org.distorted.library.mesh;
22 6a06a912 Leszek Koltunski
23
///////////////////////////////////////////////////////////////////////////////////////////////////
24 e0b6c593 Leszek Koltunski
/**
25 05403bba Leszek Koltunski
 * Create a flat, rectangular grid.
26 e0b6c593 Leszek Koltunski
 * <p>
27
 * Perfect if you just want to display a flat Texture. If you are not planning to apply any VERTEX
28 12e379d6 Leszek Koltunski
 * effects to it, use MeshRectangles(1,1), i.e. a Quad. Otherwise, create more vertices for more realistic effects!
29 e0b6c593 Leszek Koltunski
 */
30 b948df7a Leszek Koltunski
public class MeshSquare extends MeshBase
31 6a06a912 Leszek Koltunski
  {
32 adb2661c Leszek Koltunski
  private int mCols, mRows;
33
  private int remainingVert;
34 da681e7e Leszek Koltunski
  private int numVertices;
35 6a06a912 Leszek Koltunski
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37 cacc63de Leszek Koltunski
// Create a flat, full grid.
38 6a06a912 Leszek Koltunski
39 a51fe521 Leszek Koltunski
  private void computeNumberOfVertices(int cols, int rows)
40 adb2661c Leszek Koltunski
     {
41 79f172ab Leszek Koltunski
     mRows=rows;
42
     mCols=cols;
43 d44ac567 Leszek Koltunski
44 79f172ab Leszek Koltunski
     if( cols==1 && rows==1 )
45
       {
46 da681e7e Leszek Koltunski
       numVertices = 4;
47 79f172ab Leszek Koltunski
       }
48
     else
49 adb2661c Leszek Koltunski
       {
50 da681e7e Leszek Koltunski
       numVertices = 2*( mRows*mCols +2*mRows - 1) +2*(mCols>=2 ? mRows:0) +
51 a51fe521 Leszek Koltunski
                     (mCols>=2 && mRows>=2 ? 2*mRows-2 : 1);
52 79f172ab Leszek Koltunski
       }
53 d44ac567 Leszek Koltunski
54 da681e7e Leszek Koltunski
     remainingVert = numVertices;
55 adb2661c Leszek Koltunski
     }
56 d44ac567 Leszek Koltunski
57 adb2661c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
58 6a06a912 Leszek Koltunski
59 e54bfada Leszek Koltunski
  private int addVertex(int vertex, float x, float y, float[] attribs1, float[] attribs2)
60 adb2661c Leszek Koltunski
     {
61
     remainingVert--;
62 6a06a912 Leszek Koltunski
63 1833b0a0 Leszek Koltunski
     attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB  ] =    x;
64
     attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] =   -y;
65 e54bfada Leszek Koltunski
     attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = 0.0f;
66 6a06a912 Leszek Koltunski
67 e54bfada Leszek Koltunski
     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 6a06a912 Leszek Koltunski
71 1833b0a0 Leszek Koltunski
     attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB  ] = x+0.5f;
72
     attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = 0.5f-y;
73 6a06a912 Leszek Koltunski
74 adb2661c Leszek Koltunski
     return vertex+1;
75
     }
76 466450b5 Leszek Koltunski
77 adb2661c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
78 d44ac567 Leszek Koltunski
79 e54bfada Leszek Koltunski
  private int repeatLast(int vertex, float[] attribs1, float[] attribs2)
80 adb2661c Leszek Koltunski
     {
81
     if( vertex>0 )
82
       {
83 6f2d931d Leszek Koltunski
       remainingVert--;
84
85 e54bfada Leszek Koltunski
       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 6f2d931d Leszek Koltunski
89 e54bfada Leszek Koltunski
       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 6f2d931d Leszek Koltunski
93 e54bfada Leszek Koltunski
       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 d44ac567 Leszek Koltunski
96 adb2661c Leszek Koltunski
       vertex++;
97
       }
98 6a06a912 Leszek Koltunski
99 adb2661c Leszek Koltunski
     return vertex;
100
     }
101 d44ac567 Leszek Koltunski
102 adb2661c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
103
104 e54bfada Leszek Koltunski
  private void buildGrid(float[] attribs1, float[] attribs2)
105 adb2661c Leszek Koltunski
     {
106 1833b0a0 Leszek Koltunski
     boolean currBlockIsNE, lastBlockIsNE = false;
107 adb2661c Leszek Koltunski
     int vertex = 0;
108 69ed1eb4 Leszek Koltunski
     float x,y;
109 1833b0a0 Leszek Koltunski
     final float dx = 1.0f/mCols;
110
     final float dy = 1.0f/mRows;
111 69ed1eb4 Leszek Koltunski
112 1833b0a0 Leszek Koltunski
     y =-0.5f;
113 69ed1eb4 Leszek Koltunski
114 adb2661c Leszek Koltunski
     for(int row=0; row<mRows; row++)
115
       {
116 1833b0a0 Leszek Koltunski
       x =-0.5f;
117 69ed1eb4 Leszek Koltunski
118 adb2661c Leszek Koltunski
       for(int col=0; col<mCols; col++)
119
         {
120 1833b0a0 Leszek Koltunski
         currBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1);
121 adb2661c Leszek Koltunski
122 1833b0a0 Leszek Koltunski
         if( col==0 || (lastBlockIsNE^currBlockIsNE) )
123 adb2661c Leszek Koltunski
           {
124 e54bfada Leszek Koltunski
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs1,attribs2);
125 1833b0a0 Leszek Koltunski
           vertex= addVertex( vertex, x, y+(currBlockIsNE?0:dy), attribs1,attribs2);
126 e54bfada Leszek Koltunski
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs1,attribs2);
127 1833b0a0 Leszek Koltunski
           if( lastBlockIsNE^currBlockIsNE)  vertex = repeatLast(vertex,attribs1,attribs2);
128
           vertex= addVertex( vertex, x, y+(currBlockIsNE?dy:0), attribs1,attribs2);
129 adb2661c Leszek Koltunski
           }
130 1833b0a0 Leszek Koltunski
         vertex= addVertex( vertex, x+dx, y+(currBlockIsNE?0:dy), attribs1,attribs2);
131
         vertex= addVertex( vertex, x+dx, y+(currBlockIsNE?dy:0), attribs1,attribs2);
132 adb2661c Leszek Koltunski
133 1833b0a0 Leszek Koltunski
         lastBlockIsNE = currBlockIsNE;
134
         x+=dx;
135 adb2661c Leszek Koltunski
         }
136 69ed1eb4 Leszek Koltunski
137 1833b0a0 Leszek Koltunski
       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 adb2661c Leszek Koltunski
       }
176
     }
177
178 79f172ab Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
179 a56bc359 Leszek Koltunski
// PUBLIC API
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181 adb2661c Leszek Koltunski
/**
182 1833b0a0 Leszek Koltunski
 * Creates a rectangular grid of vertices, normals and texture coords.
183 adb2661c Leszek Koltunski
 *
184
 * @param cols Number of columns in the grid.
185
 * @param rows Number of rows in the grid.
186
 */
187 b948df7a Leszek Koltunski
  public MeshSquare(int cols, int rows)
188 a51fe521 Leszek Koltunski
    {
189 0f10a0b6 Leszek Koltunski
    super();
190 a51fe521 Leszek Koltunski
    computeNumberOfVertices(cols,rows);
191 adb2661c Leszek Koltunski
192 e54bfada Leszek Koltunski
    float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
193
    float[] attribs2= new float[VERT2_ATTRIBS*numVertices];
194 adb2661c Leszek Koltunski
195 e54bfada Leszek Koltunski
    buildGrid(attribs1,attribs2);
196 adb2661c Leszek Koltunski
197 a51fe521 Leszek Koltunski
    if( remainingVert!=0 )
198 12e379d6 Leszek Koltunski
      android.util.Log.d("MeshRectangles", "remainingVert " +remainingVert );
199 adb2661c Leszek Koltunski
200 e54bfada Leszek Koltunski
    setAttribs(attribs1,attribs2);
201 a51fe521 Leszek Koltunski
    }
202 cbd502ec Leszek Koltunski
203 1833b0a0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 b948df7a Leszek Koltunski
  public MeshSquare(float[] xLoc, float[] yLoc)
213 1833b0a0 Leszek Koltunski
    {
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 cbd502ec Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
229
/**
230 4f81e0c8 Leszek Koltunski
 * Copy constructor.
231 cbd502ec Leszek Koltunski
 */
232 b948df7a Leszek Koltunski
  public MeshSquare(MeshSquare mesh, boolean deep)
233 e54bfada Leszek Koltunski
    {
234 4f81e0c8 Leszek Koltunski
    super(mesh,deep);
235 e54bfada Leszek Koltunski
    }
236 cbd502ec Leszek Koltunski
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238
/**
239 4f81e0c8 Leszek Koltunski
 * 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 aee078f8 Leszek Koltunski
 *             coordinates and effect associations, is always deep copied)
244 cbd502ec Leszek Koltunski
 */
245 b948df7a Leszek Koltunski
  public MeshSquare copy(boolean deep)
246 e54bfada Leszek Koltunski
    {
247 b948df7a Leszek Koltunski
    return new MeshSquare(this,deep);
248 e54bfada Leszek Koltunski
    }
249 a51fe521 Leszek Koltunski
 }