Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshSquare.java @ 8c57d77b

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 8c57d77b Leszek Koltunski
25
import org.distorted.library.main.DistortedLibrary;
26
27 e0b6c593 Leszek Koltunski
/**
28 05403bba Leszek Koltunski
 * Create a flat, rectangular grid.
29 e0b6c593 Leszek Koltunski
 * <p>
30
 * Perfect if you just want to display a flat Texture. If you are not planning to apply any VERTEX
31 12e379d6 Leszek Koltunski
 * effects to it, use MeshRectangles(1,1), i.e. a Quad. Otherwise, create more vertices for more realistic effects!
32 e0b6c593 Leszek Koltunski
 */
33 b948df7a Leszek Koltunski
public class MeshSquare extends MeshBase
34 6a06a912 Leszek Koltunski
  {
35 adb2661c Leszek Koltunski
  private int mCols, mRows;
36
  private int remainingVert;
37 da681e7e Leszek Koltunski
  private int numVertices;
38 6a06a912 Leszek Koltunski
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40 cacc63de Leszek Koltunski
// Create a flat, full grid.
41 6a06a912 Leszek Koltunski
42 a51fe521 Leszek Koltunski
  private void computeNumberOfVertices(int cols, int rows)
43 adb2661c Leszek Koltunski
     {
44 79f172ab Leszek Koltunski
     mRows=rows;
45
     mCols=cols;
46 d44ac567 Leszek Koltunski
47 79f172ab Leszek Koltunski
     if( cols==1 && rows==1 )
48
       {
49 da681e7e Leszek Koltunski
       numVertices = 4;
50 79f172ab Leszek Koltunski
       }
51
     else
52 adb2661c Leszek Koltunski
       {
53 da681e7e Leszek Koltunski
       numVertices = 2*( mRows*mCols +2*mRows - 1) +2*(mCols>=2 ? mRows:0) +
54 a51fe521 Leszek Koltunski
                     (mCols>=2 && mRows>=2 ? 2*mRows-2 : 1);
55 79f172ab Leszek Koltunski
       }
56 d44ac567 Leszek Koltunski
57 da681e7e Leszek Koltunski
     remainingVert = numVertices;
58 adb2661c Leszek Koltunski
     }
59 d44ac567 Leszek Koltunski
60 adb2661c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
61 6a06a912 Leszek Koltunski
62 e54bfada Leszek Koltunski
  private int addVertex(int vertex, float x, float y, float[] attribs1, float[] attribs2)
63 adb2661c Leszek Koltunski
     {
64
     remainingVert--;
65 6a06a912 Leszek Koltunski
66 1833b0a0 Leszek Koltunski
     attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB  ] =    x;
67
     attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] =   -y;
68 e54bfada Leszek Koltunski
     attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = 0.0f;
69 6a06a912 Leszek Koltunski
70 e54bfada Leszek Koltunski
     attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB  ] = 0.0f;
71
     attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+1] = 0.0f;
72
     attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+2] = 1.0f;
73 6a06a912 Leszek Koltunski
74 1833b0a0 Leszek Koltunski
     attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB  ] = x+0.5f;
75
     attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = 0.5f-y;
76 6a06a912 Leszek Koltunski
77 adb2661c Leszek Koltunski
     return vertex+1;
78
     }
79 466450b5 Leszek Koltunski
80 adb2661c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
81 d44ac567 Leszek Koltunski
82 e54bfada Leszek Koltunski
  private int repeatLast(int vertex, float[] attribs1, float[] attribs2)
83 adb2661c Leszek Koltunski
     {
84
     if( vertex>0 )
85
       {
86 6f2d931d Leszek Koltunski
       remainingVert--;
87
88 e54bfada Leszek Koltunski
       attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB  ] = attribs1[VERT1_ATTRIBS*(vertex-1) + POS_ATTRIB  ];
89
       attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] = attribs1[VERT1_ATTRIBS*(vertex-1) + POS_ATTRIB+1];
90
       attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = attribs1[VERT1_ATTRIBS*(vertex-1) + POS_ATTRIB+2];
91 6f2d931d Leszek Koltunski
92 e54bfada Leszek Koltunski
       attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB  ] = attribs1[VERT1_ATTRIBS*(vertex-1) + NOR_ATTRIB  ];
93
       attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+1] = attribs1[VERT1_ATTRIBS*(vertex-1) + NOR_ATTRIB+1];
