Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedTexture.java @ 7a5e538a

1 4e2382f3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 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 e6519ac8 Leszek Koltunski
import android.opengl.GLES31;
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 12f9e4bb Leszek Koltunski
public class DistortedTexture extends DistortedSurface
36 4e2382f3 Leszek Koltunski
  {
37 9d845904 Leszek Koltunski
  private Bitmap mBmp;
38 4e2382f3 Leszek Koltunski
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40
// We have to flip vertically every single Bitmap that we get fed with.
41
//
42
// Reason: textures read from files are the only objects in OpenGL which have their origins at the
43
// upper-left corner. Everywhere else the origin is in the lower-left corner. Thus we have to flip.
44
// The alternative solution, namely inverting the y-coordinate of the TexCoord does not really work-
45
// i.e. it works only in case of rendering directly to the screen, but if we render to an FBO and
46 a09ada4c Leszek Koltunski
// then take the FBO and render to screen, (DistortedNode does so!) things get inverted as textures
47 4e2382f3 Leszek Koltunski
// created from FBO have their origins in the lower-left... Mindfuck!
48
49
  private static Bitmap flipBitmap(Bitmap src)
50
    {
51
    Matrix matrix = new Matrix();
52
    matrix.preScale(1.0f,-1.0f);
53
54
    return Bitmap.createBitmap(src,0,0,src.getWidth(),src.getHeight(), matrix,true);
55
    }
56
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58 1942537e Leszek Koltunski
// must be called from a thread holding OpenGL Context
59 4e2382f3 Leszek Koltunski
60 f8377ef8 leszek
  void create()
61 4e2382f3 Leszek Koltunski
    {
62 c7da4e65 leszek
    if( mBmp!=null )
63 4e2382f3 Leszek Koltunski
      {
64 c7da4e65 leszek
      if( mColorCreated==NOT_CREATED_YET )
65 05403bba Leszek Koltunski
        {
66 c7da4e65 leszek
        mColorCreated = CREATED;
67 e6519ac8 Leszek Koltunski
        GLES31.glGenTextures(1, mColorH, 0);
68
        GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, mColorH[0]);
69
        GLES31.glTexParameteri ( GLES31.GL_TEXTURE_2D, GLES31.GL_TEXTURE_MIN_FILTER, GLES31.GL_LINEAR );
70
        GLES31.glTexParameteri ( GLES31.GL_TEXTURE_2D, GLES31.GL_TEXTURE_MAG_FILTER, GLES31.GL_LINEAR );
71
        GLES31.glTexParameteri ( GLES31.GL_TEXTURE_2D, GLES31.GL_TEXTURE_WRAP_S, GLES31.GL_CLAMP_TO_EDGE );
72
        GLES31.glTexParameteri ( GLES31.GL_TEXTURE_2D, GLES31.GL_TEXTURE_WRAP_T, GLES31.GL_CLAMP_TO_EDGE );
73
        GLUtils.texImage2D(GLES31.GL_TEXTURE_2D, 0, flipBitmap(mBmp), 0);
74 05403bba Leszek Koltunski
        }
75
      else
76
        {
77 e6519ac8 Leszek Koltunski
        GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, mColorH[0]);
78
        GLUtils.texSubImage2D(GLES31.GL_TEXTURE_2D, 0,0,0,flipBitmap(mBmp));
79 05403bba Leszek Koltunski
        }
80 4e2382f3 Leszek Koltunski
81 e7a20702 Leszek Koltunski
      mBmp = null;
82 4e2382f3 Leszek Koltunski
      }
83
    }
84
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86 1942537e Leszek Koltunski
// must be called from a thread holding OpenGL Context
87 4e2382f3 Leszek Koltunski
88 227ac49a Leszek Koltunski
  void delete()
