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 android.opengl.GLES30;
|
23
|
|
24
|
import java.nio.FloatBuffer;
|
25
|
|
26
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
27
|
/**
|
28
|
* Abstract class which represents a Mesh, ie 3 arrays of Vertex attributes: 1) positions
|
29
|
* 2) normals 3) texture coordinates.
|
30
|
* <p>
|
31
|
* If you want to render to a particular shape, extend from here, construct the three FloatBuffers and
|
32
|
* provide correct dataLength, i.e. the number of vertices.
|
33
|
*/
|
34
|
public abstract class MeshObject extends DistortedObject
|
35
|
{
|
36
|
static final int BYTES_PER_FLOAT = 4; //
|
37
|
static final int POSITION_DATA_SIZE= 3; // Size of the position data in elements
|
38
|
static final int NORMAL_DATA_SIZE = 3; // Size of the normal data in elements.
|
39
|
static final int TEX_DATA_SIZE = 2; // Size of the texture coordinate data in elements.
|
40
|
|
41
|
int dataLength;
|
42
|
FloatBuffer mMeshPositions, mMeshNormals, mMeshTexture;
|
43
|
int[] mPosVBO = new int[1];
|
44
|
int[] mNorVBO = new int[1];
|
45
|
int[] mTexVBO = new int[1];
|
46
|
|
47
|
int[] mPosTBO = new int[1]; // Transform Feedback
|
48
|
int[] mNorTBO = new int[1]; // Transform Feedback
|
49
|
|
50
|
final float zFactor; // strange workaround for the fact that we need to somehow store the 'depth'
|
51
|
// of the Mesh. Used in DistortedEffects. See DistortedTexture.getDepth().
|
52
|
|
53
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
54
|
|
55
|
MeshObject(float factor)
|
56
|
{
|
57
|
super(DistortedObject.NOT_CREATED_YET,DistortedObject.TYPE_USER);
|
58
|
|
59
|
zFactor = factor;
|
60
|
recreate();
|
61
|
}
|
62
|
|
63
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
64
|
// must be called from a thread holding OpenGL Context
|
65
|
//
|
66
|
// Do NOT release mMeshPositions etc as we will need them when we need to re-create the buffers after
|
67
|
// a loss of OpenGL context!
|
68
|
|
69
|
void create()
|
70
|
{
|
71
|
if( mPosVBO[0]<0 )
|
72
|
{
|
73
|
GLES30.glGenBuffers(1, mPosVBO, 0);
|
74
|
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mPosVBO[0]);
|
75
|
GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, dataLength*POSITION_DATA_SIZE*BYTES_PER_FLOAT, mMeshPositions, GLES30.GL_STATIC_DRAW);
|
76
|
}
|
77
|
if( mNorVBO[0]<0 )
|
78
|
{
|
79
|
GLES30.glGenBuffers(1, mNorVBO, 0);
|
80
|
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mNorVBO[0]);
|
81
|
GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, dataLength* NORMAL_DATA_SIZE*BYTES_PER_FLOAT, mMeshNormals , GLES30.GL_STATIC_DRAW);
|
82
|
}
|
83
|
if( mTexVBO[0]<0 )
|
84
|
{
|
85
|
GLES30.glGenBuffers(1, mTexVBO, 0);
|
86
|
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mTexVBO[0]);
|
87
|
GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, dataLength* TEX_DATA_SIZE*BYTES_PER_FLOAT, mMeshTexture , GLES30.GL_STATIC_DRAW);
|
88
|
}
|
89
|
|
90
|
if( mPosTBO[0]<0 )
|
91
|
{
|
92
|
GLES30.glBindBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, mPosTBO[0]);
|
93
|
GLES30.glBufferData(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, dataLength*POSITION_DATA_SIZE*BYTES_PER_FLOAT, null, GLES30.GL_STATIC_READ);
|
94
|
GLES30.glBindBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0);
|
95
|
}
|
96
|
if( mNorTBO[0]<0 )
|
97
|
{
|
98
|
GLES30.glBindBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, mNorTBO[0]);
|
99
|
GLES30.glBufferData(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, dataLength* NORMAL_DATA_SIZE*BYTES_PER_FLOAT, null, GLES30.GL_STATIC_READ);
|
100
|
GLES30.glBindBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0);
|
101
|
}
|
102
|
|
103
|
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
|
104
|
}
|
105
|
|
106
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
107
|
// must be called from a thread holding OpenGL Context
|
108
|
|
109
|
void delete()
|
110
|
{
|
111
|
if( mPosVBO[0]>=0 )
|
112
|
{
|
113
|
GLES30.glDeleteBuffers(1, mPosVBO, 0);
|
114
|
mPosVBO[0] = -1;
|
115
|
}
|
116
|
if( mNorVBO[0]>=0 )
|
117
|
{
|
118
|
GLES30.glDeleteBuffers(1, mNorVBO, 0);
|
119
|
mNorVBO[0] = -1;
|
120
|
}
|
121
|
if( mTexVBO[0]>=0 )
|
122
|
{
|
123
|
GLES30.glDeleteBuffers(1, mTexVBO, 0);
|
124
|
mTexVBO[0] = -1;
|
125
|
}
|
126
|
if( mPosTBO[0]>=0 )
|
127
|
{
|
128
|
GLES30.glDeleteBuffers(1, mPosTBO, 0);
|
129
|
mPosTBO[0] = -1;
|
130
|
}
|
131
|
if( mNorTBO[0]>=0 )
|
132
|
{
|
133
|
GLES30.glDeleteBuffers(1, mNorTBO, 0);
|
134
|
mNorTBO[0] = -1;
|
135
|
}
|
136
|
}
|
137
|
|
138
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
139
|
|
140
|
void recreate()
|
141
|
{
|
142
|
mPosVBO[0] = -1;
|
143
|
mNorVBO[0] = -1;
|
144
|
mTexVBO[0] = -1;
|
145
|
mPosTBO[0] = -1;
|
146
|
mNorTBO[0] = -1;
|
147
|
}
|
148
|
|
149
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
150
|
// debugging only
|
151
|
|
152
|
String printDetails()
|
153
|
{
|
154
|
return getClass().getSimpleName()+" vertices:"+dataLength;
|
155
|
}
|
156
|
|
157
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
158
|
/**
|
159
|
* Get the minimal set of Vertices which have the same convex hull as the whole set.
|
160
|
* <p>
|
161
|
* In case of Flat Meshes, the set is obviously just the 4 corners. In case of the Cubes Mesh,
|
162
|
* it is a subset of the set of each rightmost- and leftmost- corners in each row.
|
163
|
* <p>
|
164
|
* This is used to be able to quickly compute, in window coordinates, the Mesh'es bounding rectangle.
|
165
|
*/
|
166
|
abstract float[] getBoundingVertices();
|
167
|
}
|
168
|
|