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