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
|
import android.opengl.GLES31;
|
23
|
|
24
|
import org.distorted.library.main.Distorted;
|
25
|
import org.distorted.library.main.DistortedBuffer;
|
26
|
import org.distorted.library.program.DistortedProgram;
|
27
|
|
28
|
import java.nio.ByteBuffer;
|
29
|
import java.nio.ByteOrder;
|
30
|
import java.nio.FloatBuffer;
|
31
|
|
32
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
33
|
/**
|
34
|
* Abstract class which represents a Mesh, ie an array of vertices (rendered as a TRIANGLE_STRIP).
|
35
|
* <p>
|
36
|
* If you want to render to a particular shape, extend from here, construct a float array
|
37
|
* containing per-vertex attributes, and call back setAttribs().
|
38
|
*/
|
39
|
public abstract class MeshBase
|
40
|
{
|
41
|
// sizes of attributes of an individual vertex.
|
42
|
private static final int POS_DATA_SIZE= 3; // vertex coordinates: x,y,z
|
43
|
private static final int NOR_DATA_SIZE= 3; // normal vector: x,y,z
|
44
|
private static final int INF_DATA_SIZE= 3; // 'inflate' vector: x,y,z
|
45
|
private static final int TEX_DATA_SIZE= 2; // texture coordinates: s,t
|
46
|
|
47
|
static final int POS_ATTRIB = 0;
|
48
|
static final int NOR_ATTRIB = POS_DATA_SIZE;
|
49
|
static final int INF_ATTRIB = POS_DATA_SIZE + NOR_DATA_SIZE;
|
50
|
static final int TEX_ATTRIB = POS_DATA_SIZE + NOR_DATA_SIZE + INF_DATA_SIZE;
|
51
|
static final int VERT_ATTRIBS = POS_DATA_SIZE + NOR_DATA_SIZE + INF_DATA_SIZE + TEX_DATA_SIZE; // number of attributes of a 'normal' vertex
|
52
|
static final int TRAN_ATTRIBS = POS_DATA_SIZE + POS_DATA_SIZE; // number of attributes of a transform feedback vertex
|
53
|
|
54
|
private static final int BYTES_PER_FLOAT = 4;
|
55
|
|
56
|
private static final int OFFSET_POS = POS_ATTRIB*BYTES_PER_FLOAT;
|
57
|
private static final int OFFSET_NOR = NOR_ATTRIB*BYTES_PER_FLOAT;
|
58
|
private static final int OFFSET_INF = INF_ATTRIB*BYTES_PER_FLOAT;
|
59
|
private static final int OFFSET_TEX = TEX_ATTRIB*BYTES_PER_FLOAT;
|
60
|
private static final int TRAN_SIZE = TRAN_ATTRIBS*BYTES_PER_FLOAT;
|
61
|
private static final int VERT_SIZE = VERT_ATTRIBS*BYTES_PER_FLOAT;
|
62
|
|
63
|
private boolean mShowNormals; // when rendering this mesh, draw normal vectors?
|
64
|
private DistortedBuffer mVBO, mTFO; // main vertex buffer and transform feedback buffer
|
65
|
private final float zFactor; // strange workaround for the fact that we need to somehow store the 'depth'
|
66
|
// of the Mesh. Used in DistortedEffects. See DistortedTexture.getDepth().
|
67
|
private int mNumVertices;
|
68
|
private FloatBuffer mVertAttribs; // packed: PosX,PosY,PosZ, NorX,NorY,NorZ, InfX,InfY,InfZ, TexS,TexT
|
69
|
private float mInflate;
|
70
|
|
71
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
72
|
|
73
|
MeshBase(float factor)
|
74
|
{
|
75
|
zFactor = factor;
|
76
|
mShowNormals = false;
|
77
|
|
78
|
mVBO = new DistortedBuffer(GLES31.GL_ARRAY_BUFFER , GLES31.GL_STATIC_READ);
|
79
|
mTFO = new DistortedBuffer(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, GLES31.GL_STATIC_READ);
|
80
|
}
|
81
|
|
82
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
83
|
// when a derived class is done computing its mesh, it has to call this method.
|
84
|
|
85
|
void setAttribs(float[] vertexAttribs)
|
86
|
{
|
87
|
mNumVertices = vertexAttribs.length/VERT_ATTRIBS;
|
88
|
|
89
|
mVertAttribs = ByteBuffer.allocateDirect(mNumVertices*VERT_SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
90
|
mVertAttribs.put(vertexAttribs).position(0);
|
91
|
|
92
|
mVBO.setData(mNumVertices*VERT_SIZE, mVertAttribs);
|
93
|
mTFO.setData(mNumVertices*TRAN_SIZE, null );
|
94
|
}
|
95
|
|
96
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
97
|
/**
|
98
|
* Not part of public API, do not document (public only because has to be used from the main package)
|
99
|
*
|
100
|
* @y.exclude
|
101
|
*/
|
102
|
public int getTFO()
|
103
|
{
|
104
|
return mTFO.mIndex[0];
|
105
|
}
|
106
|
|
107
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
108
|
/**
|
109
|
* Not part of public API, do not document (public only because has to be used from the main package)
|
110
|
*
|
111
|
* @y.exclude
|
112
|
*/
|
113
|
public int getNumVertices()
|
114
|
{
|
115
|
return mNumVertices;
|
116
|
}
|
117
|
|
118
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
119
|
/**
|
120
|
* Not part of public API, do not document (public only because has to be used from the main package)
|
121
|
*
|
122
|
* @y.exclude
|
123
|
*/
|
124
|
public float getZFactor()
|
125
|
{
|
126
|
return zFactor;
|
127
|
}
|
128
|
|
129
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
130
|
/**
|
131
|
* Not part of public API, do not document (public only because has to be used from the main package)
|
132
|
*
|
133
|
* @y.exclude
|
134
|
*/
|
135
|
public void bindVertexAttribs(DistortedProgram program)
|
136
|
{
|
137
|
GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mVBO.mIndex[0] );
|
138
|
GLES31.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES31.GL_FLOAT, false, VERT_SIZE, OFFSET_POS);
|
139
|
GLES31.glVertexAttribPointer(program.mAttribute[1], NOR_DATA_SIZE, GLES31.GL_FLOAT, false, VERT_SIZE, OFFSET_NOR);
|
140
|
GLES31.glVertexAttribPointer(program.mAttribute[2], INF_DATA_SIZE, GLES31.GL_FLOAT, false, VERT_SIZE, OFFSET_INF);
|
141
|
GLES31.glVertexAttribPointer(program.mAttribute[3], TEX_DATA_SIZE, GLES31.GL_FLOAT, false, VERT_SIZE, OFFSET_TEX);
|
142
|
GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
|
143
|
}
|
144
|
|
145
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
146
|
/**
|
147
|
* Not part of public API, do not document (public only because has to be used from the main package)
|
148
|
*
|
149
|
* @y.exclude
|
150
|
*/
|
151
|
public void bindTransformAttribs(DistortedProgram program)
|
152
|
{
|
153
|
GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mTFO.mIndex[0] );
|
154
|
GLES31.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES31.GL_FLOAT, false, 0, 0);
|
155
|
GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
|
156
|
}
|
157
|
|
158
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
159
|
/**
|
160
|
* Not part of public API, do not document (public only because has to be used from the main package)
|
161
|
*
|
162
|
* @y.exclude
|
163
|
*/
|
164
|
public void setInflate(float inflate)
|
165
|
{
|
166
|
mInflate = inflate;
|
167
|
}
|
168
|
|
169
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
170
|
/**
|
171
|
* Not part of public API, do not document (public only because has to be used from the main package)
|
172
|
*
|
173
|
* @y.exclude
|
174
|
*/
|
175
|
public float getInflate()
|
176
|
{
|
177
|
return mInflate;
|
178
|
}
|
179
|
|
180
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
181
|
// PUBLIC API
|
182
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
183
|
/**
|
184
|
* When rendering this Mesh, do we want to render the Normal vectors as well?
|
185
|
* <p>
|
186
|
* Will work only on OpenGL ES >= 3.0 devices.
|
187
|
*
|
188
|
* @param show Controls if we render the Normal vectors or not.
|
189
|
*/
|
190
|
public void setShowNormals(boolean show)
|
191
|
{
|
192
|
mShowNormals = (Distorted.GLSL >= 300 && show);
|
193
|
}
|
194
|
|
195
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
196
|
/**
|
197
|
* When rendering this mesh, should we also draw the normal vectors?
|
198
|
*
|
199
|
* @return <i>true</i> if we do render normal vectors
|
200
|
*/
|
201
|
public boolean getShowNormals()
|
202
|
{
|
203
|
return mShowNormals;
|
204
|
}
|
205
|
|
206
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
207
|
/**
|
208
|
* Release all internal resources.
|
209
|
*/
|
210
|
public void markForDeletion()
|
211
|
{
|
212
|
mVertAttribs.clear();
|
213
|
|
214
|
mVBO.markForDeletion();
|
215
|
mTFO.markForDeletion();
|
216
|
}
|
217
|
}
|
218
|
|
219
|
|
220
|
|