| 1 |
8eccf334
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 2 |
|
|
// Copyright 2017 Leszek Koltunski //
|
| 3 |
|
|
// //
|
| 4 |
46b572b5
|
Leszek Koltunski
|
// This file is part of Distorted. //
|
| 5 |
8eccf334
|
Leszek Koltunski
|
// //
|
| 6 |
46b572b5
|
Leszek Koltunski
|
// Distorted is free software: you can redistribute it and/or modify //
|
| 7 |
8eccf334
|
Leszek Koltunski
|
// 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 |
46b572b5
|
Leszek Koltunski
|
// Distorted is distributed in the hope that it will be useful, //
|
| 12 |
8eccf334
|
Leszek Koltunski
|
// 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 |
46b572b5
|
Leszek Koltunski
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
| 18 |
8eccf334
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 19 |
|
|
|
| 20 |
|
|
package org.distorted.library.effect;
|
| 21 |
|
|
|
| 22 |
dd89c7f4
|
Leszek Koltunski
|
import org.distorted.library.type.Static4D;
|
| 23 |
|
|
|
| 24 |
f046b159
|
Leszek Koltunski
|
import java.lang.reflect.Method;
|
| 25 |
|
|
|
| 26 |
8eccf334
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 27 |
faa3ff56
|
Leszek Koltunski
|
/**
|
| 28 |
a20f274f
|
Leszek Koltunski
|
* Abstract class that represents an Effect that works by injecting certain code into the main Vertex shader.
|
| 29 |
faa3ff56
|
Leszek Koltunski
|
*/
|
| 30 |
8eccf334
|
Leszek Koltunski
|
public abstract class VertexEffect extends Effect
|
| 31 |
|
|
{
|
| 32 |
faa3ff56
|
Leszek Koltunski
|
/**
|
| 33 |
|
|
* 12: 5 per-effect interpolated values, 3-dimensional center, 4-dimensional Region
|
| 34 |
|
|
*/
|
| 35 |
|
|
public static final int NUM_UNIFORMS = 12;
|
| 36 |
b24e4719
|
Leszek Koltunski
|
static final int VALUES_OFFSET = 0;
|
| 37 |
|
|
static final int CENTER_OFFSET = 5;
|
| 38 |
|
|
static final int REGION_OFFSET = 8;
|
| 39 |
7cd24173
|
leszek
|
private static String mGLSL = "";
|
| 40 |
|
|
private static int mNumEnabled = 0;
|
| 41 |
15aa7d94
|
Leszek Koltunski
|
|
| 42 |
f046b159
|
Leszek Koltunski
|
private static String mFullGLSL = "";
|
| 43 |
|
|
private static int mFullEnabled = 0;
|
| 44 |
|
|
private static boolean mFullPrepared = false;
|
| 45 |
|
|
|
| 46 |
dd89c7f4
|
Leszek Koltunski
|
final static Static4D MAX_REGION = new Static4D(0,0,0,1000000);
|
| 47 |
|
|
|
| 48 |
b547aaba
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 49 |
|
|
|
| 50 |
a0d5e302
|
Leszek Koltunski
|
VertexEffect(EffectName name)
|
| 51 |
b547aaba
|
leszek
|
{
|
| 52 |
9d0d8530
|
leszek
|
super(name);
|
| 53 |
b547aaba
|
leszek
|
}
|
| 54 |
7cd24173
|
leszek
|
|
| 55 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 56 |
|
|
|
| 57 |
f046b159
|
Leszek Koltunski
|
private static String retSection(int effect, String code)
|
| 58 |
7cd24173
|
leszek
|
{
|
| 59 |
f046b159
|
Leszek Koltunski
|
return
|
| 60 |
7cd24173
|
leszek
|
|
| 61 |
9af837e8
|
leszek
|
"if( vName[i]=="+effect+")\n" +
|
| 62 |
7cd24173
|
leszek
|
"{\n" +
|
| 63 |
|
|
code +"\n" +
|
| 64 |
|
|
"}\n" +
|
| 65 |
|
|
"else\n";
|
| 66 |
|
|
}
|
| 67 |
|
|
|
| 68 |
f046b159
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 69 |
|
|
// prepare the code to be injected into the Full program, i.e. code of ALL vertex effects.
|
| 70 |
|
|
|
| 71 |
|
|
private static void prepareFull()
|
| 72 |
|
|
{
|
| 73 |
|
|
Method method;
|
| 74 |
|
|
|
| 75 |
|
|
for(EffectName name: EffectName.values())
|
| 76 |
|
|
{
|
| 77 |
|
|
if( name.getType() == EffectType.VERTEX )
|
| 78 |
|
|
{
|
| 79 |
|
|
Class<? extends Effect> cls = name.getEffectClass();
|
| 80 |
|
|
|
| 81 |
|
|
try
|
| 82 |
|
|
{
|
| 83 |
|
|
method = cls.getDeclaredMethod("code");
|
| 84 |
|
|
}
|
| 85 |
|
|
catch(NoSuchMethodException ex)
|
| 86 |
|
|
{
|
| 87 |
|
|
android.util.Log.e("Effect", "exception getting method: "+ex.getMessage());
|
| 88 |
|
|
method = null;
|
| 89 |
|
|
}
|
| 90 |
|
|
|
| 91 |
|
|
try
|
| 92 |
|
|
{
|
| 93 |
|
|
if( method!=null )
|
| 94 |
|
|
{
|
| 95 |
|
|
Object value = method.invoke(null);
|
| 96 |
|
|
String code = (String)value;
|
| 97 |
|
|
mFullGLSL += retSection(name.ordinal(),code);
|
| 98 |
|
|
mFullEnabled++;
|
| 99 |
|
|
}
|
| 100 |
|
|
}
|
| 101 |
|
|
catch(Exception ex)
|
| 102 |
|
|
{
|
| 103 |
|
|
android.util.Log.e("Effect", "exception invoking method: "+ex.getMessage());
|
| 104 |
|
|
}
|
| 105 |
|
|
}
|
| 106 |
|
|
}
|
| 107 |
|
|
}
|
| 108 |
|
|
|
| 109 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 110 |
|
|
// prepare code to be injected into the 'main_vertex_shader' main() function.
|
| 111 |
|
|
|
| 112 |
|
|
static void addEffect(EffectName name, String code)
|
| 113 |
|
|
{
|
| 114 |
|
|
int effect = name.ordinal();
|
| 115 |
|
|
|
| 116 |
|
|
if( !mEnabled[effect] )
|
| 117 |
|
|
{
|
| 118 |
|
|
mEnabled[effect] = true;
|
| 119 |
|
|
mNumEnabled ++;
|
| 120 |
|
|
mGLSL += retSection(effect,code);
|
| 121 |
|
|
}
|
| 122 |
|
|
}
|
| 123 |
|
|
|
| 124 |
7cd24173
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 125 |
faa3ff56
|
Leszek Koltunski
|
/**
|
| 126 |
|
|
* Only for use by the library itself.
|
| 127 |
|
|
*
|
| 128 |
|
|
* @y.exclude
|
| 129 |
|
|
*/
|
| 130 |
7cd24173
|
leszek
|
public static String getGLSL()
|
| 131 |
|
|
{
|
| 132 |
|
|
return mGLSL + "{}";
|
| 133 |
|
|
}
|
| 134 |
|
|
|
| 135 |
f046b159
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 136 |
|
|
/**
|
| 137 |
|
|
* Only for use by the library itself.
|
| 138 |
|
|
*
|
| 139 |
|
|
* @y.exclude
|
| 140 |
|
|
*/
|
| 141 |
|
|
public static String getAllGLSL()
|
| 142 |
|
|
{
|
| 143 |
|
|
if( !mFullPrepared )
|
| 144 |
|
|
{
|
| 145 |
|
|
prepareFull();
|
| 146 |
|
|
mFullPrepared = true;
|
| 147 |
|
|
}
|
| 148 |
|
|
|
| 149 |
|
|
return mFullGLSL + "{}";
|
| 150 |
|
|
}
|
| 151 |
|
|
|
| 152 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 153 |
|
|
/**
|
| 154 |
|
|
* Only for use by the library itself.
|
| 155 |
|
|
*
|
| 156 |
|
|
* @y.exclude
|
| 157 |
|
|
*/
|
| 158 |
|
|
public static int getAllEnabled()
|
| 159 |
|
|
{
|
| 160 |
|
|
if( !mFullPrepared )
|
| 161 |
|
|
{
|
| 162 |
|
|
prepareFull();
|
| 163 |
|
|
mFullPrepared = true;
|
| 164 |
|
|
}
|
| 165 |
|
|
|
| 166 |
|
|
return mFullEnabled;
|
| 167 |
|
|
}
|
| 168 |
|
|
|
| 169 |
7cd24173
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 170 |
2f1f7570
|
Leszek Koltunski
|
|
| 171 |
|
|
static void destroyStatics()
|
| 172 |
7cd24173
|
leszek
|
{
|
| 173 |
faa3ff56
|
Leszek Koltunski
|
mNumEnabled = 0;
|
| 174 |
|
|
mGLSL = "";
|
| 175 |
f046b159
|
Leszek Koltunski
|
mFullEnabled= 0;
|
| 176 |
|
|
mFullGLSL = "";
|
| 177 |
|
|
mFullPrepared = false;
|
| 178 |
7cd24173
|
leszek
|
}
|
| 179 |
|
|
|
| 180 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 181 |
faa3ff56
|
Leszek Koltunski
|
// PUBLIC API
|
| 182 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 183 |
|
|
/**
|
| 184 |
6b816678
|
Leszek Koltunski
|
* Return the number of Vertex effects enabled.
|
| 185 |
faa3ff56
|
Leszek Koltunski
|
*/
|
| 186 |
|
|
public static int getNumEnabled()
|
| 187 |
7cd24173
|
leszek
|
{
|
| 188 |
faa3ff56
|
Leszek Koltunski
|
return mNumEnabled;
|
| 189 |
7cd24173
|
leszek
|
}
|
| 190 |
8eccf334
|
Leszek Koltunski
|
}
|