Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshFlat.java @ 15290f35

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 adb2661c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
59 6a06a912 Leszek Koltunski
60 a51fe521 Leszek Koltunski
  private int addVertex(int vertex, float x, float y, float[] attribs)
61 adb2661c Leszek Koltunski
     {
62
     remainingVert--;
63 6a06a912 Leszek Koltunski
64 6f2d931d Leszek Koltunski
     attribs[VERT_ATTRIBS*vertex + POS_ATTRIB  ] = x-0.5f;
65
     attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+1] = 0.5f-y;
66 7a5e538a Leszek Koltunski
     attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+2] = 0.0f;
67 6a06a912 Leszek Koltunski
68 6f2d931d Leszek Koltunski
     attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB  ] = 0.0f;
69
     attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+1] = 0.0f;
70
     attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+2] = 1.0f;
71 6a06a912 Leszek Koltunski
72 7a5e538a Leszek Koltunski
     attribs[VERT_ATTRIBS*vertex + INF_ATTRIB  ] = x-0.5f;
73
     attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+1] = 0.5f-y;
74
     attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+2] = 0.0f;
75 6f2d931d Leszek Koltunski
76
     attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB  ] = x;
77
     attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB+1] = 1.0f-y;
78 6a06a912 Leszek Koltunski
79 adb2661c Leszek Koltunski
     return vertex+1;
80
     }
81 466450b5 Leszek Koltunski
82 adb2661c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
83 d44ac567 Leszek Koltunski
84 a51fe521 Leszek Koltunski
  private int repeatLast(int vertex, float[] attribs)
85 adb2661c Leszek Koltunski
     {
86 466450b5 Leszek Koltunski
     //android.util.Log.e("MeshFlat", "repeating last vertex!");
87 6a06a912 Leszek Koltunski
88 adb2661c Leszek Koltunski
     if( vertex>0 )
89
       {
90 6f2d931d Leszek Koltunski
       remainingVert--;
91
92
       attribs[VERT_ATTRIBS*vertex + POS_ATTRIB  ] = attribs[VERT_ATTRIBS*(vertex-1) + POS_ATTRIB  ];
93
       attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+1] = attribs[VERT_ATTRIBS*(vertex-1) + POS_ATTRIB+1];
94
       attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+2] = attribs[VERT_ATTRIBS*(vertex-1) + POS_ATTRIB+2];
95
96
       attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB  ] = attribs[VERT_ATTRIBS*(vertex-1) + NOR_ATTRIB  ];
97
       attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+1] = attribs[VERT_ATTRIBS*(vertex-1) + NOR_ATTRIB+1];
98
       attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+2] = attribs[VERT_ATTRIBS*(vertex-1) + NOR_ATTRIB+2];
99
100
       attribs[VERT_ATTRIBS*vertex + INF_ATTRIB  ] = attribs[VERT_ATTRIBS*(vertex-1) + INF_ATTRIB  ];
101
       attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+1] = attribs[VERT_ATTRIBS*(vertex-1) + INF_ATTRIB+1];
102
       attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+2] = attribs[VERT_ATTRIBS*(vertex-1) + INF_ATTRIB+2];
103
104
       attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB  ] = attribs[VERT_ATTRIBS*(vertex-1) + TEX_ATTRIB  ];
105
       attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB+1] = attribs[VERT_ATTRIBS*(vertex-1) + TEX_ATTRIB+1];
106 d44ac567 Leszek Koltunski
107 adb2661c Leszek Koltunski
       vertex++;
108
       }
109 6a06a912 Leszek Koltunski
110 adb2661c Leszek Koltunski
     return vertex;
111
     }
112 d44ac567 Leszek Koltunski
113 adb2661c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
114
115 a51fe521 Leszek Koltunski
  private void buildGrid(float[] attribs)
