Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedTexture.java @ 889cce10

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
package org.distorted.library;
21
22
import android.graphics.Bitmap;
23
import android.graphics.Matrix;
24 194ab46f Leszek Koltunski
import android.opengl.GLES30;
25 4e2382f3 Leszek Koltunski
import android.opengl.GLUtils;
26
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28 e0b6c593 Leszek Koltunski
/**
29
 * Class which represents a OpenGL Texture object.
30
 * <p>
31
 * Create a Texture of arbitrary size and feed some pixels to it via the setTexture() method.
32
 */
33 af4cc5db Leszek Koltunski
public class DistortedTexture extends DistortedSurface implements DistortedInputSurface
34 4e2382f3 Leszek Koltunski
  {
35 e7a20702 Leszek Koltunski
  private Bitmap mBmp= null;
36 4e2382f3 Leszek Koltunski
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38
// We have to flip vertically every single Bitmap that we get fed with.
39
//
40
// Reason: textures read from files are the only objects in OpenGL which have their origins at the
41
// upper-left corner. Everywhere else the origin is in the lower-left corner. Thus we have to flip.
42
// The alternative solution, namely inverting the y-coordinate of the TexCoord does not really work-
43
// i.e. it works only in case of rendering directly to the screen, but if we render to an FBO and
44 a09ada4c Leszek Koltunski
// then take the FBO and render to screen, (DistortedNode does so!) things get inverted as textures
45 4e2382f3 Leszek Koltunski
// created from FBO have their origins in the lower-left... Mindfuck!
46
47
  private static Bitmap flipBitmap(Bitmap src)
48
    {
49
    Matrix matrix = new Matrix();
50
    matrix.preScale(1.0f,-1.0f);
51
52
    return Bitmap.createBitmap(src,0,0,src.getWidth(),src.getHeight(), matrix,true);
53
    }
54
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56 1942537e Leszek Koltunski
// must be called from a thread holding OpenGL Context
57 4e2382f3 Leszek Koltunski
58 f8377ef8 leszek
  void create()
59 4e2382f3 Leszek Koltunski
    {
60 c7da4e65 leszek
    if( mBmp!=null )
61 4e2382f3 Leszek Koltunski
      {
62 c7da4e65 leszek
      if( mColorCreated==NOT_CREATED_YET )
63 05403bba Leszek Koltunski
        {
64 c7da4e65 leszek
        mColorCreated = CREATED;
65 133cbb2b Leszek Koltunski
        GLES30.glGenTextures(1, mColorH, 0);
66
        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
67 194ab46f Leszek Koltunski
        GLES30.glTexParameteri ( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_LINEAR );
68
        GLES30.glTexParameteri ( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_LINEAR );
69
        GLES30.glTexParameteri ( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_CLAMP_TO_EDGE );
70
        GLES30.glTexParameteri ( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_CLAMP_TO_EDGE );
71
        GLUtils.texImage2D(GLES30.GL_TEXTURE_2D, 0, flipBitmap(mBmp), 0);
72 05403bba Leszek Koltunski
        }
73
      else
74
        {
75 133cbb2b Leszek Koltunski
        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
76 194ab46f Leszek Koltunski
        GLUtils.texSubImage2D(GLES30.GL_TEXTURE_2D, 0,0,0,flipBitmap(mBmp));
77 05403bba Leszek Koltunski
        }
78 4e2382f3 Leszek Koltunski
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 c7da4e65 leszek
    if( mColorH[0]>=0 )
89 1942537e Leszek Koltunski
      {
90 133cbb2b 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 c7da4e65 leszek
    if( mColorCreated!=DONT_CREATE ) mColorCreated = NOT_CREATED_YET;
102 4ebbb17a Leszek Koltunski
    }
103 c5369f1b leszek
104 f8686932 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
105
106
  static void getUniforms(int mProgramH)
107
    {
108 2e49718d Leszek Koltunski
    int textureH= GLES30.glGetUniformLocation( mProgramH, "u_Texture");
109 f8686932 Leszek Koltunski
110 194ab46f Leszek Koltunski
    GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
111 2e49718d Leszek Koltunski
    GLES30.glUniform1i(textureH, 0);
112 4e2382f3 Leszek Koltunski
    }
113
114 09ab7524 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
115
// create SYSTEM or TREE textures (those are just like normal Textures, just hold information
116
// that they were autocreated only for the Library's internal purposes (SYSTEM) or for using
117
// inside a Tree of DistortedNodes (TREE)
118
// SYSTEM surfaces do not get removed in onDestroy().
119
120
  public DistortedTexture(int width, int height, int type)
121
    {
122
    super(width,height,NOT_CREATED_YET,type);
123
    mBmp= null;
124
    }
125
126 4e2382f3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
127
// PUBLIC API
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
/**
130
 * Create empty texture of given dimensions.
131
 */
132 3ef3364d Leszek Koltunski
  public DistortedTexture(int width, int height)
133 4e2382f3 Leszek Koltunski
    {
134 09ab7524 Leszek Koltunski
    super(width,height,NOT_CREATED_YET,TYPE_USER);
135 2e49718d Leszek Koltunski
    mBmp= null;
136 4e2382f3 Leszek Koltunski
    }
137
138 c5369f1b leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
139
/**
140
 * Bind the underlying rectangle of pixels as a OpenGL Texture.
141
 */
142
  public boolean setAsInput()
143
    {
144
    if( mColorH[0]>0 )
145
      {
146
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
147
      return true;
148
      }
149
150
    return false;
151
    }
152
153 4e2382f3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
154
/**
155 1942537e Leszek Koltunski
 * Sets the underlying android.graphics.Bitmap object.
156 4e2382f3 Leszek Koltunski
 * <p>
157
 * You can only recycle() the passed Bitmap once the OpenGL context gets created (i.e. after call
158 432442f9 Leszek Koltunski
 * to GLSurfaceView.onSurfaceCreated) because only after this point can the Library upload it to the GPU!
159 4e2382f3 Leszek Koltunski
 *
160
 * @param bmp The android.graphics.Bitmap object to apply effects to and display.
161
 */
162
  public void setTexture(Bitmap bmp)
163
    {
164 0c827acc Leszek Koltunski
    mBmp= bmp;
165 f28fffc2 Leszek Koltunski
    markForCreation();
166 1942537e Leszek Koltunski
    }
167 4e2382f3 Leszek Koltunski
  }