Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshSquare.java @ 97b6c85e

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