Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedTexture.java @ 133cbb2b

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.GLES30;
25
import android.opengl.GLUtils;
26

    
27
import java.util.Iterator;
28
import java.util.LinkedList;
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
 * <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
public class DistortedTexture extends DistortedRenderable
43
  {
44
  private static boolean mListMarked = false;
45
  private static LinkedList<DistortedTexture> mList = new LinkedList<>();
46

    
47
  private static int mTextureH;
48

    
49
  private boolean mMarked;
50
  private Bitmap mBmp= null;
51

    
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
// then take the FBO and render to screen, (DistortedTree does so!) things get inverted as textures
60
// 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
// must be called from a thread holding OpenGL Context
72

    
73
  void create()
74
    {
75
    if( mBmp!=null && mColorH !=null )
76
      {
77
      if( mColorH[0]==0 )
78
        {
79
        GLES30.glGenTextures(1, mColorH, 0);
80
        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
81
        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
        }
87
      else
88
        {
89
        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
90
        GLUtils.texSubImage2D(GLES30.GL_TEXTURE_2D, 0,0,0,flipBitmap(mBmp));
91
        }
92

    
93
      mBmp = null;
94
      }
95
    }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
// must be called from a thread holding OpenGL Context
99

    
100
  private void delete()
101
    {
102
    if( mColorH !=null && mColorH[0]>0 )
103
      {
104
      GLES30.glDeleteTextures(1, mColorH, 0);
105
      mColorH[0] = 0;
106
      }
107

    
108
    mMarked = false;
109
    }
110

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

    
113
  static void getUniforms(int mProgramH)
114
    {
115
    mTextureH= GLES30.glGetUniformLocation( mProgramH, "u_Texture");
116

    
117
    GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
118
    GLES30.glUniform1i(mTextureH, 0);
119
    }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

    
123
  static synchronized void onDestroy()
124
    {
125
    mListMarked = false;
126
    mList.clear();
127
    }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130
// must be called form a thread holding OpenGL Context
131

    
132
  static synchronized void deleteAllMarked()
133
    {
134
    if( mListMarked )
135
      {
136
      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
          tmp.delete();
146
          iterator.remove();
147
          }
148
        }
149

    
150
      mListMarked = false;
151
      }
152
    }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155
// PUBLIC API
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157
/**
158
 * Create empty texture of given dimensions.
159
 */
160
  public DistortedTexture(int width, int height)
161
    {
162
    mSizeX    = width ;
163
    mSizeY    = height;
164
    mColorH[0]= 0;
165
    mBmp      = null;
166
    mMarked   = false;
167

    
168
    mList.add(this);
169
    }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172
/**
173
 * Mark the underlying OpenGL object for deletion. Actual deletion will take place on the next render.
174
 */
175
  public void markForDeletion()
176
    {
177
    mListMarked = true;
178
    mMarked     = true;
179
    }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182
/**
183
 * Sets the underlying android.graphics.Bitmap object.
184
 * <p>
185
 * You can only recycle() the passed Bitmap once the OpenGL context gets created (i.e. after call
186
 * to GLSurfaceView.onSurfaceCreated) because only after this point can the Library upload it to the GPU!
187
 *
188
 * @param bmp The android.graphics.Bitmap object to apply effects to and display.
189
 */
190

    
191
  public void setTexture(Bitmap bmp)
192
    {
193
    mBmp= bmp;
194
    }
195
  }
(5-5/17)