Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
package org.distorted.library.mesh;
21

    
22
import java.nio.ByteBuffer;
23
import java.nio.ByteOrder;
24

    
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26
/**
27
 * Create a flat, rectangular grid.
28
 * <p>
29
 * Perfect if you just want to display a flat Texture. If you are not planning to apply any VERTEX
30
 * effects to it, use MeshFlat(1,1), i.e. a Quad. Otherwise, create more vertices for more realistic effects!
31
 */
32
public class MeshFlat extends MeshBase
33
  {
34
  private int mCols, mRows;
35
  private int remainingVert;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38
// Create a flat, full grid.
39

    
40
  private void computeNumberOfVertices(int cols, int rows)
41
     {
42
     mRows=rows;
43
     mCols=cols;
44

    
45
     if( cols==1 && rows==1 )
46
       {
47
       mNumVertices = 4;
48
       }
49
     else
50
       {
51
       mNumVertices = 2*( mRows*mCols +2*mRows - 1) +2*(mCols>=2 ? mRows:0) +
52
                     (mCols>=2 && mRows>=2 ? 2*mRows-2 : 1);
53
       }
54

    
55
     //android.util.Log.e("MeshFlat","vertices="+numVertices+" rows="+mRows+" cols="+mCols);
56

    
57
     remainingVert = mNumVertices;
58
     }
59

    
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
  private int addVertex(int vertex, float x, float y, float[] attribs)
64
     {
65
     remainingVert--;
66

    
67
     attribs[8*vertex  ] = x-0.5f;
68
     attribs[8*vertex+1] = 0.5f-y;
69
     attribs[8*vertex+2] = 0;
70

    
71
     attribs[8*vertex+3] = 0.0f;
72
     attribs[8*vertex+4] = 0.0f;
73
     attribs[8*vertex+5] = 1.0f;
74

    
75
     attribs[8*vertex+6] = x;
76
     attribs[8*vertex+7] = 1.0f-y;
77

    
78
     return vertex+1;
79
     }
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

    
83
  private int repeatLast(int vertex, float[] attribs)
84
     {
85
     remainingVert--;
86

    
87
     //android.util.Log.e("MeshFlat", "repeating last vertex!");
88

    
89
     if( vertex>0 )
90
       {
91
       attribs[8*vertex  ] = attribs[8*vertex-8];
92
       attribs[8*vertex+1] = attribs[8*vertex-7];
93
       attribs[8*vertex+2] = attribs[8*vertex-6];
94
       attribs[8*vertex+3] = attribs[8*vertex-5];
95
       attribs[8*vertex+4] = attribs[8*vertex-4];
96
       attribs[8*vertex+5] = attribs[8*vertex-3];
97
       attribs[8*vertex+6] = attribs[8*vertex-2];
98
       attribs[8*vertex+7] = attribs[8*vertex-1];
99

    
100
       vertex++;
101
       }
102

    
103
     return vertex;
104
     }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

    
108
  private void buildGrid(float[] attribs)
109
     {
110
     boolean lastBlockIsNE = false;
111
     boolean currentBlockIsNE;
112
     int vertex = 0;
113

    
114
     float x,y;
115
     final float X = 1.0f/mCols;
116
     final float Y = 1.0f/mRows;
117

    
118
     //android.util.Log.d("MeshFlat", "buildGrid");
119

    
120
     y = 0.0f;
121

    
122
     for(int row=0; row<mRows; row++)
123
       {
124
       x = 0.0f;
125

    
126
       for(int col=0; col<mCols; col++)
127
         {
128
         currentBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1);
129

    
130
         if( col==0 || (lastBlockIsNE^currentBlockIsNE) )
131
           {
132
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs);
133
           vertex= addVertex( vertex, x, y+(currentBlockIsNE?0:Y), attribs);
134
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs);
135
           if( lastBlockIsNE^currentBlockIsNE)  vertex = repeatLast(vertex,attribs);
136
           vertex= addVertex( vertex, x, y+(currentBlockIsNE?Y:0), attribs);
137
           }
138
         vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?0:Y), attribs);
139
         vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?Y:0), attribs);
140

    
141
         lastBlockIsNE = currentBlockIsNE;
142
         x+=X;
143
         }
144

    
145
       y+=Y;
146
       }
147

    
148
     //android.util.Log.d("MeshFlat", "buildGrid done");
149
     }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152
/*
153
  private static String debug(float[] val, int stop)
154
     {
155
     String ret="";
156

    
157
     for(int i=0; i<val.length; i++)
158
        {
159
        if( i%stop==0 ) ret+="\n";
160
        ret+=(" "+val[i]);
161
        }
162

    
163
     return ret;
164
     }
165
*/
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168
// PUBLIC API
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170
/**
171
 * Creates the underlying grid of vertices, normals and texture coords.
172
 *
173
 * @param cols Number of columns in the grid.
174
 * @param rows Number of rows in the grid.
175
 */
176
 public MeshFlat(int cols, int rows)
177
    {
178
    super(0.0f);
179
    computeNumberOfVertices(cols,rows);
180

    
181
    float[] attribs= new float[(POS_DATA_SIZE+NOR_DATA_SIZE+TEX_DATA_SIZE)*mNumVertices];
182

    
183
    buildGrid(attribs);
184

    
185
    //android.util.Log.e("MeshFlat", "dataLen="+numVertices);
186
    //android.util.Log.d("MeshFlat", "attribs: "+debug(attribs,8) );
187

    
188
    if( remainingVert!=0 )
189
      android.util.Log.d("MeshFlat", "remainingVert " +remainingVert );
190

    
191
    mVertAttribs = ByteBuffer.allocateDirect(mNumVertices*VERTSIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
192
    mVertAttribs.put(attribs).position(0);
193

    
194
    setData(mNumVertices, mVertAttribs);
195
    }
196
 }
(3-3/3)