Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedBitmapGrid.java @ 5bf698ee

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

    
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31

    
32
  DistortedBitmapGrid(int xLength, int yLength)
33
    {
34
    float tmpx,tmpy,tmpz;
35
    float[] bufferData;
36

    
37
    dataLength = 2*xLength*(yLength-1)  // (yLength-1) strips, 2*xLength triangles in each,
38
                        +2*(yLength-2); // plus 2 degenerate triangles per each of (yLength-2) joins
39

    
40
    if( xLength>2 )
41
      {
42
      dataLength += 3*(yLength-1); // we change direction of triangles midway through each row (+3 triangles per row)
43
      dataLength +=   (yLength-2); // we also need to change direction when pulling back to the left (+1 extra triangle)
44

    
45
      if( yLength>2 )
46
        dataLength -= 1;           // but if we do a vertical triangle direction shift as well, then one of the 'pull backs'
47
                                   // does not have to add an extra triangle.
48
      }
49

    
50
    ////////////////////////////////////////////////////////////
51
    // vertex indices
52

    
53
    int offset=0;
54
    final short[] indexData = new short[dataLength];
55

    
56
    int changePointX = xLength/2;
57
    int changePointY = yLength/2;
58

    
59
    for(int y=0; y<yLength-1; y++)
60
      {
61
      if( y>0 )
62
        {
63
        if( y<changePointY )
64
          {
65
          indexData[offset++] = (short) ((y+1)*xLength);
66
          if( xLength>2 )
67
            indexData[offset++] = (short) ((y+1)*xLength);
68
          }
69
        else
70
          {
71
          indexData[offset++] = (short) (y*xLength);
72
          if( xLength>2 && y!=changePointY)
73
            indexData[offset++] = (short) (y*xLength);
74
          }
75
        }
76

    
77
      if (y<changePointY)
78
        {
79
        for(int x=0; x<xLength; x++)
80
          {
81
          if( x<=changePointX )
82
            {
83
            indexData[offset++] = (short) (((y+1)*xLength)+x);
84
            indexData[offset++] = (short) (( y   *xLength)+x);
85
            }
86
          else
87
            {
88
            indexData[offset++] = (short) (( y   *xLength)+x);
89
            indexData[offset++] = (short) (((y+1)*xLength)+x);
90
            }
91

    
92
          if( x==changePointX && xLength>2 )
93
            {
94
            indexData[offset++] = (short) (( y   *xLength)+x);
95
            indexData[offset++] = (short) (( y   *xLength)+x);
96
            indexData[offset++] = (short) (((y+1)*xLength)+x);
97
            }
98
          }
99
        }
100
      else
101
        {
102
        for(int x=0; x<xLength; x++)
103
          {
104
          if( x<=changePointX )
105
            {
106
            indexData[offset++] = (short) (( y   *xLength)+x);
107
            indexData[offset++] = (short) (((y+1)*xLength)+x);
108
            }
109
          else
110
            {
111
            indexData[offset++] = (short) (((y+1)*xLength)+x);
112
            indexData[offset++] = (short) (( y   *xLength)+x);
113
            }
114

    
115
          if( x==changePointX && xLength>2 )
116
            {
117
            indexData[offset++] = (short) (((y+1)*xLength)+x);
118
            indexData[offset++] = (short) (((y+1)*xLength)+x);
119
            indexData[offset++] = (short) (( y   *xLength)+x);
120
            }
121
          }
122
        }
123

    
124
      if (y<changePointY && yLength>2 )
125
        {
126
        indexData[offset++] = (short) (((y+1)*xLength) + (xLength-1));
127
        }
128
      else if(y<yLength-2)
129
        {
130
        indexData[offset++] = (short) ((y    *xLength) + (xLength-1));
131
        }
132
      }
133

    
134
     //android.util.Log.e("grid", "xLength="+xLength+" yLength="+yLength);
135
     //for(int g=0; g<dataLength; g++) android.util.Log.e("grid", "index["+g+"]="+indexData[g]);
136

    
137
    ////////////////////////////////////////////////////////////
138
    // texture
139

    
140
    offset=0;
141
    bufferData = new float[TEX_DATA_SIZE*dataLength];
142

    
143
    for(int i=0; i<dataLength; i++)
144
      {
145
      tmpx = ((float)(indexData[i]%xLength))/(xLength-1);
146
      tmpy = ((float)(indexData[i]/xLength))/(yLength-1);
147

    
148
      bufferData[offset++] = tmpx; // s=x
149
      bufferData[offset++] = tmpy; // t=y
150
      }
151
    mGridTexture = ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
152
    mGridTexture.put(bufferData).position(0);
153

    
154
    //for(int g=0; g<dataLength; g++) Log.e("grid", "tex["+g+"]=("+bufferData[2*g]+","+bufferData[2*g+1]+")");
155
    //Log.e("grid", "regWidth="+(2*mRegW)+" regHeight="+(2*mRegH)+" xLength="+xLength+" yLength="+yLength);
156

    
157
    ////////////////////////////////////////////////////////////
158
    // vertex positions
159

    
160
    offset=0;
161
    bufferData= new float[POSITION_DATA_SIZE*dataLength];
162

    
163
    for(int i=0; i<dataLength; i++)
164
      {
165
      tmpx = ((float)(indexData[i]%xLength))/(xLength-1);
166
      tmpy = ((float)(indexData[i]/xLength))/(yLength-1);
167
      tmpz = 0;
168

    
169
      bufferData[offset++] = (tmpx-0.5f); // x
170
      bufferData[offset++] = (0.5f-tmpy); // y
171
      bufferData[offset++] =        tmpz; // z
172
      }
173
    mGridPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
174
    mGridPositions.put(bufferData).position(0);
175

    
176
    //for(int g=0; g<dataLength; g++) android.util.Log.e("grid", "pos["+g+"]=("+bufferData[3*g]+","+bufferData[3*g+1]+")");
177
 
178
    ////////////////////////////////////////////////////////////
179
    // normals
180

    
181
    offset=0;
182
    bufferData = new float[NORMAL_DATA_SIZE*dataLength];
183

    
184
    for(int i=0; i<dataLength; i++)
185
      {
186
      bufferData[offset++] = 0.0f; // x
187
      bufferData[offset++] = 0.0f; // y
188
      bufferData[offset++] = 1.0f; // z
189
      }
190
    mGridNormals = ByteBuffer.allocateDirect(NORMAL_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();                                                        
191
    mGridNormals.put(bufferData).position(0);
192
    }
193
  }
(3-3/18)