Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshRectangles.java @ 4f81e0c8

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 12e379d6 Leszek Koltunski
public class MeshRectangles 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 e54bfada Leszek Koltunski
     attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB  ] = x-0.5f;
63
     attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] = 0.5f-y;
64
     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 e54bfada Leszek Koltunski
     attribs1[VERT1_ATTRIBS*vertex + INF_ATTRIB  ] = (x-0.5f);
71
     attribs1[VERT1_ATTRIBS*vertex + INF_ATTRIB+1] = (0.5f-y);
72
     attribs1[VERT1_ATTRIBS*vertex + INF_ATTRIB+2] = 0.01f   ;  // Inflated surface needs to be slightly in front
73 6f2d931d Leszek Koltunski
74 e54bfada Leszek Koltunski
     attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB  ] = x;
75
     attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = 1.0f-y;
76 1e672c1d Leszek Koltunski
     attribs2[VERT2_ATTRIBS*vertex + ASS_ATTRIB  ] = DEFAULT_ASSOCIATION;
77 6a06a912 Leszek Koltunski
78 adb2661c Leszek Koltunski
     return vertex+1;
79
     }
80 466450b5 Leszek Koltunski
81 adb2661c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
82 d44ac567 Leszek Koltunski
83 e54bfada Leszek Koltunski
  private int repeatLast(int vertex, float[] attribs1, float[] attribs2)
84 adb2661c Leszek Koltunski
     {
85
     if( vertex>0 )
86
       {
87 6f2d931d Leszek Koltunski
       remainingVert--;
88
89 e54bfada Leszek Koltunski
       attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB  ] = attribs1[VERT1_ATTRIBS*(vertex-1) + POS_ATTRIB  ];
90
       attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] = attribs1[VERT1_ATTRIBS*(vertex-1) + POS_ATTRIB+1];
91
       attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = attribs1[VERT1_ATTRIBS*(vertex-1) + POS_ATTRIB+2];
92 6f2d931d Leszek Koltunski
93 e54bfada Leszek Koltunski
       attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB  ] = attribs1[VERT1_ATTRIBS*(vertex-1) + NOR_ATTRIB  ];
94
       attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+1] = attribs1[VERT1_ATTRIBS*(vertex-1) + NOR_ATTRIB+1];
95
       attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+2] = attribs1[VERT1_ATTRIBS*(vertex-1) + NOR_ATTRIB+2];
96 6f2d931d Leszek Koltunski
97 e54bfada Leszek Koltunski
       attribs1[VERT1_ATTRIBS*vertex + INF_ATTRIB  ] = attribs1[VERT1_ATTRIBS*(vertex-1) + INF_ATTRIB  ];
98
       attribs1[VERT1_ATTRIBS*vertex + INF_ATTRIB+1] = attribs1[VERT1_ATTRIBS*(vertex-1) + INF_ATTRIB+1];
99
       attribs1[VERT1_ATTRIBS*vertex + INF_ATTRIB+2] = attribs1[VERT1_ATTRIBS*(vertex-1) + INF_ATTRIB+2];
100 6f2d931d Leszek Koltunski
101 e54bfada Leszek Koltunski
       attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB  ] = attribs2[VERT2_ATTRIBS*(vertex-1) + TEX_ATTRIB  ];
102
       attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = attribs2[VERT2_ATTRIBS*(vertex-1) + TEX_ATTRIB+1];
103 1e672c1d Leszek Koltunski
       attribs2[VERT2_ATTRIBS*vertex + ASS_ATTRIB  ] = DEFAULT_ASSOCIATION;
104 d44ac567 Leszek Koltunski
105 adb2661c Leszek Koltunski
       vertex++;
106
       }
107 6a06a912 Leszek Koltunski
108 adb2661c Leszek Koltunski
     return vertex;
109
     }
110 d44ac567 Leszek Koltunski
111 adb2661c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
112
113 e54bfada Leszek Koltunski
  private void buildGrid(float[] attribs1, float[] attribs2)
