Project

General

Profile

Download (10.3 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / DistortedTexture.java @ f8686932

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
import android.opengl.GLES20;
25
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 4e2382f3 Leszek Koltunski
public class DistortedTexture
43
  {
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 3ef3364d Leszek Koltunski
  private int mSizeX, mSizeY;  // in screen space
50
  float mHalfX, mHalfY;        // halves of the above
51 4e2382f3 Leszek Koltunski
  private long mID;
52 1942537e Leszek Koltunski
  private boolean mMarked;
53 4e2382f3 Leszek Koltunski
54
  private Bitmap[] mBmp= null; //
55
  int[] mTextureDataH;         // have to be shared among all the cloned Objects
56
  boolean[] mBitmapSet;        //
57
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59
// We have to flip vertically every single Bitmap that we get fed with.
60
//
61
// Reason: textures read from files are the only objects in OpenGL which have their origins at the
62
// upper-left corner. Everywhere else the origin is in the lower-left corner. Thus we have to flip.
63
// The alternative solution, namely inverting the y-coordinate of the TexCoord does not really work-
64
// i.e. it works only in case of rendering directly to the screen, but if we render to an FBO and
65 421c2728 Leszek Koltunski
// then take the FBO and render to screen, (DistortedTree does so!) things get inverted as textures
66 4e2382f3 Leszek Koltunski
// created from FBO have their origins in the lower-left... Mindfuck!
67
68
  private static Bitmap flipBitmap(Bitmap src)
69
    {
70
    Matrix matrix = new Matrix();
71
    matrix.preScale(1.0f,-1.0f);
72
73
    return Bitmap.createBitmap(src,0,0,src.getWidth(),src.getHeight(), matrix,true);
74
    }
75
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77 1942537e Leszek Koltunski
// must be called from a thread holding OpenGL Context
78 4e2382f3 Leszek Koltunski
79 1942537e Leszek Koltunski
  void createTexture()
80 4e2382f3 Leszek Koltunski
    {
81 1942537e Leszek Koltunski
    if( mBmp[0]!=null && mTextureDataH!=null )
82 4e2382f3 Leszek Koltunski
      {
83 1942537e Leszek Koltunski
      //android.util.Log.e("Texture", "creating "+mID);
84
85 4e2382f3 Leszek Koltunski
      if( mTextureDataH[0]==0 ) GLES20.glGenTextures(1, mTextureDataH, 0);
86
87
      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataH[0]);
88
      GLES20.glTexParameteri ( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR );
89
      GLES20.glTexParameteri ( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR );
90
      GLES20.glTexParameteri ( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE );
91
      GLES20.glTexParameteri ( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE );
92
93 1942537e Leszek Koltunski
      GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, flipBitmap(mBmp[0]), 0);
94
      mBmp[0] = null;
95 4e2382f3 Leszek Koltunski
      }
96
    }
97
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99 1942537e Leszek Koltunski
// must be called from a thread holding OpenGL Context
100 4e2382f3 Leszek Koltunski
101 1942537e Leszek Koltunski
  private void deleteTexture()
102 4e2382f3 Leszek Koltunski
    {
103 1942537e Leszek Koltunski
    if( mTextureDataH!=null && mTextureDataH[0]>0 )
104
      {
105
      //android.util.Log.e("Texture", "deleting "+mID);
106 4e2382f3 Leszek Koltunski
107 1942537e Leszek Koltunski
      GLES20.glDeleteTextures(1, mTextureDataH, 0);
108 4e2382f3 Leszek Koltunski
109 1942537e Leszek Koltunski
      mTextureDataH[0] = 0;
110
      mBitmapSet[0] = false;
111
      }
112
113
    mMarked = false;
114 4e2382f3 Leszek Koltunski
    }
115
116 d558d9bd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
117
118
  long getID()
119 1942537e Leszek Koltunski
    {
120
    return mID;
121
    }
122 d558d9bd Leszek Koltunski
123 f8686932 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
124
125
  static void getUniforms(int mProgramH)
126
    {
127
    mTextureH= GLES20.glGetUniformLocation( mProgramH, "u_Texture");
128
129
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
130
    GLES20.glUniform1i(mTextureH, 0);
131
    }
132
133 4e2382f3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
134
135 7b8086eb Leszek Koltunski
  static synchronized void onDestroy()
136 4e2382f3 Leszek Koltunski
    {
137 1942537e Leszek Koltunski
    mListMarked = false;
138
    mList.clear();
139 4e2382f3 Leszek Koltunski
    }
140
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142 1942537e Leszek Koltunski
// must be called form a thread holding OpenGL Context
143 4e2382f3 Leszek Koltunski
144 1942537e Leszek Koltunski
  static synchronized void deleteAllMarked()
145 4e2382f3 Leszek Koltunski
    {
146 1942537e Leszek Koltunski
    if( mListMarked )
147 4e2382f3 Leszek Koltunski
      {
148 1942537e Leszek Koltunski
      DistortedTexture tmp;
149
      Iterator<DistortedTexture> iterator = mList.iterator();
150
151
      while(iterator.hasNext())
152
        {
153
        tmp = iterator.next();
154
155
        if( tmp.mMarked )
156
          {
157
          tmp.deleteTexture();
158
          iterator.remove();
159
          }
160
        }
161 4e2382f3 Leszek Koltunski
162 1942537e Leszek Koltunski
      mListMarked = false;
163
      }
164 4e2382f3 Leszek Koltunski
    }
165
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
// PUBLIC API
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169
/**
170
 * Create empty texture of given dimensions.
171
 */
172 3ef3364d Leszek Koltunski
  public DistortedTexture(int width, int height)
