Project

General

Profile

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

library / src / main / java / org / distorted / library / GridFlat.java @ 3ef3364d

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