114 adb2661c Leszek Koltunski
     {
115
     boolean lastBlockIsNE = false;
116
     boolean currentBlockIsNE;
117
     int vertex = 0;
118
119 69ed1eb4 Leszek Koltunski
     float x,y;
120
     final float X = 1.0f/mCols;
121
     final float Y = 1.0f/mRows;
122
123
     y = 0.0f;
124
125 adb2661c Leszek Koltunski
     for(int row=0; row<mRows; row++)
126
       {
127 69ed1eb4 Leszek Koltunski
       x = 0.0f;
128
129 adb2661c Leszek Koltunski
       for(int col=0; col<mCols; col++)
130
         {
131
         currentBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1);
132
133
         if( col==0 || (lastBlockIsNE^currentBlockIsNE) )
134
           {
135 e54bfada Leszek Koltunski
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs1,attribs2);
136
           vertex= addVertex( vertex, x, y+(currentBlockIsNE?0:Y), attribs1,attribs2);
137
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs1,attribs2);
138
           if( lastBlockIsNE^currentBlockIsNE)  vertex = repeatLast(vertex,attribs1,attribs2);
139
           vertex= addVertex( vertex, x, y+(currentBlockIsNE?Y:0), attribs1,attribs2);
140 adb2661c Leszek Koltunski
           }
141 e54bfada Leszek Koltunski
         vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?0:Y), attribs1,attribs2);
142
         vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?Y:0), attribs1,attribs2);
143 adb2661c Leszek Koltunski
144
         lastBlockIsNE = currentBlockIsNE;
145 69ed1eb4 Leszek Koltunski
         x+=X;
146 adb2661c Leszek Koltunski
         }
147 69ed1eb4 Leszek Koltunski
148
       y+=Y;
149 adb2661c Leszek Koltunski
       }
150
     }
151
152 79f172ab Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
153 a56bc359 Leszek Koltunski
// PUBLIC API
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155 adb2661c Leszek Koltunski
/**
156
 * Creates the underlying grid of vertices, normals and texture coords.
157
 *
158
 * @param cols Number of columns in the grid.
159
 * @param rows Number of rows in the grid.
160
 */
161 e54bfada Leszek Koltunski
  public MeshRectangles(int cols, int rows)
162 a51fe521 Leszek Koltunski
    {
163 0f10a0b6 Leszek Koltunski
    super();
164 a51fe521 Leszek Koltunski
    computeNumberOfVertices(cols,rows);
165 adb2661c Leszek Koltunski
166 e54bfada Leszek Koltunski
    float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
167
    float[] attribs2= new float[VERT2_ATTRIBS*numVertices];
168 adb2661c Leszek Koltunski
169 e54bfada Leszek Koltunski
    buildGrid(attribs1,attribs2);
170 adb2661c Leszek Koltunski
171 a51fe521 Leszek Koltunski
    if( remainingVert!=0 )
172 12e379d6 Leszek Koltunski
      android.util.Log.d("MeshRectangles", "remainingVert " +remainingVert );
173 adb2661c Leszek Koltunski
174 e54bfada Leszek Koltunski
    setAttribs(attribs1,attribs2);
175 a51fe521 Leszek Koltunski
    }
176 cbd502ec Leszek Koltunski
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178
/**
179 4f81e0c8 Leszek Koltunski
 * Copy constructor.
180 cbd502ec Leszek Koltunski
 */
181 4f81e0c8 Leszek Koltunski
  public MeshRectangles(MeshRectangles mesh, boolean deep)
182 e54bfada Leszek Koltunski
    {
183 4f81e0c8 Leszek Koltunski
    super(mesh,deep);
184 e54bfada Leszek Koltunski
    }
185 cbd502ec Leszek Koltunski
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187
/**
188 4f81e0c8 Leszek Koltunski
 * Copy the Mesh.
189
 *
190
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
191
 *             normals and inflates (the rest, in particular the mVertAttribs2 containing texture
192
 *             coordinates and effect assocciations, is always deep copied)
193 cbd502ec Leszek Koltunski
 */
194 4f81e0c8 Leszek Koltunski
  public MeshRectangles copy(boolean deep)
195 e54bfada Leszek Koltunski
    {
196 4f81e0c8 Leszek Koltunski
    return new MeshRectangles(this,deep);
197 e54bfada Leszek Koltunski
    }
198 a51fe521 Leszek Koltunski
 }