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. cols and rows is guaranteed (by DistortedBitmap constructor)
|
34
|
// to be in 1,2,...,256.
|
35
|
|
36
|
private void computeNumberOfVertices(int cols, int rows)
|
37
|
{
|
38
|
mRows=rows;
|
39
|
mCols=cols;
|
40
|
|
41
|
if( cols==1 && rows==1 )
|
42
|
{
|
43
|
dataLength = 4;
|
44
|
}
|
45
|
else
|
46
|
{
|
47
|
dataLength = 2*( mRows*mCols +2*mRows - 1) +2*(mCols>=2 ? mRows:0) +
|
48
|
(mCols>=2 && mRows>=2 ? 2*mRows-2 : 1);
|
49
|
}
|
50
|
|
51
|
//android.util.Log.e("BITMAP","vertices="+dataLength+" rows="+mRows+" cols="+mCols);
|
52
|
|
53
|
remainingVert = dataLength;
|
54
|
}
|
55
|
|
56
|
|
57
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
58
|
|
59
|
private int addVertex(int vertex, int col, int row, float[] position, float[] normal, float[] texture)
|
60
|
{
|
61
|
remainingVert--;
|
62
|
|
63
|
float x= (float)col/mCols;
|
64
|
float y= (float)row/mRows;
|
65
|
|
66
|
position[3*vertex ] = x-0.5f;
|
67
|
position[3*vertex+1] = 0.5f-y;
|
68
|
position[3*vertex+2] = 0;
|
69
|
|
70
|
texture[2*vertex ] = x;
|
71
|
texture[2*vertex+1] = 1.0f-y;
|
72
|
|
73
|
normal[3*vertex ] = 0.0f;
|
74
|
normal[3*vertex+1] = 0.0f;
|
75
|
normal[3*vertex+2] = 1.0f;
|
76
|
|
77
|
return vertex+1;
|
78
|
}
|
79
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
80
|
|
81
|
private int repeatLast(int vertex, float[] position, float[] normal, float[] texture)
|
82
|
{
|
83
|
remainingVert--;
|
84
|
|
85
|
//android.util.Log.e("BITMAP", "repeating last vertex!");
|
86
|
|
87
|
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
|
|
93
|
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
|
|
100
|
vertex++;
|
101
|
}
|
102
|
|
103
|
return vertex;
|
104
|
}
|
105
|
|
106
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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
|
/*
|
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
|
|
157
|
/**
|
158
|
* Creates the underlying grid of vertices, normals and texture coords.
|
159
|
*
|
160
|
* @param cols Number of columns in the grid.
|
161
|
* @param rows Number of rows in the grid.
|
162
|
*/
|
163
|
public DistortedBitmapGrid(int cols, int rows)
|
164
|
{
|
165
|
computeNumberOfVertices(cols,rows);
|
166
|
|
167
|
float[] positionData= new float[POSITION_DATA_SIZE*dataLength];
|
168
|
float[] normalData = new float[NORMAL_DATA_SIZE *dataLength];
|
169
|
float[] textureData = new float[TEX_DATA_SIZE *dataLength];
|
170
|
|
171
|
buildGrid(positionData,normalData,textureData);
|
172
|
|
173
|
//android.util.Log.e("CUBES","dataLen="+dataLength);
|
174
|
//android.util.Log.d("CUBES", "position: "+debug(positionData,3) );
|
175
|
//android.util.Log.d("CUBES", "normal: " +debug( normalData,3) );
|
176
|
//android.util.Log.d("CUBES", "texture: " +debug( textureData,2) );
|
177
|
|
178
|
android.util.Log.d("BITMAP", "remainingVert " +remainingVert );
|
179
|
|
180
|
mGridPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
181
|
mGridPositions.put(positionData).position(0);
|
182
|
|
183
|
mGridNormals = ByteBuffer.allocateDirect(NORMAL_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
184
|
mGridNormals.put(normalData).position(0);
|
185
|
|
186
|
mGridTexture = ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
187
|
mGridTexture.put(textureData).position(0);
|
188
|
}
|
189
|
}
|