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 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
28 |
e0b6c593
|
Leszek Koltunski
|
/**
|
29 |
|
|
* Class which represents a OpenGL Texture object.
|
30 |
|
|
* <p>
|
31 |
|
|
* Create a Texture of arbitrary size and feed some pixels to it via the setTexture() method.
|
32 |
|
|
* <p>
|
33 |
|
|
* Keep all objects created in a static LinkedList. The point: we need to be able to mark
|
34 |
|
|
* Textures for deletion, and delete all marked objects later at a convenient time (that's
|
35 |
|
|
* because we can only delete from a thread that holds the OpenGL context so here we provide a
|
36 |
|
|
* framework where one is able to mark for deletion at any time and actual deletion takes place
|
37 |
|
|
* on the next render).
|
38 |
|
|
*/
|
39 |
133cbb2b
|
Leszek Koltunski
|
public class DistortedTexture extends DistortedRenderable
|
40 |
4e2382f3
|
Leszek Koltunski
|
{
|
41 |
e7a20702
|
Leszek Koltunski
|
private Bitmap mBmp= null;
|
42 |
4e2382f3
|
Leszek Koltunski
|
|
43 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
44 |
|
|
// We have to flip vertically every single Bitmap that we get fed with.
|
45 |
|
|
//
|
46 |
|
|
// Reason: textures read from files are the only objects in OpenGL which have their origins at the
|
47 |
|
|
// upper-left corner. Everywhere else the origin is in the lower-left corner. Thus we have to flip.
|
48 |
|
|
// The alternative solution, namely inverting the y-coordinate of the TexCoord does not really work-
|
49 |
|
|
// i.e. it works only in case of rendering directly to the screen, but if we render to an FBO and
|
50 |
421c2728
|
Leszek Koltunski
|
// then take the FBO and render to screen, (DistortedTree does so!) things get inverted as textures
|
51 |
4e2382f3
|
Leszek Koltunski
|
// created from FBO have their origins in the lower-left... Mindfuck!
|
52 |
|
|
|
53 |
|
|
private static Bitmap flipBitmap(Bitmap src)
|
54 |
|
|
{
|
55 |
|
|
Matrix matrix = new Matrix();
|
56 |
|
|
matrix.preScale(1.0f,-1.0f);
|
57 |
|
|
|
58 |
|
|
return Bitmap.createBitmap(src,0,0,src.getWidth(),src.getHeight(), matrix,true);
|
59 |
|
|
}
|
60 |
|
|
|
61 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
62 |
1942537e
|
Leszek Koltunski
|
// must be called from a thread holding OpenGL Context
|
63 |
4e2382f3
|
Leszek Koltunski
|
|
64 |
133cbb2b
|
Leszek Koltunski
|
void create()
|
65 |
4e2382f3
|
Leszek Koltunski
|
{
|
66 |
133cbb2b
|
Leszek Koltunski
|
if( mBmp!=null && mColorH !=null )
|
67 |
4e2382f3
|
Leszek Koltunski
|
{
|
68 |
2e49718d
|
Leszek Koltunski
|
if( mColorH[0]==NOT_CREATED_YET )
|
69 |
05403bba
|
Leszek Koltunski
|
{
|
70 |
133cbb2b
|
Leszek Koltunski
|
GLES30.glGenTextures(1, mColorH, 0);
|
71 |
|
|
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
|
72 |
194ab46f
|
Leszek Koltunski
|
GLES30.glTexParameteri ( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_LINEAR );
|
73 |
|
|
GLES30.glTexParameteri ( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_LINEAR );
|
74 |
|
|
GLES30.glTexParameteri ( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_CLAMP_TO_EDGE );
|
75 |
|
|
GLES30.glTexParameteri ( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_CLAMP_TO_EDGE );
|
76 |
|
|
GLUtils.texImage2D(GLES30.GL_TEXTURE_2D, 0, flipBitmap(mBmp), 0);
|
77 |
05403bba
|
Leszek Koltunski
|
}
|
78 |
|
|
else
|
79 |
|
|
{
|
80 |
133cbb2b
|
Leszek Koltunski
|
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
|
81 |
194ab46f
|
Leszek Koltunski
|
GLUtils.texSubImage2D(GLES30.GL_TEXTURE_2D, 0,0,0,flipBitmap(mBmp));
|
82 |
05403bba
|
Leszek Koltunski
|
}
|
83 |
4e2382f3
|
Leszek Koltunski
|
|
84 |
e7a20702
|
Leszek Koltunski
|
mBmp = null;
|
85 |
4e2382f3
|
Leszek Koltunski
|
}
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
89 |
1942537e
|
Leszek Koltunski
|
// must be called from a thread holding OpenGL Context
|
90 |
4e2382f3
|
Leszek Koltunski
|
|
91 |
227ac49a
|
Leszek Koltunski
|
void delete()
|
92 |
4e2382f3
|
Leszek Koltunski
|
{
|
93 |
2e49718d
|
Leszek Koltunski
|
if( mColorH !=null && mColorH[0]>=0 )
|
94 |
1942537e
|
Leszek Koltunski
|
{
|
95 |
133cbb2b
|
Leszek Koltunski
|
GLES30.glDeleteTextures(1, mColorH, 0);
|
96 |
2e49718d
|
Leszek Koltunski
|
mColorH[0] = NOT_CREATED_YET;
|
97 |
1942537e
|
Leszek Koltunski
|
}
|
98 |
4e2382f3
|
Leszek Koltunski
|
}
|
99 |
|
|
|
100 |
4ebbb17a
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
101 |
|
|
// called from onDestroy(); mark OpenGL assets as 'not created'
|
102 |
|
|
|
103 |
|
|
void destroy()
|
104 |
|
|
{
|
105 |
|
|
if( mColorH[0]!=DONT_CREATE ) mColorH[0] = NOT_CREATED_YET;
|
106 |
|
|
}
|
107 |
f8686932
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
108 |
|
|
|
109 |
|
|
static void getUniforms(int mProgramH)
|
110 |
|
|
{
|
111 |
2e49718d
|
Leszek Koltunski
|
int textureH= GLES30.glGetUniformLocation( mProgramH, "u_Texture");
|
112 |
f8686932
|
Leszek Koltunski
|
|
113 |
194ab46f
|
Leszek Koltunski
|
GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
|
114 |
2e49718d
|
Leszek Koltunski
|
GLES30.glUniform1i(textureH, 0);
|
115 |
4e2382f3
|
Leszek Koltunski
|
}
|
116 |
|
|
|
117 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
118 |
|
|
// PUBLIC API
|
119 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
120 |
|
|
/**
|
121 |
|
|
* Create empty texture of given dimensions.
|
122 |
|
|
*/
|
123 |
3ef3364d
|
Leszek Koltunski
|
public DistortedTexture(int width, int height)
|
124 |
4e2382f3
|
Leszek Koltunski
|
{
|
125 |
2e49718d
|
Leszek Koltunski
|
super(width,height,NOT_CREATED_YET);
|
126 |
|
|
mBmp= null;
|
127 |
4e2382f3
|
Leszek Koltunski
|
}
|
128 |
|
|
|
129 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
130 |
|
|
/**
|
131 |
1942537e
|
Leszek Koltunski
|
* Sets the underlying android.graphics.Bitmap object.
|
132 |
4e2382f3
|
Leszek Koltunski
|
* <p>
|
133 |
|
|
* You can only recycle() the passed Bitmap once the OpenGL context gets created (i.e. after call
|
134 |
432442f9
|
Leszek Koltunski
|
* to GLSurfaceView.onSurfaceCreated) because only after this point can the Library upload it to the GPU!
|
135 |
4e2382f3
|
Leszek Koltunski
|
*
|
136 |
|
|
* @param bmp The android.graphics.Bitmap object to apply effects to and display.
|
137 |
|
|
*/
|
138 |
|
|
public void setTexture(Bitmap bmp)
|
139 |
|
|
{
|
140 |
0c827acc
|
Leszek Koltunski
|
mBmp= bmp;
|
141 |
1942537e
|
Leszek Koltunski
|
}
|
142 |
4e2382f3
|
Leszek Koltunski
|
}
|