94
       attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+2] = attribs1[VERT1_ATTRIBS*(vertex-1) + NOR_ATTRIB+2];
95 6f2d931d Leszek Koltunski
96 e54bfada Leszek Koltunski
       attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB  ] = attribs2[VERT2_ATTRIBS*(vertex-1) + TEX_ATTRIB  ];
97
       attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = attribs2[VERT2_ATTRIBS*(vertex-1) + TEX_ATTRIB+1];
98 d44ac567 Leszek Koltunski
99 adb2661c Leszek Koltunski
       vertex++;
100
       }
101 6a06a912 Leszek Koltunski
102 adb2661c Leszek Koltunski
     return vertex;
103
     }
104 d44ac567 Leszek Koltunski
105 adb2661c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
106
107 e54bfada Leszek Koltunski
  private void buildGrid(float[] attribs1, float[] attribs2)
108 adb2661c Leszek Koltunski
     {
109 1833b0a0 Leszek Koltunski
     boolean currBlockIsNE, lastBlockIsNE = false;
110 adb2661c Leszek Koltunski
     int vertex = 0;
111 69ed1eb4 Leszek Koltunski
     float x,y;
112 1833b0a0 Leszek Koltunski
     final float dx = 1.0f/mCols;
113
     final float dy = 1.0f/mRows;
114 69ed1eb4 Leszek Koltunski
115 1833b0a0 Leszek Koltunski
     y =-0.5f;
116 69ed1eb4 Leszek Koltunski
117 adb2661c Leszek Koltunski
     for(int row=0; row<mRows; row++)
118
       {
119 1833b0a0 Leszek Koltunski
       x =-0.5f;
120 69ed1eb4 Leszek Koltunski
121 adb2661c Leszek Koltunski
       for(int col=0; col<mCols; col++)
122
         {
123 1833b0a0 Leszek Koltunski
         currBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1);
124 adb2661c Leszek Koltunski
125 1833b0a0 Leszek Koltunski
         if( col==0 || (lastBlockIsNE^currBlockIsNE) )
126 adb2661c Leszek Koltunski
           {
127 e54bfada Leszek Koltunski
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs1,attribs2);
128 1833b0a0 Leszek Koltunski
           vertex= addVertex( vertex, x, y+(currBlockIsNE?0:dy), attribs1,attribs2);
129 e54bfada Leszek Koltunski
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs1,attribs2);
130 1833b0a0 Leszek Koltunski
           if( lastBlockIsNE^currBlockIsNE)  vertex = repeatLast(vertex,attribs1,attribs2);
131
           vertex= addVertex( vertex, x, y+(currBlockIsNE?dy:0), attribs1,attribs2);
132 adb2661c Leszek Koltunski
           }
133 1833b0a0 Leszek Koltunski
         vertex= addVertex( vertex, x+dx, y+(currBlockIsNE?0:dy), attribs1,attribs2);
134
         vertex= addVertex( vertex, x+dx, y+(currBlockIsNE?dy:0), attribs1,attribs2);
135 adb2661c Leszek Koltunski
136 1833b0a0 Leszek Koltunski
         lastBlockIsNE = currBlockIsNE;
137
         x+=dx;
138 adb2661c Leszek Koltunski
         }
139 69ed1eb4 Leszek Koltunski
140 1833b0a0 Leszek Koltunski
       y+=dy;
141
       }
142
     }
143
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145
146
  private void buildGrid(float[] attribs1, float[] attribs2, float[] xLoc, float[] yLoc)
147
     {
148
     boolean currBlockIsNE,lastBlockIsNE = false;
149
     int vertex = 0;
150
     float dx,dy,x,y= yLoc[0];
151
152
     for(int row=0; row<mRows; row++)
153
       {
154
       x = xLoc[0];
155
       dy= yLoc[row+1];
156
157
       for(int col=0; col<mCols; col++)
158
         {
159
         dx = xLoc[col+1];
160
         currBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1);
161
162
         if( col==0 || (lastBlockIsNE^currBlockIsNE) )
163
           {
164
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs1,attribs2);
165
           vertex= addVertex( vertex, x, y+(currBlockIsNE?0:dy), attribs1,attribs2);
166
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs1,attribs2);
167
           if( lastBlockIsNE^currBlockIsNE)  vertex = repeatLast(vertex,attribs1,attribs2);
168
           vertex= addVertex( vertex, x, y+(currBlockIsNE?dy:0), attribs1,attribs2);
169
           }
