Project

General

Profile

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

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

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 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 b7074bc6 Leszek Koltunski
        GLES30.glGenTextures(1, mColorH, 0);
68 05403bba Leszek Koltunski
        }
69 4e2382f3 Leszek Koltunski
70 f0604575 Leszek Koltunski
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
71
      GLES30.glTexParameteri ( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_LINEAR );
72
      GLES30.glTexParameteri ( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_LINEAR );
73
      GLES30.glTexParameteri ( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_CLAMP_TO_EDGE );
74
      GLES30.glTexParameteri ( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_CLAMP_TO_EDGE );
75
      GLUtils.texImage2D(GLES30.GL_TEXTURE_2D, 0, flipBitmap(mBmp), 0);
76
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
77
78 e7a20702 Leszek Koltunski
      mBmp = null;
79 4e2382f3 Leszek Koltunski
      }
80
    }
81
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83 1942537e Leszek Koltunski
// must be called from a thread holding OpenGL Context
84 4e2382f3 Leszek Koltunski
85 227ac49a Leszek Koltunski
  void delete()
86 4e2382f3 Leszek Koltunski
    {
87 05ecc6fe Leszek Koltunski
    if( mColorH[0]>0 )
88 1942537e Leszek Koltunski
      {
89 b7074bc6 Leszek Koltunski
      GLES30.glDeleteTextures(1, mColorH, 0);
90 c7da4e65 leszek
      mColorH[0] = 0;
91
      mColorCreated = NOT_CREATED_YET;
92 1942537e Leszek Koltunski
      }
93 4e2382f3 Leszek Koltunski
    }
94
95 4ebbb17a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
96
// called from onDestroy(); mark OpenGL assets as 'not created'
97
98 f8377ef8 leszek
  void recreate()
99 4ebbb17a Leszek Koltunski
    {
100 05ecc6fe Leszek Koltunski
    if( mColorCreated!=DONT_CREATE )
101
      {
102
      mColorCreated = NOT_CREATED_YET;
103
      mColorH[0] = 0;
104
      }
105 4ebbb17a Leszek Koltunski
    }
106 c5369f1b leszek
107 09ab7524 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
108
// create SYSTEM or TREE textures (those are just like normal Textures, just hold information
109
// that they were autocreated only for the Library's internal purposes (SYSTEM) or for using
110
// inside a Tree of DistortedNodes (TREE)
111
// SYSTEM surfaces do not get removed in onDestroy().
112
113 c90aca24 Leszek Koltunski
  public DistortedTexture(int type)
114 09ab7524 Leszek Koltunski
    {
115 9ec374e8 Leszek Koltunski
    super(NOT_CREATED_YET,1,1,type,InternalObject.STORAGE_PRIVATE);
116 09ab7524 Leszek Koltunski
    mBmp= null;
117
    }
118
119 4e2382f3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
120
// PUBLIC API
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122
/**
123 c90aca24 Leszek Koltunski
 * Create an empty texture.
124 4e2382f3 Leszek Koltunski
 */
125 c90aca24 Leszek Koltunski
  public DistortedTexture()
126 4e2382f3 Leszek Koltunski
    {
127 c90aca24 Leszek Koltunski
    this(TYPE_USER);
128 4e2382f3 Leszek Koltunski
    }
129
130 c5369f1b leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
131 4e2382f3 Leszek Koltunski
/**
132 1942537e Leszek Koltunski
 * Sets the underlying android.graphics.Bitmap object.
133 4e2382f3 Leszek Koltunski
 * <p>
134
 * You can only recycle() the passed Bitmap once the OpenGL context gets created (i.e. after call
135 432442f9 Leszek Koltunski
 * to GLSurfaceView.onSurfaceCreated) because only after this point can the Library upload it to the GPU!
136 4e2382f3 Leszek Koltunski
 *
137
 * @param bmp The android.graphics.Bitmap object to apply effects to and display.
138 2bd3d0fb Leszek Koltunski
 * @return true if successful, false if texture too large.
139 4e2382f3 Leszek Koltunski
 */
140 2bd3d0fb Leszek Koltunski
  public boolean setTexture(Bitmap bmp)
141 4e2382f3 Leszek Koltunski
    {
142 2bd3d0fb Leszek Koltunski
    int width = bmp.getWidth();
143
    int height= bmp.getHeight();
144
    int max   = DistortedLibrary.getMaxTextureSize();
145
146
    if( width>max || height>max )
147
      {
148
      android.util.Log.e("texture","error, trying to upload too large texture of size "+width+" x "+height+". Max is "+max);
149
      return false;
150
      }
151
    else
152
      {
153
      mBmp= bmp;
154
      markForCreation();
155
      return true;
156
      }
157 1942537e Leszek Koltunski
    }
158 11bf077b Leszek Koltunski
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160
/**
161
 * Paints the Texture with solid color.
162
 *
163 4db11f0c Leszek Koltunski
 * @param argb The color to paint the Texture with. Example: 0xffff0000 - red.
164 11bf077b Leszek Koltunski
 */
165 246d021c Leszek Koltunski
  public void setColorARGB(int argb)
166 11bf077b Leszek Koltunski
    {
167
    Paint paint = new Paint();
168
    paint.setColor(argb);
169
    paint.setStyle(Paint.Style.FILL);
170
171 d58b50e7 Leszek Koltunski
    mBmp = Bitmap.createBitmap(1,1, Bitmap.Config.ARGB_8888);
172 11bf077b Leszek Koltunski
    Canvas canvas = new Canvas(mBmp);
173 d58b50e7 Leszek Koltunski
    canvas.drawRect(0,0,1,1,paint);
174 11bf077b Leszek Koltunski
175
    markForCreation();
176
    }
177 4e2382f3 Leszek Koltunski
  }