1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2016 Leszek Koltunski leszek@koltunski.pl //
|
3
|
// //
|
4
|
// This file is part of Distorted. //
|
5
|
// //
|
6
|
// This library is free software; you can redistribute it and/or //
|
7
|
// modify it under the terms of the GNU Lesser General Public //
|
8
|
// License as published by the Free Software Foundation; either //
|
9
|
// version 2.1 of the License, or (at your option) any later version. //
|
10
|
// //
|
11
|
// This library 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 GNU //
|
14
|
// Lesser General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU Lesser General Public //
|
17
|
// License along with this library; if not, write to the Free Software //
|
18
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA //
|
19
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
20
|
|
21
|
package org.distorted.library.main;
|
22
|
|
23
|
import android.graphics.Bitmap;
|
24
|
import android.graphics.Canvas;
|
25
|
import android.graphics.Matrix;
|
26
|
import android.graphics.Paint;
|
27
|
import android.opengl.GLES30;
|
28
|
import android.opengl.GLUtils;
|
29
|
|
30
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
31
|
/**
|
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
|
*/
|
36
|
public class DistortedTexture extends InternalSurface
|
37
|
{
|
38
|
private Bitmap mBmp;
|
39
|
private boolean mBitmapInverted;
|
40
|
|
41
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
42
|
// We have to vertically flip all the bitmaps passed here via setTexture().
|
43
|
//
|
44
|
// Reason: textures read from files are the only objects in OpenGL which have their origins at the
|
45
|
// upper-left corner. Everywhere else the origin is in the lower-left corner. Thus we have to flip.
|
46
|
// The alternative solution, namely inverting the y-coordinate of the TexCoord does not really work-
|
47
|
// i.e. it works only in case of rendering directly to the screen, but if we render to an FBO and
|
48
|
// then take the FBO and render to screen, (DistortedNode does so!) things get inverted as textures
|
49
|
// created from FBO have their origins in the lower-left...
|
50
|
|
51
|
private static Bitmap flipBitmap(Bitmap src)
|
52
|
{
|
53
|
Matrix matrix = new Matrix();
|
54
|
matrix.preScale(1.0f,-1.0f);
|
55
|
|
56
|
return Bitmap.createBitmap(src,0,0,src.getWidth(),src.getHeight(), matrix,true);
|
57
|
}
|
58
|
|
59
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
60
|
// must be called from a thread holding OpenGL Context
|
61
|
|
62
|
void create()
|
63
|
{
|
64
|
if( mBmp!=null )
|
65
|
{
|
66
|
if( mColorCreated==NOT_CREATED_YET )
|
67
|
{
|
68
|
mColorCreated = CREATED;
|
69
|
GLES30.glGenTextures(1, mColorH, 0);
|
70
|
}
|
71
|
|
72
|
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
|
73
|
GLES30.glTexParameteri ( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_LINEAR );
|
74
|
GLES30.glTexParameteri ( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_LINEAR );
|
75
|
GLES30.glTexParameteri ( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_CLAMP_TO_EDGE );
|
76
|
GLES30.glTexParameteri ( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_CLAMP_TO_EDGE );
|
77
|
GLUtils.texImage2D(GLES30.GL_TEXTURE_2D, 0, mBitmapInverted ? mBmp : flipBitmap(mBmp), 0);
|
78
|
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
|
79
|
|
80
|
mBmp = null;
|
81
|
}
|
82
|
}
|
83
|
|
84
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
85
|
// must be called from a thread holding OpenGL Context
|
86
|
|
87
|
void delete()
|
88
|
{
|
89
|
if( mColorH[0]>0 )
|
90
|
{
|
91
|
GLES30.glDeleteTextures(1, mColorH, 0);
|
92
|
mColorH[0] = 0;
|
93
|
mColorCreated = NOT_CREATED_YET;
|
94
|
}
|
95
|
}
|
96
|
|
97
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
98
|
// called from onDestroy(); mark OpenGL assets as 'not created'
|
99
|
|
100
|
void recreate()
|
101
|
{
|
102
|
if( mColorCreated!=DONT_CREATE )
|
103
|
{
|
104
|
mColorCreated = NOT_CREATED_YET;
|
105
|
mColorH[0] = 0;
|
106
|
}
|
107
|
}
|
108
|
|
109
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
110
|
// create SYSTEM or TREE textures (those are just like normal Textures, just hold information
|
111
|
// that they were autocreated only for the Library's internal purposes (SYSTEM) or for using
|
112
|
// inside a Tree of DistortedNodes (TREE)
|
113
|
// SYSTEM surfaces do not get removed in onDestroy().
|
114
|
|
115
|
public DistortedTexture(int type)
|
116
|
{
|
117
|
super(NOT_CREATED_YET,1,1,type,InternalObject.STORAGE_PRIVATE);
|
118
|
mBmp= null;
|
119
|
}
|
120
|
|
121
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
122
|
// PUBLIC API
|
123
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
124
|
/**
|
125
|
* Create an empty texture.
|
126
|
*/
|
127
|
public DistortedTexture()
|
128
|
{
|
129
|
this(TYPE_USER);
|
130
|
}
|
131
|
|
132
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
133
|
/**
|
134
|
* Sets the underlying android.graphics.Bitmap object.
|
135
|
* <p>
|
136
|
* You can only recycle() the passed Bitmap once the OpenGL context gets created (i.e. after call
|
137
|
* to GLSurfaceView.onSurfaceCreated) because only after this point can the Library upload it to the GPU!
|
138
|
*
|
139
|
* @param bmp The android.graphics.Bitmap object to apply effects to and display.
|
140
|
* @return true if successful, false if texture too large.
|
141
|
*/
|
142
|
public boolean setTexture(Bitmap bmp)
|
143
|
{
|
144
|
int width = bmp.getWidth();
|
145
|
int height= bmp.getHeight();
|
146
|
int max = DistortedLibrary.getMaxTextureSize();
|
147
|
|
148
|
if( width>max || height>max )
|
149
|
{
|
150
|
android.util.Log.e("texture","error, trying to upload too large texture of size "+width+" x "+height+". Max is "+max);
|
151
|
return false;
|
152
|
}
|
153
|
else
|
154
|
{
|
155
|
mBitmapInverted = false;
|
156
|
mBmp= bmp;
|
157
|
markForCreation();
|
158
|
return true;
|
159
|
}
|
160
|
}
|
161
|
|
162
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
163
|
/**
|
164
|
* Sets the underlying android.graphics.Bitmap object - this version assumes the object is already
|
165
|
* flipped upside down.
|
166
|
* <p>
|
167
|
* You can only recycle() the passed Bitmap once the OpenGL context gets created (i.e. after call
|
168
|
* to GLSurfaceView.onSurfaceCreated) because only after this point can the Library upload it to the GPU!
|
169
|
*
|
170
|
* @param bmp The (vertically flipped!) android.graphics.Bitmap object to apply effects to and display.
|
171
|
* @return true if successful, false if texture too large.
|
172
|
*/
|
173
|
public boolean setTextureAlreadyInverted(Bitmap bmp)
|
174
|
{
|
175
|
int width = bmp.getWidth();
|
176
|
int height= bmp.getHeight();
|
177
|
int max = DistortedLibrary.getMaxTextureSize();
|
178
|
|
179
|
if( width>max || height>max )
|
180
|
{
|
181
|
android.util.Log.e("texture","error, trying to upload too large texture of size "+width+" x "+height+". Max is "+max);
|
182
|
return false;
|
183
|
}
|
184
|
else
|
185
|
{
|
186
|
mBitmapInverted = true;
|
187
|
mBmp= bmp;
|
188
|
markForCreation();
|
189
|
return true;
|
190
|
}
|
191
|
}
|
192
|
|
193
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
194
|
/**
|
195
|
* Paints the Texture with solid color.
|
196
|
*
|
197
|
* @param argb The color to paint the Texture with. Example: 0xffff0000 - red.
|
198
|
*/
|
199
|
public void setColorARGB(int argb)
|
200
|
{
|
201
|
Paint paint = new Paint();
|
202
|
paint.setColor(argb);
|
203
|
paint.setStyle(Paint.Style.FILL);
|
204
|
|
205
|
mBmp = Bitmap.createBitmap(1,1, Bitmap.Config.ARGB_8888);
|
206
|
Canvas canvas = new Canvas(mBmp);
|
207
|
canvas.drawRect(0,0,1,1,paint);
|
208
|
|
209
|
markForCreation();
|
210
|
}
|
211
|
}
|