Project

General

Profile

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

library / src / main / java / org / distorted / library / Distorted.java @ 8c327653

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
package org.distorted.library;
21

    
22
import android.content.Context;
23
import android.opengl.GLES20;
24
import org.distorted.library.program.*;
25

    
26
import java.io.InputStream;
27

    
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29
/**
30
 * A singleton class used to control various global settings.
31
 */
32
public class Distorted 
33
  {
34
  /**
35
   * When creating an instance of a DistortedTexture (or Tree) from another instance, do not clone anything.
36
   * Used in the copy constructor.
37
   */
38
  public static final int CLONE_NOTHING = 0x0;
39
  /**
40
   * When creating an instance of a DistortedTexture from another instance, clone the Bitmap that's
41
   * backing up our DistortedTexture.
42
   * <p>
43
   * This way we can have two DistortedTextures, both backed up by the same Bitmap, to which we can
44
   * apply different effects. Used in the copy constructor.
45
   */
46
  public static final int CLONE_BITMAP  = 0x1;
47
  /**
48
   * When creating an instance of a DistortedEffects from another instance, clone the Matrix Effects.
49
   * <p>
50
   * This way we can have two different DistortedEffects sharing the MATRIX queue.
51
   */
52
  public static final int CLONE_MATRIX = 0x2;
53
  /**
54
   * When creating an instance of a DistortedEffects from another instance, clone the Vertex Effects.
55
   * <p>
56
   * This way we can have two different DistortedEffects sharing the VERTEX queue.
57
   */
58
  public static final int CLONE_VERTEX  = 0x4;
59
  /**
60
   * When creating an instance of a DistortedEffects from another instance, clone the Fragment Effects.
61
   * <p>
62
   * This way we can have two different DistortedEffects sharing the FRAGMENT queue.
63
   */
64
  public static final int CLONE_FRAGMENT= 0x8;
65
  /**
66
   * When creating an instance of a DistortedTree from another instance, clone the children Nodes.
67
   * <p>
68
   * 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
   */
71
  public static final int CLONE_CHILDREN= 0x10;
72

    
73
  static int[] mMainProgramAttributes;
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

    
77
  private Distorted()
78
    {
79
    
80
    }
81

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83

    
84
  static boolean isInitialized()
85
    {
86
    return (mMainProgramAttributes!=null);
87
    }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90
/**
91
 * When OpenGL context gets created, you need to call this method so that the library can initialise its internal data structures.
92
 * I.e. best called from GLSurfaceView.onCreate().
93
 * <p>
94
 * Needs to be called from a thread holding the OpenGL context.
95
 *   
96
 * @param context Context of the App using the library - used to open up Resources and read Shader code.
97
 * @throws FragmentCompilationException
98
 * @throws VertexCompilationException
99
 * @throws VertexUniformsException
100
 * @throws FragmentUniformsException
101
 * @throws LinkingException
102
 */
103
  public static void onCreate(final Context context)
104
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
105
    { 
106
    final InputStream vertexStream   = context.getResources().openRawResource(R.raw.main_vertex_shader);
107
    final InputStream fragmentStream = context.getResources().openRawResource(R.raw.main_fragment_shader);
108

    
109
    DistortedProgram mainProgram = new DistortedProgram(vertexStream,fragmentStream);
110
    int programH = mainProgram.getProgramHandle();
111
    GLES20.glUseProgram(programH);
112
    mainProgram.bindAndEnableAttributes();
113
    mMainProgramAttributes = mainProgram.getAttributes();
114

    
115
    GLES20.glDepthFunc(GLES20.GL_LEQUAL);
116
    GLES20.glEnable(GLES20.GL_BLEND);
117
    GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
118
    GLES20.glEnable(GLES20.GL_CULL_FACE);
119
    GLES20.glCullFace(GLES20.GL_BACK);
120
    GLES20.glFrontFace(GLES20.GL_CW);
121

    
122
    EffectQueueFragment.getUniforms(programH);
123
    EffectQueueVertex.getUniforms(programH);
124
    EffectQueueMatrix.getUniforms(programH);
125
    DistortedTexture.getUniforms(programH);
126

    
127
    DistortedTree.reset();
128
    EffectMessageSender.startSending();
129
    }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
/**
133
 * Call this so that the Library can release its internal data structures.
134
 * Must be called from Activity.onDestroy(). 
135
 */
136
  public static void onDestroy()
137
    {
138
    DistortedTexture.onDestroy();
139
    DistortedFramebuffer.onDestroy();
140
    DistortedTree.onDestroy();
141
    EffectQueue.onDestroy();
142
    DistortedEffects.onDestroy();
143
    EffectMessageSender.stopSending();
144

    
145
    mMainProgramAttributes = null;
146
    }
147
  }
(1-1/16)