Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedTexture.java @ 46e25345

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.graphics.Bitmap;
23
import android.graphics.Matrix;
24
import android.opengl.GLES20;
25
import android.opengl.GLUtils;
26

    
27
import java.util.Arrays;
28
import java.util.HashMap;
29

    
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31

    
32
public class DistortedTexture
33
  {
34
  private static long mNextID =0;
35
  private static HashMap<Long,DistortedTexture> mTextures = new HashMap<>();
36

    
37
  private int mSizeX, mSizeY;  // in screen space
38
  float mHalfX, mHalfY;        // halves of the above
39

    
40
  private long mID;
41
  private long mBitmapID=0;
42

    
43
  private Bitmap[] mBmp= null; //
44
  int[] mTextureDataH;         // have to be shared among all the cloned Objects
45
  boolean[] mBitmapSet;        //
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48
// We have to flip vertically every single Bitmap that we get fed with.
49
//
50
// Reason: textures read from files are the only objects in OpenGL which have their origins at the
51
// upper-left corner. Everywhere else the origin is in the lower-left corner. Thus we have to flip.
52
// The alternative solution, namely inverting the y-coordinate of the TexCoord does not really work-
53
// i.e. it works only in case of rendering directly to the screen, but if we render to an FBO and
54
// then take the FBO and render to screen, (DistortedObjectTree does so!) things get inverted as textures
55
// created from FBO have their origins in the lower-left... Mindfuck!
56

    
57
  private static Bitmap flipBitmap(Bitmap src)
58
    {
59
    Matrix matrix = new Matrix();
60
    matrix.preScale(1.0f,-1.0f);
61

    
62
    return Bitmap.createBitmap(src,0,0,src.getWidth(),src.getHeight(), matrix,true);
63
    }
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66
// this will be called on startup and every time OpenGL context has been lost
67
// also call this from the constructor if the OpenGL context has been created already.
68

    
69
  private void resetTexture()
70
    {
71
    if( mTextureDataH!=null )
72
      {
73
      if( mTextureDataH[0]==0 ) GLES20.glGenTextures(1, mTextureDataH, 0);
74

    
75
      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataH[0]);
76
      GLES20.glTexParameteri ( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR );
77
      GLES20.glTexParameteri ( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR );
78
      GLES20.glTexParameteri ( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE );
79
      GLES20.glTexParameteri ( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE );
80

    
81
      if( mBmp!=null && mBmp[0]!=null)
82
        {
83
        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, flipBitmap(mBmp[0]), 0);
84
        mBmp[0] = null;
85
        }
86
      }
87
    }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

    
91
  private void releasePriv()
92
    {
93
    mBmp          = null;
94
    mTextureDataH = null;
95
    }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

    
99
  long getBitmapID()
100
    {
101
    if( mBmp!=null && mBitmapID==0 ) mBitmapID = Arrays.hashCode(mBmp);
102
    return mBitmapID;
103
    }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

    
107
  long getID()
108
      {
109
      return mID;
110
      }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

    
114
  static synchronized void reset()
115
    {
116
    for(long id: mTextures.keySet())
117
      {
118
      mTextures.get(id).resetTexture();
119
      }
120
    }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
  static synchronized void release()
125
    {
126
    for(long id: mTextures.keySet())
127
      {
128
      mTextures.get(id).releasePriv();
129
      }
130

    
131
    mTextures.clear();
132
    mNextID = 0;
133
    }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136
// PUBLIC API
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138
/**
139
 * Create empty texture of given dimensions.
140
 */
141
  public DistortedTexture(int width, int height)
142
    {
143
    mID = mNextID++;
144
    mTextures.put(mID,this);
145

    
146
    mSizeX= width ; mHalfX = mSizeX/2.0f;
147
    mSizeY= height; mHalfY = mSizeY/2.0f;
148

    
149
    mTextureDataH   = new int[1];
150
    mTextureDataH[0]= 0;
151
    mBmp            = new Bitmap[1];
152
    mBmp[0]         = null;
153
    mBitmapSet      = new boolean[1];
154
    mBitmapSet[0]   = false;
155

    
156
    if( Distorted.isInitialized() ) resetTexture();
157
    }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

    
161
/**
162
 * Copy constructor.
163
 * <p>
164
 * Whatever we do not clone gets created just like in the default constructor.
165
 *
166
 * @param dt    Source object to create our object from
167
 * @param flags A bitmask of values specifying what to copy.
168
 *              Only possibilities: CLONE_BITMAP or CLONE_NOTHING.
169
 */
170

    
171
  public DistortedTexture(DistortedTexture dt, int flags)
172
    {
173
    mID = mNextID++;
174
    mTextures.put(mID,this);
175

    
176
    mSizeX= dt.mSizeX ; mHalfX = mSizeX/2.0f;
177
    mSizeY= dt.mSizeY ; mHalfY = mSizeY/2.0f;
178

    
179
    if( (flags & Distorted.CLONE_BITMAP) != 0 )
180
      {
181
      mTextureDataH = dt.mTextureDataH;
182
      mBmp          = dt.mBmp;
183
      mBitmapSet    = dt.mBitmapSet;
184
      }
185
    else
186
      {
187
      mTextureDataH   = new int[1];
188
      mTextureDataH[0]= 0;
189
      mBitmapSet      = new boolean[1];
190
      mBitmapSet[0]   = false;
191
      mBmp            = new Bitmap[1];
192
      mBmp[0]         = null;
193

    
194
      if( Distorted.isInitialized() ) resetTexture();
195
      }
196
    }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199
/**
200
 * Releases all resources.
201
 */
202
  public synchronized void delete()
203
    {
204
    releasePriv();
205
    mTextures.remove(this);
206
    }
207

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209
/**
210
 * Sets the underlying android.graphics.Bitmap object and uploads it to the GPU.
211
 * <p>
212
 * You can only recycle() the passed Bitmap once the OpenGL context gets created (i.e. after call
213
 * to onSurfaceCreated) because only after this point can the Library upload it to the GPU!
214
 *
215
 * @param bmp The android.graphics.Bitmap object to apply effects to and display.
216
 */
217

    
218
  public void setTexture(Bitmap bmp)
219
    {
220
    mBitmapSet[0] = true;
221

    
222
    if( Distorted.isInitialized() )
223
      {
224
      GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
225
      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataH[0]);
226
      GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, flipBitmap(bmp), 0);
227
      }
228
    else
229
      {
230
      mBmp[0] = bmp;
231
      }
232
    }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235
/**
236
 * Returns the height of the Texture.
237
 *
238
 * @return height of the object, in pixels.
239
 */
240
  public int getWidth()
241
     {
242
     return mSizeX;
243
     }
244

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246
/**
247
 * Returns the width of the Texture.
248
 *
249
 * @return width of the Object, in pixels.
250
 */
251
  public int getHeight()
252
      {
253
      return mSizeY;
254
      }
255

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257
/**
258
 * Returns the depth of the Texture.
259
 * <p>
260
 * Admittedly quite a strange method. Why do we need to pass a Grid to it? Because one cannot determine
261
 * 'depth' of a texture when rendered based only on the texture itself, that depends on the Grid it is
262
 * rendered with.
263
 *
264
 * @return depth of the Object, in pixels.
265
 */
266
  public int getDepth(GridObject grid)
267
      {
268
      return grid==null ? 0 : (int)(mSizeX*grid.zFactor);
269
      }
270
  }
(5-5/15)