Project

General

Profile

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

library / src / main / java / org / distorted / library / Distorted.java @ a8162df9

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