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.util.Iterator;
|
25
|
import java.util.LinkedList;
|
26
|
|
27
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
28
|
/**
|
29
|
* Abstract class which represents a Renderable Object, i.e. something that we can take an skin our Mesh with.
|
30
|
* <p>
|
31
|
* Currently a DistortedTexture or a DistortedFramebuffer are Renderables.
|
32
|
*/
|
33
|
public abstract class DistortedRenderable
|
34
|
{
|
35
|
static final int FAILED_TO_CREATE = -1;
|
36
|
static final int NOT_CREATED_YET = -2;
|
37
|
static final int DONT_CREATE = -3;
|
38
|
|
39
|
private static boolean mListMarked = false;
|
40
|
private static LinkedList<DistortedRenderable> mList = new LinkedList<>();
|
41
|
|
42
|
private boolean mMarked;
|
43
|
int[] mColorH = new int[1];
|
44
|
int mSizeX, mSizeY; // in screen space
|
45
|
|
46
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
47
|
|
48
|
abstract void create();
|
49
|
abstract void delete();
|
50
|
abstract void destroy();
|
51
|
|
52
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
53
|
// must be called form a thread holding OpenGL Context
|
54
|
|
55
|
static synchronized void deleteAllMarked()
|
56
|
{
|
57
|
if( mListMarked )
|
58
|
{
|
59
|
DistortedRenderable tmp;
|
60
|
Iterator<DistortedRenderable> iterator = mList.iterator();
|
61
|
|
62
|
while(iterator.hasNext())
|
63
|
{
|
64
|
tmp = iterator.next();
|
65
|
|
66
|
if( tmp.mMarked )
|
67
|
{
|
68
|
tmp.delete();
|
69
|
tmp.mMarked = false;
|
70
|
iterator.remove();
|
71
|
}
|
72
|
}
|
73
|
|
74
|
mListMarked = false;
|
75
|
}
|
76
|
}
|
77
|
|
78
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
79
|
|
80
|
long getID()
|
81
|
{
|
82
|
return mColorH[0];
|
83
|
}
|
84
|
|
85
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
86
|
|
87
|
boolean setAsInput()
|
88
|
{
|
89
|
if( mColorH[0]>0 )
|
90
|
{
|
91
|
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
|
92
|
return true;
|
93
|
}
|
94
|
|
95
|
return false;
|
96
|
}
|
97
|
|
98
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
99
|
|
100
|
static synchronized void onDestroy()
|
101
|
{
|
102
|
for( DistortedRenderable ren : mList)
|
103
|
{
|
104
|
ren.destroy();
|
105
|
ren.mMarked = false;
|
106
|
}
|
107
|
|
108
|
mListMarked = false;
|
109
|
}
|
110
|
|
111
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
112
|
|
113
|
DistortedRenderable(int width, int height, int color)
|
114
|
{
|
115
|
mSizeX = width ;
|
116
|
mSizeY = height;
|
117
|
mColorH[0]= color;
|
118
|
mMarked = false;
|
119
|
mList.add(this);
|
120
|
}
|
121
|
|
122
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
123
|
// PUBLIC API
|
124
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
125
|
/**
|
126
|
* Mark the underlying OpenGL object for deletion. Actual deletion will take place on the next render.
|
127
|
*/
|
128
|
public void markForDeletion()
|
129
|
{
|
130
|
mListMarked = true;
|
131
|
mMarked = true;
|
132
|
}
|
133
|
|
134
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
135
|
|
136
|
/**
|
137
|
* Returns the height of the Renderable.
|
138
|
*
|
139
|
* @return height of the object, in pixels.
|
140
|
*/
|
141
|
public int getWidth()
|
142
|
{
|
143
|
return mSizeX;
|
144
|
}
|
145
|
|
146
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
147
|
/**
|
148
|
* Returns the width of the Renderable.
|
149
|
*
|
150
|
* @return width of the Object, in pixels.
|
151
|
*/
|
152
|
public int getHeight()
|
153
|
{
|
154
|
return mSizeY;
|
155
|
}
|
156
|
|
157
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
158
|
/**
|
159
|
* Returns the depth of the Renderable.
|
160
|
* <p>
|
161
|
* Admittedly quite a strange method. Why do we need to pass a Mesh to it? Because one cannot determine
|
162
|
* 'depth' of a Renderable (bitmap really!) when rendered based only on the texture itself, that depends
|
163
|
* on the Mesh it is rendered with.
|
164
|
*
|
165
|
* @return depth of the Object, in pixels.
|
166
|
*/
|
167
|
public int getDepth(MeshObject mesh)
|
168
|
{
|
169
|
return mesh==null ? 0 : (int)(mSizeX*mesh.zFactor);
|
170
|
}
|
171
|
}
|