Project

General

Profile

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

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

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