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 |
fe82a979
|
Leszek Koltunski
|
package org.distorted.library.main;
|
21 |
6a06a912
|
Leszek Koltunski
|
|
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 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
38 |
cacc63de
|
Leszek Koltunski
|
// Create a flat, full grid.
|
39 |
6a06a912
|
Leszek Koltunski
|
|
40 |
a51fe521
|
Leszek Koltunski
|
private void computeNumberOfVertices(int cols, int rows)
|
41 |
adb2661c
|
Leszek Koltunski
|
{
|
42 |
79f172ab
|
Leszek Koltunski
|
mRows=rows;
|
43 |
|
|
mCols=cols;
|
44 |
d44ac567
|
Leszek Koltunski
|
|
45 |
79f172ab
|
Leszek Koltunski
|
if( cols==1 && rows==1 )
|
46 |
|
|
{
|
47 |
a51fe521
|
Leszek Koltunski
|
numVertices = 4;
|
48 |
79f172ab
|
Leszek Koltunski
|
}
|
49 |
|
|
else
|
50 |
adb2661c
|
Leszek Koltunski
|
{
|
51 |
a51fe521
|
Leszek Koltunski
|
numVertices = 2*( mRows*mCols +2*mRows - 1) +2*(mCols>=2 ? mRows:0) +
|
52 |
|
|
(mCols>=2 && mRows>=2 ? 2*mRows-2 : 1);
|
53 |
79f172ab
|
Leszek Koltunski
|
}
|
54 |
d44ac567
|
Leszek Koltunski
|
|
55 |
a51fe521
|
Leszek Koltunski
|
//android.util.Log.e("BITMAP","vertices="+numVertices+" rows="+mRows+" cols="+mCols);
|
56 |
d44ac567
|
Leszek Koltunski
|
|
57 |
a51fe521
|
Leszek Koltunski
|
remainingVert = numVertices;
|
58 |
adb2661c
|
Leszek Koltunski
|
}
|
59 |
d44ac567
|
Leszek Koltunski
|
|
60 |
5bf698ee
|
Leszek Koltunski
|
|
61 |
adb2661c
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
62 |
6a06a912
|
Leszek Koltunski
|
|
63 |
a51fe521
|
Leszek Koltunski
|
private int addVertex(int vertex, float x, float y, float[] attribs)
|
64 |
adb2661c
|
Leszek Koltunski
|
{
|
65 |
|
|
remainingVert--;
|
66 |
6a06a912
|
Leszek Koltunski
|
|
67 |
a51fe521
|
Leszek Koltunski
|
attribs[8*vertex ] = x-0.5f;
|
68 |
|
|
attribs[8*vertex+1] = 0.5f-y;
|
69 |
|
|
attribs[8*vertex+2] = 0;
|
70 |
6a06a912
|
Leszek Koltunski
|
|
71 |
a51fe521
|
Leszek Koltunski
|
attribs[8*vertex+3] = 0.0f;
|
72 |
|
|
attribs[8*vertex+4] = 0.0f;
|
73 |
|
|
attribs[8*vertex+5] = 1.0f;
|
74 |
6a06a912
|
Leszek Koltunski
|
|
75 |
a51fe521
|
Leszek Koltunski
|
attribs[8*vertex+6] = x;
|
76 |
|
|
attribs[8*vertex+7] = 1.0f-y;
|
77 |
6a06a912
|
Leszek Koltunski
|
|
78 |
adb2661c
|
Leszek Koltunski
|
return vertex+1;
|
79 |
|
|
}
|
80 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
81 |
d44ac567
|
Leszek Koltunski
|
|
82 |
a51fe521
|
Leszek Koltunski
|
private int repeatLast(int vertex, float[] attribs)
|
83 |
adb2661c
|
Leszek Koltunski
|
{
|
84 |
|
|
remainingVert--;
|
85 |
6a06a912
|
Leszek Koltunski
|
|
86 |
adb2661c
|
Leszek Koltunski
|
//android.util.Log.e("BITMAP", "repeating last vertex!");
|
87 |
6a06a912
|
Leszek Koltunski
|
|
88 |
adb2661c
|
Leszek Koltunski
|
if( vertex>0 )
|
89 |
|
|
{
|
90 |
a51fe521
|
Leszek Koltunski
|
attribs[8*vertex ] = attribs[8*vertex-8];
|
91 |
|
|
attribs[8*vertex+1] = attribs[8*vertex-7];
|
92 |
|
|
attribs[8*vertex+2] = attribs[8*vertex-6];
|
93 |
|
|
attribs[8*vertex+3] = attribs[8*vertex-5];
|
94 |
|
|
attribs[8*vertex+4] = attribs[8*vertex-4];
|
95 |
|
|
attribs[8*vertex+5] = attribs[8*vertex-3];
|
96 |
|
|
attribs[8*vertex+6] = attribs[8*vertex-2];
|
97 |
|
|
attribs[8*vertex+7] = attribs[8*vertex-1];
|
98 |
d44ac567
|
Leszek Koltunski
|
|
99 |
adb2661c
|
Leszek Koltunski
|
vertex++;
|
100 |
|
|
}
|
101 |
6a06a912
|
Leszek Koltunski
|
|
102 |
adb2661c
|
Leszek Koltunski
|
return vertex;
|
103 |
|
|
}
|
104 |
d44ac567
|
Leszek Koltunski
|
|
105 |
adb2661c
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
106 |
|
|
|
107 |
a51fe521
|
Leszek Koltunski
|
private void buildGrid(float[] attribs)
|
108 |
adb2661c
|
Leszek Koltunski
|
{
|
109 |
|
|
boolean lastBlockIsNE = false;
|
110 |
|
|
boolean currentBlockIsNE;
|
111 |
|
|
int vertex = 0;
|
112 |
|
|
|
113 |
69ed1eb4
|
Leszek Koltunski
|
float x,y;
|
114 |
|
|
final float X = 1.0f/mCols;
|
115 |
|
|
final float Y = 1.0f/mRows;
|
116 |
|
|
|
117 |
adb2661c
|
Leszek Koltunski
|
//android.util.Log.d("BITMAP", "buildGrid");
|
118 |
|
|
|
119 |
69ed1eb4
|
Leszek Koltunski
|
y = 0.0f;
|
120 |
|
|
|
121 |
adb2661c
|
Leszek Koltunski
|
for(int row=0; row<mRows; row++)
|
122 |
|
|
{
|
123 |
69ed1eb4
|
Leszek Koltunski
|
x = 0.0f;
|
124 |
|
|
|
125 |
adb2661c
|
Leszek Koltunski
|
for(int col=0; col<mCols; col++)
|
126 |
|
|
{
|
127 |
|
|
currentBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1);
|
128 |
|
|
|
129 |
|
|
if( col==0 || (lastBlockIsNE^currentBlockIsNE) )
|
130 |
|
|
{
|
131 |
a51fe521
|
Leszek Koltunski
|
if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs);
|
132 |
|
|
vertex= addVertex( vertex, x, y+(currentBlockIsNE?0:Y), attribs);
|
133 |
|
|
if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs);
|
134 |
|
|
if( lastBlockIsNE^currentBlockIsNE) vertex = repeatLast(vertex,attribs);
|
135 |
|
|
vertex= addVertex( vertex, x, y+(currentBlockIsNE?Y:0), attribs);
|
136 |
adb2661c
|
Leszek Koltunski
|
}
|
137 |
a51fe521
|
Leszek Koltunski
|
vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?0:Y), attribs);
|
138 |
|
|
vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?Y:0), attribs);
|
139 |
adb2661c
|
Leszek Koltunski
|
|
140 |
|
|
lastBlockIsNE = currentBlockIsNE;
|
141 |
69ed1eb4
|
Leszek Koltunski
|
x+=X;
|
142 |
adb2661c
|
Leszek Koltunski
|
}
|
143 |
69ed1eb4
|
Leszek Koltunski
|
|
144 |
|
|
y+=Y;
|
145 |
adb2661c
|
Leszek Koltunski
|
}
|
146 |
|
|
|
147 |
|
|
//android.util.Log.d("BITMAP", "buildGrid done");
|
148 |
|
|
}
|
149 |
|
|
|
150 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
151 |
79f172ab
|
Leszek Koltunski
|
/*
|
152 |
a51fe521
|
Leszek Koltunski
|
private static String debug(float[] val, int stop)
|
153 |
79f172ab
|
Leszek Koltunski
|
{
|
154 |
|
|
String ret="";
|
155 |
|
|
|
156 |
|
|
for(int i=0; i<val.length; i++)
|
157 |
|
|
{
|
158 |
|
|
if( i%stop==0 ) ret+="\n";
|
159 |
|
|
ret+=(" "+val[i]);
|
160 |
|
|
}
|
161 |
|
|
|
162 |
|
|
return ret;
|
163 |
|
|
}
|
164 |
|
|
*/
|
165 |
11fb6ce0
|
Leszek Koltunski
|
|
166 |
79f172ab
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
167 |
a56bc359
|
Leszek Koltunski
|
// PUBLIC API
|
168 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
169 |
adb2661c
|
Leszek Koltunski
|
/**
|
170 |
|
|
* Creates the underlying grid of vertices, normals and texture coords.
|
171 |
|
|
*
|
172 |
|
|
* @param cols Number of columns in the grid.
|
173 |
|
|
* @param rows Number of rows in the grid.
|
174 |
|
|
*/
|
175 |
a51fe521
|
Leszek Koltunski
|
public MeshFlat(int cols, int rows)
|
176 |
|
|
{
|
177 |
|
|
super(0.0f);
|
178 |
|
|
computeNumberOfVertices(cols,rows);
|
179 |
adb2661c
|
Leszek Koltunski
|
|
180 |
cab7c165
|
Leszek Koltunski
|
float[] attribs= new float[(POS_DATA_SIZE+NOR_DATA_SIZE+TEX_DATA_SIZE)*numVertices];
|
181 |
adb2661c
|
Leszek Koltunski
|
|
182 |
a51fe521
|
Leszek Koltunski
|
buildGrid(attribs);
|
183 |
adb2661c
|
Leszek Koltunski
|
|
184 |
a51fe521
|
Leszek Koltunski
|
//android.util.Log.e("MeshFlat", "dataLen="+numVertices);
|
185 |
|
|
//android.util.Log.d("MeshFlat", "attribs: "+debug(attribs,8) );
|
186 |
adb2661c
|
Leszek Koltunski
|
|
187 |
a51fe521
|
Leszek Koltunski
|
if( remainingVert!=0 )
|
188 |
|
|
android.util.Log.d("BITMAP", "remainingVert " +remainingVert );
|
189 |
adb2661c
|
Leszek Koltunski
|
|
190 |
a51fe521
|
Leszek Koltunski
|
mVertAttribs = ByteBuffer.allocateDirect(numVertices*VERTSIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
191 |
|
|
mVertAttribs.put(attribs).position(0);
|
192 |
|
|
}
|
193 |
|
|
}
|