116 adb2661c Leszek Koltunski
     {
117
     boolean lastBlockIsNE = false;
118
     boolean currentBlockIsNE;
119
     int vertex = 0;
120
121 69ed1eb4 Leszek Koltunski
     float x,y;
122
     final float X = 1.0f/mCols;
123
     final float Y = 1.0f/mRows;
124
125 466450b5 Leszek Koltunski
     //android.util.Log.d("MeshFlat", "buildGrid");
126 adb2661c Leszek Koltunski
127 69ed1eb4 Leszek Koltunski
     y = 0.0f;
128
129 adb2661c Leszek Koltunski
     for(int row=0; row<mRows; row++)
130
       {
131 69ed1eb4 Leszek Koltunski
       x = 0.0f;
132
133 adb2661c Leszek Koltunski
       for(int col=0; col<mCols; col++)
134
         {
135
         currentBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1);
136
137
         if( col==0 || (lastBlockIsNE^currentBlockIsNE) )
138
           {
139 a51fe521 Leszek Koltunski
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs);
140
           vertex= addVertex( vertex, x, y+(currentBlockIsNE?0:Y), attribs);
141
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs);
142
           if( lastBlockIsNE^currentBlockIsNE)  vertex = repeatLast(vertex,attribs);
143
           vertex= addVertex( vertex, x, y+(currentBlockIsNE?Y:0), attribs);
144 adb2661c Leszek Koltunski
           }
145 a51fe521 Leszek Koltunski
         vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?0:Y), attribs);
146
         vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?Y:0), attribs);
147 adb2661c Leszek Koltunski
148
         lastBlockIsNE = currentBlockIsNE;
149 69ed1eb4 Leszek Koltunski
         x+=X;
150 adb2661c Leszek Koltunski
         }
151 69ed1eb4 Leszek Koltunski
152
       y+=Y;
153 adb2661c Leszek Koltunski
       }
154
155 466450b5 Leszek Koltunski
     //android.util.Log.d("MeshFlat", "buildGrid done");
156 adb2661c Leszek Koltunski
     }
157
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159 79f172ab Leszek Koltunski
/*
160 a51fe521 Leszek Koltunski
  private static String debug(float[] val, int stop)
161 79f172ab Leszek Koltunski
     {
162
     String ret="";
163
164
     for(int i=0; i<val.length; i++)
165
        {
166
        if( i%stop==0 ) ret+="\n";
167
        ret+=(" "+val[i]);
168
        }
169
170
     return ret;
171
     }
172
*/
173 11fb6ce0 Leszek Koltunski
174 79f172ab Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
175 a56bc359 Leszek Koltunski
// PUBLIC API
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177 adb2661c Leszek Koltunski
/**
178
 * Creates the underlying grid of vertices, normals and texture coords.
179
 *
180
 * @param cols Number of columns in the grid.
181
 * @param rows Number of rows in the grid.
182
 */
183 a51fe521 Leszek Koltunski
 public MeshFlat(int cols, int rows)
184
    {
185
    super(0.0f);
186
    computeNumberOfVertices(cols,rows);
187 adb2661c Leszek Koltunski
188 6f2d931d Leszek Koltunski
    float[] attribs= new float[VERT_ATTRIBS*numVertices];
189 adb2661c Leszek Koltunski
190 a51fe521 Leszek Koltunski
    buildGrid(attribs);
191 adb2661c Leszek Koltunski
192 a51fe521 Leszek Koltunski
    //android.util.Log.e("MeshFlat", "dataLen="+numVertices);
193 6f2d931d Leszek Koltunski
    //android.util.Log.d("MeshFlat", "attribs: "+debug(attribs,VERT_ATTRIBS) );
194 adb2661c Leszek Koltunski
195 a51fe521 Leszek Koltunski
    if( remainingVert!=0 )
196 466450b5 Leszek Koltunski
      android.util.Log.d("MeshFlat", "remainingVert " +remainingVert );
197 adb2661c Leszek Koltunski
198 227b9bca Leszek Koltunski
    setAttribs(attribs);
199 a51fe521 Leszek Koltunski
    }
200
 }