Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedTexture.java @ 8c57d77b

1 4e2382f3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2 2e569ff6 Leszek Koltunski
// Copyright 2016 Leszek Koltunski  leszek@koltunski.pl                                          //
3 4e2382f3 Leszek Koltunski
//                                                                                               //
4 46b572b5 Leszek Koltunski
// This file is part of Distorted.                                                               //
5 4e2382f3 Leszek Koltunski
//                                                                                               //
6 2e569ff6 Leszek Koltunski
// 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 4e2382f3 Leszek Koltunski
//                                                                                               //
11 2e569ff6 Leszek Koltunski
// This library is distributed in the hope that it will be useful,                               //
12 4e2382f3 Leszek Koltunski
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13 2e569ff6 Leszek Koltunski
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                             //
14
// Lesser General Public License for more details.                                               //
15 4e2382f3 Leszek Koltunski
//                                                                                               //
16 2e569ff6 Leszek Koltunski
// 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 4e2382f3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
20
21 fe82a979 Leszek Koltunski
package org.distorted.library.main;
22 4e2382f3 Leszek Koltunski
23
import android.graphics.Bitmap;
24 11bf077b Leszek Koltunski
import android.graphics.Canvas;
25 4e2382f3 Leszek Koltunski
import android.graphics.Matrix;
26 11bf077b Leszek Koltunski
import android.graphics.Paint;
27 b7074bc6 Leszek Koltunski
import android.opengl.GLES30;
28 4e2382f3 Leszek Koltunski
import android.opengl.GLUtils;
29
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
 */