89 4e2382f3 Leszek Koltunski
    {
90 05ecc6fe Leszek Koltunski
    if( mColorH[0]>0 )
91 1942537e Leszek Koltunski
      {
92 e6519ac8 Leszek Koltunski
      GLES31.glDeleteTextures(1, mColorH, 0);
93 c7da4e65 leszek
      mColorH[0] = 0;
94
      mColorCreated = NOT_CREATED_YET;
95 1942537e Leszek Koltunski
      }
96 4e2382f3 Leszek Koltunski
    }
97
98 4ebbb17a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
99
// called from onDestroy(); mark OpenGL assets as 'not created'
100
101 f8377ef8 leszek
  void recreate()
102 4ebbb17a Leszek Koltunski
    {
103 05ecc6fe Leszek Koltunski
    if( mColorCreated!=DONT_CREATE )
104
      {
105
      mColorCreated = NOT_CREATED_YET;
106
      mColorH[0] = 0;
107
      }
108 4ebbb17a Leszek Koltunski
    }
109 c5369f1b leszek
110 09ab7524 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
111
// create SYSTEM or TREE textures (those are just like normal Textures, just hold information
112
// that they were autocreated only for the Library's internal purposes (SYSTEM) or for using
113
// inside a Tree of DistortedNodes (TREE)
114
// SYSTEM surfaces do not get removed in onDestroy().
115
116 eacd5df5 leszek
  public DistortedTexture(int width, int height, int type)
117 09ab7524 Leszek Koltunski
    {
118 9d845904 Leszek Koltunski
    super(width,height,NOT_CREATED_YET,1,1,type);
119 09ab7524 Leszek Koltunski
    mBmp= null;
120
    }
121
122 4e2382f3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
123
// PUBLIC API
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125
/**
126
 * Create empty texture of given dimensions.
127
 */
128 3ef3364d Leszek Koltunski
  public DistortedTexture(int width, int height)
129 4e2382f3 Leszek Koltunski
    {
130 9d845904 Leszek Koltunski
    super(width,height,NOT_CREATED_YET,1,1,TYPE_USER);
131 2e49718d Leszek Koltunski
    mBmp= null;
132 4e2382f3 Leszek Koltunski
    }
133
134 c5369f1b leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
135
/**
136
 * Bind the underlying rectangle of pixels as a OpenGL Texture.
137
 */
138
  public boolean setAsInput()
139
    {
140
    if( mColorH[0]>0 )
141
      {
142 e6519ac8 Leszek Koltunski
      GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
143
      GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, mColorH[0]);
144 c5369f1b leszek
      return true;
145
      }
146
147
    return false;
148
    }
149
150 4e2382f3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
151
/**
152 1942537e Leszek Koltunski
 * Sets the underlying android.graphics.Bitmap object.
153 4e2382f3 Leszek Koltunski
 * <p>
154
 * You can only recycle() the passed Bitmap once the OpenGL context gets created (i.e. after call
155 432442f9 Leszek Koltunski
 * to GLSurfaceView.onSurfaceCreated) because only after this point can the Library upload it to the GPU!
156 4e2382f3 Leszek Koltunski
 *
157
 * @param bmp The android.graphics.Bitmap object to apply effects to and display.
158
 */
159
  public void setTexture(Bitmap bmp)
160
    {
161 0c827acc Leszek Koltunski
    mBmp= bmp;
162 f28fffc2 Leszek Koltunski
    markForCreation();
163 1942537e Leszek Koltunski
    }
164 11bf077b Leszek Koltunski
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166
/**
167
 * Paints the Texture with solid color.
168
 *
169
 * @param argb The color to paint the Texture with.
170
 */
171
  public void setColor(int argb)
172
    {
173
    Paint paint = new Paint();
174
    paint.setColor(argb);
175
    paint.setStyle(Paint.Style.FILL);
176
177
    mBmp = Bitmap.createBitmap(1,1, Bitmap.Config.ARGB_8888);
178
    Canvas canvas = new Canvas(mBmp);
179
    canvas.drawRect(0,0,1,1,paint);
180
181
    markForCreation();
182
    }
183 4e2382f3 Leszek Koltunski
  }