173 4e2382f3 Leszek Koltunski
    {
174 3ef3364d Leszek Koltunski
    mSizeX= width ; mHalfX = mSizeX/2.0f;
175
    mSizeY= height; mHalfY = mSizeY/2.0f;
176
177 4e2382f3 Leszek Koltunski
    mTextureDataH   = new int[1];
178
    mTextureDataH[0]= 0;
179
    mBmp            = new Bitmap[1];
180
    mBmp[0]         = null;
181
    mBitmapSet      = new boolean[1];
182
    mBitmapSet[0]   = false;
183 1942537e Leszek Koltunski
    mID             = 0;
184
    mMarked         = false;
185 4e2382f3 Leszek Koltunski
186 1942537e Leszek Koltunski
    mList.add(this);
187 4e2382f3 Leszek Koltunski
    }
188
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190
191
/**
192
 * Copy constructor.
193
 * <p>
194
 * Whatever we do not clone gets created just like in the default constructor.
195
 *
196
 * @param dt    Source object to create our object from
197
 * @param flags A bitmask of values specifying what to copy.
198 d558d9bd Leszek Koltunski
 *              Only possibilities: CLONE_BITMAP or CLONE_NOTHING.
199 4e2382f3 Leszek Koltunski
 */
200 d558d9bd Leszek Koltunski
201 4e2382f3 Leszek Koltunski
  public DistortedTexture(DistortedTexture dt, int flags)
202
    {
203 3ef3364d Leszek Koltunski
    mSizeX= dt.mSizeX ; mHalfX = mSizeX/2.0f;
204
    mSizeY= dt.mSizeY ; mHalfY = mSizeY/2.0f;
205 4e2382f3 Leszek Koltunski
206
    if( (flags & Distorted.CLONE_BITMAP) != 0 )
207
      {
208
      mTextureDataH = dt.mTextureDataH;
209
      mBmp          = dt.mBmp;
210
      mBitmapSet    = dt.mBitmapSet;
211 1942537e Leszek Koltunski
      mID           = dt.getID();
212 4e2382f3 Leszek Koltunski
      }
213
    else
214
      {
215
      mTextureDataH   = new int[1];
216
      mTextureDataH[0]= 0;
217
      mBitmapSet      = new boolean[1];
218
      mBitmapSet[0]   = false;
219
      mBmp            = new Bitmap[1];
220
      mBmp[0]         = null;
221 1942537e Leszek Koltunski
      mID             = 0;
222 4e2382f3 Leszek Koltunski
      }
223 1942537e Leszek Koltunski
224
    mMarked = false;
225
226
    mList.add(this);
227 4e2382f3 Leszek Koltunski
    }
228
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230
/**
231 1942537e Leszek Koltunski
 * Mark the underlying OpenGL object for deletion. Actual deletion will take place on the next render.
232 4e2382f3 Leszek Koltunski
 */
233 1942537e Leszek Koltunski
  public void markForDeletion()
234 4e2382f3 Leszek Koltunski
    {
235 1942537e Leszek Koltunski
    //android.util.Log.e("Texture", "marking for deletion "+mID);
236
237
    mListMarked = true;
238
    mMarked     = true;
239 4e2382f3 Leszek Koltunski
    }
240
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242
/**
243 1942537e Leszek Koltunski
 * Sets the underlying android.graphics.Bitmap object.
244 4e2382f3 Leszek Koltunski
 * <p>
245
 * You can only recycle() the passed Bitmap once the OpenGL context gets created (i.e. after call
246 432442f9 Leszek Koltunski
 * to GLSurfaceView.onSurfaceCreated) because only after this point can the Library upload it to the GPU!
247 4e2382f3 Leszek Koltunski
 *
248
 * @param bmp The android.graphics.Bitmap object to apply effects to and display.
249
 */
250
251
  public void setTexture(Bitmap bmp)
252
    {
253
    mBitmapSet[0] = true;
254 1942537e Leszek Koltunski
    mBmp[0]       = bmp;
255
    mID           = bmp.hashCode();
256 4e2382f3 Leszek Koltunski
257 1942537e Leszek Koltunski
    //android.util.Log.e("Texture", "setting new bitmap "+mID);
258 4e2382f3 Leszek Koltunski
    }
259
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261
/**
262 46e25345 Leszek Koltunski
 * Returns the height of the Texture.
263 4e2382f3 Leszek Koltunski
 *
264
 * @return height of the object, in pixels.
265
 */
266
  public int getWidth()
267 1942537e Leszek Koltunski
    {
268
    return mSizeX;
269
    }
270 4e2382f3 Leszek Koltunski
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272
/**
273 46e25345 Leszek Koltunski
 * Returns the width of the Texture.
274 4e2382f3 Leszek Koltunski
 *
275
 * @return width of the Object, in pixels.
276
 */
277
  public int getHeight()
278 1942537e Leszek Koltunski
    {
279
    return mSizeY;
280
    }
281 4e2382f3 Leszek Koltunski
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283
/**
284 46e25345 Leszek Koltunski
 * Returns the depth of the Texture.
285 3ef3364d Leszek Koltunski
 * <p>
286
 * Admittedly quite a strange method. Why do we need to pass a Grid to it? Because one cannot determine
287
 * 'depth' of a texture when rendered based only on the texture itself, that depends on the Grid it is
288
 * rendered with.
289 4e2382f3 Leszek Koltunski
 *
290
 * @return depth of the Object, in pixels.
291
 */
292 3ef3364d Leszek Koltunski
  public int getDepth(GridObject grid)
293 1942537e Leszek Koltunski
    {
294
    return grid==null ? 0 : (int)(mSizeX*grid.zFactor);
295
    }
296 4e2382f3 Leszek Koltunski
  }