1 |
466450b5
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
// Copyright 2018 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.main;
|
21 |
|
|
|
22 |
|
|
import android.opengl.GLES31;
|
23 |
|
|
import java.nio.Buffer;
|
24 |
|
|
|
25 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
26 |
|
|
/**
|
27 |
|
|
* Implements OpenGL buffer object such as GL_ARRAY_BUFFER or GL_TRANSFORM_FEEDBACK_BUFFER.
|
28 |
|
|
* Main point: deal with Android lifecycle and recreate the buffer on loss of OpenGL context.
|
29 |
|
|
* <p>
|
30 |
|
|
* Not part of public API, do not document (public only because has to be used in Meshes)
|
31 |
|
|
*
|
32 |
|
|
* @y.exclude
|
33 |
|
|
*/
|
34 |
|
|
public class DistortedBuffer extends DistortedObject
|
35 |
|
|
{
|
36 |
6c00149d
|
Leszek Koltunski
|
public int[] mIndex;
|
37 |
466450b5
|
Leszek Koltunski
|
|
38 |
|
|
private int mTarget, mSize, mUsage;
|
39 |
|
|
private Buffer mBuffer;
|
40 |
|
|
|
41 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
42 |
|
|
|
43 |
6c00149d
|
Leszek Koltunski
|
public DistortedBuffer(int target, int usage)
|
44 |
466450b5
|
Leszek Koltunski
|
{
|
45 |
|
|
super(DistortedObject.NOT_CREATED_YET,DistortedObject.TYPE_USER);
|
46 |
|
|
|
47 |
|
|
mIndex = new int[1];
|
48 |
|
|
mTarget = target;
|
49 |
|
|
mUsage = usage;
|
50 |
|
|
|
51 |
|
|
recreate();
|
52 |
|
|
}
|
53 |
|
|
|
54 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
55 |
|
|
|
56 |
6c00149d
|
Leszek Koltunski
|
public void setData(int size, Buffer buffer)
|
57 |
466450b5
|
Leszek Koltunski
|
{
|
58 |
|
|
mSize = size;
|
59 |
|
|
mBuffer = buffer;
|
60 |
|
|
}
|
61 |
|
|
|
62 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
63 |
|
|
// must be called from a thread holding OpenGL Context
|
64 |
|
|
|
65 |
|
|
void create()
|
66 |
|
|
{
|
67 |
|
|
if( mIndex[0]<0 )
|
68 |
|
|
{
|
69 |
|
|
GLES31.glGenBuffers( 1, mIndex, 0);
|
70 |
|
|
GLES31.glBindBuffer( mTarget, mIndex[0]);
|
71 |
|
|
GLES31.glBufferData( mTarget, mSize, mBuffer, mUsage);
|
72 |
|
|
GLES31.glBindBuffer( mTarget, 0);
|
73 |
|
|
}
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
77 |
|
|
// must be called from a thread holding OpenGL Context
|
78 |
|
|
|
79 |
|
|
void delete()
|
80 |
|
|
{
|
81 |
|
|
if( mIndex[0]>=0 )
|
82 |
|
|
{
|
83 |
|
|
GLES31.glDeleteBuffers(1, mIndex, 0);
|
84 |
|
|
mIndex[0] = -1;
|
85 |
|
|
}
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
89 |
|
|
|
90 |
|
|
void recreate()
|
91 |
|
|
{
|
92 |
|
|
mIndex[0] = -1;
|
93 |
|
|
}
|
94 |
|
|
|
95 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
96 |
|
|
// debugging only
|
97 |
|
|
|
98 |
|
|
String printDetails()
|
99 |
|
|
{
|
100 |
|
|
return getClass().getSimpleName();
|
101 |
|
|
}
|
102 |
|
|
}
|