Project

General

Profile

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

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

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