Project

General

Profile

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

library / src / main / java / org / distorted / library / Distorted.java @ 43fbf0dd

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 from another instance, clone the Bitmap that's
35
   * backing up our DistortedTexture.
36 6a06a912 Leszek Koltunski
   * <p>
37 cacc63de Leszek Koltunski
   * This way we can have two DistortedTextures, both backed up by the same Bitmap, to which we can
38 6a06a912 Leszek Koltunski
   * apply different effects. Used in the copy constructor.
39
   */
40 29a06526 Leszek Koltunski
  public static final int CLONE_SURFACE = 0x1;
41 6a06a912 Leszek Koltunski
  /**
42 cacc63de Leszek Koltunski
   * When creating an instance of a DistortedEffects from another instance, clone the Matrix Effects.
43 6a06a912 Leszek Koltunski
   * <p>
44 cacc63de Leszek Koltunski
   * This way we can have two different DistortedEffects sharing the MATRIX queue.
45 6a06a912 Leszek Koltunski
   */
46 015642fb Leszek Koltunski
  public static final int CLONE_MATRIX = 0x2;
47 6a06a912 Leszek Koltunski
  /**
48 cacc63de Leszek Koltunski
   * When creating an instance of a DistortedEffects from another instance, clone the Vertex Effects.
49 6a06a912 Leszek Koltunski
   * <p>
50 cacc63de Leszek Koltunski
   * This way we can have two different DistortedEffects sharing the VERTEX queue.
51 6a06a912 Leszek Koltunski
   */
52
  public static final int CLONE_VERTEX  = 0x4;
53
  /**
54 cacc63de Leszek Koltunski
   * When creating an instance of a DistortedEffects from another instance, clone the Fragment Effects.
55 6a06a912 Leszek Koltunski
   * <p>
56 cacc63de Leszek Koltunski
   * This way we can have two different DistortedEffects sharing the FRAGMENT queue.
57 6a06a912 Leszek Koltunski
   */
58
  public static final int CLONE_FRAGMENT= 0x8;
59 d6e94c84 Leszek Koltunski
   /**
60
   * When creating an instance of a DistortedEffects from another instance, clone the PostProcess Effects.
61
   * <p>
62
   * This way we can have two different DistortedEffects sharing the POSTPROCESS queue.
63
   */
64
  public static final int CLONE_POSTPROCESS= 0x10;
65 6a06a912 Leszek Koltunski
  /**
66 a09ada4c Leszek Koltunski
   * When creating an instance of a DistortedNode from another instance, clone the children Nodes.
67 6a06a912 Leszek Koltunski
   * <p>
68 cacc63de Leszek Koltunski
   * This is mainly useful for creating many similar sub-trees and rendering then at different places
69
   * on the screen with (optionally) different Effects.
70 6a06a912 Leszek Koltunski
   */
71 d6e94c84 Leszek Koltunski
  public static final int CLONE_CHILDREN= 0x20;
72 b3618cb5 Leszek Koltunski
73 55c14a19 Leszek Koltunski
  private static boolean mInitialized=false;
74 6a06a912 Leszek Koltunski
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76 c638c1b0 Leszek Koltunski
// private: hide this from Javadoc
77 6a06a912 Leszek Koltunski
78
  private Distorted()
79
    {
80
    
81
    }
82
83 421c2728 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
84
85
  static boolean isInitialized()
86
    {
87 55c14a19 Leszek Koltunski
    return mInitialized;
88 421c2728 Leszek Koltunski
    }
89
90 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
91
/**
92
 * When OpenGL context gets created, you need to call this method so that the library can initialise its internal data structures.
93 432442f9 Leszek Koltunski
 * I.e. best called from GLSurfaceView.onCreate().
94 6a06a912 Leszek Koltunski
 * <p>
95 432442f9 Leszek Koltunski
 * Needs to be called from a thread holding the OpenGL context.
96 6a06a912 Leszek Koltunski
 *   
97 015642fb Leszek Koltunski
 * @param context Context of the App using the library - used to open up Resources and read Shader code.
98 6a06a912 Leszek Koltunski
 * @throws FragmentCompilationException
99
 * @throws VertexCompilationException
100
 * @throws VertexUniformsException
101
 * @throws FragmentUniformsException
102
 * @throws LinkingException
103
 */
104 432442f9 Leszek Koltunski
  public static void onCreate(final Context context)
105
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
106 d6e94c84 Leszek Koltunski
    {
107
    final Resources resources = context.getResources();
108 57578636 Leszek Koltunski
109 194ab46f Leszek Koltunski
    GLES30.glDepthFunc(GLES30.GL_LEQUAL);
110
    GLES30.glEnable(GLES30.GL_BLEND);
111
    GLES30.glBlendFunc(GLES30.GL_SRC_ALPHA, GLES30.GL_ONE_MINUS_SRC_ALPHA);
112
    GLES30.glEnable(GLES30.GL_CULL_FACE);
113
    GLES30.glCullFace(GLES30.GL_BACK);
114
    GLES30.glFrontFace(GLES30.GL_CW);
115 8c893ffc Leszek Koltunski
116 c90b9e01 Leszek Koltunski
    DistortedEffects.createProgram(resources);
117
    EffectQueuePostprocess.createProgram(resources);
118 d6e94c84 Leszek Koltunski
119 6a06a912 Leszek Koltunski
    EffectMessageSender.startSending();
120 c90b9e01 Leszek Koltunski
121
    mInitialized = true;
122 6a06a912 Leszek Koltunski
    }
123
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125
/**
126
 * Call this so that the Library can release its internal data structures.
127
 * Must be called from Activity.onDestroy(). 
128
 */
129
  public static void onDestroy()
130
    {
131 af4cc5db Leszek Koltunski
    DistortedSurface.onDestroy();
132 a09ada4c Leszek Koltunski
    DistortedNode.onDestroy();
133 7b8086eb Leszek Koltunski
    EffectQueue.onDestroy();
134 421c2728 Leszek Koltunski
    DistortedEffects.onDestroy();
135 c204c69d leszek
    DistortedAttachDaemon.onDestroy();
136 6a06a912 Leszek Koltunski
    EffectMessageSender.stopSending();
137 f8686932 Leszek Koltunski
138 55c14a19 Leszek Koltunski
    mInitialized = false;
139 6a06a912 Leszek Koltunski
    }
140 39cbf9dc Leszek Koltunski
  }