Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedTexture.java @ 86782a25

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
import android.opengl.GLES30;
25
import android.opengl.GLUtils;
26

    
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28
/**
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
public class DistortedTexture extends DistortedSurface implements DistortedInputSurface
34
  {
35
  private Bitmap mBmp= null;
36

    
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
// then take the FBO and render to screen, (DistortedNode does so!) things get inverted as textures
45
// 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
// must be called from a thread holding OpenGL Context
57

    
58
  void create()
59
    {
60
    if( mBmp!=null )
61
      {
62
      if( mColorCreated==NOT_CREATED_YET )
63
        {
64
        mColorCreated = CREATED;
65
        GLES30.glGenTextures(1, mColorH, 0);
66
        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
67
        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
        }
73
      else
74
        {
75
        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
76
        GLUtils.texSubImage2D(GLES30.GL_TEXTURE_2D, 0,0,0,flipBitmap(mBmp));
77
        }
78

    
79
      mBmp = null;
80
      }
81
    }
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84
// must be called from a thread holding OpenGL Context
85

    
86
  void delete()
87
    {
88
    if( mColorH[0]>=0 )
89
      {
90
      GLES30.glDeleteTextures(1, mColorH, 0);
91
      mColorH[0] = 0;
92
      mColorCreated = NOT_CREATED_YET;
93
      }
94
    }
95

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
// called from onDestroy(); mark OpenGL assets as 'not created'
98

    
99
  void recreate()
100
    {
101
    if( mColorCreated!=DONT_CREATE ) mColorCreated = NOT_CREATED_YET;
102
    }
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105
// create SYSTEM or TREE textures (those are just like normal Textures, just hold information
106
// that they were autocreated only for the Library's internal purposes (SYSTEM) or for using
107
// inside a Tree of DistortedNodes (TREE)
108
// SYSTEM surfaces do not get removed in onDestroy().
109

    
110
  public DistortedTexture(int width, int height, int type)
111
    {
112
    super(width,height,NOT_CREATED_YET,type);
113
    mBmp= null;
114
    }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117
// PUBLIC API
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
/**
120
 * Create empty texture of given dimensions.
121
 */
122
  public DistortedTexture(int width, int height)
123
    {
124
    super(width,height,NOT_CREATED_YET,TYPE_USER);
125
    mBmp= null;
126
    }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
/**
130
 * Bind the underlying rectangle of pixels as a OpenGL Texture.
131
 */
132
  public boolean setAsInput()
133
    {
134
    if( mColorH[0]>0 )
135
      {
136
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
137
      return true;
138
      }
139

    
140
    return false;
141
    }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144
/**
145
 * Sets the underlying android.graphics.Bitmap object.
146
 * <p>
147
 * You can only recycle() the passed Bitmap once the OpenGL context gets created (i.e. after call
148
 * to GLSurfaceView.onSurfaceCreated) because only after this point can the Library upload it to the GPU!
149
 *
150
 * @param bmp The android.graphics.Bitmap object to apply effects to and display.
151
 */
152
  public void setTexture(Bitmap bmp)
153
    {
154
    mBmp= bmp;
155
    markForCreation();
156
    }
157
  }
(12-12/23)