Project

General

Profile

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

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

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