Project

General

Profile

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

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

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
    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 
35
    final short[] indexData = new short[dataLength];
36
    
37
    int offset=0;    
38
    for(int y=0; y<yLength-1; y++) 
39
      {
40
      if (y>0) indexData[offset++] = (short) (y*xLength); // Degenerate begin: repeat first vertex
41

    
42
      for (int x = 0; x < xLength; x++) 
43
        {
44
        indexData[offset++] = (short) (( y   *xLength)+x);
45
        indexData[offset++] = (short) (((y+1)*xLength)+x);
46
        }
47

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

    
51
    // for(int g=0; g<dataLength; g++) Log.e(TAG_BACKGROUND, "index["+g+"]="+indexData[g]);
52

    
53
    float[] bufferData= new float[COLOR_DATA_SIZE*dataLength];
54

    
55
    offset=0;
56
    for(int i=0; i<dataLength; i++)
57
      {
58
      bufferData[offset++] = 1.0f; // r
59
      bufferData[offset++] = 1.0f; // g
60
      bufferData[offset++] = 1.0f; // b
61
      bufferData[offset++] = 1.0f; // a
62
      }
63
    mGridColors = ByteBuffer.allocateDirect(COLOR_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();                                                        
64
    mGridColors.put(bufferData).position(0); 
65

    
66
    bufferData = new float[NORMAL_DATA_SIZE*dataLength];
67

    
68
    offset=0;
69
    for(int i=0; i<dataLength; i++)
70
      {
71
      bufferData[offset++] = 0.0f; // x
72
      bufferData[offset++] = 0.0f; // y
73
      bufferData[offset++] = 1.0f; // z
74
      }
75
    mGridNormals = ByteBuffer.allocateDirect(NORMAL_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();                                                        
76
    mGridNormals.put(bufferData).position(0); 
77

    
78
    float tmpx,tmpy,tmpz;
79
    bufferData = new float[TEX_DATA_SIZE*dataLength];
80

    
81
    offset=0;
82
    for(int i=0; i<dataLength; i++)
83
      {
84
      tmpx = ((float)(indexData[offset/2]%xLength))/(xLength-1);
85
      tmpy = ((float)(indexData[offset/2]/xLength))/(yLength-1);
86

    
87
      bufferData[offset++] = tmpx; // s=x
88
      bufferData[offset++] = tmpy; // t=y
89
      }
90
    mGridTexture = ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();                                                        
91
    mGridTexture.put(bufferData).position(0); 
92

    
93
    //for(int g=0; g<dataLength; g++) Log.e(TAG_BACKGROUND, "tex["+g+"]=("+bufferData[2*g]+","+bufferData[2*g+1]+")");
94
    //Log.e(TAG, "regWidth="+(2*mRegW)+" regHeight="+(2*mRegH)+" xLength="+xLength+" yLength="+yLength);
95

    
96
    offset=0;
97
    bufferData= new float[POSITION_DATA_SIZE*dataLength];
98

    
99
    for(int i=0; i<dataLength; i++)
100
      {
101
      tmpx = ((float)(indexData[offset/3]%xLength))/(xLength-1);
102
      tmpy = ((float)(indexData[offset/3]/xLength))/(yLength-1);
103
      tmpz = 0;
104

    
105
      bufferData[offset++] = (tmpx-0.5f); // x
106
      bufferData[offset++] = (0.5f-tmpy); // y
107
      bufferData[offset++] =        tmpz; // z
108
      }
109
    mGridPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();                                                        
110
    mGridPositions.put(bufferData).position(0); 
111

    
112
    //for(int g=0; g<dataLength; g++) android.util.Log.e("BACKGROUND", "pos["+g+"]=("+bufferData[3*g]+","+bufferData[3*g+1]+")");
113
    }
114
  };
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
(3-3/17)