Project

General

Profile

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

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

1 4e2382f3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 46b572b5 Leszek Koltunski
// This file is part of Distorted.                                                               //
5 4e2382f3 Leszek Koltunski
//                                                                                               //
6 46b572b5 Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 4e2382f3 Leszek Koltunski
// 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 46b572b5 Leszek Koltunski
// Distorted 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
// 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 46b572b5 Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 4e2382f3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 fe82a979 Leszek Koltunski
package org.distorted.library.main;
21 4e2382f3 Leszek Koltunski
22
import android.graphics.Bitmap;
23 11bf077b Leszek Koltunski
import android.graphics.Canvas;
24 4e2382f3 Leszek Koltunski
import android.graphics.Matrix;
25 11bf077b Leszek Koltunski
import android.graphics.Paint;
26 b7074bc6 Leszek Koltunski
import android.opengl.GLES30;
27 4e2382f3 Leszek Koltunski
import android.opengl.GLUtils;
28
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30 e0b6c593 Leszek Koltunski
/**
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 7602a827 Leszek Koltunski
public class DistortedTexture extends InternalSurface
36 4e2382f3 Leszek Koltunski
  {
37 9d845904 Leszek Koltunski
  private Bitmap mBmp;
38 f85f2943 Leszek Koltunski
  private boolean mBitmapInverted;
39 4e2382f3 Leszek Koltunski
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41 f85f2943 Leszek Koltunski
// We have to vertically flip all the bitmaps passed here via setTexture().
42 4e2382f3 Leszek Koltunski
//
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 a09ada4c Leszek Koltunski
// then take the FBO and render to screen, (DistortedNode does so!) things get inverted as textures
48 f85f2943 Leszek Koltunski
// created from FBO have their origins in the lower-left...
49 4e2382f3 Leszek Koltunski
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 1942537e Leszek Koltunski
// must be called from a thread holding OpenGL Context
60 4e2382f3 Leszek Koltunski
61 f8377ef8 leszek
  void create()
62 4e2382f3 Leszek Koltunski
    {
63 c7da4e65 leszek
    if( mBmp!=null )
64 4e2382f3 Leszek Koltunski
      {
65 c7da4e65 leszek
      if( mColorCreated==NOT_CREATED_YET )
66 05403bba Leszek Koltunski
        {
67 c7da4e65 leszek
        mColorCreated = CREATED;
68 b7074bc6 Leszek Koltunski
        GLES30.glGenTextures(1, mColorH, 0);
69 05403bba Leszek Koltunski
        }
70 4e2382f3 Leszek Koltunski
71 f0604575 Leszek Koltunski
      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 f85f2943 Leszek Koltunski
      GLUtils.texImage2D(GLES30.GL_TEXTURE_2D, 0, mBitmapInverted ? mBmp : flipBitmap(mBmp), 0);
77 f0604575 Leszek Koltunski
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
78
79 e7a20702 Leszek Koltunski
      mBmp = null;
80 4e2382f3 Leszek Koltunski
      }
81
    }
82
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84 1942537e Leszek Koltunski
// must be called from a thread holding OpenGL Context
85 4e2382f3 Leszek Koltunski
86 227ac49a Leszek Koltunski
  void delete()
87 4e2382f3 Leszek Koltunski
    {
88 05ecc6fe Leszek Koltunski
    if( mColorH[0]>0 )
89 1942537e Leszek Koltunski
      {
90 b7074bc6 Leszek Koltunski
      GLES30.glDeleteTextures(1, mColorH, 0);
91 c7da4e65 leszek
      mColorH[0] = 0;
92
      mColorCreated = NOT_CREATED_YET;
93 1942537e Leszek Koltunski
      }
94 4e2382f3 Leszek Koltunski
    }
95
96 4ebbb17a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
97
// called from onDestroy(); mark OpenGL assets as 'not created'
98
99 f8377ef8 leszek
  void recreate()
100 4ebbb17a Leszek Koltunski
    {
101 05ecc6fe Leszek Koltunski
    if( mColorCreated!=DONT_CREATE )
102
      {
103
      mColorCreated = NOT_CREATED_YET;
104
      mColorH[0] = 0;
105
      }
106 4ebbb17a Leszek Koltunski
    }
107 c5369f1b leszek
108 09ab7524 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 c90aca24 Leszek Koltunski
  public DistortedTexture(int type)
115 09ab7524 Leszek Koltunski
    {
116 9ec374e8 Leszek Koltunski
    super(NOT_CREATED_YET,1,1,type,InternalObject.STORAGE_PRIVATE);
117 09ab7524 Leszek Koltunski
    mBmp= null;
118
    }
119
120 4e2382f3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
121
// PUBLIC API
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123
/**
124 c90aca24 Leszek Koltunski
 * Create an empty texture.
125 4e2382f3 Leszek Koltunski
 */
126 c90aca24 Leszek Koltunski
  public DistortedTexture()
127 4e2382f3 Leszek Koltunski
    {
128 c90aca24 Leszek Koltunski
    this(TYPE_USER);
129 4e2382f3 Leszek Koltunski
    }
130
131 c5369f1b leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
132 4e2382f3 Leszek Koltunski
/**
133 1942537e Leszek Koltunski
 * Sets the underlying android.graphics.Bitmap object.
134 4e2382f3 Leszek Koltunski
 * <p>
135
 * You can only recycle() the passed Bitmap once the OpenGL context gets created (i.e. after call
136 432442f9 Leszek Koltunski
 * to GLSurfaceView.onSurfaceCreated) because only after this point can the Library upload it to the GPU!
137 4e2382f3 Leszek Koltunski
 *
138
 * @param bmp The android.graphics.Bitmap object to apply effects to and display.
139 2bd3d0fb Leszek Koltunski
 * @return true if successful, false if texture too large.
140 4e2382f3 Leszek Koltunski
 */
141 2bd3d0fb Leszek Koltunski
  public boolean setTexture(Bitmap bmp)
142 4e2382f3 Leszek Koltunski
    {
143 2bd3d0fb Leszek Koltunski
    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 f85f2943 Leszek Koltunski
      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 2bd3d0fb Leszek Koltunski
      mBmp= bmp;
187
      markForCreation();
188
      return true;
189
      }
190 1942537e Leszek Koltunski
    }
191 11bf077b Leszek Koltunski
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193
/**
194
 * Paints the Texture with solid color.
195
 *
196 4db11f0c Leszek Koltunski
 * @param argb The color to paint the Texture with. Example: 0xffff0000 - red.
197 11bf077b Leszek Koltunski
 */
198 246d021c Leszek Koltunski
  public void setColorARGB(int argb)
199 11bf077b Leszek Koltunski
    {
200
    Paint paint = new Paint();
201
    paint.setColor(argb);
202
    paint.setStyle(Paint.Style.FILL);
203
204 d58b50e7 Leszek Koltunski
    mBmp = Bitmap.createBitmap(1,1, Bitmap.Config.ARGB_8888);
205 11bf077b Leszek Koltunski
    Canvas canvas = new Canvas(mBmp);
206 d58b50e7 Leszek Koltunski
    canvas.drawRect(0,0,1,1,paint);
207 11bf077b Leszek Koltunski
208
    markForCreation();
209
    }
210 4e2382f3 Leszek Koltunski
  }