Project

General

Profile

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

library / src / main / java / org / distorted / library / Distorted.java @ 86eb00a9

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.content.Context;
23
import android.content.res.Resources;
24
import android.opengl.GLES20;
25
import org.distorted.library.program.*;
26

    
27
import java.io.InputStream;
28

    
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30
/**
31
 * A singleton class used to control various global settings.
32
 */
33
public class Distorted 
34
  {
35
  /**
36
   * When creating an instance of a DistortedTexture (or Tree) from another instance, do not clone anything.
37
   * Used in the copy constructor.
38
   */
39
  public static final int CLONE_NOTHING = 0x0;
40
  /**
41
   * When creating an instance of a DistortedTexture from another instance, clone the Bitmap that's
42
   * backing up our DistortedTexture.
43
   * <p>
44
   * This way we can have two DistortedTextures, both backed up by the same Bitmap, to which we can
45
   * apply different effects. Used in the copy constructor.
46
   */
47
  public static final int CLONE_BITMAP  = 0x1;
48
  /**
49
   * When creating an instance of a DistortedEffects from another instance, clone the Matrix Effects.
50
   * <p>
51
   * This way we can have two different DistortedEffects sharing the MATRIX queue.
52
   */
53
  public static final int CLONE_MATRIX = 0x2;
54
  /**
55
   * When creating an instance of a DistortedEffects from another instance, clone the Vertex Effects.
56
   * <p>
57
   * This way we can have two different DistortedEffects sharing the VERTEX queue.
58
   */
59
  public static final int CLONE_VERTEX  = 0x4;
60
  /**
61
   * When creating an instance of a DistortedEffects from another instance, clone the Fragment Effects.
62
   * <p>
63
   * This way we can have two different DistortedEffects sharing the FRAGMENT queue.
64
   */
65
  public static final int CLONE_FRAGMENT= 0x8;
66
   /**
67
   * When creating an instance of a DistortedEffects from another instance, clone the PostProcess Effects.
68
   * <p>
69
   * This way we can have two different DistortedEffects sharing the POSTPROCESS queue.
70
   */
71
  public static final int CLONE_POSTPROCESS= 0x10;
72
  /**
73
   * When creating an instance of a DistortedTree from another instance, clone the children Nodes.
74
   * <p>
75
   * This is mainly useful for creating many similar sub-trees and rendering then at different places
76
   * on the screen with (optionally) different Effects.
77
   */
78
  public static final int CLONE_CHILDREN= 0x20;
79

    
80
  static int[] mMainProgramAttributes;
81
  static int[] mPostProgramAttributes;
82

    
83
  static int mainProgramH, postProgramH;
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86
// private: hide this from Javadoc
87

    
88
  private Distorted()
89
    {
90
    
91
    }
92

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

    
95
  static boolean isInitialized()
96
    {
97
    return (mMainProgramAttributes!=null);
98
    }
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101
/**
102
 * When OpenGL context gets created, you need to call this method so that the library can initialise its internal data structures.
103
 * I.e. best called from GLSurfaceView.onCreate().
104
 * <p>
105
 * Needs to be called from a thread holding the OpenGL context.
106
 *   
107
 * @param context Context of the App using the library - used to open up Resources and read Shader code.
108
 * @throws FragmentCompilationException
109
 * @throws VertexCompilationException
110
 * @throws VertexUniformsException
111
 * @throws FragmentUniformsException
112
 * @throws LinkingException
113
 */
114
  public static void onCreate(final Context context)
115
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
116
    {
117
    final Resources resources = context.getResources();
118

    
119
    final InputStream mainVertexStream   = resources.openRawResource(R.raw.main_vertex_shader);
120
    final InputStream mainFragmentStream = resources.openRawResource(R.raw.main_fragment_shader);
121

    
122
    String mainVertexHeader= ("#version 100\n#define NUM_VERTEX "  + DistortedEffects.getMaxVertex()+"\n");
123

    
124
    for(EffectNames name: EffectNames.values() )
125
      {
126
      if( name.getType()== EffectTypes.VERTEX)
127
        mainVertexHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
128
      }
129

    
130
    String mainFragmentHeader= ("#version 100\n#define NUM_FRAGMENT "  + DistortedEffects.getMaxFragment()+"\n");
131

    
132
    for(EffectNames name: EffectNames.values() )
133
      {
134
      if( name.getType()== EffectTypes.FRAGMENT)
135
        mainFragmentHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
136
      }
137

    
138
    DistortedProgram mainProgram = new DistortedProgram(mainVertexStream,mainFragmentStream, mainVertexHeader, mainFragmentHeader);
139
    mainProgramH = mainProgram.getProgramHandle();
140
    GLES20.glUseProgram(mainProgramH);
141
    mainProgram.bindAndEnableAttributes();
142
    mMainProgramAttributes = mainProgram.getAttributes();
143

    
144
    GLES20.glDepthFunc(GLES20.GL_LEQUAL);
145
    GLES20.glEnable(GLES20.GL_BLEND);
146
    GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
147
    GLES20.glEnable(GLES20.GL_CULL_FACE);
148
    GLES20.glCullFace(GLES20.GL_BACK);
149
    GLES20.glFrontFace(GLES20.GL_CW);
150

    
151
    EffectQueueFragment.getUniforms(mainProgramH);
152
    EffectQueueVertex.getUniforms(mainProgramH);
153
    EffectQueueMatrix.getUniforms(mainProgramH);
154
    DistortedTexture.getUniforms(mainProgramH);
155

    
156
    final InputStream postVertexStream   = resources.openRawResource(R.raw.post_vertex_shader);
157
    final InputStream postFragmentStream = resources.openRawResource(R.raw.post_fragment_shader);
158

    
159
    String postFragmentHeader= ("#version 100\n#define NUM_POSTPROCESS "  + DistortedEffects.getMaxPostprocess()+"\n");
160

    
161
    for(EffectNames name: EffectNames.values() )
162
      {
163
      if( name.getType()== EffectTypes.POSTPROCESS)
164
        postFragmentHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
165
      }
166

    
167
    DistortedProgram postProgram = new DistortedProgram(postVertexStream,postFragmentStream, "", postFragmentHeader);
168
    postProgramH = postProgram.getProgramHandle();
169
    GLES20.glUseProgram(postProgramH);
170
    postProgram.bindAndEnableAttributes();
171
    mPostProgramAttributes = postProgram.getAttributes();
172

    
173
    EffectQueuePostprocess.getUniforms(postProgramH);
174

    
175
    DistortedTree.reset();
176
    EffectMessageSender.startSending();
177
    }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180
/**
181
 * Call this so that the Library can release its internal data structures.
182
 * Must be called from Activity.onDestroy(). 
183
 */
184
  public static void onDestroy()
185
    {
186
    DistortedTexture.onDestroy();
187
    DistortedFramebuffer.onDestroy();
188
    DistortedTree.onDestroy();
189
    EffectQueue.onDestroy();
190
    DistortedEffects.onDestroy();
191
    EffectMessageSender.stopSending();
192

    
193
    mMainProgramAttributes = null;
194
    mPostProgramAttributes = null;
195
    }
196
  }
(1-1/16)