Project

General

Profile

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

library / src / main / java / org / distorted / library / GridFlat.java @ e0b6c593

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 6a06a912 Leszek Koltunski
package org.distorted.library;
21
22
import java.nio.ByteBuffer;
23
import java.nio.ByteOrder;
24
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26 e0b6c593 Leszek Koltunski
/**
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 GridFlat(1,1), i.e. a Quad. Otherwise, create more vertices for more realistic effects!
31
 */
32 b455eb48 Leszek Koltunski
public class GridFlat extends GridObject
33 6a06a912 Leszek Koltunski
  {
34 adb2661c Leszek Koltunski
  private int mCols, mRows;
35
  private int remainingVert;
36 6a06a912 Leszek Koltunski
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38 cacc63de Leszek Koltunski
// Create a flat, full grid.
39 6a06a912 Leszek Koltunski
40 adb2661c Leszek Koltunski
   private void computeNumberOfVertices(int cols, int rows)
41
     {
42 79f172ab Leszek Koltunski
     mRows=rows;
43
     mCols=cols;
44 d44ac567 Leszek Koltunski
45 79f172ab Leszek Koltunski
     if( cols==1 && rows==1 )
46
       {
47
       dataLength = 4;
48
       }
49
     else
50 adb2661c Leszek Koltunski
       {
51
       dataLength = 2*( mRows*mCols +2*mRows - 1) +2*(mCols>=2 ? mRows:0) +
52
                    (mCols>=2 && mRows>=2 ? 2*mRows-2 : 1);
53 79f172ab Leszek Koltunski
       }
54 d44ac567 Leszek Koltunski
55 79f172ab Leszek Koltunski
     //android.util.Log.e("BITMAP","vertices="+dataLength+" rows="+mRows+" cols="+mCols);
56 d44ac567 Leszek Koltunski
57 79f172ab Leszek Koltunski
     remainingVert = dataLength;
58 adb2661c Leszek Koltunski
     }
59 d44ac567 Leszek Koltunski
60 5bf698ee Leszek Koltunski
61 adb2661c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
62 6a06a912 Leszek Koltunski
63 adb2661c Leszek Koltunski
   private int addVertex(int vertex, int col, int row, float[] position, float[] normal, float[] texture)
64
     {
65
     remainingVert--;
66 6a06a912 Leszek Koltunski
67 adb2661c Leszek Koltunski
     float x= (float)col/mCols;
68
     float y= (float)row/mRows;
69 6a06a912 Leszek Koltunski
70 adb2661c Leszek Koltunski
     position[3*vertex  ] = x-0.5f;
71
     position[3*vertex+1] = 0.5f-y;
72
     position[3*vertex+2] = 0;
73 6a06a912 Leszek Koltunski
74 adb2661c Leszek Koltunski
     texture[2*vertex  ]  = x;
75 985ea9c5 Leszek Koltunski
     texture[2*vertex+1]  = 1.0f-y;
76 6a06a912 Leszek Koltunski
77 adb2661c Leszek Koltunski
     normal[3*vertex  ]   = 0.0f;
78
     normal[3*vertex+1]   = 0.0f;
79
     normal[3*vertex+2]   = 1.0f;
80 6a06a912 Leszek Koltunski
81 adb2661c Leszek Koltunski
     return vertex+1;
82
     }
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84 d44ac567 Leszek Koltunski
85 adb2661c Leszek Koltunski
   private int repeatLast(int vertex, float[] position, float[] normal, float[] texture)
86
     {
87
     remainingVert--;
88 6a06a912 Leszek Koltunski
89 adb2661c Leszek Koltunski
     //android.util.Log.e("BITMAP", "repeating last vertex!");
90 6a06a912 Leszek Koltunski
91 adb2661c Leszek Koltunski
     if( vertex>0 )
92
       {
93
       position[3*vertex  ] = position[3*vertex-3];
94
       position[3*vertex+1] = position[3*vertex-2];
95
       position[3*vertex+2] = position[3*vertex-1];
96 6a06a912 Leszek Koltunski
97 adb2661c Leszek Koltunski
       normal[3*vertex  ]   = normal[3*vertex-3];
98
       normal[3*vertex+1]   = normal[3*vertex-2];
99
       normal[3*vertex+2]   = normal[3*vertex-1];
100
101
       texture[2*vertex  ]  = texture[2*vertex-2];
102
       texture[2*vertex+1]  = texture[2*vertex-1];
103 d44ac567 Leszek Koltunski
104 adb2661c Leszek Koltunski
       vertex++;
105
       }
106 6a06a912 Leszek Koltunski
107 adb2661c Leszek Koltunski
     return vertex;
108
     }
109 d44ac567 Leszek Koltunski
110 adb2661c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
111
112
   private void buildGrid(float[] position, float[] normal, float[] texture)
113
     {
114
     boolean lastBlockIsNE = false;
115
     boolean currentBlockIsNE;
116
     int vertex = 0;
117
118
     //android.util.Log.d("BITMAP", "buildGrid");
119
120
     for(int row=0; row<mRows; row++)
121
       {
122
       for(int col=0; col<mCols; col++)
123
         {
124
         currentBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1);
125
126
         if( col==0 || (lastBlockIsNE^currentBlockIsNE) )
127
           {
128
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,position,normal,texture);
129
           vertex= addVertex( vertex, col, row+(currentBlockIsNE?0:1), position, normal, texture);
130
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,position,normal,texture);
131
           if( lastBlockIsNE^currentBlockIsNE)  vertex = repeatLast(vertex,position,normal,texture);
132
           vertex= addVertex( vertex, col, row+(currentBlockIsNE?1:0), position, normal, texture);
133
           }
134
         vertex= addVertex( vertex, col+1, row+(currentBlockIsNE?0:1), position, normal, texture);
135
         vertex= addVertex( vertex, col+1, row+(currentBlockIsNE?1:0), position, normal, texture);
136
137
         lastBlockIsNE = currentBlockIsNE;
138
         }
