Revision 12e379d6
Added by Leszek Koltunski almost 5 years ago
src/main/java/org/distorted/library/mesh/MeshFlat.java | ||
---|---|---|
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.mesh; |
|
21 |
|
|
22 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
23 |
/** |
|
24 |
* Create a flat, rectangular grid. |
|
25 |
* <p> |
|
26 |
* Perfect if you just want to display a flat Texture. If you are not planning to apply any VERTEX |
|
27 |
* effects to it, use MeshFlat(1,1), i.e. a Quad. Otherwise, create more vertices for more realistic effects! |
|
28 |
*/ |
|
29 |
public class MeshFlat extends MeshBase |
|
30 |
{ |
|
31 |
private int mCols, mRows; |
|
32 |
private int remainingVert; |
|
33 |
private int numVertices; |
|
34 |
|
|
35 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
36 |
// Create a flat, full grid. |
|
37 |
|
|
38 |
private void computeNumberOfVertices(int cols, int rows) |
|
39 |
{ |
|
40 |
mRows=rows; |
|
41 |
mCols=cols; |
|
42 |
|
|
43 |
if( cols==1 && rows==1 ) |
|
44 |
{ |
|
45 |
numVertices = 4; |
|
46 |
} |
|
47 |
else |
|
48 |
{ |
|
49 |
numVertices = 2*( mRows*mCols +2*mRows - 1) +2*(mCols>=2 ? mRows:0) + |
|
50 |
(mCols>=2 && mRows>=2 ? 2*mRows-2 : 1); |
|
51 |
} |
|
52 |
|
|
53 |
//android.util.Log.e("MeshFlat","vertices="+numVertices+" rows="+mRows+" cols="+mCols); |
|
54 |
|
|
55 |
remainingVert = numVertices; |
|
56 |
} |
|
57 |
|
|
58 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
59 |
|
|
60 |
private int addVertex(int vertex, float x, float y, float[] attribs) |
|
61 |
{ |
|
62 |
remainingVert--; |
|
63 |
|
|
64 |
attribs[VERT_ATTRIBS*vertex + POS_ATTRIB ] = x-0.5f; |
|
65 |
attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+1] = 0.5f-y; |
|
66 |
attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+2] = 0.0f; |
|
67 |
|
|
68 |
attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB ] = 0.0f; |
|
69 |
attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+1] = 0.0f; |
|
70 |
attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+2] = 1.0f; |
|
71 |
|
|
72 |
attribs[VERT_ATTRIBS*vertex + INF_ATTRIB ] = (x-0.5f); |
|
73 |
attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+1] = (0.5f-y); |
|
74 |
attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+2] = 0.01f ; // Inflated surface needs to be slightly in front |
|
75 |
|
|
76 |
attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB ] = x; |
|
77 |
attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB+1] = 1.0f-y; |
|
78 |
|
|
79 |
return vertex+1; |
|
80 |
} |
|
81 |
|
|
82 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
83 |
|
|
84 |
private int repeatLast(int vertex, float[] attribs) |
|
85 |
{ |
|
86 |
//android.util.Log.e("MeshFlat", "repeating last vertex!"); |
|
87 |
|
|
88 |
if( vertex>0 ) |
|
89 |
{ |
|
90 |
remainingVert--; |
|
91 |
|
|
92 |
attribs[VERT_ATTRIBS*vertex + POS_ATTRIB ] = attribs[VERT_ATTRIBS*(vertex-1) + POS_ATTRIB ]; |
|
93 |
attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+1] = attribs[VERT_ATTRIBS*(vertex-1) + POS_ATTRIB+1]; |
|
94 |
attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+2] = attribs[VERT_ATTRIBS*(vertex-1) + POS_ATTRIB+2]; |
|
95 |
|
|
96 |
attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB ] = attribs[VERT_ATTRIBS*(vertex-1) + NOR_ATTRIB ]; |
|
97 |
attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+1] = attribs[VERT_ATTRIBS*(vertex-1) + NOR_ATTRIB+1]; |
|
98 |
attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+2] = attribs[VERT_ATTRIBS*(vertex-1) + NOR_ATTRIB+2]; |
|
99 |
|
|
100 |
attribs[VERT_ATTRIBS*vertex + INF_ATTRIB ] = attribs[VERT_ATTRIBS*(vertex-1) + INF_ATTRIB ]; |
|
101 |
attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+1] = attribs[VERT_ATTRIBS*(vertex-1) + INF_ATTRIB+1]; |
|
102 |
attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+2] = attribs[VERT_ATTRIBS*(vertex-1) + INF_ATTRIB+2]; |
|
103 |
|
|
104 |
attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB ] = attribs[VERT_ATTRIBS*(vertex-1) + TEX_ATTRIB ]; |
|
105 |
attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB+1] = attribs[VERT_ATTRIBS*(vertex-1) + TEX_ATTRIB+1]; |
|
106 |
|
|
107 |
vertex++; |
|
108 |
} |
|
109 |
|
|
110 |
return vertex; |
|
111 |
} |
|
112 |
|
|
113 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
114 |
|
|
115 |
private void buildGrid(float[] attribs) |
|
116 |
{ |
|
117 |
boolean lastBlockIsNE = false; |
|
118 |
boolean currentBlockIsNE; |
|
119 |
int vertex = 0; |
|
120 |
|
|
121 |
float x,y; |
|
122 |
final float X = 1.0f/mCols; |
|
123 |
final float Y = 1.0f/mRows; |
|
124 |
|
|
125 |
//android.util.Log.d("MeshFlat", "buildGrid"); |
|
126 |
|
|
127 |
y = 0.0f; |
|
128 |
|
|
129 |
for(int row=0; row<mRows; row++) |
|
130 |
{ |
|
131 |
x = 0.0f; |
|
132 |
|
|
133 |
for(int col=0; col<mCols; col++) |
|
134 |
{ |
|
135 |
currentBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1); |
|
136 |
|
|
137 |
if( col==0 || (lastBlockIsNE^currentBlockIsNE) ) |
|
138 |
{ |
|
139 |
if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs); |
|
140 |
vertex= addVertex( vertex, x, y+(currentBlockIsNE?0:Y), attribs); |
|
141 |
if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs); |
|
142 |
if( lastBlockIsNE^currentBlockIsNE) vertex = repeatLast(vertex,attribs); |
|
143 |
vertex= addVertex( vertex, x, y+(currentBlockIsNE?Y:0), attribs); |
|
144 |
} |
|
145 |
vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?0:Y), attribs); |
|
146 |
vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?Y:0), attribs); |
|
147 |
|
|
148 |
lastBlockIsNE = currentBlockIsNE; |
|
149 |
x+=X; |
|
150 |
} |
|
151 |
|
|
152 |
y+=Y; |
|
153 |
} |
|
154 |
|
|
155 |
//android.util.Log.d("MeshFlat", "buildGrid done"); |
|
156 |
} |
|
157 |
|
|
158 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
159 |
/* |
|
160 |
private static String debug(float[] val, int stop) |
|
161 |
{ |
|
162 |
String ret=""; |
|
163 |
|
|
164 |
for(int i=0; i<val.length; i++) |
|
165 |
{ |
|
166 |
if( i%stop==0 ) ret+="\n"; |
|
167 |
ret+=(" "+val[i]); |
|
168 |
} |
|
169 |
|
|
170 |
return ret; |
|
171 |
} |
|
172 |
*/ |
|
173 |
|
|
174 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
175 |
// PUBLIC API |
|
176 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
177 |
/** |
|
178 |
* Creates the underlying grid of vertices, normals and texture coords. |
|
179 |
* |
|
180 |
* @param cols Number of columns in the grid. |
|
181 |
* @param rows Number of rows in the grid. |
|
182 |
*/ |
|
183 |
public MeshFlat(int cols, int rows) |
|
184 |
{ |
|
185 |
super(0.0f); |
|
186 |
computeNumberOfVertices(cols,rows); |
|
187 |
|
|
188 |
float[] attribs= new float[VERT_ATTRIBS*numVertices]; |
|
189 |
|
|
190 |
buildGrid(attribs); |
|
191 |
|
|
192 |
//android.util.Log.e("MeshFlat", "dataLen="+numVertices); |
|
193 |
//android.util.Log.d("MeshFlat", "attribs: "+debug(attribs,VERT_ATTRIBS) ); |
|
194 |
|
|
195 |
if( remainingVert!=0 ) |
|
196 |
android.util.Log.d("MeshFlat", "remainingVert " +remainingVert ); |
|
197 |
|
|
198 |
setAttribs(attribs); |
|
199 |
} |
|
200 |
} |
src/main/java/org/distorted/library/mesh/MeshQuad.java | ||
---|---|---|
24 | 24 |
* Create a quad, i.e. two triangles with vertices at (+-0.5,+-0.5). |
25 | 25 |
* <p> |
26 | 26 |
* Mainly to have a simple example showing how to create a Mesh; otherwise a MeshQuad can be perfectly |
27 |
* emulated by a MeshFlat(1,1) or a MeshCubes(1,1,0).
|
|
27 |
* emulated by a MeshRectangles(1,1) or a MeshCubes(1,1,0).
|
|
28 | 28 |
*/ |
29 | 29 |
public class MeshQuad extends MeshBase |
30 | 30 |
{ |
src/main/java/org/distorted/library/mesh/MeshRectangles.java | ||
---|---|---|
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.mesh; |
|
21 |
|
|
22 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
23 |
/** |
|
24 |
* Create a flat, rectangular grid. |
|
25 |
* <p> |
|
26 |
* Perfect if you just want to display a flat Texture. If you are not planning to apply any VERTEX |
|
27 |
* effects to it, use MeshRectangles(1,1), i.e. a Quad. Otherwise, create more vertices for more realistic effects! |
|
28 |
*/ |
|
29 |
public class MeshRectangles extends MeshBase |
|
30 |
{ |
|
31 |
private int mCols, mRows; |
|
32 |
private int remainingVert; |
|
33 |
private int numVertices; |
|
34 |
|
|
35 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
36 |
// Create a flat, full grid. |
|
37 |
|
|
38 |
private void computeNumberOfVertices(int cols, int rows) |
|
39 |
{ |
|
40 |
mRows=rows; |
|
41 |
mCols=cols; |
|
42 |
|
|
43 |
if( cols==1 && rows==1 ) |
|
44 |
{ |
|
45 |
numVertices = 4; |
|
46 |
} |
|
47 |
else |
|
48 |
{ |
|
49 |
numVertices = 2*( mRows*mCols +2*mRows - 1) +2*(mCols>=2 ? mRows:0) + |
|
50 |
(mCols>=2 && mRows>=2 ? 2*mRows-2 : 1); |
|
51 |
} |
|
52 |
|
|
53 |
//android.util.Log.e("MeshRectangles","vertices="+numVertices+" rows="+mRows+" cols="+mCols); |
|
54 |
|
|
55 |
remainingVert = numVertices; |
|
56 |
} |
|
57 |
|
|
58 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
59 |
|
|
60 |
private int addVertex(int vertex, float x, float y, float[] attribs) |
|
61 |
{ |
|
62 |
remainingVert--; |
|
63 |
|
|
64 |
attribs[VERT_ATTRIBS*vertex + POS_ATTRIB ] = x-0.5f; |
|
65 |
attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+1] = 0.5f-y; |
|
66 |
attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+2] = 0.0f; |
|
67 |
|
|
68 |
attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB ] = 0.0f; |
|
69 |
attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+1] = 0.0f; |
|
70 |
attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+2] = 1.0f; |
|
71 |
|
|
72 |
attribs[VERT_ATTRIBS*vertex + INF_ATTRIB ] = (x-0.5f); |
|
73 |
attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+1] = (0.5f-y); |
|
74 |
attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+2] = 0.01f ; // Inflated surface needs to be slightly in front |
|
75 |
|
|
76 |
attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB ] = x; |
|
77 |
attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB+1] = 1.0f-y; |
|
78 |
|
|
79 |
return vertex+1; |
|
80 |
} |
|
81 |
|
|
82 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
83 |
|
|
84 |
private int repeatLast(int vertex, float[] attribs) |
|
85 |
{ |
|
86 |
//android.util.Log.e("MeshRectangles", "repeating last vertex!"); |
|
87 |
|
|
88 |
if( vertex>0 ) |
|
89 |
{ |
|
90 |
remainingVert--; |
|
91 |
|
|
92 |
attribs[VERT_ATTRIBS*vertex + POS_ATTRIB ] = attribs[VERT_ATTRIBS*(vertex-1) + POS_ATTRIB ]; |
|
93 |
attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+1] = attribs[VERT_ATTRIBS*(vertex-1) + POS_ATTRIB+1]; |
|
94 |
attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+2] = attribs[VERT_ATTRIBS*(vertex-1) + POS_ATTRIB+2]; |
|
95 |
|
|
96 |
attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB ] = attribs[VERT_ATTRIBS*(vertex-1) + NOR_ATTRIB ]; |
|
97 |
attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+1] = attribs[VERT_ATTRIBS*(vertex-1) + NOR_ATTRIB+1]; |
|
98 |
attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+2] = attribs[VERT_ATTRIBS*(vertex-1) + NOR_ATTRIB+2]; |
|
99 |
|
|
100 |
attribs[VERT_ATTRIBS*vertex + INF_ATTRIB ] = attribs[VERT_ATTRIBS*(vertex-1) + INF_ATTRIB ]; |
|
101 |
attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+1] = attribs[VERT_ATTRIBS*(vertex-1) + INF_ATTRIB+1]; |
|
102 |
attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+2] = attribs[VERT_ATTRIBS*(vertex-1) + INF_ATTRIB+2]; |
|
103 |
|
|
104 |
attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB ] = attribs[VERT_ATTRIBS*(vertex-1) + TEX_ATTRIB ]; |
|
105 |
attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB+1] = attribs[VERT_ATTRIBS*(vertex-1) + TEX_ATTRIB+1]; |
|
106 |
|
|
107 |
vertex++; |
|
108 |
} |
|
109 |
|
|
110 |
return vertex; |
|
111 |
} |
|
112 |
|
|
113 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
114 |
|
|
115 |
private void buildGrid(float[] attribs) |
|
116 |
{ |
|
117 |
boolean lastBlockIsNE = false; |
|
118 |
boolean currentBlockIsNE; |
|
119 |
int vertex = 0; |
|
120 |
|
|
121 |
float x,y; |
|
122 |
final float X = 1.0f/mCols; |
|
123 |
final float Y = 1.0f/mRows; |
|
124 |
|
|
125 |
//android.util.Log.d("MeshRectangles", "buildGrid"); |
|
126 |
|
|
127 |
y = 0.0f; |
|
128 |
|
|
129 |
for(int row=0; row<mRows; row++) |
|
130 |
{ |
|
131 |
x = 0.0f; |
|
132 |
|
|
133 |
for(int col=0; col<mCols; col++) |
|
134 |
{ |
|
135 |
currentBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1); |
|
136 |
|
|
137 |
if( col==0 || (lastBlockIsNE^currentBlockIsNE) ) |
|
138 |
{ |
|
139 |
if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs); |
|
140 |
vertex= addVertex( vertex, x, y+(currentBlockIsNE?0:Y), attribs); |
|
141 |
if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs); |
|
142 |
if( lastBlockIsNE^currentBlockIsNE) vertex = repeatLast(vertex,attribs); |
|
143 |
vertex= addVertex( vertex, x, y+(currentBlockIsNE?Y:0), attribs); |
|
144 |
} |
|
145 |
vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?0:Y), attribs); |
|
146 |
vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?Y:0), attribs); |
|
147 |
|
|
148 |
lastBlockIsNE = currentBlockIsNE; |
|
149 |
x+=X; |
|
150 |
} |
|
151 |
|
|
152 |
y+=Y; |
|
153 |
} |
|
154 |
|
|
155 |
//android.util.Log.d("MeshRectangles", "buildGrid done"); |
|
156 |
} |
|
157 |
|
|
158 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
159 |
/* |
|
160 |
private static String debug(float[] val, int stop) |
|
161 |
{ |
|
162 |
String ret=""; |
|
163 |
|
|
164 |
for(int i=0; i<val.length; i++) |
|
165 |
{ |
|
166 |
if( i%stop==0 ) ret+="\n"; |
|
167 |
ret+=(" "+val[i]); |
|
168 |
} |
|
169 |
|
|
170 |
return ret; |
|
171 |
} |
|
172 |
*/ |
|
173 |
|
|
174 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
175 |
// PUBLIC API |
|
176 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
177 |
/** |
|
178 |
* Creates the underlying grid of vertices, normals and texture coords. |
|
179 |
* |
|
180 |
* @param cols Number of columns in the grid. |
|
181 |
* @param rows Number of rows in the grid. |
|
182 |
*/ |
|
183 |
public MeshRectangles(int cols, int rows) |
|
184 |
{ |
|
185 |
super(0.0f); |
|
186 |
computeNumberOfVertices(cols,rows); |
|
187 |
|
|
188 |
float[] attribs= new float[VERT_ATTRIBS*numVertices]; |
|
189 |
|
|
190 |
buildGrid(attribs); |
|
191 |
|
|
192 |
//android.util.Log.e("MeshRectangles", "dataLen="+numVertices); |
|
193 |
//android.util.Log.d("MeshRectangles", "attribs: "+debug(attribs,VERT_ATTRIBS) ); |
|
194 |
|
|
195 |
if( remainingVert!=0 ) |
|
196 |
android.util.Log.d("MeshRectangles", "remainingVert " +remainingVert ); |
|
197 |
|
|
198 |
setAttribs(attribs); |
|
199 |
} |
|
200 |
} |
Also available in: Unified diff
Rename MeshFlat MeshRectangles.