Project

General

Profile

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

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

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)+2*(yLength-2); // (yLength-1) strips, 2*xLength triangles in each, plus 2 degenerate triangles per each of (yLength-2) joins 
38

    
39
    ////////////////////////////////////////////////////////////
40
    // vertex indices
41

    
42
    int offset=0;
43
    final short[] indexData = new short[dataLength];
44

    
45
    for(int y=0; y<yLength-1; y++)
46
      {
47
      if (y>0) indexData[offset++] = (short) (y*xLength); // Degenerate begin: repeat first vertex
48

    
49
      for (int x = 0; x < xLength; x++)
50
        {
51
        indexData[offset++] = (short) (( y   *xLength)+x);
52
        indexData[offset++] = (short) (((y+1)*xLength)+x);
53
        }
54

    
55
      if (y<yLength-2) indexData[offset++] = (short) (((y+1)*xLength) + (xLength-1)); // Degenerate end: repeat last vertex
56
      }
57

    
58
    // for(int g=0; g<dataLength; g++) Log.e("grid", "index["+g+"]="+indexData[g]);
59

    
60
    ////////////////////////////////////////////////////////////
61
    // texture
62

    
63
    offset=0;
64
    bufferData = new float[TEX_DATA_SIZE*dataLength];
65

    
66
    for(int i=0; i<dataLength; i++)
67
      {
68
      tmpx = ((float)(indexData[offset/2]%xLength))/(xLength-1);
69
      tmpy = ((float)(indexData[offset/2]/xLength))/(yLength-1);
70

    
71
      bufferData[offset++] = tmpx; // s=x
72
      bufferData[offset++] = tmpy; // t=y
73
      }
74
    mGridTexture = ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
75
    mGridTexture.put(bufferData).position(0);
76

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

    
80
    ////////////////////////////////////////////////////////////
81
    // vertex positions
82

    
83
    offset=0;
84
    bufferData= new float[POSITION_DATA_SIZE*dataLength];
85

    
86
    for(int i=0; i<dataLength; i++)
87
      {
88
      tmpx = ((float)(indexData[offset/3]%xLength))/(xLength-1);
89
      tmpy = ((float)(indexData[offset/3]/xLength))/(yLength-1);
90
      tmpz = 0;
91

    
92
      bufferData[offset++] = (tmpx-0.5f); // x
93
      bufferData[offset++] = (0.5f-tmpy); // y
94
      bufferData[offset++] =        tmpz; // z
95
      }
96
    mGridPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
97
    mGridPositions.put(bufferData).position(0);
98

    
99
    //for(int g=0; g<dataLength; g++) android.util.Log.e("grid", "pos["+g+"]=("+bufferData[3*g]+","+bufferData[3*g+1]+")");
100
 
101
    ////////////////////////////////////////////////////////////
102
    // normals
103

    
104
    offset=0;
105
    bufferData = new float[NORMAL_DATA_SIZE*dataLength];
106

    
107
    for(int i=0; i<dataLength; i++)
108
      {
109
      bufferData[offset++] = 0.0f; // x
110
      bufferData[offset++] = 0.0f; // y
111
      bufferData[offset++] = 1.0f; // z
112
      }
113
    mGridNormals = ByteBuffer.allocateDirect(NORMAL_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();                                                        
114
    mGridNormals.put(bufferData).position(0);
115
    }
116
  }
(3-3/18)