139
       }
140
141
     //android.util.Log.d("BITMAP", "buildGrid done");
142
     }
143
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145 79f172ab Leszek Koltunski
/*
146
   private static String debug(float[] val, int stop)
147
     {
148
     String ret="";
149
150
     for(int i=0; i<val.length; i++)
151
        {
152
        if( i%stop==0 ) ret+="\n";
153
        ret+=(" "+val[i]);
154
        }
155
156
     return ret;
157
     }
158
*/
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160 a56bc359 Leszek Koltunski
// PUBLIC API
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162 adb2661c Leszek Koltunski
/**
163
 * Creates the underlying grid of vertices, normals and texture coords.
164
 *
165
 * @param cols Number of columns in the grid.
166
 * @param rows Number of rows in the grid.
167
 */
168 b455eb48 Leszek Koltunski
   public GridFlat(int cols, int rows)
169 d44ac567 Leszek Koltunski
      {
170 3ef3364d Leszek Koltunski
      super(0.0f);
171 adb2661c Leszek Koltunski
      computeNumberOfVertices(cols,rows);
172
173
      float[] positionData= new float[POSITION_DATA_SIZE*dataLength];
174
      float[] normalData  = new float[NORMAL_DATA_SIZE  *dataLength];
175
      float[] textureData = new float[TEX_DATA_SIZE     *dataLength];
176
177
      buildGrid(positionData,normalData,textureData);
178
179 79f172ab Leszek Koltunski
      //android.util.Log.e("CUBES","dataLen="+dataLength);
180
      //android.util.Log.d("CUBES", "position: "+debug(positionData,3) );
181
      //android.util.Log.d("CUBES", "normal: "  +debug(  normalData,3) );
182
      //android.util.Log.d("CUBES", "texture: " +debug( textureData,2) );
183 adb2661c Leszek Koltunski
184 16d8b8f3 Leszek Koltunski
      if( remainingVert!=0 )
185
        android.util.Log.d("BITMAP", "remainingVert " +remainingVert );
186 adb2661c Leszek Koltunski
187
      mGridPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
188
      mGridPositions.put(positionData).position(0);
189
190
      mGridNormals = ByteBuffer.allocateDirect(NORMAL_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
191
      mGridNormals.put(normalData).position(0);
192
193
      mGridTexture = ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
194
      mGridTexture.put(textureData).position(0);
195 d44ac567 Leszek Koltunski
      }
196 39cbf9dc Leszek Koltunski
  }