36 7602a827 Leszek Koltunski
public class DistortedTexture extends InternalSurface
37 4e2382f3 Leszek Koltunski
  {
38 9d845904 Leszek Koltunski
  private Bitmap mBmp;
39 f85f2943 Leszek Koltunski
  private boolean mBitmapInverted;
40 4e2382f3 Leszek Koltunski
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42 f85f2943 Leszek Koltunski
// We have to vertically flip all the bitmaps passed here via setTexture().
43 4e2382f3 Leszek Koltunski
//
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 a09ada4c Leszek Koltunski
// then take the FBO and render to screen, (DistortedNode does so!) things get inverted as textures
49 f85f2943 Leszek Koltunski
// created from FBO have their origins in the lower-left...
50 4e2382f3 Leszek Koltunski
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 1942537e Leszek Koltunski
// must be called from a thread holding OpenGL Context
61 4e2382f3 Leszek Koltunski
62 f8377ef8 leszek
  void create()
63 4e2382f3 Leszek Koltunski
    {
64 c7da4e65 leszek
    if( mBmp!=null )
65 4e2382f3 Leszek Koltunski
      {
66 c7da4e65 leszek
      if( mColorCreated==NOT_CREATED_YET )
67 05403bba Leszek Koltunski
        {
68 c7da4e65 leszek
        mColorCreated = CREATED;
69 b7074bc6 Leszek Koltunski
        GLES30.glGenTextures(1, mColorH, 0);
70 05403bba Leszek Koltunski
        }
71 4e2382f3 Leszek Koltunski
72 f0604575 Leszek Koltunski
      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 f85f2943 Leszek Koltunski
      GLUtils.texImage2D(GLES30.GL_TEXTURE_2D, 0, mBitmapInverted ? mBmp : flipBitmap(mBmp), 0);
78 f0604575 Leszek Koltunski
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
79
80 e7a20702 Leszek Koltunski
      mBmp = null;
81 4e2382f3 Leszek Koltunski
      }
82
    }
83
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85 1942537e Leszek Koltunski
// must be called from a thread holding OpenGL Context
86 4e2382f3 Leszek Koltunski
87 227ac49a Leszek Koltunski
  void delete()
88 4e2382f3 Leszek Koltunski
    {
89 05ecc6fe Leszek Koltunski
    if( mColorH[0]>0 )
90 1942537e Leszek Koltunski
      {
91 b7074bc6 Leszek Koltunski
      GLES30.glDeleteTextures(1, mColorH, 0);
92 c7da4e65 leszek
      mColorH[0] = 0;
93
      mColorCreated = NOT_CREATED_YET;
94 1942537e Leszek Koltunski
      }
95 4e2382f3 Leszek Koltunski
    }
96
97 4ebbb17a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
98
// called from onDestroy(); mark OpenGL assets as 'not created'
99
100 f8377ef8 leszek
  void recreate()
101 4ebbb17a Leszek Koltunski
    {
102 05ecc6fe Leszek Koltunski
    if( mColorCreated!=DONT_CREATE )
103
      {
104
      mColorCreated = NOT_CREATED_YET;
105
      mColorH[0] = 0;
106
      }
107 4ebbb17a Leszek Koltunski
    }
108 c5369f1b leszek
109 09ab7524 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 c90aca24 Leszek Koltunski
  public DistortedTexture(int type)
116 09ab7524 Leszek Koltunski
    {
117 9ec374e8 Leszek Koltunski
    super(NOT_CREATED_YET,1,1,type,InternalObject.STORAGE_PRIVATE);
118 09ab7524 Leszek Koltunski
    mBmp= null;
119
    }
120
121 4e2382f3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
122
// PUBLIC API
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124
/**
125 c90aca24 Leszek Koltunski
 * Create an empty texture.
126 4e2382f3 Leszek Koltunski
 */
127 c90aca24 Leszek Koltunski
  public DistortedTexture()
128 4e2382f3 Leszek Koltunski
    {
129 c90aca24 Leszek Koltunski
    this(TYPE_USER);
130 4e2382f3 Leszek Koltunski
    }
131
132 c5369f1b leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
133 4e2382f3 Leszek Koltunski
/**
134 1942537e Leszek Koltunski
 * Sets the underlying android.graphics.Bitmap object.
135 4e2382f3 Leszek Koltunski
 * <p>
136
 * You can only recycle() the passed Bitmap once the OpenGL context gets created (i.e. after call
137 432442f9 Leszek Koltunski
 * to GLSurfaceView.onSurfaceCreated) because only after this point can the Library upload it to the GPU!
138 4e2382f3 Leszek Koltunski
 *
139
 * @param bmp The android.graphics.Bitmap object to apply effects to and display.
140 2bd3d0fb Leszek Koltunski
 * @return true if successful, false if texture too large.
141 4e2382f3 Leszek Koltunski
 */
142 2bd3d0fb Leszek Koltunski
  public boolean setTexture(Bitmap bmp)
143 4e2382f3 Leszek Koltunski
    {
144 2bd3d0fb Leszek Koltunski
    int width = bmp.getWidth();
145
    int height= bmp.getHeight();
146
    int max   = DistortedLibrary.getMaxTextureSize();
147
148
    if( width>max || height>max )
149
      {
150 8c57d77b Leszek Koltunski
      DistortedLibrary.logMessage("DistortedTexture: error, trying to upload too large texture of size "+width+" x "+height+". Max is "+max);
151 2bd3d0fb Leszek Koltunski
      return false;
152
      }
153
    else
154
      {
155 f85f2943 Leszek Koltunski
      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 8c57d77b Leszek Koltunski
      DistortedLibrary.logMessage("DistortedTexture: error, trying to upload too large texture of size "+width+" x "+height+". Max is "+max);
182 f85f2943 Leszek Koltunski
      return false;
183
      }
184
    else
185
      {
186
      mBitmapInverted = true;
187 2bd3d0fb Leszek Koltunski
      mBmp= bmp;
188
      markForCreation();
189
      return true;
190
      }
191 1942537e Leszek Koltunski
    }
192 11bf077b Leszek Koltunski
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194
/**
195
 * Paints the Texture with solid color.
196
 *
197 4db11f0c Leszek Koltunski
 * @param argb The color to paint the Texture with. Example: 0xffff0000 - red.
198 11bf077b Leszek Koltunski
 */
199 246d021c Leszek Koltunski
  public void setColorARGB(int argb)
200 11bf077b Leszek Koltunski
    {
201
    Paint paint = new Paint();
202
    paint.setColor(argb);
203
    paint.setStyle(Paint.Style.FILL);
204
205 d58b50e7 Leszek Koltunski
    mBmp = Bitmap.createBitmap(1,1, Bitmap.Config.ARGB_8888);
206 11bf077b Leszek Koltunski
    Canvas canvas = new Canvas(mBmp);
207 d58b50e7 Leszek Koltunski
    canvas.drawRect(0,0,1,1,paint);
208 11bf077b Leszek Koltunski
209
    markForCreation();
210
    }
211 4e2382f3 Leszek Koltunski
  }