| 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 |
6a06a912
|
Leszek Koltunski
|
import android.opengl.GLES20;
|
| 25 |
432442f9
|
Leszek Koltunski
|
import org.distorted.library.program.*;
|
| 26 |
6a06a912
|
Leszek Koltunski
|
|
| 27 |
432442f9
|
Leszek Koltunski
|
import java.io.InputStream;
|
| 28 |
6a06a912
|
Leszek Koltunski
|
|
| 29 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 30 |
|
|
/**
|
| 31 |
|
|
* A singleton class used to control various global settings.
|
| 32 |
|
|
*/
|
| 33 |
|
|
public class Distorted
|
| 34 |
39cbf9dc
|
Leszek Koltunski
|
{
|
| 35 |
6a06a912
|
Leszek Koltunski
|
/**
|
| 36 |
cacc63de
|
Leszek Koltunski
|
* When creating an instance of a DistortedTexture (or Tree) from another instance, do not clone anything.
|
| 37 |
6a06a912
|
Leszek Koltunski
|
* Used in the copy constructor.
|
| 38 |
|
|
*/
|
| 39 |
|
|
public static final int CLONE_NOTHING = 0x0;
|
| 40 |
|
|
/**
|
| 41 |
cacc63de
|
Leszek Koltunski
|
* When creating an instance of a DistortedTexture from another instance, clone the Bitmap that's
|
| 42 |
|
|
* backing up our DistortedTexture.
|
| 43 |
6a06a912
|
Leszek Koltunski
|
* <p>
|
| 44 |
cacc63de
|
Leszek Koltunski
|
* This way we can have two DistortedTextures, both backed up by the same Bitmap, to which we can
|
| 45 |
6a06a912
|
Leszek Koltunski
|
* apply different effects. Used in the copy constructor.
|
| 46 |
|
|
*/
|
| 47 |
|
|
public static final int CLONE_BITMAP = 0x1;
|
| 48 |
|
|
/**
|
| 49 |
cacc63de
|
Leszek Koltunski
|
* When creating an instance of a DistortedEffects from another instance, clone the Matrix Effects.
|
| 50 |
6a06a912
|
Leszek Koltunski
|
* <p>
|
| 51 |
cacc63de
|
Leszek Koltunski
|
* This way we can have two different DistortedEffects sharing the MATRIX queue.
|
| 52 |
6a06a912
|
Leszek Koltunski
|
*/
|
| 53 |
015642fb
|
Leszek Koltunski
|
public static final int CLONE_MATRIX = 0x2;
|
| 54 |
6a06a912
|
Leszek Koltunski
|
/**
|
| 55 |
cacc63de
|
Leszek Koltunski
|
* When creating an instance of a DistortedEffects from another instance, clone the Vertex Effects.
|
| 56 |
6a06a912
|
Leszek Koltunski
|
* <p>
|
| 57 |
cacc63de
|
Leszek Koltunski
|
* This way we can have two different DistortedEffects sharing the VERTEX queue.
|
| 58 |
6a06a912
|
Leszek Koltunski
|
*/
|
| 59 |
|
|
public static final int CLONE_VERTEX = 0x4;
|
| 60 |
|
|
/**
|
| 61 |
cacc63de
|
Leszek Koltunski
|
* When creating an instance of a DistortedEffects from another instance, clone the Fragment Effects.
|
| 62 |
6a06a912
|
Leszek Koltunski
|
* <p>
|
| 63 |
cacc63de
|
Leszek Koltunski
|
* This way we can have two different DistortedEffects sharing the FRAGMENT queue.
|
| 64 |
6a06a912
|
Leszek Koltunski
|
*/
|
| 65 |
|
|
public static final int CLONE_FRAGMENT= 0x8;
|
| 66 |
d6e94c84
|
Leszek Koltunski
|
/**
|
| 67 |
|
|
* When creating an instance of a DistortedEffects from another instance, clone the PostProcess Effects.
|
| 68 |
|
|
* <p>
|
| 69 |
|
|
* This way we can have two different DistortedEffects sharing the POSTPROCESS queue.
|
| 70 |
|
|
*/
|
| 71 |
|
|
public static final int CLONE_POSTPROCESS= 0x10;
|
| 72 |
6a06a912
|
Leszek Koltunski
|
/**
|
| 73 |
421c2728
|
Leszek Koltunski
|
* When creating an instance of a DistortedTree from another instance, clone the children Nodes.
|
| 74 |
6a06a912
|
Leszek Koltunski
|
* <p>
|
| 75 |
cacc63de
|
Leszek Koltunski
|
* This is mainly useful for creating many similar sub-trees and rendering then at different places
|
| 76 |
|
|
* on the screen with (optionally) different Effects.
|
| 77 |
6a06a912
|
Leszek Koltunski
|
*/
|
| 78 |
d6e94c84
|
Leszek Koltunski
|
public static final int CLONE_CHILDREN= 0x20;
|
| 79 |
b3618cb5
|
Leszek Koltunski
|
|
| 80 |
55c14a19
|
Leszek Koltunski
|
private static boolean mInitialized=false;
|
| 81 |
6a06a912
|
Leszek Koltunski
|
|
| 82 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 83 |
c638c1b0
|
Leszek Koltunski
|
// private: hide this from Javadoc
|
| 84 |
6a06a912
|
Leszek Koltunski
|
|
| 85 |
|
|
private Distorted()
|
| 86 |
|
|
{
|
| 87 |
|
|
|
| 88 |
|
|
}
|
| 89 |
|
|
|
| 90 |
421c2728
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 91 |
|
|
|
| 92 |
|
|
static boolean isInitialized()
|
| 93 |
|
|
{
|
| 94 |
55c14a19
|
Leszek Koltunski
|
return mInitialized;
|
| 95 |
421c2728
|
Leszek Koltunski
|
}
|
| 96 |
|
|
|
| 97 |
6a06a912
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 98 |
|
|
/**
|
| 99 |
|
|
* When OpenGL context gets created, you need to call this method so that the library can initialise its internal data structures.
|
| 100 |
432442f9
|
Leszek Koltunski
|
* I.e. best called from GLSurfaceView.onCreate().
|
| 101 |
6a06a912
|
Leszek Koltunski
|
* <p>
|
| 102 |
432442f9
|
Leszek Koltunski
|
* Needs to be called from a thread holding the OpenGL context.
|
| 103 |
6a06a912
|
Leszek Koltunski
|
*
|
| 104 |
015642fb
|
Leszek Koltunski
|
* @param context Context of the App using the library - used to open up Resources and read Shader code.
|
| 105 |
6a06a912
|
Leszek Koltunski
|
* @throws FragmentCompilationException
|
| 106 |
|
|
* @throws VertexCompilationException
|
| 107 |
|
|
* @throws VertexUniformsException
|
| 108 |
|
|
* @throws FragmentUniformsException
|
| 109 |
|
|
* @throws LinkingException
|
| 110 |
|
|
*/
|
| 111 |
432442f9
|
Leszek Koltunski
|
public static void onCreate(final Context context)
|
| 112 |
|
|
throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
|
| 113 |
d6e94c84
|
Leszek Koltunski
|
{
|
| 114 |
|
|
final Resources resources = context.getResources();
|
| 115 |
57578636
|
Leszek Koltunski
|
|
| 116 |
d6e94c84
|
Leszek Koltunski
|
final InputStream mainVertexStream = resources.openRawResource(R.raw.main_vertex_shader);
|
| 117 |
|
|
final InputStream mainFragmentStream = resources.openRawResource(R.raw.main_fragment_shader);
|
| 118 |
|
|
|
| 119 |
47d838ca
|
Leszek Koltunski
|
String mainVertexHeader= ("#version 100\n#define NUM_VERTEX " + DistortedEffects.getMaxVertex()+"\n");
|
| 120 |
|
|
|
| 121 |
|
|
for(EffectNames name: EffectNames.values() )
|
| 122 |
|
|
{
|
| 123 |
|
|
if( name.getType()== EffectTypes.VERTEX)
|
| 124 |
|
|
mainVertexHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
|
| 125 |
|
|
}
|
| 126 |
|
|
|
| 127 |
|
|
String mainFragmentHeader= ("#version 100\n#define NUM_FRAGMENT " + DistortedEffects.getMaxFragment()+"\n");
|
| 128 |
|
|
|
| 129 |
|
|
for(EffectNames name: EffectNames.values() )
|
| 130 |
|
|
{
|
| 131 |
|
|
if( name.getType()== EffectTypes.FRAGMENT)
|
| 132 |
|
|
mainFragmentHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
|
| 133 |
|
|
}
|
| 134 |
|
|
|
| 135 |
55c14a19
|
Leszek Koltunski
|
DistortedProgram mainProgram = new DistortedProgram(mainVertexStream,mainFragmentStream, mainVertexHeader, mainFragmentHeader);
|
| 136 |
|
|
int mainProgramH = mainProgram.getProgramHandle();
|
| 137 |
57578636
|
Leszek Koltunski
|
|
| 138 |
6a06a912
|
Leszek Koltunski
|
GLES20.glDepthFunc(GLES20.GL_LEQUAL);
|
| 139 |
|
|
GLES20.glEnable(GLES20.GL_BLEND);
|
| 140 |
|
|
GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
|
| 141 |
0ce6fcef
|
Leszek Koltunski
|
GLES20.glEnable(GLES20.GL_CULL_FACE);
|
| 142 |
|
|
GLES20.glCullFace(GLES20.GL_BACK);
|
| 143 |
5bf698ee
|
Leszek Koltunski
|
GLES20.glFrontFace(GLES20.GL_CW);
|
| 144 |
8c893ffc
|
Leszek Koltunski
|
|
| 145 |
d6e94c84
|
Leszek Koltunski
|
EffectQueueFragment.getUniforms(mainProgramH);
|
| 146 |
|
|
EffectQueueVertex.getUniforms(mainProgramH);
|
| 147 |
|
|
EffectQueueMatrix.getUniforms(mainProgramH);
|
| 148 |
|
|
DistortedTexture.getUniforms(mainProgramH);
|
| 149 |
|
|
|
| 150 |
|
|
final InputStream postVertexStream = resources.openRawResource(R.raw.post_vertex_shader);
|
| 151 |
|
|
final InputStream postFragmentStream = resources.openRawResource(R.raw.post_fragment_shader);
|
| 152 |
|
|
|
| 153 |
47d838ca
|
Leszek Koltunski
|
String postFragmentHeader= ("#version 100\n#define NUM_POSTPROCESS " + DistortedEffects.getMaxPostprocess()+"\n");
|
| 154 |
|
|
|
| 155 |
|
|
for(EffectNames name: EffectNames.values() )
|
| 156 |
|
|
{
|
| 157 |
|
|
if( name.getType()== EffectTypes.POSTPROCESS)
|
| 158 |
|
|
postFragmentHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
|
| 159 |
|
|
}
|
| 160 |
|
|
|
| 161 |
55c14a19
|
Leszek Koltunski
|
DistortedProgram postProgram = new DistortedProgram(postVertexStream,postFragmentStream, "", postFragmentHeader);
|
| 162 |
|
|
int postProgramH = postProgram.getProgramHandle();
|
| 163 |
d6e94c84
|
Leszek Koltunski
|
|
| 164 |
|
|
EffectQueuePostprocess.getUniforms(postProgramH);
|
| 165 |
432442f9
|
Leszek Koltunski
|
|
| 166 |
55c14a19
|
Leszek Koltunski
|
DistortedEffects.setPrograms(mainProgram,postProgram);
|
| 167 |
421c2728
|
Leszek Koltunski
|
DistortedTree.reset();
|
| 168 |
6a06a912
|
Leszek Koltunski
|
EffectMessageSender.startSending();
|
| 169 |
|
|
}
|
| 170 |
|
|
|
| 171 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 172 |
|
|
/**
|
| 173 |
|
|
* Call this so that the Library can release its internal data structures.
|
| 174 |
|
|
* Must be called from Activity.onDestroy().
|
| 175 |
|
|
*/
|
| 176 |
|
|
public static void onDestroy()
|
| 177 |
|
|
{
|
| 178 |
7b8086eb
|
Leszek Koltunski
|
DistortedTexture.onDestroy();
|
| 179 |
|
|
DistortedFramebuffer.onDestroy();
|
| 180 |
421c2728
|
Leszek Koltunski
|
DistortedTree.onDestroy();
|
| 181 |
7b8086eb
|
Leszek Koltunski
|
EffectQueue.onDestroy();
|
| 182 |
421c2728
|
Leszek Koltunski
|
DistortedEffects.onDestroy();
|
| 183 |
6a06a912
|
Leszek Koltunski
|
EffectMessageSender.stopSending();
|
| 184 |
f8686932
|
Leszek Koltunski
|
|
| 185 |
55c14a19
|
Leszek Koltunski
|
mInitialized = false;
|
| 186 |
6a06a912
|
Leszek Koltunski
|
}
|
| 187 |
39cbf9dc
|
Leszek Koltunski
|
}
|