org.rajawali3d.materials.shaders / AShader

AShader

abstract class AShader : AShaderBase
abstract class AShader : AShaderBase

This class is a wrapper class for shader creation. GLSL shaders are text files that are compiled at runtime. The GLSL language is based on C. This class is like a mini framework that lets you write shaders in Java. The main reason for this is maintainability and code reuse. The lack of operator overloading makes this slightly verbose however. Instead of writing this in GLSL:


  myVar *= myOtherVar;
  
You'll have to write this:

  myVar.assignAdd(myOtherVar);
  
GLSL data types are wrapped into their own classes. Because most of the data type names are reserved keywords in Java they are prefixed with 'R'. For instance:

Shader initialization should be done in the AShader#initialize() method. This is the place where you would create your uniforms, varyings, constanst, etc:

Override
public void initialize() { super.initialize(); muMyVec3Uniform = (RVec3) addUniform("uMyVec3Uniform", DataType.VEC3); maMyVec2Attribute = (RVec2) addAttribute("uMyVec2Attribute", DataType.VEC2); mvMyFloatVarying = (RFloat) addVarying("vMyFloatVarying", DataType.FLOAT); mgMyMat4Global = (RMat4) addGlobal("gMyMat4Global", DataType.MAT4); mcMyIntConstant = (RInt) addConstant("cMyIntConstant", DataType.INT); } All attributes and uniforms needs to get their handles. This is an integer that represents the location of a specific attribute or uniform within a shader program.

Override
public void setLocations(int programHandle) { muMyVec3UniformHandle = getUniformLocation(programHandle, "uMyVec3Uniform"); maMyVec2AttributeHandle = getAttributeLocation(programHandle, "uMyVec2Attribute"); } This handle is subsequently used in ) to set the attribute/uniform value:

Override
public void applyParams() { super.applyParams(); GLES20.glUniform3fv(muMyVec3UniformHandle, 1, myFloatArrayValue, 0); } The shader code that goes into main() in a regular shader goes into AShader#main():

Override
public void main() { // corresponds to GLSL: vec3 myVar = maMyVec3Uniform; RVec3 myVar = new RVec3("myVar"); myVar.assign(maMyVec3Uniform); // corresponds to GLSL: myVar *= 1.0f; myVar.assignMultiply(1.0f); // etc .. }

Author
dennis.ippel