Project

General

Profile

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

library / src / main / java / org / distorted / library / MeshFlat.java @ 86782a25

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;
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 MeshObject
33
  {
34
  private int mCols, mRows;
35
  private int remainingVert;
36

    
37
  private static float[] mBoundingVert = new float[] { -0.5f,+0.5f,0.0f,
38
                                                       -0.5f,-0.5f,0.0f,
39
                                                       +0.5f,+0.5f,0.0f,
40
                                                       +0.5f,-0.5f,0.0f };
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43
// Create a flat, full grid.
44

    
45
   private void computeNumberOfVertices(int cols, int rows)
46
     {
47
     mRows=rows;
48
     mCols=cols;
49

    
50
     if( cols==1 && rows==1 )
51
       {
52
       dataLength = 4;
53
       }
54
     else
55
       {
56
       dataLength = 2*( mRows*mCols +2*mRows - 1) +2*(mCols>=2 ? mRows:0) +
57
                    (mCols>=2 && mRows>=2 ? 2*mRows-2 : 1);
58
       }
59

    
60
     //android.util.Log.e("BITMAP","vertices="+dataLength+" rows="+mRows+" cols="+mCols);
61

    
62
     remainingVert = dataLength;
63
     }
64

    
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

    
68
   private int addVertex(int vertex, float x, float y, float[] position, float[] normal, float[] texture)
69
     {
70
     remainingVert--;
71

    
72
     position[3*vertex  ] = x-0.5f;
73
     position[3*vertex+1] = 0.5f-y;
74
     position[3*vertex+2] = 0;
75

    
76
     texture[2*vertex  ]  = x;
77
     texture[2*vertex+1]  = 1.0f-y;
78

    
79
     normal[3*vertex  ]   = 0.0f;
80
     normal[3*vertex+1]   = 0.0f;
81
     normal[3*vertex+2]   = 1.0f;
82

    
83
     return vertex+1;
84
     }
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

    
87
   private int repeatLast(int vertex, float[] position, float[] normal, float[] texture)
88
     {
89
     remainingVert--;
90

    
91
     //android.util.Log.e("BITMAP", "repeating last vertex!");
92

    
93
     if( vertex>0 )
94
       {
95
       position[3*vertex  ] = position[3*vertex-3];
96
       position[3*vertex+1] = position[3*vertex-2];
97
       position[3*vertex+2] = position[3*vertex-1];
98

    
99
       normal[3*vertex  ]   = normal[3*vertex-3];
100
       normal[3*vertex+1]   = normal[3*vertex-2];
101
       normal[3*vertex+2]   = normal[3*vertex-1];
102

    
103
       texture[2*vertex  ]  = texture[2*vertex-2];
104
       texture[2*vertex+1]  = texture[2*vertex-1];
105

    
106
       vertex++;
107
       }
108

    
109
     return vertex;
110
     }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

    
114
   private void buildGrid(float[] position, float[] normal, float[] texture)
115
     {
116
     boolean lastBlockIsNE = false;
117
     boolean currentBlockIsNE;
118
     int vertex = 0;
119

    
120
     float x,y;
121
     final float X = 1.0f/mCols;
122
     final float Y = 1.0f/mRows;
123

    
124
     //android.util.Log.d("BITMAP", "buildGrid");
125

    
126
     y = 0.0f;
127

    
128
     for(int row=0; row<mRows; row++)
129
       {
130
       x = 0.0f;
131

    
132
       for(int col=0; col<mCols; col++)
133
         {
134
         currentBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1);
135

    
136
         if( col==0 || (lastBlockIsNE^currentBlockIsNE) )
137
           {
138
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,position,normal,texture);
139
           vertex= addVertex( vertex, x, y+(currentBlockIsNE?0:Y), position, normal, texture);
140
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,position,normal,texture);
141
           if( lastBlockIsNE^currentBlockIsNE)  vertex = repeatLast(vertex,position,normal,texture);
142
           vertex= addVertex( vertex, x, y+(currentBlockIsNE?Y:0), position, normal, texture);
143
           }
144
         vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?0:Y), position, normal, texture);
145
         vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?Y:0), position, normal, texture);
146

    
147
         lastBlockIsNE = currentBlockIsNE;
148
         x+=X;
149
         }
150

    
151
       y+=Y;
152
       }
153

    
154
     //android.util.Log.d("BITMAP", "buildGrid done");
155
     }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158
/*
159
   private static String debug(float[] val, int stop)
160
     {
161
     String ret="";
162

    
163
     for(int i=0; i<val.length; i++)
164
        {
165
        if( i%stop==0 ) ret+="\n";
166
        ret+=(" "+val[i]);
167
        }
168

    
169
     return ret;
170
     }
171
*/
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

    
175
   float[] getBoundingVertices()
176
     {
177
     return mBoundingVert;
178
     }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181
// PUBLIC API
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183
/**
184
 * Creates the underlying grid of vertices, normals and texture coords.
185
 *
186
 * @param cols Number of columns in the grid.
187
 * @param rows Number of rows in the grid.
188
 */
189
   public MeshFlat(int cols, int rows)
190
      {
191
      super(0.0f);
192
      computeNumberOfVertices(cols,rows);
193

    
194
      float[] positionData= new float[POSITION_DATA_SIZE*dataLength];
195
      float[] normalData  = new float[NORMAL_DATA_SIZE  *dataLength];
196
      float[] textureData = new float[TEX_DATA_SIZE     *dataLength];
197

    
198
      buildGrid(positionData,normalData,textureData);
199

    
200
      //android.util.Log.e("CUBES","dataLen="+dataLength);
201
      //android.util.Log.d("CUBES", "position: "+debug(positionData,3) );
202
      //android.util.Log.d("CUBES", "normal: "  +debug(  normalData,3) );
203
      //android.util.Log.d("CUBES", "texture: " +debug( textureData,2) );
204

    
205
      if( remainingVert!=0 )
206
        android.util.Log.d("BITMAP", "remainingVert " +remainingVert );
207

    
208
      mMeshPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
209
      mMeshPositions.put(positionData).position(0);
210

    
211
      mMeshNormals = ByteBuffer.allocateDirect(NORMAL_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
212
      mMeshNormals.put(normalData).position(0);
213

    
214
      mMeshTexture = ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
215
      mMeshTexture.put(textureData).position(0);
216
      }
217
  }
(22-22/23)