170
         vertex= addVertex( vertex, x+dx, y+(currBlockIsNE?0:dy), attribs1,attribs2);
171
         vertex= addVertex( vertex, x+dx, y+(currBlockIsNE?dy:0), attribs1,attribs2);
172
173
         lastBlockIsNE = currBlockIsNE;
174
         x+=dx;
175
         }
176
177
       y+=dy;
178 adb2661c Leszek Koltunski
       }
179
     }
180
181 79f172ab Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
182 a56bc359 Leszek Koltunski
// PUBLIC API
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184 adb2661c Leszek Koltunski
/**
185 1833b0a0 Leszek Koltunski
 * Creates a rectangular grid of vertices, normals and texture coords.
186 adb2661c Leszek Koltunski
 *
187
 * @param cols Number of columns in the grid.
188
 * @param rows Number of rows in the grid.
189
 */
190 b948df7a Leszek Koltunski
  public MeshSquare(int cols, int rows)
191 a51fe521 Leszek Koltunski
    {
192 0f10a0b6 Leszek Koltunski
    super();
193 a51fe521 Leszek Koltunski
    computeNumberOfVertices(cols,rows);
194 adb2661c Leszek Koltunski
195 e54bfada Leszek Koltunski
    float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
196
    float[] attribs2= new float[VERT2_ATTRIBS*numVertices];
197 adb2661c Leszek Koltunski
198 e54bfada Leszek Koltunski
    buildGrid(attribs1,attribs2);
199 adb2661c Leszek Koltunski
200 a51fe521 Leszek Koltunski
    if( remainingVert!=0 )
201 8c57d77b Leszek Koltunski
      DistortedLibrary.logMessage("MeshSquare: remainingVert " +remainingVert );
202 adb2661c Leszek Koltunski
203 e54bfada Leszek Koltunski
    setAttribs(attribs1,attribs2);
204 a51fe521 Leszek Koltunski
    }
205 cbd502ec Leszek Koltunski
206 1833b0a0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
207
/**
208
 * Creates a rectangular grid of vertices, normals and texture coords.
209
 *
210
 * @param xLoc list of x-coordinates of vertices. First value: distance of the left edge from 0.
211
 *             Next values: distance of the next column from the previous. Must be NonNull!
212
 * @param yLoc list of y-coordinates of vertices. First value: distance of the bottom edge from 0.
213
 *             Next values: distance of the next row from the previous. Must be NonNull!
214
 */
215 b948df7a Leszek Koltunski
  public MeshSquare(float[] xLoc, float[] yLoc)
216 1833b0a0 Leszek Koltunski
    {
217
    super();
218
    computeNumberOfVertices(xLoc.length-1,yLoc.length-1);
219
220
    float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
221
    float[] attribs2= new float[VERT2_ATTRIBS*numVertices];
222
223
    buildGrid(attribs1,attribs2,xLoc,yLoc);
224
225
    if( remainingVert!=0 )
226 8c57d77b Leszek Koltunski
      DistortedLibrary.logMessage("MeshSquare: remainingVert " +remainingVert );
227 1833b0a0 Leszek Koltunski
228
    setAttribs(attribs1,attribs2);
229
    }
230
231 cbd502ec Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
232
/**
233 4f81e0c8 Leszek Koltunski
 * Copy constructor.
234 cbd502ec Leszek Koltunski
 */
235 b948df7a Leszek Koltunski
  public MeshSquare(MeshSquare mesh, boolean deep)
236 e54bfada Leszek Koltunski
    {
237 4f81e0c8 Leszek Koltunski
    super(mesh,deep);
238 e54bfada Leszek Koltunski
    }
239 cbd502ec Leszek Koltunski
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241
/**
242 4f81e0c8 Leszek Koltunski
 * Copy the Mesh.
243
 *
244
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
245
 *             normals and inflates (the rest, in particular the mVertAttribs2 containing texture
246 aee078f8 Leszek Koltunski
 *             coordinates and effect associations, is always deep copied)
247 cbd502ec Leszek Koltunski
 */
248 b948df7a Leszek Koltunski
  public MeshSquare copy(boolean deep)
249 e54bfada Leszek Koltunski
    {
250 b948df7a Leszek Koltunski
    return new MeshSquare(this,deep);
251 e54bfada Leszek Koltunski
    }
252 a51fe521 Leszek Koltunski
 }