Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshFlat.java @ da681e7e

1 d333eb6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// 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
// Distorted 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                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
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 26df012c Leszek Koltunski
 * effects to it, use MeshFlat(1,1), i.e. a Quad. Otherwise, create more vertices for more realistic effects!
28 e0b6c593 Leszek Koltunski
 */
29 715e7726 Leszek Koltunski
public class MeshFlat 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 466450b5 Leszek Koltunski
     //android.util.Log.e("MeshFlat","vertices="+numVertices+" rows="+mRows+" cols="+mCols);
54 d44ac567 Leszek Koltunski
55 da681e7e Leszek Koltunski
     remainingVert = numVertices;
56 adb2661c Leszek Koltunski
     }
57 d44ac567 Leszek Koltunski
58 5bf698ee Leszek Koltunski
59 adb2661c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
60 6a06a912 Leszek Koltunski
61 a51fe521 Leszek Koltunski
  private int addVertex(int vertex, float x, float y, float[] attribs)
62 adb2661c Leszek Koltunski
     {
63
     remainingVert--;
64 6a06a912 Leszek Koltunski
65 a51fe521 Leszek Koltunski
     attribs[8*vertex  ] = x-0.5f;
66
     attribs[8*vertex+1] = 0.5f-y;
67
     attribs[8*vertex+2] = 0;
68 6a06a912 Leszek Koltunski
69 a51fe521 Leszek Koltunski
     attribs[8*vertex+3] = 0.0f;
70
     attribs[8*vertex+4] = 0.0f;
71
     attribs[8*vertex+5] = 1.0f;
72 6a06a912 Leszek Koltunski
73 a51fe521 Leszek Koltunski
     attribs[8*vertex+6] = x;
74
     attribs[8*vertex+7] = 1.0f-y;
75 6a06a912 Leszek Koltunski
76 adb2661c Leszek Koltunski
     return vertex+1;
77
     }
78 466450b5 Leszek Koltunski
79 adb2661c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
80 d44ac567 Leszek Koltunski
81 a51fe521 Leszek Koltunski
  private int repeatLast(int vertex, float[] attribs)
82 adb2661c Leszek Koltunski
     {
83
     remainingVert--;
84 6a06a912 Leszek Koltunski
85 466450b5 Leszek Koltunski
     //android.util.Log.e("MeshFlat", "repeating last vertex!");
86 6a06a912 Leszek Koltunski
87 adb2661c Leszek Koltunski
     if( vertex>0 )
88
       {
89 a51fe521 Leszek Koltunski
       attribs[8*vertex  ] = attribs[8*vertex-8];
90
       attribs[8*vertex+1] = attribs[8*vertex-7];
91
       attribs[8*vertex+2] = attribs[8*vertex-6];
92
       attribs[8*vertex+3] = attribs[8*vertex-5];
93
       attribs[8*vertex+4] = attribs[8*vertex-4];
94
       attribs[8*vertex+5] = attribs[8*vertex-3];
95
       attribs[8*vertex+6] = attribs[8*vertex-2];
96
       attribs[8*vertex+7] = attribs[8*vertex-1];
97 d44ac567 Leszek Koltunski
98 adb2661c Leszek Koltunski
       vertex++;
99
       }
100 6a06a912 Leszek Koltunski
101 adb2661c Leszek Koltunski
     return vertex;
102
     }
103 d44ac567 Leszek Koltunski
104 adb2661c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
105
106 a51fe521 Leszek Koltunski
  private void buildGrid(float[] attribs)
107 adb2661c Leszek Koltunski
     {
108
     boolean lastBlockIsNE = false;
109
     boolean currentBlockIsNE;
110
     int vertex = 0;
111
112 69ed1eb4 Leszek Koltunski
     float x,y;
113
     final float X = 1.0f/mCols;
114
     final float Y = 1.0f/mRows;
115
116 466450b5 Leszek Koltunski
     //android.util.Log.d("MeshFlat", "buildGrid");
117 adb2661c Leszek Koltunski
118 69ed1eb4 Leszek Koltunski
     y = 0.0f;
119
120 adb2661c Leszek Koltunski
     for(int row=0; row<mRows; row++)
121
       {
122 69ed1eb4 Leszek Koltunski
       x = 0.0f;
123
124 adb2661c Leszek Koltunski
       for(int col=0; col<mCols; col++)
125
         {
126
         currentBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1);
127
128
         if( col==0 || (lastBlockIsNE^currentBlockIsNE) )
129
           {
130 a51fe521 Leszek Koltunski
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs);
131
           vertex= addVertex( vertex, x, y+(currentBlockIsNE?0:Y), attribs);
132
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs);
133
           if( lastBlockIsNE^currentBlockIsNE)  vertex = repeatLast(vertex,attribs);
134
           vertex= addVertex( vertex, x, y+(currentBlockIsNE?Y:0), attribs);
135 adb2661c Leszek Koltunski
           }
136 a51fe521 Leszek Koltunski
         vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?0:Y), attribs);
137
         vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?Y:0), attribs);
138 adb2661c Leszek Koltunski
139
         lastBlockIsNE = currentBlockIsNE;
140 69ed1eb4 Leszek Koltunski
         x+=X;
141 adb2661c Leszek Koltunski
         }
142 69ed1eb4 Leszek Koltunski
143
       y+=Y;
144 adb2661c Leszek Koltunski
       }
145
146 466450b5 Leszek Koltunski
     //android.util.Log.d("MeshFlat", "buildGrid done");
147 adb2661c Leszek Koltunski
     }
148
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150 79f172ab Leszek Koltunski
/*
151 a51fe521 Leszek Koltunski
  private static String debug(float[] val, int stop)
152 79f172ab Leszek Koltunski
     {
153
     String ret="";
154
155
     for(int i=0; i<val.length; i++)
156
        {
157
        if( i%stop==0 ) ret+="\n";
158
        ret+=(" "+val[i]);
159
        }
160
161
     return ret;
162
     }
163
*/
164 11fb6ce0 Leszek Koltunski
165 79f172ab Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
166 a56bc359 Leszek Koltunski
// PUBLIC API
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168 adb2661c Leszek Koltunski
/**
169
 * Creates the underlying grid of vertices, normals and texture coords.
170
 *
171
 * @param cols Number of columns in the grid.
172
 * @param rows Number of rows in the grid.
173
 */
174 a51fe521 Leszek Koltunski
 public MeshFlat(int cols, int rows)
175
    {
176
    super(0.0f);
177
    computeNumberOfVertices(cols,rows);
178 adb2661c Leszek Koltunski
179 da681e7e Leszek Koltunski
    float[] attribs= new float[(POS_DATA_SIZE+NOR_DATA_SIZE+TEX_DATA_SIZE)*numVertices];
180 adb2661c Leszek Koltunski
181 a51fe521 Leszek Koltunski
    buildGrid(attribs);
182 adb2661c Leszek Koltunski
183 a51fe521 Leszek Koltunski
    //android.util.Log.e("MeshFlat", "dataLen="+numVertices);
184
    //android.util.Log.d("MeshFlat", "attribs: "+debug(attribs,8) );
185 adb2661c Leszek Koltunski
186 a51fe521 Leszek Koltunski
    if( remainingVert!=0 )
187 466450b5 Leszek Koltunski
      android.util.Log.d("MeshFlat", "remainingVert " +remainingVert );
188 adb2661c Leszek Koltunski
189 da681e7e Leszek Koltunski
    setData(numVertices, attribs);
190 a51fe521 Leszek Koltunski
    }
191
 }