Project

General

Profile

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

library / src / main / java / org / distorted / library / Distorted.java @ 63b6561a

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 7845dc66 Leszek Koltunski
import android.content.Context;
23 d6e94c84 Leszek Koltunski
import android.content.res.Resources;
24 194ab46f Leszek Koltunski
import android.opengl.GLES30;
25 432442f9 Leszek Koltunski
import org.distorted.library.program.*;
26 6a06a912 Leszek Koltunski
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28
/**
29
 * A singleton class used to control various global settings.
30
 */
31
public class Distorted 
32 39cbf9dc Leszek Koltunski
  {
33 6a06a912 Leszek Koltunski
  /**
34 cacc63de Leszek Koltunski
   * When creating an instance of a DistortedTexture (or Tree) from another instance, do not clone anything.
35 6a06a912 Leszek Koltunski
   * Used in the copy constructor.
36
   */
37
  public static final int CLONE_NOTHING = 0x0;
38
  /**
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
    
86
    }
87
88 421c2728 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
89
90
  static boolean isInitialized()
91
    {
92 55c14a19 Leszek Koltunski
    return mInitialized;
93 421c2728 Leszek Koltunski
    }
94
95 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
96
/**
97
 * When OpenGL context gets created, you need to call this method so that the library can initialise its internal data structures.
98 432442f9 Leszek Koltunski
 * I.e. best called from GLSurfaceView.onCreate().
99 6a06a912 Leszek Koltunski
 * <p>
100 432442f9 Leszek Koltunski
 * Needs to be called from a thread holding the OpenGL context.
101 6a06a912 Leszek Koltunski
 *   
102 015642fb Leszek Koltunski
 * @param context Context of the App using the library - used to open up Resources and read Shader code.
103 6a06a912 Leszek Koltunski
 * @throws FragmentCompilationException
104
 * @throws VertexCompilationException
105
 * @throws VertexUniformsException
106
 * @throws FragmentUniformsException
107
 * @throws LinkingException
108
 */
109 432442f9 Leszek Koltunski
  public static void onCreate(final Context context)
110
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
111 d6e94c84 Leszek Koltunski
    {
112
    final Resources resources = context.getResources();
113 57578636 Leszek Koltunski
114 194ab46f Leszek Koltunski
    GLES30.glDepthFunc(GLES30.GL_LEQUAL);
115
    GLES30.glEnable(GLES30.GL_BLEND);
116
    GLES30.glBlendFunc(GLES30.GL_SRC_ALPHA, GLES30.GL_ONE_MINUS_SRC_ALPHA);
117
    GLES30.glEnable(GLES30.GL_CULL_FACE);
118
    GLES30.glCullFace(GLES30.GL_BACK);
119
    GLES30.glFrontFace(GLES30.GL_CW);
120 8c893ffc Leszek Koltunski
121 c90b9e01 Leszek Koltunski
    DistortedEffects.createProgram(resources);
122
    EffectQueuePostprocess.createProgram(resources);
123 d6e94c84 Leszek Koltunski
124 a09ada4c Leszek Koltunski
    DistortedNode.reset();
125 6a06a912 Leszek Koltunski
    EffectMessageSender.startSending();
126 c90b9e01 Leszek Koltunski
127
    mInitialized = true;
128 6a06a912 Leszek Koltunski
    }
129
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131
/**
132
 * Call this so that the Library can release its internal data structures.
133
 * Must be called from Activity.onDestroy(). 
134
 */
135
  public static void onDestroy()
136
    {
137 af4cc5db Leszek Koltunski
    DistortedSurface.onDestroy();
138 a09ada4c Leszek Koltunski
    DistortedNode.onDestroy();
139 7b8086eb Leszek Koltunski
    EffectQueue.onDestroy();
140 421c2728 Leszek Koltunski
    DistortedEffects.onDestroy();
141 c204c69d leszek
    DistortedAttachDaemon.onDestroy();
142 6a06a912 Leszek Koltunski
    EffectMessageSender.stopSending();
143 f8686932 Leszek Koltunski
144 55c14a19 Leszek Koltunski
    mInitialized = false;
145 6a06a912 Leszek Koltunski
    }
146 39cbf9dc Leszek Koltunski
  }