Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1051 | chris | 1 | package com.gebauz.bauzoid.graphics.shader; |
| 2 | |||
| 3 | import com.badlogic.gdx.graphics.Texture; |
||
| 4 | import com.gebauz.bauzoid.graphics.Graphics; |
||
| 5 | import com.gebauz.bauzoid.graphics.GraphicsObject; |
||
| 6 | import com.gebauz.bauzoid.math.Vector2; |
||
| 7 | import com.gebauz.bauzoid.math.Vector3; |
||
| 8 | import com.gebauz.bauzoid.math.Vector4; |
||
| 9 | |||
| 10 | /** An effect containing shaders, variables, render states, techniques. */ |
||
| 11 | public class Effect extends GraphicsObject |
||
| 12 | { |
||
| 13 | //================================= Effect Variables ====================================================== |
||
| 14 | |||
| 15 | /** Effect variable base class. */ |
||
| 16 | public abstract class Variable |
||
| 17 | { |
||
| 18 | public static final int TYPE_TEXTURE2D = 0; |
||
| 19 | public static final int TYPE_TEXTURE3D = 1; // not supported on Android |
||
| 20 | public static final int TYPE_TEXTURECUBE = 2; |
||
| 21 | public static final int TYPE_BOOL = 3; |
||
| 22 | public static final int TYPE_INT = 4; |
||
| 23 | public static final int TYPE_FLOAT = 5; |
||
| 24 | public static final int TYPE_VECTOR2 = 6; |
||
| 25 | public static final int TYPE_VECTOR3 = 7; |
||
| 26 | public static final int TYPE_VECTOR4 = 8; |
||
| 27 | |||
| 28 | private String mName; |
||
| 29 | private int mType; |
||
| 30 | |||
| 31 | /** Constructor. */ |
||
| 32 | public Variable(String name, int type) |
||
| 33 | { |
||
| 34 | mType = type; |
||
| 35 | mName = name; |
||
| 36 | } |
||
| 37 | |||
| 38 | /** Destroy internal resources (if any). */ |
||
| 39 | public void dispose() {} |
||
| 40 | |||
| 41 | /** Obtain name. */ |
||
| 42 | public final String getName() |
||
| 43 | { |
||
| 44 | return mName; |
||
| 45 | } |
||
| 46 | |||
| 47 | /** Apply to uniform handle. */ |
||
| 48 | public abstract void apply(ShaderUniform uniform); |
||
| 49 | |||
| 50 | /** Return type of Variable. */ |
||
| 51 | public final int getType() |
||
| 52 | { |
||
| 53 | return mType; |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | /** Effect variable for texture. */ |
||
| 58 | public class VariableTexture extends Variable |
||
| 59 | { |
||
| 60 | private Texture mTexture = null; |
||
| 61 | private int mTextureStage = 0; |
||
| 62 | |||
| 63 | /** Constructor. */ |
||
| 64 | public VariableTexture(String name, Texture texture) |
||
| 65 | { |
||
| 66 | super(name, TYPE_TEXTURE2D); |
||
| 67 | mTexture = texture; |
||
| 68 | } |
||
| 69 | |||
| 70 | /** Destroy internal texture. */ |
||
| 71 | public void dispose() |
||
| 72 | { |
||
| 73 | if (mTexture != null) |
||
| 74 | { |
||
| 75 | mTexture.dispose(); |
||
| 76 | mTexture = null; |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | /** Apply to shader uniform. */ |
||
| 81 | public void apply(ShaderUniform uniform) |
||
| 82 | { |
||
| 83 | uniform.set(mTextureStage); |
||
| 84 | getRenderStates().getTextureStage(mTextureStage).bindTexture(mTexture); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** Set the texture stage used. */ |
||
| 88 | public final void setTextureStage(int stage) |
||
| 89 | { |
||
| 90 | mTextureStage = stage; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | /** Effect variable for bool value. */ |
||
| 95 | public class VariableBool extends Variable |
||
| 96 | { |
||
| 97 | private boolean mValue = false; |
||
| 98 | |||
| 99 | public VariableBool(String name, boolean value) |
||
| 100 | { |
||
| 101 | super(name, TYPE_BOOL); |
||
| 102 | mValue = value; |
||
| 103 | } |
||
| 104 | |||
| 105 | public void apply(ShaderUniform uniform) |
||
| 106 | { |
||
| 107 | uniform.set(mValue); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | /** Effect variable for integer value. */ |
||
| 112 | public class VariableInt extends Variable |
||
| 113 | { |
||
| 114 | private int mValue = 0; |
||
| 115 | |||
| 116 | public VariableInt(String name, int value) |
||
| 117 | { |
||
| 118 | super(name, TYPE_INT); |
||
| 119 | mValue = value; |
||
| 120 | } |
||
| 121 | |||
| 122 | public void apply(ShaderUniform uniform) |
||
| 123 | { |
||
| 124 | uniform.set(mValue); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | /** Effect variable for float value. */ |
||
| 129 | public class VariableFloat extends Variable |
||
| 130 | { |
||
| 131 | private float mValue = 0.0f; |
||
| 132 | |||
| 133 | public VariableFloat(String name, float value) |
||
| 134 | { |
||
| 135 | super(name, TYPE_FLOAT); |
||
| 136 | mValue = value; |
||
| 137 | } |
||
| 138 | |||
| 139 | public void apply(ShaderUniform uniform) |
||
| 140 | { |
||
| 141 | uniform.set(mValue); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | /** Effect variable for Vector2 value. */ |
||
| 146 | public class VariableVector2 extends Variable |
||
| 147 | { |
||
| 148 | private Vector2 mValue; |
||
| 149 | |||
| 150 | public VariableVector2(String name, Vector2 v2) |
||
| 151 | { |
||
| 152 | super(name, TYPE_VECTOR2); |
||
| 153 | mValue = v2; |
||
| 154 | } |
||
| 155 | |||
| 156 | public void apply(ShaderUniform uniform) |
||
| 157 | { |
||
| 158 | uniform.set(mValue); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | /** Effect variable for Vector3 value. */ |
||
| 163 | public class VariableVector3 extends Variable |
||
| 164 | { |
||
| 165 | private Vector3 mValue; |
||
| 166 | |||
| 167 | public VariableVector3(String name, Vector3 value) |
||
| 168 | { |
||
| 169 | super(name, TYPE_VECTOR3); |
||
| 170 | mValue = value; |
||
| 171 | } |
||
| 172 | |||
| 173 | public void apply(ShaderUniform uniform) |
||
| 174 | { |
||
| 175 | uniform.set(mValue); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | /** Effect variable for Vector4 value. */ |
||
| 180 | public class VariableVector4 extends Variable |
||
| 181 | { |
||
| 182 | private Vector4 mValue; |
||
| 183 | |||
| 184 | public VariableVector4(String name, Vector4 value) |
||
| 185 | { |
||
| 186 | super(name, TYPE_VECTOR4); |
||
| 187 | mValue = value; |
||
| 188 | } |
||
| 189 | |||
| 190 | public void apply(ShaderUniform uniform) |
||
| 191 | { |
||
| 192 | uniform.set(mValue); |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | //================================= Effect class implementation ====================================================== |
||
| 197 | |||
| 198 | private Technique[] mTechniques = null; |
||
| 199 | private Variable[] mVariables = null; |
||
| 200 | |||
| 201 | /** Constructor. */ |
||
| 202 | public Effect(Graphics graphics, String name) |
||
| 203 | { |
||
| 204 | super(graphics); |
||
| 205 | |||
| 206 | } |
||
| 207 | |||
| 208 | /** Dispose techniques. */ |
||
| 209 | public void dispose() |
||
| 210 | { |
||
| 211 | clearTechniques(); |
||
| 212 | clearVariables(); |
||
| 213 | } |
||
| 214 | |||
| 215 | |||
| 216 | /** Connect variables to techniques after techs/vars have been set. */ |
||
| 217 | public void connectVariables() |
||
| 218 | { |
||
| 219 | if ((mTechniques == null) || (mVariables == null)) |
||
| 220 | return; |
||
| 221 | |||
| 222 | for (Technique tech : mTechniques) |
||
| 223 | { |
||
| 224 | tech.connectVariables(); |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | /** Set the techniques. */ |
||
| 229 | public void setTechniques(Technique[] techniques) |
||
| 230 | { |
||
| 231 | if (techniques == null) |
||
| 232 | clearTechniques(); |
||
| 233 | |||
| 234 | mTechniques = techniques; |
||
| 235 | } |
||
| 236 | |||
| 237 | /** Destroy techniques. */ |
||
| 238 | public void clearTechniques() |
||
| 239 | { |
||
| 240 | if (mTechniques != null) |
||
| 241 | { |
||
| 242 | mTechniques = null; |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | /** Set the variables. */ |
||
| 247 | public void setVariables(Variable[] variables) |
||
| 248 | { |
||
| 249 | if (variables == null) |
||
| 250 | clearVariables(); |
||
| 251 | |||
| 252 | mVariables = variables; |
||
| 253 | } |
||
| 254 | |||
| 255 | /** Destroy variables. */ |
||
| 256 | public void clearVariables() |
||
| 257 | { |
||
| 258 | if (mVariables != null) |
||
| 259 | { |
||
| 260 | for (int i = 0; i < mVariables.length; i++) |
||
| 261 | { |
||
| 262 | if (mVariables[i] != null) |
||
| 263 | mVariables[i].dispose(); |
||
| 264 | } |
||
| 265 | |||
| 266 | mVariables = null; |
||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | /** Get variable by name. */ |
||
| 271 | public Variable getVariable(String name) |
||
| 272 | { |
||
| 273 | if (mVariables == null) |
||
| 274 | return null; |
||
| 275 | |||
| 276 | for (Variable v : mVariables) |
||
| 277 | { |
||
| 278 | if (v.getName().equalsIgnoreCase(name)) |
||
| 279 | return v; |
||
| 280 | } |
||
| 281 | |||
| 282 | return null; |
||
| 283 | } |
||
| 284 | |||
| 285 | /** Get variable by index. */ |
||
| 286 | public Variable getVariable(int index) |
||
| 287 | { |
||
| 288 | if ((mVariables == null) || (index >= mVariables.length)) |
||
| 289 | return null; |
||
| 290 | |||
| 291 | return mVariables[index]; |
||
| 292 | } |
||
| 293 | |||
| 294 | /** Get variable count. */ |
||
| 295 | public int getVariableCount() |
||
| 296 | { |
||
| 297 | if (mVariables == null) |
||
| 298 | return 0; |
||
| 299 | |||
| 300 | return mVariables.length; |
||
| 301 | } |
||
| 302 | |||
| 303 | } |
||
| 304 |