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
|
private static boolean mInitialized = false;
|
74
|
|
75
|
static int[] mAttributes;
|
76
|
|
77
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
78
|
|
79
|
private Distorted()
|
80
|
{
|
81
|
|
82
|
}
|
83
|
|
84
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
85
|
|
86
|
static boolean isInitialized()
|
87
|
{
|
88
|
return mInitialized;
|
89
|
}
|
90
|
|
91
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
92
|
/**
|
93
|
* When OpenGL context gets created, you need to call this method so that the library can initialise its internal data structures.
|
94
|
* I.e. best called from GLSurfaceView.onCreate().
|
95
|
* <p>
|
96
|
* Needs to be called from a thread holding the OpenGL context.
|
97
|
*
|
98
|
* @param context Context of the App using the library - used to open up Resources and read Shader code.
|
99
|
* @throws FragmentCompilationException
|
100
|
* @throws VertexCompilationException
|
101
|
* @throws VertexUniformsException
|
102
|
* @throws FragmentUniformsException
|
103
|
* @throws LinkingException
|
104
|
*/
|
105
|
public static void onCreate(final Context context)
|
106
|
throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
|
107
|
{
|
108
|
mInitialized = true;
|
109
|
|
110
|
final InputStream vertexStream = context.getResources().openRawResource(R.raw.main_vertex_shader);
|
111
|
final InputStream fragmentStream = context.getResources().openRawResource(R.raw.main_fragment_shader);
|
112
|
|
113
|
DistortedProgram mainProgram = new DistortedProgram(vertexStream,fragmentStream);
|
114
|
int programH = mainProgram.getProgramHandle();
|
115
|
GLES20.glUseProgram(programH);
|
116
|
mainProgram.bindAndEnableAttributes();
|
117
|
mAttributes = mainProgram.getAttributes();
|
118
|
|
119
|
int textureUniformH = GLES20.glGetUniformLocation(programH, "u_Texture");
|
120
|
|
121
|
GLES20.glEnable (GLES20.GL_DEPTH_TEST);
|
122
|
GLES20.glDepthFunc(GLES20.GL_LEQUAL);
|
123
|
GLES20.glEnable(GLES20.GL_BLEND);
|
124
|
GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
|
125
|
GLES20.glEnable(GLES20.GL_CULL_FACE);
|
126
|
GLES20.glCullFace(GLES20.GL_BACK);
|
127
|
GLES20.glFrontFace(GLES20.GL_CW);
|
128
|
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
|
129
|
GLES20.glUniform1i(textureUniformH, 0);
|
130
|
|
131
|
EffectQueueFragment.getUniforms(programH);
|
132
|
EffectQueueVertex.getUniforms(programH);
|
133
|
EffectQueueMatrix.getUniforms(programH);
|
134
|
|
135
|
DistortedTree.reset();
|
136
|
EffectMessageSender.startSending();
|
137
|
}
|
138
|
|
139
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
140
|
/**
|
141
|
* Call this so that the Library can release its internal data structures.
|
142
|
* Must be called from Activity.onDestroy().
|
143
|
*/
|
144
|
public static void onDestroy()
|
145
|
{
|
146
|
DistortedTexture.onDestroy();
|
147
|
DistortedFramebuffer.onDestroy();
|
148
|
DistortedTree.onDestroy();
|
149
|
EffectQueue.onDestroy();
|
150
|
DistortedEffects.onDestroy();
|
151
|
EffectMessageSender.stopSending();
|
152
|
|
153
|
mInitialized = false;
|
154
|
}
|
155
|
}
|