1 |
4e2382f3
|
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 |
|
|
package org.distorted.library;
|
21 |
|
|
|
22 |
|
|
import android.graphics.Bitmap;
|
23 |
|
|
import android.graphics.Matrix;
|
24 |
194ab46f
|
Leszek Koltunski
|
import android.opengl.GLES30;
|
25 |
4e2382f3
|
Leszek Koltunski
|
import android.opengl.GLUtils;
|
26 |
|
|
|
27 |
1942537e
|
Leszek Koltunski
|
import java.util.Iterator;
|
28 |
|
|
import java.util.LinkedList;
|
29 |
4e2382f3
|
Leszek Koltunski
|
|
30 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
31 |
e0b6c593
|
Leszek Koltunski
|
/**
|
32 |
|
|
* Class which represents a OpenGL Texture object.
|
33 |
|
|
* <p>
|
34 |
|
|
* Create a Texture of arbitrary size and feed some pixels to it via the setTexture() method.
|
35 |
|
|
* <p>
|
36 |
|
|
* Keep all objects created in a static LinkedList. The point: we need to be able to mark
|
37 |
|
|
* Textures for deletion, and delete all marked objects later at a convenient time (that's
|
38 |
|
|
* because we can only delete from a thread that holds the OpenGL context so here we provide a
|
39 |
|
|
* framework where one is able to mark for deletion at any time and actual deletion takes place
|
40 |
|
|
* on the next render).
|
41 |
|
|
*/
|
42 |
133cbb2b
|
Leszek Koltunski
|
public class DistortedTexture extends DistortedRenderable
|
43 |
4e2382f3
|
Leszek Koltunski
|
{
|
44 |
1942537e
|
Leszek Koltunski
|
private static boolean mListMarked = false;
|
45 |
|
|
private static LinkedList<DistortedTexture> mList = new LinkedList<>();
|
46 |
4e2382f3
|
Leszek Koltunski
|
|
47 |
f8686932
|
Leszek Koltunski
|
private static int mTextureH;
|
48 |
|
|
|
49 |
1942537e
|
Leszek Koltunski
|
private boolean mMarked;
|
50 |
e7a20702
|
Leszek Koltunski
|
private Bitmap mBmp= null;
|
51 |
4e2382f3
|
Leszek Koltunski
|
|
52 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
53 |
|
|
// We have to flip vertically every single Bitmap that we get fed with.
|
54 |
|
|
//
|
55 |
|
|
// Reason: textures read from files are the only objects in OpenGL which have their origins at the
|
56 |
|
|
// upper-left corner. Everywhere else the origin is in the lower-left corner. Thus we have to flip.
|
57 |
|
|
// The alternative solution, namely inverting the y-coordinate of the TexCoord does not really work-
|
58 |
|
|
// i.e. it works only in case of rendering directly to the screen, but if we render to an FBO and
|
59 |
421c2728
|
Leszek Koltunski
|
// then take the FBO and render to screen, (DistortedTree does so!) things get inverted as textures
|
60 |
4e2382f3
|
Leszek Koltunski
|
// created from FBO have their origins in the lower-left... Mindfuck!
|
61 |
|
|
|
62 |
|
|
private static Bitmap flipBitmap(Bitmap src)
|
63 |
|
|
{
|
64 |
|
|
Matrix matrix = new Matrix();
|
65 |
|
|
matrix.preScale(1.0f,-1.0f);
|
66 |
|
|
|
67 |
|
|
return Bitmap.createBitmap(src,0,0,src.getWidth(),src.getHeight(), matrix,true);
|
68 |
|
|
}
|
69 |
|
|
|
70 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
71 |
1942537e
|
Leszek Koltunski
|
// must be called from a thread holding OpenGL Context
|
72 |
4e2382f3
|
Leszek Koltunski
|
|
73 |
133cbb2b
|
Leszek Koltunski
|
void create()
|
74 |
4e2382f3
|
Leszek Koltunski
|
{
|
75 |
133cbb2b
|
Leszek Koltunski
|
if( mBmp!=null && mColorH !=null )
|
76 |
4e2382f3
|
Leszek Koltunski
|
{
|
77 |
133cbb2b
|
Leszek Koltunski
|
if( mColorH[0]==0 )
|
78 |
05403bba
|
Leszek Koltunski
|
{
|
79 |
133cbb2b
|
Leszek Koltunski
|
GLES30.glGenTextures(1, mColorH, 0);
|
80 |
|
|
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
|
81 |
194ab46f
|
Leszek Koltunski
|
GLES30.glTexParameteri ( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_LINEAR );
|
82 |
|
|
GLES30.glTexParameteri ( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_LINEAR );
|
83 |
|
|
GLES30.glTexParameteri ( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_CLAMP_TO_EDGE );
|
84 |
|
|
GLES30.glTexParameteri ( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_CLAMP_TO_EDGE );
|
85 |
|
|
GLUtils.texImage2D(GLES30.GL_TEXTURE_2D, 0, flipBitmap(mBmp), 0);
|
86 |
05403bba
|
Leszek Koltunski
|
}
|
87 |
|
|
else
|
88 |
|
|
{
|
89 |
133cbb2b
|
Leszek Koltunski
|
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
|
90 |
194ab46f
|
Leszek Koltunski
|
GLUtils.texSubImage2D(GLES30.GL_TEXTURE_2D, 0,0,0,flipBitmap(mBmp));
|
91 |
05403bba
|
Leszek Koltunski
|
}
|
92 |
4e2382f3
|
Leszek Koltunski
|
|
93 |
e7a20702
|
Leszek Koltunski
|
mBmp = null;
|
94 |
4e2382f3
|
Leszek Koltunski
|
}
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
98 |
1942537e
|
Leszek Koltunski
|
// must be called from a thread holding OpenGL Context
|
99 |
4e2382f3
|
Leszek Koltunski
|
|
100 |
133cbb2b
|
Leszek Koltunski
|
private void delete()
|
101 |
4e2382f3
|
Leszek Koltunski
|
{
|
102 |
133cbb2b
|
Leszek Koltunski
|
if( mColorH !=null && mColorH[0]>0 )
|
103 |
1942537e
|
Leszek Koltunski
|
{
|
104 |
133cbb2b
|
Leszek Koltunski
|
GLES30.glDeleteTextures(1, mColorH, 0);
|
105 |
|
|
mColorH[0] = 0;
|
106 |
1942537e
|
Leszek Koltunski
|
}
|
107 |
|
|
|
108 |
|
|
mMarked = false;
|
109 |
4e2382f3
|
Leszek Koltunski
|
}
|
110 |
|
|
|
111 |
f8686932
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
112 |
|
|
|
113 |
|
|
static void getUniforms(int mProgramH)
|
114 |
|
|
{
|
115 |
194ab46f
|
Leszek Koltunski
|
mTextureH= GLES30.glGetUniformLocation( mProgramH, "u_Texture");
|
116 |
f8686932
|
Leszek Koltunski
|
|
117 |
194ab46f
|
Leszek Koltunski
|
GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
|
118 |
|
|
GLES30.glUniform1i(mTextureH, 0);
|
119 |
f8686932
|
Leszek Koltunski
|
}
|
120 |
|
|
|
121 |
4e2382f3
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
122 |
|
|
|
123 |
7b8086eb
|
Leszek Koltunski
|
static synchronized void onDestroy()
|
124 |
4e2382f3
|
Leszek Koltunski
|
{
|
125 |
1942537e
|
Leszek Koltunski
|
mListMarked = false;
|
126 |
|
|
mList.clear();
|
127 |
4e2382f3
|
Leszek Koltunski
|
}
|
128 |
|
|
|
129 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
130 |
1942537e
|
Leszek Koltunski
|
// must be called form a thread holding OpenGL Context
|
131 |
4e2382f3
|
Leszek Koltunski
|
|
132 |
1942537e
|
Leszek Koltunski
|
static synchronized void deleteAllMarked()
|
133 |
4e2382f3
|
Leszek Koltunski
|
{
|
134 |
1942537e
|
Leszek Koltunski
|
if( mListMarked )
|
135 |
4e2382f3
|
Leszek Koltunski
|
{
|
136 |
1942537e
|
Leszek Koltunski
|
DistortedTexture tmp;
|
137 |
|
|
Iterator<DistortedTexture> iterator = mList.iterator();
|
138 |
|
|
|
139 |
|
|
while(iterator.hasNext())
|
140 |
|
|
{
|
141 |
|
|
tmp = iterator.next();
|
142 |
|
|
|
143 |
|
|
if( tmp.mMarked )
|
144 |
|
|
{
|
145 |
133cbb2b
|
Leszek Koltunski
|
tmp.delete();
|
146 |
1942537e
|
Leszek Koltunski
|
iterator.remove();
|
147 |
|
|
}
|
148 |
|
|
}
|
149 |
4e2382f3
|
Leszek Koltunski
|
|
150 |
1942537e
|
Leszek Koltunski
|
mListMarked = false;
|
151 |
|
|
}
|
152 |
4e2382f3
|
Leszek Koltunski
|
}
|
153 |
|
|
|
154 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
155 |
|
|
// PUBLIC API
|
156 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
157 |
|
|
/**
|
158 |
|
|
* Create empty texture of given dimensions.
|
159 |
|
|
*/
|
160 |
3ef3364d
|
Leszek Koltunski
|
public DistortedTexture(int width, int height)
|
161 |
4e2382f3
|
Leszek Koltunski
|
{
|
162 |
133cbb2b
|
Leszek Koltunski
|
mSizeX = width ;
|
163 |
|
|
mSizeY = height;
|
164 |
|
|
mColorH[0]= 0;
|
165 |
|
|
mBmp = null;
|
166 |
|
|
mMarked = false;
|
167 |
4e2382f3
|
Leszek Koltunski
|
|
168 |
1942537e
|
Leszek Koltunski
|
mList.add(this);
|
169 |
4e2382f3
|
Leszek Koltunski
|
}
|
170 |
|
|
|
171 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
172 |
|
|
/**
|
173 |
1942537e
|
Leszek Koltunski
|
* Mark the underlying OpenGL object for deletion. Actual deletion will take place on the next render.
|
174 |
4e2382f3
|
Leszek Koltunski
|
*/
|
175 |
1942537e
|
Leszek Koltunski
|
public void markForDeletion()
|
176 |
4e2382f3
|
Leszek Koltunski
|
{
|
177 |
1942537e
|
Leszek Koltunski
|
mListMarked = true;
|
178 |
|
|
mMarked = true;
|
179 |
4e2382f3
|
Leszek Koltunski
|
}
|
180 |
|
|
|
181 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
182 |
|
|
/**
|
183 |
1942537e
|
Leszek Koltunski
|
* Sets the underlying android.graphics.Bitmap object.
|
184 |
4e2382f3
|
Leszek Koltunski
|
* <p>
|
185 |
|
|
* You can only recycle() the passed Bitmap once the OpenGL context gets created (i.e. after call
|
186 |
432442f9
|
Leszek Koltunski
|
* to GLSurfaceView.onSurfaceCreated) because only after this point can the Library upload it to the GPU!
|
187 |
4e2382f3
|
Leszek Koltunski
|
*
|
188 |
|
|
* @param bmp The android.graphics.Bitmap object to apply effects to and display.
|
189 |
|
|
*/
|
190 |
|
|
|
191 |
|
|
public void setTexture(Bitmap bmp)
|
192 |
|
|
{
|
193 |
0c827acc
|
Leszek Koltunski
|
mBmp= bmp;
|
194 |
1942537e
|
Leszek Koltunski
|
}
|
195 |
4e2382f3
|
Leszek Koltunski
|
}
|