1 |
d333eb6b
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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 |
6a06a912
|
Leszek Koltunski
|
package org.distorted.library;
|
21 |
|
|
|
22 |
|
|
import java.nio.ByteBuffer;
|
23 |
|
|
import java.nio.ByteOrder;
|
24 |
|
|
|
25 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
26 |
e0b6c593
|
Leszek Koltunski
|
/**
|
27 |
05403bba
|
Leszek Koltunski
|
* Create a flat, rectangular grid.
|
28 |
e0b6c593
|
Leszek Koltunski
|
* <p>
|
29 |
|
|
* Perfect if you just want to display a flat Texture. If you are not planning to apply any VERTEX
|
30 |
26df012c
|
Leszek Koltunski
|
* effects to it, use MeshFlat(1,1), i.e. a Quad. Otherwise, create more vertices for more realistic effects!
|
31 |
e0b6c593
|
Leszek Koltunski
|
*/
|
32 |
26df012c
|
Leszek Koltunski
|
public class MeshFlat extends MeshObject
|
33 |
6a06a912
|
Leszek Koltunski
|
{
|
34 |
adb2661c
|
Leszek Koltunski
|
private int mCols, mRows;
|
35 |
|
|
private int remainingVert;
|
36 |
6a06a912
|
Leszek Koltunski
|
|
37 |
7d755851
|
Leszek Koltunski
|
private static float[] mBoundingVert = new float[] { -0.5f,+0.5f,0.0f,
|
38 |
|
|
-0.5f,-0.5f,0.0f,
|
39 |
|
|
+0.5f,+0.5f,0.0f,
|
40 |
|
|
+0.5f,-0.5f,0.0f };
|
41 |
69ed1eb4
|
Leszek Koltunski
|
|
42 |
6a06a912
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
43 |
cacc63de
|
Leszek Koltunski
|
// Create a flat, full grid.
|
44 |
6a06a912
|
Leszek Koltunski
|
|
45 |
adb2661c
|
Leszek Koltunski
|
private void computeNumberOfVertices(int cols, int rows)
|
46 |
|
|
{
|
47 |
79f172ab
|
Leszek Koltunski
|
mRows=rows;
|
48 |
|
|
mCols=cols;
|
49 |
d44ac567
|
Leszek Koltunski
|
|
50 |
79f172ab
|
Leszek Koltunski
|
if( cols==1 && rows==1 )
|
51 |
|
|
{
|
52 |
|
|
dataLength = 4;
|
53 |
|
|
}
|
54 |
|
|
else
|
55 |
adb2661c
|
Leszek Koltunski
|
{
|
56 |
|
|
dataLength = 2*( mRows*mCols +2*mRows - 1) +2*(mCols>=2 ? mRows:0) +
|
57 |
|
|
(mCols>=2 && mRows>=2 ? 2*mRows-2 : 1);
|
58 |
79f172ab
|
Leszek Koltunski
|
}
|
59 |
d44ac567
|
Leszek Koltunski
|
|
60 |
79f172ab
|
Leszek Koltunski
|
//android.util.Log.e("BITMAP","vertices="+dataLength+" rows="+mRows+" cols="+mCols);
|
61 |
d44ac567
|
Leszek Koltunski
|
|
62 |
79f172ab
|
Leszek Koltunski
|
remainingVert = dataLength;
|
63 |
adb2661c
|
Leszek Koltunski
|
}
|
64 |
d44ac567
|
Leszek Koltunski
|
|
65 |
5bf698ee
|
Leszek Koltunski
|
|
66 |
adb2661c
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
67 |
6a06a912
|
Leszek Koltunski
|
|
68 |
69ed1eb4
|
Leszek Koltunski
|
private int addVertex(int vertex, float x, float y, float[] position, float[] normal, float[] texture)
|
69 |
adb2661c
|
Leszek Koltunski
|
{
|
70 |
|
|
remainingVert--;
|
71 |
6a06a912
|
Leszek Koltunski
|
|
72 |
adb2661c
|
Leszek Koltunski
|
position[3*vertex ] = x-0.5f;
|
73 |
|
|
position[3*vertex+1] = 0.5f-y;
|
74 |
|
|
position[3*vertex+2] = 0;
|
75 |
6a06a912
|
Leszek Koltunski
|
|
76 |
adb2661c
|
Leszek Koltunski
|
texture[2*vertex ] = x;
|
77 |
985ea9c5
|
Leszek Koltunski
|
texture[2*vertex+1] = 1.0f-y;
|
78 |
6a06a912
|
Leszek Koltunski
|
|
79 |
adb2661c
|
Leszek Koltunski
|
normal[3*vertex ] = 0.0f;
|
80 |
|
|
normal[3*vertex+1] = 0.0f;
|
81 |
|
|
normal[3*vertex+2] = 1.0f;
|
82 |
6a06a912
|
Leszek Koltunski
|
|
83 |
adb2661c
|
Leszek Koltunski
|
return vertex+1;
|
84 |
|
|
}
|
85 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
86 |
d44ac567
|
Leszek Koltunski
|
|
87 |
adb2661c
|
Leszek Koltunski
|
private int repeatLast(int vertex, float[] position, float[] normal, float[] texture)
|
88 |
|
|
{
|
89 |
|
|
remainingVert--;
|
90 |
6a06a912
|
Leszek Koltunski
|
|
91 |
adb2661c
|
Leszek Koltunski
|
//android.util.Log.e("BITMAP", "repeating last vertex!");
|
92 |
6a06a912
|
Leszek Koltunski
|
|
93 |
adb2661c
|
Leszek Koltunski
|
if( vertex>0 )
|
94 |
|
|
{
|
95 |
|
|
position[3*vertex ] = position[3*vertex-3];
|
96 |
|
|
position[3*vertex+1] = position[3*vertex-2];
|
97 |
|
|
position[3*vertex+2] = position[3*vertex-1];
|
98 |
6a06a912
|
Leszek Koltunski
|
|
99 |
adb2661c
|
Leszek Koltunski
|
normal[3*vertex ] = normal[3*vertex-3];
|
100 |
|
|
normal[3*vertex+1] = normal[3*vertex-2];
|
101 |
|
|
normal[3*vertex+2] = normal[3*vertex-1];
|
102 |
|
|
|
103 |
|
|
texture[2*vertex ] = texture[2*vertex-2];
|
104 |
|
|
texture[2*vertex+1] = texture[2*vertex-1];
|
105 |
d44ac567
|
Leszek Koltunski
|
|
106 |
adb2661c
|
Leszek Koltunski
|
vertex++;
|
107 |
|
|
}
|
108 |
6a06a912
|
Leszek Koltunski
|
|
109 |
adb2661c
|
Leszek Koltunski
|
return vertex;
|
110 |
|
|
}
|
111 |
d44ac567
|
Leszek Koltunski
|
|
112 |
adb2661c
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
113 |
|
|
|
114 |
|
|
private void buildGrid(float[] position, float[] normal, float[] texture)
|
115 |
|
|
{
|
116 |
|
|
boolean lastBlockIsNE = false;
|
117 |
|
|
boolean currentBlockIsNE;
|
118 |
|
|
int vertex = 0;
|
119 |
|
|
|
120 |
69ed1eb4
|
Leszek Koltunski
|
float x,y;
|
121 |
|
|
final float X = 1.0f/mCols;
|
122 |
|
|
final float Y = 1.0f/mRows;
|
123 |
|
|
|
124 |
adb2661c
|
Leszek Koltunski
|
//android.util.Log.d("BITMAP", "buildGrid");
|
125 |
|
|
|
126 |
69ed1eb4
|
Leszek Koltunski
|
y = 0.0f;
|
127 |
|
|
|
128 |
adb2661c
|
Leszek Koltunski
|
for(int row=0; row<mRows; row++)
|
129 |
|
|
{
|
130 |
69ed1eb4
|
Leszek Koltunski
|
x = 0.0f;
|
131 |
|
|
|
132 |
adb2661c
|
Leszek Koltunski
|
for(int col=0; col<mCols; col++)
|
133 |
|
|
{
|
134 |
|
|
currentBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1);
|
135 |
|
|
|
136 |
|
|
if( col==0 || (lastBlockIsNE^currentBlockIsNE) )
|
137 |
|
|
{
|
138 |
|
|
if( row!=0 && col==0 ) vertex = repeatLast(vertex,position,normal,texture);
|
139 |
69ed1eb4
|
Leszek Koltunski
|
vertex= addVertex( vertex, x, y+(currentBlockIsNE?0:Y), position, normal, texture);
|
140 |
adb2661c
|
Leszek Koltunski
|
if( row!=0 && col==0 ) vertex = repeatLast(vertex,position,normal,texture);
|
141 |
|
|
if( lastBlockIsNE^currentBlockIsNE) vertex = repeatLast(vertex,position,normal,texture);
|
142 |
69ed1eb4
|
Leszek Koltunski
|
vertex= addVertex( vertex, x, y+(currentBlockIsNE?Y:0), position, normal, texture);
|
143 |
adb2661c
|
Leszek Koltunski
|
}
|
144 |
69ed1eb4
|
Leszek Koltunski
|
vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?0:Y), position, normal, texture);
|
145 |
|
|
vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?Y:0), position, normal, texture);
|
146 |
adb2661c
|
Leszek Koltunski
|
|
147 |
|
|
lastBlockIsNE = currentBlockIsNE;
|
148 |
69ed1eb4
|
Leszek Koltunski
|
x+=X;
|
149 |
adb2661c
|
Leszek Koltunski
|
}
|
150 |
69ed1eb4
|
Leszek Koltunski
|
|
151 |
|
|
y+=Y;
|
152 |
adb2661c
|
Leszek Koltunski
|
}
|
153 |
|
|
|
154 |
|
|
//android.util.Log.d("BITMAP", "buildGrid done");
|
155 |
|
|
}
|
156 |
|
|
|
157 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
158 |
79f172ab
|
Leszek Koltunski
|
/*
|
159 |
|
|
private static String debug(float[] val, int stop)
|
160 |
|
|
{
|
161 |
|
|
String ret="";
|
162 |
|
|
|
163 |
|
|
for(int i=0; i<val.length; i++)
|
164 |
|
|
{
|
165 |
|
|
if( i%stop==0 ) ret+="\n";
|
166 |
|
|
ret+=(" "+val[i]);
|
167 |
|
|
}
|
168 |
|
|
|
169 |
|
|
return ret;
|
170 |
|
|
}
|
171 |
|
|
*/
|
172 |
11fb6ce0
|
Leszek Koltunski
|
|
173 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
174 |
|
|
|
175 |
69ed1eb4
|
Leszek Koltunski
|
float[] getBoundingVertices()
|
176 |
|
|
{
|
177 |
|
|
return mBoundingVert;
|
178 |
|
|
}
|
179 |
11fb6ce0
|
Leszek Koltunski
|
|
180 |
79f172ab
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
181 |
a56bc359
|
Leszek Koltunski
|
// PUBLIC API
|
182 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
183 |
adb2661c
|
Leszek Koltunski
|
/**
|
184 |
|
|
* Creates the underlying grid of vertices, normals and texture coords.
|
185 |
|
|
*
|
186 |
|
|
* @param cols Number of columns in the grid.
|
187 |
|
|
* @param rows Number of rows in the grid.
|
188 |
|
|
*/
|
189 |
26df012c
|
Leszek Koltunski
|
public MeshFlat(int cols, int rows)
|
190 |
d44ac567
|
Leszek Koltunski
|
{
|
191 |
3ef3364d
|
Leszek Koltunski
|
super(0.0f);
|
192 |
adb2661c
|
Leszek Koltunski
|
computeNumberOfVertices(cols,rows);
|
193 |
|
|
|
194 |
|
|
float[] positionData= new float[POSITION_DATA_SIZE*dataLength];
|
195 |
|
|
float[] normalData = new float[NORMAL_DATA_SIZE *dataLength];
|
196 |
|
|
float[] textureData = new float[TEX_DATA_SIZE *dataLength];
|
197 |
|
|
|
198 |
|
|
buildGrid(positionData,normalData,textureData);
|
199 |
|
|
|
200 |
79f172ab
|
Leszek Koltunski
|
//android.util.Log.e("CUBES","dataLen="+dataLength);
|
201 |
|
|
//android.util.Log.d("CUBES", "position: "+debug(positionData,3) );
|
202 |
|
|
//android.util.Log.d("CUBES", "normal: " +debug( normalData,3) );
|
203 |
|
|
//android.util.Log.d("CUBES", "texture: " +debug( textureData,2) );
|
204 |
adb2661c
|
Leszek Koltunski
|
|
205 |
16d8b8f3
|
Leszek Koltunski
|
if( remainingVert!=0 )
|
206 |
|
|
android.util.Log.d("BITMAP", "remainingVert " +remainingVert );
|
207 |
adb2661c
|
Leszek Koltunski
|
|
208 |
05403bba
|
Leszek Koltunski
|
mMeshPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
209 |
|
|
mMeshPositions.put(positionData).position(0);
|
210 |
adb2661c
|
Leszek Koltunski
|
|
211 |
05403bba
|
Leszek Koltunski
|
mMeshNormals = ByteBuffer.allocateDirect(NORMAL_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
212 |
|
|
mMeshNormals.put(normalData).position(0);
|
213 |
adb2661c
|
Leszek Koltunski
|
|
214 |
05403bba
|
Leszek Koltunski
|
mMeshTexture = ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
215 |
|
|
mMeshTexture.put(textureData).position(0);
|
216 |
d44ac567
|
Leszek Koltunski
|
}
|
217 |
39cbf9dc
|
Leszek Koltunski
|
}
|