Project

General

Profile

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

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

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.main;
21

    
22
import android.graphics.Bitmap;
23
import android.graphics.Canvas;
24
import android.graphics.Matrix;
25
import android.graphics.Paint;
26
import android.opengl.GLES30;
27
import android.opengl.GLUtils;
28

    
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30
/**
31
 * Class which represents a OpenGL Texture object.
32
 * <p>
33
 * Create a Texture of arbitrary size and feed some pixels to it via the setTexture() method.
34
 */
35
public class DistortedTexture extends InternalSurface
36
  {
37
  private Bitmap mBmp;
38
  private boolean mBitmapInverted;
39

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41
// We have to vertically flip all the bitmaps passed here via setTexture().
42
//
43
// Reason: textures read from files are the only objects in OpenGL which have their origins at the
44
// upper-left corner. Everywhere else the origin is in the lower-left corner. Thus we have to flip.
45
// The alternative solution, namely inverting the y-coordinate of the TexCoord does not really work-
46
// i.e. it works only in case of rendering directly to the screen, but if we render to an FBO and
47
// then take the FBO and render to screen, (DistortedNode does so!) things get inverted as textures
48
// created from FBO have their origins in the lower-left...
49

    
50
  private static Bitmap flipBitmap(Bitmap src)
51
    {
52
    Matrix matrix = new Matrix();
53
    matrix.preScale(1.0f,-1.0f);
54

    
55
    return Bitmap.createBitmap(src,0,0,src.getWidth(),src.getHeight(), matrix,true);
56
    }
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59
// must be called from a thread holding OpenGL Context
60

    
61
  void create()
62
    {
63
    if( mBmp!=null )
64
      {
65
      if( mColorCreated==NOT_CREATED_YET )
66
        {
67
        mColorCreated = CREATED;
68
        GLES30.glGenTextures(1, mColorH, 0);
69
        }
70

    
71
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
72
      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, mBitmapInverted ? mBmp : flipBitmap(mBmp), 0);
77
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
78

    
79
      mBmp = null;
80
      }
81
    }
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84
// must be called from a thread holding OpenGL Context
85

    
86
  void delete()
87
    {
88
    if( mColorH[0]>0 )
89
      {
90
      GLES30.glDeleteTextures(1, mColorH, 0);
91
      mColorH[0] = 0;
92
      mColorCreated = NOT_CREATED_YET;
93
      }
94
    }
95

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
// called from onDestroy(); mark OpenGL assets as 'not created'
98

    
99
  void recreate()
100
    {
101
    if( mColorCreated!=DONT_CREATE )
102
      {
103
      mColorCreated = NOT_CREATED_YET;
104
      mColorH[0] = 0;
105
      }
106
    }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
// create SYSTEM or TREE textures (those are just like normal Textures, just hold information
110
// that they were autocreated only for the Library's internal purposes (SYSTEM) or for using
111
// inside a Tree of DistortedNodes (TREE)
112
// SYSTEM surfaces do not get removed in onDestroy().
113

    
114
  public DistortedTexture(int type)
115
    {
116
    super(NOT_CREATED_YET,1,1,type,InternalObject.STORAGE_PRIVATE);
117
    mBmp= null;
118
    }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
// PUBLIC API
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123
/**
124
 * Create an empty texture.
125
 */
126
  public DistortedTexture()
127
    {
128
    this(TYPE_USER);
129
    }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
/**
133
 * Sets the underlying android.graphics.Bitmap object.
134
 * <p>
135
 * You can only recycle() the passed Bitmap once the OpenGL context gets created (i.e. after call
136
 * to GLSurfaceView.onSurfaceCreated) because only after this point can the Library upload it to the GPU!
137
 *
138
 * @param bmp The android.graphics.Bitmap object to apply effects to and display.
139
 * @return true if successful, false if texture too large.
140
 */
141
  public boolean setTexture(Bitmap bmp)
142
    {
143
    int width = bmp.getWidth();
144
    int height= bmp.getHeight();
145
    int max   = DistortedLibrary.getMaxTextureSize();
146

    
147
    if( width>max || height>max )
148
      {
149
      android.util.Log.e("texture","error, trying to upload too large texture of size "+width+" x "+height+". Max is "+max);
150
      return false;
151
      }
152
    else
153
      {
154
      mBitmapInverted = false;
155
      mBmp= bmp;
156
      markForCreation();
157
      return true;
158
      }
159
    }
160

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162
/**
163
 * Sets the underlying android.graphics.Bitmap object - this version assumes the object is already
164
 * flipped upside down.
165
 * <p>
166
 * You can only recycle() the passed Bitmap once the OpenGL context gets created (i.e. after call
167
 * to GLSurfaceView.onSurfaceCreated) because only after this point can the Library upload it to the GPU!
168
 *
169
 * @param bmp The (vertically flipped!) android.graphics.Bitmap object to apply effects to and display.
170
 * @return true if successful, false if texture too large.
171
 */
172
  public boolean setTextureAlreadyInverted(Bitmap bmp)
173
    {
174
    int width = bmp.getWidth();
175
    int height= bmp.getHeight();
176
    int max   = DistortedLibrary.getMaxTextureSize();
177

    
178
    if( width>max || height>max )
179
      {
180
      android.util.Log.e("texture","error, trying to upload too large texture of size "+width+" x "+height+". Max is "+max);
181
      return false;
182
      }
183
    else
184
      {
185
      mBitmapInverted = true;
186
      mBmp= bmp;
187
      markForCreation();
188
      return true;
189
      }
190
    }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193
/**
194
 * Paints the Texture with solid color.
195
 *
196
 * @param argb The color to paint the Texture with. Example: 0xffff0000 - red.
197
 */
198
  public void setColorARGB(int argb)
199
    {
200
    Paint paint = new Paint();
201
    paint.setColor(argb);
202
    paint.setStyle(Paint.Style.FILL);
203

    
204
    mBmp = Bitmap.createBitmap(1,1, Bitmap.Config.ARGB_8888);
205
    Canvas canvas = new Canvas(mBmp);
206
    canvas.drawRect(0,0,1,1,paint);
207

    
208
    markForCreation();
209
    }
210
  }
(6-6/17)