Project

General

Profile

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

library / src / main / java / org / distorted / library / Distorted.java @ 05ecc6fe

1 d333eb6b 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 6a06a912 Leszek Koltunski
package org.distorted.library;
21
22 30beb34f Leszek Koltunski
import android.app.ActivityManager;
23 7845dc66 Leszek Koltunski
import android.content.Context;
24 30beb34f Leszek Koltunski
import android.content.pm.ConfigurationInfo;
25 d6e94c84 Leszek Koltunski
import android.content.res.Resources;
26 432442f9 Leszek Koltunski
import org.distorted.library.program.*;
27 6a06a912 Leszek Koltunski
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29
/**
30
 * A singleton class used to control various global settings.
31
 */
32
public class Distorted 
33 39cbf9dc Leszek Koltunski
  {
34 94f6d472 Leszek Koltunski
  static int GLSL;
35
  static String GLSL_VERSION;
36
37 6a06a912 Leszek Koltunski
  /**
38 cacc63de Leszek Koltunski
   * When creating an instance of a DistortedTexture from another instance, clone the Bitmap that's
39
   * backing up our DistortedTexture.
40 6a06a912 Leszek Koltunski
   * <p>
41 cacc63de Leszek Koltunski
   * This way we can have two DistortedTextures, both backed up by the same Bitmap, to which we can
42 6a06a912 Leszek Koltunski
   * apply different effects. Used in the copy constructor.
43
   */
44 29a06526 Leszek Koltunski
  public static final int CLONE_SURFACE = 0x1;
45 6a06a912 Leszek Koltunski
  /**
46 cacc63de Leszek Koltunski
   * When creating an instance of a DistortedEffects from another instance, clone the Matrix Effects.
47 6a06a912 Leszek Koltunski
   * <p>
48 cacc63de Leszek Koltunski
   * This way we can have two different DistortedEffects sharing the MATRIX queue.
49 6a06a912 Leszek Koltunski
   */
50 015642fb Leszek Koltunski
  public static final int CLONE_MATRIX = 0x2;
51 6a06a912 Leszek Koltunski
  /**
52 cacc63de Leszek Koltunski
   * When creating an instance of a DistortedEffects from another instance, clone the Vertex Effects.
53 6a06a912 Leszek Koltunski
   * <p>
54 cacc63de Leszek Koltunski
   * This way we can have two different DistortedEffects sharing the VERTEX queue.
55 6a06a912 Leszek Koltunski
   */
56
  public static final int CLONE_VERTEX  = 0x4;
57
  /**
58 cacc63de Leszek Koltunski
   * When creating an instance of a DistortedEffects from another instance, clone the Fragment Effects.
59 6a06a912 Leszek Koltunski
   * <p>
60 cacc63de Leszek Koltunski
   * This way we can have two different DistortedEffects sharing the FRAGMENT queue.
61 6a06a912 Leszek Koltunski
   */
62
  public static final int CLONE_FRAGMENT= 0x8;
63 d6e94c84 Leszek Koltunski
   /**
64
   * When creating an instance of a DistortedEffects from another instance, clone the PostProcess Effects.
65
   * <p>
66
   * This way we can have two different DistortedEffects sharing the POSTPROCESS queue.
67
   */
68
  public static final int CLONE_POSTPROCESS= 0x10;
69 6a06a912 Leszek Koltunski
  /**
70 a09ada4c Leszek Koltunski
   * When creating an instance of a DistortedNode from another instance, clone the children Nodes.
71 6a06a912 Leszek Koltunski
   * <p>
72 cacc63de Leszek Koltunski
   * This is mainly useful for creating many similar sub-trees and rendering then at different places
73
   * on the screen with (optionally) different Effects.
74 6a06a912 Leszek Koltunski
   */
75 d6e94c84 Leszek Koltunski
  public static final int CLONE_CHILDREN= 0x20;
76 b3618cb5 Leszek Koltunski
77 55c14a19 Leszek Koltunski
  private static boolean mInitialized=false;
78 6a06a912 Leszek Koltunski
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80 c638c1b0 Leszek Koltunski
// private: hide this from Javadoc
81 6a06a912 Leszek Koltunski
82
  private Distorted()
83
    {
84
    
85
    }
86
87 421c2728 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
88
89
  static boolean isInitialized()
90
    {
91 55c14a19 Leszek Koltunski
    return mInitialized;
92 421c2728 Leszek Koltunski
    }
93
94 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
95
/**
96
 * When OpenGL context gets created, you need to call this method so that the library can initialise its internal data structures.
97 432442f9 Leszek Koltunski
 * I.e. best called from GLSurfaceView.onCreate().
98 6a06a912 Leszek Koltunski
 * <p>
99 432442f9 Leszek Koltunski
 * Needs to be called from a thread holding the OpenGL context.
100 6a06a912 Leszek Koltunski
 *   
101 015642fb Leszek Koltunski
 * @param context Context of the App using the library - used to open up Resources and read Shader code.
102 6a06a912 Leszek Koltunski
 * @throws FragmentCompilationException
103
 * @throws VertexCompilationException
104
 * @throws VertexUniformsException
105
 * @throws FragmentUniformsException
106
 * @throws LinkingException
107
 */
108 432442f9 Leszek Koltunski
  public static void onCreate(final Context context)
109
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
110 d6e94c84 Leszek Koltunski
    {
111 30beb34f Leszek Koltunski
    final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
112
    final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
113
    android.util.Log.e("DISTORTED", "Using OpenGL ES "+configurationInfo.getGlEsVersion());
114
115
    GLSL = ( (configurationInfo.reqGlEsVersion>>16)>=3 ? 300 : 100 );
116 94f6d472 Leszek Koltunski
    GLSL_VERSION= (GLSL==100 ? "#version 100\n" : "#version 300 es\n");
117
118 d6e94c84 Leszek Koltunski
    final Resources resources = context.getResources();
119 c90b9e01 Leszek Koltunski
    DistortedEffects.createProgram(resources);
120
    EffectQueuePostprocess.createProgram(resources);
121 6a06a912 Leszek Koltunski
    EffectMessageSender.startSending();
122 c90b9e01 Leszek Koltunski
123
    mInitialized = true;
124 6a06a912 Leszek Koltunski
    }
125
126 05ecc6fe Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
127
/**
128
 * Call this so that the Library can release its internal data structures.
129
 * Must be called from Activity.onPause().
130
 */
131
  public static void onPause()
132
    {
133
    DistortedSurface.onPause();
134
    }
135
136 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
137
/**
138
 * Call this so that the Library can release its internal data structures.
139
 * Must be called from Activity.onDestroy(). 
140
 */
141
  public static void onDestroy()
142
    {
143 af4cc5db Leszek Koltunski
    DistortedSurface.onDestroy();
144 a09ada4c Leszek Koltunski
    DistortedNode.onDestroy();
145 7b8086eb Leszek Koltunski
    EffectQueue.onDestroy();
146 421c2728 Leszek Koltunski
    DistortedEffects.onDestroy();
147 0afc143a leszek
    DistortedEffectsPostprocess.onDestroy();
148 efe3d8fe leszek
    DistortedMaster.onDestroy();
149 6a06a912 Leszek Koltunski
    EffectMessageSender.stopSending();
150 f8686932 Leszek Koltunski
151 55c14a19 Leszek Koltunski
    mInitialized = false;
152 6a06a912 Leszek Koltunski
    }
153 39cbf9dc Leszek Koltunski
  }