Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedBitmapGrid.java @ adb2661c

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
class DistortedBitmapGrid extends DistortedObjectGrid
28
  {
29
  private int mCols, mRows;
30
  private int remainingVert;
31

    
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33
// create a flat, full grid
34

    
35
   private void computeNumberOfVertices(int cols, int rows)
36
     {
37
     //android.util.Log.e("BITMAP","preparing data structures, cols="+cols+" rows="+rows);
38

    
39
     mRows     =0;
40
     mCols     =0;
41
     dataLength=0;
42

    
43
     if( cols>0 )
44
       {
45
       mCols = cols;
46
       mRows = rows;
47

    
48
       dataLength = 2*( mRows*mCols +2*mRows - 1) +2*(mCols>=2 ? mRows:0) +
49
                    (mCols>=2 && mRows>=2 ? 2*mRows-2 : 1);
50

    
51
       //android.util.Log.e("BITMAP","vertices="+dataLength);
52

    
53
       remainingVert = dataLength;
54
       }
55
     }
56

    
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
   private int addVertex(int vertex, int col, int row, float[] position, float[] normal, float[] texture)
61
     {
62
     remainingVert--;
63

    
64
     float x= (float)col/mCols;
65
     float y= (float)row/mRows;
66

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

    
71
     texture[2*vertex  ]  = x;
72
     texture[2*vertex+1]  = y;
73

    
74
     normal[3*vertex  ]   = 0.0f;
75
     normal[3*vertex+1]   = 0.0f;
76
     normal[3*vertex+2]   = 1.0f;
77

    
78
     return vertex+1;
79
     }
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

    
82
   private int repeatLast(int vertex, float[] position, float[] normal, float[] texture)
83
     {
84
     remainingVert--;
85

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

    
88
     if( vertex>0 )
89
       {
90
       position[3*vertex  ] = position[3*vertex-3];
91
       position[3*vertex+1] = position[3*vertex-2];
92
       position[3*vertex+2] = position[3*vertex-1];
93

    
94
       normal[3*vertex  ]   = normal[3*vertex-3];
95
       normal[3*vertex+1]   = normal[3*vertex-2];
96
       normal[3*vertex+2]   = normal[3*vertex-1];
97

    
98
       texture[2*vertex  ]  = texture[2*vertex-2];
99
       texture[2*vertex+1]  = texture[2*vertex-1];
100

    
101
       vertex++;
102
       }
103

    
104
     return vertex;
105
     }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

    
109
   private void buildGrid(float[] position, float[] normal, float[] texture)
110
     {
111
     boolean lastBlockIsNE = false;
112
     boolean currentBlockIsNE;
113
     int vertex = 0;
114

    
115
     //android.util.Log.d("BITMAP", "buildGrid");
116

    
117
     for(int row=0; row<mRows; row++)
118
       {
119
       for(int col=0; col<mCols; col++)
120
         {
121
         currentBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1);
122

    
123
         if( col==0 || (lastBlockIsNE^currentBlockIsNE) )
124
           {
125
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,position,normal,texture);
126
           vertex= addVertex( vertex, col, row+(currentBlockIsNE?0:1), position, normal, texture);
127
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,position,normal,texture);
128
           if( lastBlockIsNE^currentBlockIsNE)  vertex = repeatLast(vertex,position,normal,texture);
129
           vertex= addVertex( vertex, col, row+(currentBlockIsNE?1:0), position, normal, texture);
130
           }
131
         vertex= addVertex( vertex, col+1, row+(currentBlockIsNE?0:1), position, normal, texture);
132
         vertex= addVertex( vertex, col+1, row+(currentBlockIsNE?1:0), position, normal, texture);
133

    
134
         lastBlockIsNE = currentBlockIsNE;
135
         }
136
       }
137

    
138
     //android.util.Log.d("BITMAP", "buildGrid done");
139
     }
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142

    
143
/**
144
 * Creates the underlying grid of vertices, normals and texture coords.
145
 *
146
 * @param cols Number of columns in the grid.
147
 * @param rows Number of rows in the grid.
148
 */
149
   public DistortedBitmapGrid(int cols, int rows)
150
      {
151
      computeNumberOfVertices(cols,rows);
152

    
153
      float[] positionData= new float[POSITION_DATA_SIZE*dataLength];
154
      float[] normalData  = new float[NORMAL_DATA_SIZE  *dataLength];
155
      float[] textureData = new float[TEX_DATA_SIZE     *dataLength];
156

    
157
      buildGrid(positionData,normalData,textureData);
158

    
159
      /*
160
      android.util.Log.e("CUBES","dataLen="+dataLength+" vertex="+numVertices);
161
      android.util.Log.d("CUBES", "position: "+debug(positionData,3) );
162
      android.util.Log.d("CUBES", "normal: "  +debug(  normalData,3) );
163
      android.util.Log.d("CUBES", "texture: " +debug( textureData,2) );
164
      */
165

    
166
      android.util.Log.d("BITMAP", "remainingVert " +remainingVert );
167

    
168
      mGridPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
169
      mGridPositions.put(positionData).position(0);
170

    
171
      mGridNormals = ByteBuffer.allocateDirect(NORMAL_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
172
      mGridNormals.put(normalData).position(0);
173

    
174
      mGridTexture = ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
175
      mGridTexture.put(textureData).position(0);
176
      }
177
  }
(3-3/18)